2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
12 #include <openssl/aes.h>
15 #define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long))
17 unsigned long data[N_WORDS];
20 /* XXX: probably some better way to do this */
21 #if defined(__i386__) || defined(__x86_64__)
22 # define UNALIGNED_MEMOPS_ARE_FAST 1
24 # define UNALIGNED_MEMOPS_ARE_FAST 0
27 #if UNALIGNED_MEMOPS_ARE_FAST
28 # define load_block(d, s) (d) = *(const aes_block_t *)(s)
29 # define store_block(d, s) *(aes_block_t *)(d) = (s)
31 # define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE)
32 # define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE)
35 /* N.B. The IV for this mode is _twice_ the block size */
37 void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
38 size_t length, const AES_KEY *key,
39 unsigned char *ivec, const int enc)
44 OPENSSL_assert(in && out && key && ivec);
45 OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
46 OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
48 len = length / AES_BLOCK_SIZE;
50 if (AES_ENCRYPT == enc) {
52 (UNALIGNED_MEMOPS_ARE_FAST
53 || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
55 aes_block_t *ivp = (aes_block_t *) ivec;
56 aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);
59 aes_block_t *inp = (aes_block_t *) in;
60 aes_block_t *outp = (aes_block_t *) out;
62 for (n = 0; n < N_WORDS; ++n)
63 outp->data[n] = inp->data[n] ^ ivp->data[n];
64 AES_encrypt((unsigned char *)outp->data,
65 (unsigned char *)outp->data, key);
66 for (n = 0; n < N_WORDS; ++n)
67 outp->data[n] ^= iv2p->data[n];
72 out += AES_BLOCK_SIZE;
74 memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
75 memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
77 aes_block_t tmp, tmp2;
82 load_block(iv2, ivec + AES_BLOCK_SIZE);
86 for (n = 0; n < N_WORDS; ++n)
87 tmp2.data[n] = tmp.data[n] ^ iv.data[n];
88 AES_encrypt((unsigned char *)tmp2.data,
89 (unsigned char *)tmp2.data, key);
90 for (n = 0; n < N_WORDS; ++n)
91 tmp2.data[n] ^= iv2.data[n];
92 store_block(out, tmp2);
97 out += AES_BLOCK_SIZE;
99 memcpy(ivec, iv.data, AES_BLOCK_SIZE);
100 memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
104 (UNALIGNED_MEMOPS_ARE_FAST
105 || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
107 aes_block_t *ivp = (aes_block_t *) ivec;
108 aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);
112 aes_block_t *inp = (aes_block_t *) in;
113 aes_block_t *outp = (aes_block_t *) out;
115 for (n = 0; n < N_WORDS; ++n)
116 tmp.data[n] = inp->data[n] ^ iv2p->data[n];
117 AES_decrypt((unsigned char *)tmp.data,
118 (unsigned char *)outp->data, key);
119 for (n = 0; n < N_WORDS; ++n)
120 outp->data[n] ^= ivp->data[n];
124 in += AES_BLOCK_SIZE;
125 out += AES_BLOCK_SIZE;
127 memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
128 memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
130 aes_block_t tmp, tmp2;
134 load_block(iv, ivec);
135 load_block(iv2, ivec + AES_BLOCK_SIZE);
140 for (n = 0; n < N_WORDS; ++n)
141 tmp.data[n] ^= iv2.data[n];
142 AES_decrypt((unsigned char *)tmp.data,
143 (unsigned char *)tmp.data, key);
144 for (n = 0; n < N_WORDS; ++n)
145 tmp.data[n] ^= iv.data[n];
146 store_block(out, tmp);
150 in += AES_BLOCK_SIZE;
151 out += AES_BLOCK_SIZE;
153 memcpy(ivec, iv.data, AES_BLOCK_SIZE);
154 memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
160 * Note that its effectively impossible to do biIGE in anything other
161 * than a single pass, so no provision is made for chaining.
164 /* N.B. The IV for this mode is _four times_ the block size */
166 void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out,
167 size_t length, const AES_KEY *key,
168 const AES_KEY *key2, const unsigned char *ivec,
173 unsigned char tmp[AES_BLOCK_SIZE];
174 unsigned char tmp2[AES_BLOCK_SIZE];
175 unsigned char tmp3[AES_BLOCK_SIZE];
176 unsigned char prev[AES_BLOCK_SIZE];
177 const unsigned char *iv;
178 const unsigned char *iv2;
180 OPENSSL_assert(in && out && key && ivec);
181 OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
182 OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
184 if (AES_ENCRYPT == enc) {
186 * XXX: Do a separate case for when in != out (strictly should check
190 /* First the forward pass */
192 iv2 = ivec + AES_BLOCK_SIZE;
193 while (len >= AES_BLOCK_SIZE) {
194 for (n = 0; n < AES_BLOCK_SIZE; ++n)
195 out[n] = in[n] ^ iv[n];
196 AES_encrypt(out, out, key);
197 for (n = 0; n < AES_BLOCK_SIZE; ++n)
200 memcpy(prev, in, AES_BLOCK_SIZE);
202 len -= AES_BLOCK_SIZE;
203 in += AES_BLOCK_SIZE;
204 out += AES_BLOCK_SIZE;
207 /* And now backwards */
208 iv = ivec + AES_BLOCK_SIZE * 2;
209 iv2 = ivec + AES_BLOCK_SIZE * 3;
211 while (len >= AES_BLOCK_SIZE) {
212 out -= AES_BLOCK_SIZE;
214 * XXX: reduce copies by alternating between buffers
216 memcpy(tmp, out, AES_BLOCK_SIZE);
217 for (n = 0; n < AES_BLOCK_SIZE; ++n)
220 * hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE);
222 AES_encrypt(out, out, key);
224 * hexdump(stdout,"enc", out, AES_BLOCK_SIZE);
227 * hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE);
229 for (n = 0; n < AES_BLOCK_SIZE; ++n)
232 * hexdump(stdout,"out", out, AES_BLOCK_SIZE);
235 memcpy(prev, tmp, AES_BLOCK_SIZE);
237 len -= AES_BLOCK_SIZE;
240 /* First backwards */
241 iv = ivec + AES_BLOCK_SIZE * 2;
242 iv2 = ivec + AES_BLOCK_SIZE * 3;
245 while (len >= AES_BLOCK_SIZE) {
246 in -= AES_BLOCK_SIZE;
247 out -= AES_BLOCK_SIZE;
248 memcpy(tmp, in, AES_BLOCK_SIZE);
249 memcpy(tmp2, in, AES_BLOCK_SIZE);
250 for (n = 0; n < AES_BLOCK_SIZE; ++n)
252 AES_decrypt(tmp, out, key);
253 for (n = 0; n < AES_BLOCK_SIZE; ++n)
255 memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
258 len -= AES_BLOCK_SIZE;
261 /* And now forwards */
263 iv2 = ivec + AES_BLOCK_SIZE;
265 while (len >= AES_BLOCK_SIZE) {
266 memcpy(tmp, out, AES_BLOCK_SIZE);
267 memcpy(tmp2, out, AES_BLOCK_SIZE);
268 for (n = 0; n < AES_BLOCK_SIZE; ++n)
270 AES_decrypt(tmp, out, key);
271 for (n = 0; n < AES_BLOCK_SIZE; ++n)
273 memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
276 len -= AES_BLOCK_SIZE;
277 in += AES_BLOCK_SIZE;
278 out += AES_BLOCK_SIZE;