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 "crypto/evp.h"
20 #include "internal/cryptlib.h"
21 #include "crypto/modes.h"
22 #include "crypto/siv.h"
23 #include "crypto/ciphermode_platform.h"
24 #include "evp_local.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_iv_length(c
->cipher
);
1338 iv
= EVP_CIPHER_CTX_iv_noconst(c
);
1341 gctx
->ivlen
= ivlen
;
1345 gctx
->tls_aad_len
= -1;
1348 case EVP_CTRL_GET_IVLEN
:
1349 *(int *)ptr
= gctx
->ivlen
;
1352 case EVP_CTRL_AEAD_SET_IVLEN
:
1357 iv
= EVP_CIPHER_CTX_iv_noconst(c
);
1358 len
= S390X_gcm_ivpadlen(arg
);
1360 /* Allocate memory for iv if needed. */
1361 if (gctx
->ivlen
== 12 || len
> S390X_gcm_ivpadlen(gctx
->ivlen
)) {
1363 OPENSSL_free(gctx
->iv
);
1365 if ((gctx
->iv
= OPENSSL_malloc(len
)) == NULL
) {
1366 EVPerr(EVP_F_S390X_AES_GCM_CTRL
, ERR_R_MALLOC_FAILURE
);
1371 memset(gctx
->iv
+ arg
, 0, len
- arg
- 8);
1372 *((unsigned long long *)(gctx
->iv
+ len
- 8)) = arg
<< 3;
1377 case EVP_CTRL_AEAD_SET_TAG
:
1378 buf
= EVP_CIPHER_CTX_buf_noconst(c
);
1379 enc
= EVP_CIPHER_CTX_encrypting(c
);
1380 if (arg
<= 0 || arg
> 16 || enc
)
1383 memcpy(buf
, ptr
, arg
);
1387 case EVP_CTRL_AEAD_GET_TAG
:
1388 enc
= EVP_CIPHER_CTX_encrypting(c
);
1389 if (arg
<= 0 || arg
> 16 || !enc
|| gctx
->taglen
< 0)
1392 memcpy(ptr
, gctx
->kma
.param
.t
.b
, arg
);
1395 case EVP_CTRL_GCM_SET_IV_FIXED
:
1396 /* Special case: -1 length restores whole iv */
1398 memcpy(gctx
->iv
, ptr
, gctx
->ivlen
);
1403 * Fixed field must be at least 4 bytes and invocation field at least
1406 if ((arg
< 4) || (gctx
->ivlen
- arg
) < 8)
1410 memcpy(gctx
->iv
, ptr
, arg
);
1412 enc
= EVP_CIPHER_CTX_encrypting(c
);
1413 if (enc
&& RAND_bytes(gctx
->iv
+ arg
, gctx
->ivlen
- arg
) <= 0)
1419 case EVP_CTRL_GCM_IV_GEN
:
1420 if (gctx
->iv_gen
== 0 || gctx
->key_set
== 0)
1423 s390x_aes_gcm_setiv(gctx
, gctx
->iv
);
1425 if (arg
<= 0 || arg
> gctx
->ivlen
)
1428 memcpy(ptr
, gctx
->iv
+ gctx
->ivlen
- arg
, arg
);
1430 * Invocation field will be at least 8 bytes in size and so no need
1431 * to check wrap around or increment more than last 8 bytes.
1433 ctr64_inc(gctx
->iv
+ gctx
->ivlen
- 8);
1437 case EVP_CTRL_GCM_SET_IV_INV
:
1438 enc
= EVP_CIPHER_CTX_encrypting(c
);
1439 if (gctx
->iv_gen
== 0 || gctx
->key_set
== 0 || enc
)
1442 memcpy(gctx
->iv
+ gctx
->ivlen
- arg
, ptr
, arg
);
1443 s390x_aes_gcm_setiv(gctx
, gctx
->iv
);
1447 case EVP_CTRL_AEAD_TLS1_AAD
:
1448 /* Save the aad for later use. */
1449 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
1452 buf
= EVP_CIPHER_CTX_buf_noconst(c
);
1453 memcpy(buf
, ptr
, arg
);
1454 gctx
->tls_aad_len
= arg
;
1455 gctx
->tls_enc_records
= 0;
1457 len
= buf
[arg
- 2] << 8 | buf
[arg
- 1];
1458 /* Correct length for explicit iv. */
1459 if (len
< EVP_GCM_TLS_EXPLICIT_IV_LEN
)
1461 len
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1463 /* If decrypting correct for tag too. */
1464 enc
= EVP_CIPHER_CTX_encrypting(c
);
1466 if (len
< EVP_GCM_TLS_TAG_LEN
)
1468 len
-= EVP_GCM_TLS_TAG_LEN
;
1470 buf
[arg
- 2] = len
>> 8;
1471 buf
[arg
- 1] = len
& 0xff;
1472 /* Extra padding: tag appended to record. */
1473 return EVP_GCM_TLS_TAG_LEN
;
1477 gctx_out
= EVP_C_DATA(S390X_AES_GCM_CTX
, out
);
1478 iv
= EVP_CIPHER_CTX_iv_noconst(c
);
1480 if (gctx
->iv
== iv
) {
1481 gctx_out
->iv
= EVP_CIPHER_CTX_iv_noconst(out
);
1483 len
= S390X_gcm_ivpadlen(gctx
->ivlen
);
1485 if ((gctx_out
->iv
= OPENSSL_malloc(len
)) == NULL
) {
1486 EVPerr(EVP_F_S390X_AES_GCM_CTRL
, ERR_R_MALLOC_FAILURE
);
1490 memcpy(gctx_out
->iv
, gctx
->iv
, len
);
1500 * Set key and/or iv. Returns 1 on success. Otherwise 0 is returned.
1502 static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX
*ctx
,
1503 const unsigned char *key
,
1504 const unsigned char *iv
, int enc
)
1506 S390X_AES_GCM_CTX
*gctx
= EVP_C_DATA(S390X_AES_GCM_CTX
, ctx
);
1509 if (iv
== NULL
&& key
== NULL
)
1513 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
1514 memcpy(&gctx
->kma
.param
.k
, key
, keylen
);
1516 gctx
->fc
= S390X_AES_FC(keylen
);
1518 gctx
->fc
|= S390X_DECRYPT
;
1520 if (iv
== NULL
&& gctx
->iv_set
)
1524 s390x_aes_gcm_setiv(gctx
, iv
);
1530 s390x_aes_gcm_setiv(gctx
, iv
);
1532 memcpy(gctx
->iv
, iv
, gctx
->ivlen
);
1541 * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
1542 * if successful. Otherwise -1 is returned. Code is big-endian.
1544 static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1545 const unsigned char *in
, size_t len
)
1547 S390X_AES_GCM_CTX
*gctx
= EVP_C_DATA(S390X_AES_GCM_CTX
, ctx
);
1548 const unsigned char *buf
= EVP_CIPHER_CTX_buf_noconst(ctx
);
1549 const int enc
= EVP_CIPHER_CTX_encrypting(ctx
);
1552 if (out
!= in
|| len
< (EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
))
1556 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
1557 * Requirements from SP 800-38D". The requirements is for one party to the
1558 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
1561 if (ctx
->encrypt
&& ++gctx
->tls_enc_records
== 0) {
1562 EVPerr(EVP_F_S390X_AES_GCM_TLS_CIPHER
, EVP_R_TOO_MANY_RECORDS
);
1566 if (EVP_CIPHER_CTX_ctrl(ctx
, enc
? EVP_CTRL_GCM_IV_GEN
1567 : EVP_CTRL_GCM_SET_IV_INV
,
1568 EVP_GCM_TLS_EXPLICIT_IV_LEN
, out
) <= 0)
1571 in
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1572 out
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1573 len
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
;
1575 gctx
->kma
.param
.taadl
= gctx
->tls_aad_len
<< 3;
1576 gctx
->kma
.param
.tpcl
= len
<< 3;
1577 s390x_kma(buf
, gctx
->tls_aad_len
, in
, len
, out
,
1578 gctx
->fc
| S390X_KMA_LAAD
| S390X_KMA_LPC
, &gctx
->kma
.param
);
1581 memcpy(out
+ len
, gctx
->kma
.param
.t
.b
, EVP_GCM_TLS_TAG_LEN
);
1582 rv
= len
+ EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
;
1584 if (CRYPTO_memcmp(gctx
->kma
.param
.t
.b
, in
+ len
,
1585 EVP_GCM_TLS_TAG_LEN
)) {
1586 OPENSSL_cleanse(out
, len
);
1593 gctx
->tls_aad_len
= -1;
1598 * Called from EVP layer to initialize context, process additional
1599 * authenticated data, en/de-crypt plain/cipher-text and authenticate
1600 * ciphertext or process a TLS packet, depending on context. Returns bytes
1601 * written on success. Otherwise -1 is returned. Code is big-endian.
1603 static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1604 const unsigned char *in
, size_t len
)
1606 S390X_AES_GCM_CTX
*gctx
= EVP_C_DATA(S390X_AES_GCM_CTX
, ctx
);
1607 unsigned char *buf
, tmp
[16];
1613 if (gctx
->tls_aad_len
>= 0)
1614 return s390x_aes_gcm_tls_cipher(ctx
, out
, in
, len
);
1621 if (s390x_aes_gcm_aad(gctx
, in
, len
))
1624 if (s390x_aes_gcm(gctx
, in
, out
, len
))
1629 gctx
->kma
.param
.taadl
<<= 3;
1630 gctx
->kma
.param
.tpcl
<<= 3;
1631 s390x_kma(gctx
->ares
, gctx
->areslen
, gctx
->mres
, gctx
->mreslen
, tmp
,
1632 gctx
->fc
| S390X_KMA_LAAD
| S390X_KMA_LPC
, &gctx
->kma
.param
);
1633 /* recall that we already did en-/decrypt gctx->mres
1634 * and returned it to caller... */
1635 OPENSSL_cleanse(tmp
, gctx
->mreslen
);
1638 enc
= EVP_CIPHER_CTX_encrypting(ctx
);
1642 if (gctx
->taglen
< 0)
1645 buf
= EVP_CIPHER_CTX_buf_noconst(ctx
);
1646 if (CRYPTO_memcmp(buf
, gctx
->kma
.param
.t
.b
, gctx
->taglen
))
1653 static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX
*c
)
1655 S390X_AES_GCM_CTX
*gctx
= EVP_C_DATA(S390X_AES_GCM_CTX
, c
);
1656 const unsigned char *iv
;
1661 iv
= EVP_CIPHER_CTX_iv(c
);
1663 OPENSSL_free(gctx
->iv
);
1665 OPENSSL_cleanse(gctx
, sizeof(*gctx
));
1669 # define S390X_AES_XTS_CTX EVP_AES_XTS_CTX
1671 # define s390x_aes_xts_init_key aes_xts_init_key
1672 static int s390x_aes_xts_init_key(EVP_CIPHER_CTX
*ctx
,
1673 const unsigned char *key
,
1674 const unsigned char *iv
, int enc
);
1675 # define s390x_aes_xts_cipher aes_xts_cipher
1676 static int s390x_aes_xts_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1677 const unsigned char *in
, size_t len
);
1678 # define s390x_aes_xts_ctrl aes_xts_ctrl
1679 static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX
*, int type
, int arg
, void *ptr
);
1680 # define s390x_aes_xts_cleanup aes_xts_cleanup
1683 * Set nonce and length fields. Code is big-endian.
1685 static inline void s390x_aes_ccm_setiv(S390X_AES_CCM_CTX
*ctx
,
1686 const unsigned char *nonce
,
1689 ctx
->aes
.ccm
.nonce
.b
[0] &= ~S390X_CCM_AAD_FLAG
;
1690 ctx
->aes
.ccm
.nonce
.g
[1] = mlen
;
1691 memcpy(ctx
->aes
.ccm
.nonce
.b
+ 1, nonce
, 15 - ctx
->aes
.ccm
.l
);
1695 * Process additional authenticated data. Code is big-endian.
1697 static void s390x_aes_ccm_aad(S390X_AES_CCM_CTX
*ctx
, const unsigned char *aad
,
1706 ctx
->aes
.ccm
.nonce
.b
[0] |= S390X_CCM_AAD_FLAG
;
1708 /* Suppress 'type-punned pointer dereference' warning. */
1709 ptr
= ctx
->aes
.ccm
.buf
.b
;
1711 if (alen
< ((1 << 16) - (1 << 8))) {
1712 *(uint16_t *)ptr
= alen
;
1714 } else if (sizeof(alen
) == 8
1715 && alen
>= (size_t)1 << (32 % (sizeof(alen
) * 8))) {
1716 *(uint16_t *)ptr
= 0xffff;
1717 *(uint64_t *)(ptr
+ 2) = alen
;
1720 *(uint16_t *)ptr
= 0xfffe;
1721 *(uint32_t *)(ptr
+ 2) = alen
;
1725 while (i
< 16 && alen
) {
1726 ctx
->aes
.ccm
.buf
.b
[i
] = *aad
;
1732 ctx
->aes
.ccm
.buf
.b
[i
] = 0;
1736 ctx
->aes
.ccm
.kmac_param
.icv
.g
[0] = 0;
1737 ctx
->aes
.ccm
.kmac_param
.icv
.g
[1] = 0;
1738 s390x_kmac(ctx
->aes
.ccm
.nonce
.b
, 32, ctx
->aes
.ccm
.fc
,
1739 &ctx
->aes
.ccm
.kmac_param
);
1740 ctx
->aes
.ccm
.blocks
+= 2;
1743 alen
&= ~(size_t)0xf;
1745 s390x_kmac(aad
, alen
, ctx
->aes
.ccm
.fc
, &ctx
->aes
.ccm
.kmac_param
);
1746 ctx
->aes
.ccm
.blocks
+= alen
>> 4;
1750 for (i
= 0; i
< rem
; i
++)
1751 ctx
->aes
.ccm
.kmac_param
.icv
.b
[i
] ^= aad
[i
];
1753 s390x_km(ctx
->aes
.ccm
.kmac_param
.icv
.b
, 16,
1754 ctx
->aes
.ccm
.kmac_param
.icv
.b
, ctx
->aes
.ccm
.fc
,
1755 ctx
->aes
.ccm
.kmac_param
.k
);
1756 ctx
->aes
.ccm
.blocks
++;
1761 * En/de-crypt plain/cipher-text. Compute tag from plaintext. Returns 0 for
1764 static int s390x_aes_ccm(S390X_AES_CCM_CTX
*ctx
, const unsigned char *in
,
1765 unsigned char *out
, size_t len
, int enc
)
1768 unsigned int i
, l
, num
;
1769 unsigned char flags
;
1771 flags
= ctx
->aes
.ccm
.nonce
.b
[0];
1772 if (!(flags
& S390X_CCM_AAD_FLAG
)) {
1773 s390x_km(ctx
->aes
.ccm
.nonce
.b
, 16, ctx
->aes
.ccm
.kmac_param
.icv
.b
,
1774 ctx
->aes
.ccm
.fc
, ctx
->aes
.ccm
.kmac_param
.k
);
1775 ctx
->aes
.ccm
.blocks
++;
1778 ctx
->aes
.ccm
.nonce
.b
[0] = l
;
1781 * Reconstruct length from encoded length field
1782 * and initialize it with counter value.
1785 for (i
= 15 - l
; i
< 15; i
++) {
1786 n
|= ctx
->aes
.ccm
.nonce
.b
[i
];
1787 ctx
->aes
.ccm
.nonce
.b
[i
] = 0;
1790 n
|= ctx
->aes
.ccm
.nonce
.b
[15];
1791 ctx
->aes
.ccm
.nonce
.b
[15] = 1;
1794 return -1; /* length mismatch */
1797 /* Two operations per block plus one for tag encryption */
1798 ctx
->aes
.ccm
.blocks
+= (((len
+ 15) >> 4) << 1) + 1;
1799 if (ctx
->aes
.ccm
.blocks
> (1ULL << 61))
1800 return -2; /* too much data */
1805 len
&= ~(size_t)0xf;
1808 /* mac-then-encrypt */
1810 s390x_kmac(in
, len
, ctx
->aes
.ccm
.fc
, &ctx
->aes
.ccm
.kmac_param
);
1812 for (i
= 0; i
< rem
; i
++)
1813 ctx
->aes
.ccm
.kmac_param
.icv
.b
[i
] ^= in
[len
+ i
];
1815 s390x_km(ctx
->aes
.ccm
.kmac_param
.icv
.b
, 16,
1816 ctx
->aes
.ccm
.kmac_param
.icv
.b
, ctx
->aes
.ccm
.fc
,
1817 ctx
->aes
.ccm
.kmac_param
.k
);
1820 CRYPTO_ctr128_encrypt_ctr32(in
, out
, len
+ rem
, &ctx
->aes
.key
.k
,
1821 ctx
->aes
.ccm
.nonce
.b
, ctx
->aes
.ccm
.buf
.b
,
1822 &num
, (ctr128_f
)AES_ctr32_encrypt
);
1824 /* decrypt-then-mac */
1825 CRYPTO_ctr128_encrypt_ctr32(in
, out
, len
+ rem
, &ctx
->aes
.key
.k
,
1826 ctx
->aes
.ccm
.nonce
.b
, ctx
->aes
.ccm
.buf
.b
,
1827 &num
, (ctr128_f
)AES_ctr32_encrypt
);
1830 s390x_kmac(out
, len
, ctx
->aes
.ccm
.fc
, &ctx
->aes
.ccm
.kmac_param
);
1832 for (i
= 0; i
< rem
; i
++)
1833 ctx
->aes
.ccm
.kmac_param
.icv
.b
[i
] ^= out
[len
+ i
];
1835 s390x_km(ctx
->aes
.ccm
.kmac_param
.icv
.b
, 16,
1836 ctx
->aes
.ccm
.kmac_param
.icv
.b
, ctx
->aes
.ccm
.fc
,
1837 ctx
->aes
.ccm
.kmac_param
.k
);
1841 for (i
= 15 - l
; i
< 16; i
++)
1842 ctx
->aes
.ccm
.nonce
.b
[i
] = 0;
1844 s390x_km(ctx
->aes
.ccm
.nonce
.b
, 16, ctx
->aes
.ccm
.buf
.b
, ctx
->aes
.ccm
.fc
,
1845 ctx
->aes
.ccm
.kmac_param
.k
);
1846 ctx
->aes
.ccm
.kmac_param
.icv
.g
[0] ^= ctx
->aes
.ccm
.buf
.g
[0];
1847 ctx
->aes
.ccm
.kmac_param
.icv
.g
[1] ^= ctx
->aes
.ccm
.buf
.g
[1];
1849 ctx
->aes
.ccm
.nonce
.b
[0] = flags
; /* restore flags field */
1854 * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
1855 * if successful. Otherwise -1 is returned.
1857 static int s390x_aes_ccm_tls_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1858 const unsigned char *in
, size_t len
)
1860 S390X_AES_CCM_CTX
*cctx
= EVP_C_DATA(S390X_AES_CCM_CTX
, ctx
);
1861 unsigned char *ivec
= EVP_CIPHER_CTX_iv_noconst(ctx
);
1862 unsigned char *buf
= EVP_CIPHER_CTX_buf_noconst(ctx
);
1863 const int enc
= EVP_CIPHER_CTX_encrypting(ctx
);
1866 || len
< (EVP_CCM_TLS_EXPLICIT_IV_LEN
+ (size_t)cctx
->aes
.ccm
.m
))
1870 /* Set explicit iv (sequence number). */
1871 memcpy(out
, buf
, EVP_CCM_TLS_EXPLICIT_IV_LEN
);
1874 len
-= EVP_CCM_TLS_EXPLICIT_IV_LEN
+ cctx
->aes
.ccm
.m
;
1876 * Get explicit iv (sequence number). We already have fixed iv
1877 * (server/client_write_iv) here.
1879 memcpy(ivec
+ EVP_CCM_TLS_FIXED_IV_LEN
, in
, EVP_CCM_TLS_EXPLICIT_IV_LEN
);
1880 s390x_aes_ccm_setiv(cctx
, ivec
, len
);
1882 /* Process aad (sequence number|type|version|length) */
1883 s390x_aes_ccm_aad(cctx
, buf
, cctx
->aes
.ccm
.tls_aad_len
);
1885 in
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1886 out
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1889 if (s390x_aes_ccm(cctx
, in
, out
, len
, enc
))
1892 memcpy(out
+ len
, cctx
->aes
.ccm
.kmac_param
.icv
.b
, cctx
->aes
.ccm
.m
);
1893 return len
+ EVP_CCM_TLS_EXPLICIT_IV_LEN
+ cctx
->aes
.ccm
.m
;
1895 if (!s390x_aes_ccm(cctx
, in
, out
, len
, enc
)) {
1896 if (!CRYPTO_memcmp(cctx
->aes
.ccm
.kmac_param
.icv
.b
, in
+ len
,
1901 OPENSSL_cleanse(out
, len
);
1907 * Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is
1910 static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX
*ctx
,
1911 const unsigned char *key
,
1912 const unsigned char *iv
, int enc
)
1914 S390X_AES_CCM_CTX
*cctx
= EVP_C_DATA(S390X_AES_CCM_CTX
, ctx
);
1915 unsigned char *ivec
;
1918 if (iv
== NULL
&& key
== NULL
)
1922 keylen
= EVP_CIPHER_CTX_key_length(ctx
);
1923 cctx
->aes
.ccm
.fc
= S390X_AES_FC(keylen
);
1924 memcpy(cctx
->aes
.ccm
.kmac_param
.k
, key
, keylen
);
1926 /* Store encoded m and l. */
1927 cctx
->aes
.ccm
.nonce
.b
[0] = ((cctx
->aes
.ccm
.l
- 1) & 0x7)
1928 | (((cctx
->aes
.ccm
.m
- 2) >> 1) & 0x7) << 3;
1929 memset(cctx
->aes
.ccm
.nonce
.b
+ 1, 0,
1930 sizeof(cctx
->aes
.ccm
.nonce
.b
));
1931 cctx
->aes
.ccm
.blocks
= 0;
1933 cctx
->aes
.ccm
.key_set
= 1;
1937 ivec
= EVP_CIPHER_CTX_iv_noconst(ctx
);
1938 memcpy(ivec
, iv
, 15 - cctx
->aes
.ccm
.l
);
1940 cctx
->aes
.ccm
.iv_set
= 1;
1947 * Called from EVP layer to initialize context, process additional
1948 * authenticated data, en/de-crypt plain/cipher-text and authenticate
1949 * plaintext or process a TLS packet, depending on context. Returns bytes
1950 * written on success. Otherwise -1 is returned.
1952 static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1953 const unsigned char *in
, size_t len
)
1955 S390X_AES_CCM_CTX
*cctx
= EVP_C_DATA(S390X_AES_CCM_CTX
, ctx
);
1956 const int enc
= EVP_CIPHER_CTX_encrypting(ctx
);
1958 unsigned char *buf
, *ivec
;
1960 if (!cctx
->aes
.ccm
.key_set
)
1963 if (cctx
->aes
.ccm
.tls_aad_len
>= 0)
1964 return s390x_aes_ccm_tls_cipher(ctx
, out
, in
, len
);
1967 * Final(): Does not return any data. Recall that ccm is mac-then-encrypt
1968 * so integrity must be checked already at Update() i.e., before
1969 * potentially corrupted data is output.
1971 if (in
== NULL
&& out
!= NULL
)
1974 if (!cctx
->aes
.ccm
.iv_set
)
1978 /* Update(): Pass message length. */
1980 ivec
= EVP_CIPHER_CTX_iv_noconst(ctx
);
1981 s390x_aes_ccm_setiv(cctx
, ivec
, len
);
1983 cctx
->aes
.ccm
.len_set
= 1;
1987 /* Update(): Process aad. */
1988 if (!cctx
->aes
.ccm
.len_set
&& len
)
1991 s390x_aes_ccm_aad(cctx
, in
, len
);
1995 /* The tag must be set before actually decrypting data */
1996 if (!enc
&& !cctx
->aes
.ccm
.tag_set
)
1999 /* Update(): Process message. */
2001 if (!cctx
->aes
.ccm
.len_set
) {
2003 * In case message length was not previously set explicitly via
2004 * Update(), set it now.
2006 ivec
= EVP_CIPHER_CTX_iv_noconst(ctx
);
2007 s390x_aes_ccm_setiv(cctx
, ivec
, len
);
2009 cctx
->aes
.ccm
.len_set
= 1;
2013 if (s390x_aes_ccm(cctx
, in
, out
, len
, enc
))
2016 cctx
->aes
.ccm
.tag_set
= 1;
2021 if (!s390x_aes_ccm(cctx
, in
, out
, len
, enc
)) {
2022 buf
= EVP_CIPHER_CTX_buf_noconst(ctx
);
2023 if (!CRYPTO_memcmp(cctx
->aes
.ccm
.kmac_param
.icv
.b
, buf
,
2029 OPENSSL_cleanse(out
, len
);
2031 cctx
->aes
.ccm
.iv_set
= 0;
2032 cctx
->aes
.ccm
.tag_set
= 0;
2033 cctx
->aes
.ccm
.len_set
= 0;
2039 * Performs various operations on the context structure depending on control
2040 * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
2041 * Code is big-endian.
2043 static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
2045 S390X_AES_CCM_CTX
*cctx
= EVP_C_DATA(S390X_AES_CCM_CTX
, c
);
2046 unsigned char *buf
, *iv
;
2051 cctx
->aes
.ccm
.key_set
= 0;
2052 cctx
->aes
.ccm
.iv_set
= 0;
2053 cctx
->aes
.ccm
.l
= 8;
2054 cctx
->aes
.ccm
.m
= 12;
2055 cctx
->aes
.ccm
.tag_set
= 0;
2056 cctx
->aes
.ccm
.len_set
= 0;
2057 cctx
->aes
.ccm
.tls_aad_len
= -1;
2060 case EVP_CTRL_GET_IVLEN
:
2061 *(int *)ptr
= 15 - cctx
->aes
.ccm
.l
;
2064 case EVP_CTRL_AEAD_TLS1_AAD
:
2065 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
2068 /* Save the aad for later use. */
2069 buf
= EVP_CIPHER_CTX_buf_noconst(c
);
2070 memcpy(buf
, ptr
, arg
);
2071 cctx
->aes
.ccm
.tls_aad_len
= arg
;
2073 len
= buf
[arg
- 2] << 8 | buf
[arg
- 1];
2074 if (len
< EVP_CCM_TLS_EXPLICIT_IV_LEN
)
2077 /* Correct length for explicit iv. */
2078 len
-= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
2080 enc
= EVP_CIPHER_CTX_encrypting(c
);
2082 if (len
< cctx
->aes
.ccm
.m
)
2085 /* Correct length for tag. */
2086 len
-= cctx
->aes
.ccm
.m
;
2089 buf
[arg
- 2] = len
>> 8;
2090 buf
[arg
- 1] = len
& 0xff;
2092 /* Extra padding: tag appended to record. */
2093 return cctx
->aes
.ccm
.m
;
2095 case EVP_CTRL_CCM_SET_IV_FIXED
:
2096 if (arg
!= EVP_CCM_TLS_FIXED_IV_LEN
)
2099 /* Copy to first part of the iv. */
2100 iv
= EVP_CIPHER_CTX_iv_noconst(c
);
2101 memcpy(iv
, ptr
, arg
);
2104 case EVP_CTRL_AEAD_SET_IVLEN
:
2108 case EVP_CTRL_CCM_SET_L
:
2109 if (arg
< 2 || arg
> 8)
2112 cctx
->aes
.ccm
.l
= arg
;
2115 case EVP_CTRL_AEAD_SET_TAG
:
2116 if ((arg
& 1) || arg
< 4 || arg
> 16)
2119 enc
= EVP_CIPHER_CTX_encrypting(c
);
2124 cctx
->aes
.ccm
.tag_set
= 1;
2125 buf
= EVP_CIPHER_CTX_buf_noconst(c
);
2126 memcpy(buf
, ptr
, arg
);
2129 cctx
->aes
.ccm
.m
= arg
;
2132 case EVP_CTRL_AEAD_GET_TAG
:
2133 enc
= EVP_CIPHER_CTX_encrypting(c
);
2134 if (!enc
|| !cctx
->aes
.ccm
.tag_set
)
2137 if(arg
< cctx
->aes
.ccm
.m
)
2140 memcpy(ptr
, cctx
->aes
.ccm
.kmac_param
.icv
.b
, cctx
->aes
.ccm
.m
);
2141 cctx
->aes
.ccm
.tag_set
= 0;
2142 cctx
->aes
.ccm
.iv_set
= 0;
2143 cctx
->aes
.ccm
.len_set
= 0;
2154 # define s390x_aes_ccm_cleanup aes_ccm_cleanup
2156 # ifndef OPENSSL_NO_OCB
2157 # define S390X_AES_OCB_CTX EVP_AES_OCB_CTX
2159 # define s390x_aes_ocb_init_key aes_ocb_init_key
2160 static int s390x_aes_ocb_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
2161 const unsigned char *iv
, int enc
);
2162 # define s390x_aes_ocb_cipher aes_ocb_cipher
2163 static int s390x_aes_ocb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2164 const unsigned char *in
, size_t len
);
2165 # define s390x_aes_ocb_cleanup aes_ocb_cleanup
2166 static int s390x_aes_ocb_cleanup(EVP_CIPHER_CTX
*);
2167 # define s390x_aes_ocb_ctrl aes_ocb_ctrl
2168 static int s390x_aes_ocb_ctrl(EVP_CIPHER_CTX
*, int type
, int arg
, void *ptr
);
2171 # ifndef OPENSSL_NO_SIV
2172 # define S390X_AES_SIV_CTX EVP_AES_SIV_CTX
2174 # define s390x_aes_siv_init_key aes_siv_init_key
2175 # define s390x_aes_siv_cipher aes_siv_cipher
2176 # define s390x_aes_siv_cleanup aes_siv_cleanup
2177 # define s390x_aes_siv_ctrl aes_siv_ctrl
2180 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode, \
2182 static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
2183 nid##_##keylen##_##nmode,blocksize, \
2186 flags | EVP_CIPH_##MODE##_MODE, \
2187 s390x_aes_##mode##_init_key, \
2188 s390x_aes_##mode##_cipher, \
2190 sizeof(S390X_AES_##MODE##_CTX), \
2196 static const EVP_CIPHER aes_##keylen##_##mode = { \
2197 nid##_##keylen##_##nmode, \
2201 flags | EVP_CIPH_##MODE##_MODE, \
2203 aes_##mode##_cipher, \
2205 sizeof(EVP_AES_KEY), \
2211 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2213 return S390X_aes_##keylen##_##mode##_CAPABLE ? \
2214 &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
2217 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\
2218 static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
2219 nid##_##keylen##_##mode, \
2221 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE ? 2 : 1) * keylen / 8, \
2223 flags | EVP_CIPH_##MODE##_MODE, \
2224 s390x_aes_##mode##_init_key, \
2225 s390x_aes_##mode##_cipher, \
2226 s390x_aes_##mode##_cleanup, \
2227 sizeof(S390X_AES_##MODE##_CTX), \
2230 s390x_aes_##mode##_ctrl, \
2233 static const EVP_CIPHER aes_##keylen##_##mode = { \
2234 nid##_##keylen##_##mode,blocksize, \
2235 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE ? 2 : 1) * keylen / 8, \
2237 flags | EVP_CIPH_##MODE##_MODE, \
2238 aes_##mode##_init_key, \
2239 aes_##mode##_cipher, \
2240 aes_##mode##_cleanup, \
2241 sizeof(EVP_AES_##MODE##_CTX), \
2244 aes_##mode##_ctrl, \
2247 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2249 return S390X_aes_##keylen##_##mode##_CAPABLE ? \
2250 &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
2255 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
2256 static const EVP_CIPHER aes_##keylen##_##mode = { \
2257 nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
2258 flags|EVP_CIPH_##MODE##_MODE, \
2260 aes_##mode##_cipher, \
2262 sizeof(EVP_AES_KEY), \
2263 NULL,NULL,NULL,NULL }; \
2264 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2265 { return &aes_##keylen##_##mode; }
2267 # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
2268 static const EVP_CIPHER aes_##keylen##_##mode = { \
2269 nid##_##keylen##_##mode,blocksize, \
2270 (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \
2272 flags|EVP_CIPH_##MODE##_MODE, \
2273 aes_##mode##_init_key, \
2274 aes_##mode##_cipher, \
2275 aes_##mode##_cleanup, \
2276 sizeof(EVP_AES_##MODE##_CTX), \
2277 NULL,NULL,aes_##mode##_ctrl,NULL }; \
2278 const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
2279 { return &aes_##keylen##_##mode; }
2283 #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
2284 BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2285 BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2286 BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2287 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
2288 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
2289 BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
2290 BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
2292 static int aes_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
2293 const unsigned char *iv
, int enc
)
2296 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2298 mode
= EVP_CIPHER_CTX_mode(ctx
);
2299 if ((mode
== EVP_CIPH_ECB_MODE
|| mode
== EVP_CIPH_CBC_MODE
)
2301 #ifdef HWAES_CAPABLE
2302 if (HWAES_CAPABLE
) {
2303 ret
= HWAES_set_decrypt_key(key
,
2304 EVP_CIPHER_CTX_key_length(ctx
) * 8,
2306 dat
->block
= (block128_f
) HWAES_decrypt
;
2307 dat
->stream
.cbc
= NULL
;
2308 # ifdef HWAES_cbc_encrypt
2309 if (mode
== EVP_CIPH_CBC_MODE
)
2310 dat
->stream
.cbc
= (cbc128_f
) HWAES_cbc_encrypt
;
2314 #ifdef BSAES_CAPABLE
2315 if (BSAES_CAPABLE
&& mode
== EVP_CIPH_CBC_MODE
) {
2316 ret
= AES_set_decrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
2318 dat
->block
= (block128_f
) AES_decrypt
;
2319 dat
->stream
.cbc
= (cbc128_f
) bsaes_cbc_encrypt
;
2322 #ifdef VPAES_CAPABLE
2323 if (VPAES_CAPABLE
) {
2324 ret
= vpaes_set_decrypt_key(key
,
2325 EVP_CIPHER_CTX_key_length(ctx
) * 8,
2327 dat
->block
= (block128_f
) vpaes_decrypt
;
2328 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
2329 (cbc128_f
) vpaes_cbc_encrypt
: NULL
;
2333 ret
= AES_set_decrypt_key(key
,
2334 EVP_CIPHER_CTX_key_length(ctx
) * 8,
2336 dat
->block
= (block128_f
) AES_decrypt
;
2337 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
2338 (cbc128_f
) AES_cbc_encrypt
: NULL
;
2341 #ifdef HWAES_CAPABLE
2342 if (HWAES_CAPABLE
) {
2343 ret
= HWAES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
2345 dat
->block
= (block128_f
) HWAES_encrypt
;
2346 dat
->stream
.cbc
= NULL
;
2347 # ifdef HWAES_cbc_encrypt
2348 if (mode
== EVP_CIPH_CBC_MODE
)
2349 dat
->stream
.cbc
= (cbc128_f
) HWAES_cbc_encrypt
;
2352 # ifdef HWAES_ctr32_encrypt_blocks
2353 if (mode
== EVP_CIPH_CTR_MODE
)
2354 dat
->stream
.ctr
= (ctr128_f
) HWAES_ctr32_encrypt_blocks
;
2357 (void)0; /* terminate potentially open 'else' */
2360 #ifdef BSAES_CAPABLE
2361 if (BSAES_CAPABLE
&& mode
== EVP_CIPH_CTR_MODE
) {
2362 ret
= AES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
2364 dat
->block
= (block128_f
) AES_encrypt
;
2365 dat
->stream
.ctr
= (ctr128_f
) bsaes_ctr32_encrypt_blocks
;
2368 #ifdef VPAES_CAPABLE
2369 if (VPAES_CAPABLE
) {
2370 ret
= vpaes_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
2372 dat
->block
= (block128_f
) vpaes_encrypt
;
2373 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
2374 (cbc128_f
) vpaes_cbc_encrypt
: NULL
;
2378 ret
= AES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
2380 dat
->block
= (block128_f
) AES_encrypt
;
2381 dat
->stream
.cbc
= mode
== EVP_CIPH_CBC_MODE
?
2382 (cbc128_f
) AES_cbc_encrypt
: NULL
;
2384 if (mode
== EVP_CIPH_CTR_MODE
)
2385 dat
->stream
.ctr
= (ctr128_f
) AES_ctr32_encrypt
;
2390 EVPerr(EVP_F_AES_INIT_KEY
, EVP_R_AES_KEY_SETUP_FAILED
);
2397 static int aes_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2398 const unsigned char *in
, size_t len
)
2400 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2402 if (dat
->stream
.cbc
)
2403 (*dat
->stream
.cbc
) (in
, out
, len
, &dat
->ks
,
2404 EVP_CIPHER_CTX_iv_noconst(ctx
),
2405 EVP_CIPHER_CTX_encrypting(ctx
));
2406 else if (EVP_CIPHER_CTX_encrypting(ctx
))
2407 CRYPTO_cbc128_encrypt(in
, out
, len
, &dat
->ks
,
2408 EVP_CIPHER_CTX_iv_noconst(ctx
), dat
->block
);
2410 CRYPTO_cbc128_decrypt(in
, out
, len
, &dat
->ks
,
2411 EVP_CIPHER_CTX_iv_noconst(ctx
), dat
->block
);
2416 static int aes_ecb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2417 const unsigned char *in
, size_t len
)
2419 size_t bl
= EVP_CIPHER_CTX_block_size(ctx
);
2421 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2426 for (i
= 0, len
-= bl
; i
<= len
; i
+= bl
)
2427 (*dat
->block
) (in
+ i
, out
+ i
, &dat
->ks
);
2432 static int aes_ofb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2433 const unsigned char *in
, size_t len
)
2435 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2437 int num
= EVP_CIPHER_CTX_num(ctx
);
2438 CRYPTO_ofb128_encrypt(in
, out
, len
, &dat
->ks
,
2439 EVP_CIPHER_CTX_iv_noconst(ctx
), &num
, dat
->block
);
2440 EVP_CIPHER_CTX_set_num(ctx
, num
);
2444 static int aes_cfb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2445 const unsigned char *in
, size_t len
)
2447 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2449 int num
= EVP_CIPHER_CTX_num(ctx
);
2450 CRYPTO_cfb128_encrypt(in
, out
, len
, &dat
->ks
,
2451 EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
2452 EVP_CIPHER_CTX_encrypting(ctx
), dat
->block
);
2453 EVP_CIPHER_CTX_set_num(ctx
, num
);
2457 static int aes_cfb8_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2458 const unsigned char *in
, size_t len
)
2460 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2462 int num
= EVP_CIPHER_CTX_num(ctx
);
2463 CRYPTO_cfb128_8_encrypt(in
, out
, len
, &dat
->ks
,
2464 EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
2465 EVP_CIPHER_CTX_encrypting(ctx
), dat
->block
);
2466 EVP_CIPHER_CTX_set_num(ctx
, num
);
2470 static int aes_cfb1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2471 const unsigned char *in
, size_t len
)
2473 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2475 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
)) {
2476 int num
= EVP_CIPHER_CTX_num(ctx
);
2477 CRYPTO_cfb128_1_encrypt(in
, out
, len
, &dat
->ks
,
2478 EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
2479 EVP_CIPHER_CTX_encrypting(ctx
), dat
->block
);
2480 EVP_CIPHER_CTX_set_num(ctx
, num
);
2484 while (len
>= MAXBITCHUNK
) {
2485 int num
= EVP_CIPHER_CTX_num(ctx
);
2486 CRYPTO_cfb128_1_encrypt(in
, out
, MAXBITCHUNK
* 8, &dat
->ks
,
2487 EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
2488 EVP_CIPHER_CTX_encrypting(ctx
), dat
->block
);
2489 EVP_CIPHER_CTX_set_num(ctx
, num
);
2495 int num
= EVP_CIPHER_CTX_num(ctx
);
2496 CRYPTO_cfb128_1_encrypt(in
, out
, len
* 8, &dat
->ks
,
2497 EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
2498 EVP_CIPHER_CTX_encrypting(ctx
), dat
->block
);
2499 EVP_CIPHER_CTX_set_num(ctx
, num
);
2505 static int aes_ctr_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2506 const unsigned char *in
, size_t len
)
2508 unsigned int num
= EVP_CIPHER_CTX_num(ctx
);
2509 EVP_AES_KEY
*dat
= EVP_C_DATA(EVP_AES_KEY
,ctx
);
2511 if (dat
->stream
.ctr
)
2512 CRYPTO_ctr128_encrypt_ctr32(in
, out
, len
, &dat
->ks
,
2513 EVP_CIPHER_CTX_iv_noconst(ctx
),
2514 EVP_CIPHER_CTX_buf_noconst(ctx
),
2515 &num
, dat
->stream
.ctr
);
2517 CRYPTO_ctr128_encrypt(in
, out
, len
, &dat
->ks
,
2518 EVP_CIPHER_CTX_iv_noconst(ctx
),
2519 EVP_CIPHER_CTX_buf_noconst(ctx
), &num
,
2521 EVP_CIPHER_CTX_set_num(ctx
, num
);
2525 BLOCK_CIPHER_generic_pack(NID_aes
, 128, 0)
2526 BLOCK_CIPHER_generic_pack(NID_aes
, 192, 0)
2527 BLOCK_CIPHER_generic_pack(NID_aes
, 256, 0)
2529 static int aes_gcm_cleanup(EVP_CIPHER_CTX
*c
)
2531 EVP_AES_GCM_CTX
*gctx
= EVP_C_DATA(EVP_AES_GCM_CTX
,c
);
2534 OPENSSL_cleanse(&gctx
->gcm
, sizeof(gctx
->gcm
));
2535 if (gctx
->iv
!= EVP_CIPHER_CTX_iv_noconst(c
))
2536 OPENSSL_free(gctx
->iv
);
2540 static int aes_gcm_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
2542 EVP_AES_GCM_CTX
*gctx
= EVP_C_DATA(EVP_AES_GCM_CTX
,c
);
2547 gctx
->ivlen
= EVP_CIPHER_iv_length(c
->cipher
);
2551 gctx
->tls_aad_len
= -1;
2554 case EVP_CTRL_GET_IVLEN
:
2555 *(int *)ptr
= gctx
->ivlen
;
2558 case EVP_CTRL_AEAD_SET_IVLEN
:
2561 /* Allocate memory for IV if needed */
2562 if ((arg
> EVP_MAX_IV_LENGTH
) && (arg
> gctx
->ivlen
)) {
2563 if (gctx
->iv
!= c
->iv
)
2564 OPENSSL_free(gctx
->iv
);
2565 if ((gctx
->iv
= OPENSSL_malloc(arg
)) == NULL
) {
2566 EVPerr(EVP_F_AES_GCM_CTRL
, ERR_R_MALLOC_FAILURE
);
2573 case EVP_CTRL_AEAD_SET_TAG
:
2574 if (arg
<= 0 || arg
> 16 || c
->encrypt
)
2576 memcpy(c
->buf
, ptr
, arg
);
2580 case EVP_CTRL_AEAD_GET_TAG
:
2581 if (arg
<= 0 || arg
> 16 || !c
->encrypt
2582 || gctx
->taglen
< 0)
2584 memcpy(ptr
, c
->buf
, arg
);
2587 case EVP_CTRL_GET_IV
:
2588 if (gctx
->iv_gen
!= 1 && gctx
->iv_gen_rand
!= 1)
2590 if (gctx
->ivlen
!= arg
)
2592 memcpy(ptr
, gctx
->iv
, arg
);
2595 case EVP_CTRL_GCM_SET_IV_FIXED
:
2596 /* Special case: -1 length restores whole IV */
2598 memcpy(gctx
->iv
, ptr
, gctx
->ivlen
);
2603 * Fixed field must be at least 4 bytes and invocation field at least
2606 if ((arg
< 4) || (gctx
->ivlen
- arg
) < 8)
2609 memcpy(gctx
->iv
, ptr
, arg
);
2610 if (c
->encrypt
&& RAND_bytes(gctx
->iv
+ arg
, gctx
->ivlen
- arg
) <= 0)
2615 case EVP_CTRL_GCM_IV_GEN
:
2616 if (gctx
->iv_gen
== 0 || gctx
->key_set
== 0)
2618 CRYPTO_gcm128_setiv(&gctx
->gcm
, gctx
->iv
, gctx
->ivlen
);
2619 if (arg
<= 0 || arg
> gctx
->ivlen
)
2621 memcpy(ptr
, gctx
->iv
+ gctx
->ivlen
- arg
, arg
);
2623 * Invocation field will be at least 8 bytes in size and so no need
2624 * to check wrap around or increment more than last 8 bytes.
2626 ctr64_inc(gctx
->iv
+ gctx
->ivlen
- 8);
2630 case EVP_CTRL_GCM_SET_IV_INV
:
2631 if (gctx
->iv_gen
== 0 || gctx
->key_set
== 0 || c
->encrypt
)
2633 memcpy(gctx
->iv
+ gctx
->ivlen
- arg
, ptr
, arg
);
2634 CRYPTO_gcm128_setiv(&gctx
->gcm
, gctx
->iv
, gctx
->ivlen
);
2638 case EVP_CTRL_AEAD_TLS1_AAD
:
2639 /* Save the AAD for later use */
2640 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
2642 memcpy(c
->buf
, ptr
, arg
);
2643 gctx
->tls_aad_len
= arg
;
2644 gctx
->tls_enc_records
= 0;
2646 unsigned int len
= c
->buf
[arg
- 2] << 8 | c
->buf
[arg
- 1];
2647 /* Correct length for explicit IV */
2648 if (len
< EVP_GCM_TLS_EXPLICIT_IV_LEN
)
2650 len
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
2651 /* If decrypting correct for tag too */
2653 if (len
< EVP_GCM_TLS_TAG_LEN
)
2655 len
-= EVP_GCM_TLS_TAG_LEN
;
2657 c
->buf
[arg
- 2] = len
>> 8;
2658 c
->buf
[arg
- 1] = len
& 0xff;
2660 /* Extra padding: tag appended to record */
2661 return EVP_GCM_TLS_TAG_LEN
;
2665 EVP_CIPHER_CTX
*out
= ptr
;
2666 EVP_AES_GCM_CTX
*gctx_out
= EVP_C_DATA(EVP_AES_GCM_CTX
,out
);
2667 if (gctx
->gcm
.key
) {
2668 if (gctx
->gcm
.key
!= &gctx
->ks
)
2670 gctx_out
->gcm
.key
= &gctx_out
->ks
;
2672 if (gctx
->iv
== c
->iv
)
2673 gctx_out
->iv
= out
->iv
;
2675 if ((gctx_out
->iv
= OPENSSL_malloc(gctx
->ivlen
)) == NULL
) {
2676 EVPerr(EVP_F_AES_GCM_CTRL
, ERR_R_MALLOC_FAILURE
);
2679 memcpy(gctx_out
->iv
, gctx
->iv
, gctx
->ivlen
);
2690 static int aes_gcm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
2691 const unsigned char *iv
, int enc
)
2693 EVP_AES_GCM_CTX
*gctx
= EVP_C_DATA(EVP_AES_GCM_CTX
,ctx
);
2698 #ifdef HWAES_CAPABLE
2699 if (HWAES_CAPABLE
) {
2700 HWAES_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
2701 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
2702 (block128_f
) HWAES_encrypt
);
2703 # ifdef HWAES_ctr32_encrypt_blocks
2704 gctx
->ctr
= (ctr128_f
) HWAES_ctr32_encrypt_blocks
;
2711 #ifdef BSAES_CAPABLE
2712 if (BSAES_CAPABLE
) {
2713 AES_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
2714 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
2715 (block128_f
) AES_encrypt
);
2716 gctx
->ctr
= (ctr128_f
) bsaes_ctr32_encrypt_blocks
;
2720 #ifdef VPAES_CAPABLE
2721 if (VPAES_CAPABLE
) {
2722 vpaes_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
2723 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
2724 (block128_f
) vpaes_encrypt
);
2729 (void)0; /* terminate potentially open 'else' */
2731 AES_set_encrypt_key(key
, ctx
->key_len
* 8, &gctx
->ks
.ks
);
2732 CRYPTO_gcm128_init(&gctx
->gcm
, &gctx
->ks
,
2733 (block128_f
) AES_encrypt
);
2735 gctx
->ctr
= (ctr128_f
) AES_ctr32_encrypt
;
2742 * If we have an iv can set it directly, otherwise use saved IV.
2744 if (iv
== NULL
&& gctx
->iv_set
)
2747 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
2752 /* If key set use IV, otherwise copy */
2754 CRYPTO_gcm128_setiv(&gctx
->gcm
, iv
, gctx
->ivlen
);
2756 memcpy(gctx
->iv
, iv
, gctx
->ivlen
);
2764 * Handle TLS GCM packet format. This consists of the last portion of the IV
2765 * followed by the payload and finally the tag. On encrypt generate IV,
2766 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
2770 static int aes_gcm_tls_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2771 const unsigned char *in
, size_t len
)
2773 EVP_AES_GCM_CTX
*gctx
= EVP_C_DATA(EVP_AES_GCM_CTX
,ctx
);
2775 /* Encrypt/decrypt must be performed in place */
2777 || len
< (EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
))
2781 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
2782 * Requirements from SP 800-38D". The requirements is for one party to the
2783 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
2786 if (ctx
->encrypt
&& ++gctx
->tls_enc_records
== 0) {
2787 EVPerr(EVP_F_AES_GCM_TLS_CIPHER
, EVP_R_TOO_MANY_RECORDS
);
2792 * Set IV from start of buffer or generate IV and write to start of
2795 if (EVP_CIPHER_CTX_ctrl(ctx
, ctx
->encrypt
? EVP_CTRL_GCM_IV_GEN
2796 : EVP_CTRL_GCM_SET_IV_INV
,
2797 EVP_GCM_TLS_EXPLICIT_IV_LEN
, out
) <= 0)
2800 if (CRYPTO_gcm128_aad(&gctx
->gcm
, ctx
->buf
, gctx
->tls_aad_len
))
2802 /* Fix buffer and length to point to payload */
2803 in
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
2804 out
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
2805 len
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
;
2807 /* Encrypt payload */
2810 #if defined(AES_GCM_ASM)
2811 if (len
>= 32 && AES_GCM_ASM(gctx
)) {
2812 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, NULL
, NULL
, 0))
2815 bulk
= AES_gcm_encrypt(in
, out
, len
,
2817 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
2818 gctx
->gcm
.len
.u
[1] += bulk
;
2821 if (CRYPTO_gcm128_encrypt_ctr32(&gctx
->gcm
,
2824 len
- bulk
, gctx
->ctr
))
2828 #if defined(AES_GCM_ASM2)
2829 if (len
>= 32 && AES_GCM_ASM2(gctx
)) {
2830 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, NULL
, NULL
, 0))
2833 bulk
= AES_gcm_encrypt(in
, out
, len
,
2835 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
2836 gctx
->gcm
.len
.u
[1] += bulk
;
2839 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
,
2840 in
+ bulk
, out
+ bulk
, len
- bulk
))
2844 /* Finally write tag */
2845 CRYPTO_gcm128_tag(&gctx
->gcm
, out
, EVP_GCM_TLS_TAG_LEN
);
2846 rv
= len
+ EVP_GCM_TLS_EXPLICIT_IV_LEN
+ EVP_GCM_TLS_TAG_LEN
;
2851 #if defined(AES_GCM_ASM)
2852 if (len
>= 16 && AES_GCM_ASM(gctx
)) {
2853 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, NULL
, NULL
, 0))
2856 bulk
= AES_gcm_decrypt(in
, out
, len
,
2858 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
2859 gctx
->gcm
.len
.u
[1] += bulk
;
2862 if (CRYPTO_gcm128_decrypt_ctr32(&gctx
->gcm
,
2865 len
- bulk
, gctx
->ctr
))
2869 #if defined(AES_GCM_ASM2)
2870 if (len
>= 16 && AES_GCM_ASM2(gctx
)) {
2871 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, NULL
, NULL
, 0))
2874 bulk
= AES_gcm_decrypt(in
, out
, len
,
2876 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
2877 gctx
->gcm
.len
.u
[1] += bulk
;
2880 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
,
2881 in
+ bulk
, out
+ bulk
, len
- bulk
))
2885 CRYPTO_gcm128_tag(&gctx
->gcm
, ctx
->buf
, EVP_GCM_TLS_TAG_LEN
);
2886 /* If tag mismatch wipe buffer */
2887 if (CRYPTO_memcmp(ctx
->buf
, in
+ len
, EVP_GCM_TLS_TAG_LEN
)) {
2888 OPENSSL_cleanse(out
, len
);
2896 gctx
->tls_aad_len
= -1;
2902 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
2904 * See also 8.2.2 RBG-based construction.
2905 * Random construction consists of a free field (which can be NULL) and a
2906 * random field which will use a DRBG that can return at least 96 bits of
2907 * entropy strength. (The DRBG must be seeded by the FIPS module).
2909 static int aes_gcm_iv_generate(EVP_AES_GCM_CTX
*gctx
, int offset
)
2911 int sz
= gctx
->ivlen
- offset
;
2913 /* Must be at least 96 bits */
2914 if (sz
<= 0 || gctx
->ivlen
< 12)
2917 /* Use DRBG to generate random iv */
2918 if (RAND_bytes(gctx
->iv
+ offset
, sz
) <= 0)
2922 #endif /* FIPS_MODE */
2924 static int aes_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
2925 const unsigned char *in
, size_t len
)
2927 EVP_AES_GCM_CTX
*gctx
= EVP_C_DATA(EVP_AES_GCM_CTX
,ctx
);
2929 /* If not set up, return error */
2933 if (gctx
->tls_aad_len
>= 0)
2934 return aes_gcm_tls_cipher(ctx
, out
, in
, len
);
2938 * FIPS requires generation of AES-GCM IV's inside the FIPS module.
2939 * The IV can still be set externally (the security policy will state that
2940 * this is not FIPS compliant). There are some applications
2941 * where setting the IV externally is the only option available.
2943 if (!gctx
->iv_set
) {
2944 if (!ctx
->encrypt
|| !aes_gcm_iv_generate(gctx
, 0))
2946 CRYPTO_gcm128_setiv(&gctx
->gcm
, gctx
->iv
, gctx
->ivlen
);
2948 gctx
->iv_gen_rand
= 1;
2953 #endif /* FIPS_MODE */
2957 if (CRYPTO_gcm128_aad(&gctx
->gcm
, in
, len
))
2959 } else if (ctx
->encrypt
) {
2962 #if defined(AES_GCM_ASM)
2963 if (len
>= 32 && AES_GCM_ASM(gctx
)) {
2964 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
2966 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, in
, out
, res
))
2969 bulk
= AES_gcm_encrypt(in
+ res
,
2970 out
+ res
, len
- res
,
2971 gctx
->gcm
.key
, gctx
->gcm
.Yi
.c
,
2973 gctx
->gcm
.len
.u
[1] += bulk
;
2977 if (CRYPTO_gcm128_encrypt_ctr32(&gctx
->gcm
,
2980 len
- bulk
, gctx
->ctr
))
2984 #if defined(AES_GCM_ASM2)
2985 if (len
>= 32 && AES_GCM_ASM2(gctx
)) {
2986 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
2988 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
, in
, out
, res
))
2991 bulk
= AES_gcm_encrypt(in
+ res
,
2992 out
+ res
, len
- res
,
2993 gctx
->gcm
.key
, gctx
->gcm
.Yi
.c
,
2995 gctx
->gcm
.len
.u
[1] += bulk
;
2999 if (CRYPTO_gcm128_encrypt(&gctx
->gcm
,
3000 in
+ bulk
, out
+ bulk
, len
- bulk
))
3006 #if defined(AES_GCM_ASM)
3007 if (len
>= 16 && AES_GCM_ASM(gctx
)) {
3008 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
3010 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, in
, out
, res
))
3013 bulk
= AES_gcm_decrypt(in
+ res
,
3014 out
+ res
, len
- res
,
3016 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
3017 gctx
->gcm
.len
.u
[1] += bulk
;
3021 if (CRYPTO_gcm128_decrypt_ctr32(&gctx
->gcm
,
3024 len
- bulk
, gctx
->ctr
))
3028 #if defined(AES_GCM_ASM2)
3029 if (len
>= 16 && AES_GCM_ASM2(gctx
)) {
3030 size_t res
= (16 - gctx
->gcm
.mres
) % 16;
3032 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
, in
, out
, res
))
3035 bulk
= AES_gcm_decrypt(in
+ res
,
3036 out
+ res
, len
- res
,
3038 gctx
->gcm
.Yi
.c
, gctx
->gcm
.Xi
.u
);
3039 gctx
->gcm
.len
.u
[1] += bulk
;
3043 if (CRYPTO_gcm128_decrypt(&gctx
->gcm
,
3044 in
+ bulk
, out
+ bulk
, len
- bulk
))
3050 if (!ctx
->encrypt
) {
3051 if (gctx
->taglen
< 0)
3053 if (CRYPTO_gcm128_finish(&gctx
->gcm
, ctx
->buf
, gctx
->taglen
) != 0)
3058 CRYPTO_gcm128_tag(&gctx
->gcm
, ctx
->buf
, 16);
3060 /* Don't reuse the IV */
3067 #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
3068 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
3069 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
3070 | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_CUSTOM_IV_LENGTH)
3072 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 12, gcm
, GCM
,
3073 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
3074 BLOCK_CIPHER_custom(NID_aes
, 192, 1, 12, gcm
, GCM
,
3075 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
3076 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 12, gcm
, GCM
,
3077 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
3079 static int aes_xts_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
3081 EVP_AES_XTS_CTX
*xctx
= EVP_C_DATA(EVP_AES_XTS_CTX
, c
);
3083 if (type
== EVP_CTRL_COPY
) {
3084 EVP_CIPHER_CTX
*out
= ptr
;
3085 EVP_AES_XTS_CTX
*xctx_out
= EVP_C_DATA(EVP_AES_XTS_CTX
,out
);
3087 if (xctx
->xts
.key1
) {
3088 if (xctx
->xts
.key1
!= &xctx
->ks1
)
3090 xctx_out
->xts
.key1
= &xctx_out
->ks1
;
3092 if (xctx
->xts
.key2
) {
3093 if (xctx
->xts
.key2
!= &xctx
->ks2
)
3095 xctx_out
->xts
.key2
= &xctx_out
->ks2
;
3098 } else if (type
!= EVP_CTRL_INIT
)
3100 /* key1 and key2 are used as an indicator both key and IV are set */
3101 xctx
->xts
.key1
= NULL
;
3102 xctx
->xts
.key2
= NULL
;
3106 static int aes_xts_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
3107 const unsigned char *iv
, int enc
)
3109 EVP_AES_XTS_CTX
*xctx
= EVP_C_DATA(EVP_AES_XTS_CTX
,ctx
);
3116 /* The key is two half length keys in reality */
3117 const int bytes
= EVP_CIPHER_CTX_key_length(ctx
) / 2;
3118 const int bits
= bytes
* 8;
3121 * Verify that the two keys are different.
3123 * This addresses the vulnerability described in Rogaway's
3124 * September 2004 paper:
3126 * "Efficient Instantiations of Tweakable Blockciphers and
3127 * Refinements to Modes OCB and PMAC".
3128 * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
3130 * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
3132 * "The check for Key_1 != Key_2 shall be done at any place
3133 * BEFORE using the keys in the XTS-AES algorithm to process
3136 if ((!allow_insecure_decrypt
|| enc
)
3137 && CRYPTO_memcmp(key
, key
+ bytes
, bytes
) == 0) {
3138 EVPerr(EVP_F_AES_XTS_INIT_KEY
, EVP_R_XTS_DUPLICATED_KEYS
);
3143 xctx
->stream
= enc
? AES_xts_encrypt
: AES_xts_decrypt
;
3145 xctx
->stream
= NULL
;
3147 /* key_len is two AES keys */
3148 #ifdef HWAES_CAPABLE
3149 if (HWAES_CAPABLE
) {
3151 HWAES_set_encrypt_key(key
, bits
, &xctx
->ks1
.ks
);
3152 xctx
->xts
.block1
= (block128_f
) HWAES_encrypt
;
3153 # ifdef HWAES_xts_encrypt
3154 xctx
->stream
= HWAES_xts_encrypt
;
3157 HWAES_set_decrypt_key(key
, bits
, &xctx
->ks1
.ks
);
3158 xctx
->xts
.block1
= (block128_f
) HWAES_decrypt
;
3159 # ifdef HWAES_xts_decrypt
3160 xctx
->stream
= HWAES_xts_decrypt
;
3164 HWAES_set_encrypt_key(key
+ bytes
, bits
, &xctx
->ks2
.ks
);
3165 xctx
->xts
.block2
= (block128_f
) HWAES_encrypt
;
3167 xctx
->xts
.key1
= &xctx
->ks1
;
3171 #ifdef BSAES_CAPABLE
3173 xctx
->stream
= enc
? bsaes_xts_encrypt
: bsaes_xts_decrypt
;
3176 #ifdef VPAES_CAPABLE
3177 if (VPAES_CAPABLE
) {
3179 vpaes_set_encrypt_key(key
, bits
, &xctx
->ks1
.ks
);
3180 xctx
->xts
.block1
= (block128_f
) vpaes_encrypt
;
3182 vpaes_set_decrypt_key(key
, bits
, &xctx
->ks1
.ks
);
3183 xctx
->xts
.block1
= (block128_f
) vpaes_decrypt
;
3186 vpaes_set_encrypt_key(key
+ bytes
, bits
, &xctx
->ks2
.ks
);
3187 xctx
->xts
.block2
= (block128_f
) vpaes_encrypt
;
3189 xctx
->xts
.key1
= &xctx
->ks1
;
3193 (void)0; /* terminate potentially open 'else' */
3196 AES_set_encrypt_key(key
, bits
, &xctx
->ks1
.ks
);
3197 xctx
->xts
.block1
= (block128_f
) AES_encrypt
;
3199 AES_set_decrypt_key(key
, bits
, &xctx
->ks1
.ks
);
3200 xctx
->xts
.block1
= (block128_f
) AES_decrypt
;
3203 AES_set_encrypt_key(key
+ bytes
, bits
, &xctx
->ks2
.ks
);
3204 xctx
->xts
.block2
= (block128_f
) AES_encrypt
;
3206 xctx
->xts
.key1
= &xctx
->ks1
;
3211 xctx
->xts
.key2
= &xctx
->ks2
;
3212 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
), iv
, 16);
3218 static int aes_xts_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
3219 const unsigned char *in
, size_t len
)
3221 EVP_AES_XTS_CTX
*xctx
= EVP_C_DATA(EVP_AES_XTS_CTX
,ctx
);
3223 if (xctx
->xts
.key1
== NULL
3224 || xctx
->xts
.key2
== NULL
3227 || len
< AES_BLOCK_SIZE
)
3231 * Impose a limit of 2^20 blocks per data unit as specifed by
3232 * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
3233 * indicated that this was a SHOULD NOT rather than a MUST NOT.
3234 * NIST SP 800-38E mandates the same limit.
3236 if (len
> XTS_MAX_BLOCKS_PER_DATA_UNIT
* AES_BLOCK_SIZE
) {
3237 EVPerr(EVP_F_AES_XTS_CIPHER
, EVP_R_XTS_DATA_UNIT_IS_TOO_LARGE
);
3242 (*xctx
->stream
) (in
, out
, len
,
3243 xctx
->xts
.key1
, xctx
->xts
.key2
,
3244 EVP_CIPHER_CTX_iv_noconst(ctx
));
3245 else if (CRYPTO_xts128_encrypt(&xctx
->xts
, EVP_CIPHER_CTX_iv_noconst(ctx
),
3247 EVP_CIPHER_CTX_encrypting(ctx
)))
3252 #define aes_xts_cleanup NULL
3254 #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
3255 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
3256 | EVP_CIPH_CUSTOM_COPY)
3258 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 16, xts
, XTS
, XTS_FLAGS
)
3259 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 16, xts
, XTS
, XTS_FLAGS
)
3261 static int aes_ccm_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
3263 EVP_AES_CCM_CTX
*cctx
= EVP_C_DATA(EVP_AES_CCM_CTX
,c
);
3272 cctx
->tls_aad_len
= -1;
3275 case EVP_CTRL_GET_IVLEN
:
3276 *(int *)ptr
= 15 - cctx
->L
;
3279 case EVP_CTRL_AEAD_TLS1_AAD
:
3280 /* Save the AAD for later use */
3281 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
3283 memcpy(EVP_CIPHER_CTX_buf_noconst(c
), ptr
, arg
);
3284 cctx
->tls_aad_len
= arg
;
3287 EVP_CIPHER_CTX_buf_noconst(c
)[arg
- 2] << 8
3288 | EVP_CIPHER_CTX_buf_noconst(c
)[arg
- 1];
3289 /* Correct length for explicit IV */
3290 if (len
< EVP_CCM_TLS_EXPLICIT_IV_LEN
)
3292 len
-= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
3293 /* If decrypting correct for tag too */
3294 if (!EVP_CIPHER_CTX_encrypting(c
)) {
3299 EVP_CIPHER_CTX_buf_noconst(c
)[arg
- 2] = len
>> 8;
3300 EVP_CIPHER_CTX_buf_noconst(c
)[arg
- 1] = len
& 0xff;
3302 /* Extra padding: tag appended to record */
3305 case EVP_CTRL_CCM_SET_IV_FIXED
:
3306 /* Sanity check length */
3307 if (arg
!= EVP_CCM_TLS_FIXED_IV_LEN
)
3309 /* Just copy to first part of IV */
3310 memcpy(EVP_CIPHER_CTX_iv_noconst(c
), ptr
, arg
);
3313 case EVP_CTRL_AEAD_SET_IVLEN
:
3316 case EVP_CTRL_CCM_SET_L
:
3317 if (arg
< 2 || arg
> 8)
3322 case EVP_CTRL_AEAD_SET_TAG
:
3323 if ((arg
& 1) || arg
< 4 || arg
> 16)
3325 if (EVP_CIPHER_CTX_encrypting(c
) && ptr
)
3329 memcpy(EVP_CIPHER_CTX_buf_noconst(c
), ptr
, arg
);
3334 case EVP_CTRL_AEAD_GET_TAG
:
3335 if (!EVP_CIPHER_CTX_encrypting(c
) || !cctx
->tag_set
)
3337 if (!CRYPTO_ccm128_tag(&cctx
->ccm
, ptr
, (size_t)arg
))
3346 EVP_CIPHER_CTX
*out
= ptr
;
3347 EVP_AES_CCM_CTX
*cctx_out
= EVP_C_DATA(EVP_AES_CCM_CTX
,out
);
3348 if (cctx
->ccm
.key
) {
3349 if (cctx
->ccm
.key
!= &cctx
->ks
)
3351 cctx_out
->ccm
.key
= &cctx_out
->ks
;
3362 static int aes_ccm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
3363 const unsigned char *iv
, int enc
)
3365 EVP_AES_CCM_CTX
*cctx
= EVP_C_DATA(EVP_AES_CCM_CTX
,ctx
);
3370 #ifdef HWAES_CAPABLE
3371 if (HWAES_CAPABLE
) {
3372 HWAES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3375 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
3376 &cctx
->ks
, (block128_f
) HWAES_encrypt
);
3382 #ifdef VPAES_CAPABLE
3383 if (VPAES_CAPABLE
) {
3384 vpaes_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3386 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
3387 &cctx
->ks
, (block128_f
) vpaes_encrypt
);
3393 AES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3395 CRYPTO_ccm128_init(&cctx
->ccm
, cctx
->M
, cctx
->L
,
3396 &cctx
->ks
, (block128_f
) AES_encrypt
);
3401 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
), iv
, 15 - cctx
->L
);
3407 static int aes_ccm_tls_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
3408 const unsigned char *in
, size_t len
)
3410 EVP_AES_CCM_CTX
*cctx
= EVP_C_DATA(EVP_AES_CCM_CTX
,ctx
);
3411 CCM128_CONTEXT
*ccm
= &cctx
->ccm
;
3412 /* Encrypt/decrypt must be performed in place */
3413 if (out
!= in
|| len
< (EVP_CCM_TLS_EXPLICIT_IV_LEN
+ (size_t)cctx
->M
))
3415 /* If encrypting set explicit IV from sequence number (start of AAD) */
3416 if (EVP_CIPHER_CTX_encrypting(ctx
))
3417 memcpy(out
, EVP_CIPHER_CTX_buf_noconst(ctx
),
3418 EVP_CCM_TLS_EXPLICIT_IV_LEN
);
3419 /* Get rest of IV from explicit IV */
3420 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
) + EVP_CCM_TLS_FIXED_IV_LEN
, in
,
3421 EVP_CCM_TLS_EXPLICIT_IV_LEN
);
3422 /* Correct length value */
3423 len
-= EVP_CCM_TLS_EXPLICIT_IV_LEN
+ cctx
->M
;
3424 if (CRYPTO_ccm128_setiv(ccm
, EVP_CIPHER_CTX_iv_noconst(ctx
), 15 - cctx
->L
,
3428 CRYPTO_ccm128_aad(ccm
, EVP_CIPHER_CTX_buf_noconst(ctx
), cctx
->tls_aad_len
);
3429 /* Fix buffer to point to payload */
3430 in
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
3431 out
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
3432 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
3433 if (cctx
->str
? CRYPTO_ccm128_encrypt_ccm64(ccm
, in
, out
, len
,
3435 CRYPTO_ccm128_encrypt(ccm
, in
, out
, len
))
3437 if (!CRYPTO_ccm128_tag(ccm
, out
+ len
, cctx
->M
))
3439 return len
+ EVP_CCM_TLS_EXPLICIT_IV_LEN
+ cctx
->M
;
3441 if (cctx
->str
? !CRYPTO_ccm128_decrypt_ccm64(ccm
, in
, out
, len
,
3443 !CRYPTO_ccm128_decrypt(ccm
, in
, out
, len
)) {
3444 unsigned char tag
[16];
3445 if (CRYPTO_ccm128_tag(ccm
, tag
, cctx
->M
)) {
3446 if (!CRYPTO_memcmp(tag
, in
+ len
, cctx
->M
))
3450 OPENSSL_cleanse(out
, len
);
3455 static int aes_ccm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
3456 const unsigned char *in
, size_t len
)
3458 EVP_AES_CCM_CTX
*cctx
= EVP_C_DATA(EVP_AES_CCM_CTX
,ctx
);
3459 CCM128_CONTEXT
*ccm
= &cctx
->ccm
;
3460 /* If not set up, return error */
3464 if (cctx
->tls_aad_len
>= 0)
3465 return aes_ccm_tls_cipher(ctx
, out
, in
, len
);
3467 /* EVP_*Final() doesn't return any data */
3468 if (in
== NULL
&& out
!= NULL
)
3476 if (CRYPTO_ccm128_setiv(ccm
, EVP_CIPHER_CTX_iv_noconst(ctx
),
3482 /* If have AAD need message length */
3483 if (!cctx
->len_set
&& len
)
3485 CRYPTO_ccm128_aad(ccm
, in
, len
);
3489 /* The tag must be set before actually decrypting data */
3490 if (!EVP_CIPHER_CTX_encrypting(ctx
) && !cctx
->tag_set
)
3493 /* If not set length yet do it */
3494 if (!cctx
->len_set
) {
3495 if (CRYPTO_ccm128_setiv(ccm
, EVP_CIPHER_CTX_iv_noconst(ctx
),
3500 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
3501 if (cctx
->str
? CRYPTO_ccm128_encrypt_ccm64(ccm
, in
, out
, len
,
3503 CRYPTO_ccm128_encrypt(ccm
, in
, out
, len
))
3509 if (cctx
->str
? !CRYPTO_ccm128_decrypt_ccm64(ccm
, in
, out
, len
,
3511 !CRYPTO_ccm128_decrypt(ccm
, in
, out
, len
)) {
3512 unsigned char tag
[16];
3513 if (CRYPTO_ccm128_tag(ccm
, tag
, cctx
->M
)) {
3514 if (!CRYPTO_memcmp(tag
, EVP_CIPHER_CTX_buf_noconst(ctx
),
3520 OPENSSL_cleanse(out
, len
);
3528 #define aes_ccm_cleanup NULL
3530 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 12, ccm
, CCM
,
3531 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
3532 BLOCK_CIPHER_custom(NID_aes
, 192, 1, 12, ccm
, CCM
,
3533 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
3534 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 12, ccm
, CCM
,
3535 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
3542 /* Indicates if IV has been set */
3546 static int aes_wrap_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
3547 const unsigned char *iv
, int enc
)
3549 EVP_AES_WRAP_CTX
*wctx
= EVP_C_DATA(EVP_AES_WRAP_CTX
,ctx
);
3553 if (EVP_CIPHER_CTX_encrypting(ctx
))
3554 AES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3557 AES_set_decrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3563 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
), iv
, EVP_CIPHER_CTX_iv_length(ctx
));
3564 wctx
->iv
= EVP_CIPHER_CTX_iv_noconst(ctx
);
3569 static int aes_wrap_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
3570 const unsigned char *in
, size_t inlen
)
3572 EVP_AES_WRAP_CTX
*wctx
= EVP_C_DATA(EVP_AES_WRAP_CTX
,ctx
);
3574 /* AES wrap with padding has IV length of 4, without padding 8 */
3575 int pad
= EVP_CIPHER_CTX_iv_length(ctx
) == 4;
3576 /* No final operation so always return zero length */
3579 /* Input length must always be non-zero */
3582 /* If decrypting need at least 16 bytes and multiple of 8 */
3583 if (!EVP_CIPHER_CTX_encrypting(ctx
) && (inlen
< 16 || inlen
& 0x7))
3585 /* If not padding input must be multiple of 8 */
3586 if (!pad
&& inlen
& 0x7)
3588 if (is_partially_overlapping(out
, in
, inlen
)) {
3589 EVPerr(EVP_F_AES_WRAP_CIPHER
, EVP_R_PARTIALLY_OVERLAPPING
);
3593 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
3594 /* If padding round up to multiple of 8 */
3596 inlen
= (inlen
+ 7) / 8 * 8;
3601 * If not padding output will be exactly 8 bytes smaller than
3602 * input. If padding it will be at least 8 bytes smaller but we
3603 * don't know how much.
3609 if (EVP_CIPHER_CTX_encrypting(ctx
))
3610 rv
= CRYPTO_128_wrap_pad(&wctx
->ks
.ks
, wctx
->iv
,
3612 (block128_f
) AES_encrypt
);
3614 rv
= CRYPTO_128_unwrap_pad(&wctx
->ks
.ks
, wctx
->iv
,
3616 (block128_f
) AES_decrypt
);
3618 if (EVP_CIPHER_CTX_encrypting(ctx
))
3619 rv
= CRYPTO_128_wrap(&wctx
->ks
.ks
, wctx
->iv
,
3620 out
, in
, inlen
, (block128_f
) AES_encrypt
);
3622 rv
= CRYPTO_128_unwrap(&wctx
->ks
.ks
, wctx
->iv
,
3623 out
, in
, inlen
, (block128_f
) AES_decrypt
);
3625 return rv
? (int)rv
: -1;
3628 #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
3629 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
3630 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
3632 static const EVP_CIPHER aes_128_wrap
= {
3634 8, 16, 8, WRAP_FLAGS
,
3635 aes_wrap_init_key
, aes_wrap_cipher
,
3637 sizeof(EVP_AES_WRAP_CTX
),
3638 NULL
, NULL
, NULL
, NULL
3641 const EVP_CIPHER
*EVP_aes_128_wrap(void)
3643 return &aes_128_wrap
;
3646 static const EVP_CIPHER aes_192_wrap
= {
3648 8, 24, 8, WRAP_FLAGS
,
3649 aes_wrap_init_key
, aes_wrap_cipher
,
3651 sizeof(EVP_AES_WRAP_CTX
),
3652 NULL
, NULL
, NULL
, NULL
3655 const EVP_CIPHER
*EVP_aes_192_wrap(void)
3657 return &aes_192_wrap
;
3660 static const EVP_CIPHER aes_256_wrap
= {
3662 8, 32, 8, WRAP_FLAGS
,
3663 aes_wrap_init_key
, aes_wrap_cipher
,
3665 sizeof(EVP_AES_WRAP_CTX
),
3666 NULL
, NULL
, NULL
, NULL
3669 const EVP_CIPHER
*EVP_aes_256_wrap(void)
3671 return &aes_256_wrap
;
3674 static const EVP_CIPHER aes_128_wrap_pad
= {
3675 NID_id_aes128_wrap_pad
,
3676 8, 16, 4, WRAP_FLAGS
,
3677 aes_wrap_init_key
, aes_wrap_cipher
,
3679 sizeof(EVP_AES_WRAP_CTX
),
3680 NULL
, NULL
, NULL
, NULL
3683 const EVP_CIPHER
*EVP_aes_128_wrap_pad(void)
3685 return &aes_128_wrap_pad
;
3688 static const EVP_CIPHER aes_192_wrap_pad
= {
3689 NID_id_aes192_wrap_pad
,
3690 8, 24, 4, WRAP_FLAGS
,
3691 aes_wrap_init_key
, aes_wrap_cipher
,
3693 sizeof(EVP_AES_WRAP_CTX
),
3694 NULL
, NULL
, NULL
, NULL
3697 const EVP_CIPHER
*EVP_aes_192_wrap_pad(void)
3699 return &aes_192_wrap_pad
;
3702 static const EVP_CIPHER aes_256_wrap_pad
= {
3703 NID_id_aes256_wrap_pad
,
3704 8, 32, 4, WRAP_FLAGS
,
3705 aes_wrap_init_key
, aes_wrap_cipher
,
3707 sizeof(EVP_AES_WRAP_CTX
),
3708 NULL
, NULL
, NULL
, NULL
3711 const EVP_CIPHER
*EVP_aes_256_wrap_pad(void)
3713 return &aes_256_wrap_pad
;
3716 #ifndef OPENSSL_NO_OCB
3717 static int aes_ocb_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
3719 EVP_AES_OCB_CTX
*octx
= EVP_C_DATA(EVP_AES_OCB_CTX
,c
);
3720 EVP_CIPHER_CTX
*newc
;
3721 EVP_AES_OCB_CTX
*new_octx
;
3727 octx
->ivlen
= EVP_CIPHER_iv_length(c
->cipher
);
3728 octx
->iv
= EVP_CIPHER_CTX_iv_noconst(c
);
3730 octx
->data_buf_len
= 0;
3731 octx
->aad_buf_len
= 0;
3734 case EVP_CTRL_GET_IVLEN
:
3735 *(int *)ptr
= octx
->ivlen
;
3738 case EVP_CTRL_AEAD_SET_IVLEN
:
3739 /* IV len must be 1 to 15 */
3740 if (arg
<= 0 || arg
> 15)
3746 case EVP_CTRL_AEAD_SET_TAG
:
3748 /* Tag len must be 0 to 16 */
3749 if (arg
< 0 || arg
> 16)
3755 if (arg
!= octx
->taglen
|| EVP_CIPHER_CTX_encrypting(c
))
3757 memcpy(octx
->tag
, ptr
, arg
);
3760 case EVP_CTRL_AEAD_GET_TAG
:
3761 if (arg
!= octx
->taglen
|| !EVP_CIPHER_CTX_encrypting(c
))
3764 memcpy(ptr
, octx
->tag
, arg
);
3768 newc
= (EVP_CIPHER_CTX
*)ptr
;
3769 new_octx
= EVP_C_DATA(EVP_AES_OCB_CTX
,newc
);
3770 return CRYPTO_ocb128_copy_ctx(&new_octx
->ocb
, &octx
->ocb
,
3771 &new_octx
->ksenc
.ks
,
3772 &new_octx
->ksdec
.ks
);
3780 static int aes_ocb_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
3781 const unsigned char *iv
, int enc
)
3783 EVP_AES_OCB_CTX
*octx
= EVP_C_DATA(EVP_AES_OCB_CTX
,ctx
);
3789 * We set both the encrypt and decrypt key here because decrypt
3790 * needs both. We could possibly optimise to remove setting the
3791 * decrypt for an encryption operation.
3793 # ifdef HWAES_CAPABLE
3794 if (HWAES_CAPABLE
) {
3795 HWAES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3797 HWAES_set_decrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3799 if (!CRYPTO_ocb128_init(&octx
->ocb
,
3800 &octx
->ksenc
.ks
, &octx
->ksdec
.ks
,
3801 (block128_f
) HWAES_encrypt
,
3802 (block128_f
) HWAES_decrypt
,
3803 enc
? HWAES_ocb_encrypt
3804 : HWAES_ocb_decrypt
))
3809 # ifdef VPAES_CAPABLE
3810 if (VPAES_CAPABLE
) {
3811 vpaes_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3813 vpaes_set_decrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3815 if (!CRYPTO_ocb128_init(&octx
->ocb
,
3816 &octx
->ksenc
.ks
, &octx
->ksdec
.ks
,
3817 (block128_f
) vpaes_encrypt
,
3818 (block128_f
) vpaes_decrypt
,
3824 AES_set_encrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3826 AES_set_decrypt_key(key
, EVP_CIPHER_CTX_key_length(ctx
) * 8,
3828 if (!CRYPTO_ocb128_init(&octx
->ocb
,
3829 &octx
->ksenc
.ks
, &octx
->ksdec
.ks
,
3830 (block128_f
) AES_encrypt
,
3831 (block128_f
) AES_decrypt
,
3838 * If we have an iv we can set it directly, otherwise use saved IV.
3840 if (iv
== NULL
&& octx
->iv_set
)
3843 if (CRYPTO_ocb128_setiv(&octx
->ocb
, iv
, octx
->ivlen
, octx
->taglen
)
3850 /* If key set use IV, otherwise copy */
3852 CRYPTO_ocb128_setiv(&octx
->ocb
, iv
, octx
->ivlen
, octx
->taglen
);
3854 memcpy(octx
->iv
, iv
, octx
->ivlen
);
3860 static int aes_ocb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
3861 const unsigned char *in
, size_t len
)
3865 int written_len
= 0;
3866 size_t trailing_len
;
3867 EVP_AES_OCB_CTX
*octx
= EVP_C_DATA(EVP_AES_OCB_CTX
,ctx
);
3869 /* If IV or Key not set then return error */
3878 * Need to ensure we are only passing full blocks to low level OCB
3879 * routines. We do it here rather than in EVP_EncryptUpdate/
3880 * EVP_DecryptUpdate because we need to pass full blocks of AAD too
3881 * and those routines don't support that
3884 /* Are we dealing with AAD or normal data here? */
3886 buf
= octx
->aad_buf
;
3887 buf_len
= &(octx
->aad_buf_len
);
3889 buf
= octx
->data_buf
;
3890 buf_len
= &(octx
->data_buf_len
);
3892 if (is_partially_overlapping(out
+ *buf_len
, in
, len
)) {
3893 EVPerr(EVP_F_AES_OCB_CIPHER
, EVP_R_PARTIALLY_OVERLAPPING
);
3899 * If we've got a partially filled buffer from a previous call then
3900 * use that data first
3903 unsigned int remaining
;
3905 remaining
= AES_BLOCK_SIZE
- (*buf_len
);
3906 if (remaining
> len
) {
3907 memcpy(buf
+ (*buf_len
), in
, len
);
3911 memcpy(buf
+ (*buf_len
), in
, remaining
);
3914 * If we get here we've filled the buffer, so process it
3919 if (!CRYPTO_ocb128_aad(&octx
->ocb
, buf
, AES_BLOCK_SIZE
))
3921 } else if (EVP_CIPHER_CTX_encrypting(ctx
)) {
3922 if (!CRYPTO_ocb128_encrypt(&octx
->ocb
, buf
, out
,
3926 if (!CRYPTO_ocb128_decrypt(&octx
->ocb
, buf
, out
,
3930 written_len
= AES_BLOCK_SIZE
;
3933 out
+= AES_BLOCK_SIZE
;
3936 /* Do we have a partial block to handle at the end? */
3937 trailing_len
= len
% AES_BLOCK_SIZE
;
3940 * If we've got some full blocks to handle, then process these first
3942 if (len
!= trailing_len
) {
3944 if (!CRYPTO_ocb128_aad(&octx
->ocb
, in
, len
- trailing_len
))
3946 } else if (EVP_CIPHER_CTX_encrypting(ctx
)) {
3947 if (!CRYPTO_ocb128_encrypt
3948 (&octx
->ocb
, in
, out
, len
- trailing_len
))
3951 if (!CRYPTO_ocb128_decrypt
3952 (&octx
->ocb
, in
, out
, len
- trailing_len
))
3955 written_len
+= len
- trailing_len
;
3956 in
+= len
- trailing_len
;
3959 /* Handle any trailing partial block */
3960 if (trailing_len
> 0) {
3961 memcpy(buf
, in
, trailing_len
);
3962 *buf_len
= trailing_len
;
3968 * First of all empty the buffer of any partial block that we might
3969 * have been provided - both for data and AAD
3971 if (octx
->data_buf_len
> 0) {
3972 if (EVP_CIPHER_CTX_encrypting(ctx
)) {
3973 if (!CRYPTO_ocb128_encrypt(&octx
->ocb
, octx
->data_buf
, out
,
3974 octx
->data_buf_len
))
3977 if (!CRYPTO_ocb128_decrypt(&octx
->ocb
, octx
->data_buf
, out
,
3978 octx
->data_buf_len
))
3981 written_len
= octx
->data_buf_len
;
3982 octx
->data_buf_len
= 0;
3984 if (octx
->aad_buf_len
> 0) {
3985 if (!CRYPTO_ocb128_aad
3986 (&octx
->ocb
, octx
->aad_buf
, octx
->aad_buf_len
))
3988 octx
->aad_buf_len
= 0;
3990 /* If decrypting then verify */
3991 if (!EVP_CIPHER_CTX_encrypting(ctx
)) {
3992 if (octx
->taglen
< 0)
3994 if (CRYPTO_ocb128_finish(&octx
->ocb
,
3995 octx
->tag
, octx
->taglen
) != 0)
4000 /* If encrypting then just get the tag */
4001 if (CRYPTO_ocb128_tag(&octx
->ocb
, octx
->tag
, 16) != 1)
4003 /* Don't reuse the IV */
4009 static int aes_ocb_cleanup(EVP_CIPHER_CTX
*c
)
4011 EVP_AES_OCB_CTX
*octx
= EVP_C_DATA(EVP_AES_OCB_CTX
,c
);
4012 CRYPTO_ocb128_cleanup(&octx
->ocb
);
4016 BLOCK_CIPHER_custom(NID_aes
, 128, 16, 12, ocb
, OCB
,
4017 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
4018 BLOCK_CIPHER_custom(NID_aes
, 192, 16, 12, ocb
, OCB
,
4019 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
4020 BLOCK_CIPHER_custom(NID_aes
, 256, 16, 12, ocb
, OCB
,
4021 EVP_CIPH_FLAG_AEAD_CIPHER
| CUSTOM_FLAGS
)
4022 #endif /* OPENSSL_NO_OCB */
4025 #ifndef OPENSSL_NO_SIV
4027 typedef SIV128_CONTEXT EVP_AES_SIV_CTX
;
4029 #define aesni_siv_init_key aes_siv_init_key
4030 static int aes_siv_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
4031 const unsigned char *iv
, int enc
)
4033 const EVP_CIPHER
*ctr
;
4034 const EVP_CIPHER
*cbc
;
4035 SIV128_CONTEXT
*sctx
= EVP_C_DATA(SIV128_CONTEXT
, ctx
);
4036 int klen
= EVP_CIPHER_CTX_key_length(ctx
) / 2;
4043 cbc
= EVP_aes_128_cbc();
4044 ctr
= EVP_aes_128_ctr();
4047 cbc
= EVP_aes_192_cbc();
4048 ctr
= EVP_aes_192_ctr();
4051 cbc
= EVP_aes_256_cbc();
4052 ctr
= EVP_aes_256_ctr();
4058 /* klen is the length of the underlying cipher, not the input key,
4059 which should be twice as long */
4060 return CRYPTO_siv128_init(sctx
, key
, klen
, cbc
, ctr
);
4063 #define aesni_siv_cipher aes_siv_cipher
4064 static int aes_siv_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
4065 const unsigned char *in
, size_t len
)
4067 SIV128_CONTEXT
*sctx
= EVP_C_DATA(SIV128_CONTEXT
, ctx
);
4069 /* EncryptFinal or DecryptFinal */
4071 return CRYPTO_siv128_finish(sctx
);
4073 /* Deal with associated data */
4075 return CRYPTO_siv128_aad(sctx
, in
, len
);
4077 if (EVP_CIPHER_CTX_encrypting(ctx
))
4078 return CRYPTO_siv128_encrypt(sctx
, in
, out
, len
);
4080 return CRYPTO_siv128_decrypt(sctx
, in
, out
, len
);
4083 #define aesni_siv_cleanup aes_siv_cleanup
4084 static int aes_siv_cleanup(EVP_CIPHER_CTX
*c
)
4086 SIV128_CONTEXT
*sctx
= EVP_C_DATA(SIV128_CONTEXT
, c
);
4088 return CRYPTO_siv128_cleanup(sctx
);
4092 #define aesni_siv_ctrl aes_siv_ctrl
4093 static int aes_siv_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
4095 SIV128_CONTEXT
*sctx
= EVP_C_DATA(SIV128_CONTEXT
, c
);
4096 SIV128_CONTEXT
*sctx_out
;
4100 return CRYPTO_siv128_cleanup(sctx
);
4102 case EVP_CTRL_SET_SPEED
:
4103 return CRYPTO_siv128_speed(sctx
, arg
);
4105 case EVP_CTRL_AEAD_SET_TAG
:
4106 if (!EVP_CIPHER_CTX_encrypting(c
))
4107 return CRYPTO_siv128_set_tag(sctx
, ptr
, arg
);
4110 case EVP_CTRL_AEAD_GET_TAG
:
4111 if (!EVP_CIPHER_CTX_encrypting(c
))
4113 return CRYPTO_siv128_get_tag(sctx
, ptr
, arg
);
4116 sctx_out
= EVP_C_DATA(SIV128_CONTEXT
, (EVP_CIPHER_CTX
*)ptr
);
4117 return CRYPTO_siv128_copy_ctx(sctx_out
, sctx
);
4125 #define SIV_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1 \
4126 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
4127 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_COPY \
4128 | EVP_CIPH_CTRL_INIT)
4130 BLOCK_CIPHER_custom(NID_aes
, 128, 1, 0, siv
, SIV
, SIV_FLAGS
)
4131 BLOCK_CIPHER_custom(NID_aes
, 192, 1, 0, siv
, SIV
, SIV_FLAGS
)
4132 BLOCK_CIPHER_custom(NID_aes
, 256, 1, 0, siv
, SIV
, SIV_FLAGS
)