2 * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/opensslconf.h>
13 #include <openssl/crypto.h>
14 #include <openssl/evp.h>
15 #include <openssl/err.h>
16 #include <openssl/aes.h>
17 #include <openssl/rand.h>
18 #include <openssl/cmac.h>
19 #include "internal/evp_int.h"
20 #include "internal/cryptlib.h"
21 #include "internal/modes_int.h"
22 #include "internal/siv_int.h"
23 #include "internal/aes_platform.h"
42 } ks; /* AES key schedule to use */
43 int key_set; /* Set if key initialised */
44 int iv_set; /* Set if an iv is set */
46 unsigned char *iv; /* Temporary IV store */
47 int ivlen; /* IV length */
49 int iv_gen; /* It is OK to generate IVs */
50 int iv_gen_rand; /* No IV was specified, so generate a rand IV */
51 int tls_aad_len; /* TLS AAD length */
52 uint64_t tls_enc_records; /* Number of TLS records encrypted */
60 } ks1, ks2; /* AES key schedules to use */
62 void (*stream) (const unsigned char *in,
63 unsigned char *out, size_t length,
64 const AES_KEY *key1, const AES_KEY *key2,
65 const unsigned char iv[16]);
69 static const int allow_insecure_decrypt = 0;
71 static const int allow_insecure_decrypt = 1;
78 } ks; /* AES key schedule to use */
79 int key_set; /* Set if key initialised */
80 int iv_set; /* Set if an iv is set */
81 int tag_set; /* Set if tag is valid */
82 int len_set; /* Set if message length set */
83 int L, M; /* L and M parameters from RFC3610 */
84 int tls_aad_len; /* TLS AAD length */
89 #ifndef OPENSSL_NO_OCB
94 } ksenc; /* AES key schedule to use for encryption */
98 } ksdec; /* AES key schedule to use for decryption */
99 int key_set; /* Set if key initialised */
100 int iv_set; /* Set if an iv is set */
102 unsigned char *iv; /* Temporary IV store */
103 unsigned char tag[16];
104 unsigned char data_buf[16]; /* Store partial data blocks */
105 unsigned char aad_buf[16]; /* Store partial AAD blocks */
108 int ivlen; /* IV length */
113 #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
115 /* increment counter (64-bit int) by 1 */
116 static void ctr64_inc(unsigned char *counter)
131 #if defined(AESNI_CAPABLE)
132 # if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
133 # define AES_gcm_encrypt aesni_gcm_encrypt
134 # define AES_gcm_decrypt aesni_gcm_decrypt
135 # define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \
136 gctx->gcm.ghash==gcm_ghash_avx)
137 # undef AES_GCM_ASM2 /* minor size optimization */
140 static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
141 const unsigned char *iv, int enc)
144 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
146 mode = EVP_CIPHER_CTX_mode(ctx);
147 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
149 ret = aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
151 dat->block = (block128_f) aesni_decrypt;
152 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
153 (cbc128_f) aesni_cbc_encrypt : NULL;
155 ret = aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
157 dat->block = (block128_f) aesni_encrypt;
158 if (mode == EVP_CIPH_CBC_MODE)
159 dat->stream.cbc = (cbc128_f) aesni_cbc_encrypt;
160 else if (mode == EVP_CIPH_CTR_MODE)
161 dat->stream.ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
163 dat->stream.cbc = NULL;
167 EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
174 static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
175 const unsigned char *in, size_t len)
177 aesni_cbc_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks,
178 EVP_CIPHER_CTX_iv_noconst(ctx),
179 EVP_CIPHER_CTX_encrypting(ctx));
184 static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
185 const unsigned char *in, size_t len)
187 size_t bl = EVP_CIPHER_CTX_block_size(ctx);
192 aesni_ecb_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks,
193 EVP_CIPHER_CTX_encrypting(ctx));
198 # define aesni_ofb_cipher aes_ofb_cipher
199 static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
200 const unsigned char *in, size_t len);
202 # define aesni_cfb_cipher aes_cfb_cipher
203 static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
204 const unsigned char *in, size_t len);
206 # define aesni_cfb8_cipher aes_cfb8_cipher
207 static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
208 const unsigned char *in, size_t len);
210 # define aesni_cfb1_cipher aes_cfb1_cipher
211 static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
212 const unsigned char *in, size_t len);
214 # define aesni_ctr_cipher aes_ctr_cipher
215 static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
216 const unsigned char *in, size_t len);
218 static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
219 const unsigned char *iv, int enc)
221 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
225 aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
227 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f) aesni_encrypt);
228 gctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
230 * If we have an iv can set it directly, otherwise use saved IV.
232 if (iv == NULL && gctx->iv_set)
235 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
240 /* If key set use IV, otherwise copy */
242 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
244 memcpy(gctx->iv, iv, gctx->ivlen);
251 # define aesni_gcm_cipher aes_gcm_cipher
252 static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
253 const unsigned char *in, size_t len);
255 static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
256 const unsigned char *iv, int enc)
258 EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
264 /* The key is two half length keys in reality */
265 const int bytes = EVP_CIPHER_CTX_key_length(ctx) / 2;
266 const int bits = bytes * 8;
269 * Verify that the two keys are different.
271 * This addresses Rogaway's vulnerability.
272 * See comment in aes_xts_init_key() below.
274 if ((!allow_insecure_decrypt || enc)
275 && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
276 EVPerr(EVP_F_AESNI_XTS_INIT_KEY, EVP_R_XTS_DUPLICATED_KEYS);
280 /* key_len is two AES keys */
282 aesni_set_encrypt_key(key, bits, &xctx->ks1.ks);
283 xctx->xts.block1 = (block128_f) aesni_encrypt;
284 xctx->stream = aesni_xts_encrypt;
286 aesni_set_decrypt_key(key, bits, &xctx->ks1.ks);
287 xctx->xts.block1 = (block128_f) aesni_decrypt;
288 xctx->stream = aesni_xts_decrypt;
291 aesni_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
292 xctx->xts.block2 = (block128_f) aesni_encrypt;
294 xctx->xts.key1 = &xctx->ks1;
298 xctx->xts.key2 = &xctx->ks2;
299 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
305 # define aesni_xts_cipher aes_xts_cipher
306 static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
307 const unsigned char *in, size_t len);
309 static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
310 const unsigned char *iv, int enc)
312 EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
316 aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
318 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
319 &cctx->ks, (block128_f) aesni_encrypt);
320 cctx->str = enc ? (ccm128_f) aesni_ccm64_encrypt_blocks :
321 (ccm128_f) aesni_ccm64_decrypt_blocks;
325 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
331 # define aesni_ccm_cipher aes_ccm_cipher
332 static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
333 const unsigned char *in, size_t len);
335 # ifndef OPENSSL_NO_OCB
336 static int aesni_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
337 const unsigned char *iv, int enc)
339 EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
345 * We set both the encrypt and decrypt key here because decrypt
346 * needs both. We could possibly optimise to remove setting the
347 * decrypt for an encryption operation.
349 aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
351 aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
353 if (!CRYPTO_ocb128_init(&octx->ocb,
354 &octx->ksenc.ks, &octx->ksdec.ks,
355 (block128_f) aesni_encrypt,
356 (block128_f) aesni_decrypt,
357 enc ? aesni_ocb_encrypt
358 : aesni_ocb_decrypt))
364 * If we have an iv we can set it directly, otherwise use saved IV.
366 if (iv == NULL && octx->iv_set)
369 if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
376 /* If key set use IV, otherwise copy */
378 CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
380 memcpy(octx->iv, iv, octx->ivlen);
386 # define aesni_ocb_cipher aes_ocb_cipher
387 static int aesni_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
388 const unsigned char *in, size_t len);
389 # endif /* OPENSSL_NO_OCB */
391 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
392 static const EVP_CIPHER aesni_##keylen##_##mode = { \
393 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
394 flags|EVP_CIPH_##MODE##_MODE, \
396 aesni_##mode##_cipher, \
398 sizeof(EVP_AES_KEY), \
399 NULL,NULL,NULL,NULL }; \
400 static const EVP_CIPHER aes_##keylen##_##mode = { \
401 nid##_##keylen##_##nmode,blocksize, \
403 flags|EVP_CIPH_##MODE##_MODE, \
405 aes_##mode##_cipher, \
407 sizeof(EVP_AES_KEY), \
408 NULL,NULL,NULL,NULL }; \
409 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
410 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
412 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
413 static const EVP_CIPHER aesni_##keylen##_##mode = { \
414 nid##_##keylen##_##mode,blocksize, \
415 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
417 flags|EVP_CIPH_##MODE##_MODE, \
418 aesni_##mode##_init_key, \
419 aesni_##mode##_cipher, \
420 aes_##mode##_cleanup, \
421 sizeof(EVP_AES_##MODE##_CTX), \
422 NULL,NULL,aes_##mode##_ctrl,NULL }; \
423 static const EVP_CIPHER aes_##keylen##_##mode = { \
424 nid##_##keylen##_##mode,blocksize, \
425 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
427 flags|EVP_CIPH_##MODE##_MODE, \
428 aes_##mode##_init_key, \
429 aes_##mode##_cipher, \
430 aes_##mode##_cleanup, \
431 sizeof(EVP_AES_##MODE##_CTX), \
432 NULL,NULL,aes_##mode##_ctrl,NULL }; \
433 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
434 { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
436 #elif defined(SPARC_AES_CAPABLE)
438 static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
439 const unsigned char *iv, int enc)
442 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
444 mode = EVP_CIPHER_CTX_mode(ctx);
445 bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
446 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
449 aes_t4_set_decrypt_key(key, bits, &dat->ks.ks);
450 dat->block = (block128_f) aes_t4_decrypt;
453 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
454 (cbc128_f) aes128_t4_cbc_decrypt : NULL;
457 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
458 (cbc128_f) aes192_t4_cbc_decrypt : NULL;
461 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
462 (cbc128_f) aes256_t4_cbc_decrypt : NULL;
469 aes_t4_set_encrypt_key(key, bits, &dat->ks.ks);
470 dat->block = (block128_f) aes_t4_encrypt;
473 if (mode == EVP_CIPH_CBC_MODE)
474 dat->stream.cbc = (cbc128_f) aes128_t4_cbc_encrypt;
475 else if (mode == EVP_CIPH_CTR_MODE)
476 dat->stream.ctr = (ctr128_f) aes128_t4_ctr32_encrypt;
478 dat->stream.cbc = NULL;
481 if (mode == EVP_CIPH_CBC_MODE)
482 dat->stream.cbc = (cbc128_f) aes192_t4_cbc_encrypt;
483 else if (mode == EVP_CIPH_CTR_MODE)
484 dat->stream.ctr = (ctr128_f) aes192_t4_ctr32_encrypt;
486 dat->stream.cbc = NULL;
489 if (mode == EVP_CIPH_CBC_MODE)
490 dat->stream.cbc = (cbc128_f) aes256_t4_cbc_encrypt;
491 else if (mode == EVP_CIPH_CTR_MODE)
492 dat->stream.ctr = (ctr128_f) aes256_t4_ctr32_encrypt;
494 dat->stream.cbc = NULL;
502 EVPerr(EVP_F_AES_T4_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
509 # define aes_t4_cbc_cipher aes_cbc_cipher
510 static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
511 const unsigned char *in, size_t len);
513 # define aes_t4_ecb_cipher aes_ecb_cipher
514 static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
515 const unsigned char *in, size_t len);
517 # define aes_t4_ofb_cipher aes_ofb_cipher
518 static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
519 const unsigned char *in, size_t len);
521 # define aes_t4_cfb_cipher aes_cfb_cipher
522 static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
523 const unsigned char *in, size_t len);
525 # define aes_t4_cfb8_cipher aes_cfb8_cipher
526 static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
527 const unsigned char *in, size_t len);
529 # define aes_t4_cfb1_cipher aes_cfb1_cipher
530 static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
531 const unsigned char *in, size_t len);
533 # define aes_t4_ctr_cipher aes_ctr_cipher
534 static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
535 const unsigned char *in, size_t len);
537 static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
538 const unsigned char *iv, int enc)
540 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
544 int bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
545 aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
546 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
547 (block128_f) aes_t4_encrypt);
550 gctx->ctr = (ctr128_f) aes128_t4_ctr32_encrypt;
553 gctx->ctr = (ctr128_f) aes192_t4_ctr32_encrypt;
556 gctx->ctr = (ctr128_f) aes256_t4_ctr32_encrypt;
562 * If we have an iv can set it directly, otherwise use saved IV.
564 if (iv == NULL && gctx->iv_set)
567 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
572 /* If key set use IV, otherwise copy */
574 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
576 memcpy(gctx->iv, iv, gctx->ivlen);
583 # define aes_t4_gcm_cipher aes_gcm_cipher
584 static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
585 const unsigned char *in, size_t len);
587 static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
588 const unsigned char *iv, int enc)
590 EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
596 /* The key is two half length keys in reality */
597 const int bytes = EVP_CIPHER_CTX_key_length(ctx) / 2;
598 const int bits = bytes * 8;
601 * Verify that the two keys are different.
603 * This addresses Rogaway's vulnerability.
604 * See comment in aes_xts_init_key() below.
606 if ((!allow_insecure_decrypt || enc)
607 && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
608 EVPerr(EVP_F_AES_T4_XTS_INIT_KEY, EVP_R_XTS_DUPLICATED_KEYS);
613 /* key_len is two AES keys */
615 aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
616 xctx->xts.block1 = (block128_f) aes_t4_encrypt;
619 xctx->stream = aes128_t4_xts_encrypt;
622 xctx->stream = aes256_t4_xts_encrypt;
628 aes_t4_set_decrypt_key(key, bits, &xctx->ks1.ks);
629 xctx->xts.block1 = (block128_f) aes_t4_decrypt;
632 xctx->stream = aes128_t4_xts_decrypt;
635 xctx->stream = aes256_t4_xts_decrypt;
642 aes_t4_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
643 xctx->xts.block2 = (block128_f) aes_t4_encrypt;
645 xctx->xts.key1 = &xctx->ks1;
649 xctx->xts.key2 = &xctx->ks2;
650 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
656 # define aes_t4_xts_cipher aes_xts_cipher
657 static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
658 const unsigned char *in, size_t len);
660 static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
661 const unsigned char *iv, int enc)
663 EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
667 int bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
668 aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
669 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
670 &cctx->ks, (block128_f) aes_t4_encrypt);
675 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
681 # define aes_t4_ccm_cipher aes_ccm_cipher
682 static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
683 const unsigned char *in, size_t len);
685 # ifndef OPENSSL_NO_OCB
686 static int aes_t4_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
687 const unsigned char *iv, int enc)
689 EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
695 * We set both the encrypt and decrypt key here because decrypt
696 * needs both. We could possibly optimise to remove setting the
697 * decrypt for an encryption operation.
699 aes_t4_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
701 aes_t4_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
703 if (!CRYPTO_ocb128_init(&octx->ocb,
704 &octx->ksenc.ks, &octx->ksdec.ks,
705 (block128_f) aes_t4_encrypt,
706 (block128_f) aes_t4_decrypt,
713 * If we have an iv we can set it directly, otherwise use saved IV.
715 if (iv == NULL && octx->iv_set)
718 if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
725 /* If key set use IV, otherwise copy */
727 CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
729 memcpy(octx->iv, iv, octx->ivlen);
735 # define aes_t4_ocb_cipher aes_ocb_cipher
736 static int aes_t4_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
737 const unsigned char *in, size_t len);
738 # endif /* OPENSSL_NO_OCB */
740 # ifndef OPENSSL_NO_SIV
741 # define aes_t4_siv_init_key aes_siv_init_key
742 # define aes_t4_siv_cipher aes_siv_cipher
743 # endif /* OPENSSL_NO_SIV */
745 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
746 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
747 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
748 flags|EVP_CIPH_##MODE##_MODE, \
750 aes_t4_##mode##_cipher, \
752 sizeof(EVP_AES_KEY), \
753 NULL,NULL,NULL,NULL }; \
754 static const EVP_CIPHER aes_##keylen##_##mode = { \
755 nid##_##keylen##_##nmode,blocksize, \
757 flags|EVP_CIPH_##MODE##_MODE, \
759 aes_##mode##_cipher, \
761 sizeof(EVP_AES_KEY), \
762 NULL,NULL,NULL,NULL }; \
763 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
764 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
766 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
767 static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
768 nid##_##keylen##_##mode,blocksize, \
769 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
771 flags|EVP_CIPH_##MODE##_MODE, \
772 aes_t4_##mode##_init_key, \
773 aes_t4_##mode##_cipher, \
774 aes_##mode##_cleanup, \
775 sizeof(EVP_AES_##MODE##_CTX), \
776 NULL,NULL,aes_##mode##_ctrl,NULL }; \
777 static const EVP_CIPHER aes_##keylen##_##mode = { \
778 nid##_##keylen##_##mode,blocksize, \
779 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
781 flags|EVP_CIPH_##MODE##_MODE, \
782 aes_##mode##_init_key, \
783 aes_##mode##_cipher, \
784 aes_##mode##_cleanup, \
785 sizeof(EVP_AES_##MODE##_CTX), \
786 NULL,NULL,aes_##mode##_ctrl,NULL }; \
787 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
788 { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
790 #elif defined(S390X_aes_128_CAPABLE)
791 /* IBM S390X support */
796 * KM-AES parameter block - begin
797 * (see z/Architecture Principles of Operation >= SA22-7832-06)
802 /* KM-AES parameter block - end */
811 * KMO-AES parameter block - begin
812 * (see z/Architecture Principles of Operation >= SA22-7832-08)
815 unsigned char cv[16];
818 /* KMO-AES parameter block - end */
829 * KMF-AES parameter block - begin
830 * (see z/Architecture Principles of Operation >= SA22-7832-08)
833 unsigned char cv[16];
836 /* KMF-AES parameter block - end */
847 * KMA-GCM-AES parameter block - begin
848 * (see z/Architecture Principles of Operation >= SA22-7832-11)
851 unsigned char reserved[12];
857 unsigned long long g[2];
861 unsigned long long taadl;
862 unsigned long long tpcl;
864 unsigned long long g[2];
869 /* KMA-GCM-AES parameter block - end */
881 unsigned char ares[16];
882 unsigned char mres[16];
883 unsigned char kres[16];
889 uint64_t tls_enc_records; /* Number of TLS records encrypted */
896 * Padding is chosen so that ccm.kmac_param.k overlaps with key.k and
897 * ccm.fc with key.k.rounds. Remember that on s390x, an AES_KEY's
898 * rounds field is used to store the function code and that the key
899 * schedule is not stored (if aes hardware support is detected).
902 unsigned char pad[16];
908 * KMAC-AES parameter block - begin
909 * (see z/Architecture Principles of Operation >= SA22-7832-08)
913 unsigned long long g[2];
918 /* KMAC-AES paramater block - end */
921 unsigned long long g[2];
925 unsigned long long g[2];
929 unsigned long long blocks;
938 unsigned char pad[140];
944 # define s390x_aes_init_key aes_init_key
945 static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
946 const unsigned char *iv, int enc);
948 # define S390X_AES_CBC_CTX EVP_AES_KEY
950 # define s390x_aes_cbc_init_key aes_init_key
952 # define s390x_aes_cbc_cipher aes_cbc_cipher
953 static int s390x_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
954 const unsigned char *in, size_t len);
956 static int s390x_aes_ecb_init_key(EVP_CIPHER_CTX *ctx,
957 const unsigned char *key,
958 const unsigned char *iv, int enc)
960 S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
961 const int keylen = EVP_CIPHER_CTX_key_length(ctx);
963 cctx->fc = S390X_AES_FC(keylen);
965 cctx->fc |= S390X_DECRYPT;
967 memcpy(cctx->km.param.k, key, keylen);
971 static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
972 const unsigned char *in, size_t len)
974 S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
976 s390x_km(in, len, out, cctx->fc, &cctx->km.param);
980 static int s390x_aes_ofb_init_key(EVP_CIPHER_CTX *ctx,
981 const unsigned char *key,
982 const unsigned char *ivec, int enc)
984 S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
985 const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
986 const int keylen = EVP_CIPHER_CTX_key_length(ctx);
987 const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
989 memcpy(cctx->kmo.param.cv, iv, ivlen);
990 memcpy(cctx->kmo.param.k, key, keylen);
991 cctx->fc = S390X_AES_FC(keylen);
996 static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
997 const unsigned char *in, size_t len)
999 S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
1004 *out = *in ^ cctx->kmo.param.cv[n];
1013 len &= ~(size_t)0xf;
1015 s390x_kmo(in, len, out, cctx->fc, &cctx->kmo.param);
1022 s390x_km(cctx->kmo.param.cv, 16, cctx->kmo.param.cv, cctx->fc,
1026 out[n] = in[n] ^ cctx->kmo.param.cv[n];
1035 static int s390x_aes_cfb_init_key(EVP_CIPHER_CTX *ctx,
1036 const unsigned char *key,
1037 const unsigned char *ivec, int enc)
1039 S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1040 const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
1041 const int keylen = EVP_CIPHER_CTX_key_length(ctx);
1042 const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
1044 cctx->fc = S390X_AES_FC(keylen);
1045 cctx->fc |= 16 << 24; /* 16 bytes cipher feedback */
1047 cctx->fc |= S390X_DECRYPT;
1050 memcpy(cctx->kmf.param.cv, iv, ivlen);
1051 memcpy(cctx->kmf.param.k, key, keylen);
1055 static int s390x_aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1056 const unsigned char *in, size_t len)
1058 S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1059 const int keylen = EVP_CIPHER_CTX_key_length(ctx);
1060 const int enc = EVP_CIPHER_CTX_encrypting(ctx);
1067 *out = cctx->kmf.param.cv[n] ^ tmp;
1068 cctx->kmf.param.cv[n] = enc ? *out : tmp;
1077 len &= ~(size_t)0xf;
1079 s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param);
1086 s390x_km(cctx->kmf.param.cv, 16, cctx->kmf.param.cv,
1087 S390X_AES_FC(keylen), cctx->kmf.param.k);
1091 out[n] = cctx->kmf.param.cv[n] ^ tmp;
1092 cctx->kmf.param.cv[n] = enc ? out[n] : tmp;
1101 static int s390x_aes_cfb8_init_key(EVP_CIPHER_CTX *ctx,
1102 const unsigned char *key,
1103 const unsigned char *ivec, int enc)
1105 S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1106 const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
1107 const int keylen = EVP_CIPHER_CTX_key_length(ctx);
1108 const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
1110 cctx->fc = S390X_AES_FC(keylen);
1111 cctx->fc |= 1 << 24; /* 1 byte cipher feedback */
1113 cctx->fc |= S390X_DECRYPT;
1115 memcpy(cctx->kmf.param.cv, iv, ivlen);
1116 memcpy(cctx->kmf.param.k, key, keylen);
1120 static int s390x_aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1121 const unsigned char *in, size_t len)
1123 S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
1125 s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param);
1129 # define s390x_aes_cfb1_init_key aes_init_key
1131 # define s390x_aes_cfb1_cipher aes_cfb1_cipher
1132 static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1133 const unsigned char *in, size_t len);
1135 # define S390X_AES_CTR_CTX EVP_AES_KEY
1137 # define s390x_aes_ctr_init_key aes_init_key
1139 # define s390x_aes_ctr_cipher aes_ctr_cipher
1140 static int s390x_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1141 const unsigned char *in, size_t len);
1143 /* iv + padding length for iv lengths != 12 */
1144 # define S390X_gcm_ivpadlen(i) ((((i) + 15) >> 4 << 4) + 16)
1147 * Process additional authenticated data. Returns 0 on success. Code is
1150 static int s390x_aes_gcm_aad(S390X_AES_GCM_CTX *ctx, const unsigned char *aad,
1153 unsigned long long alen;
1156 if (ctx->kma.param.tpcl)
1159 alen = ctx->kma.param.taadl + len;
1160 if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
1162 ctx->kma.param.taadl = alen;
1167 ctx->ares[n] = *aad;
1172 /* ctx->ares contains a complete block if offset has wrapped around */
1174 s390x_kma(ctx->ares, 16, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
1175 ctx->fc |= S390X_KMA_HS;
1182 len &= ~(size_t)0xf;
1184 s390x_kma(aad, len, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
1186 ctx->fc |= S390X_KMA_HS;
1194 ctx->ares[rem] = aad[rem];
1201 * En/de-crypt plain/cipher-text and authenticate ciphertext. Returns 0 for
1202 * success. Code is big-endian.
1204 static int s390x_aes_gcm(S390X_AES_GCM_CTX *ctx, const unsigned char *in,
1205 unsigned char *out, size_t len)
1207 const unsigned char *inptr;
1208 unsigned long long mlen;
1211 unsigned char b[16];
1216 mlen = ctx->kma.param.tpcl + len;
1217 if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1219 ctx->kma.param.tpcl = mlen;
1225 while (n && inlen) {
1226 ctx->mres[n] = *inptr;
1231 /* ctx->mres contains a complete block if offset has wrapped around */
1233 s390x_kma(ctx->ares, ctx->areslen, ctx->mres, 16, buf.b,
1234 ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
1235 ctx->fc |= S390X_KMA_HS;
1238 /* previous call already encrypted/decrypted its remainder,
1239 * see comment below */
1254 len &= ~(size_t)0xf;
1256 s390x_kma(ctx->ares, ctx->areslen, in, len, out,
1257 ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
1260 ctx->fc |= S390X_KMA_HS;
1265 * If there is a remainder, it has to be saved such that it can be
1266 * processed by kma later. However, we also have to do the for-now
1267 * unauthenticated encryption/decryption part here and now...
1270 if (!ctx->mreslen) {
1271 buf.w[0] = ctx->kma.param.j0.w[0];
1272 buf.w[1] = ctx->kma.param.j0.w[1];
1273 buf.w[2] = ctx->kma.param.j0.w[2];
1274 buf.w[3] = ctx->kma.param.cv.w + 1;
1275 s390x_km(buf.b, 16, ctx->kres, ctx->fc & 0x1f, &ctx->kma.param.k);
1279 for (i = 0; i < rem; i++) {
1280 ctx->mres[n + i] = in[i];
1281 out[i] = in[i] ^ ctx->kres[n + i];
1284 ctx->mreslen += rem;
1290 * Initialize context structure. Code is big-endian.
1292 static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx,
1293 const unsigned char *iv)
1295 ctx->kma.param.t.g[0] = 0;
1296 ctx->kma.param.t.g[1] = 0;
1297 ctx->kma.param.tpcl = 0;
1298 ctx->kma.param.taadl = 0;
1303 if (ctx->ivlen == 12) {
1304 memcpy(&ctx->kma.param.j0, iv, ctx->ivlen);
1305 ctx->kma.param.j0.w[3] = 1;
1306 ctx->kma.param.cv.w = 1;
1308 /* ctx->iv has the right size and is already padded. */
1309 memcpy(ctx->iv, iv, ctx->ivlen);
1310 s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL,
1311 ctx->fc, &ctx->kma.param);
1312 ctx->fc |= S390X_KMA_HS;
1314 ctx->kma.param.j0.g[0] = ctx->kma.param.t.g[0];
1315 ctx->kma.param.j0.g[1] = ctx->kma.param.t.g[1];
1316 ctx->kma.param.cv.w = ctx->kma.param.j0.w[3];
1317 ctx->kma.param.t.g[0] = 0;
1318 ctx->kma.param.t.g[1] = 0;
1323 * Performs various operations on the context structure depending on control
1324 * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
1325 * Code is big-endian.
1327 static int s390x_aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
1329 S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
1330 S390X_AES_GCM_CTX *gctx_out;
1331 EVP_CIPHER_CTX *out;
1332 unsigned char *buf, *iv;
1333 int ivlen, enc, len;
1337 ivlen = EVP_CIPHER_CTX_iv_length(c);
1338 iv = EVP_CIPHER_CTX_iv_noconst(c);
1341 gctx->ivlen = ivlen;
1345 gctx->tls_aad_len = -1;
1348 case EVP_CTRL_AEAD_SET_IVLEN:
1353 iv = EVP_CIPHER_CTX_iv_noconst(c);
1354 len = S390X_gcm_ivpadlen(arg);
1356 /* Allocate memory for iv if needed. */
1357 if (gctx->ivlen == 12 || len > S390X_gcm_ivpadlen(gctx->ivlen)) {
1359 OPENSSL_free(gctx->iv);
1361 if ((gctx->iv = OPENSSL_malloc(len)) == NULL) {
1362 EVPerr(EVP_F_S390X_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
1367 memset(gctx->iv + arg, 0, len - arg - 8);
1368 *((unsigned long long *)(gctx->iv + len - 8)) = arg << 3;
1373 case EVP_CTRL_AEAD_SET_TAG:
1374 buf = EVP_CIPHER_CTX_buf_noconst(c);
1375 enc = EVP_CIPHER_CTX_encrypting(c);
1376 if (arg <= 0 || arg > 16 || enc)
1379 memcpy(buf, ptr, arg);
1383 case EVP_CTRL_AEAD_GET_TAG:
1384 enc = EVP_CIPHER_CTX_encrypting(c);
1385 if (arg <= 0 || arg > 16 || !enc || gctx->taglen < 0)
1388 memcpy(ptr, gctx->kma.param.t.b, arg);
1391 case EVP_CTRL_GCM_SET_IV_FIXED:
1392 /* Special case: -1 length restores whole iv */
1394 memcpy(gctx->iv, ptr, gctx->ivlen);
1399 * Fixed field must be at least 4 bytes and invocation field at least
1402 if ((arg < 4) || (gctx->ivlen - arg) < 8)
1406 memcpy(gctx->iv, ptr, arg);
1408 enc = EVP_CIPHER_CTX_encrypting(c);
1409 if (enc && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
1415 case EVP_CTRL_GCM_IV_GEN:
1416 if (gctx->iv_gen == 0 || gctx->key_set == 0)
1419 s390x_aes_gcm_setiv(gctx, gctx->iv);
1421 if (arg <= 0 || arg > gctx->ivlen)
1424 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
1426 * Invocation field will be at least 8 bytes in size and so no need
1427 * to check wrap around or increment more than last 8 bytes.
1429 ctr64_inc(gctx->iv + gctx->ivlen - 8);
1433 case EVP_CTRL_GCM_SET_IV_INV:
1434 enc = EVP_CIPHER_CTX_encrypting(c);
1435 if (gctx->iv_gen == 0 || gctx->key_set == 0 || enc)
1438 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
1439 s390x_aes_gcm_setiv(gctx, gctx->iv);
1443 case EVP_CTRL_AEAD_TLS1_AAD:
1444 /* Save the aad for later use. */
1445 if (arg != EVP_AEAD_TLS1_AAD_LEN)
1448 buf = EVP_CIPHER_CTX_buf_noconst(c);
1449 memcpy(buf, ptr, arg);
1450 gctx->tls_aad_len = arg;
1451 gctx->tls_enc_records = 0;
1453 len = buf[arg - 2] << 8 | buf[arg - 1];
1454 /* Correct length for explicit iv. */
1455 if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
1457 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1459 /* If decrypting correct for tag too. */
1460 enc = EVP_CIPHER_CTX_encrypting(c);
1462 if (len < EVP_GCM_TLS_TAG_LEN)
1464 len -= EVP_GCM_TLS_TAG_LEN;
1466 buf[arg - 2] = len >> 8;
1467 buf[arg - 1] = len & 0xff;
1468 /* Extra padding: tag appended to record. */
1469 return EVP_GCM_TLS_TAG_LEN;
1473 gctx_out = EVP_C_DATA(S390X_AES_GCM_CTX, out);
1474 iv = EVP_CIPHER_CTX_iv_noconst(c);
1476 if (gctx->iv == iv) {
1477 gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out);
1479 len = S390X_gcm_ivpadlen(gctx->ivlen);
1481 if ((gctx_out->iv = OPENSSL_malloc(len)) == NULL) {
1482 EVPerr(EVP_F_S390X_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
1486 memcpy(gctx_out->iv, gctx->iv, len);
1496 * Set key and/or iv. Returns 1 on success. Otherwise 0 is returned.
1498 static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx,
1499 const unsigned char *key,
1500 const unsigned char *iv, int enc)
1502 S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
1505 if (iv == NULL && key == NULL)
1509 keylen = EVP_CIPHER_CTX_key_length(ctx);
1510 memcpy(&gctx->kma.param.k, key, keylen);
1512 gctx->fc = S390X_AES_FC(keylen);
1514 gctx->fc |= S390X_DECRYPT;
1516 if (iv == NULL && gctx->iv_set)
1520 s390x_aes_gcm_setiv(gctx, iv);
1526 s390x_aes_gcm_setiv(gctx, iv);
1528 memcpy(gctx->iv, iv, gctx->ivlen);
1537 * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
1538 * if successful. Otherwise -1 is returned. Code is big-endian.
1540 static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1541 const unsigned char *in, size_t len)
1543 S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
1544 const unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
1545 const int enc = EVP_CIPHER_CTX_encrypting(ctx);
1548 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
1552 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
1553 * Requirements from SP 800-38D". The requirements is for one party to the
1554 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
1557 if (ctx->encrypt && ++gctx->tls_enc_records == 0) {
1558 EVPerr(EVP_F_S390X_AES_GCM_TLS_CIPHER, EVP_R_TOO_MANY_RECORDS);
1562 if (EVP_CIPHER_CTX_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN
1563 : EVP_CTRL_GCM_SET_IV_INV,
1564 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
1567 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1568 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1569 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1571 gctx->kma.param.taadl = gctx->tls_aad_len << 3;
1572 gctx->kma.param.tpcl = len << 3;
1573 s390x_kma(buf, gctx->tls_aad_len, in, len, out,
1574 gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
1577 memcpy(out + len, gctx->kma.param.t.b, EVP_GCM_TLS_TAG_LEN);
1578 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
1580 if (CRYPTO_memcmp(gctx->kma.param.t.b, in + len,
1581 EVP_GCM_TLS_TAG_LEN)) {
1582 OPENSSL_cleanse(out, len);
1589 gctx->tls_aad_len = -1;
1594 * Called from EVP layer to initialize context, process additional
1595 * authenticated data, en/de-crypt plain/cipher-text and authenticate
1596 * ciphertext or process a TLS packet, depending on context. Returns bytes
1597 * written on success. Otherwise -1 is returned. Code is big-endian.
1599 static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1600 const unsigned char *in, size_t len)
1602 S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
1603 unsigned char *buf, tmp[16];
1609 if (gctx->tls_aad_len >= 0)
1610 return s390x_aes_gcm_tls_cipher(ctx, out, in, len);
1617 if (s390x_aes_gcm_aad(gctx, in, len))
1620 if (s390x_aes_gcm(gctx, in, out, len))
1625 gctx->kma.param.taadl <<= 3;
1626 gctx->kma.param.tpcl <<= 3;
1627 s390x_kma(gctx->ares, gctx->areslen, gctx->mres, gctx->mreslen, tmp,
1628 gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
1629 /* recall that we already did en-/decrypt gctx->mres
1630 * and returned it to caller... */
1631 OPENSSL_cleanse(tmp, gctx->mreslen);
1634 enc = EVP_CIPHER_CTX_encrypting(ctx);
1638 if (gctx->taglen < 0)
1641 buf = EVP_CIPHER_CTX_buf_noconst(ctx);
1642 if (CRYPTO_memcmp(buf, gctx->kma.param.t.b, gctx->taglen))
1649 static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX *c)
1651 S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
1652 const unsigned char *iv;
1657 iv = EVP_CIPHER_CTX_iv(c);
1659 OPENSSL_free(gctx->iv);
1661 OPENSSL_cleanse(gctx, sizeof(*gctx));
1665 # define S390X_AES_XTS_CTX EVP_AES_XTS_CTX
1667 # define s390x_aes_xts_init_key aes_xts_init_key
1668 static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx,
1669 const unsigned char *key,
1670 const unsigned char *iv, int enc);
1671 # define s390x_aes_xts_cipher aes_xts_cipher
1672 static int s390x_aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1673 const unsigned char *in, size_t len);
1674 # define s390x_aes_xts_ctrl aes_xts_ctrl
1675 static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
1676 # define s390x_aes_xts_cleanup aes_xts_cleanup
1679 * Set nonce and length fields. Code is big-endian.
1681 static inline void s390x_aes_ccm_setiv(S390X_AES_CCM_CTX *ctx,
1682 const unsigned char *nonce,
1685 ctx->aes.ccm.nonce.b[0] &= ~S390X_CCM_AAD_FLAG;
1686 ctx->aes.ccm.nonce.g[1] = mlen;
1687 memcpy(ctx->aes.ccm.nonce.b + 1, nonce, 15 - ctx->aes.ccm.l);
1691 * Process additional authenticated data. Code is big-endian.
1693 static void s390x_aes_ccm_aad(S390X_AES_CCM_CTX *ctx, const unsigned char *aad,
1702 ctx->aes.ccm.nonce.b[0] |= S390X_CCM_AAD_FLAG;
1704 /* Suppress 'type-punned pointer dereference' warning. */
1705 ptr = ctx->aes.ccm.buf.b;
1707 if (alen < ((1 << 16) - (1 << 8))) {
1708 *(uint16_t *)ptr = alen;
1710 } else if (sizeof(alen) == 8
1711 && alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) {
1712 *(uint16_t *)ptr = 0xffff;
1713 *(uint64_t *)(ptr + 2) = alen;
1716 *(uint16_t *)ptr = 0xfffe;
1717 *(uint32_t *)(ptr + 2) = alen;
1721 while (i < 16 && alen) {
1722 ctx->aes.ccm.buf.b[i] = *aad;
1728 ctx->aes.ccm.buf.b[i] = 0;
1732 ctx->aes.ccm.kmac_param.icv.g[0] = 0;
1733 ctx->aes.ccm.kmac_param.icv.g[1] = 0;
1734 s390x_kmac(ctx->aes.ccm.nonce.b, 32, ctx->aes.ccm.fc,
1735 &ctx->aes.ccm.kmac_param);
1736 ctx->aes.ccm.blocks += 2;
1739 alen &= ~(size_t)0xf;
1741 s390x_kmac(aad, alen, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
1742 ctx->aes.ccm.blocks += alen >> 4;
1746 for (i = 0; i < rem; i++)
1747 ctx->aes.ccm.kmac_param.icv.b[i] ^= aad[i];
1749 s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
1750 ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
1751 ctx->aes.ccm.kmac_param.k);
1752 ctx->aes.ccm.blocks++;
1757 * En/de-crypt plain/cipher-text. Compute tag from plaintext. Returns 0 for
1760 static int s390x_aes_ccm(S390X_AES_CCM_CTX *ctx, const unsigned char *in,
1761 unsigned char *out, size_t len, int enc)
1764 unsigned int i, l, num;
1765 unsigned char flags;
1767 flags = ctx->aes.ccm.nonce.b[0];
1768 if (!(flags & S390X_CCM_AAD_FLAG)) {
1769 s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.kmac_param.icv.b,
1770 ctx->aes.ccm.fc, ctx->aes.ccm.kmac_param.k);
1771 ctx->aes.ccm.blocks++;
1774 ctx->aes.ccm.nonce.b[0] = l;
1777 * Reconstruct length from encoded length field
1778 * and initialize it with counter value.
1781 for (i = 15 - l; i < 15; i++) {
1782 n |= ctx->aes.ccm.nonce.b[i];
1783 ctx->aes.ccm.nonce.b[i] = 0;
1786 n |= ctx->aes.ccm.nonce.b[15];
1787 ctx->aes.ccm.nonce.b[15] = 1;
1790 return -1; /* length mismatch */
1793 /* Two operations per block plus one for tag encryption */
1794 ctx->aes.ccm.blocks += (((len + 15) >> 4) << 1) + 1;
1795 if (ctx->aes.ccm.blocks > (1ULL << 61))
1796 return -2; /* too much data */
1801 len &= ~(size_t)0xf;
1804 /* mac-then-encrypt */
1806 s390x_kmac(in, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
1808 for (i = 0; i < rem; i++)
1809 ctx->aes.ccm.kmac_param.icv.b[i] ^= in[len + i];
1811 s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
1812 ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
1813 ctx->aes.ccm.kmac_param.k);
1816 CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
1817 ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
1818 &num, (ctr128_f)AES_ctr32_encrypt);
1820 /* decrypt-then-mac */
1821 CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
1822 ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
1823 &num, (ctr128_f)AES_ctr32_encrypt);
1826 s390x_kmac(out, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
1828 for (i = 0; i < rem; i++)
1829 ctx->aes.ccm.kmac_param.icv.b[i] ^= out[len + i];
1831 s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
1832 ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
1833 ctx->aes.ccm.kmac_param.k);
1837 for (i = 15 - l; i < 16; i++)
1838 ctx->aes.ccm.nonce.b[i] = 0;
1840 s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.buf.b, ctx->aes.ccm.fc,
1841 ctx->aes.ccm.kmac_param.k);
1842 ctx->aes.ccm.kmac_param.icv.g[0] ^= ctx->aes.ccm.buf.g[0];
1843 ctx->aes.ccm.kmac_param.icv.g[1] ^= ctx->aes.ccm.buf.g[1];
1845 ctx->aes.ccm.nonce.b[0] = flags; /* restore flags field */
1850 * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
1851 * if successful. Otherwise -1 is returned.
1853 static int s390x_aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1854 const unsigned char *in, size_t len)
1856 S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
1857 unsigned char *ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
1858 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
1859 const int enc = EVP_CIPHER_CTX_encrypting(ctx);
1862 || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->aes.ccm.m))
1866 /* Set explicit iv (sequence number). */
1867 memcpy(out, buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
1870 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
1872 * Get explicit iv (sequence number). We already have fixed iv
1873 * (server/client_write_iv) here.
1875 memcpy(ivec + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
1876 s390x_aes_ccm_setiv(cctx, ivec, len);
1878 /* Process aad (sequence number|type|version|length) */
1879 s390x_aes_ccm_aad(cctx, buf, cctx->aes.ccm.tls_aad_len);
1881 in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1882 out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1885 if (s390x_aes_ccm(cctx, in, out, len, enc))
1888 memcpy(out + len, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
1889 return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
1891 if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
1892 if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, in + len,
1897 OPENSSL_cleanse(out, len);
1903 * Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is
1906 static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx,
1907 const unsigned char *key,
1908 const unsigned char *iv, int enc)
1910 S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
1911 unsigned char *ivec;
1914 if (iv == NULL && key == NULL)
1918 keylen = EVP_CIPHER_CTX_key_length(ctx);
1919 cctx->aes.ccm.fc = S390X_AES_FC(keylen);
1920 memcpy(cctx->aes.ccm.kmac_param.k, key, keylen);
1922 /* Store encoded m and l. */
1923 cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7)
1924 | (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3;
1925 memset(cctx->aes.ccm.nonce.b + 1, 0,
1926 sizeof(cctx->aes.ccm.nonce.b));
1927 cctx->aes.ccm.blocks = 0;
1929 cctx->aes.ccm.key_set = 1;
1933 ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
1934 memcpy(ivec, iv, 15 - cctx->aes.ccm.l);
1936 cctx->aes.ccm.iv_set = 1;
1943 * Called from EVP layer to initialize context, process additional
1944 * authenticated data, en/de-crypt plain/cipher-text and authenticate
1945 * plaintext or process a TLS packet, depending on context. Returns bytes
1946 * written on success. Otherwise -1 is returned.
1948 static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1949 const unsigned char *in, size_t len)
1951 S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
1952 const int enc = EVP_CIPHER_CTX_encrypting(ctx);
1954 unsigned char *buf, *ivec;
1956 if (!cctx->aes.ccm.key_set)
1959 if (cctx->aes.ccm.tls_aad_len >= 0)
1960 return s390x_aes_ccm_tls_cipher(ctx, out, in, len);
1963 * Final(): Does not return any data. Recall that ccm is mac-then-encrypt
1964 * so integrity must be checked already at Update() i.e., before
1965 * potentially corrupted data is output.
1967 if (in == NULL && out != NULL)
1970 if (!cctx->aes.ccm.iv_set)
1974 /* Update(): Pass message length. */
1976 ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
1977 s390x_aes_ccm_setiv(cctx, ivec, len);
1979 cctx->aes.ccm.len_set = 1;
1983 /* Update(): Process aad. */
1984 if (!cctx->aes.ccm.len_set && len)
1987 s390x_aes_ccm_aad(cctx, in, len);
1991 /* The tag must be set before actually decrypting data */
1992 if (!enc && !cctx->aes.ccm.tag_set)
1995 /* Update(): Process message. */
1997 if (!cctx->aes.ccm.len_set) {
1999 * In case message length was not previously set explicitly via
2000 * Update(), set it now.
2002 ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
2003 s390x_aes_ccm_setiv(cctx, ivec, len);
2005 cctx->aes.ccm.len_set = 1;
2009 if (s390x_aes_ccm(cctx, in, out, len, enc))
2012 cctx->aes.ccm.tag_set = 1;
2017 if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
2018 buf = EVP_CIPHER_CTX_buf_noconst(ctx);
2019 if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, buf,
2025 OPENSSL_cleanse(out, len);
2027 cctx->aes.ccm.iv_set = 0;
2028 cctx->aes.ccm.tag_set = 0;
2029 cctx->aes.ccm.len_set = 0;
2035 * Performs various operations on the context structure depending on control
2036 * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
2037 * Code is big-endian.
2039 static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
2041 S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, c);
2042 unsigned char *buf, *iv;
2047 cctx->aes.ccm.key_set = 0;
2048 cctx->aes.ccm.iv_set = 0;
2049 cctx->aes.ccm.l = 8;
2050 cctx->aes.ccm.m = 12;
2051 cctx->aes.ccm.tag_set = 0;
2052 cctx->aes.ccm.len_set = 0;
2053 cctx->aes.ccm.tls_aad_len = -1;
2056 case EVP_CTRL_AEAD_TLS1_AAD:
2057 if (arg != EVP_AEAD_TLS1_AAD_LEN)
2060 /* Save the aad for later use. */
2061 buf = EVP_CIPHER_CTX_buf_noconst(c);
2062 memcpy(buf, ptr, arg);
2063 cctx->aes.ccm.tls_aad_len = arg;
2065 len = buf[arg - 2] << 8 | buf[arg - 1];
2066 if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
2069 /* Correct length for explicit iv. */
2070 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
2072 enc = EVP_CIPHER_CTX_encrypting(c);
2074 if (len < cctx->aes.ccm.m)
2077 /* Correct length for tag. */
2078 len -= cctx->aes.ccm.m;
2081 buf[arg - 2] = len >> 8;
2082 buf[arg - 1] = len & 0xff;
2084 /* Extra padding: tag appended to record. */
2085 return cctx->aes.ccm.m;
2087 case EVP_CTRL_CCM_SET_IV_FIXED:
2088 if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
2091 /* Copy to first part of the iv. */
2092 iv = EVP_CIPHER_CTX_iv_noconst(c);
2093 memcpy(iv, ptr, arg);
2096 case EVP_CTRL_AEAD_SET_IVLEN:
2100 case EVP_CTRL_CCM_SET_L:
2101 if (arg < 2 || arg > 8)
2104 cctx->aes.ccm.l = arg;
2107 case EVP_CTRL_AEAD_SET_TAG:
2108 if ((arg & 1) || arg < 4 || arg > 16)
2111 enc = EVP_CIPHER_CTX_encrypting(c);
2116 cctx->aes.ccm.tag_set = 1;
2117 buf = EVP_CIPHER_CTX_buf_noconst(c);
2118 memcpy(buf, ptr, arg);
2121 cctx->aes.ccm.m = arg;
2124 case EVP_CTRL_AEAD_GET_TAG:
2125 enc = EVP_CIPHER_CTX_encrypting(c);
2126 if (!enc || !cctx->aes.ccm.tag_set)
2129 if(arg < cctx->aes.ccm.m)
2132 memcpy(ptr, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
2133 cctx->aes.ccm.tag_set = 0;
2134 cctx->aes.ccm.iv_set = 0;
2135 cctx->aes.ccm.len_set = 0;
2146 # define s390x_aes_ccm_cleanup aes_ccm_cleanup
2148 # ifndef OPENSSL_NO_OCB
2149 # define S390X_AES_OCB_CTX EVP_AES_OCB_CTX
2151 # define s390x_aes_ocb_init_key aes_ocb_init_key
2152 static int s390x_aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2153 const unsigned char *iv, int enc);
2154 # define s390x_aes_ocb_cipher aes_ocb_cipher
2155 static int s390x_aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2156 const unsigned char *in, size_t len);
2157 # define s390x_aes_ocb_cleanup aes_ocb_cleanup
2158 static int s390x_aes_ocb_cleanup(EVP_CIPHER_CTX *);
2159 # define s390x_aes_ocb_ctrl aes_ocb_ctrl
2160 static int s390x_aes_ocb_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
2163 # ifndef OPENSSL_NO_SIV
2164 # define S390X_AES_SIV_CTX EVP_AES_SIV_CTX
2166 # define s390x_aes_siv_init_key aes_siv_init_key
2167 # define s390x_aes_siv_cipher aes_siv_cipher
2168 # define s390x_aes_siv_cleanup aes_siv_cleanup
2169 # define s390x_aes_siv_ctrl aes_siv_ctrl
2172 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode, \
2174 static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
2175 nid##_##keylen##_##nmode,blocksize, \
2178 flags | EVP_CIPH_##MODE##_MODE, \
2179 s390x_aes_##mode##_init_key, \
2180 s390x_aes_##mode##_cipher, \
2182 sizeof(S390X_AES_##MODE##_CTX), \
2188 static const EVP_CIPHER aes_##keylen##_##mode = { \
2189 nid##_##keylen##_##nmode, \
2193 flags | EVP_CIPH_##MODE##_MODE, \
2195 aes_##mode##_cipher, \
2197 sizeof(EVP_AES_KEY), \
2203 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2205 return S390X_aes_##keylen##_##mode##_CAPABLE ? \
2206 &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
2209 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\
2210 static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
2211 nid##_##keylen##_##mode, \
2213 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE ? 2 : 1) * keylen / 8, \
2215 flags | EVP_CIPH_##MODE##_MODE, \
2216 s390x_aes_##mode##_init_key, \
2217 s390x_aes_##mode##_cipher, \
2218 s390x_aes_##mode##_cleanup, \
2219 sizeof(S390X_AES_##MODE##_CTX), \
2222 s390x_aes_##mode##_ctrl, \
2225 static const EVP_CIPHER aes_##keylen##_##mode = { \
2226 nid##_##keylen##_##mode,blocksize, \
2227 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE ? 2 : 1) * keylen / 8, \
2229 flags | EVP_CIPH_##MODE##_MODE, \
2230 aes_##mode##_init_key, \
2231 aes_##mode##_cipher, \
2232 aes_##mode##_cleanup, \
2233 sizeof(EVP_AES_##MODE##_CTX), \
2236 aes_##mode##_ctrl, \
2239 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2241 return S390X_aes_##keylen##_##mode##_CAPABLE ? \
2242 &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
2247 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
2248 static const EVP_CIPHER aes_##keylen##_##mode = { \
2249 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
2250 flags|EVP_CIPH_##MODE##_MODE, \
2252 aes_##mode##_cipher, \
2254 sizeof(EVP_AES_KEY), \
2255 NULL,NULL,NULL,NULL }; \
2256 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2257 { return &aes_##keylen##_##mode; }
2259 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
2260 static const EVP_CIPHER aes_##keylen##_##mode = { \
2261 nid##_##keylen##_##mode,blocksize, \
2262 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
2264 flags|EVP_CIPH_##MODE##_MODE, \
2265 aes_##mode##_init_key, \
2266 aes_##mode##_cipher, \
2267 aes_##mode##_cleanup, \
2268 sizeof(EVP_AES_##MODE##_CTX), \
2269 NULL,NULL,aes_##mode##_ctrl,NULL }; \
2270 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2271 { return &aes_##keylen##_##mode; }
2275 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
2276 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2277 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2278 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2279 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2280 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
2281 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
2282 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
2284 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2285 const unsigned char *iv, int enc)
2288 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2290 mode = EVP_CIPHER_CTX_mode(ctx);
2291 if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
2293 #ifdef HWAES_CAPABLE
2294 if (HWAES_CAPABLE) {
2295 ret = HWAES_set_decrypt_key(key,
2296 EVP_CIPHER_CTX_key_length(ctx) * 8,
2298 dat->block = (block128_f) HWAES_decrypt;
2299 dat->stream.cbc = NULL;
2300 # ifdef HWAES_cbc_encrypt
2301 if (mode == EVP_CIPH_CBC_MODE)
2302 dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt;
2306 #ifdef BSAES_CAPABLE
2307 if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) {
2308 ret = AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
2310 dat->block = (block128_f) AES_decrypt;
2311 dat->stream.cbc = (cbc128_f) bsaes_cbc_encrypt;
2314 #ifdef VPAES_CAPABLE
2315 if (VPAES_CAPABLE) {
2316 ret = vpaes_set_decrypt_key(key,
2317 EVP_CIPHER_CTX_key_length(ctx) * 8,
2319 dat->block = (block128_f) vpaes_decrypt;
2320 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2321 (cbc128_f) vpaes_cbc_encrypt : NULL;
2325 ret = AES_set_decrypt_key(key,
2326 EVP_CIPHER_CTX_key_length(ctx) * 8,
2328 dat->block = (block128_f) AES_decrypt;
2329 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2330 (cbc128_f) AES_cbc_encrypt : NULL;
2333 #ifdef HWAES_CAPABLE
2334 if (HWAES_CAPABLE) {
2335 ret = HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
2337 dat->block = (block128_f) HWAES_encrypt;
2338 dat->stream.cbc = NULL;
2339 # ifdef HWAES_cbc_encrypt
2340 if (mode == EVP_CIPH_CBC_MODE)
2341 dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt;
2344 # ifdef HWAES_ctr32_encrypt_blocks
2345 if (mode == EVP_CIPH_CTR_MODE)
2346 dat->stream.ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks;
2349 (void)0; /* terminate potentially open 'else' */
2352 #ifdef BSAES_CAPABLE
2353 if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) {
2354 ret = AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
2356 dat->block = (block128_f) AES_encrypt;
2357 dat->stream.ctr = (ctr128_f) bsaes_ctr32_encrypt_blocks;
2360 #ifdef VPAES_CAPABLE
2361 if (VPAES_CAPABLE) {
2362 ret = vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
2364 dat->block = (block128_f) vpaes_encrypt;
2365 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2366 (cbc128_f) vpaes_cbc_encrypt : NULL;
2370 ret = AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
2372 dat->block = (block128_f) AES_encrypt;
2373 dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
2374 (cbc128_f) AES_cbc_encrypt : NULL;
2376 if (mode == EVP_CIPH_CTR_MODE)
2377 dat->stream.ctr = (ctr128_f) AES_ctr32_encrypt;
2382 EVPerr(EVP_F_AES_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
2389 static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2390 const unsigned char *in, size_t len)
2392 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2394 if (dat->stream.cbc)
2395 (*dat->stream.cbc) (in, out, len, &dat->ks,
2396 EVP_CIPHER_CTX_iv_noconst(ctx),
2397 EVP_CIPHER_CTX_encrypting(ctx));
2398 else if (EVP_CIPHER_CTX_encrypting(ctx))
2399 CRYPTO_cbc128_encrypt(in, out, len, &dat->ks,
2400 EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
2402 CRYPTO_cbc128_decrypt(in, out, len, &dat->ks,
2403 EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
2408 static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2409 const unsigned char *in, size_t len)
2411 size_t bl = EVP_CIPHER_CTX_block_size(ctx);
2413 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2418 for (i = 0, len -= bl; i <= len; i += bl)
2419 (*dat->block) (in + i, out + i, &dat->ks);
2424 static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2425 const unsigned char *in, size_t len)
2427 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2429 int num = EVP_CIPHER_CTX_num(ctx);
2430 CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
2431 EVP_CIPHER_CTX_iv_noconst(ctx), &num, dat->block);
2432 EVP_CIPHER_CTX_set_num(ctx, num);
2436 static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2437 const unsigned char *in, size_t len)
2439 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2441 int num = EVP_CIPHER_CTX_num(ctx);
2442 CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
2443 EVP_CIPHER_CTX_iv_noconst(ctx), &num,
2444 EVP_CIPHER_CTX_encrypting(ctx), dat->block);
2445 EVP_CIPHER_CTX_set_num(ctx, num);
2449 static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2450 const unsigned char *in, size_t len)
2452 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2454 int num = EVP_CIPHER_CTX_num(ctx);
2455 CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks,
2456 EVP_CIPHER_CTX_iv_noconst(ctx), &num,
2457 EVP_CIPHER_CTX_encrypting(ctx), dat->block);
2458 EVP_CIPHER_CTX_set_num(ctx, num);
2462 static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2463 const unsigned char *in, size_t len)
2465 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2467 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) {
2468 int num = EVP_CIPHER_CTX_num(ctx);
2469 CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks,
2470 EVP_CIPHER_CTX_iv_noconst(ctx), &num,
2471 EVP_CIPHER_CTX_encrypting(ctx), dat->block);
2472 EVP_CIPHER_CTX_set_num(ctx, num);
2476 while (len >= MAXBITCHUNK) {
2477 int num = EVP_CIPHER_CTX_num(ctx);
2478 CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
2479 EVP_CIPHER_CTX_iv_noconst(ctx), &num,
2480 EVP_CIPHER_CTX_encrypting(ctx), dat->block);
2481 EVP_CIPHER_CTX_set_num(ctx, num);
2487 int num = EVP_CIPHER_CTX_num(ctx);
2488 CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
2489 EVP_CIPHER_CTX_iv_noconst(ctx), &num,
2490 EVP_CIPHER_CTX_encrypting(ctx), dat->block);
2491 EVP_CIPHER_CTX_set_num(ctx, num);
2497 static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2498 const unsigned char *in, size_t len)
2500 unsigned int num = EVP_CIPHER_CTX_num(ctx);
2501 EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
2503 if (dat->stream.ctr)
2504 CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
2505 EVP_CIPHER_CTX_iv_noconst(ctx),
2506 EVP_CIPHER_CTX_buf_noconst(ctx),
2507 &num, dat->stream.ctr);
2509 CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
2510 EVP_CIPHER_CTX_iv_noconst(ctx),
2511 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
2513 EVP_CIPHER_CTX_set_num(ctx, num);
2517 BLOCK_CIPHER_generic_pack(NID_aes, 128, 0)
2518 BLOCK_CIPHER_generic_pack(NID_aes, 192, 0)
2519 BLOCK_CIPHER_generic_pack(NID_aes, 256, 0)
2521 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
2523 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c);
2526 OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
2527 if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
2528 OPENSSL_free(gctx->iv);
2532 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
2534 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c);
2539 gctx->ivlen = c->cipher->iv_len;
2543 gctx->tls_aad_len = -1;
2546 case EVP_CTRL_AEAD_SET_IVLEN:
2549 /* Allocate memory for IV if needed */
2550 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
2551 if (gctx->iv != c->iv)
2552 OPENSSL_free(gctx->iv);
2553 if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) {
2554 EVPerr(EVP_F_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
2561 case EVP_CTRL_AEAD_SET_TAG:
2562 if (arg <= 0 || arg > 16 || c->encrypt)
2564 memcpy(c->buf, ptr, arg);
2568 case EVP_CTRL_AEAD_GET_TAG:
2569 if (arg <= 0 || arg > 16 || !c->encrypt
2570 || gctx->taglen < 0)
2572 memcpy(ptr, c->buf, arg);
2575 case EVP_CTRL_GET_IV:
2576 if (gctx->iv_gen != 1 && gctx->iv_gen_rand != 1)
2578 if (gctx->ivlen != arg)
2580 memcpy(ptr, gctx->iv, arg);
2583 case EVP_CTRL_GCM_SET_IV_FIXED:
2584 /* Special case: -1 length restores whole IV */
2586 memcpy(gctx->iv, ptr, gctx->ivlen);
2591 * Fixed field must be at least 4 bytes and invocation field at least
2594 if ((arg < 4) || (gctx->ivlen - arg) < 8)
2597 memcpy(gctx->iv, ptr, arg);
2598 if (c->encrypt && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
2603 case EVP_CTRL_GCM_IV_GEN:
2604 if (gctx->iv_gen == 0 || gctx->key_set == 0)
2606 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
2607 if (arg <= 0 || arg > gctx->ivlen)
2609 memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
2611 * Invocation field will be at least 8 bytes in size and so no need
2612 * to check wrap around or increment more than last 8 bytes.
2614 ctr64_inc(gctx->iv + gctx->ivlen - 8);
2618 case EVP_CTRL_GCM_SET_IV_INV:
2619 if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
2621 memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
2622 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
2626 case EVP_CTRL_AEAD_TLS1_AAD:
2627 /* Save the AAD for later use */
2628 if (arg != EVP_AEAD_TLS1_AAD_LEN)
2630 memcpy(c->buf, ptr, arg);
2631 gctx->tls_aad_len = arg;
2632 gctx->tls_enc_records = 0;
2634 unsigned int len = c->buf[arg - 2] << 8 | c->buf[arg - 1];
2635 /* Correct length for explicit IV */
2636 if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
2638 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
2639 /* If decrypting correct for tag too */
2641 if (len < EVP_GCM_TLS_TAG_LEN)
2643 len -= EVP_GCM_TLS_TAG_LEN;
2645 c->buf[arg - 2] = len >> 8;
2646 c->buf[arg - 1] = len & 0xff;
2648 /* Extra padding: tag appended to record */
2649 return EVP_GCM_TLS_TAG_LEN;
2653 EVP_CIPHER_CTX *out = ptr;
2654 EVP_AES_GCM_CTX *gctx_out = EVP_C_DATA(EVP_AES_GCM_CTX,out);
2655 if (gctx->gcm.key) {
2656 if (gctx->gcm.key != &gctx->ks)
2658 gctx_out->gcm.key = &gctx_out->ks;
2660 if (gctx->iv == c->iv)
2661 gctx_out->iv = out->iv;
2663 if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) {
2664 EVPerr(EVP_F_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
2667 memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
2678 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2679 const unsigned char *iv, int enc)
2681 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
2686 #ifdef HWAES_CAPABLE
2687 if (HWAES_CAPABLE) {
2688 HWAES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
2689 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2690 (block128_f) HWAES_encrypt);
2691 # ifdef HWAES_ctr32_encrypt_blocks
2692 gctx->ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks;
2699 #ifdef BSAES_CAPABLE
2700 if (BSAES_CAPABLE) {
2701 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
2702 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2703 (block128_f) AES_encrypt);
2704 gctx->ctr = (ctr128_f) bsaes_ctr32_encrypt_blocks;
2708 #ifdef VPAES_CAPABLE
2709 if (VPAES_CAPABLE) {
2710 vpaes_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
2711 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2712 (block128_f) vpaes_encrypt);
2717 (void)0; /* terminate potentially open 'else' */
2719 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
2720 CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
2721 (block128_f) AES_encrypt);
2723 gctx->ctr = (ctr128_f) AES_ctr32_encrypt;
2730 * If we have an iv can set it directly, otherwise use saved IV.
2732 if (iv == NULL && gctx->iv_set)
2735 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
2740 /* If key set use IV, otherwise copy */
2742 CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
2744 memcpy(gctx->iv, iv, gctx->ivlen);
2752 * Handle TLS GCM packet format. This consists of the last portion of the IV
2753 * followed by the payload and finally the tag. On encrypt generate IV,
2754 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
2758 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2759 const unsigned char *in, size_t len)
2761 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
2763 /* Encrypt/decrypt must be performed in place */
2765 || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
2769 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
2770 * Requirements from SP 800-38D". The requirements is for one party to the
2771 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
2774 if (ctx->encrypt && ++gctx->tls_enc_records == 0) {
2775 EVPerr(EVP_F_AES_GCM_TLS_CIPHER, EVP_R_TOO_MANY_RECORDS);
2780 * Set IV from start of buffer or generate IV and write to start of
2783 if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? EVP_CTRL_GCM_IV_GEN
2784 : EVP_CTRL_GCM_SET_IV_INV,
2785 EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
2788 if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
2790 /* Fix buffer and length to point to payload */
2791 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
2792 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
2793 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
2795 /* Encrypt payload */
2798 #if defined(AES_GCM_ASM)
2799 if (len >= 32 && AES_GCM_ASM(gctx)) {
2800 if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))
2803 bulk = AES_gcm_encrypt(in, out, len,
2805 gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2806 gctx->gcm.len.u[1] += bulk;
2809 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
2812 len - bulk, gctx->ctr))
2816 #if defined(AES_GCM_ASM2)
2817 if (len >= 32 && AES_GCM_ASM2(gctx)) {
2818 if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))
2821 bulk = AES_gcm_encrypt(in, out, len,
2823 gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2824 gctx->gcm.len.u[1] += bulk;
2827 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
2828 in + bulk, out + bulk, len - bulk))
2832 /* Finally write tag */
2833 CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
2834 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
2839 #if defined(AES_GCM_ASM)
2840 if (len >= 16 && AES_GCM_ASM(gctx)) {
2841 if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))
2844 bulk = AES_gcm_decrypt(in, out, len,
2846 gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2847 gctx->gcm.len.u[1] += bulk;
2850 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
2853 len - bulk, gctx->ctr))
2857 #if defined(AES_GCM_ASM2)
2858 if (len >= 16 && AES_GCM_ASM2(gctx)) {
2859 if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))
2862 bulk = AES_gcm_decrypt(in, out, len,
2864 gctx->gcm.Yi.c, gctx->gcm.Xi.u);
2865 gctx->gcm.len.u[1] += bulk;
2868 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
2869 in + bulk, out + bulk, len - bulk))
2873 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN);
2874 /* If tag mismatch wipe buffer */
2875 if (CRYPTO_memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) {
2876 OPENSSL_cleanse(out, len);
2884 gctx->tls_aad_len = -1;
2890 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
2892 * See also 8.2.2 RBG-based construction.
2893 * Random construction consists of a free field (which can be NULL) and a
2894 * random field which will use a DRBG that can return at least 96 bits of
2895 * entropy strength. (The DRBG must be seeded by the FIPS module).
2897 static int aes_gcm_iv_generate(EVP_AES_GCM_CTX *gctx, int offset)
2899 int sz = gctx->ivlen - offset;
2901 /* Must be at least 96 bits */
2902 if (sz <= 0 || gctx->ivlen < 12)
2905 /* Use DRBG to generate random iv */
2906 if (RAND_bytes(gctx->iv + offset, sz) <= 0)
2910 #endif /* FIPS_MODE */
2912 static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
2913 const unsigned char *in, size_t len)
2915 EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
2917 /* If not set up, return error */
2921 if (gctx->tls_aad_len >= 0)
2922 return aes_gcm_tls_cipher(ctx, out, in, len);
2926 * FIPS requires generation of AES-GCM IV's inside the FIPS module.
2927 * The IV can still be set externally (the security policy will state that
2928 * this is not FIPS compliant). There are some applications
2929 * where setting the IV externally is the only option available.
2931 if (!gctx->iv_set) {
2932 if (!ctx->encrypt || !aes_gcm_iv_generate(gctx, 0))
2934 CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
2936 gctx->iv_gen_rand = 1;
2941 #endif /* FIPS_MODE */
2945 if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
2947 } else if (ctx->encrypt) {
2950 #if defined(AES_GCM_ASM)
2951 if (len >= 32 && AES_GCM_ASM(gctx)) {
2952 size_t res = (16 - gctx->gcm.mres) % 16;
2954 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res))
2957 bulk = AES_gcm_encrypt(in + res,
2958 out + res, len - res,
2959 gctx->gcm.key, gctx->gcm.Yi.c,
2961 gctx->gcm.len.u[1] += bulk;
2965 if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
2968 len - bulk, gctx->ctr))
2972 #if defined(AES_GCM_ASM2)
2973 if (len >= 32 && AES_GCM_ASM2(gctx)) {
2974 size_t res = (16 - gctx->gcm.mres) % 16;
2976 if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res))
2979 bulk = AES_gcm_encrypt(in + res,
2980 out + res, len - res,
2981 gctx->gcm.key, gctx->gcm.Yi.c,
2983 gctx->gcm.len.u[1] += bulk;
2987 if (CRYPTO_gcm128_encrypt(&gctx->gcm,
2988 in + bulk, out + bulk, len - bulk))
2994 #if defined(AES_GCM_ASM)
2995 if (len >= 16 && AES_GCM_ASM(gctx)) {
2996 size_t res = (16 - gctx->gcm.mres) % 16;
2998 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res))
3001 bulk = AES_gcm_decrypt(in + res,
3002 out + res, len - res,
3004 gctx->gcm.Yi.c, gctx->gcm.Xi.u);
3005 gctx->gcm.len.u[1] += bulk;
3009 if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
3012 len - bulk, gctx->ctr))
3016 #if defined(AES_GCM_ASM2)
3017 if (len >= 16 && AES_GCM_ASM2(gctx)) {
3018 size_t res = (16 - gctx->gcm.mres) % 16;
3020 if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res))
3023 bulk = AES_gcm_decrypt(in + res,
3024 out + res, len - res,
3026 gctx->gcm.Yi.c, gctx->gcm.Xi.u);
3027 gctx->gcm.len.u[1] += bulk;
3031 if (CRYPTO_gcm128_decrypt(&gctx->gcm,
3032 in + bulk, out + bulk, len - bulk))
3038 if (!ctx->encrypt) {
3039 if (gctx->taglen < 0)
3041 if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen) != 0)
3046 CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
3048 /* Don't reuse the IV */
3055 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
3056 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
3057 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
3058 | EVP_CIPH_CUSTOM_COPY)
3060 BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, gcm, GCM,
3061 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3062 BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, gcm, GCM,
3063 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3064 BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, gcm, GCM,
3065 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3067 static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
3069 EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX, c);
3071 if (type == EVP_CTRL_COPY) {
3072 EVP_CIPHER_CTX *out = ptr;
3073 EVP_AES_XTS_CTX *xctx_out = EVP_C_DATA(EVP_AES_XTS_CTX,out);
3075 if (xctx->xts.key1) {
3076 if (xctx->xts.key1 != &xctx->ks1)
3078 xctx_out->xts.key1 = &xctx_out->ks1;
3080 if (xctx->xts.key2) {
3081 if (xctx->xts.key2 != &xctx->ks2)
3083 xctx_out->xts.key2 = &xctx_out->ks2;
3086 } else if (type != EVP_CTRL_INIT)
3088 /* key1 and key2 are used as an indicator both key and IV are set */
3089 xctx->xts.key1 = NULL;
3090 xctx->xts.key2 = NULL;
3094 static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3095 const unsigned char *iv, int enc)
3097 EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
3104 /* The key is two half length keys in reality */
3105 const int bytes = EVP_CIPHER_CTX_key_length(ctx) / 2;
3106 const int bits = bytes * 8;
3109 * Verify that the two keys are different.
3111 * This addresses the vulnerability described in Rogaway's
3112 * September 2004 paper:
3114 * "Efficient Instantiations of Tweakable Blockciphers and
3115 * Refinements to Modes OCB and PMAC".
3116 * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
3118 * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
3120 * "The check for Key_1 != Key_2 shall be done at any place
3121 * BEFORE using the keys in the XTS-AES algorithm to process
3124 if ((!allow_insecure_decrypt || enc)
3125 && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
3126 EVPerr(EVP_F_AES_XTS_INIT_KEY, EVP_R_XTS_DUPLICATED_KEYS);
3131 xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
3133 xctx->stream = NULL;
3135 /* key_len is two AES keys */
3136 #ifdef HWAES_CAPABLE
3137 if (HWAES_CAPABLE) {
3139 HWAES_set_encrypt_key(key, bits, &xctx->ks1.ks);
3140 xctx->xts.block1 = (block128_f) HWAES_encrypt;
3141 # ifdef HWAES_xts_encrypt
3142 xctx->stream = HWAES_xts_encrypt;
3145 HWAES_set_decrypt_key(key, bits, &xctx->ks1.ks);
3146 xctx->xts.block1 = (block128_f) HWAES_decrypt;
3147 # ifdef HWAES_xts_decrypt
3148 xctx->stream = HWAES_xts_decrypt;
3152 HWAES_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
3153 xctx->xts.block2 = (block128_f) HWAES_encrypt;
3155 xctx->xts.key1 = &xctx->ks1;
3159 #ifdef BSAES_CAPABLE
3161 xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
3164 #ifdef VPAES_CAPABLE
3165 if (VPAES_CAPABLE) {
3167 vpaes_set_encrypt_key(key, bits, &xctx->ks1.ks);
3168 xctx->xts.block1 = (block128_f) vpaes_encrypt;
3170 vpaes_set_decrypt_key(key, bits, &xctx->ks1.ks);
3171 xctx->xts.block1 = (block128_f) vpaes_decrypt;
3174 vpaes_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
3175 xctx->xts.block2 = (block128_f) vpaes_encrypt;
3177 xctx->xts.key1 = &xctx->ks1;
3181 (void)0; /* terminate potentially open 'else' */
3184 AES_set_encrypt_key(key, bits, &xctx->ks1.ks);
3185 xctx->xts.block1 = (block128_f) AES_encrypt;
3187 AES_set_decrypt_key(key, bits, &xctx->ks1.ks);
3188 xctx->xts.block1 = (block128_f) AES_decrypt;
3191 AES_set_encrypt_key(key + bytes, bits, &xctx->ks2.ks);
3192 xctx->xts.block2 = (block128_f) AES_encrypt;
3194 xctx->xts.key1 = &xctx->ks1;
3199 xctx->xts.key2 = &xctx->ks2;
3200 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
3206 static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3207 const unsigned char *in, size_t len)
3209 EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
3211 if (xctx->xts.key1 == NULL
3212 || xctx->xts.key2 == NULL
3215 || len < AES_BLOCK_SIZE)
3219 * Impose a limit of 2^20 blocks per data unit as specifed by
3220 * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
3221 * indicated that this was a SHOULD NOT rather than a MUST NOT.
3222 * NIST SP 800-38E mandates the same limit.
3224 if (len > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
3225 EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_XTS_DATA_UNIT_IS_TOO_LARGE);
3230 (*xctx->stream) (in, out, len,
3231 xctx->xts.key1, xctx->xts.key2,
3232 EVP_CIPHER_CTX_iv_noconst(ctx));
3233 else if (CRYPTO_xts128_encrypt(&xctx->xts, EVP_CIPHER_CTX_iv_noconst(ctx),
3235 EVP_CIPHER_CTX_encrypting(ctx)))
3240 #define aes_xts_cleanup NULL
3242 #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
3243 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
3244 | EVP_CIPH_CUSTOM_COPY)
3246 BLOCK_CIPHER_custom(NID_aes, 128, 1, 16, xts, XTS, XTS_FLAGS)
3247 BLOCK_CIPHER_custom(NID_aes, 256, 1, 16, xts, XTS, XTS_FLAGS)
3249 static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
3251 EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,c);
3260 cctx->tls_aad_len = -1;
3263 case EVP_CTRL_AEAD_TLS1_AAD:
3264 /* Save the AAD for later use */
3265 if (arg != EVP_AEAD_TLS1_AAD_LEN)
3267 memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
3268 cctx->tls_aad_len = arg;
3271 EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
3272 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
3273 /* Correct length for explicit IV */
3274 if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
3276 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
3277 /* If decrypting correct for tag too */
3278 if (!EVP_CIPHER_CTX_encrypting(c)) {
3283 EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
3284 EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
3286 /* Extra padding: tag appended to record */
3289 case EVP_CTRL_CCM_SET_IV_FIXED:
3290 /* Sanity check length */
3291 if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
3293 /* Just copy to first part of IV */
3294 memcpy(EVP_CIPHER_CTX_iv_noconst(c), ptr, arg);
3297 case EVP_CTRL_AEAD_SET_IVLEN:
3300 case EVP_CTRL_CCM_SET_L:
3301 if (arg < 2 || arg > 8)
3306 case EVP_CTRL_AEAD_SET_TAG:
3307 if ((arg & 1) || arg < 4 || arg > 16)
3309 if (EVP_CIPHER_CTX_encrypting(c) && ptr)
3313 memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
3318 case EVP_CTRL_AEAD_GET_TAG:
3319 if (!EVP_CIPHER_CTX_encrypting(c) || !cctx->tag_set)
3321 if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
3330 EVP_CIPHER_CTX *out = ptr;
3331 EVP_AES_CCM_CTX *cctx_out = EVP_C_DATA(EVP_AES_CCM_CTX,out);
3332 if (cctx->ccm.key) {
3333 if (cctx->ccm.key != &cctx->ks)
3335 cctx_out->ccm.key = &cctx_out->ks;
3346 static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3347 const unsigned char *iv, int enc)
3349 EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
3354 #ifdef HWAES_CAPABLE
3355 if (HWAES_CAPABLE) {
3356 HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3359 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
3360 &cctx->ks, (block128_f) HWAES_encrypt);
3366 #ifdef VPAES_CAPABLE
3367 if (VPAES_CAPABLE) {
3368 vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3370 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
3371 &cctx->ks, (block128_f) vpaes_encrypt);
3377 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3379 CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
3380 &cctx->ks, (block128_f) AES_encrypt);
3385 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
3391 static int aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3392 const unsigned char *in, size_t len)
3394 EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
3395 CCM128_CONTEXT *ccm = &cctx->ccm;
3396 /* Encrypt/decrypt must be performed in place */
3397 if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
3399 /* If encrypting set explicit IV from sequence number (start of AAD) */
3400 if (EVP_CIPHER_CTX_encrypting(ctx))
3401 memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
3402 EVP_CCM_TLS_EXPLICIT_IV_LEN);
3403 /* Get rest of IV from explicit IV */
3404 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx) + EVP_CCM_TLS_FIXED_IV_LEN, in,
3405 EVP_CCM_TLS_EXPLICIT_IV_LEN);
3406 /* Correct length value */
3407 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
3408 if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), 15 - cctx->L,
3412 CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), cctx->tls_aad_len);
3413 /* Fix buffer to point to payload */
3414 in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
3415 out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
3416 if (EVP_CIPHER_CTX_encrypting(ctx)) {
3417 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
3419 CRYPTO_ccm128_encrypt(ccm, in, out, len))
3421 if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
3423 return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
3425 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
3427 !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
3428 unsigned char tag[16];
3429 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
3430 if (!CRYPTO_memcmp(tag, in + len, cctx->M))
3434 OPENSSL_cleanse(out, len);
3439 static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3440 const unsigned char *in, size_t len)
3442 EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
3443 CCM128_CONTEXT *ccm = &cctx->ccm;
3444 /* If not set up, return error */
3448 if (cctx->tls_aad_len >= 0)
3449 return aes_ccm_tls_cipher(ctx, out, in, len);
3451 /* EVP_*Final() doesn't return any data */
3452 if (in == NULL && out != NULL)
3460 if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
3466 /* If have AAD need message length */
3467 if (!cctx->len_set && len)
3469 CRYPTO_ccm128_aad(ccm, in, len);
3473 /* The tag must be set before actually decrypting data */
3474 if (!EVP_CIPHER_CTX_encrypting(ctx) && !cctx->tag_set)
3477 /* If not set length yet do it */
3478 if (!cctx->len_set) {
3479 if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
3484 if (EVP_CIPHER_CTX_encrypting(ctx)) {
3485 if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
3487 CRYPTO_ccm128_encrypt(ccm, in, out, len))
3493 if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
3495 !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
3496 unsigned char tag[16];
3497 if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
3498 if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
3504 OPENSSL_cleanse(out, len);
3512 #define aes_ccm_cleanup NULL
3514 BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, ccm, CCM,
3515 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3516 BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, ccm, CCM,
3517 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3518 BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, ccm, CCM,
3519 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3526 /* Indicates if IV has been set */
3530 static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3531 const unsigned char *iv, int enc)
3533 EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx);
3537 if (EVP_CIPHER_CTX_encrypting(ctx))
3538 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3541 AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3547 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, EVP_CIPHER_CTX_iv_length(ctx));
3548 wctx->iv = EVP_CIPHER_CTX_iv_noconst(ctx);
3553 static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3554 const unsigned char *in, size_t inlen)
3556 EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx);
3558 /* AES wrap with padding has IV length of 4, without padding 8 */
3559 int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4;
3560 /* No final operation so always return zero length */
3563 /* Input length must always be non-zero */
3566 /* If decrypting need at least 16 bytes and multiple of 8 */
3567 if (!EVP_CIPHER_CTX_encrypting(ctx) && (inlen < 16 || inlen & 0x7))
3569 /* If not padding input must be multiple of 8 */
3570 if (!pad && inlen & 0x7)
3572 if (is_partially_overlapping(out, in, inlen)) {
3573 EVPerr(EVP_F_AES_WRAP_CIPHER, EVP_R_PARTIALLY_OVERLAPPING);
3577 if (EVP_CIPHER_CTX_encrypting(ctx)) {
3578 /* If padding round up to multiple of 8 */
3580 inlen = (inlen + 7) / 8 * 8;
3585 * If not padding output will be exactly 8 bytes smaller than
3586 * input. If padding it will be at least 8 bytes smaller but we
3587 * don't know how much.
3593 if (EVP_CIPHER_CTX_encrypting(ctx))
3594 rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
3596 (block128_f) AES_encrypt);
3598 rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
3600 (block128_f) AES_decrypt);
3602 if (EVP_CIPHER_CTX_encrypting(ctx))
3603 rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
3604 out, in, inlen, (block128_f) AES_encrypt);
3606 rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
3607 out, in, inlen, (block128_f) AES_decrypt);
3609 return rv ? (int)rv : -1;
3612 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
3613 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
3614 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
3616 static const EVP_CIPHER aes_128_wrap = {
3618 8, 16, 8, WRAP_FLAGS,
3619 aes_wrap_init_key, aes_wrap_cipher,
3621 sizeof(EVP_AES_WRAP_CTX),
3622 NULL, NULL, NULL, NULL
3625 const EVP_CIPHER *EVP_aes_128_wrap(void)
3627 return &aes_128_wrap;
3630 static const EVP_CIPHER aes_192_wrap = {
3632 8, 24, 8, WRAP_FLAGS,
3633 aes_wrap_init_key, aes_wrap_cipher,
3635 sizeof(EVP_AES_WRAP_CTX),
3636 NULL, NULL, NULL, NULL
3639 const EVP_CIPHER *EVP_aes_192_wrap(void)
3641 return &aes_192_wrap;
3644 static const EVP_CIPHER aes_256_wrap = {
3646 8, 32, 8, WRAP_FLAGS,
3647 aes_wrap_init_key, aes_wrap_cipher,
3649 sizeof(EVP_AES_WRAP_CTX),
3650 NULL, NULL, NULL, NULL
3653 const EVP_CIPHER *EVP_aes_256_wrap(void)
3655 return &aes_256_wrap;
3658 static const EVP_CIPHER aes_128_wrap_pad = {
3659 NID_id_aes128_wrap_pad,
3660 8, 16, 4, WRAP_FLAGS,
3661 aes_wrap_init_key, aes_wrap_cipher,
3663 sizeof(EVP_AES_WRAP_CTX),
3664 NULL, NULL, NULL, NULL
3667 const EVP_CIPHER *EVP_aes_128_wrap_pad(void)
3669 return &aes_128_wrap_pad;
3672 static const EVP_CIPHER aes_192_wrap_pad = {
3673 NID_id_aes192_wrap_pad,
3674 8, 24, 4, WRAP_FLAGS,
3675 aes_wrap_init_key, aes_wrap_cipher,
3677 sizeof(EVP_AES_WRAP_CTX),
3678 NULL, NULL, NULL, NULL
3681 const EVP_CIPHER *EVP_aes_192_wrap_pad(void)
3683 return &aes_192_wrap_pad;
3686 static const EVP_CIPHER aes_256_wrap_pad = {
3687 NID_id_aes256_wrap_pad,
3688 8, 32, 4, WRAP_FLAGS,
3689 aes_wrap_init_key, aes_wrap_cipher,
3691 sizeof(EVP_AES_WRAP_CTX),
3692 NULL, NULL, NULL, NULL
3695 const EVP_CIPHER *EVP_aes_256_wrap_pad(void)
3697 return &aes_256_wrap_pad;
3700 #ifndef OPENSSL_NO_OCB
3701 static int aes_ocb_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
3703 EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c);
3704 EVP_CIPHER_CTX *newc;
3705 EVP_AES_OCB_CTX *new_octx;
3711 octx->ivlen = EVP_CIPHER_CTX_iv_length(c);
3712 octx->iv = EVP_CIPHER_CTX_iv_noconst(c);
3714 octx->data_buf_len = 0;
3715 octx->aad_buf_len = 0;
3718 case EVP_CTRL_AEAD_SET_IVLEN:
3719 /* IV len must be 1 to 15 */
3720 if (arg <= 0 || arg > 15)
3726 case EVP_CTRL_AEAD_SET_TAG:
3728 /* Tag len must be 0 to 16 */
3729 if (arg < 0 || arg > 16)
3735 if (arg != octx->taglen || EVP_CIPHER_CTX_encrypting(c))
3737 memcpy(octx->tag, ptr, arg);
3740 case EVP_CTRL_AEAD_GET_TAG:
3741 if (arg != octx->taglen || !EVP_CIPHER_CTX_encrypting(c))
3744 memcpy(ptr, octx->tag, arg);
3748 newc = (EVP_CIPHER_CTX *)ptr;
3749 new_octx = EVP_C_DATA(EVP_AES_OCB_CTX,newc);
3750 return CRYPTO_ocb128_copy_ctx(&new_octx->ocb, &octx->ocb,
3751 &new_octx->ksenc.ks,
3752 &new_octx->ksdec.ks);
3760 static int aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
3761 const unsigned char *iv, int enc)
3763 EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
3769 * We set both the encrypt and decrypt key here because decrypt
3770 * needs both. We could possibly optimise to remove setting the
3771 * decrypt for an encryption operation.
3773 # ifdef HWAES_CAPABLE
3774 if (HWAES_CAPABLE) {
3775 HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3777 HWAES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3779 if (!CRYPTO_ocb128_init(&octx->ocb,
3780 &octx->ksenc.ks, &octx->ksdec.ks,
3781 (block128_f) HWAES_encrypt,
3782 (block128_f) HWAES_decrypt,
3783 enc ? HWAES_ocb_encrypt
3784 : HWAES_ocb_decrypt))
3789 # ifdef VPAES_CAPABLE
3790 if (VPAES_CAPABLE) {
3791 vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3793 vpaes_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3795 if (!CRYPTO_ocb128_init(&octx->ocb,
3796 &octx->ksenc.ks, &octx->ksdec.ks,
3797 (block128_f) vpaes_encrypt,
3798 (block128_f) vpaes_decrypt,
3804 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3806 AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
3808 if (!CRYPTO_ocb128_init(&octx->ocb,
3809 &octx->ksenc.ks, &octx->ksdec.ks,
3810 (block128_f) AES_encrypt,
3811 (block128_f) AES_decrypt,
3818 * If we have an iv we can set it directly, otherwise use saved IV.
3820 if (iv == NULL && octx->iv_set)
3823 if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
3830 /* If key set use IV, otherwise copy */
3832 CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
3834 memcpy(octx->iv, iv, octx->ivlen);
3840 static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
3841 const unsigned char *in, size_t len)
3845 int written_len = 0;
3846 size_t trailing_len;
3847 EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
3849 /* If IV or Key not set then return error */
3858 * Need to ensure we are only passing full blocks to low level OCB
3859 * routines. We do it here rather than in EVP_EncryptUpdate/
3860 * EVP_DecryptUpdate because we need to pass full blocks of AAD too
3861 * and those routines don't support that
3864 /* Are we dealing with AAD or normal data here? */
3866 buf = octx->aad_buf;
3867 buf_len = &(octx->aad_buf_len);
3869 buf = octx->data_buf;
3870 buf_len = &(octx->data_buf_len);
3872 if (is_partially_overlapping(out + *buf_len, in, len)) {
3873 EVPerr(EVP_F_AES_OCB_CIPHER, EVP_R_PARTIALLY_OVERLAPPING);
3879 * If we've got a partially filled buffer from a previous call then
3880 * use that data first
3883 unsigned int remaining;
3885 remaining = AES_BLOCK_SIZE - (*buf_len);
3886 if (remaining > len) {
3887 memcpy(buf + (*buf_len), in, len);
3891 memcpy(buf + (*buf_len), in, remaining);
3894 * If we get here we've filled the buffer, so process it
3899 if (!CRYPTO_ocb128_aad(&octx->ocb, buf, AES_BLOCK_SIZE))
3901 } else if (EVP_CIPHER_CTX_encrypting(ctx)) {
3902 if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out,
3906 if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out,
3910 written_len = AES_BLOCK_SIZE;
3913 out += AES_BLOCK_SIZE;
3916 /* Do we have a partial block to handle at the end? */
3917 trailing_len = len % AES_BLOCK_SIZE;
3920 * If we've got some full blocks to handle, then process these first
3922 if (len != trailing_len) {
3924 if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len))
3926 } else if (EVP_CIPHER_CTX_encrypting(ctx)) {
3927 if (!CRYPTO_ocb128_encrypt
3928 (&octx->ocb, in, out, len - trailing_len))
3931 if (!CRYPTO_ocb128_decrypt
3932 (&octx->ocb, in, out, len - trailing_len))
3935 written_len += len - trailing_len;
3936 in += len - trailing_len;
3939 /* Handle any trailing partial block */
3940 if (trailing_len > 0) {
3941 memcpy(buf, in, trailing_len);
3942 *buf_len = trailing_len;
3948 * First of all empty the buffer of any partial block that we might
3949 * have been provided - both for data and AAD
3951 if (octx->data_buf_len > 0) {
3952 if (EVP_CIPHER_CTX_encrypting(ctx)) {
3953 if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,
3954 octx->data_buf_len))
3957 if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,
3958 octx->data_buf_len))
3961 written_len = octx->data_buf_len;
3962 octx->data_buf_len = 0;
3964 if (octx->aad_buf_len > 0) {
3965 if (!CRYPTO_ocb128_aad
3966 (&octx->ocb, octx->aad_buf, octx->aad_buf_len))
3968 octx->aad_buf_len = 0;
3970 /* If decrypting then verify */
3971 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
3972 if (octx->taglen < 0)
3974 if (CRYPTO_ocb128_finish(&octx->ocb,
3975 octx->tag, octx->taglen) != 0)
3980 /* If encrypting then just get the tag */
3981 if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)
3983 /* Don't reuse the IV */
3989 static int aes_ocb_cleanup(EVP_CIPHER_CTX *c)
3991 EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c);
3992 CRYPTO_ocb128_cleanup(&octx->ocb);
3996 BLOCK_CIPHER_custom(NID_aes, 128, 16, 12, ocb, OCB,
3997 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
3998 BLOCK_CIPHER_custom(NID_aes, 192, 16, 12, ocb, OCB,
3999 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
4000 BLOCK_CIPHER_custom(NID_aes, 256, 16, 12, ocb, OCB,
4001 EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
4002 #endif /* OPENSSL_NO_OCB */
4005 #ifndef OPENSSL_NO_SIV
4007 typedef SIV128_CONTEXT EVP_AES_SIV_CTX;
4009 #define aesni_siv_init_key aes_siv_init_key
4010 static int aes_siv_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
4011 const unsigned char *iv, int enc)
4013 const EVP_CIPHER *ctr;
4014 const EVP_CIPHER *cbc;
4015 SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, ctx);
4016 int klen = EVP_CIPHER_CTX_key_length(ctx) / 2;
4023 cbc = EVP_aes_128_cbc();
4024 ctr = EVP_aes_128_ctr();
4027 cbc = EVP_aes_192_cbc();
4028 ctr = EVP_aes_192_ctr();
4031 cbc = EVP_aes_256_cbc();
4032 ctr = EVP_aes_256_ctr();
4038 /* klen is the length of the underlying cipher, not the input key,
4039 which should be twice as long */
4040 return CRYPTO_siv128_init(sctx, key, klen, cbc, ctr);
4043 #define aesni_siv_cipher aes_siv_cipher
4044 static int aes_siv_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
4045 const unsigned char *in, size_t len)
4047 SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, ctx);
4049 /* EncryptFinal or DecryptFinal */
4051 return CRYPTO_siv128_finish(sctx);
4053 /* Deal with associated data */
4055 return CRYPTO_siv128_aad(sctx, in, len);
4057 if (EVP_CIPHER_CTX_encrypting(ctx))
4058 return CRYPTO_siv128_encrypt(sctx, in, out, len);
4060 return CRYPTO_siv128_decrypt(sctx, in, out, len);
4063 #define aesni_siv_cleanup aes_siv_cleanup
4064 static int aes_siv_cleanup(EVP_CIPHER_CTX *c)
4066 SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, c);
4068 return CRYPTO_siv128_cleanup(sctx);
4072 #define aesni_siv_ctrl aes_siv_ctrl
4073 static int aes_siv_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
4075 SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, c);
4076 SIV128_CONTEXT *sctx_out;
4080 return CRYPTO_siv128_cleanup(sctx);
4082 case EVP_CTRL_SET_SPEED:
4083 return CRYPTO_siv128_speed(sctx, arg);
4085 case EVP_CTRL_AEAD_SET_TAG:
4086 if (!EVP_CIPHER_CTX_encrypting(c))
4087 return CRYPTO_siv128_set_tag(sctx, ptr, arg);
4090 case EVP_CTRL_AEAD_GET_TAG:
4091 if (!EVP_CIPHER_CTX_encrypting(c))
4093 return CRYPTO_siv128_get_tag(sctx, ptr, arg);
4096 sctx_out = EVP_C_DATA(SIV128_CONTEXT, (EVP_CIPHER_CTX*)ptr);
4097 return CRYPTO_siv128_copy_ctx(sctx_out, sctx);
4105 #define SIV_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1 \
4106 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
4107 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_COPY \
4108 | EVP_CIPH_CTRL_INIT)
4110 BLOCK_CIPHER_custom(NID_aes, 128, 1, 0, siv, SIV, SIV_FLAGS)
4111 BLOCK_CIPHER_custom(NID_aes, 192, 1, 0, siv, SIV, SIV_FLAGS)
4112 BLOCK_CIPHER_custom(NID_aes, 256, 1, 0, siv, SIV, SIV_FLAGS)