| 1 |
/* ==================================================================== |
| 2 |
* Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in |
| 13 |
* the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* |
| 16 |
* 3. All advertising materials mentioning features or use of this |
| 17 |
* software must display the following acknowledgment: |
| 18 |
* "This product includes software developed by the OpenSSL Project |
| 19 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 20 |
* |
| 21 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 22 |
* endorse or promote products derived from this software without |
| 23 |
* prior written permission. For written permission, please contact |
| 24 |
* [email protected]. |
| 25 |
* |
| 26 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 27 |
* nor may "OpenSSL" appear in their names without prior written |
| 28 |
* permission of the OpenSSL Project. |
| 29 |
* |
| 30 |
* 6. Redistributions of any form whatsoever must retain the following |
| 31 |
* acknowledgment: |
| 32 |
* "This product includes software developed by the OpenSSL Project |
| 33 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 36 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 39 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 42 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 43 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 44 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 46 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 |
* ==================================================================== */ |
| 48 |
|
| 49 |
#include <string.h> |
| 50 |
|
| 51 |
#include <openssl/aead.h> |
| 52 |
#include <openssl/aes.h> |
| 53 |
#include <openssl/cipher.h> |
| 54 |
#include <openssl/cpu.h> |
| 55 |
#include <openssl/err.h> |
| 56 |
#include <openssl/mem.h> |
| 57 |
#include <openssl/nid.h> |
| 58 |
#include <openssl/rand.h> |
| 59 |
#include <openssl/sha.h> |
| 60 |
|
| 61 |
#include "internal.h" |
| 62 |
#include "../internal.h" |
| 63 |
#include "../modes/internal.h" |
| 64 |
|
| 65 |
#if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 66 |
#include <openssl/arm_arch.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
|
| 70 |
OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) /* Unreachable code. */ |
| 71 |
|
| 72 |
typedef struct { |
| 73 |
union { |
| 74 |
double align; |
| 75 |
AES_KEY ks; |
| 76 |
} ks; |
| 77 |
block128_f block; |
| 78 |
union { |
| 79 |
cbc128_f cbc; |
| 80 |
ctr128_f ctr; |
| 81 |
} stream; |
| 82 |
} EVP_AES_KEY; |
| 83 |
|
| 84 |
typedef struct { |
| 85 |
union { |
| 86 |
double align; |
| 87 |
AES_KEY ks; |
| 88 |
} ks; /* AES key schedule to use */ |
| 89 |
int key_set; /* Set if key initialised */ |
| 90 |
int iv_set; /* Set if an iv is set */ |
| 91 |
GCM128_CONTEXT gcm; |
| 92 |
uint8_t *iv; /* Temporary IV store */ |
| 93 |
int ivlen; /* IV length */ |
| 94 |
int taglen; |
| 95 |
int iv_gen; /* It is OK to generate IVs */ |
| 96 |
ctr128_f ctr; |
| 97 |
} EVP_AES_GCM_CTX; |
| 98 |
|
| 99 |
#if !defined(OPENSSL_NO_ASM) && \ |
| 100 |
(defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) |
| 101 |
#define VPAES |
| 102 |
static char vpaes_capable(void) { |
| 103 |
return (OPENSSL_ia32cap_P[1] & (1 << (41 - 32))) != 0; |
| 104 |
} |
| 105 |
|
| 106 |
#if defined(OPENSSL_X86_64) |
| 107 |
#define BSAES |
| 108 |
static char bsaes_capable(void) { |
| 109 |
return vpaes_capable(); |
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
#elif !defined(OPENSSL_NO_ASM) && \ |
| 114 |
(defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) |
| 115 |
|
| 116 |
#if defined(OPENSSL_ARM) && __ARM_MAX_ARCH__ >= 7 |
| 117 |
#define BSAES |
| 118 |
static char bsaes_capable(void) { |
| 119 |
return CRYPTO_is_NEON_capable(); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
#define HWAES |
| 124 |
static int hwaes_capable(void) { |
| 125 |
return CRYPTO_is_ARMv8_AES_capable(); |
| 126 |
} |
| 127 |
|
| 128 |
int aes_v8_set_encrypt_key(const uint8_t *user_key, const int bits, |
| 129 |
AES_KEY *key); |
| 130 |
int aes_v8_set_decrypt_key(const uint8_t *user_key, const int bits, |
| 131 |
AES_KEY *key); |
| 132 |
void aes_v8_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 133 |
void aes_v8_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 134 |
void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 135 |
const AES_KEY *key, uint8_t *ivec, const int enc); |
| 136 |
void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 137 |
const AES_KEY *key, const uint8_t ivec[16]); |
| 138 |
|
| 139 |
#endif /* OPENSSL_ARM */ |
| 140 |
|
| 141 |
#if defined(BSAES) |
| 142 |
/* On platforms where BSAES gets defined (just above), then these functions are |
| 143 |
* provided by asm. */ |
| 144 |
void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 145 |
const AES_KEY *key, uint8_t ivec[16], int enc); |
| 146 |
void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 147 |
const AES_KEY *key, const uint8_t ivec[16]); |
| 148 |
#else |
| 149 |
static char bsaes_capable(void) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
/* On other platforms, bsaes_capable() will always return false and so the |
| 154 |
* following will never be called. */ |
| 155 |
static void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 156 |
const AES_KEY *key, uint8_t ivec[16], int enc) { |
| 157 |
abort(); |
| 158 |
} |
| 159 |
|
| 160 |
static void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 161 |
size_t len, const AES_KEY *key, |
| 162 |
const uint8_t ivec[16]) { |
| 163 |
abort(); |
| 164 |
} |
| 165 |
#endif |
| 166 |
|
| 167 |
#if defined(VPAES) |
| 168 |
/* On platforms where VPAES gets defined (just above), then these functions are |
| 169 |
* provided by asm. */ |
| 170 |
int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 171 |
int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 172 |
|
| 173 |
void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 174 |
void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 175 |
|
| 176 |
void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 177 |
const AES_KEY *key, uint8_t *ivec, int enc); |
| 178 |
#else |
| 179 |
static char vpaes_capable(void) { |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
/* On other platforms, vpaes_capable() will always return false and so the |
| 184 |
* following will never be called. */ |
| 185 |
static int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, |
| 186 |
AES_KEY *key) { |
| 187 |
abort(); |
| 188 |
} |
| 189 |
static int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, |
| 190 |
AES_KEY *key) { |
| 191 |
abort(); |
| 192 |
} |
| 193 |
static void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { |
| 194 |
abort(); |
| 195 |
} |
| 196 |
static void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { |
| 197 |
abort(); |
| 198 |
} |
| 199 |
static void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 200 |
const AES_KEY *key, uint8_t *ivec, int enc) { |
| 201 |
abort(); |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
#if !defined(HWAES) |
| 206 |
/* If HWAES isn't defined then we provide dummy functions for each of the hwaes |
| 207 |
* functions. */ |
| 208 |
static int hwaes_capable(void) { |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
static int aes_v8_set_encrypt_key(const uint8_t *user_key, int bits, |
| 213 |
AES_KEY *key) { |
| 214 |
abort(); |
| 215 |
} |
| 216 |
|
| 217 |
static int aes_v8_set_decrypt_key(const uint8_t *user_key, int bits, |
| 218 |
AES_KEY *key) { |
| 219 |
abort(); |
| 220 |
} |
| 221 |
|
| 222 |
static void aes_v8_encrypt(const uint8_t *in, uint8_t *out, |
| 223 |
const AES_KEY *key) { |
| 224 |
abort(); |
| 225 |
} |
| 226 |
|
| 227 |
static void aes_v8_decrypt(const uint8_t *in, uint8_t *out, |
| 228 |
const AES_KEY *key) { |
| 229 |
abort(); |
| 230 |
} |
| 231 |
|
| 232 |
static void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 233 |
const AES_KEY *key, uint8_t *ivec, int enc) { |
| 234 |
abort(); |
| 235 |
} |
| 236 |
|
| 237 |
static void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 238 |
size_t len, const AES_KEY *key, |
| 239 |
const uint8_t ivec[16]) { |
| 240 |
abort(); |
| 241 |
} |
| 242 |
#endif |
| 243 |
|
| 244 |
#if !defined(OPENSSL_NO_ASM) && \ |
| 245 |
(defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) |
| 246 |
int aesni_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 247 |
int aesni_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 248 |
|
| 249 |
void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 250 |
void aesni_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 251 |
|
| 252 |
void aesni_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 253 |
const AES_KEY *key, int enc); |
| 254 |
void aesni_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 255 |
const AES_KEY *key, uint8_t *ivec, int enc); |
| 256 |
|
| 257 |
#else |
| 258 |
|
| 259 |
/* On other platforms, aesni_capable() will always return false and so the |
| 260 |
* following will never be called. */ |
| 261 |
static void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { |
| 262 |
abort(); |
| 263 |
} |
| 264 |
static int aesni_set_encrypt_key(const uint8_t *userKey, int bits, |
| 265 |
AES_KEY *key) { |
| 266 |
abort(); |
| 267 |
} |
| 268 |
static void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 269 |
size_t blocks, const void *key, |
| 270 |
const uint8_t *ivec) { |
| 271 |
abort(); |
| 272 |
} |
| 273 |
|
| 274 |
#endif |
| 275 |
|
| 276 |
static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 277 |
const uint8_t *iv, int enc) { |
| 278 |
int ret, mode; |
| 279 |
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 280 |
|
| 281 |
mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
| 282 |
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { |
| 283 |
if (hwaes_capable()) { |
| 284 |
ret = aes_v8_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 285 |
dat->block = (block128_f)aes_v8_decrypt; |
| 286 |
dat->stream.cbc = NULL; |
| 287 |
if (mode == EVP_CIPH_CBC_MODE) { |
| 288 |
dat->stream.cbc = (cbc128_f)aes_v8_cbc_encrypt; |
| 289 |
} |
| 290 |
} else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) { |
| 291 |
ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 292 |
dat->block = (block128_f)AES_decrypt; |
| 293 |
dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; |
| 294 |
} else if (vpaes_capable()) { |
| 295 |
ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 296 |
dat->block = (block128_f)vpaes_decrypt; |
| 297 |
dat->stream.cbc = |
| 298 |
mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL; |
| 299 |
} else { |
| 300 |
ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 301 |
dat->block = (block128_f)AES_decrypt; |
| 302 |
dat->stream.cbc = |
| 303 |
mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL; |
| 304 |
} |
| 305 |
} else if (hwaes_capable()) { |
| 306 |
ret = aes_v8_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 307 |
dat->block = (block128_f)aes_v8_encrypt; |
| 308 |
dat->stream.cbc = NULL; |
| 309 |
if (mode == EVP_CIPH_CBC_MODE) { |
| 310 |
dat->stream.cbc = (cbc128_f)aes_v8_cbc_encrypt; |
| 311 |
} else if (mode == EVP_CIPH_CTR_MODE) { |
| 312 |
dat->stream.ctr = (ctr128_f)aes_v8_ctr32_encrypt_blocks; |
| 313 |
} |
| 314 |
} else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) { |
| 315 |
ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 316 |
dat->block = (block128_f)AES_encrypt; |
| 317 |
dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; |
| 318 |
} else if (vpaes_capable()) { |
| 319 |
ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 320 |
dat->block = (block128_f)vpaes_encrypt; |
| 321 |
dat->stream.cbc = |
| 322 |
mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL; |
| 323 |
} else { |
| 324 |
ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 325 |
dat->block = (block128_f)AES_encrypt; |
| 326 |
dat->stream.cbc = |
| 327 |
mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL; |
| 328 |
} |
| 329 |
|
| 330 |
if (ret < 0) { |
| 331 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED); |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
return 1; |
| 336 |
} |
| 337 |
|
| 338 |
static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 339 |
size_t len) { |
| 340 |
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 341 |
|
| 342 |
if (dat->stream.cbc) { |
| 343 |
(*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, ctx->encrypt); |
| 344 |
} else if (ctx->encrypt) { |
| 345 |
CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block); |
| 346 |
} else { |
| 347 |
CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block); |
| 348 |
} |
| 349 |
|
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 354 |
size_t len) { |
| 355 |
size_t bl = ctx->cipher->block_size; |
| 356 |
size_t i; |
| 357 |
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 358 |
|
| 359 |
if (len < bl) { |
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
for (i = 0, len -= bl; i <= len; i += bl) { |
| 364 |
(*dat->block)(in + i, out + i, &dat->ks); |
| 365 |
} |
| 366 |
|
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 371 |
size_t len) { |
| 372 |
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 373 |
|
| 374 |
if (dat->stream.ctr) { |
| 375 |
CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv, ctx->buf, |
| 376 |
&ctx->num, dat->stream.ctr); |
| 377 |
} else { |
| 378 |
CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, ctx->buf, &ctx->num, |
| 379 |
dat->block); |
| 380 |
} |
| 381 |
return 1; |
| 382 |
} |
| 383 |
|
| 384 |
static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 385 |
size_t len) { |
| 386 |
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 387 |
|
| 388 |
CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, dat->block); |
| 389 |
return 1; |
| 390 |
} |
| 391 |
|
| 392 |
static char aesni_capable(void); |
| 393 |
|
| 394 |
static ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, |
| 395 |
block128_f *out_block, const uint8_t *key, |
| 396 |
size_t key_len) { |
| 397 |
if (aesni_capable()) { |
| 398 |
aesni_set_encrypt_key(key, key_len * 8, aes_key); |
| 399 |
if (gcm_ctx != NULL) { |
| 400 |
CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt); |
| 401 |
} |
| 402 |
if (out_block) { |
| 403 |
*out_block = (block128_f) aesni_encrypt; |
| 404 |
} |
| 405 |
return (ctr128_f)aesni_ctr32_encrypt_blocks; |
| 406 |
} |
| 407 |
|
| 408 |
if (hwaes_capable()) { |
| 409 |
aes_v8_set_encrypt_key(key, key_len * 8, aes_key); |
| 410 |
if (gcm_ctx != NULL) { |
| 411 |
CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_v8_encrypt); |
| 412 |
} |
| 413 |
if (out_block) { |
| 414 |
*out_block = (block128_f) aes_v8_encrypt; |
| 415 |
} |
| 416 |
return (ctr128_f)aes_v8_ctr32_encrypt_blocks; |
| 417 |
} |
| 418 |
|
| 419 |
if (bsaes_capable()) { |
| 420 |
AES_set_encrypt_key(key, key_len * 8, aes_key); |
| 421 |
if (gcm_ctx != NULL) { |
| 422 |
CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
| 423 |
} |
| 424 |
if (out_block) { |
| 425 |
*out_block = (block128_f) AES_encrypt; |
| 426 |
} |
| 427 |
return (ctr128_f)bsaes_ctr32_encrypt_blocks; |
| 428 |
} |
| 429 |
|
| 430 |
if (vpaes_capable()) { |
| 431 |
vpaes_set_encrypt_key(key, key_len * 8, aes_key); |
| 432 |
if (out_block) { |
| 433 |
*out_block = (block128_f) vpaes_encrypt; |
| 434 |
} |
| 435 |
if (gcm_ctx != NULL) { |
| 436 |
CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt); |
| 437 |
} |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
|
| 441 |
AES_set_encrypt_key(key, key_len * 8, aes_key); |
| 442 |
if (gcm_ctx != NULL) { |
| 443 |
CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt); |
| 444 |
} |
| 445 |
if (out_block) { |
| 446 |
*out_block = (block128_f) AES_encrypt; |
| 447 |
} |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
|
| 451 |
static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 452 |
const uint8_t *iv, int enc) { |
| 453 |
EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
| 454 |
if (!iv && !key) { |
| 455 |
return 1; |
| 456 |
} |
| 457 |
if (key) { |
| 458 |
gctx->ctr = |
| 459 |
aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm, NULL, key, ctx->key_len); |
| 460 |
/* If we have an iv can set it directly, otherwise use saved IV. */ |
| 461 |
if (iv == NULL && gctx->iv_set) { |
| 462 |
iv = gctx->iv; |
| 463 |
} |
| 464 |
if (iv) { |
| 465 |
CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
| 466 |
gctx->iv_set = 1; |
| 467 |
} |
| 468 |
gctx->key_set = 1; |
| 469 |
} else { |
| 470 |
/* If key set use IV, otherwise copy */ |
| 471 |
if (gctx->key_set) { |
| 472 |
CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
| 473 |
} else { |
| 474 |
memcpy(gctx->iv, iv, gctx->ivlen); |
| 475 |
} |
| 476 |
gctx->iv_set = 1; |
| 477 |
gctx->iv_gen = 0; |
| 478 |
} |
| 479 |
return 1; |
| 480 |
} |
| 481 |
|
| 482 |
static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) { |
| 483 |
EVP_AES_GCM_CTX *gctx = c->cipher_data; |
| 484 |
OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); |
| 485 |
if (gctx->iv != c->iv) { |
| 486 |
OPENSSL_free(gctx->iv); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
/* increment counter (64-bit int) by 1 */ |
| 491 |
static void ctr64_inc(uint8_t *counter) { |
| 492 |
int n = 8; |
| 493 |
uint8_t c; |
| 494 |
|
| 495 |
do { |
| 496 |
--n; |
| 497 |
c = counter[n]; |
| 498 |
++c; |
| 499 |
counter[n] = c; |
| 500 |
if (c) { |
| 501 |
return; |
| 502 |
} |
| 503 |
} while (n); |
| 504 |
} |
| 505 |
|
| 506 |
static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { |
| 507 |
EVP_AES_GCM_CTX *gctx = c->cipher_data; |
| 508 |
switch (type) { |
| 509 |
case EVP_CTRL_INIT: |
| 510 |
gctx->key_set = 0; |
| 511 |
gctx->iv_set = 0; |
| 512 |
gctx->ivlen = c->cipher->iv_len; |
| 513 |
gctx->iv = c->iv; |
| 514 |
gctx->taglen = -1; |
| 515 |
gctx->iv_gen = 0; |
| 516 |
return 1; |
| 517 |
|
| 518 |
case EVP_CTRL_GCM_SET_IVLEN: |
| 519 |
if (arg <= 0) { |
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
/* Allocate memory for IV if needed */ |
| 524 |
if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) { |
| 525 |
if (gctx->iv != c->iv) { |
| 526 |
OPENSSL_free(gctx->iv); |
| 527 |
} |
| 528 |
gctx->iv = OPENSSL_malloc(arg); |
| 529 |
if (!gctx->iv) { |
| 530 |
return 0; |
| 531 |
} |
| 532 |
} |
| 533 |
gctx->ivlen = arg; |
| 534 |
return 1; |
| 535 |
|
| 536 |
case EVP_CTRL_GCM_SET_TAG: |
| 537 |
if (arg <= 0 || arg > 16 || c->encrypt) { |
| 538 |
return 0; |
| 539 |
} |
| 540 |
memcpy(c->buf, ptr, arg); |
| 541 |
gctx->taglen = arg; |
| 542 |
return 1; |
| 543 |
|
| 544 |
case EVP_CTRL_GCM_GET_TAG: |
| 545 |
if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) { |
| 546 |
return 0; |
| 547 |
} |
| 548 |
memcpy(ptr, c->buf, arg); |
| 549 |
return 1; |
| 550 |
|
| 551 |
case EVP_CTRL_GCM_SET_IV_FIXED: |
| 552 |
/* Special case: -1 length restores whole IV */ |
| 553 |
if (arg == -1) { |
| 554 |
memcpy(gctx->iv, ptr, gctx->ivlen); |
| 555 |
gctx->iv_gen = 1; |
| 556 |
return 1; |
| 557 |
} |
| 558 |
/* Fixed field must be at least 4 bytes and invocation field |
| 559 |
* at least 8. */ |
| 560 |
if (arg < 4 || (gctx->ivlen - arg) < 8) { |
| 561 |
return 0; |
| 562 |
} |
| 563 |
if (arg) { |
| 564 |
memcpy(gctx->iv, ptr, arg); |
| 565 |
} |
| 566 |
if (c->encrypt && !RAND_bytes(gctx->iv + arg, gctx->ivlen - arg)) { |
| 567 |
return 0; |
| 568 |
} |
| 569 |
gctx->iv_gen = 1; |
| 570 |
return 1; |
| 571 |
|
| 572 |
case EVP_CTRL_GCM_IV_GEN: |
| 573 |
if (gctx->iv_gen == 0 || gctx->key_set == 0) { |
| 574 |
return 0; |
| 575 |
} |
| 576 |
CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen); |
| 577 |
if (arg <= 0 || arg > gctx->ivlen) { |
| 578 |
arg = gctx->ivlen; |
| 579 |
} |
| 580 |
memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); |
| 581 |
/* Invocation field will be at least 8 bytes in size and |
| 582 |
* so no need to check wrap around or increment more than |
| 583 |
* last 8 bytes. */ |
| 584 |
ctr64_inc(gctx->iv + gctx->ivlen - 8); |
| 585 |
gctx->iv_set = 1; |
| 586 |
return 1; |
| 587 |
|
| 588 |
case EVP_CTRL_GCM_SET_IV_INV: |
| 589 |
if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) { |
| 590 |
return 0; |
| 591 |
} |
| 592 |
memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); |
| 593 |
CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen); |
| 594 |
gctx->iv_set = 1; |
| 595 |
return 1; |
| 596 |
|
| 597 |
case EVP_CTRL_COPY: { |
| 598 |
EVP_CIPHER_CTX *out = ptr; |
| 599 |
EVP_AES_GCM_CTX *gctx_out = out->cipher_data; |
| 600 |
if (gctx->iv == c->iv) { |
| 601 |
gctx_out->iv = out->iv; |
| 602 |
} else { |
| 603 |
gctx_out->iv = OPENSSL_malloc(gctx->ivlen); |
| 604 |
if (!gctx_out->iv) { |
| 605 |
return 0; |
| 606 |
} |
| 607 |
memcpy(gctx_out->iv, gctx->iv, gctx->ivlen); |
| 608 |
} |
| 609 |
return 1; |
| 610 |
} |
| 611 |
|
| 612 |
default: |
| 613 |
return -1; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 618 |
size_t len) { |
| 619 |
EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
| 620 |
|
| 621 |
/* If not set up, return error */ |
| 622 |
if (!gctx->key_set) { |
| 623 |
return -1; |
| 624 |
} |
| 625 |
if (!gctx->iv_set) { |
| 626 |
return -1; |
| 627 |
} |
| 628 |
|
| 629 |
if (in) { |
| 630 |
if (out == NULL) { |
| 631 |
if (!CRYPTO_gcm128_aad(&gctx->gcm, in, len)) { |
| 632 |
return -1; |
| 633 |
} |
| 634 |
} else if (ctx->encrypt) { |
| 635 |
if (gctx->ctr) { |
| 636 |
if (!CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len, |
| 637 |
gctx->ctr)) { |
| 638 |
return -1; |
| 639 |
} |
| 640 |
} else { |
| 641 |
if (!CRYPTO_gcm128_encrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) { |
| 642 |
return -1; |
| 643 |
} |
| 644 |
} |
| 645 |
} else { |
| 646 |
if (gctx->ctr) { |
| 647 |
if (!CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len, |
| 648 |
gctx->ctr)) { |
| 649 |
return -1; |
| 650 |
} |
| 651 |
} else { |
| 652 |
if (!CRYPTO_gcm128_decrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) { |
| 653 |
return -1; |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
return len; |
| 658 |
} else { |
| 659 |
if (!ctx->encrypt) { |
| 660 |
if (gctx->taglen < 0 || |
| 661 |
!CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen)) { |
| 662 |
return -1; |
| 663 |
} |
| 664 |
gctx->iv_set = 0; |
| 665 |
return 0; |
| 666 |
} |
| 667 |
CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); |
| 668 |
gctx->taglen = 16; |
| 669 |
/* Don't reuse the IV */ |
| 670 |
gctx->iv_set = 0; |
| 671 |
return 0; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
static const EVP_CIPHER aes_128_cbc = { |
| 676 |
NID_aes_128_cbc, 16 /* block_size */, 16 /* key_size */, |
| 677 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 678 |
NULL /* app_data */, aes_init_key, aes_cbc_cipher, |
| 679 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 680 |
|
| 681 |
static const EVP_CIPHER aes_128_ctr = { |
| 682 |
NID_aes_128_ctr, 1 /* block_size */, 16 /* key_size */, |
| 683 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 684 |
NULL /* app_data */, aes_init_key, aes_ctr_cipher, |
| 685 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 686 |
|
| 687 |
static const EVP_CIPHER aes_128_ecb = { |
| 688 |
NID_aes_128_ecb, 16 /* block_size */, 16 /* key_size */, |
| 689 |
0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 690 |
NULL /* app_data */, aes_init_key, aes_ecb_cipher, |
| 691 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 692 |
|
| 693 |
static const EVP_CIPHER aes_128_ofb = { |
| 694 |
NID_aes_128_ofb128, 1 /* block_size */, 16 /* key_size */, |
| 695 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 696 |
NULL /* app_data */, aes_init_key, aes_ofb_cipher, |
| 697 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 698 |
|
| 699 |
static const EVP_CIPHER aes_128_gcm = { |
| 700 |
NID_aes_128_gcm, 1 /* block_size */, 16 /* key_size */, 12 /* iv_len */, |
| 701 |
sizeof(EVP_AES_GCM_CTX), |
| 702 |
EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 703 |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 704 |
EVP_CIPH_FLAG_AEAD_CIPHER, |
| 705 |
NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 706 |
aes_gcm_ctrl}; |
| 707 |
|
| 708 |
|
| 709 |
static const EVP_CIPHER aes_192_cbc = { |
| 710 |
NID_aes_192_cbc, 16 /* block_size */, 24 /* key_size */, |
| 711 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 712 |
NULL /* app_data */, aes_init_key, aes_cbc_cipher, |
| 713 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 714 |
|
| 715 |
static const EVP_CIPHER aes_192_ctr = { |
| 716 |
NID_aes_192_ctr, 1 /* block_size */, 24 /* key_size */, |
| 717 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 718 |
NULL /* app_data */, aes_init_key, aes_ctr_cipher, |
| 719 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 720 |
|
| 721 |
static const EVP_CIPHER aes_192_ecb = { |
| 722 |
NID_aes_192_ecb, 16 /* block_size */, 24 /* key_size */, |
| 723 |
0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 724 |
NULL /* app_data */, aes_init_key, aes_ecb_cipher, |
| 725 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 726 |
|
| 727 |
static const EVP_CIPHER aes_192_gcm = { |
| 728 |
NID_aes_192_gcm, 1 /* block_size */, 24 /* key_size */, 12 /* iv_len */, |
| 729 |
sizeof(EVP_AES_GCM_CTX), |
| 730 |
EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 731 |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 732 |
EVP_CIPH_FLAG_AEAD_CIPHER, |
| 733 |
NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 734 |
aes_gcm_ctrl}; |
| 735 |
|
| 736 |
|
| 737 |
static const EVP_CIPHER aes_256_cbc = { |
| 738 |
NID_aes_256_cbc, 16 /* block_size */, 32 /* key_size */, |
| 739 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 740 |
NULL /* app_data */, aes_init_key, aes_cbc_cipher, |
| 741 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 742 |
|
| 743 |
static const EVP_CIPHER aes_256_ctr = { |
| 744 |
NID_aes_256_ctr, 1 /* block_size */, 32 /* key_size */, |
| 745 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 746 |
NULL /* app_data */, aes_init_key, aes_ctr_cipher, |
| 747 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 748 |
|
| 749 |
static const EVP_CIPHER aes_256_ecb = { |
| 750 |
NID_aes_256_ecb, 16 /* block_size */, 32 /* key_size */, |
| 751 |
0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 752 |
NULL /* app_data */, aes_init_key, aes_ecb_cipher, |
| 753 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 754 |
|
| 755 |
static const EVP_CIPHER aes_256_ofb = { |
| 756 |
NID_aes_256_ofb128, 1 /* block_size */, 32 /* key_size */, |
| 757 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 758 |
NULL /* app_data */, aes_init_key, aes_ofb_cipher, |
| 759 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 760 |
|
| 761 |
static const EVP_CIPHER aes_256_gcm = { |
| 762 |
NID_aes_256_gcm, 1 /* block_size */, 32 /* key_size */, 12 /* iv_len */, |
| 763 |
sizeof(EVP_AES_GCM_CTX), |
| 764 |
EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 765 |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 766 |
EVP_CIPH_FLAG_AEAD_CIPHER, |
| 767 |
NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 768 |
aes_gcm_ctrl}; |
| 769 |
|
| 770 |
#if !defined(OPENSSL_NO_ASM) && \ |
| 771 |
(defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) |
| 772 |
|
| 773 |
/* AES-NI section. */ |
| 774 |
|
| 775 |
static char aesni_capable(void) { |
| 776 |
return (OPENSSL_ia32cap_P[1] & (1 << (57 - 32))) != 0; |
| 777 |
} |
| 778 |
|
| 779 |
static int aesni_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 780 |
const uint8_t *iv, int enc) { |
| 781 |
int ret, mode; |
| 782 |
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 783 |
|
| 784 |
mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
| 785 |
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { |
| 786 |
ret = aesni_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data); |
| 787 |
dat->block = (block128_f)aesni_decrypt; |
| 788 |
dat->stream.cbc = |
| 789 |
mode == EVP_CIPH_CBC_MODE ? (cbc128_f)aesni_cbc_encrypt : NULL; |
| 790 |
} else { |
| 791 |
ret = aesni_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data); |
| 792 |
dat->block = (block128_f)aesni_encrypt; |
| 793 |
if (mode == EVP_CIPH_CBC_MODE) { |
| 794 |
dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; |
| 795 |
} else if (mode == EVP_CIPH_CTR_MODE) { |
| 796 |
dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
| 797 |
} else { |
| 798 |
dat->stream.cbc = NULL; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
if (ret < 0) { |
| 803 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED); |
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
return 1; |
| 808 |
} |
| 809 |
|
| 810 |
static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 811 |
const uint8_t *in, size_t len) { |
| 812 |
aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, ctx->encrypt); |
| 813 |
|
| 814 |
return 1; |
| 815 |
} |
| 816 |
|
| 817 |
static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 818 |
const uint8_t *in, size_t len) { |
| 819 |
size_t bl = ctx->cipher->block_size; |
| 820 |
|
| 821 |
if (len < bl) { |
| 822 |
return 1; |
| 823 |
} |
| 824 |
|
| 825 |
aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); |
| 826 |
|
| 827 |
return 1; |
| 828 |
} |
| 829 |
|
| 830 |
static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 831 |
const uint8_t *iv, int enc) { |
| 832 |
EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
| 833 |
if (!iv && !key) { |
| 834 |
return 1; |
| 835 |
} |
| 836 |
if (key) { |
| 837 |
aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks); |
| 838 |
CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)aesni_encrypt); |
| 839 |
gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
| 840 |
/* If we have an iv can set it directly, otherwise use |
| 841 |
* saved IV. */ |
| 842 |
if (iv == NULL && gctx->iv_set) { |
| 843 |
iv = gctx->iv; |
| 844 |
} |
| 845 |
if (iv) { |
| 846 |
CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
| 847 |
gctx->iv_set = 1; |
| 848 |
} |
| 849 |
gctx->key_set = 1; |
| 850 |
} else { |
| 851 |
/* If key set use IV, otherwise copy */ |
| 852 |
if (gctx->key_set) { |
| 853 |
CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
| 854 |
} else { |
| 855 |
memcpy(gctx->iv, iv, gctx->ivlen); |
| 856 |
} |
| 857 |
gctx->iv_set = 1; |
| 858 |
gctx->iv_gen = 0; |
| 859 |
} |
| 860 |
return 1; |
| 861 |
} |
| 862 |
|
| 863 |
static const EVP_CIPHER aesni_128_cbc = { |
| 864 |
NID_aes_128_cbc, 16 /* block_size */, 16 /* key_size */, |
| 865 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 866 |
NULL /* app_data */, aesni_init_key, aesni_cbc_cipher, |
| 867 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 868 |
|
| 869 |
static const EVP_CIPHER aesni_128_ctr = { |
| 870 |
NID_aes_128_ctr, 1 /* block_size */, 16 /* key_size */, |
| 871 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 872 |
NULL /* app_data */, aesni_init_key, aes_ctr_cipher, |
| 873 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 874 |
|
| 875 |
static const EVP_CIPHER aesni_128_ecb = { |
| 876 |
NID_aes_128_ecb, 16 /* block_size */, 16 /* key_size */, |
| 877 |
0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 878 |
NULL /* app_data */, aesni_init_key, aesni_ecb_cipher, |
| 879 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 880 |
|
| 881 |
static const EVP_CIPHER aesni_128_ofb = { |
| 882 |
NID_aes_128_ofb128, 1 /* block_size */, 16 /* key_size */, |
| 883 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 884 |
NULL /* app_data */, aesni_init_key, aes_ofb_cipher, |
| 885 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 886 |
|
| 887 |
static const EVP_CIPHER aesni_128_gcm = { |
| 888 |
NID_aes_128_gcm, 1 /* block_size */, 16 /* key_size */, 12 /* iv_len */, |
| 889 |
sizeof(EVP_AES_GCM_CTX), |
| 890 |
EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 891 |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 892 |
EVP_CIPH_FLAG_AEAD_CIPHER, |
| 893 |
NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 894 |
aes_gcm_ctrl}; |
| 895 |
|
| 896 |
|
| 897 |
static const EVP_CIPHER aesni_192_cbc = { |
| 898 |
NID_aes_192_cbc, 16 /* block_size */, 24 /* key_size */, |
| 899 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 900 |
NULL /* app_data */, aesni_init_key, aesni_cbc_cipher, |
| 901 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 902 |
|
| 903 |
static const EVP_CIPHER aesni_192_ctr = { |
| 904 |
NID_aes_192_ctr, 1 /* block_size */, 24 /* key_size */, |
| 905 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 906 |
NULL /* app_data */, aesni_init_key, aes_ctr_cipher, |
| 907 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 908 |
|
| 909 |
static const EVP_CIPHER aesni_192_ecb = { |
| 910 |
NID_aes_192_ecb, 16 /* block_size */, 24 /* key_size */, |
| 911 |
0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 912 |
NULL /* app_data */, aesni_init_key, aesni_ecb_cipher, |
| 913 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 914 |
|
| 915 |
static const EVP_CIPHER aesni_192_gcm = { |
| 916 |
NID_aes_192_gcm, 1 /* block_size */, 24 /* key_size */, 12 /* iv_len */, |
| 917 |
sizeof(EVP_AES_GCM_CTX), |
| 918 |
EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 919 |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 920 |
EVP_CIPH_FLAG_AEAD_CIPHER, |
| 921 |
NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 922 |
aes_gcm_ctrl}; |
| 923 |
|
| 924 |
|
| 925 |
static const EVP_CIPHER aesni_256_cbc = { |
| 926 |
NID_aes_256_cbc, 16 /* block_size */, 32 /* key_size */, |
| 927 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 928 |
NULL /* app_data */, aesni_init_key, aesni_cbc_cipher, |
| 929 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 930 |
|
| 931 |
static const EVP_CIPHER aesni_256_ctr = { |
| 932 |
NID_aes_256_ctr, 1 /* block_size */, 32 /* key_size */, |
| 933 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 934 |
NULL /* app_data */, aesni_init_key, aes_ctr_cipher, |
| 935 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 936 |
|
| 937 |
static const EVP_CIPHER aesni_256_ecb = { |
| 938 |
NID_aes_256_ecb, 16 /* block_size */, 32 /* key_size */, |
| 939 |
0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 940 |
NULL /* app_data */, aesni_init_key, aesni_ecb_cipher, |
| 941 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 942 |
|
| 943 |
static const EVP_CIPHER aesni_256_ofb = { |
| 944 |
NID_aes_256_ofb128, 1 /* block_size */, 32 /* key_size */, |
| 945 |
16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 946 |
NULL /* app_data */, aesni_init_key, aes_ofb_cipher, |
| 947 |
NULL /* cleanup */, NULL /* ctrl */}; |
| 948 |
|
| 949 |
static const EVP_CIPHER aesni_256_gcm = { |
| 950 |
NID_aes_256_gcm, 1 /* block_size */, 32 /* key_size */, 12 /* iv_len */, |
| 951 |
sizeof(EVP_AES_GCM_CTX), |
| 952 |
EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 953 |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY | |
| 954 |
EVP_CIPH_FLAG_AEAD_CIPHER, |
| 955 |
NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 956 |
aes_gcm_ctrl}; |
| 957 |
|
| 958 |
#define EVP_CIPHER_FUNCTION(keybits, mode) \ |
| 959 |
const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \ |
| 960 |
if (aesni_capable()) { \ |
| 961 |
return &aesni_##keybits##_##mode; \ |
| 962 |
} else { \ |
| 963 |
return &aes_##keybits##_##mode; \ |
| 964 |
} \ |
| 965 |
} |
| 966 |
|
| 967 |
#else /* ^^^ OPENSSL_X86_64 || OPENSSL_X86 */ |
| 968 |
|
| 969 |
static char aesni_capable(void) { |
| 970 |
return 0; |
| 971 |
} |
| 972 |
|
| 973 |
#define EVP_CIPHER_FUNCTION(keybits, mode) \ |
| 974 |
const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \ |
| 975 |
return &aes_##keybits##_##mode; \ |
| 976 |
} |
| 977 |
|
| 978 |
#endif |
| 979 |
|
| 980 |
EVP_CIPHER_FUNCTION(128, cbc) |
| 981 |
EVP_CIPHER_FUNCTION(128, ctr) |
| 982 |
EVP_CIPHER_FUNCTION(128, ecb) |
| 983 |
EVP_CIPHER_FUNCTION(128, ofb) |
| 984 |
EVP_CIPHER_FUNCTION(128, gcm) |
| 985 |
|
| 986 |
EVP_CIPHER_FUNCTION(192, cbc) |
| 987 |
EVP_CIPHER_FUNCTION(192, ctr) |
| 988 |
EVP_CIPHER_FUNCTION(192, ecb) |
| 989 |
EVP_CIPHER_FUNCTION(192, gcm) |
| 990 |
|
| 991 |
EVP_CIPHER_FUNCTION(256, cbc) |
| 992 |
EVP_CIPHER_FUNCTION(256, ctr) |
| 993 |
EVP_CIPHER_FUNCTION(256, ecb) |
| 994 |
EVP_CIPHER_FUNCTION(256, ofb) |
| 995 |
EVP_CIPHER_FUNCTION(256, gcm) |
| 996 |
|
| 997 |
|
| 998 |
#define EVP_AEAD_AES_GCM_TAG_LEN 16 |
| 999 |
|
| 1000 |
struct aead_aes_gcm_ctx { |
| 1001 |
union { |
| 1002 |
double align; |
| 1003 |
AES_KEY ks; |
| 1004 |
} ks; |
| 1005 |
GCM128_CONTEXT gcm; |
| 1006 |
ctr128_f ctr; |
| 1007 |
uint8_t tag_len; |
| 1008 |
}; |
| 1009 |
|
| 1010 |
static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 1011 |
size_t key_len, size_t tag_len) { |
| 1012 |
struct aead_aes_gcm_ctx *gcm_ctx; |
| 1013 |
const size_t key_bits = key_len * 8; |
| 1014 |
|
| 1015 |
if (key_bits != 128 && key_bits != 256) { |
| 1016 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 1017 |
return 0; /* EVP_AEAD_CTX_init should catch this. */ |
| 1018 |
} |
| 1019 |
|
| 1020 |
if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 1021 |
tag_len = EVP_AEAD_AES_GCM_TAG_LEN; |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) { |
| 1025 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
| 1026 |
return 0; |
| 1027 |
} |
| 1028 |
|
| 1029 |
gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx)); |
| 1030 |
if (gcm_ctx == NULL) { |
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
|
| 1034 |
gcm_ctx->ctr = |
| 1035 |
aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, NULL, key, key_len); |
| 1036 |
gcm_ctx->tag_len = tag_len; |
| 1037 |
ctx->aead_state = gcm_ctx; |
| 1038 |
|
| 1039 |
return 1; |
| 1040 |
} |
| 1041 |
|
| 1042 |
static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) { |
| 1043 |
struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
| 1044 |
OPENSSL_cleanse(gcm_ctx, sizeof(struct aead_aes_gcm_ctx)); |
| 1045 |
OPENSSL_free(gcm_ctx); |
| 1046 |
} |
| 1047 |
|
| 1048 |
static int aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1049 |
size_t *out_len, size_t max_out_len, |
| 1050 |
const uint8_t *nonce, size_t nonce_len, |
| 1051 |
const uint8_t *in, size_t in_len, |
| 1052 |
const uint8_t *ad, size_t ad_len) { |
| 1053 |
const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
| 1054 |
GCM128_CONTEXT gcm; |
| 1055 |
|
| 1056 |
if (in_len + gcm_ctx->tag_len < in_len) { |
| 1057 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 1058 |
return 0; |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (max_out_len < in_len + gcm_ctx->tag_len) { |
| 1062 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1063 |
return 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
const AES_KEY *key = &gcm_ctx->ks.ks; |
| 1067 |
|
| 1068 |
memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); |
| 1069 |
CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len); |
| 1070 |
|
| 1071 |
if (ad_len > 0 && !CRYPTO_gcm128_aad(&gcm, ad, ad_len)) { |
| 1072 |
return 0; |
| 1073 |
} |
| 1074 |
|
| 1075 |
if (gcm_ctx->ctr) { |
| 1076 |
if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, in, out, in_len, |
| 1077 |
gcm_ctx->ctr)) { |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
} else { |
| 1081 |
if (!CRYPTO_gcm128_encrypt(&gcm, key, in, out, in_len)) { |
| 1082 |
return 0; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len); |
| 1087 |
*out_len = in_len + gcm_ctx->tag_len; |
| 1088 |
return 1; |
| 1089 |
} |
| 1090 |
|
| 1091 |
static int aead_aes_gcm_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1092 |
size_t *out_len, size_t max_out_len, |
| 1093 |
const uint8_t *nonce, size_t nonce_len, |
| 1094 |
const uint8_t *in, size_t in_len, |
| 1095 |
const uint8_t *ad, size_t ad_len) { |
| 1096 |
const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
| 1097 |
uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN]; |
| 1098 |
size_t plaintext_len; |
| 1099 |
GCM128_CONTEXT gcm; |
| 1100 |
|
| 1101 |
if (in_len < gcm_ctx->tag_len) { |
| 1102 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
|
| 1106 |
plaintext_len = in_len - gcm_ctx->tag_len; |
| 1107 |
|
| 1108 |
if (max_out_len < plaintext_len) { |
| 1109 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1110 |
return 0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
const AES_KEY *key = &gcm_ctx->ks.ks; |
| 1114 |
|
| 1115 |
memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); |
| 1116 |
CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len); |
| 1117 |
|
| 1118 |
if (!CRYPTO_gcm128_aad(&gcm, ad, ad_len)) { |
| 1119 |
return 0; |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (gcm_ctx->ctr) { |
| 1123 |
if (!CRYPTO_gcm128_decrypt_ctr32(&gcm, key, in, out, |
| 1124 |
in_len - gcm_ctx->tag_len, gcm_ctx->ctr)) { |
| 1125 |
return 0; |
| 1126 |
} |
| 1127 |
} else { |
| 1128 |
if (!CRYPTO_gcm128_decrypt(&gcm, key, in, out, in_len - gcm_ctx->tag_len)) { |
| 1129 |
return 0; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len); |
| 1134 |
if (CRYPTO_memcmp(tag, in + plaintext_len, gcm_ctx->tag_len) != 0) { |
| 1135 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
|
| 1139 |
*out_len = plaintext_len; |
| 1140 |
return 1; |
| 1141 |
} |
| 1142 |
|
| 1143 |
static const EVP_AEAD aead_aes_128_gcm = { |
| 1144 |
16, /* key len */ |
| 1145 |
12, /* nonce len */ |
| 1146 |
EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */ |
| 1147 |
EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */ |
| 1148 |
aead_aes_gcm_init, |
| 1149 |
NULL, /* init_with_direction */ |
| 1150 |
aead_aes_gcm_cleanup, |
| 1151 |
aead_aes_gcm_seal, |
| 1152 |
aead_aes_gcm_open, |
| 1153 |
NULL, /* get_rc4_state */ |
| 1154 |
NULL, /* get_iv */ |
| 1155 |
}; |
| 1156 |
|
| 1157 |
static const EVP_AEAD aead_aes_256_gcm = { |
| 1158 |
32, /* key len */ |
| 1159 |
12, /* nonce len */ |
| 1160 |
EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */ |
| 1161 |
EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */ |
| 1162 |
aead_aes_gcm_init, |
| 1163 |
NULL, /* init_with_direction */ |
| 1164 |
aead_aes_gcm_cleanup, |
| 1165 |
aead_aes_gcm_seal, |
| 1166 |
aead_aes_gcm_open, |
| 1167 |
NULL, /* get_rc4_state */ |
| 1168 |
NULL, /* get_iv */ |
| 1169 |
}; |
| 1170 |
|
| 1171 |
const EVP_AEAD *EVP_aead_aes_128_gcm(void) { return &aead_aes_128_gcm; } |
| 1172 |
|
| 1173 |
const EVP_AEAD *EVP_aead_aes_256_gcm(void) { return &aead_aes_256_gcm; } |
| 1174 |
|
| 1175 |
|
| 1176 |
/* AES Key Wrap is specified in |
| 1177 |
* http://csrc.nist.gov/groups/ST/toolkit/documents/kms/key-wrap.pdf |
| 1178 |
* or https://tools.ietf.org/html/rfc3394 */ |
| 1179 |
|
| 1180 |
struct aead_aes_key_wrap_ctx { |
| 1181 |
uint8_t key[32]; |
| 1182 |
unsigned key_bits; |
| 1183 |
}; |
| 1184 |
|
| 1185 |
static int aead_aes_key_wrap_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 1186 |
size_t key_len, size_t tag_len) { |
| 1187 |
struct aead_aes_key_wrap_ctx *kw_ctx; |
| 1188 |
const size_t key_bits = key_len * 8; |
| 1189 |
|
| 1190 |
if (key_bits != 128 && key_bits != 256) { |
| 1191 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 1192 |
return 0; /* EVP_AEAD_CTX_init should catch this. */ |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 1196 |
tag_len = 8; |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (tag_len != 8) { |
| 1200 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE); |
| 1201 |
return 0; |
| 1202 |
} |
| 1203 |
|
| 1204 |
kw_ctx = OPENSSL_malloc(sizeof(struct aead_aes_key_wrap_ctx)); |
| 1205 |
if (kw_ctx == NULL) { |
| 1206 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
| 1207 |
return 0; |
| 1208 |
} |
| 1209 |
|
| 1210 |
memcpy(kw_ctx->key, key, key_len); |
| 1211 |
kw_ctx->key_bits = key_bits; |
| 1212 |
|
| 1213 |
ctx->aead_state = kw_ctx; |
| 1214 |
return 1; |
| 1215 |
} |
| 1216 |
|
| 1217 |
static void aead_aes_key_wrap_cleanup(EVP_AEAD_CTX *ctx) { |
| 1218 |
struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state; |
| 1219 |
OPENSSL_cleanse(kw_ctx, sizeof(struct aead_aes_key_wrap_ctx)); |
| 1220 |
OPENSSL_free(kw_ctx); |
| 1221 |
} |
| 1222 |
|
| 1223 |
/* kDefaultAESKeyWrapNonce is the default nonce value given in 2.2.3.1. */ |
| 1224 |
static const uint8_t kDefaultAESKeyWrapNonce[8] = {0xa6, 0xa6, 0xa6, 0xa6, |
| 1225 |
0xa6, 0xa6, 0xa6, 0xa6}; |
| 1226 |
|
| 1227 |
|
| 1228 |
static int aead_aes_key_wrap_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1229 |
size_t *out_len, size_t max_out_len, |
| 1230 |
const uint8_t *nonce, size_t nonce_len, |
| 1231 |
const uint8_t *in, size_t in_len, |
| 1232 |
const uint8_t *ad, size_t ad_len) { |
| 1233 |
const struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state; |
| 1234 |
union { |
| 1235 |
double align; |
| 1236 |
AES_KEY ks; |
| 1237 |
} ks; |
| 1238 |
/* Variables in this function match up with the variables in the second half |
| 1239 |
* of section 2.2.1. */ |
| 1240 |
unsigned i, j, n; |
| 1241 |
uint8_t A[AES_BLOCK_SIZE]; |
| 1242 |
|
| 1243 |
if (ad_len != 0) { |
| 1244 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_AD_SIZE); |
| 1245 |
return 0; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (nonce_len == 0) { |
| 1249 |
nonce = kDefaultAESKeyWrapNonce; |
| 1250 |
nonce_len = sizeof(kDefaultAESKeyWrapNonce); |
| 1251 |
} |
| 1252 |
|
| 1253 |
if (nonce_len != 8) { |
| 1254 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 1255 |
return 0; |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (in_len % 8 != 0) { |
| 1259 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_INPUT_SIZE); |
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* The code below only handles a 32-bit |t| thus 6*|n| must be less than |
| 1264 |
* 2^32, where |n| is |in_len| / 8. So in_len < 4/3 * 2^32 and we |
| 1265 |
* conservatively cap it to 2^32-16 to stop 32-bit platforms complaining that |
| 1266 |
* a comparison is always true. */ |
| 1267 |
if (in_len > 0xfffffff0) { |
| 1268 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 1269 |
return 0; |
| 1270 |
} |
| 1271 |
|
| 1272 |
n = in_len / 8; |
| 1273 |
|
| 1274 |
if (n < 2) { |
| 1275 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_INPUT_SIZE); |
| 1276 |
return 0; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (in_len + 8 < in_len) { |
| 1280 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 1281 |
return 0; |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (max_out_len < in_len + 8) { |
| 1285 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1286 |
return 0; |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (AES_set_encrypt_key(kw_ctx->key, kw_ctx->key_bits, &ks.ks) < 0) { |
| 1290 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED); |
| 1291 |
return 0; |
| 1292 |
} |
| 1293 |
|
| 1294 |
memmove(out + 8, in, in_len); |
| 1295 |
memcpy(A, nonce, 8); |
| 1296 |
|
| 1297 |
for (j = 0; j < 6; j++) { |
| 1298 |
for (i = 1; i <= n; i++) { |
| 1299 |
uint32_t t; |
| 1300 |
|
| 1301 |
memcpy(A + 8, out + 8 * i, 8); |
| 1302 |
AES_encrypt(A, A, &ks.ks); |
| 1303 |
t = n * j + i; |
| 1304 |
A[7] ^= t & 0xff; |
| 1305 |
A[6] ^= (t >> 8) & 0xff; |
| 1306 |
A[5] ^= (t >> 16) & 0xff; |
| 1307 |
A[4] ^= (t >> 24) & 0xff; |
| 1308 |
memcpy(out + 8 * i, A + 8, 8); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
memcpy(out, A, 8); |
| 1313 |
*out_len = in_len + 8; |
| 1314 |
return 1; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static int aead_aes_key_wrap_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1318 |
size_t *out_len, size_t max_out_len, |
| 1319 |
const uint8_t *nonce, size_t nonce_len, |
| 1320 |
const uint8_t *in, size_t in_len, |
| 1321 |
const uint8_t *ad, size_t ad_len) { |
| 1322 |
const struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state; |
| 1323 |
union { |
| 1324 |
double align; |
| 1325 |
AES_KEY ks; |
| 1326 |
} ks; |
| 1327 |
/* Variables in this function match up with the variables in the second half |
| 1328 |
* of section 2.2.1. */ |
| 1329 |
unsigned i, j, n; |
| 1330 |
uint8_t A[AES_BLOCK_SIZE]; |
| 1331 |
|
| 1332 |
if (ad_len != 0) { |
| 1333 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_AD_SIZE); |
| 1334 |
return 0; |
| 1335 |
} |
| 1336 |
|
| 1337 |
if (nonce_len == 0) { |
| 1338 |
nonce = kDefaultAESKeyWrapNonce; |
| 1339 |
nonce_len = sizeof(kDefaultAESKeyWrapNonce); |
| 1340 |
} |
| 1341 |
|
| 1342 |
if (nonce_len != 8) { |
| 1343 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 1344 |
return 0; |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (in_len % 8 != 0) { |
| 1348 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_INPUT_SIZE); |
| 1349 |
return 0; |
| 1350 |
} |
| 1351 |
|
| 1352 |
/* The code below only handles a 32-bit |t| thus 6*|n| must be less than |
| 1353 |
* 2^32, where |n| is |in_len| / 8. So in_len < 4/3 * 2^32 and we |
| 1354 |
* conservatively cap it to 2^32-8 to stop 32-bit platforms complaining that |
| 1355 |
* a comparison is always true. */ |
| 1356 |
if (in_len > 0xfffffff8) { |
| 1357 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 1358 |
return 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
if (in_len < 24) { |
| 1362 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 1363 |
return 0; |
| 1364 |
} |
| 1365 |
|
| 1366 |
n = (in_len / 8) - 1; |
| 1367 |
|
| 1368 |
if (max_out_len < in_len - 8) { |
| 1369 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1370 |
return 0; |
| 1371 |
} |
| 1372 |
|
| 1373 |
if (AES_set_decrypt_key(kw_ctx->key, kw_ctx->key_bits, &ks.ks) < 0) { |
| 1374 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED); |
| 1375 |
return 0; |
| 1376 |
} |
| 1377 |
|
| 1378 |
memcpy(A, in, 8); |
| 1379 |
memmove(out, in + 8, in_len - 8); |
| 1380 |
|
| 1381 |
for (j = 5; j < 6; j--) { |
| 1382 |
for (i = n; i > 0; i--) { |
| 1383 |
uint32_t t; |
| 1384 |
|
| 1385 |
t = n * j + i; |
| 1386 |
A[7] ^= t & 0xff; |
| 1387 |
A[6] ^= (t >> 8) & 0xff; |
| 1388 |
A[5] ^= (t >> 16) & 0xff; |
| 1389 |
A[4] ^= (t >> 24) & 0xff; |
| 1390 |
memcpy(A + 8, out + 8 * (i - 1), 8); |
| 1391 |
AES_decrypt(A, A, &ks.ks); |
| 1392 |
memcpy(out + 8 * (i - 1), A + 8, 8); |
| 1393 |
} |
| 1394 |
} |
| 1395 |
|
| 1396 |
if (CRYPTO_memcmp(A, nonce, 8) != 0) { |
| 1397 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
|
| 1401 |
*out_len = in_len - 8; |
| 1402 |
return 1; |
| 1403 |
} |
| 1404 |
|
| 1405 |
static const EVP_AEAD aead_aes_128_key_wrap = { |
| 1406 |
16, /* key len */ |
| 1407 |
8, /* nonce len */ |
| 1408 |
8, /* overhead */ |
| 1409 |
8, /* max tag length */ |
| 1410 |
aead_aes_key_wrap_init, |
| 1411 |
NULL, /* init_with_direction */ |
| 1412 |
aead_aes_key_wrap_cleanup, |
| 1413 |
aead_aes_key_wrap_seal, |
| 1414 |
aead_aes_key_wrap_open, |
| 1415 |
NULL, /* get_rc4_state */ |
| 1416 |
NULL, /* get_iv */ |
| 1417 |
}; |
| 1418 |
|
| 1419 |
static const EVP_AEAD aead_aes_256_key_wrap = { |
| 1420 |
32, /* key len */ |
| 1421 |
8, /* nonce len */ |
| 1422 |
8, /* overhead */ |
| 1423 |
8, /* max tag length */ |
| 1424 |
aead_aes_key_wrap_init, |
| 1425 |
NULL, /* init_with_direction */ |
| 1426 |
aead_aes_key_wrap_cleanup, |
| 1427 |
aead_aes_key_wrap_seal, |
| 1428 |
aead_aes_key_wrap_open, |
| 1429 |
NULL, /* get_rc4_state */ |
| 1430 |
NULL, /* get_iv */ |
| 1431 |
}; |
| 1432 |
|
| 1433 |
const EVP_AEAD *EVP_aead_aes_128_key_wrap(void) { return &aead_aes_128_key_wrap; } |
| 1434 |
|
| 1435 |
const EVP_AEAD *EVP_aead_aes_256_key_wrap(void) { return &aead_aes_256_key_wrap; } |
| 1436 |
|
| 1437 |
|
| 1438 |
#define EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN SHA256_DIGEST_LENGTH |
| 1439 |
#define EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN 12 |
| 1440 |
|
| 1441 |
struct aead_aes_ctr_hmac_sha256_ctx { |
| 1442 |
union { |
| 1443 |
double align; |
| 1444 |
AES_KEY ks; |
| 1445 |
} ks; |
| 1446 |
ctr128_f ctr; |
| 1447 |
block128_f block; |
| 1448 |
SHA256_CTX inner_init_state; |
| 1449 |
SHA256_CTX outer_init_state; |
| 1450 |
uint8_t tag_len; |
| 1451 |
}; |
| 1452 |
|
| 1453 |
static void hmac_init(SHA256_CTX *out_inner, SHA256_CTX *out_outer, |
| 1454 |
const uint8_t hmac_key[32]) { |
| 1455 |
static const size_t hmac_key_len = 32; |
| 1456 |
uint8_t block[SHA256_CBLOCK]; |
| 1457 |
memcpy(block, hmac_key, hmac_key_len); |
| 1458 |
memset(block + hmac_key_len, 0x36, sizeof(block) - hmac_key_len); |
| 1459 |
|
| 1460 |
unsigned i; |
| 1461 |
for (i = 0; i < hmac_key_len; i++) { |
| 1462 |
block[i] ^= 0x36; |
| 1463 |
} |
| 1464 |
|
| 1465 |
SHA256_Init(out_inner); |
| 1466 |
SHA256_Update(out_inner, block, sizeof(block)); |
| 1467 |
|
| 1468 |
memset(block + hmac_key_len, 0x5c, sizeof(block) - hmac_key_len); |
| 1469 |
for (i = 0; i < hmac_key_len; i++) { |
| 1470 |
block[i] ^= (0x36 ^ 0x5c); |
| 1471 |
} |
| 1472 |
|
| 1473 |
SHA256_Init(out_outer); |
| 1474 |
SHA256_Update(out_outer, block, sizeof(block)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
static int aead_aes_ctr_hmac_sha256_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 1478 |
size_t key_len, size_t tag_len) { |
| 1479 |
struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx; |
| 1480 |
static const size_t hmac_key_len = 32; |
| 1481 |
|
| 1482 |
if (key_len < hmac_key_len) { |
| 1483 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 1484 |
return 0; /* EVP_AEAD_CTX_init should catch this. */ |
| 1485 |
} |
| 1486 |
|
| 1487 |
const size_t aes_key_len = key_len - hmac_key_len; |
| 1488 |
if (aes_key_len != 16 && aes_key_len != 32) { |
| 1489 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 1490 |
return 0; /* EVP_AEAD_CTX_init should catch this. */ |
| 1491 |
} |
| 1492 |
|
| 1493 |
if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 1494 |
tag_len = EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN; |
| 1495 |
} |
| 1496 |
|
| 1497 |
if (tag_len > EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN) { |
| 1498 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
| 1499 |
return 0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
aes_ctx = OPENSSL_malloc(sizeof(struct aead_aes_ctr_hmac_sha256_ctx)); |
| 1503 |
if (aes_ctx == NULL) { |
| 1504 |
OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
| 1505 |
return 0; |
| 1506 |
} |
| 1507 |
|
| 1508 |
aes_ctx->ctr = |
| 1509 |
aes_ctr_set_key(&aes_ctx->ks.ks, NULL, &aes_ctx->block, key, aes_key_len); |
| 1510 |
aes_ctx->tag_len = tag_len; |
| 1511 |
hmac_init(&aes_ctx->inner_init_state, &aes_ctx->outer_init_state, |
| 1512 |
key + aes_key_len); |
| 1513 |
|
| 1514 |
ctx->aead_state = aes_ctx; |
| 1515 |
|
| 1516 |
return 1; |
| 1517 |
} |
| 1518 |
|
| 1519 |
static void aead_aes_ctr_hmac_sha256_cleanup(EVP_AEAD_CTX *ctx) { |
| 1520 |
struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state; |
| 1521 |
OPENSSL_cleanse(aes_ctx, sizeof(struct aead_aes_ctr_hmac_sha256_ctx)); |
| 1522 |
OPENSSL_free(aes_ctx); |
| 1523 |
} |
| 1524 |
|
| 1525 |
static void hmac_update_uint64(SHA256_CTX *sha256, uint64_t value) { |
| 1526 |
unsigned i; |
| 1527 |
uint8_t bytes[8]; |
| 1528 |
|
| 1529 |
for (i = 0; i < sizeof(bytes); i++) { |
| 1530 |
bytes[i] = value & 0xff; |
| 1531 |
value >>= 8; |
| 1532 |
} |
| 1533 |
SHA256_Update(sha256, bytes, sizeof(bytes)); |
| 1534 |
} |
| 1535 |
|
| 1536 |
static void hmac_calculate(uint8_t out[SHA256_DIGEST_LENGTH], |
| 1537 |
const SHA256_CTX *inner_init_state, |
| 1538 |
const SHA256_CTX *outer_init_state, |
| 1539 |
const uint8_t *ad, size_t ad_len, |
| 1540 |
const uint8_t *nonce, const uint8_t *ciphertext, |
| 1541 |
size_t ciphertext_len) { |
| 1542 |
SHA256_CTX sha256; |
| 1543 |
memcpy(&sha256, inner_init_state, sizeof(sha256)); |
| 1544 |
hmac_update_uint64(&sha256, ad_len); |
| 1545 |
hmac_update_uint64(&sha256, ciphertext_len); |
| 1546 |
SHA256_Update(&sha256, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN); |
| 1547 |
SHA256_Update(&sha256, ad, ad_len); |
| 1548 |
|
| 1549 |
/* Pad with zeros to the end of the SHA-256 block. */ |
| 1550 |
const unsigned num_padding = |
| 1551 |
(SHA256_CBLOCK - ((sizeof(uint64_t)*2 + |
| 1552 |
EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN + ad_len) % |
| 1553 |
SHA256_CBLOCK)) % |
| 1554 |
SHA256_CBLOCK; |
| 1555 |
uint8_t padding[SHA256_CBLOCK]; |
| 1556 |
memset(padding, 0, num_padding); |
| 1557 |
SHA256_Update(&sha256, padding, num_padding); |
| 1558 |
|
| 1559 |
SHA256_Update(&sha256, ciphertext, ciphertext_len); |
| 1560 |
|
| 1561 |
uint8_t inner_digest[SHA256_DIGEST_LENGTH]; |
| 1562 |
SHA256_Final(inner_digest, &sha256); |
| 1563 |
|
| 1564 |
memcpy(&sha256, outer_init_state, sizeof(sha256)); |
| 1565 |
SHA256_Update(&sha256, inner_digest, sizeof(inner_digest)); |
| 1566 |
SHA256_Final(out, &sha256); |
| 1567 |
} |
| 1568 |
|
| 1569 |
static void aead_aes_ctr_hmac_sha256_crypt( |
| 1570 |
const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx, uint8_t *out, |
| 1571 |
const uint8_t *in, size_t len, const uint8_t *nonce) { |
| 1572 |
/* Since the AEAD operation is one-shot, keeping a buffer of unused keystream |
| 1573 |
* bytes is pointless. However, |CRYPTO_ctr128_encrypt| requires it. */ |
| 1574 |
uint8_t partial_block_buffer[AES_BLOCK_SIZE]; |
| 1575 |
unsigned partial_block_offset = 0; |
| 1576 |
memset(partial_block_buffer, 0, sizeof(partial_block_buffer)); |
| 1577 |
|
| 1578 |
uint8_t counter[AES_BLOCK_SIZE]; |
| 1579 |
memcpy(counter, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN); |
| 1580 |
memset(counter + EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN, 0, 4); |
| 1581 |
|
| 1582 |
if (aes_ctx->ctr) { |
| 1583 |
CRYPTO_ctr128_encrypt_ctr32(in, out, len, &aes_ctx->ks.ks, counter, |
| 1584 |
partial_block_buffer, &partial_block_offset, |
| 1585 |
aes_ctx->ctr); |
| 1586 |
} else { |
| 1587 |
CRYPTO_ctr128_encrypt(in, out, len, &aes_ctx->ks.ks, counter, |
| 1588 |
partial_block_buffer, &partial_block_offset, |
| 1589 |
aes_ctx->block); |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
static int aead_aes_ctr_hmac_sha256_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1594 |
size_t *out_len, size_t max_out_len, |
| 1595 |
const uint8_t *nonce, size_t nonce_len, |
| 1596 |
const uint8_t *in, size_t in_len, |
| 1597 |
const uint8_t *ad, size_t ad_len) { |
| 1598 |
const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state; |
| 1599 |
const uint64_t in_len_64 = in_len; |
| 1600 |
|
| 1601 |
if (in_len + aes_ctx->tag_len < in_len || |
| 1602 |
/* This input is so large it would overflow the 32-bit block counter. */ |
| 1603 |
in_len_64 >= (UINT64_C(1) << 32) * AES_BLOCK_SIZE) { |
| 1604 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 1605 |
return 0; |
| 1606 |
} |
| 1607 |
|
| 1608 |
if (max_out_len < in_len + aes_ctx->tag_len) { |
| 1609 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1610 |
return 0; |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) { |
| 1614 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 1615 |
return 0; |
| 1616 |
} |
| 1617 |
|
| 1618 |
aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce); |
| 1619 |
|
| 1620 |
uint8_t hmac_result[SHA256_DIGEST_LENGTH]; |
| 1621 |
hmac_calculate(hmac_result, &aes_ctx->inner_init_state, |
| 1622 |
&aes_ctx->outer_init_state, ad, ad_len, nonce, out, in_len); |
| 1623 |
memcpy(out + in_len, hmac_result, aes_ctx->tag_len); |
| 1624 |
*out_len = in_len + aes_ctx->tag_len; |
| 1625 |
|
| 1626 |
return 1; |
| 1627 |
} |
| 1628 |
|
| 1629 |
static int aead_aes_ctr_hmac_sha256_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1630 |
size_t *out_len, size_t max_out_len, |
| 1631 |
const uint8_t *nonce, size_t nonce_len, |
| 1632 |
const uint8_t *in, size_t in_len, |
| 1633 |
const uint8_t *ad, size_t ad_len) { |
| 1634 |
const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state; |
| 1635 |
size_t plaintext_len; |
| 1636 |
|
| 1637 |
if (in_len < aes_ctx->tag_len) { |
| 1638 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 1639 |
return 0; |
| 1640 |
} |
| 1641 |
|
| 1642 |
plaintext_len = in_len - aes_ctx->tag_len; |
| 1643 |
|
| 1644 |
if (max_out_len < plaintext_len) { |
| 1645 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1646 |
return 0; |
| 1647 |
} |
| 1648 |
|
| 1649 |
if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) { |
| 1650 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 1651 |
return 0; |
| 1652 |
} |
| 1653 |
|
| 1654 |
uint8_t hmac_result[SHA256_DIGEST_LENGTH]; |
| 1655 |
hmac_calculate(hmac_result, &aes_ctx->inner_init_state, |
| 1656 |
&aes_ctx->outer_init_state, ad, ad_len, nonce, in, |
| 1657 |
plaintext_len); |
| 1658 |
if (CRYPTO_memcmp(hmac_result, in + plaintext_len, aes_ctx->tag_len) != 0) { |
| 1659 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 1660 |
return 0; |
| 1661 |
} |
| 1662 |
|
| 1663 |
aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, plaintext_len, nonce); |
| 1664 |
|
| 1665 |
*out_len = plaintext_len; |
| 1666 |
return 1; |
| 1667 |
} |
| 1668 |
|
| 1669 |
static const EVP_AEAD aead_aes_128_ctr_hmac_sha256 = { |
| 1670 |
16 /* AES key */ + 32 /* HMAC key */, |
| 1671 |
12, /* nonce length */ |
| 1672 |
EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */ |
| 1673 |
EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */ |
| 1674 |
|
| 1675 |
aead_aes_ctr_hmac_sha256_init, |
| 1676 |
NULL /* init_with_direction */, |
| 1677 |
aead_aes_ctr_hmac_sha256_cleanup, |
| 1678 |
aead_aes_ctr_hmac_sha256_seal, |
| 1679 |
aead_aes_ctr_hmac_sha256_open, |
| 1680 |
NULL /* get_rc4_state */, |
| 1681 |
NULL /* get_iv */, |
| 1682 |
}; |
| 1683 |
|
| 1684 |
static const EVP_AEAD aead_aes_256_ctr_hmac_sha256 = { |
| 1685 |
32 /* AES key */ + 32 /* HMAC key */, |
| 1686 |
12, /* nonce length */ |
| 1687 |
EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */ |
| 1688 |
EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */ |
| 1689 |
|
| 1690 |
aead_aes_ctr_hmac_sha256_init, |
| 1691 |
NULL /* init_with_direction */, |
| 1692 |
aead_aes_ctr_hmac_sha256_cleanup, |
| 1693 |
aead_aes_ctr_hmac_sha256_seal, |
| 1694 |
aead_aes_ctr_hmac_sha256_open, |
| 1695 |
NULL /* get_rc4_state */, |
| 1696 |
NULL /* get_iv */, |
| 1697 |
}; |
| 1698 |
|
| 1699 |
const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void) { |
| 1700 |
return &aead_aes_128_ctr_hmac_sha256; |
| 1701 |
} |
| 1702 |
|
| 1703 |
const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void) { |
| 1704 |
return &aead_aes_256_ctr_hmac_sha256; |
| 1705 |
} |
| 1706 |
|
| 1707 |
int EVP_has_aes_hardware(void) { |
| 1708 |
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 1709 |
return aesni_capable() && crypto_gcm_clmul_enabled(); |
| 1710 |
#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 1711 |
return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable(); |
| 1712 |
#else |
| 1713 |
return 0; |
| 1714 |
#endif |
| 1715 |
} |