| 1 |
/* |
| 2 |
* Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (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 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* AES_encrypt/AES_decrypt are deprecated - but we need to use them to implement |
| 12 |
* these functions |
| 13 |
*/ |
| 14 |
#include "internal/deprecated.h" |
| 15 |
|
| 16 |
#include "internal/cryptlib.h" |
| 17 |
|
| 18 |
#include <openssl/aes.h> |
| 19 |
#include "aes_local.h" |
| 20 |
|
| 21 |
/* XXX: probably some better way to do this */ |
| 22 |
#if defined(__i386__) || defined(__x86_64__) |
| 23 |
#define UNALIGNED_MEMOPS_ARE_FAST 1 |
| 24 |
#else |
| 25 |
#define UNALIGNED_MEMOPS_ARE_FAST 0 |
| 26 |
#endif |
| 27 |
|
| 28 |
#define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long)) |
| 29 |
typedef struct { |
| 30 |
unsigned long data[N_WORDS]; |
| 31 |
#if defined(__GNUC__) && UNALIGNED_MEMOPS_ARE_FAST |
| 32 |
} aes_block_t __attribute((__aligned__(1))); |
| 33 |
#else |
| 34 |
} aes_block_t; |
| 35 |
#endif |
| 36 |
|
| 37 |
#if UNALIGNED_MEMOPS_ARE_FAST |
| 38 |
#define load_block(d, s) (d) = *(const aes_block_t *)(s) |
| 39 |
#define store_block(d, s) *(aes_block_t *)(d) = (s) |
| 40 |
#else |
| 41 |
#define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE) |
| 42 |
#define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE) |
| 43 |
#endif |
| 44 |
|
| 45 |
/* N.B. The IV for this mode is _twice_ the block size */ |
| 46 |
|
| 47 |
/* Use of this function is deprecated. */ |
| 48 |
void AES_ige_encrypt(const unsigned char *in, unsigned char *out, |
| 49 |
size_t length, const AES_KEY *key, |
| 50 |
unsigned char *ivec, const int enc) |
| 51 |
{ |
| 52 |
size_t n; |
| 53 |
size_t len = length / AES_BLOCK_SIZE; |
| 54 |
|
| 55 |
if (length == 0) |
| 56 |
return; |
| 57 |
|
| 58 |
OPENSSL_assert(in && out && key && ivec); |
| 59 |
OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc)); |
| 60 |
OPENSSL_assert((length % AES_BLOCK_SIZE) == 0); |
| 61 |
|
| 62 |
if (AES_ENCRYPT == enc) { |
| 63 |
if (in != out && (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) == 0)) { |
| 64 |
aes_block_t *ivp = (aes_block_t *)ivec; |
| 65 |
aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE); |
| 66 |
|
| 67 |
while (len) { |
| 68 |
aes_block_t *inp = (aes_block_t *)in; |
| 69 |
aes_block_t *outp = (aes_block_t *)out; |
| 70 |
|
| 71 |
for (n = 0; n < N_WORDS; ++n) |
| 72 |
outp->data[n] = inp->data[n] ^ ivp->data[n]; |
| 73 |
AES_encrypt((unsigned char *)outp->data, |
| 74 |
(unsigned char *)outp->data, key); |
| 75 |
for (n = 0; n < N_WORDS; ++n) |
| 76 |
outp->data[n] ^= iv2p->data[n]; |
| 77 |
ivp = outp; |
| 78 |
iv2p = inp; |
| 79 |
--len; |
| 80 |
in += AES_BLOCK_SIZE; |
| 81 |
out += AES_BLOCK_SIZE; |
| 82 |
} |
| 83 |
memcpy(ivec, ivp->data, AES_BLOCK_SIZE); |
| 84 |
memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE); |
| 85 |
} else { |
| 86 |
aes_block_t tmp, tmp2; |
| 87 |
aes_block_t iv; |
| 88 |
aes_block_t iv2; |
| 89 |
|
| 90 |
load_block(iv, ivec); |
| 91 |
load_block(iv2, ivec + AES_BLOCK_SIZE); |
| 92 |
|
| 93 |
while (len) { |
| 94 |
load_block(tmp, in); |
| 95 |
for (n = 0; n < N_WORDS; ++n) |
| 96 |
tmp2.data[n] = tmp.data[n] ^ iv.data[n]; |
| 97 |
AES_encrypt((unsigned char *)tmp2.data, |
| 98 |
(unsigned char *)tmp2.data, key); |
| 99 |
for (n = 0; n < N_WORDS; ++n) |
| 100 |
tmp2.data[n] ^= iv2.data[n]; |
| 101 |
store_block(out, tmp2); |
| 102 |
iv = tmp2; |
| 103 |
iv2 = tmp; |
| 104 |
--len; |
| 105 |
in += AES_BLOCK_SIZE; |
| 106 |
out += AES_BLOCK_SIZE; |
| 107 |
} |
| 108 |
memcpy(ivec, iv.data, AES_BLOCK_SIZE); |
| 109 |
memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE); |
| 110 |
} |
| 111 |
} else { |
| 112 |
if (in != out && (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) == 0)) { |
| 113 |
aes_block_t *ivp = (aes_block_t *)ivec; |
| 114 |
aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE); |
| 115 |
|
| 116 |
while (len) { |
| 117 |
aes_block_t tmp; |
| 118 |
aes_block_t *inp = (aes_block_t *)in; |
| 119 |
aes_block_t *outp = (aes_block_t *)out; |
| 120 |
|
| 121 |
for (n = 0; n < N_WORDS; ++n) |
| 122 |
tmp.data[n] = inp->data[n] ^ iv2p->data[n]; |
| 123 |
AES_decrypt((unsigned char *)tmp.data, |
| 124 |
(unsigned char *)outp->data, key); |
| 125 |
for (n = 0; n < N_WORDS; ++n) |
| 126 |
outp->data[n] ^= ivp->data[n]; |
| 127 |
ivp = inp; |
| 128 |
iv2p = outp; |
| 129 |
--len; |
| 130 |
in += AES_BLOCK_SIZE; |
| 131 |
out += AES_BLOCK_SIZE; |
| 132 |
} |
| 133 |
memcpy(ivec, ivp->data, AES_BLOCK_SIZE); |
| 134 |
memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE); |
| 135 |
} else { |
| 136 |
aes_block_t tmp, tmp2; |
| 137 |
aes_block_t iv; |
| 138 |
aes_block_t iv2; |
| 139 |
|
| 140 |
load_block(iv, ivec); |
| 141 |
load_block(iv2, ivec + AES_BLOCK_SIZE); |
| 142 |
|
| 143 |
while (len) { |
| 144 |
load_block(tmp, in); |
| 145 |
tmp2 = tmp; |
| 146 |
for (n = 0; n < N_WORDS; ++n) |
| 147 |
tmp.data[n] ^= iv2.data[n]; |
| 148 |
AES_decrypt((unsigned char *)tmp.data, |
| 149 |
(unsigned char *)tmp.data, key); |
| 150 |
for (n = 0; n < N_WORDS; ++n) |
| 151 |
tmp.data[n] ^= iv.data[n]; |
| 152 |
store_block(out, tmp); |
| 153 |
iv = tmp2; |
| 154 |
iv2 = tmp; |
| 155 |
--len; |
| 156 |
in += AES_BLOCK_SIZE; |
| 157 |
out += AES_BLOCK_SIZE; |
| 158 |
} |
| 159 |
memcpy(ivec, iv.data, AES_BLOCK_SIZE); |
| 160 |
memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* Note that its effectively impossible to do biIGE in anything other |
| 167 |
* than a single pass, so no provision is made for chaining. |
| 168 |
* |
| 169 |
* NB: The implementation of AES_bi_ige_encrypt has a bug. It is supposed to use |
| 170 |
* 2 AES keys, but in fact only one is ever used. This bug has been present |
| 171 |
* since this code was first implemented. It is believed to have minimal |
| 172 |
* security impact in practice and has therefore not been fixed for backwards |
| 173 |
* compatibility reasons. |
| 174 |
* |
| 175 |
* Use of this function is deprecated. |
| 176 |
*/ |
| 177 |
|
| 178 |
/* N.B. The IV for this mode is _four times_ the block size */ |
| 179 |
|
| 180 |
void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out, |
| 181 |
size_t length, const AES_KEY *key, |
| 182 |
const AES_KEY *key2, const unsigned char *ivec, |
| 183 |
const int enc) |
| 184 |
{ |
| 185 |
size_t n; |
| 186 |
size_t len = length; |
| 187 |
unsigned char tmp[AES_BLOCK_SIZE]; |
| 188 |
unsigned char tmp2[AES_BLOCK_SIZE]; |
| 189 |
unsigned char tmp3[AES_BLOCK_SIZE]; |
| 190 |
unsigned char prev[AES_BLOCK_SIZE]; |
| 191 |
const unsigned char *iv; |
| 192 |
const unsigned char *iv2; |
| 193 |
|
| 194 |
OPENSSL_assert(in && out && key && ivec); |
| 195 |
OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc)); |
| 196 |
OPENSSL_assert((length % AES_BLOCK_SIZE) == 0); |
| 197 |
|
| 198 |
if (AES_ENCRYPT == enc) { |
| 199 |
/* |
| 200 |
* XXX: Do a separate case for when in != out (strictly should check |
| 201 |
* for overlap, too) |
| 202 |
*/ |
| 203 |
|
| 204 |
/* First the forward pass */ |
| 205 |
iv = ivec; |
| 206 |
iv2 = ivec + AES_BLOCK_SIZE; |
| 207 |
while (len >= AES_BLOCK_SIZE) { |
| 208 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 209 |
out[n] = in[n] ^ iv[n]; |
| 210 |
AES_encrypt(out, out, key); |
| 211 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 212 |
out[n] ^= iv2[n]; |
| 213 |
iv = out; |
| 214 |
memcpy(prev, in, AES_BLOCK_SIZE); |
| 215 |
iv2 = prev; |
| 216 |
len -= AES_BLOCK_SIZE; |
| 217 |
in += AES_BLOCK_SIZE; |
| 218 |
out += AES_BLOCK_SIZE; |
| 219 |
} |
| 220 |
|
| 221 |
/* And now backwards */ |
| 222 |
iv = ivec + AES_BLOCK_SIZE * 2; |
| 223 |
iv2 = ivec + AES_BLOCK_SIZE * 3; |
| 224 |
len = length; |
| 225 |
while (len >= AES_BLOCK_SIZE) { |
| 226 |
out -= AES_BLOCK_SIZE; |
| 227 |
/* |
| 228 |
* XXX: reduce copies by alternating between buffers |
| 229 |
*/ |
| 230 |
memcpy(tmp, out, AES_BLOCK_SIZE); |
| 231 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 232 |
out[n] ^= iv[n]; |
| 233 |
/* |
| 234 |
* hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE); |
| 235 |
*/ |
| 236 |
AES_encrypt(out, out, key); |
| 237 |
/* |
| 238 |
* hexdump(stdout,"enc", out, AES_BLOCK_SIZE); |
| 239 |
*/ |
| 240 |
/* |
| 241 |
* hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE); |
| 242 |
*/ |
| 243 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 244 |
out[n] ^= iv2[n]; |
| 245 |
/* |
| 246 |
* hexdump(stdout,"out", out, AES_BLOCK_SIZE); |
| 247 |
*/ |
| 248 |
iv = out; |
| 249 |
memcpy(prev, tmp, AES_BLOCK_SIZE); |
| 250 |
iv2 = prev; |
| 251 |
len -= AES_BLOCK_SIZE; |
| 252 |
} |
| 253 |
} else { |
| 254 |
/* First backwards */ |
| 255 |
iv = ivec + AES_BLOCK_SIZE * 2; |
| 256 |
iv2 = ivec + AES_BLOCK_SIZE * 3; |
| 257 |
in += length; |
| 258 |
out += length; |
| 259 |
while (len >= AES_BLOCK_SIZE) { |
| 260 |
in -= AES_BLOCK_SIZE; |
| 261 |
out -= AES_BLOCK_SIZE; |
| 262 |
memcpy(tmp, in, AES_BLOCK_SIZE); |
| 263 |
memcpy(tmp2, in, AES_BLOCK_SIZE); |
| 264 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 265 |
tmp[n] ^= iv2[n]; |
| 266 |
AES_decrypt(tmp, out, key); |
| 267 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 268 |
out[n] ^= iv[n]; |
| 269 |
memcpy(tmp3, tmp2, AES_BLOCK_SIZE); |
| 270 |
iv = tmp3; |
| 271 |
iv2 = out; |
| 272 |
len -= AES_BLOCK_SIZE; |
| 273 |
} |
| 274 |
|
| 275 |
/* And now forwards */ |
| 276 |
iv = ivec; |
| 277 |
iv2 = ivec + AES_BLOCK_SIZE; |
| 278 |
len = length; |
| 279 |
while (len >= AES_BLOCK_SIZE) { |
| 280 |
memcpy(tmp, out, AES_BLOCK_SIZE); |
| 281 |
memcpy(tmp2, out, AES_BLOCK_SIZE); |
| 282 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 283 |
tmp[n] ^= iv2[n]; |
| 284 |
AES_decrypt(tmp, out, key); |
| 285 |
for (n = 0; n < AES_BLOCK_SIZE; ++n) |
| 286 |
out[n] ^= iv[n]; |
| 287 |
memcpy(tmp3, tmp2, AES_BLOCK_SIZE); |
| 288 |
iv = tmp3; |
| 289 |
iv2 = out; |
| 290 |
len -= AES_BLOCK_SIZE; |
| 291 |
in += AES_BLOCK_SIZE; |
| 292 |
out += AES_BLOCK_SIZE; |
| 293 |
} |
| 294 |
} |
| 295 |
} |