1 /* crypto/aes/aes_ige.c */
2 /* ====================================================================
3 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
54 #include <openssl/aes.h>
57 #define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long))
59 unsigned long data
[N_WORDS
];
62 /* XXX: probably some better way to do this */
63 #if defined(__i386__) || defined(__x86_64__)
64 # define UNALIGNED_MEMOPS_ARE_FAST 1
66 # define UNALIGNED_MEMOPS_ARE_FAST 0
69 #if UNALIGNED_MEMOPS_ARE_FAST
70 # define load_block(d, s) (d) = *(const aes_block_t *)(s)
71 # define store_block(d, s) *(aes_block_t *)(d) = (s)
73 # define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE)
74 # define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE)
77 /* N.B. The IV for this mode is _twice_ the block size */
79 void AES_ige_encrypt(const unsigned char *in
, unsigned char *out
,
80 size_t length
, const AES_KEY
*key
,
81 unsigned char *ivec
, const int enc
)
86 OPENSSL_assert(in
&& out
&& key
&& ivec
);
87 OPENSSL_assert((AES_ENCRYPT
== enc
) || (AES_DECRYPT
== enc
));
88 OPENSSL_assert((length
% AES_BLOCK_SIZE
) == 0);
90 len
= length
/ AES_BLOCK_SIZE
;
92 if (AES_ENCRYPT
== enc
) {
94 (UNALIGNED_MEMOPS_ARE_FAST
95 || ((size_t)in
| (size_t)out
| (size_t)ivec
) % sizeof(long) ==
97 aes_block_t
*ivp
= (aes_block_t
*) ivec
;
98 aes_block_t
*iv2p
= (aes_block_t
*) (ivec
+ AES_BLOCK_SIZE
);
101 aes_block_t
*inp
= (aes_block_t
*) in
;
102 aes_block_t
*outp
= (aes_block_t
*) out
;
104 for (n
= 0; n
< N_WORDS
; ++n
)
105 outp
->data
[n
] = inp
->data
[n
] ^ ivp
->data
[n
];
106 AES_encrypt((unsigned char *)outp
->data
,
107 (unsigned char *)outp
->data
, key
);
108 for (n
= 0; n
< N_WORDS
; ++n
)
109 outp
->data
[n
] ^= iv2p
->data
[n
];
113 in
+= AES_BLOCK_SIZE
;
114 out
+= AES_BLOCK_SIZE
;
116 memcpy(ivec
, ivp
->data
, AES_BLOCK_SIZE
);
117 memcpy(ivec
+ AES_BLOCK_SIZE
, iv2p
->data
, AES_BLOCK_SIZE
);
119 aes_block_t tmp
, tmp2
;
123 load_block(iv
, ivec
);
124 load_block(iv2
, ivec
+ AES_BLOCK_SIZE
);
128 for (n
= 0; n
< N_WORDS
; ++n
)
129 tmp2
.data
[n
] = tmp
.data
[n
] ^ iv
.data
[n
];
130 AES_encrypt((unsigned char *)tmp2
.data
,
131 (unsigned char *)tmp2
.data
, key
);
132 for (n
= 0; n
< N_WORDS
; ++n
)
133 tmp2
.data
[n
] ^= iv2
.data
[n
];
134 store_block(out
, tmp2
);
138 in
+= AES_BLOCK_SIZE
;
139 out
+= AES_BLOCK_SIZE
;
141 memcpy(ivec
, iv
.data
, AES_BLOCK_SIZE
);
142 memcpy(ivec
+ AES_BLOCK_SIZE
, iv2
.data
, AES_BLOCK_SIZE
);
146 (UNALIGNED_MEMOPS_ARE_FAST
147 || ((size_t)in
| (size_t)out
| (size_t)ivec
) % sizeof(long) ==
149 aes_block_t
*ivp
= (aes_block_t
*) ivec
;
150 aes_block_t
*iv2p
= (aes_block_t
*) (ivec
+ AES_BLOCK_SIZE
);
154 aes_block_t
*inp
= (aes_block_t
*) in
;
155 aes_block_t
*outp
= (aes_block_t
*) out
;
157 for (n
= 0; n
< N_WORDS
; ++n
)
158 tmp
.data
[n
] = inp
->data
[n
] ^ iv2p
->data
[n
];
159 AES_decrypt((unsigned char *)tmp
.data
,
160 (unsigned char *)outp
->data
, key
);
161 for (n
= 0; n
< N_WORDS
; ++n
)
162 outp
->data
[n
] ^= ivp
->data
[n
];
166 in
+= AES_BLOCK_SIZE
;
167 out
+= AES_BLOCK_SIZE
;
169 memcpy(ivec
, ivp
->data
, AES_BLOCK_SIZE
);
170 memcpy(ivec
+ AES_BLOCK_SIZE
, iv2p
->data
, AES_BLOCK_SIZE
);
172 aes_block_t tmp
, tmp2
;
176 load_block(iv
, ivec
);
177 load_block(iv2
, ivec
+ AES_BLOCK_SIZE
);
182 for (n
= 0; n
< N_WORDS
; ++n
)
183 tmp
.data
[n
] ^= iv2
.data
[n
];
184 AES_decrypt((unsigned char *)tmp
.data
,
185 (unsigned char *)tmp
.data
, key
);
186 for (n
= 0; n
< N_WORDS
; ++n
)
187 tmp
.data
[n
] ^= iv
.data
[n
];
188 store_block(out
, tmp
);
192 in
+= AES_BLOCK_SIZE
;
193 out
+= AES_BLOCK_SIZE
;
195 memcpy(ivec
, iv
.data
, AES_BLOCK_SIZE
);
196 memcpy(ivec
+ AES_BLOCK_SIZE
, iv2
.data
, AES_BLOCK_SIZE
);
202 * Note that its effectively impossible to do biIGE in anything other
203 * than a single pass, so no provision is made for chaining.
206 /* N.B. The IV for this mode is _four times_ the block size */
208 void AES_bi_ige_encrypt(const unsigned char *in
, unsigned char *out
,
209 size_t length
, const AES_KEY
*key
,
210 const AES_KEY
*key2
, const unsigned char *ivec
,
215 unsigned char tmp
[AES_BLOCK_SIZE
];
216 unsigned char tmp2
[AES_BLOCK_SIZE
];
217 unsigned char tmp3
[AES_BLOCK_SIZE
];
218 unsigned char prev
[AES_BLOCK_SIZE
];
219 const unsigned char *iv
;
220 const unsigned char *iv2
;
222 OPENSSL_assert(in
&& out
&& key
&& ivec
);
223 OPENSSL_assert((AES_ENCRYPT
== enc
) || (AES_DECRYPT
== enc
));
224 OPENSSL_assert((length
% AES_BLOCK_SIZE
) == 0);
226 if (AES_ENCRYPT
== enc
) {
228 * XXX: Do a separate case for when in != out (strictly should check
232 /* First the forward pass */
234 iv2
= ivec
+ AES_BLOCK_SIZE
;
235 while (len
>= AES_BLOCK_SIZE
) {
236 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
237 out
[n
] = in
[n
] ^ iv
[n
];
238 AES_encrypt(out
, out
, key
);
239 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
242 memcpy(prev
, in
, AES_BLOCK_SIZE
);
244 len
-= AES_BLOCK_SIZE
;
245 in
+= AES_BLOCK_SIZE
;
246 out
+= AES_BLOCK_SIZE
;
249 /* And now backwards */
250 iv
= ivec
+ AES_BLOCK_SIZE
* 2;
251 iv2
= ivec
+ AES_BLOCK_SIZE
* 3;
253 while (len
>= AES_BLOCK_SIZE
) {
254 out
-= AES_BLOCK_SIZE
;
256 * XXX: reduce copies by alternating between buffers
258 memcpy(tmp
, out
, AES_BLOCK_SIZE
);
259 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
262 * hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE);
264 AES_encrypt(out
, out
, key
);
266 * hexdump(stdout,"enc", out, AES_BLOCK_SIZE);
269 * hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE);
271 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
274 * hexdump(stdout,"out", out, AES_BLOCK_SIZE);
277 memcpy(prev
, tmp
, AES_BLOCK_SIZE
);
279 len
-= AES_BLOCK_SIZE
;
282 /* First backwards */
283 iv
= ivec
+ AES_BLOCK_SIZE
* 2;
284 iv2
= ivec
+ AES_BLOCK_SIZE
* 3;
287 while (len
>= AES_BLOCK_SIZE
) {
288 in
-= AES_BLOCK_SIZE
;
289 out
-= AES_BLOCK_SIZE
;
290 memcpy(tmp
, in
, AES_BLOCK_SIZE
);
291 memcpy(tmp2
, in
, AES_BLOCK_SIZE
);
292 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
294 AES_decrypt(tmp
, out
, key
);
295 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
297 memcpy(tmp3
, tmp2
, AES_BLOCK_SIZE
);
300 len
-= AES_BLOCK_SIZE
;
303 /* And now forwards */
305 iv2
= ivec
+ AES_BLOCK_SIZE
;
307 while (len
>= AES_BLOCK_SIZE
) {
308 memcpy(tmp
, out
, AES_BLOCK_SIZE
);
309 memcpy(tmp2
, out
, AES_BLOCK_SIZE
);
310 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
312 AES_decrypt(tmp
, out
, key
);
313 for (n
= 0; n
< AES_BLOCK_SIZE
; ++n
)
315 memcpy(tmp3
, tmp2
, AES_BLOCK_SIZE
);
318 len
-= AES_BLOCK_SIZE
;
319 in
+= AES_BLOCK_SIZE
;
320 out
+= AES_BLOCK_SIZE
;