]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
3c7d0945 | 2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. |
deb2c1a1 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
deb2c1a1 DSH |
8 | */ |
9 | ||
8c84b677 | 10 | #include <openssl/opensslconf.h> |
5158c763 MC |
11 | #include <openssl/crypto.h> |
12 | #include <openssl/evp.h> | |
13 | #include <openssl/err.h> | |
14 | #include <string.h> | |
15 | #include <assert.h> | |
16 | #include <openssl/aes.h> | |
17 | #include "internal/evp_int.h" | |
18 | #include "modes_lcl.h" | |
19 | #include <openssl/rand.h> | |
b1ceb439 | 20 | #include <openssl/cmac.h> |
7141ba31 | 21 | #include "evp_locl.h" |
0f113f3e MC |
22 | |
23 | typedef struct { | |
24 | union { | |
25 | double align; | |
26 | AES_KEY ks; | |
27 | } ks; | |
28 | block128_f block; | |
29 | union { | |
30 | cbc128_f cbc; | |
31 | ctr128_f ctr; | |
32 | } stream; | |
33 | } EVP_AES_KEY; | |
34 | ||
35 | typedef struct { | |
36 | union { | |
37 | double align; | |
38 | AES_KEY ks; | |
39 | } ks; /* AES key schedule to use */ | |
40 | int key_set; /* Set if key initialised */ | |
41 | int iv_set; /* Set if an iv is set */ | |
42 | GCM128_CONTEXT gcm; | |
43 | unsigned char *iv; /* Temporary IV store */ | |
44 | int ivlen; /* IV length */ | |
45 | int taglen; | |
46 | int iv_gen; /* It is OK to generate IVs */ | |
47 | int tls_aad_len; /* TLS AAD length */ | |
d6b34570 | 48 | uint64_t tls_enc_records; /* Number of TLS records encrypted */ |
0f113f3e MC |
49 | ctr128_f ctr; |
50 | } EVP_AES_GCM_CTX; | |
51 | ||
52 | typedef struct { | |
53 | union { | |
54 | double align; | |
55 | AES_KEY ks; | |
56 | } ks1, ks2; /* AES key schedules to use */ | |
57 | XTS128_CONTEXT xts; | |
58 | void (*stream) (const unsigned char *in, | |
59 | unsigned char *out, size_t length, | |
60 | const AES_KEY *key1, const AES_KEY *key2, | |
61 | const unsigned char iv[16]); | |
62 | } EVP_AES_XTS_CTX; | |
63 | ||
64 | typedef struct { | |
65 | union { | |
66 | double align; | |
67 | AES_KEY ks; | |
68 | } ks; /* AES key schedule to use */ | |
69 | int key_set; /* Set if key initialised */ | |
70 | int iv_set; /* Set if an iv is set */ | |
71 | int tag_set; /* Set if tag is valid */ | |
72 | int len_set; /* Set if message length set */ | |
73 | int L, M; /* L and M parameters from RFC3610 */ | |
e75c5a79 | 74 | int tls_aad_len; /* TLS AAD length */ |
0f113f3e MC |
75 | CCM128_CONTEXT ccm; |
76 | ccm128_f str; | |
77 | } EVP_AES_CCM_CTX; | |
78 | ||
5158c763 | 79 | #ifndef OPENSSL_NO_OCB |
0f113f3e | 80 | typedef struct { |
bdc985b1 AP |
81 | union { |
82 | double align; | |
83 | AES_KEY ks; | |
84 | } ksenc; /* AES key schedule to use for encryption */ | |
85 | union { | |
86 | double align; | |
87 | AES_KEY ks; | |
88 | } ksdec; /* AES key schedule to use for decryption */ | |
0f113f3e MC |
89 | int key_set; /* Set if key initialised */ |
90 | int iv_set; /* Set if an iv is set */ | |
91 | OCB128_CONTEXT ocb; | |
92 | unsigned char *iv; /* Temporary IV store */ | |
93 | unsigned char tag[16]; | |
94 | unsigned char data_buf[16]; /* Store partial data blocks */ | |
95 | unsigned char aad_buf[16]; /* Store partial AAD blocks */ | |
96 | int data_buf_len; | |
97 | int aad_buf_len; | |
98 | int ivlen; /* IV length */ | |
99 | int taglen; | |
100 | } EVP_AES_OCB_CTX; | |
5158c763 | 101 | #endif |
e6b336ef | 102 | |
5158c763 | 103 | #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) |
17f121de | 104 | |
5158c763 | 105 | #ifdef VPAES_ASM |
8ca28da0 | 106 | int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, |
0f113f3e | 107 | AES_KEY *key); |
8ca28da0 | 108 | int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, |
0f113f3e | 109 | AES_KEY *key); |
8ca28da0 AP |
110 | |
111 | void vpaes_encrypt(const unsigned char *in, unsigned char *out, | |
0f113f3e | 112 | const AES_KEY *key); |
8ca28da0 | 113 | void vpaes_decrypt(const unsigned char *in, unsigned char *out, |
0f113f3e | 114 | const AES_KEY *key); |
8ca28da0 AP |
115 | |
116 | void vpaes_cbc_encrypt(const unsigned char *in, | |
0f113f3e MC |
117 | unsigned char *out, |
118 | size_t length, | |
119 | const AES_KEY *key, unsigned char *ivec, int enc); | |
5158c763 MC |
120 | #endif |
121 | #ifdef BSAES_ASM | |
a75a52a4 | 122 | void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, |
0f113f3e MC |
123 | size_t length, const AES_KEY *key, |
124 | unsigned char ivec[16], int enc); | |
993adc05 | 125 | void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
0f113f3e MC |
126 | size_t len, const AES_KEY *key, |
127 | const unsigned char ivec[16]); | |
60d4e99c | 128 | void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, |
0f113f3e MC |
129 | size_t len, const AES_KEY *key1, |
130 | const AES_KEY *key2, const unsigned char iv[16]); | |
60d4e99c | 131 | void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, |
0f113f3e MC |
132 | size_t len, const AES_KEY *key1, |
133 | const AES_KEY *key2, const unsigned char iv[16]); | |
5158c763 MC |
134 | #endif |
135 | #ifdef AES_CTR_ASM | |
07904e0c | 136 | void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, |
0f113f3e MC |
137 | size_t blocks, const AES_KEY *key, |
138 | const unsigned char ivec[AES_BLOCK_SIZE]); | |
5158c763 MC |
139 | #endif |
140 | #ifdef AES_XTS_ASM | |
96cce820 | 141 | void AES_xts_encrypt(const unsigned char *inp, unsigned char *out, size_t len, |
0f113f3e MC |
142 | const AES_KEY *key1, const AES_KEY *key2, |
143 | const unsigned char iv[16]); | |
96cce820 | 144 | void AES_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len, |
0f113f3e MC |
145 | const AES_KEY *key1, const AES_KEY *key2, |
146 | const unsigned char iv[16]); | |
5158c763 | 147 | #endif |
8ca28da0 | 148 | |
03a5e5ae PS |
149 | /* increment counter (64-bit int) by 1 */ |
150 | static void ctr64_inc(unsigned char *counter) | |
151 | { | |
152 | int n = 8; | |
153 | unsigned char c; | |
154 | ||
155 | do { | |
156 | --n; | |
157 | c = counter[n]; | |
158 | ++c; | |
159 | counter[n] = c; | |
160 | if (c) | |
161 | return; | |
162 | } while (n); | |
163 | } | |
164 | ||
6944565b | 165 | #if defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC)) |
5158c763 MC |
166 | # include "ppc_arch.h" |
167 | # ifdef VPAES_ASM | |
168 | # define VPAES_CAPABLE (OPENSSL_ppccap_P & PPC_ALTIVEC) | |
de51e830 | 169 | # endif |
5158c763 MC |
170 | # define HWAES_CAPABLE (OPENSSL_ppccap_P & PPC_CRYPTO207) |
171 | # define HWAES_set_encrypt_key aes_p8_set_encrypt_key | |
172 | # define HWAES_set_decrypt_key aes_p8_set_decrypt_key | |
173 | # define HWAES_encrypt aes_p8_encrypt | |
174 | # define HWAES_decrypt aes_p8_decrypt | |
175 | # define HWAES_cbc_encrypt aes_p8_cbc_encrypt | |
176 | # define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks | |
46f047d7 AP |
177 | # define HWAES_xts_encrypt aes_p8_xts_encrypt |
178 | # define HWAES_xts_decrypt aes_p8_xts_decrypt | |
5158c763 | 179 | #endif |
07f3e4f3 | 180 | |
5158c763 | 181 | #if defined(AES_ASM) && !defined(I386_ONLY) && ( \ |
0f113f3e MC |
182 | ((defined(__i386) || defined(__i386__) || \ |
183 | defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \ | |
184 | defined(__x86_64) || defined(__x86_64__) || \ | |
b1a07c38 | 185 | defined(_M_AMD64) || defined(_M_X64) ) |
8ca28da0 | 186 | |
c5f6da54 | 187 | extern unsigned int OPENSSL_ia32cap_P[]; |
8ca28da0 | 188 | |
5158c763 MC |
189 | # ifdef VPAES_ASM |
190 | # define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32))) | |
191 | # endif | |
192 | # ifdef BSAES_ASM | |
193 | # define BSAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32))) | |
194 | # endif | |
17f121de AP |
195 | /* |
196 | * AES-NI section | |
197 | */ | |
5158c763 | 198 | # define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) |
d1fff483 AP |
199 | |
200 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, | |
0f113f3e | 201 | AES_KEY *key); |
d1fff483 | 202 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, |
0f113f3e | 203 | AES_KEY *key); |
d1fff483 AP |
204 | |
205 | void aesni_encrypt(const unsigned char *in, unsigned char *out, | |
0f113f3e | 206 | const AES_KEY *key); |
d1fff483 | 207 | void aesni_decrypt(const unsigned char *in, unsigned char *out, |
0f113f3e | 208 | const AES_KEY *key); |
d1fff483 AP |
209 | |
210 | void aesni_ecb_encrypt(const unsigned char *in, | |
0f113f3e MC |
211 | unsigned char *out, |
212 | size_t length, const AES_KEY *key, int enc); | |
d1fff483 | 213 | void aesni_cbc_encrypt(const unsigned char *in, |
0f113f3e MC |
214 | unsigned char *out, |
215 | size_t length, | |
216 | const AES_KEY *key, unsigned char *ivec, int enc); | |
d1fff483 AP |
217 | |
218 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, | |
0f113f3e MC |
219 | unsigned char *out, |
220 | size_t blocks, | |
221 | const void *key, const unsigned char *ivec); | |
17f121de AP |
222 | |
223 | void aesni_xts_encrypt(const unsigned char *in, | |
0f113f3e MC |
224 | unsigned char *out, |
225 | size_t length, | |
226 | const AES_KEY *key1, const AES_KEY *key2, | |
227 | const unsigned char iv[16]); | |
17f121de AP |
228 | |
229 | void aesni_xts_decrypt(const unsigned char *in, | |
0f113f3e MC |
230 | unsigned char *out, |
231 | size_t length, | |
232 | const AES_KEY *key1, const AES_KEY *key2, | |
233 | const unsigned char iv[16]); | |
234 | ||
235 | void aesni_ccm64_encrypt_blocks(const unsigned char *in, | |
236 | unsigned char *out, | |
237 | size_t blocks, | |
238 | const void *key, | |
239 | const unsigned char ivec[16], | |
240 | unsigned char cmac[16]); | |
241 | ||
242 | void aesni_ccm64_decrypt_blocks(const unsigned char *in, | |
243 | unsigned char *out, | |
244 | size_t blocks, | |
245 | const void *key, | |
246 | const unsigned char ivec[16], | |
247 | unsigned char cmac[16]); | |
248 | ||
5158c763 | 249 | # if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) |
4e049c52 | 250 | size_t aesni_gcm_encrypt(const unsigned char *in, |
0f113f3e MC |
251 | unsigned char *out, |
252 | size_t len, | |
253 | const void *key, unsigned char ivec[16], u64 *Xi); | |
5158c763 | 254 | # define AES_gcm_encrypt aesni_gcm_encrypt |
4e049c52 | 255 | size_t aesni_gcm_decrypt(const unsigned char *in, |
0f113f3e MC |
256 | unsigned char *out, |
257 | size_t len, | |
258 | const void *key, unsigned char ivec[16], u64 *Xi); | |
5158c763 | 259 | # define AES_gcm_decrypt aesni_gcm_decrypt |
0f113f3e MC |
260 | void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *in, |
261 | size_t len); | |
5158c763 | 262 | # define AES_GCM_ASM(gctx) (gctx->ctr==aesni_ctr32_encrypt_blocks && \ |
0f113f3e | 263 | gctx->gcm.ghash==gcm_ghash_avx) |
5158c763 | 264 | # define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \ |
0f113f3e | 265 | gctx->gcm.ghash==gcm_ghash_avx) |
5158c763 MC |
266 | # undef AES_GCM_ASM2 /* minor size optimization */ |
267 | # endif | |
4e049c52 | 268 | |
17f121de | 269 | static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
270 | const unsigned char *iv, int enc) |
271 | { | |
272 | int ret, mode; | |
6435f0f6 | 273 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
0f113f3e | 274 | |
6435f0f6 | 275 | mode = EVP_CIPHER_CTX_mode(ctx); |
0f113f3e MC |
276 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) |
277 | && !enc) { | |
6435f0f6 RL |
278 | ret = aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
279 | &dat->ks.ks); | |
0f113f3e MC |
280 | dat->block = (block128_f) aesni_decrypt; |
281 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
282 | (cbc128_f) aesni_cbc_encrypt : NULL; | |
283 | } else { | |
6435f0f6 RL |
284 | ret = aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
285 | &dat->ks.ks); | |
0f113f3e MC |
286 | dat->block = (block128_f) aesni_encrypt; |
287 | if (mode == EVP_CIPH_CBC_MODE) | |
288 | dat->stream.cbc = (cbc128_f) aesni_cbc_encrypt; | |
289 | else if (mode == EVP_CIPH_CTR_MODE) | |
290 | dat->stream.ctr = (ctr128_f) aesni_ctr32_encrypt_blocks; | |
291 | else | |
292 | dat->stream.cbc = NULL; | |
293 | } | |
294 | ||
295 | if (ret < 0) { | |
296 | EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); | |
297 | return 0; | |
298 | } | |
299 | ||
300 | return 1; | |
301 | } | |
302 | ||
303 | static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
304 | const unsigned char *in, size_t len) | |
d1fff483 | 305 | { |
6435f0f6 RL |
306 | aesni_cbc_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks, |
307 | EVP_CIPHER_CTX_iv_noconst(ctx), | |
308 | EVP_CIPHER_CTX_encrypting(ctx)); | |
d1fff483 | 309 | |
0f113f3e | 310 | return 1; |
d1fff483 AP |
311 | } |
312 | ||
0f113f3e MC |
313 | static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
314 | const unsigned char *in, size_t len) | |
d1fff483 | 315 | { |
6435f0f6 | 316 | size_t bl = EVP_CIPHER_CTX_block_size(ctx); |
d1fff483 | 317 | |
0f113f3e MC |
318 | if (len < bl) |
319 | return 1; | |
d1fff483 | 320 | |
6435f0f6 RL |
321 | aesni_ecb_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks, |
322 | EVP_CIPHER_CTX_encrypting(ctx)); | |
d1fff483 | 323 | |
0f113f3e | 324 | return 1; |
d1fff483 AP |
325 | } |
326 | ||
5158c763 | 327 | # define aesni_ofb_cipher aes_ofb_cipher |
0f113f3e MC |
328 | static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
329 | const unsigned char *in, size_t len); | |
d1fff483 | 330 | |
5158c763 | 331 | # define aesni_cfb_cipher aes_cfb_cipher |
0f113f3e MC |
332 | static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
333 | const unsigned char *in, size_t len); | |
d1fff483 | 334 | |
5158c763 | 335 | # define aesni_cfb8_cipher aes_cfb8_cipher |
0f113f3e MC |
336 | static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
337 | const unsigned char *in, size_t len); | |
d1fff483 | 338 | |
5158c763 | 339 | # define aesni_cfb1_cipher aes_cfb1_cipher |
0f113f3e MC |
340 | static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
341 | const unsigned char *in, size_t len); | |
d1fff483 | 342 | |
5158c763 | 343 | # define aesni_ctr_cipher aes_ctr_cipher |
17f121de | 344 | static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 345 | const unsigned char *in, size_t len); |
d1fff483 | 346 | |
17f121de | 347 | static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
348 | const unsigned char *iv, int enc) |
349 | { | |
6435f0f6 | 350 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx); |
0f113f3e MC |
351 | if (!iv && !key) |
352 | return 1; | |
353 | if (key) { | |
6435f0f6 RL |
354 | aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
355 | &gctx->ks.ks); | |
0f113f3e MC |
356 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f) aesni_encrypt); |
357 | gctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks; | |
358 | /* | |
359 | * If we have an iv can set it directly, otherwise use saved IV. | |
360 | */ | |
361 | if (iv == NULL && gctx->iv_set) | |
362 | iv = gctx->iv; | |
363 | if (iv) { | |
364 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | |
365 | gctx->iv_set = 1; | |
366 | } | |
367 | gctx->key_set = 1; | |
368 | } else { | |
369 | /* If key set use IV, otherwise copy */ | |
370 | if (gctx->key_set) | |
371 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | |
372 | else | |
373 | memcpy(gctx->iv, iv, gctx->ivlen); | |
374 | gctx->iv_set = 1; | |
375 | gctx->iv_gen = 0; | |
376 | } | |
377 | return 1; | |
378 | } | |
379 | ||
5158c763 | 380 | # define aesni_gcm_cipher aes_gcm_cipher |
17f121de | 381 | static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 382 | const unsigned char *in, size_t len); |
17f121de AP |
383 | |
384 | static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
385 | const unsigned char *iv, int enc) |
386 | { | |
6435f0f6 | 387 | EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx); |
0f113f3e MC |
388 | if (!iv && !key) |
389 | return 1; | |
390 | ||
391 | if (key) { | |
392 | /* key_len is two AES keys */ | |
393 | if (enc) { | |
6435f0f6 RL |
394 | aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4, |
395 | &xctx->ks1.ks); | |
0f113f3e MC |
396 | xctx->xts.block1 = (block128_f) aesni_encrypt; |
397 | xctx->stream = aesni_xts_encrypt; | |
398 | } else { | |
6435f0f6 RL |
399 | aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4, |
400 | &xctx->ks1.ks); | |
0f113f3e MC |
401 | xctx->xts.block1 = (block128_f) aesni_decrypt; |
402 | xctx->stream = aesni_xts_decrypt; | |
403 | } | |
404 | ||
6435f0f6 RL |
405 | aesni_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2, |
406 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
407 | &xctx->ks2.ks); | |
0f113f3e MC |
408 | xctx->xts.block2 = (block128_f) aesni_encrypt; |
409 | ||
410 | xctx->xts.key1 = &xctx->ks1; | |
411 | } | |
412 | ||
413 | if (iv) { | |
414 | xctx->xts.key2 = &xctx->ks2; | |
6435f0f6 | 415 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16); |
0f113f3e MC |
416 | } |
417 | ||
418 | return 1; | |
419 | } | |
420 | ||
5158c763 | 421 | # define aesni_xts_cipher aes_xts_cipher |
17f121de | 422 | static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 423 | const unsigned char *in, size_t len); |
17f121de AP |
424 | |
425 | static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
426 | const unsigned char *iv, int enc) |
427 | { | |
6435f0f6 | 428 | EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx); |
0f113f3e MC |
429 | if (!iv && !key) |
430 | return 1; | |
431 | if (key) { | |
6435f0f6 RL |
432 | aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
433 | &cctx->ks.ks); | |
0f113f3e MC |
434 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
435 | &cctx->ks, (block128_f) aesni_encrypt); | |
436 | cctx->str = enc ? (ccm128_f) aesni_ccm64_encrypt_blocks : | |
437 | (ccm128_f) aesni_ccm64_decrypt_blocks; | |
438 | cctx->key_set = 1; | |
439 | } | |
440 | if (iv) { | |
6435f0f6 | 441 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L); |
0f113f3e MC |
442 | cctx->iv_set = 1; |
443 | } | |
444 | return 1; | |
445 | } | |
446 | ||
5158c763 | 447 | # define aesni_ccm_cipher aes_ccm_cipher |
17f121de | 448 | static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 449 | const unsigned char *in, size_t len); |
17f121de | 450 | |
5158c763 | 451 | # ifndef OPENSSL_NO_OCB |
bd30091c AP |
452 | void aesni_ocb_encrypt(const unsigned char *in, unsigned char *out, |
453 | size_t blocks, const void *key, | |
454 | size_t start_block_num, | |
455 | unsigned char offset_i[16], | |
456 | const unsigned char L_[][16], | |
457 | unsigned char checksum[16]); | |
458 | void aesni_ocb_decrypt(const unsigned char *in, unsigned char *out, | |
459 | size_t blocks, const void *key, | |
460 | size_t start_block_num, | |
461 | unsigned char offset_i[16], | |
462 | const unsigned char L_[][16], | |
463 | unsigned char checksum[16]); | |
464 | ||
e6b336ef | 465 | static int aesni_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
466 | const unsigned char *iv, int enc) |
467 | { | |
6435f0f6 | 468 | EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx); |
0f113f3e MC |
469 | if (!iv && !key) |
470 | return 1; | |
471 | if (key) { | |
472 | do { | |
473 | /* | |
474 | * We set both the encrypt and decrypt key here because decrypt | |
475 | * needs both. We could possibly optimise to remove setting the | |
476 | * decrypt for an encryption operation. | |
477 | */ | |
6435f0f6 RL |
478 | aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
479 | &octx->ksenc.ks); | |
480 | aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, | |
481 | &octx->ksdec.ks); | |
bdc985b1 AP |
482 | if (!CRYPTO_ocb128_init(&octx->ocb, |
483 | &octx->ksenc.ks, &octx->ksdec.ks, | |
0f113f3e | 484 | (block128_f) aesni_encrypt, |
bd30091c AP |
485 | (block128_f) aesni_decrypt, |
486 | enc ? aesni_ocb_encrypt | |
487 | : aesni_ocb_decrypt)) | |
0f113f3e MC |
488 | return 0; |
489 | } | |
490 | while (0); | |
491 | ||
492 | /* | |
493 | * If we have an iv we can set it directly, otherwise use saved IV. | |
494 | */ | |
495 | if (iv == NULL && octx->iv_set) | |
496 | iv = octx->iv; | |
497 | if (iv) { | |
498 | if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen) | |
499 | != 1) | |
500 | return 0; | |
501 | octx->iv_set = 1; | |
502 | } | |
503 | octx->key_set = 1; | |
504 | } else { | |
505 | /* If key set use IV, otherwise copy */ | |
506 | if (octx->key_set) | |
507 | CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen); | |
508 | else | |
509 | memcpy(octx->iv, iv, octx->ivlen); | |
510 | octx->iv_set = 1; | |
511 | } | |
512 | return 1; | |
513 | } | |
514 | ||
5158c763 | 515 | # define aesni_ocb_cipher aes_ocb_cipher |
e6b336ef | 516 | static int aesni_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 517 | const unsigned char *in, size_t len); |
5158c763 | 518 | # endif /* OPENSSL_NO_OCB */ |
e6b336ef | 519 | |
5158c763 | 520 | # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ |
17f121de | 521 | static const EVP_CIPHER aesni_##keylen##_##mode = { \ |
0f113f3e MC |
522 | nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \ |
523 | flags|EVP_CIPH_##MODE##_MODE, \ | |
524 | aesni_init_key, \ | |
525 | aesni_##mode##_cipher, \ | |
526 | NULL, \ | |
527 | sizeof(EVP_AES_KEY), \ | |
528 | NULL,NULL,NULL,NULL }; \ | |
17f121de | 529 | static const EVP_CIPHER aes_##keylen##_##mode = { \ |
0f113f3e MC |
530 | nid##_##keylen##_##nmode,blocksize, \ |
531 | keylen/8,ivlen, \ | |
532 | flags|EVP_CIPH_##MODE##_MODE, \ | |
533 | aes_init_key, \ | |
534 | aes_##mode##_cipher, \ | |
535 | NULL, \ | |
536 | sizeof(EVP_AES_KEY), \ | |
537 | NULL,NULL,NULL,NULL }; \ | |
17f121de | 538 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ |
8ca28da0 | 539 | { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; } |
17f121de | 540 | |
5158c763 | 541 | # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \ |
17f121de | 542 | static const EVP_CIPHER aesni_##keylen##_##mode = { \ |
0f113f3e | 543 | nid##_##keylen##_##mode,blocksize, \ |
b1ceb439 TS |
544 | (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \ |
545 | ivlen, \ | |
0f113f3e MC |
546 | flags|EVP_CIPH_##MODE##_MODE, \ |
547 | aesni_##mode##_init_key, \ | |
548 | aesni_##mode##_cipher, \ | |
549 | aes_##mode##_cleanup, \ | |
550 | sizeof(EVP_AES_##MODE##_CTX), \ | |
551 | NULL,NULL,aes_##mode##_ctrl,NULL }; \ | |
17f121de | 552 | static const EVP_CIPHER aes_##keylen##_##mode = { \ |
0f113f3e | 553 | nid##_##keylen##_##mode,blocksize, \ |
b1ceb439 TS |
554 | (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \ |
555 | ivlen, \ | |
0f113f3e MC |
556 | flags|EVP_CIPH_##MODE##_MODE, \ |
557 | aes_##mode##_init_key, \ | |
558 | aes_##mode##_cipher, \ | |
559 | aes_##mode##_cleanup, \ | |
560 | sizeof(EVP_AES_##MODE##_CTX), \ | |
561 | NULL,NULL,aes_##mode##_ctrl,NULL }; \ | |
17f121de | 562 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ |
8ca28da0 | 563 | { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; } |
d1fff483 | 564 | |
5158c763 | 565 | #elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__)) |
c5f6da54 | 566 | |
5158c763 | 567 | # include "sparc_arch.h" |
c5f6da54 AP |
568 | |
569 | extern unsigned int OPENSSL_sparcv9cap_P[]; | |
570 | ||
6944565b AP |
571 | /* |
572 | * Initial Fujitsu SPARC64 X support | |
573 | */ | |
574 | # define HWAES_CAPABLE (OPENSSL_sparcv9cap_P[0] & SPARCV9_FJAESX) | |
575 | # define HWAES_set_encrypt_key aes_fx_set_encrypt_key | |
576 | # define HWAES_set_decrypt_key aes_fx_set_decrypt_key | |
577 | # define HWAES_encrypt aes_fx_encrypt | |
578 | # define HWAES_decrypt aes_fx_decrypt | |
365f95ad AP |
579 | # define HWAES_cbc_encrypt aes_fx_cbc_encrypt |
580 | # define HWAES_ctr32_encrypt_blocks aes_fx_ctr32_encrypt_blocks | |
6944565b | 581 | |
5158c763 | 582 | # define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES) |
c5f6da54 | 583 | |
0f113f3e MC |
584 | void aes_t4_set_encrypt_key(const unsigned char *key, int bits, AES_KEY *ks); |
585 | void aes_t4_set_decrypt_key(const unsigned char *key, int bits, AES_KEY *ks); | |
586 | void aes_t4_encrypt(const unsigned char *in, unsigned char *out, | |
587 | const AES_KEY *key); | |
588 | void aes_t4_decrypt(const unsigned char *in, unsigned char *out, | |
589 | const AES_KEY *key); | |
c5f6da54 AP |
590 | /* |
591 | * Key-length specific subroutines were chosen for following reason. | |
592 | * Each SPARC T4 core can execute up to 8 threads which share core's | |
593 | * resources. Loading as much key material to registers allows to | |
594 | * minimize references to shared memory interface, as well as amount | |
595 | * of instructions in inner loops [much needed on T4]. But then having | |
596 | * non-key-length specific routines would require conditional branches | |
597 | * either in inner loops or on subroutines' entries. Former is hardly | |
598 | * acceptable, while latter means code size increase to size occupied | |
0d4fb843 | 599 | * by multiple key-length specific subroutines, so why fight? |
c5f6da54 | 600 | */ |
0f113f3e MC |
601 | void aes128_t4_cbc_encrypt(const unsigned char *in, unsigned char *out, |
602 | size_t len, const AES_KEY *key, | |
603 | unsigned char *ivec); | |
604 | void aes128_t4_cbc_decrypt(const unsigned char *in, unsigned char *out, | |
605 | size_t len, const AES_KEY *key, | |
606 | unsigned char *ivec); | |
607 | void aes192_t4_cbc_encrypt(const unsigned char *in, unsigned char *out, | |
608 | size_t len, const AES_KEY *key, | |
609 | unsigned char *ivec); | |
610 | void aes192_t4_cbc_decrypt(const unsigned char *in, unsigned char *out, | |
611 | size_t len, const AES_KEY *key, | |
612 | unsigned char *ivec); | |
613 | void aes256_t4_cbc_encrypt(const unsigned char *in, unsigned char *out, | |
614 | size_t len, const AES_KEY *key, | |
615 | unsigned char *ivec); | |
616 | void aes256_t4_cbc_decrypt(const unsigned char *in, unsigned char *out, | |
617 | size_t len, const AES_KEY *key, | |
618 | unsigned char *ivec); | |
619 | void aes128_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out, | |
620 | size_t blocks, const AES_KEY *key, | |
621 | unsigned char *ivec); | |
622 | void aes192_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out, | |
623 | size_t blocks, const AES_KEY *key, | |
624 | unsigned char *ivec); | |
625 | void aes256_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out, | |
626 | size_t blocks, const AES_KEY *key, | |
627 | unsigned char *ivec); | |
628 | void aes128_t4_xts_encrypt(const unsigned char *in, unsigned char *out, | |
629 | size_t blocks, const AES_KEY *key1, | |
630 | const AES_KEY *key2, const unsigned char *ivec); | |
631 | void aes128_t4_xts_decrypt(const unsigned char *in, unsigned char *out, | |
632 | size_t blocks, const AES_KEY *key1, | |
633 | const AES_KEY *key2, const unsigned char *ivec); | |
634 | void aes256_t4_xts_encrypt(const unsigned char *in, unsigned char *out, | |
635 | size_t blocks, const AES_KEY *key1, | |
636 | const AES_KEY *key2, const unsigned char *ivec); | |
637 | void aes256_t4_xts_decrypt(const unsigned char *in, unsigned char *out, | |
638 | size_t blocks, const AES_KEY *key1, | |
639 | const AES_KEY *key2, const unsigned char *ivec); | |
c5f6da54 AP |
640 | |
641 | static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
642 | const unsigned char *iv, int enc) |
643 | { | |
644 | int ret, mode, bits; | |
6435f0f6 | 645 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
0f113f3e | 646 | |
6435f0f6 RL |
647 | mode = EVP_CIPHER_CTX_mode(ctx); |
648 | bits = EVP_CIPHER_CTX_key_length(ctx) * 8; | |
0f113f3e MC |
649 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) |
650 | && !enc) { | |
651 | ret = 0; | |
6435f0f6 | 652 | aes_t4_set_decrypt_key(key, bits, &dat->ks.ks); |
0f113f3e MC |
653 | dat->block = (block128_f) aes_t4_decrypt; |
654 | switch (bits) { | |
655 | case 128: | |
656 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
657 | (cbc128_f) aes128_t4_cbc_decrypt : NULL; | |
658 | break; | |
659 | case 192: | |
660 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
661 | (cbc128_f) aes192_t4_cbc_decrypt : NULL; | |
662 | break; | |
663 | case 256: | |
664 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
665 | (cbc128_f) aes256_t4_cbc_decrypt : NULL; | |
666 | break; | |
667 | default: | |
668 | ret = -1; | |
669 | } | |
670 | } else { | |
671 | ret = 0; | |
6435f0f6 | 672 | aes_t4_set_encrypt_key(key, bits, &dat->ks.ks); |
0f113f3e MC |
673 | dat->block = (block128_f) aes_t4_encrypt; |
674 | switch (bits) { | |
675 | case 128: | |
676 | if (mode == EVP_CIPH_CBC_MODE) | |
677 | dat->stream.cbc = (cbc128_f) aes128_t4_cbc_encrypt; | |
678 | else if (mode == EVP_CIPH_CTR_MODE) | |
679 | dat->stream.ctr = (ctr128_f) aes128_t4_ctr32_encrypt; | |
680 | else | |
681 | dat->stream.cbc = NULL; | |
682 | break; | |
683 | case 192: | |
684 | if (mode == EVP_CIPH_CBC_MODE) | |
685 | dat->stream.cbc = (cbc128_f) aes192_t4_cbc_encrypt; | |
686 | else if (mode == EVP_CIPH_CTR_MODE) | |
687 | dat->stream.ctr = (ctr128_f) aes192_t4_ctr32_encrypt; | |
688 | else | |
689 | dat->stream.cbc = NULL; | |
690 | break; | |
691 | case 256: | |
692 | if (mode == EVP_CIPH_CBC_MODE) | |
693 | dat->stream.cbc = (cbc128_f) aes256_t4_cbc_encrypt; | |
694 | else if (mode == EVP_CIPH_CTR_MODE) | |
695 | dat->stream.ctr = (ctr128_f) aes256_t4_ctr32_encrypt; | |
696 | else | |
697 | dat->stream.cbc = NULL; | |
698 | break; | |
699 | default: | |
700 | ret = -1; | |
701 | } | |
702 | } | |
703 | ||
704 | if (ret < 0) { | |
705 | EVPerr(EVP_F_AES_T4_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); | |
706 | return 0; | |
707 | } | |
708 | ||
709 | return 1; | |
710 | } | |
711 | ||
5158c763 | 712 | # define aes_t4_cbc_cipher aes_cbc_cipher |
0f113f3e MC |
713 | static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
714 | const unsigned char *in, size_t len); | |
715 | ||
5158c763 | 716 | # define aes_t4_ecb_cipher aes_ecb_cipher |
0f113f3e MC |
717 | static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
718 | const unsigned char *in, size_t len); | |
719 | ||
5158c763 | 720 | # define aes_t4_ofb_cipher aes_ofb_cipher |
0f113f3e MC |
721 | static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
722 | const unsigned char *in, size_t len); | |
723 | ||
5158c763 | 724 | # define aes_t4_cfb_cipher aes_cfb_cipher |
0f113f3e MC |
725 | static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
726 | const unsigned char *in, size_t len); | |
727 | ||
5158c763 | 728 | # define aes_t4_cfb8_cipher aes_cfb8_cipher |
0f113f3e MC |
729 | static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
730 | const unsigned char *in, size_t len); | |
731 | ||
5158c763 | 732 | # define aes_t4_cfb1_cipher aes_cfb1_cipher |
0f113f3e MC |
733 | static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
734 | const unsigned char *in, size_t len); | |
735 | ||
5158c763 | 736 | # define aes_t4_ctr_cipher aes_ctr_cipher |
c5f6da54 | 737 | static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 738 | const unsigned char *in, size_t len); |
c5f6da54 AP |
739 | |
740 | static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
741 | const unsigned char *iv, int enc) |
742 | { | |
6435f0f6 | 743 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx); |
0f113f3e MC |
744 | if (!iv && !key) |
745 | return 1; | |
746 | if (key) { | |
6435f0f6 | 747 | int bits = EVP_CIPHER_CTX_key_length(ctx) * 8; |
0f113f3e MC |
748 | aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks); |
749 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, | |
750 | (block128_f) aes_t4_encrypt); | |
751 | switch (bits) { | |
752 | case 128: | |
753 | gctx->ctr = (ctr128_f) aes128_t4_ctr32_encrypt; | |
754 | break; | |
755 | case 192: | |
756 | gctx->ctr = (ctr128_f) aes192_t4_ctr32_encrypt; | |
757 | break; | |
758 | case 256: | |
759 | gctx->ctr = (ctr128_f) aes256_t4_ctr32_encrypt; | |
760 | break; | |
761 | default: | |
762 | return 0; | |
763 | } | |
764 | /* | |
765 | * If we have an iv can set it directly, otherwise use saved IV. | |
766 | */ | |
767 | if (iv == NULL && gctx->iv_set) | |
768 | iv = gctx->iv; | |
769 | if (iv) { | |
770 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | |
771 | gctx->iv_set = 1; | |
772 | } | |
773 | gctx->key_set = 1; | |
774 | } else { | |
775 | /* If key set use IV, otherwise copy */ | |
776 | if (gctx->key_set) | |
777 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | |
778 | else | |
779 | memcpy(gctx->iv, iv, gctx->ivlen); | |
780 | gctx->iv_set = 1; | |
781 | gctx->iv_gen = 0; | |
782 | } | |
783 | return 1; | |
784 | } | |
785 | ||
5158c763 | 786 | # define aes_t4_gcm_cipher aes_gcm_cipher |
c5f6da54 | 787 | static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 788 | const unsigned char *in, size_t len); |
c5f6da54 AP |
789 | |
790 | static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
791 | const unsigned char *iv, int enc) |
792 | { | |
6435f0f6 | 793 | EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx); |
0f113f3e MC |
794 | if (!iv && !key) |
795 | return 1; | |
796 | ||
797 | if (key) { | |
6435f0f6 | 798 | int bits = EVP_CIPHER_CTX_key_length(ctx) * 4; |
0f113f3e MC |
799 | xctx->stream = NULL; |
800 | /* key_len is two AES keys */ | |
801 | if (enc) { | |
802 | aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks); | |
803 | xctx->xts.block1 = (block128_f) aes_t4_encrypt; | |
804 | switch (bits) { | |
805 | case 128: | |
806 | xctx->stream = aes128_t4_xts_encrypt; | |
807 | break; | |
0f113f3e MC |
808 | case 256: |
809 | xctx->stream = aes256_t4_xts_encrypt; | |
810 | break; | |
811 | default: | |
812 | return 0; | |
813 | } | |
814 | } else { | |
6435f0f6 RL |
815 | aes_t4_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4, |
816 | &xctx->ks1.ks); | |
0f113f3e MC |
817 | xctx->xts.block1 = (block128_f) aes_t4_decrypt; |
818 | switch (bits) { | |
819 | case 128: | |
820 | xctx->stream = aes128_t4_xts_decrypt; | |
821 | break; | |
0f113f3e MC |
822 | case 256: |
823 | xctx->stream = aes256_t4_xts_decrypt; | |
824 | break; | |
825 | default: | |
826 | return 0; | |
827 | } | |
828 | } | |
829 | ||
6435f0f6 RL |
830 | aes_t4_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2, |
831 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
832 | &xctx->ks2.ks); | |
0f113f3e MC |
833 | xctx->xts.block2 = (block128_f) aes_t4_encrypt; |
834 | ||
835 | xctx->xts.key1 = &xctx->ks1; | |
836 | } | |
837 | ||
838 | if (iv) { | |
839 | xctx->xts.key2 = &xctx->ks2; | |
6435f0f6 | 840 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16); |
0f113f3e MC |
841 | } |
842 | ||
843 | return 1; | |
844 | } | |
845 | ||
5158c763 | 846 | # define aes_t4_xts_cipher aes_xts_cipher |
c5f6da54 | 847 | static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 848 | const unsigned char *in, size_t len); |
c5f6da54 AP |
849 | |
850 | static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
851 | const unsigned char *iv, int enc) |
852 | { | |
6435f0f6 | 853 | EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx); |
0f113f3e MC |
854 | if (!iv && !key) |
855 | return 1; | |
856 | if (key) { | |
6435f0f6 | 857 | int bits = EVP_CIPHER_CTX_key_length(ctx) * 8; |
0f113f3e MC |
858 | aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks); |
859 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, | |
860 | &cctx->ks, (block128_f) aes_t4_encrypt); | |
bdc985b1 | 861 | cctx->str = NULL; |
0f113f3e MC |
862 | cctx->key_set = 1; |
863 | } | |
864 | if (iv) { | |
6435f0f6 | 865 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L); |
0f113f3e MC |
866 | cctx->iv_set = 1; |
867 | } | |
868 | return 1; | |
869 | } | |
870 | ||
5158c763 | 871 | # define aes_t4_ccm_cipher aes_ccm_cipher |
c5f6da54 | 872 | static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 873 | const unsigned char *in, size_t len); |
c5f6da54 | 874 | |
5158c763 | 875 | # ifndef OPENSSL_NO_OCB |
e6b336ef | 876 | static int aes_t4_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
877 | const unsigned char *iv, int enc) |
878 | { | |
6435f0f6 | 879 | EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx); |
0f113f3e MC |
880 | if (!iv && !key) |
881 | return 1; | |
882 | if (key) { | |
883 | do { | |
884 | /* | |
885 | * We set both the encrypt and decrypt key here because decrypt | |
886 | * needs both. We could possibly optimise to remove setting the | |
887 | * decrypt for an encryption operation. | |
888 | */ | |
6435f0f6 RL |
889 | aes_t4_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
890 | &octx->ksenc.ks); | |
891 | aes_t4_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, | |
892 | &octx->ksdec.ks); | |
bdc985b1 AP |
893 | if (!CRYPTO_ocb128_init(&octx->ocb, |
894 | &octx->ksenc.ks, &octx->ksdec.ks, | |
0f113f3e | 895 | (block128_f) aes_t4_encrypt, |
02dc0b82 AP |
896 | (block128_f) aes_t4_decrypt, |
897 | NULL)) | |
0f113f3e MC |
898 | return 0; |
899 | } | |
900 | while (0); | |
901 | ||
902 | /* | |
903 | * If we have an iv we can set it directly, otherwise use saved IV. | |
904 | */ | |
905 | if (iv == NULL && octx->iv_set) | |
906 | iv = octx->iv; | |
907 | if (iv) { | |
908 | if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen) | |
909 | != 1) | |
910 | return 0; | |
911 | octx->iv_set = 1; | |
912 | } | |
913 | octx->key_set = 1; | |
914 | } else { | |
915 | /* If key set use IV, otherwise copy */ | |
916 | if (octx->key_set) | |
917 | CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen); | |
918 | else | |
919 | memcpy(octx->iv, iv, octx->ivlen); | |
920 | octx->iv_set = 1; | |
921 | } | |
922 | return 1; | |
923 | } | |
924 | ||
5158c763 | 925 | # define aes_t4_ocb_cipher aes_ocb_cipher |
e6b336ef | 926 | static int aes_t4_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 927 | const unsigned char *in, size_t len); |
5158c763 | 928 | # endif /* OPENSSL_NO_OCB */ |
e6b336ef | 929 | |
5158c763 | 930 | # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ |
c5f6da54 | 931 | static const EVP_CIPHER aes_t4_##keylen##_##mode = { \ |
0f113f3e MC |
932 | nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \ |
933 | flags|EVP_CIPH_##MODE##_MODE, \ | |
934 | aes_t4_init_key, \ | |
935 | aes_t4_##mode##_cipher, \ | |
936 | NULL, \ | |
937 | sizeof(EVP_AES_KEY), \ | |
938 | NULL,NULL,NULL,NULL }; \ | |
c5f6da54 | 939 | static const EVP_CIPHER aes_##keylen##_##mode = { \ |
0f113f3e MC |
940 | nid##_##keylen##_##nmode,blocksize, \ |
941 | keylen/8,ivlen, \ | |
942 | flags|EVP_CIPH_##MODE##_MODE, \ | |
943 | aes_init_key, \ | |
944 | aes_##mode##_cipher, \ | |
945 | NULL, \ | |
946 | sizeof(EVP_AES_KEY), \ | |
947 | NULL,NULL,NULL,NULL }; \ | |
c5f6da54 AP |
948 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ |
949 | { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; } | |
950 | ||
5158c763 | 951 | # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \ |
c5f6da54 | 952 | static const EVP_CIPHER aes_t4_##keylen##_##mode = { \ |
0f113f3e | 953 | nid##_##keylen##_##mode,blocksize, \ |
b1ceb439 TS |
954 | (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \ |
955 | ivlen, \ | |
0f113f3e MC |
956 | flags|EVP_CIPH_##MODE##_MODE, \ |
957 | aes_t4_##mode##_init_key, \ | |
958 | aes_t4_##mode##_cipher, \ | |
959 | aes_##mode##_cleanup, \ | |
960 | sizeof(EVP_AES_##MODE##_CTX), \ | |
961 | NULL,NULL,aes_##mode##_ctrl,NULL }; \ | |
c5f6da54 | 962 | static const EVP_CIPHER aes_##keylen##_##mode = { \ |
0f113f3e | 963 | nid##_##keylen##_##mode,blocksize, \ |
b1ceb439 TS |
964 | (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \ |
965 | ivlen, \ | |
0f113f3e MC |
966 | flags|EVP_CIPH_##MODE##_MODE, \ |
967 | aes_##mode##_init_key, \ | |
968 | aes_##mode##_cipher, \ | |
969 | aes_##mode##_cleanup, \ | |
970 | sizeof(EVP_AES_##MODE##_CTX), \ | |
971 | NULL,NULL,aes_##mode##_ctrl,NULL }; \ | |
c5f6da54 AP |
972 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ |
973 | { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; } | |
974 | ||
96530eea PS |
975 | #elif defined(OPENSSL_CPUID_OBJ) && defined(__s390__) |
976 | /* | |
977 | * IBM S390X support | |
978 | */ | |
979 | # include "s390x_arch.h" | |
980 | ||
55bd169f PS |
981 | typedef struct { |
982 | union { | |
983 | double align; | |
984 | /*- | |
985 | * KM-AES parameter block - begin | |
986 | * (see z/Architecture Principles of Operation >= SA22-7832-06) | |
987 | */ | |
988 | struct { | |
989 | unsigned char k[32]; | |
990 | } param; | |
991 | /* KM-AES parameter block - end */ | |
992 | } km; | |
993 | unsigned int fc; | |
994 | } S390X_AES_ECB_CTX; | |
995 | ||
dacd2a87 PS |
996 | typedef struct { |
997 | union { | |
998 | double align; | |
999 | /*- | |
1000 | * KMO-AES parameter block - begin | |
1001 | * (see z/Architecture Principles of Operation >= SA22-7832-08) | |
1002 | */ | |
1003 | struct { | |
1004 | unsigned char cv[16]; | |
1005 | unsigned char k[32]; | |
1006 | } param; | |
1007 | /* KMO-AES parameter block - end */ | |
1008 | } kmo; | |
1009 | unsigned int fc; | |
1010 | ||
1011 | int res; | |
1012 | } S390X_AES_OFB_CTX; | |
1013 | ||
74d38a86 PS |
1014 | typedef struct { |
1015 | union { | |
1016 | double align; | |
1017 | /*- | |
1018 | * KMF-AES parameter block - begin | |
1019 | * (see z/Architecture Principles of Operation >= SA22-7832-08) | |
1020 | */ | |
1021 | struct { | |
1022 | unsigned char cv[16]; | |
1023 | unsigned char k[32]; | |
1024 | } param; | |
1025 | /* KMF-AES parameter block - end */ | |
1026 | } kmf; | |
1027 | unsigned int fc; | |
1028 | ||
1029 | int res; | |
1030 | } S390X_AES_CFB_CTX; | |
1031 | ||
96530eea PS |
1032 | typedef struct { |
1033 | union { | |
1034 | double align; | |
1035 | /*- | |
5d2a6f4b PS |
1036 | * KMA-GCM-AES parameter block - begin |
1037 | * (see z/Architecture Principles of Operation >= SA22-7832-11) | |
96530eea PS |
1038 | */ |
1039 | struct { | |
1040 | unsigned char reserved[12]; | |
1041 | union { | |
1042 | unsigned int w; | |
1043 | unsigned char b[4]; | |
1044 | } cv; | |
1045 | union { | |
1046 | unsigned long long g[2]; | |
1047 | unsigned char b[16]; | |
1048 | } t; | |
1049 | unsigned char h[16]; | |
1050 | unsigned long long taadl; | |
1051 | unsigned long long tpcl; | |
1052 | union { | |
1053 | unsigned long long g[2]; | |
1054 | unsigned int w[4]; | |
1055 | } j0; | |
1056 | unsigned char k[32]; | |
1057 | } param; | |
5d2a6f4b | 1058 | /* KMA-GCM-AES parameter block - end */ |
96530eea PS |
1059 | } kma; |
1060 | unsigned int fc; | |
1061 | int key_set; | |
1062 | ||
1063 | unsigned char *iv; | |
1064 | int ivlen; | |
1065 | int iv_set; | |
1066 | int iv_gen; | |
1067 | ||
1068 | int taglen; | |
1069 | ||
1070 | unsigned char ares[16]; | |
1071 | unsigned char mres[16]; | |
1072 | unsigned char kres[16]; | |
1073 | int areslen; | |
1074 | int mreslen; | |
1075 | int kreslen; | |
1076 | ||
1077 | int tls_aad_len; | |
d6b34570 | 1078 | uint64_t tls_enc_records; /* Number of TLS records encrypted */ |
96530eea PS |
1079 | } S390X_AES_GCM_CTX; |
1080 | ||
39f5b069 PS |
1081 | typedef struct { |
1082 | union { | |
1083 | double align; | |
1084 | /*- | |
1085 | * Padding is chosen so that ccm.kmac_param.k overlaps with key.k and | |
1086 | * ccm.fc with key.k.rounds. Remember that on s390x, an AES_KEY's | |
1087 | * rounds field is used to store the function code and that the key | |
1088 | * schedule is not stored (if aes hardware support is detected). | |
1089 | */ | |
1090 | struct { | |
1091 | unsigned char pad[16]; | |
1092 | AES_KEY k; | |
1093 | } key; | |
1094 | ||
1095 | struct { | |
1096 | /*- | |
1097 | * KMAC-AES parameter block - begin | |
1098 | * (see z/Architecture Principles of Operation >= SA22-7832-08) | |
1099 | */ | |
1100 | struct { | |
1101 | union { | |
1102 | unsigned long long g[2]; | |
1103 | unsigned char b[16]; | |
1104 | } icv; | |
1105 | unsigned char k[32]; | |
1106 | } kmac_param; | |
1107 | /* KMAC-AES paramater block - end */ | |
1108 | ||
1109 | union { | |
1110 | unsigned long long g[2]; | |
1111 | unsigned char b[16]; | |
1112 | } nonce; | |
1113 | union { | |
1114 | unsigned long long g[2]; | |
1115 | unsigned char b[16]; | |
1116 | } buf; | |
1117 | ||
1118 | unsigned long long blocks; | |
1119 | int l; | |
1120 | int m; | |
1121 | int tls_aad_len; | |
1122 | int iv_set; | |
1123 | int tag_set; | |
1124 | int len_set; | |
1125 | int key_set; | |
1126 | ||
1127 | unsigned char pad[140]; | |
1128 | unsigned int fc; | |
1129 | } ccm; | |
1130 | } aes; | |
1131 | } S390X_AES_CCM_CTX; | |
1132 | ||
55bd169f PS |
1133 | /* Convert key size to function code: [16,24,32] -> [18,19,20]. */ |
1134 | # define S390X_AES_FC(keylen) (S390X_AES_128 + ((((keylen) << 3) - 128) >> 6)) | |
1135 | ||
1136 | /* Most modes of operation need km for partial block processing. */ | |
1137 | # define S390X_aes_128_CAPABLE (OPENSSL_s390xcap_P.km[0] & \ | |
1138 | S390X_CAPBIT(S390X_AES_128)) | |
1139 | # define S390X_aes_192_CAPABLE (OPENSSL_s390xcap_P.km[0] & \ | |
1140 | S390X_CAPBIT(S390X_AES_192)) | |
1141 | # define S390X_aes_256_CAPABLE (OPENSSL_s390xcap_P.km[0] & \ | |
1142 | S390X_CAPBIT(S390X_AES_256)) | |
96530eea PS |
1143 | |
1144 | # define s390x_aes_init_key aes_init_key | |
1145 | static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
1146 | const unsigned char *iv, int enc); | |
1147 | ||
1148 | # define S390X_aes_128_cbc_CAPABLE 1 /* checked by callee */ | |
1149 | # define S390X_aes_192_cbc_CAPABLE 1 | |
1150 | # define S390X_aes_256_cbc_CAPABLE 1 | |
55bd169f PS |
1151 | # define S390X_AES_CBC_CTX EVP_AES_KEY |
1152 | ||
1153 | # define s390x_aes_cbc_init_key aes_init_key | |
96530eea PS |
1154 | |
1155 | # define s390x_aes_cbc_cipher aes_cbc_cipher | |
1156 | static int s390x_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
1157 | const unsigned char *in, size_t len); | |
1158 | ||
55bd169f PS |
1159 | # define S390X_aes_128_ecb_CAPABLE S390X_aes_128_CAPABLE |
1160 | # define S390X_aes_192_ecb_CAPABLE S390X_aes_192_CAPABLE | |
1161 | # define S390X_aes_256_ecb_CAPABLE S390X_aes_256_CAPABLE | |
1162 | ||
1163 | static int s390x_aes_ecb_init_key(EVP_CIPHER_CTX *ctx, | |
1164 | const unsigned char *key, | |
1165 | const unsigned char *iv, int enc) | |
1166 | { | |
1167 | S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx); | |
1168 | const int keylen = EVP_CIPHER_CTX_key_length(ctx); | |
1169 | ||
1170 | cctx->fc = S390X_AES_FC(keylen); | |
1171 | if (!enc) | |
1172 | cctx->fc |= S390X_DECRYPT; | |
1173 | ||
1174 | memcpy(cctx->km.param.k, key, keylen); | |
1175 | return 1; | |
1176 | } | |
96530eea | 1177 | |
96530eea | 1178 | static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
55bd169f PS |
1179 | const unsigned char *in, size_t len) |
1180 | { | |
1181 | S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx); | |
1182 | ||
1183 | s390x_km(in, len, out, cctx->fc, &cctx->km.param); | |
1184 | return 1; | |
1185 | } | |
96530eea | 1186 | |
dacd2a87 PS |
1187 | # define S390X_aes_128_ofb_CAPABLE (S390X_aes_128_CAPABLE && \ |
1188 | (OPENSSL_s390xcap_P.kmo[0] & \ | |
1189 | S390X_CAPBIT(S390X_AES_128))) | |
1190 | # define S390X_aes_192_ofb_CAPABLE (S390X_aes_192_CAPABLE && \ | |
1191 | (OPENSSL_s390xcap_P.kmo[0] & \ | |
1192 | S390X_CAPBIT(S390X_AES_192))) | |
1193 | # define S390X_aes_256_ofb_CAPABLE (S390X_aes_256_CAPABLE && \ | |
1194 | (OPENSSL_s390xcap_P.kmo[0] & \ | |
1195 | S390X_CAPBIT(S390X_AES_256))) | |
1196 | ||
1197 | static int s390x_aes_ofb_init_key(EVP_CIPHER_CTX *ctx, | |
1198 | const unsigned char *key, | |
1199 | const unsigned char *ivec, int enc) | |
1200 | { | |
1201 | S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx); | |
1202 | const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx); | |
1203 | const int keylen = EVP_CIPHER_CTX_key_length(ctx); | |
1204 | const int ivlen = EVP_CIPHER_CTX_iv_length(ctx); | |
55bd169f | 1205 | |
dacd2a87 PS |
1206 | memcpy(cctx->kmo.param.cv, iv, ivlen); |
1207 | memcpy(cctx->kmo.param.k, key, keylen); | |
1208 | cctx->fc = S390X_AES_FC(keylen); | |
1209 | cctx->res = 0; | |
1210 | return 1; | |
1211 | } | |
96530eea | 1212 | |
96530eea | 1213 | static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
dacd2a87 PS |
1214 | const unsigned char *in, size_t len) |
1215 | { | |
1216 | S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx); | |
1217 | int n = cctx->res; | |
1218 | int rem; | |
1219 | ||
1220 | while (n && len) { | |
1221 | *out = *in ^ cctx->kmo.param.cv[n]; | |
1222 | n = (n + 1) & 0xf; | |
1223 | --len; | |
1224 | ++in; | |
1225 | ++out; | |
1226 | } | |
1227 | ||
1228 | rem = len & 0xf; | |
1229 | ||
1230 | len &= ~(size_t)0xf; | |
1231 | if (len) { | |
1232 | s390x_kmo(in, len, out, cctx->fc, &cctx->kmo.param); | |
1233 | ||
1234 | out += len; | |
1235 | in += len; | |
1236 | } | |
1237 | ||
1238 | if (rem) { | |
1239 | s390x_km(cctx->kmo.param.cv, 16, cctx->kmo.param.cv, cctx->fc, | |
1240 | cctx->kmo.param.k); | |
1241 | ||
1242 | while (rem--) { | |
1243 | out[n] = in[n] ^ cctx->kmo.param.cv[n]; | |
1244 | ++n; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | cctx->res = n; | |
1249 | return 1; | |
1250 | } | |
96530eea | 1251 | |
74d38a86 PS |
1252 | # define S390X_aes_128_cfb_CAPABLE (S390X_aes_128_CAPABLE && \ |
1253 | (OPENSSL_s390xcap_P.kmf[0] & \ | |
1254 | S390X_CAPBIT(S390X_AES_128))) | |
1255 | # define S390X_aes_192_cfb_CAPABLE (S390X_aes_192_CAPABLE && \ | |
1256 | (OPENSSL_s390xcap_P.kmf[0] & \ | |
1257 | S390X_CAPBIT(S390X_AES_192))) | |
1258 | # define S390X_aes_256_cfb_CAPABLE (S390X_aes_256_CAPABLE && \ | |
1259 | (OPENSSL_s390xcap_P.kmf[0] & \ | |
1260 | S390X_CAPBIT(S390X_AES_256))) | |
1261 | ||
1262 | static int s390x_aes_cfb_init_key(EVP_CIPHER_CTX *ctx, | |
1263 | const unsigned char *key, | |
1264 | const unsigned char *ivec, int enc) | |
1265 | { | |
1266 | S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx); | |
1267 | const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx); | |
1268 | const int keylen = EVP_CIPHER_CTX_key_length(ctx); | |
1269 | const int ivlen = EVP_CIPHER_CTX_iv_length(ctx); | |
1270 | ||
1271 | cctx->fc = S390X_AES_FC(keylen); | |
1272 | cctx->fc |= 16 << 24; /* 16 bytes cipher feedback */ | |
1273 | if (!enc) | |
1274 | cctx->fc |= S390X_DECRYPT; | |
55bd169f | 1275 | |
74d38a86 PS |
1276 | cctx->res = 0; |
1277 | memcpy(cctx->kmf.param.cv, iv, ivlen); | |
1278 | memcpy(cctx->kmf.param.k, key, keylen); | |
1279 | return 1; | |
1280 | } | |
96530eea | 1281 | |
96530eea | 1282 | static int s390x_aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
74d38a86 PS |
1283 | const unsigned char *in, size_t len) |
1284 | { | |
1285 | S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx); | |
1286 | const int keylen = EVP_CIPHER_CTX_key_length(ctx); | |
1287 | const int enc = EVP_CIPHER_CTX_encrypting(ctx); | |
1288 | int n = cctx->res; | |
1289 | int rem; | |
1290 | unsigned char tmp; | |
1291 | ||
1292 | while (n && len) { | |
1293 | tmp = *in; | |
1294 | *out = cctx->kmf.param.cv[n] ^ tmp; | |
1295 | cctx->kmf.param.cv[n] = enc ? *out : tmp; | |
1296 | n = (n + 1) & 0xf; | |
1297 | --len; | |
1298 | ++in; | |
1299 | ++out; | |
1300 | } | |
1301 | ||
1302 | rem = len & 0xf; | |
1303 | ||
1304 | len &= ~(size_t)0xf; | |
1305 | if (len) { | |
1306 | s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param); | |
1307 | ||
1308 | out += len; | |
1309 | in += len; | |
1310 | } | |
1311 | ||
1312 | if (rem) { | |
1313 | s390x_km(cctx->kmf.param.cv, 16, cctx->kmf.param.cv, | |
1314 | S390X_AES_FC(keylen), cctx->kmf.param.k); | |
1315 | ||
1316 | while (rem--) { | |
1317 | tmp = in[n]; | |
1318 | out[n] = cctx->kmf.param.cv[n] ^ tmp; | |
1319 | cctx->kmf.param.cv[n] = enc ? out[n] : tmp; | |
1320 | ++n; | |
1321 | } | |
1322 | } | |
96530eea | 1323 | |
74d38a86 PS |
1324 | cctx->res = n; |
1325 | return 1; | |
1326 | } | |
1327 | ||
1328 | # define S390X_aes_128_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \ | |
1329 | S390X_CAPBIT(S390X_AES_128)) | |
1330 | # define S390X_aes_192_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \ | |
1331 | S390X_CAPBIT(S390X_AES_192)) | |
1332 | # define S390X_aes_256_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \ | |
1333 | S390X_CAPBIT(S390X_AES_256)) | |
1334 | ||
1335 | static int s390x_aes_cfb8_init_key(EVP_CIPHER_CTX *ctx, | |
1336 | const unsigned char *key, | |
1337 | const unsigned char *ivec, int enc) | |
1338 | { | |
1339 | S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx); | |
1340 | const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx); | |
1341 | const int keylen = EVP_CIPHER_CTX_key_length(ctx); | |
1342 | const int ivlen = EVP_CIPHER_CTX_iv_length(ctx); | |
1343 | ||
1344 | cctx->fc = S390X_AES_FC(keylen); | |
1345 | cctx->fc |= 1 << 24; /* 1 byte cipher feedback */ | |
1346 | if (!enc) | |
1347 | cctx->fc |= S390X_DECRYPT; | |
96530eea | 1348 | |
74d38a86 PS |
1349 | memcpy(cctx->kmf.param.cv, iv, ivlen); |
1350 | memcpy(cctx->kmf.param.k, key, keylen); | |
1351 | return 1; | |
1352 | } | |
55bd169f | 1353 | |
96530eea | 1354 | static int s390x_aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
74d38a86 PS |
1355 | const unsigned char *in, size_t len) |
1356 | { | |
1357 | S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx); | |
1358 | ||
1359 | s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param); | |
1360 | return 1; | |
1361 | } | |
96530eea PS |
1362 | |
1363 | # define S390X_aes_128_cfb1_CAPABLE 0 | |
1364 | # define S390X_aes_192_cfb1_CAPABLE 0 | |
1365 | # define S390X_aes_256_cfb1_CAPABLE 0 | |
1366 | ||
55bd169f PS |
1367 | # define s390x_aes_cfb1_init_key aes_init_key |
1368 | ||
96530eea PS |
1369 | # define s390x_aes_cfb1_cipher aes_cfb1_cipher |
1370 | static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
1371 | const unsigned char *in, size_t len); | |
1372 | ||
1373 | # define S390X_aes_128_ctr_CAPABLE 1 /* checked by callee */ | |
1374 | # define S390X_aes_192_ctr_CAPABLE 1 | |
1375 | # define S390X_aes_256_ctr_CAPABLE 1 | |
55bd169f PS |
1376 | # define S390X_AES_CTR_CTX EVP_AES_KEY |
1377 | ||
1378 | # define s390x_aes_ctr_init_key aes_init_key | |
96530eea PS |
1379 | |
1380 | # define s390x_aes_ctr_cipher aes_ctr_cipher | |
1381 | static int s390x_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
1382 | const unsigned char *in, size_t len); | |
1383 | ||
1384 | # define S390X_aes_128_gcm_CAPABLE (S390X_aes_128_CAPABLE && \ | |
1385 | (OPENSSL_s390xcap_P.kma[0] & \ | |
1386 | S390X_CAPBIT(S390X_AES_128))) | |
1387 | # define S390X_aes_192_gcm_CAPABLE (S390X_aes_192_CAPABLE && \ | |
1388 | (OPENSSL_s390xcap_P.kma[0] & \ | |
1389 | S390X_CAPBIT(S390X_AES_192))) | |
1390 | # define S390X_aes_256_gcm_CAPABLE (S390X_aes_256_CAPABLE && \ | |
1391 | (OPENSSL_s390xcap_P.kma[0] & \ | |
1392 | S390X_CAPBIT(S390X_AES_256))) | |
1393 | ||
1394 | /* iv + padding length for iv lenghts != 12 */ | |
1395 | # define S390X_gcm_ivpadlen(i) ((((i) + 15) >> 4 << 4) + 16) | |
1396 | ||
5d2a6f4b PS |
1397 | /*- |
1398 | * Process additional authenticated data. Returns 0 on success. Code is | |
1399 | * big-endian. | |
1400 | */ | |
96530eea PS |
1401 | static int s390x_aes_gcm_aad(S390X_AES_GCM_CTX *ctx, const unsigned char *aad, |
1402 | size_t len) | |
1403 | { | |
1404 | unsigned long long alen; | |
1405 | int n, rem; | |
1406 | ||
1407 | if (ctx->kma.param.tpcl) | |
1408 | return -2; | |
1409 | ||
1410 | alen = ctx->kma.param.taadl + len; | |
1411 | if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len)) | |
1412 | return -1; | |
1413 | ctx->kma.param.taadl = alen; | |
1414 | ||
1415 | n = ctx->areslen; | |
1416 | if (n) { | |
1417 | while (n && len) { | |
1418 | ctx->ares[n] = *aad; | |
1419 | n = (n + 1) & 0xf; | |
1420 | ++aad; | |
1421 | --len; | |
1422 | } | |
1423 | /* ctx->ares contains a complete block if offset has wrapped around */ | |
1424 | if (!n) { | |
1425 | s390x_kma(ctx->ares, 16, NULL, 0, NULL, ctx->fc, &ctx->kma.param); | |
1426 | ctx->fc |= S390X_KMA_HS; | |
1427 | } | |
1428 | ctx->areslen = n; | |
1429 | } | |
1430 | ||
1431 | rem = len & 0xf; | |
1432 | ||
25868993 | 1433 | len &= ~(size_t)0xf; |
96530eea PS |
1434 | if (len) { |
1435 | s390x_kma(aad, len, NULL, 0, NULL, ctx->fc, &ctx->kma.param); | |
1436 | aad += len; | |
1437 | ctx->fc |= S390X_KMA_HS; | |
1438 | } | |
1439 | ||
1440 | if (rem) { | |
1441 | ctx->areslen = rem; | |
1442 | ||
1443 | do { | |
1444 | --rem; | |
1445 | ctx->ares[rem] = aad[rem]; | |
1446 | } while (rem); | |
1447 | } | |
1448 | return 0; | |
1449 | } | |
1450 | ||
5d2a6f4b PS |
1451 | /*- |
1452 | * En/de-crypt plain/cipher-text and authenticate ciphertext. Returns 0 for | |
1453 | * success. Code is big-endian. | |
1454 | */ | |
96530eea PS |
1455 | static int s390x_aes_gcm(S390X_AES_GCM_CTX *ctx, const unsigned char *in, |
1456 | unsigned char *out, size_t len) | |
1457 | { | |
1458 | const unsigned char *inptr; | |
1459 | unsigned long long mlen; | |
1460 | union { | |
1461 | unsigned int w[4]; | |
1462 | unsigned char b[16]; | |
1463 | } buf; | |
1464 | size_t inlen; | |
1465 | int n, rem, i; | |
1466 | ||
1467 | mlen = ctx->kma.param.tpcl + len; | |
1468 | if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) | |
1469 | return -1; | |
1470 | ctx->kma.param.tpcl = mlen; | |
1471 | ||
1472 | n = ctx->mreslen; | |
1473 | if (n) { | |
1474 | inptr = in; | |
1475 | inlen = len; | |
1476 | while (n && inlen) { | |
1477 | ctx->mres[n] = *inptr; | |
1478 | n = (n + 1) & 0xf; | |
1479 | ++inptr; | |
1480 | --inlen; | |
1481 | } | |
1482 | /* ctx->mres contains a complete block if offset has wrapped around */ | |
1483 | if (!n) { | |
1484 | s390x_kma(ctx->ares, ctx->areslen, ctx->mres, 16, buf.b, | |
1485 | ctx->fc | S390X_KMA_LAAD, &ctx->kma.param); | |
1486 | ctx->fc |= S390X_KMA_HS; | |
1487 | ctx->areslen = 0; | |
1488 | ||
1489 | /* previous call already encrypted/decrypted its remainder, | |
1490 | * see comment below */ | |
1491 | n = ctx->mreslen; | |
1492 | while (n) { | |
1493 | *out = buf.b[n]; | |
1494 | n = (n + 1) & 0xf; | |
1495 | ++out; | |
1496 | ++in; | |
1497 | --len; | |
1498 | } | |
1499 | ctx->mreslen = 0; | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | rem = len & 0xf; | |
1504 | ||
25868993 | 1505 | len &= ~(size_t)0xf; |
96530eea PS |
1506 | if (len) { |
1507 | s390x_kma(ctx->ares, ctx->areslen, in, len, out, | |
1508 | ctx->fc | S390X_KMA_LAAD, &ctx->kma.param); | |
1509 | in += len; | |
1510 | out += len; | |
1511 | ctx->fc |= S390X_KMA_HS; | |
1512 | ctx->areslen = 0; | |
1513 | } | |
1514 | ||
1515 | /*- | |
1516 | * If there is a remainder, it has to be saved such that it can be | |
1517 | * processed by kma later. However, we also have to do the for-now | |
1518 | * unauthenticated encryption/decryption part here and now... | |
1519 | */ | |
1520 | if (rem) { | |
1521 | if (!ctx->mreslen) { | |
1522 | buf.w[0] = ctx->kma.param.j0.w[0]; | |
1523 | buf.w[1] = ctx->kma.param.j0.w[1]; | |
1524 | buf.w[2] = ctx->kma.param.j0.w[2]; | |
1525 | buf.w[3] = ctx->kma.param.cv.w + 1; | |
1526 | s390x_km(buf.b, 16, ctx->kres, ctx->fc & 0x1f, &ctx->kma.param.k); | |
1527 | } | |
1528 | ||
1529 | n = ctx->mreslen; | |
1530 | for (i = 0; i < rem; i++) { | |
1531 | ctx->mres[n + i] = in[i]; | |
1532 | out[i] = in[i] ^ ctx->kres[n + i]; | |
1533 | } | |
1534 | ||
1535 | ctx->mreslen += rem; | |
1536 | } | |
1537 | return 0; | |
1538 | } | |
1539 | ||
5d2a6f4b PS |
1540 | /*- |
1541 | * Initialize context structure. Code is big-endian. | |
1542 | */ | |
96530eea PS |
1543 | static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx, |
1544 | const unsigned char *iv) | |
1545 | { | |
1546 | ctx->kma.param.t.g[0] = 0; | |
1547 | ctx->kma.param.t.g[1] = 0; | |
1548 | ctx->kma.param.tpcl = 0; | |
1549 | ctx->kma.param.taadl = 0; | |
1550 | ctx->mreslen = 0; | |
1551 | ctx->areslen = 0; | |
1552 | ctx->kreslen = 0; | |
1553 | ||
1554 | if (ctx->ivlen == 12) { | |
1555 | memcpy(&ctx->kma.param.j0, iv, ctx->ivlen); | |
1556 | ctx->kma.param.j0.w[3] = 1; | |
1557 | ctx->kma.param.cv.w = 1; | |
1558 | } else { | |
1559 | /* ctx->iv has the right size and is already padded. */ | |
1560 | memcpy(ctx->iv, iv, ctx->ivlen); | |
1561 | s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL, | |
1562 | ctx->fc, &ctx->kma.param); | |
1563 | ctx->fc |= S390X_KMA_HS; | |
1564 | ||
1565 | ctx->kma.param.j0.g[0] = ctx->kma.param.t.g[0]; | |
1566 | ctx->kma.param.j0.g[1] = ctx->kma.param.t.g[1]; | |
1567 | ctx->kma.param.cv.w = ctx->kma.param.j0.w[3]; | |
1568 | ctx->kma.param.t.g[0] = 0; | |
1569 | ctx->kma.param.t.g[1] = 0; | |
1570 | } | |
1571 | } | |
1572 | ||
5d2a6f4b PS |
1573 | /*- |
1574 | * Performs various operations on the context structure depending on control | |
1575 | * type. Returns 1 for success, 0 for failure and -1 for unknown control type. | |
1576 | * Code is big-endian. | |
1577 | */ | |
96530eea PS |
1578 | static int s390x_aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
1579 | { | |
1580 | S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c); | |
1581 | S390X_AES_GCM_CTX *gctx_out; | |
1582 | EVP_CIPHER_CTX *out; | |
1583 | unsigned char *buf, *iv; | |
1584 | int ivlen, enc, len; | |
1585 | ||
1586 | switch (type) { | |
1587 | case EVP_CTRL_INIT: | |
1588 | ivlen = EVP_CIPHER_CTX_iv_length(c); | |
1589 | iv = EVP_CIPHER_CTX_iv_noconst(c); | |
1590 | gctx->key_set = 0; | |
1591 | gctx->iv_set = 0; | |
1592 | gctx->ivlen = ivlen; | |
1593 | gctx->iv = iv; | |
1594 | gctx->taglen = -1; | |
1595 | gctx->iv_gen = 0; | |
1596 | gctx->tls_aad_len = -1; | |
1597 | return 1; | |
1598 | ||
1599 | case EVP_CTRL_AEAD_SET_IVLEN: | |
1600 | if (arg <= 0) | |
1601 | return 0; | |
1602 | ||
1603 | if (arg != 12) { | |
1604 | iv = EVP_CIPHER_CTX_iv_noconst(c); | |
1605 | len = S390X_gcm_ivpadlen(arg); | |
1606 | ||
1607 | /* Allocate memory for iv if needed. */ | |
1608 | if (gctx->ivlen == 12 || len > S390X_gcm_ivpadlen(gctx->ivlen)) { | |
1609 | if (gctx->iv != iv) | |
1610 | OPENSSL_free(gctx->iv); | |
1611 | ||
cdb10bae RS |
1612 | if ((gctx->iv = OPENSSL_malloc(len)) == NULL) { |
1613 | EVPerr(EVP_F_S390X_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE); | |
96530eea | 1614 | return 0; |
cdb10bae | 1615 | } |
96530eea PS |
1616 | } |
1617 | /* Add padding. */ | |
1618 | memset(gctx->iv + arg, 0, len - arg - 8); | |
1619 | *((unsigned long long *)(gctx->iv + len - 8)) = arg << 3; | |
1620 | } | |
1621 | gctx->ivlen = arg; | |
1622 | return 1; | |
1623 | ||
1624 | case EVP_CTRL_AEAD_SET_TAG: | |
1625 | buf = EVP_CIPHER_CTX_buf_noconst(c); | |
1626 | enc = EVP_CIPHER_CTX_encrypting(c); | |
1627 | if (arg <= 0 || arg > 16 || enc) | |
1628 | return 0; | |
1629 | ||
1630 | memcpy(buf, ptr, arg); | |
1631 | gctx->taglen = arg; | |
1632 | return 1; | |
1633 | ||
1634 | case EVP_CTRL_AEAD_GET_TAG: | |
1635 | enc = EVP_CIPHER_CTX_encrypting(c); | |
1636 | if (arg <= 0 || arg > 16 || !enc || gctx->taglen < 0) | |
1637 | return 0; | |
1638 | ||
1639 | memcpy(ptr, gctx->kma.param.t.b, arg); | |
1640 | return 1; | |
1641 | ||
1642 | case EVP_CTRL_GCM_SET_IV_FIXED: | |
1643 | /* Special case: -1 length restores whole iv */ | |
1644 | if (arg == -1) { | |
1645 | memcpy(gctx->iv, ptr, gctx->ivlen); | |
1646 | gctx->iv_gen = 1; | |
1647 | return 1; | |
1648 | } | |
1649 | /* | |
1650 | * Fixed field must be at least 4 bytes and invocation field at least | |
1651 | * 8. | |
1652 | */ | |
1653 | if ((arg < 4) || (gctx->ivlen - arg) < 8) | |
1654 | return 0; | |
1655 | ||
1656 | if (arg) | |
1657 | memcpy(gctx->iv, ptr, arg); | |
1658 | ||
1659 | enc = EVP_CIPHER_CTX_encrypting(c); | |
16cfc2c9 KR |
1660 | if (enc && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) |
1661 | return 0; | |
96530eea PS |
1662 | |
1663 | gctx->iv_gen = 1; | |
1664 | return 1; | |
1665 | ||
1666 | case EVP_CTRL_GCM_IV_GEN: | |
1667 | if (gctx->iv_gen == 0 || gctx->key_set == 0) | |
1668 | return 0; | |
1669 | ||
1670 | s390x_aes_gcm_setiv(gctx, gctx->iv); | |
1671 | ||
1672 | if (arg <= 0 || arg > gctx->ivlen) | |
1673 | arg = gctx->ivlen; | |
1674 | ||
1675 | memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); | |
1676 | /* | |
1677 | * Invocation field will be at least 8 bytes in size and so no need | |
1678 | * to check wrap around or increment more than last 8 bytes. | |
1679 | */ | |
03a5e5ae | 1680 | ctr64_inc(gctx->iv + gctx->ivlen - 8); |
96530eea PS |
1681 | gctx->iv_set = 1; |
1682 | return 1; | |
1683 | ||
1684 | case EVP_CTRL_GCM_SET_IV_INV: | |
1685 | enc = EVP_CIPHER_CTX_encrypting(c); | |
1686 | if (gctx->iv_gen == 0 || gctx->key_set == 0 || enc) | |
1687 | return 0; | |
1688 | ||
1689 | memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); | |
1690 | s390x_aes_gcm_setiv(gctx, gctx->iv); | |
1691 | gctx->iv_set = 1; | |
1692 | return 1; | |
1693 | ||
1694 | case EVP_CTRL_AEAD_TLS1_AAD: | |
1695 | /* Save the aad for later use. */ | |
1696 | if (arg != EVP_AEAD_TLS1_AAD_LEN) | |
1697 | return 0; | |
1698 | ||
1699 | buf = EVP_CIPHER_CTX_buf_noconst(c); | |
1700 | memcpy(buf, ptr, arg); | |
1701 | gctx->tls_aad_len = arg; | |
d6b34570 | 1702 | gctx->tls_enc_records = 0; |
96530eea PS |
1703 | |
1704 | len = buf[arg - 2] << 8 | buf[arg - 1]; | |
1705 | /* Correct length for explicit iv. */ | |
1706 | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) | |
1707 | return 0; | |
1708 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
1709 | ||
1710 | /* If decrypting correct for tag too. */ | |
1711 | enc = EVP_CIPHER_CTX_encrypting(c); | |
1712 | if (!enc) { | |
1713 | if (len < EVP_GCM_TLS_TAG_LEN) | |
1714 | return 0; | |
1715 | len -= EVP_GCM_TLS_TAG_LEN; | |
1716 | } | |
1717 | buf[arg - 2] = len >> 8; | |
1718 | buf[arg - 1] = len & 0xff; | |
1719 | /* Extra padding: tag appended to record. */ | |
1720 | return EVP_GCM_TLS_TAG_LEN; | |
1721 | ||
1722 | case EVP_CTRL_COPY: | |
1723 | out = ptr; | |
1724 | gctx_out = EVP_C_DATA(S390X_AES_GCM_CTX, out); | |
1725 | iv = EVP_CIPHER_CTX_iv_noconst(c); | |
1726 | ||
1727 | if (gctx->iv == iv) { | |
1728 | gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out); | |
1729 | } else { | |
1730 | len = S390X_gcm_ivpadlen(gctx->ivlen); | |
1731 | ||
cdb10bae RS |
1732 | if ((gctx_out->iv = OPENSSL_malloc(len)) == NULL) { |
1733 | EVPerr(EVP_F_S390X_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE); | |
96530eea | 1734 | return 0; |
cdb10bae | 1735 | } |
96530eea PS |
1736 | |
1737 | memcpy(gctx_out->iv, gctx->iv, len); | |
1738 | } | |
1739 | return 1; | |
1740 | ||
1741 | default: | |
1742 | return -1; | |
1743 | } | |
1744 | } | |
1745 | ||
5d2a6f4b PS |
1746 | /*- |
1747 | * Set key and/or iv. Returns 1 on success. Otherwise 0 is returned. | |
1748 | */ | |
96530eea PS |
1749 | static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx, |
1750 | const unsigned char *key, | |
1751 | const unsigned char *iv, int enc) | |
1752 | { | |
1753 | S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx); | |
1754 | int keylen; | |
1755 | ||
1756 | if (iv == NULL && key == NULL) | |
1757 | return 1; | |
1758 | ||
1759 | if (key != NULL) { | |
1760 | keylen = EVP_CIPHER_CTX_key_length(ctx); | |
1761 | memcpy(&gctx->kma.param.k, key, keylen); | |
1762 | ||
8eb399fb | 1763 | gctx->fc = S390X_AES_FC(keylen); |
96530eea PS |
1764 | if (!enc) |
1765 | gctx->fc |= S390X_DECRYPT; | |
1766 | ||
1767 | if (iv == NULL && gctx->iv_set) | |
1768 | iv = gctx->iv; | |
1769 | ||
1770 | if (iv != NULL) { | |
1771 | s390x_aes_gcm_setiv(gctx, iv); | |
1772 | gctx->iv_set = 1; | |
1773 | } | |
1774 | gctx->key_set = 1; | |
1775 | } else { | |
1776 | if (gctx->key_set) | |
1777 | s390x_aes_gcm_setiv(gctx, iv); | |
1778 | else | |
1779 | memcpy(gctx->iv, iv, gctx->ivlen); | |
1780 | ||
1781 | gctx->iv_set = 1; | |
1782 | gctx->iv_gen = 0; | |
1783 | } | |
1784 | return 1; | |
1785 | } | |
1786 | ||
5d2a6f4b PS |
1787 | /*- |
1788 | * En/de-crypt and authenticate TLS packet. Returns the number of bytes written | |
1789 | * if successful. Otherwise -1 is returned. Code is big-endian. | |
1790 | */ | |
96530eea PS |
1791 | static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1792 | const unsigned char *in, size_t len) | |
1793 | { | |
1794 | S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx); | |
1795 | const unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx); | |
1796 | const int enc = EVP_CIPHER_CTX_encrypting(ctx); | |
1797 | int rv = -1; | |
1798 | ||
1799 | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) | |
1800 | return -1; | |
1801 | ||
d6b34570 P |
1802 | /* |
1803 | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness | |
1804 | * Requirements from SP 800-38D". The requirements is for one party to the | |
1805 | * communication to fail after 2^64 - 1 keys. We do this on the encrypting | |
1806 | * side only. | |
1807 | */ | |
1808 | if (ctx->encrypt && ++gctx->tls_enc_records == 0) { | |
1809 | EVPerr(EVP_F_S390X_AES_GCM_TLS_CIPHER, EVP_R_TOO_MANY_RECORDS); | |
1810 | goto err; | |
1811 | } | |
1812 | ||
96530eea PS |
1813 | if (EVP_CIPHER_CTX_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN |
1814 | : EVP_CTRL_GCM_SET_IV_INV, | |
1815 | EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) | |
1816 | goto err; | |
1817 | ||
1818 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
1819 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
1820 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; | |
1821 | ||
1822 | gctx->kma.param.taadl = gctx->tls_aad_len << 3; | |
1823 | gctx->kma.param.tpcl = len << 3; | |
1824 | s390x_kma(buf, gctx->tls_aad_len, in, len, out, | |
1825 | gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param); | |
1826 | ||
1827 | if (enc) { | |
1828 | memcpy(out + len, gctx->kma.param.t.b, EVP_GCM_TLS_TAG_LEN); | |
1829 | rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; | |
1830 | } else { | |
1831 | if (CRYPTO_memcmp(gctx->kma.param.t.b, in + len, | |
1832 | EVP_GCM_TLS_TAG_LEN)) { | |
1833 | OPENSSL_cleanse(out, len); | |
1834 | goto err; | |
1835 | } | |
1836 | rv = len; | |
1837 | } | |
1838 | err: | |
1839 | gctx->iv_set = 0; | |
1840 | gctx->tls_aad_len = -1; | |
1841 | return rv; | |
1842 | } | |
1843 | ||
5d2a6f4b PS |
1844 | /*- |
1845 | * Called from EVP layer to initialize context, process additional | |
1846 | * authenticated data, en/de-crypt plain/cipher-text and authenticate | |
1847 | * ciphertext or process a TLS packet, depending on context. Returns bytes | |
1848 | * written on success. Otherwise -1 is returned. Code is big-endian. | |
1849 | */ | |
96530eea PS |
1850 | static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
1851 | const unsigned char *in, size_t len) | |
1852 | { | |
1853 | S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx); | |
1854 | unsigned char *buf, tmp[16]; | |
1855 | int enc; | |
1856 | ||
1857 | if (!gctx->key_set) | |
1858 | return -1; | |
1859 | ||
1860 | if (gctx->tls_aad_len >= 0) | |
1861 | return s390x_aes_gcm_tls_cipher(ctx, out, in, len); | |
1862 | ||
1863 | if (!gctx->iv_set) | |
1864 | return -1; | |
1865 | ||
1866 | if (in != NULL) { | |
1867 | if (out == NULL) { | |
1868 | if (s390x_aes_gcm_aad(gctx, in, len)) | |
1869 | return -1; | |
1870 | } else { | |
1871 | if (s390x_aes_gcm(gctx, in, out, len)) | |
1872 | return -1; | |
1873 | } | |
1874 | return len; | |
1875 | } else { | |
1876 | gctx->kma.param.taadl <<= 3; | |
1877 | gctx->kma.param.tpcl <<= 3; | |
1878 | s390x_kma(gctx->ares, gctx->areslen, gctx->mres, gctx->mreslen, tmp, | |
1879 | gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param); | |
1880 | /* recall that we already did en-/decrypt gctx->mres | |
1881 | * and returned it to caller... */ | |
1882 | OPENSSL_cleanse(tmp, gctx->mreslen); | |
1883 | gctx->iv_set = 0; | |
1884 | ||
1885 | enc = EVP_CIPHER_CTX_encrypting(ctx); | |
1886 | if (enc) { | |
1887 | gctx->taglen = 16; | |
1888 | } else { | |
1889 | if (gctx->taglen < 0) | |
1890 | return -1; | |
1891 | ||
1892 | buf = EVP_CIPHER_CTX_buf_noconst(ctx); | |
1893 | if (CRYPTO_memcmp(buf, gctx->kma.param.t.b, gctx->taglen)) | |
1894 | return -1; | |
1895 | } | |
1896 | return 0; | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX *c) | |
1901 | { | |
1902 | S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c); | |
1903 | const unsigned char *iv; | |
1904 | ||
1905 | if (gctx == NULL) | |
1906 | return 0; | |
1907 | ||
1908 | iv = EVP_CIPHER_CTX_iv(c); | |
1909 | if (iv != gctx->iv) | |
1910 | OPENSSL_free(gctx->iv); | |
1911 | ||
1912 | OPENSSL_cleanse(gctx, sizeof(*gctx)); | |
1913 | return 1; | |
1914 | } | |
1915 | ||
1916 | # define S390X_AES_XTS_CTX EVP_AES_XTS_CTX | |
1917 | # define S390X_aes_128_xts_CAPABLE 1 /* checked by callee */ | |
1918 | # define S390X_aes_256_xts_CAPABLE 1 | |
1919 | ||
1920 | # define s390x_aes_xts_init_key aes_xts_init_key | |
1921 | static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx, | |
1922 | const unsigned char *key, | |
1923 | const unsigned char *iv, int enc); | |
1924 | # define s390x_aes_xts_cipher aes_xts_cipher | |
1925 | static int s390x_aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
1926 | const unsigned char *in, size_t len); | |
1927 | # define s390x_aes_xts_ctrl aes_xts_ctrl | |
1928 | static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr); | |
1929 | # define s390x_aes_xts_cleanup aes_xts_cleanup | |
1930 | ||
39f5b069 PS |
1931 | # define S390X_aes_128_ccm_CAPABLE (S390X_aes_128_CAPABLE && \ |
1932 | (OPENSSL_s390xcap_P.kmac[0] & \ | |
1933 | S390X_CAPBIT(S390X_AES_128))) | |
1934 | # define S390X_aes_192_ccm_CAPABLE (S390X_aes_192_CAPABLE && \ | |
1935 | (OPENSSL_s390xcap_P.kmac[0] & \ | |
1936 | S390X_CAPBIT(S390X_AES_192))) | |
1937 | # define S390X_aes_256_ccm_CAPABLE (S390X_aes_256_CAPABLE && \ | |
1938 | (OPENSSL_s390xcap_P.kmac[0] & \ | |
1939 | S390X_CAPBIT(S390X_AES_256))) | |
1940 | ||
1941 | # define S390X_CCM_AAD_FLAG 0x40 | |
1942 | ||
1943 | /*- | |
1944 | * Set nonce and length fields. Code is big-endian. | |
1945 | */ | |
1946 | static inline void s390x_aes_ccm_setiv(S390X_AES_CCM_CTX *ctx, | |
1947 | const unsigned char *nonce, | |
1948 | size_t mlen) | |
1949 | { | |
1950 | ctx->aes.ccm.nonce.b[0] &= ~S390X_CCM_AAD_FLAG; | |
1951 | ctx->aes.ccm.nonce.g[1] = mlen; | |
1952 | memcpy(ctx->aes.ccm.nonce.b + 1, nonce, 15 - ctx->aes.ccm.l); | |
1953 | } | |
1954 | ||
1955 | /*- | |
1956 | * Process additional authenticated data. Code is big-endian. | |
1957 | */ | |
1958 | static void s390x_aes_ccm_aad(S390X_AES_CCM_CTX *ctx, const unsigned char *aad, | |
1959 | size_t alen) | |
1960 | { | |
1961 | unsigned char *ptr; | |
1962 | int i, rem; | |
1963 | ||
1964 | if (!alen) | |
1965 | return; | |
1966 | ||
1967 | ctx->aes.ccm.nonce.b[0] |= S390X_CCM_AAD_FLAG; | |
1968 | ||
1969 | /* Suppress 'type-punned pointer dereference' warning. */ | |
1970 | ptr = ctx->aes.ccm.buf.b; | |
1971 | ||
1972 | if (alen < ((1 << 16) - (1 << 8))) { | |
1973 | *(uint16_t *)ptr = alen; | |
1974 | i = 2; | |
1975 | } else if (sizeof(alen) == 8 | |
1976 | && alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) { | |
1977 | *(uint16_t *)ptr = 0xffff; | |
1978 | *(uint64_t *)(ptr + 2) = alen; | |
1979 | i = 10; | |
1980 | } else { | |
1981 | *(uint16_t *)ptr = 0xfffe; | |
1982 | *(uint32_t *)(ptr + 2) = alen; | |
1983 | i = 6; | |
1984 | } | |
1985 | ||
1986 | while (i < 16 && alen) { | |
1987 | ctx->aes.ccm.buf.b[i] = *aad; | |
1988 | ++aad; | |
1989 | --alen; | |
1990 | ++i; | |
1991 | } | |
1992 | while (i < 16) { | |
1993 | ctx->aes.ccm.buf.b[i] = 0; | |
1994 | ++i; | |
1995 | } | |
1996 | ||
1997 | ctx->aes.ccm.kmac_param.icv.g[0] = 0; | |
1998 | ctx->aes.ccm.kmac_param.icv.g[1] = 0; | |
1999 | s390x_kmac(ctx->aes.ccm.nonce.b, 32, ctx->aes.ccm.fc, | |
2000 | &ctx->aes.ccm.kmac_param); | |
2001 | ctx->aes.ccm.blocks += 2; | |
2002 | ||
2003 | rem = alen & 0xf; | |
25868993 | 2004 | alen &= ~(size_t)0xf; |
39f5b069 PS |
2005 | if (alen) { |
2006 | s390x_kmac(aad, alen, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param); | |
2007 | ctx->aes.ccm.blocks += alen >> 4; | |
2008 | aad += alen; | |
2009 | } | |
2010 | if (rem) { | |
2011 | for (i = 0; i < rem; i++) | |
2012 | ctx->aes.ccm.kmac_param.icv.b[i] ^= aad[i]; | |
2013 | ||
2014 | s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16, | |
2015 | ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc, | |
2016 | ctx->aes.ccm.kmac_param.k); | |
2017 | ctx->aes.ccm.blocks++; | |
2018 | } | |
2019 | } | |
2020 | ||
2021 | /*- | |
2022 | * En/de-crypt plain/cipher-text. Compute tag from plaintext. Returns 0 for | |
2023 | * success. | |
2024 | */ | |
2025 | static int s390x_aes_ccm(S390X_AES_CCM_CTX *ctx, const unsigned char *in, | |
2026 | unsigned char *out, size_t len, int enc) | |
2027 | { | |
2028 | size_t n, rem; | |
2029 | unsigned int i, l, num; | |
2030 | unsigned char flags; | |
2031 | ||
2032 | flags = ctx->aes.ccm.nonce.b[0]; | |
2033 | if (!(flags & S390X_CCM_AAD_FLAG)) { | |
2034 | s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.kmac_param.icv.b, | |
2035 | ctx->aes.ccm.fc, ctx->aes.ccm.kmac_param.k); | |
2036 | ctx->aes.ccm.blocks++; | |
2037 | } | |
2038 | l = flags & 0x7; | |
2039 | ctx->aes.ccm.nonce.b[0] = l; | |
2040 | ||
2041 | /*- | |
2042 | * Reconstruct length from encoded length field | |
2043 | * and initialize it with counter value. | |
2044 | */ | |
2045 | n = 0; | |
2046 | for (i = 15 - l; i < 15; i++) { | |
2047 | n |= ctx->aes.ccm.nonce.b[i]; | |
2048 | ctx->aes.ccm.nonce.b[i] = 0; | |
2049 | n <<= 8; | |
2050 | } | |
2051 | n |= ctx->aes.ccm.nonce.b[15]; | |
2052 | ctx->aes.ccm.nonce.b[15] = 1; | |
2053 | ||
2054 | if (n != len) | |
2055 | return -1; /* length mismatch */ | |
2056 | ||
2057 | if (enc) { | |
2058 | /* Two operations per block plus one for tag encryption */ | |
2059 | ctx->aes.ccm.blocks += (((len + 15) >> 4) << 1) + 1; | |
2060 | if (ctx->aes.ccm.blocks > (1ULL << 61)) | |
2061 | return -2; /* too much data */ | |
2062 | } | |
2063 | ||
2064 | num = 0; | |
2065 | rem = len & 0xf; | |
25868993 | 2066 | len &= ~(size_t)0xf; |
39f5b069 PS |
2067 | |
2068 | if (enc) { | |
2069 | /* mac-then-encrypt */ | |
2070 | if (len) | |
2071 | s390x_kmac(in, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param); | |
2072 | if (rem) { | |
2073 | for (i = 0; i < rem; i++) | |
2074 | ctx->aes.ccm.kmac_param.icv.b[i] ^= in[len + i]; | |
2075 | ||
2076 | s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16, | |
2077 | ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc, | |
2078 | ctx->aes.ccm.kmac_param.k); | |
2079 | } | |
2080 | ||
2081 | CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k, | |
2082 | ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b, | |
2083 | &num, (ctr128_f)AES_ctr32_encrypt); | |
2084 | } else { | |
2085 | /* decrypt-then-mac */ | |
2086 | CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k, | |
2087 | ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b, | |
2088 | &num, (ctr128_f)AES_ctr32_encrypt); | |
2089 | ||
2090 | if (len) | |
2091 | s390x_kmac(out, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param); | |
2092 | if (rem) { | |
2093 | for (i = 0; i < rem; i++) | |
2094 | ctx->aes.ccm.kmac_param.icv.b[i] ^= out[len + i]; | |
2095 | ||
2096 | s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16, | |
2097 | ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc, | |
2098 | ctx->aes.ccm.kmac_param.k); | |
2099 | } | |
2100 | } | |
2101 | /* encrypt tag */ | |
2102 | for (i = 15 - l; i < 16; i++) | |
2103 | ctx->aes.ccm.nonce.b[i] = 0; | |
2104 | ||
2105 | s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.buf.b, ctx->aes.ccm.fc, | |
2106 | ctx->aes.ccm.kmac_param.k); | |
2107 | ctx->aes.ccm.kmac_param.icv.g[0] ^= ctx->aes.ccm.buf.g[0]; | |
2108 | ctx->aes.ccm.kmac_param.icv.g[1] ^= ctx->aes.ccm.buf.g[1]; | |
2109 | ||
2110 | ctx->aes.ccm.nonce.b[0] = flags; /* restore flags field */ | |
2111 | return 0; | |
2112 | } | |
2113 | ||
2114 | /*- | |
2115 | * En/de-crypt and authenticate TLS packet. Returns the number of bytes written | |
2116 | * if successful. Otherwise -1 is returned. | |
2117 | */ | |
2118 | static int s390x_aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
2119 | const unsigned char *in, size_t len) | |
2120 | { | |
2121 | S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx); | |
2122 | unsigned char *ivec = EVP_CIPHER_CTX_iv_noconst(ctx); | |
2123 | unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx); | |
2124 | const int enc = EVP_CIPHER_CTX_encrypting(ctx); | |
2125 | ||
2126 | if (out != in | |
2127 | || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->aes.ccm.m)) | |
2128 | return -1; | |
2129 | ||
2130 | if (enc) { | |
2131 | /* Set explicit iv (sequence number). */ | |
2132 | memcpy(out, buf, EVP_CCM_TLS_EXPLICIT_IV_LEN); | |
2133 | } | |
2134 | ||
2135 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m; | |
2136 | /*- | |
2137 | * Get explicit iv (sequence number). We already have fixed iv | |
2138 | * (server/client_write_iv) here. | |
2139 | */ | |
2140 | memcpy(ivec + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN); | |
2141 | s390x_aes_ccm_setiv(cctx, ivec, len); | |
2142 | ||
2143 | /* Process aad (sequence number|type|version|length) */ | |
2144 | s390x_aes_ccm_aad(cctx, buf, cctx->aes.ccm.tls_aad_len); | |
2145 | ||
2146 | in += EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
2147 | out += EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
96530eea | 2148 | |
39f5b069 PS |
2149 | if (enc) { |
2150 | if (s390x_aes_ccm(cctx, in, out, len, enc)) | |
2151 | return -1; | |
2152 | ||
2153 | memcpy(out + len, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m); | |
2154 | return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m; | |
2155 | } else { | |
2156 | if (!s390x_aes_ccm(cctx, in, out, len, enc)) { | |
2157 | if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, in + len, | |
2158 | cctx->aes.ccm.m)) | |
2159 | return len; | |
2160 | } | |
2161 | ||
2162 | OPENSSL_cleanse(out, len); | |
2163 | return -1; | |
2164 | } | |
2165 | } | |
2166 | ||
2167 | /*- | |
2168 | * Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is | |
2169 | * returned. | |
2170 | */ | |
96530eea PS |
2171 | static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx, |
2172 | const unsigned char *key, | |
39f5b069 PS |
2173 | const unsigned char *iv, int enc) |
2174 | { | |
2175 | S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx); | |
2176 | unsigned char *ivec; | |
2177 | int keylen; | |
2178 | ||
2179 | if (iv == NULL && key == NULL) | |
2180 | return 1; | |
2181 | ||
2182 | if (key != NULL) { | |
2183 | keylen = EVP_CIPHER_CTX_key_length(ctx); | |
8eb399fb | 2184 | cctx->aes.ccm.fc = S390X_AES_FC(keylen); |
39f5b069 PS |
2185 | memcpy(cctx->aes.ccm.kmac_param.k, key, keylen); |
2186 | ||
2187 | /* Store encoded m and l. */ | |
2188 | cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7) | |
2189 | | (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3; | |
2190 | memset(cctx->aes.ccm.nonce.b + 1, 0, | |
2191 | sizeof(cctx->aes.ccm.nonce.b)); | |
2192 | cctx->aes.ccm.blocks = 0; | |
2193 | ||
2194 | cctx->aes.ccm.key_set = 1; | |
2195 | } | |
2196 | ||
2197 | if (iv != NULL) { | |
2198 | ivec = EVP_CIPHER_CTX_iv_noconst(ctx); | |
2199 | memcpy(ivec, iv, 15 - cctx->aes.ccm.l); | |
2200 | ||
2201 | cctx->aes.ccm.iv_set = 1; | |
2202 | } | |
2203 | ||
2204 | return 1; | |
2205 | } | |
2206 | ||
2207 | /*- | |
2208 | * Called from EVP layer to initialize context, process additional | |
2209 | * authenticated data, en/de-crypt plain/cipher-text and authenticate | |
2210 | * plaintext or process a TLS packet, depending on context. Returns bytes | |
2211 | * written on success. Otherwise -1 is returned. | |
2212 | */ | |
96530eea | 2213 | static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
39f5b069 PS |
2214 | const unsigned char *in, size_t len) |
2215 | { | |
2216 | S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx); | |
2217 | const int enc = EVP_CIPHER_CTX_encrypting(ctx); | |
2218 | int rv; | |
2219 | unsigned char *buf, *ivec; | |
2220 | ||
2221 | if (!cctx->aes.ccm.key_set) | |
2222 | return -1; | |
2223 | ||
2224 | if (cctx->aes.ccm.tls_aad_len >= 0) | |
2225 | return s390x_aes_ccm_tls_cipher(ctx, out, in, len); | |
2226 | ||
2227 | /*- | |
2228 | * Final(): Does not return any data. Recall that ccm is mac-then-encrypt | |
2229 | * so integrity must be checked already at Update() i.e., before | |
2230 | * potentially corrupted data is output. | |
2231 | */ | |
2232 | if (in == NULL && out != NULL) | |
2233 | return 0; | |
2234 | ||
2235 | if (!cctx->aes.ccm.iv_set) | |
2236 | return -1; | |
2237 | ||
2238 | if (!enc && !cctx->aes.ccm.tag_set) | |
2239 | return -1; | |
2240 | ||
2241 | if (out == NULL) { | |
2242 | /* Update(): Pass message length. */ | |
2243 | if (in == NULL) { | |
2244 | ivec = EVP_CIPHER_CTX_iv_noconst(ctx); | |
2245 | s390x_aes_ccm_setiv(cctx, ivec, len); | |
2246 | ||
2247 | cctx->aes.ccm.len_set = 1; | |
2248 | return len; | |
2249 | } | |
2250 | ||
2251 | /* Update(): Process aad. */ | |
2252 | if (!cctx->aes.ccm.len_set && len) | |
2253 | return -1; | |
2254 | ||
2255 | s390x_aes_ccm_aad(cctx, in, len); | |
2256 | return len; | |
2257 | } | |
2258 | ||
2259 | /* Update(): Process message. */ | |
2260 | ||
2261 | if (!cctx->aes.ccm.len_set) { | |
2262 | /*- | |
46d08509 | 2263 | * In case message length was not previously set explicitly via |
39f5b069 PS |
2264 | * Update(), set it now. |
2265 | */ | |
2266 | ivec = EVP_CIPHER_CTX_iv_noconst(ctx); | |
2267 | s390x_aes_ccm_setiv(cctx, ivec, len); | |
2268 | ||
2269 | cctx->aes.ccm.len_set = 1; | |
2270 | } | |
2271 | ||
2272 | if (enc) { | |
2273 | if (s390x_aes_ccm(cctx, in, out, len, enc)) | |
2274 | return -1; | |
2275 | ||
2276 | cctx->aes.ccm.tag_set = 1; | |
2277 | return len; | |
2278 | } else { | |
2279 | rv = -1; | |
2280 | ||
2281 | if (!s390x_aes_ccm(cctx, in, out, len, enc)) { | |
2282 | buf = EVP_CIPHER_CTX_buf_noconst(ctx); | |
2283 | if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, buf, | |
2284 | cctx->aes.ccm.m)) | |
2285 | rv = len; | |
2286 | } | |
2287 | ||
2288 | if (rv == -1) | |
2289 | OPENSSL_cleanse(out, len); | |
2290 | ||
2291 | cctx->aes.ccm.iv_set = 0; | |
2292 | cctx->aes.ccm.tag_set = 0; | |
2293 | cctx->aes.ccm.len_set = 0; | |
2294 | return rv; | |
2295 | } | |
2296 | } | |
2297 | ||
2298 | /*- | |
2299 | * Performs various operations on the context structure depending on control | |
2300 | * type. Returns 1 for success, 0 for failure and -1 for unknown control type. | |
2301 | * Code is big-endian. | |
2302 | */ | |
2303 | static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |
2304 | { | |
2305 | S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, c); | |
2306 | unsigned char *buf, *iv; | |
2307 | int enc, len; | |
2308 | ||
2309 | switch (type) { | |
2310 | case EVP_CTRL_INIT: | |
2311 | cctx->aes.ccm.key_set = 0; | |
2312 | cctx->aes.ccm.iv_set = 0; | |
2313 | cctx->aes.ccm.l = 8; | |
2314 | cctx->aes.ccm.m = 12; | |
2315 | cctx->aes.ccm.tag_set = 0; | |
2316 | cctx->aes.ccm.len_set = 0; | |
2317 | cctx->aes.ccm.tls_aad_len = -1; | |
2318 | return 1; | |
2319 | ||
2320 | case EVP_CTRL_AEAD_TLS1_AAD: | |
2321 | if (arg != EVP_AEAD_TLS1_AAD_LEN) | |
2322 | return 0; | |
2323 | ||
2324 | /* Save the aad for later use. */ | |
2325 | buf = EVP_CIPHER_CTX_buf_noconst(c); | |
2326 | memcpy(buf, ptr, arg); | |
2327 | cctx->aes.ccm.tls_aad_len = arg; | |
2328 | ||
03a5e5ae | 2329 | len = buf[arg - 2] << 8 | buf[arg - 1]; |
39f5b069 PS |
2330 | if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN) |
2331 | return 0; | |
2332 | ||
2333 | /* Correct length for explicit iv. */ | |
2334 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
2335 | ||
2336 | enc = EVP_CIPHER_CTX_encrypting(c); | |
2337 | if (!enc) { | |
2338 | if (len < cctx->aes.ccm.m) | |
2339 | return 0; | |
2340 | ||
2341 | /* Correct length for tag. */ | |
2342 | len -= cctx->aes.ccm.m; | |
2343 | } | |
2344 | ||
03a5e5ae PS |
2345 | buf[arg - 2] = len >> 8; |
2346 | buf[arg - 1] = len & 0xff; | |
2347 | ||
39f5b069 PS |
2348 | /* Extra padding: tag appended to record. */ |
2349 | return cctx->aes.ccm.m; | |
2350 | ||
2351 | case EVP_CTRL_CCM_SET_IV_FIXED: | |
2352 | if (arg != EVP_CCM_TLS_FIXED_IV_LEN) | |
2353 | return 0; | |
2354 | ||
2355 | /* Copy to first part of the iv. */ | |
2356 | iv = EVP_CIPHER_CTX_iv_noconst(c); | |
2357 | memcpy(iv, ptr, arg); | |
2358 | return 1; | |
2359 | ||
2360 | case EVP_CTRL_AEAD_SET_IVLEN: | |
2361 | arg = 15 - arg; | |
2362 | /* fall-through */ | |
2363 | ||
2364 | case EVP_CTRL_CCM_SET_L: | |
2365 | if (arg < 2 || arg > 8) | |
2366 | return 0; | |
2367 | ||
2368 | cctx->aes.ccm.l = arg; | |
2369 | return 1; | |
2370 | ||
2371 | case EVP_CTRL_AEAD_SET_TAG: | |
2372 | if ((arg & 1) || arg < 4 || arg > 16) | |
2373 | return 0; | |
2374 | ||
2375 | enc = EVP_CIPHER_CTX_encrypting(c); | |
2376 | if (enc && ptr) | |
2377 | return 0; | |
2378 | ||
2379 | if (ptr) { | |
2380 | cctx->aes.ccm.tag_set = 1; | |
2381 | buf = EVP_CIPHER_CTX_buf_noconst(c); | |
2382 | memcpy(buf, ptr, arg); | |
2383 | } | |
2384 | ||
2385 | cctx->aes.ccm.m = arg; | |
2386 | return 1; | |
2387 | ||
2388 | case EVP_CTRL_AEAD_GET_TAG: | |
2389 | enc = EVP_CIPHER_CTX_encrypting(c); | |
2390 | if (!enc || !cctx->aes.ccm.tag_set) | |
2391 | return 0; | |
2392 | ||
2393 | if(arg < cctx->aes.ccm.m) | |
2394 | return 0; | |
2395 | ||
2396 | memcpy(ptr, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m); | |
2397 | cctx->aes.ccm.tag_set = 0; | |
2398 | cctx->aes.ccm.iv_set = 0; | |
2399 | cctx->aes.ccm.len_set = 0; | |
2400 | return 1; | |
2401 | ||
2402 | case EVP_CTRL_COPY: | |
2403 | return 1; | |
2404 | ||
2405 | default: | |
2406 | return -1; | |
2407 | } | |
2408 | } | |
2409 | ||
96530eea PS |
2410 | # define s390x_aes_ccm_cleanup aes_ccm_cleanup |
2411 | ||
2412 | # ifndef OPENSSL_NO_OCB | |
2413 | # define S390X_AES_OCB_CTX EVP_AES_OCB_CTX | |
2414 | # define S390X_aes_128_ocb_CAPABLE 0 | |
2415 | # define S390X_aes_192_ocb_CAPABLE 0 | |
2416 | # define S390X_aes_256_ocb_CAPABLE 0 | |
2417 | ||
2418 | # define s390x_aes_ocb_init_key aes_ocb_init_key | |
2419 | static int s390x_aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
2420 | const unsigned char *iv, int enc); | |
2421 | # define s390x_aes_ocb_cipher aes_ocb_cipher | |
2422 | static int s390x_aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
2423 | const unsigned char *in, size_t len); | |
2424 | # define s390x_aes_ocb_cleanup aes_ocb_cleanup | |
2425 | static int s390x_aes_ocb_cleanup(EVP_CIPHER_CTX *); | |
2426 | # define s390x_aes_ocb_ctrl aes_ocb_ctrl | |
2427 | static int s390x_aes_ocb_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr); | |
2428 | # endif | |
2429 | ||
2430 | # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode, \ | |
2431 | MODE,flags) \ | |
2432 | static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \ | |
2433 | nid##_##keylen##_##nmode,blocksize, \ | |
2434 | keylen / 8, \ | |
2435 | ivlen, \ | |
2436 | flags | EVP_CIPH_##MODE##_MODE, \ | |
55bd169f | 2437 | s390x_aes_##mode##_init_key, \ |
96530eea PS |
2438 | s390x_aes_##mode##_cipher, \ |
2439 | NULL, \ | |
55bd169f | 2440 | sizeof(S390X_AES_##MODE##_CTX), \ |
96530eea PS |
2441 | NULL, \ |
2442 | NULL, \ | |
2443 | NULL, \ | |
2444 | NULL \ | |
2445 | }; \ | |
2446 | static const EVP_CIPHER aes_##keylen##_##mode = { \ | |
2447 | nid##_##keylen##_##nmode, \ | |
2448 | blocksize, \ | |
2449 | keylen / 8, \ | |
2450 | ivlen, \ | |
2451 | flags | EVP_CIPH_##MODE##_MODE, \ | |
2452 | aes_init_key, \ | |
2453 | aes_##mode##_cipher, \ | |
2454 | NULL, \ | |
2455 | sizeof(EVP_AES_KEY), \ | |
55bd169f PS |
2456 | NULL, \ |
2457 | NULL, \ | |
2458 | NULL, \ | |
2459 | NULL \ | |
96530eea PS |
2460 | }; \ |
2461 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ | |
2462 | { \ | |
2463 | return S390X_aes_##keylen##_##mode##_CAPABLE ? \ | |
2464 | &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \ | |
2465 | } | |
2466 | ||
2467 | # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\ | |
2468 | static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \ | |
2469 | nid##_##keylen##_##mode, \ | |
2470 | blocksize, \ | |
2471 | (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8, \ | |
2472 | ivlen, \ | |
2473 | flags | EVP_CIPH_##MODE##_MODE, \ | |
2474 | s390x_aes_##mode##_init_key, \ | |
2475 | s390x_aes_##mode##_cipher, \ | |
2476 | s390x_aes_##mode##_cleanup, \ | |
2477 | sizeof(S390X_AES_##MODE##_CTX), \ | |
2478 | NULL, \ | |
2479 | NULL, \ | |
2480 | s390x_aes_##mode##_ctrl, \ | |
2481 | NULL \ | |
2482 | }; \ | |
2483 | static const EVP_CIPHER aes_##keylen##_##mode = { \ | |
2484 | nid##_##keylen##_##mode,blocksize, \ | |
2485 | (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8, \ | |
2486 | ivlen, \ | |
2487 | flags | EVP_CIPH_##MODE##_MODE, \ | |
2488 | aes_##mode##_init_key, \ | |
2489 | aes_##mode##_cipher, \ | |
2490 | aes_##mode##_cleanup, \ | |
2491 | sizeof(EVP_AES_##MODE##_CTX), \ | |
2492 | NULL, \ | |
2493 | NULL, \ | |
2494 | aes_##mode##_ctrl, \ | |
2495 | NULL \ | |
2496 | }; \ | |
2497 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ | |
2498 | { \ | |
2499 | return S390X_aes_##keylen##_##mode##_CAPABLE ? \ | |
2500 | &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \ | |
2501 | } | |
2502 | ||
5158c763 | 2503 | #else |
17f121de | 2504 | |
5158c763 | 2505 | # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ |
17f121de | 2506 | static const EVP_CIPHER aes_##keylen##_##mode = { \ |
0f113f3e MC |
2507 | nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \ |
2508 | flags|EVP_CIPH_##MODE##_MODE, \ | |
2509 | aes_init_key, \ | |
2510 | aes_##mode##_cipher, \ | |
2511 | NULL, \ | |
2512 | sizeof(EVP_AES_KEY), \ | |
2513 | NULL,NULL,NULL,NULL }; \ | |
17f121de AP |
2514 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ |
2515 | { return &aes_##keylen##_##mode; } | |
d1fff483 | 2516 | |
5158c763 | 2517 | # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \ |
17f121de | 2518 | static const EVP_CIPHER aes_##keylen##_##mode = { \ |
0f113f3e | 2519 | nid##_##keylen##_##mode,blocksize, \ |
b1ceb439 TS |
2520 | (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE||EVP_CIPH_##MODE##_MODE==EVP_CIPH_SIV_MODE?2:1)*keylen/8, \ |
2521 | ivlen, \ | |
0f113f3e MC |
2522 | flags|EVP_CIPH_##MODE##_MODE, \ |
2523 | aes_##mode##_init_key, \ | |
2524 | aes_##mode##_cipher, \ | |
2525 | aes_##mode##_cleanup, \ | |
2526 | sizeof(EVP_AES_##MODE##_CTX), \ | |
2527 | NULL,NULL,aes_##mode##_ctrl,NULL }; \ | |
17f121de AP |
2528 | const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ |
2529 | { return &aes_##keylen##_##mode; } | |
9575d1a9 | 2530 | |
5158c763 | 2531 | #endif |
9575d1a9 | 2532 | |
5158c763 MC |
2533 | #if defined(OPENSSL_CPUID_OBJ) && (defined(__arm__) || defined(__arm) || defined(__aarch64__)) |
2534 | # include "arm_arch.h" | |
2535 | # if __ARM_MAX_ARCH__>=7 | |
2536 | # if defined(BSAES_ASM) | |
2537 | # define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON) | |
2538 | # endif | |
2539 | # if defined(VPAES_ASM) | |
2540 | # define VPAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON) | |
0f113f3e | 2541 | # endif |
5158c763 MC |
2542 | # define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES) |
2543 | # define HWAES_set_encrypt_key aes_v8_set_encrypt_key | |
2544 | # define HWAES_set_decrypt_key aes_v8_set_decrypt_key | |
2545 | # define HWAES_encrypt aes_v8_encrypt | |
2546 | # define HWAES_decrypt aes_v8_decrypt | |
2547 | # define HWAES_cbc_encrypt aes_v8_cbc_encrypt | |
2548 | # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks | |
ddacb8f2 | 2549 | # endif |
5158c763 | 2550 | #endif |
d1fff483 | 2551 | |
5158c763 | 2552 | #if defined(HWAES_CAPABLE) |
ddacb8f2 | 2553 | int HWAES_set_encrypt_key(const unsigned char *userKey, const int bits, |
0f113f3e | 2554 | AES_KEY *key); |
ddacb8f2 | 2555 | int HWAES_set_decrypt_key(const unsigned char *userKey, const int bits, |
0f113f3e | 2556 | AES_KEY *key); |
ddacb8f2 | 2557 | void HWAES_encrypt(const unsigned char *in, unsigned char *out, |
0f113f3e | 2558 | const AES_KEY *key); |
ddacb8f2 | 2559 | void HWAES_decrypt(const unsigned char *in, unsigned char *out, |
0f113f3e | 2560 | const AES_KEY *key); |
ddacb8f2 | 2561 | void HWAES_cbc_encrypt(const unsigned char *in, unsigned char *out, |
0f113f3e MC |
2562 | size_t length, const AES_KEY *key, |
2563 | unsigned char *ivec, const int enc); | |
ddacb8f2 | 2564 | void HWAES_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
0f113f3e MC |
2565 | size_t len, const AES_KEY *key, |
2566 | const unsigned char ivec[16]); | |
46f047d7 AP |
2567 | void HWAES_xts_encrypt(const unsigned char *inp, unsigned char *out, |
2568 | size_t len, const AES_KEY *key1, | |
2569 | const AES_KEY *key2, const unsigned char iv[16]); | |
2570 | void HWAES_xts_decrypt(const unsigned char *inp, unsigned char *out, | |
2571 | size_t len, const AES_KEY *key1, | |
2572 | const AES_KEY *key2, const unsigned char iv[16]); | |
5158c763 | 2573 | #endif |
ddacb8f2 | 2574 | |
5158c763 | 2575 | #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \ |
0f113f3e MC |
2576 | BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ |
2577 | BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ | |
2578 | BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ | |
2579 | BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \ | |
2580 | BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \ | |
2581 | BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \ | |
2582 | BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags) | |
d1fff483 AP |
2583 | |
2584 | static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
2585 | const unsigned char *iv, int enc) |
2586 | { | |
2587 | int ret, mode; | |
6435f0f6 | 2588 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
0f113f3e | 2589 | |
6435f0f6 | 2590 | mode = EVP_CIPHER_CTX_mode(ctx); |
0f113f3e | 2591 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) |
c01a3c6d | 2592 | && !enc) { |
5158c763 | 2593 | #ifdef HWAES_CAPABLE |
0f113f3e | 2594 | if (HWAES_CAPABLE) { |
6435f0f6 RL |
2595 | ret = HWAES_set_decrypt_key(key, |
2596 | EVP_CIPHER_CTX_key_length(ctx) * 8, | |
2597 | &dat->ks.ks); | |
0f113f3e MC |
2598 | dat->block = (block128_f) HWAES_decrypt; |
2599 | dat->stream.cbc = NULL; | |
5158c763 | 2600 | # ifdef HWAES_cbc_encrypt |
0f113f3e MC |
2601 | if (mode == EVP_CIPH_CBC_MODE) |
2602 | dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt; | |
0f113f3e | 2603 | # endif |
5158c763 MC |
2604 | } else |
2605 | #endif | |
2606 | #ifdef BSAES_CAPABLE | |
0f113f3e | 2607 | if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) { |
6435f0f6 RL |
2608 | ret = AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
2609 | &dat->ks.ks); | |
0f113f3e MC |
2610 | dat->block = (block128_f) AES_decrypt; |
2611 | dat->stream.cbc = (cbc128_f) bsaes_cbc_encrypt; | |
2612 | } else | |
5158c763 MC |
2613 | #endif |
2614 | #ifdef VPAES_CAPABLE | |
0f113f3e | 2615 | if (VPAES_CAPABLE) { |
6435f0f6 RL |
2616 | ret = vpaes_set_decrypt_key(key, |
2617 | EVP_CIPHER_CTX_key_length(ctx) * 8, | |
2618 | &dat->ks.ks); | |
0f113f3e MC |
2619 | dat->block = (block128_f) vpaes_decrypt; |
2620 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
2621 | (cbc128_f) vpaes_cbc_encrypt : NULL; | |
2622 | } else | |
5158c763 | 2623 | #endif |
0f113f3e | 2624 | { |
6435f0f6 RL |
2625 | ret = AES_set_decrypt_key(key, |
2626 | EVP_CIPHER_CTX_key_length(ctx) * 8, | |
2627 | &dat->ks.ks); | |
0f113f3e MC |
2628 | dat->block = (block128_f) AES_decrypt; |
2629 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
2630 | (cbc128_f) AES_cbc_encrypt : NULL; | |
c01a3c6d | 2631 | } |
0f113f3e | 2632 | } else |
5158c763 | 2633 | #ifdef HWAES_CAPABLE |
0f113f3e | 2634 | if (HWAES_CAPABLE) { |
6435f0f6 RL |
2635 | ret = HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
2636 | &dat->ks.ks); | |
0f113f3e MC |
2637 | dat->block = (block128_f) HWAES_encrypt; |
2638 | dat->stream.cbc = NULL; | |
5158c763 | 2639 | # ifdef HWAES_cbc_encrypt |
0f113f3e MC |
2640 | if (mode == EVP_CIPH_CBC_MODE) |
2641 | dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt; | |
2642 | else | |
5158c763 MC |
2643 | # endif |
2644 | # ifdef HWAES_ctr32_encrypt_blocks | |
0f113f3e MC |
2645 | if (mode == EVP_CIPH_CTR_MODE) |
2646 | dat->stream.ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks; | |
2647 | else | |
5158c763 | 2648 | # endif |
0f113f3e MC |
2649 | (void)0; /* terminate potentially open 'else' */ |
2650 | } else | |
5158c763 MC |
2651 | #endif |
2652 | #ifdef BSAES_CAPABLE | |
0f113f3e | 2653 | if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) { |
6435f0f6 RL |
2654 | ret = AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
2655 | &dat->ks.ks); | |
0f113f3e MC |
2656 | dat->block = (block128_f) AES_encrypt; |
2657 | dat->stream.ctr = (ctr128_f) bsaes_ctr32_encrypt_blocks; | |
2658 | } else | |
5158c763 MC |
2659 | #endif |
2660 | #ifdef VPAES_CAPABLE | |
0f113f3e | 2661 | if (VPAES_CAPABLE) { |
6435f0f6 RL |
2662 | ret = vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
2663 | &dat->ks.ks); | |
0f113f3e MC |
2664 | dat->block = (block128_f) vpaes_encrypt; |
2665 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
2666 | (cbc128_f) vpaes_cbc_encrypt : NULL; | |
2667 | } else | |
5158c763 | 2668 | #endif |
0f113f3e | 2669 | { |
6435f0f6 RL |
2670 | ret = AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
2671 | &dat->ks.ks); | |
0f113f3e MC |
2672 | dat->block = (block128_f) AES_encrypt; |
2673 | dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? | |
2674 | (cbc128_f) AES_cbc_encrypt : NULL; | |
5158c763 | 2675 | #ifdef AES_CTR_ASM |
0f113f3e MC |
2676 | if (mode == EVP_CIPH_CTR_MODE) |
2677 | dat->stream.ctr = (ctr128_f) AES_ctr32_encrypt; | |
5158c763 | 2678 | #endif |
0f113f3e | 2679 | } |
d1fff483 | 2680 | |
0f113f3e MC |
2681 | if (ret < 0) { |
2682 | EVPerr(EVP_F_AES_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); | |
2683 | return 0; | |
2684 | } | |
d1fff483 | 2685 | |
0f113f3e MC |
2686 | return 1; |
2687 | } | |
d1fff483 | 2688 | |
0f113f3e MC |
2689 | static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2690 | const unsigned char *in, size_t len) | |
17f121de | 2691 | { |
6435f0f6 | 2692 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
8ca28da0 | 2693 | |
0f113f3e | 2694 | if (dat->stream.cbc) |
6435f0f6 RL |
2695 | (*dat->stream.cbc) (in, out, len, &dat->ks, |
2696 | EVP_CIPHER_CTX_iv_noconst(ctx), | |
2697 | EVP_CIPHER_CTX_encrypting(ctx)); | |
2698 | else if (EVP_CIPHER_CTX_encrypting(ctx)) | |
2699 | CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, | |
2700 | EVP_CIPHER_CTX_iv_noconst(ctx), dat->block); | |
0f113f3e | 2701 | else |
6435f0f6 RL |
2702 | CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, |
2703 | EVP_CIPHER_CTX_iv_noconst(ctx), dat->block); | |
17f121de | 2704 | |
0f113f3e | 2705 | return 1; |
17f121de AP |
2706 | } |
2707 | ||
0f113f3e MC |
2708 | static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2709 | const unsigned char *in, size_t len) | |
17f121de | 2710 | { |
6435f0f6 | 2711 | size_t bl = EVP_CIPHER_CTX_block_size(ctx); |
0f113f3e | 2712 | size_t i; |
6435f0f6 | 2713 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
17f121de | 2714 | |
0f113f3e MC |
2715 | if (len < bl) |
2716 | return 1; | |
17f121de | 2717 | |
0f113f3e MC |
2718 | for (i = 0, len -= bl; i <= len; i += bl) |
2719 | (*dat->block) (in + i, out + i, &dat->ks); | |
17f121de | 2720 | |
0f113f3e | 2721 | return 1; |
17f121de | 2722 | } |
deb2c1a1 | 2723 | |
0f113f3e MC |
2724 | static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2725 | const unsigned char *in, size_t len) | |
17f121de | 2726 | { |
6435f0f6 | 2727 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
8ca28da0 | 2728 | |
6435f0f6 | 2729 | int num = EVP_CIPHER_CTX_num(ctx); |
0f113f3e | 2730 | CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, |
6435f0f6 RL |
2731 | EVP_CIPHER_CTX_iv_noconst(ctx), &num, dat->block); |
2732 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e | 2733 | return 1; |
17f121de | 2734 | } |
deb2c1a1 | 2735 | |
0f113f3e MC |
2736 | static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2737 | const unsigned char *in, size_t len) | |
17f121de | 2738 | { |
6435f0f6 | 2739 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
8ca28da0 | 2740 | |
6435f0f6 | 2741 | int num = EVP_CIPHER_CTX_num(ctx); |
0f113f3e | 2742 | CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, |
6435f0f6 RL |
2743 | EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
2744 | EVP_CIPHER_CTX_encrypting(ctx), dat->block); | |
2745 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e | 2746 | return 1; |
17f121de AP |
2747 | } |
2748 | ||
0f113f3e MC |
2749 | static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2750 | const unsigned char *in, size_t len) | |
17f121de | 2751 | { |
6435f0f6 | 2752 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
8ca28da0 | 2753 | |
6435f0f6 | 2754 | int num = EVP_CIPHER_CTX_num(ctx); |
0f113f3e | 2755 | CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, |
6435f0f6 RL |
2756 | EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
2757 | EVP_CIPHER_CTX_encrypting(ctx), dat->block); | |
2758 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e | 2759 | return 1; |
17f121de | 2760 | } |
8d1ebe0b | 2761 | |
0f113f3e MC |
2762 | static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2763 | const unsigned char *in, size_t len) | |
17f121de | 2764 | { |
6435f0f6 | 2765 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); |
0f113f3e | 2766 | |
6435f0f6 RL |
2767 | if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) { |
2768 | int num = EVP_CIPHER_CTX_num(ctx); | |
0f113f3e | 2769 | CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, |
6435f0f6 RL |
2770 | EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
2771 | EVP_CIPHER_CTX_encrypting(ctx), dat->block); | |
2772 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e MC |
2773 | return 1; |
2774 | } | |
2775 | ||
2776 | while (len >= MAXBITCHUNK) { | |
6435f0f6 | 2777 | int num = EVP_CIPHER_CTX_num(ctx); |
0f113f3e | 2778 | CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks, |
6435f0f6 RL |
2779 | EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
2780 | EVP_CIPHER_CTX_encrypting(ctx), dat->block); | |
2781 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e | 2782 | len -= MAXBITCHUNK; |
604e591e BE |
2783 | out += MAXBITCHUNK; |
2784 | in += MAXBITCHUNK; | |
0f113f3e | 2785 | } |
6435f0f6 RL |
2786 | if (len) { |
2787 | int num = EVP_CIPHER_CTX_num(ctx); | |
0f113f3e | 2788 | CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks, |
6435f0f6 RL |
2789 | EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
2790 | EVP_CIPHER_CTX_encrypting(ctx), dat->block); | |
2791 | EVP_CIPHER_CTX_set_num(ctx, num); | |
2792 | } | |
0f113f3e MC |
2793 | |
2794 | return 1; | |
17f121de | 2795 | } |
8d1ebe0b | 2796 | |
0f113f3e MC |
2797 | static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
2798 | const unsigned char *in, size_t len) | |
d976f992 | 2799 | { |
6435f0f6 RL |
2800 | unsigned int num = EVP_CIPHER_CTX_num(ctx); |
2801 | EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx); | |
0f113f3e MC |
2802 | |
2803 | if (dat->stream.ctr) | |
2804 | CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, | |
6435f0f6 RL |
2805 | EVP_CIPHER_CTX_iv_noconst(ctx), |
2806 | EVP_CIPHER_CTX_buf_noconst(ctx), | |
2807 | &num, dat->stream.ctr); | |
0f113f3e MC |
2808 | else |
2809 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, | |
6435f0f6 RL |
2810 | EVP_CIPHER_CTX_iv_noconst(ctx), |
2811 | EVP_CIPHER_CTX_buf_noconst(ctx), &num, | |
2812 | dat->block); | |
2813 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e | 2814 | return 1; |
d976f992 AP |
2815 | } |
2816 | ||
0f113f3e MC |
2817 | BLOCK_CIPHER_generic_pack(NID_aes, 128, 0) |
2818 | BLOCK_CIPHER_generic_pack(NID_aes, 192, 0) | |
2819 | BLOCK_CIPHER_generic_pack(NID_aes, 256, 0) | |
bdaa5415 DSH |
2820 | |
2821 | static int aes_gcm_cleanup(EVP_CIPHER_CTX *c) | |
0f113f3e | 2822 | { |
6435f0f6 | 2823 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c); |
273a0218 BE |
2824 | if (gctx == NULL) |
2825 | return 0; | |
0f113f3e | 2826 | OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); |
6435f0f6 | 2827 | if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c)) |
0f113f3e MC |
2828 | OPENSSL_free(gctx->iv); |
2829 | return 1; | |
2830 | } | |
bdaa5415 DSH |
2831 | |
2832 | static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |
0f113f3e | 2833 | { |
6435f0f6 | 2834 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c); |
0f113f3e MC |
2835 | switch (type) { |
2836 | case EVP_CTRL_INIT: | |
2837 | gctx->key_set = 0; | |
2838 | gctx->iv_set = 0; | |
c5307d9c AP |
2839 | gctx->ivlen = c->cipher->iv_len; |
2840 | gctx->iv = c->iv; | |
0f113f3e MC |
2841 | gctx->taglen = -1; |
2842 | gctx->iv_gen = 0; | |
2843 | gctx->tls_aad_len = -1; | |
2844 | return 1; | |
2845 | ||
e640fa02 | 2846 | case EVP_CTRL_AEAD_SET_IVLEN: |
0f113f3e MC |
2847 | if (arg <= 0) |
2848 | return 0; | |
2849 | /* Allocate memory for IV if needed */ | |
2850 | if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { | |
c5307d9c | 2851 | if (gctx->iv != c->iv) |
0f113f3e | 2852 | OPENSSL_free(gctx->iv); |
cdb10bae RS |
2853 | if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) { |
2854 | EVPerr(EVP_F_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 2855 | return 0; |
cdb10bae | 2856 | } |
0f113f3e MC |
2857 | } |
2858 | gctx->ivlen = arg; | |
2859 | return 1; | |
2860 | ||
e640fa02 | 2861 | case EVP_CTRL_AEAD_SET_TAG: |
c5307d9c | 2862 | if (arg <= 0 || arg > 16 || c->encrypt) |
0f113f3e | 2863 | return 0; |
c5307d9c | 2864 | memcpy(c->buf, ptr, arg); |
0f113f3e MC |
2865 | gctx->taglen = arg; |
2866 | return 1; | |
2867 | ||
e640fa02 | 2868 | case EVP_CTRL_AEAD_GET_TAG: |
c5307d9c | 2869 | if (arg <= 0 || arg > 16 || !c->encrypt |
6435f0f6 | 2870 | || gctx->taglen < 0) |
0f113f3e | 2871 | return 0; |
c5307d9c | 2872 | memcpy(ptr, c->buf, arg); |
0f113f3e MC |
2873 | return 1; |
2874 | ||
ecd1557f BP |
2875 | case EVP_CTRL_GET_IV: |
2876 | if (gctx->iv_gen != 1) | |
2877 | return 0; | |
2878 | if (gctx->ivlen != arg) | |
2879 | return 0; | |
2880 | memcpy(ptr, gctx->iv, arg); | |
2881 | return 1; | |
2882 | ||
0f113f3e MC |
2883 | case EVP_CTRL_GCM_SET_IV_FIXED: |
2884 | /* Special case: -1 length restores whole IV */ | |
2885 | if (arg == -1) { | |
2886 | memcpy(gctx->iv, ptr, gctx->ivlen); | |
2887 | gctx->iv_gen = 1; | |
2888 | return 1; | |
2889 | } | |
2890 | /* | |
2891 | * Fixed field must be at least 4 bytes and invocation field at least | |
2892 | * 8. | |
2893 | */ | |
2894 | if ((arg < 4) || (gctx->ivlen - arg) < 8) | |
2895 | return 0; | |
2896 | if (arg) | |
2897 | memcpy(gctx->iv, ptr, arg); | |
c5307d9c | 2898 | if (c->encrypt && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) |
16cfc2c9 | 2899 | return 0; |
0f113f3e MC |
2900 | gctx->iv_gen = 1; |
2901 | return 1; | |
2902 | ||
2903 | case EVP_CTRL_GCM_IV_GEN: | |
2904 | if (gctx->iv_gen == 0 || gctx->key_set == 0) | |
2905 | return 0; | |
2906 | CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); | |
2907 | if (arg <= 0 || arg > gctx->ivlen) | |
2908 | arg = gctx->ivlen; | |
2909 | memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); | |
2910 | /* | |
2911 | * Invocation field will be at least 8 bytes in size and so no need | |
2912 | * to check wrap around or increment more than last 8 bytes. | |
2913 | */ | |
2914 | ctr64_inc(gctx->iv + gctx->ivlen - 8); | |
2915 | gctx->iv_set = 1; | |
2916 | return 1; | |
2917 | ||
2918 | case EVP_CTRL_GCM_SET_IV_INV: | |
c5307d9c | 2919 | if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) |
0f113f3e MC |
2920 | return 0; |
2921 | memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); | |
2922 | CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); | |
2923 | gctx->iv_set = 1; | |
2924 | return 1; | |
2925 | ||
2926 | case EVP_CTRL_AEAD_TLS1_AAD: | |
2927 | /* Save the AAD for later use */ | |
c8269881 | 2928 | if (arg != EVP_AEAD_TLS1_AAD_LEN) |
0f113f3e | 2929 | return 0; |
c5307d9c | 2930 | memcpy(c->buf, ptr, arg); |
0f113f3e | 2931 | gctx->tls_aad_len = arg; |
d6b34570 | 2932 | gctx->tls_enc_records = 0; |
0f113f3e | 2933 | { |
c5307d9c | 2934 | unsigned int len = c->buf[arg - 2] << 8 | c->buf[arg - 1]; |
0f113f3e | 2935 | /* Correct length for explicit IV */ |
2198b3a5 AP |
2936 | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
2937 | return 0; | |
0f113f3e MC |
2938 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
2939 | /* If decrypting correct for tag too */ | |
c5307d9c | 2940 | if (!c->encrypt) { |
2198b3a5 AP |
2941 | if (len < EVP_GCM_TLS_TAG_LEN) |
2942 | return 0; | |
0f113f3e | 2943 | len -= EVP_GCM_TLS_TAG_LEN; |
2198b3a5 | 2944 | } |
c5307d9c AP |
2945 | c->buf[arg - 2] = len >> 8; |
2946 | c->buf[arg - 1] = len & 0xff; | |
0f113f3e MC |
2947 | } |
2948 | /* Extra padding: tag appended to record */ | |
2949 | return EVP_GCM_TLS_TAG_LEN; | |
2950 | ||
2951 | case EVP_CTRL_COPY: | |
2952 | { | |
2953 | EVP_CIPHER_CTX *out = ptr; | |
6435f0f6 | 2954 | EVP_AES_GCM_CTX *gctx_out = EVP_C_DATA(EVP_AES_GCM_CTX,out); |
0f113f3e MC |
2955 | if (gctx->gcm.key) { |
2956 | if (gctx->gcm.key != &gctx->ks) | |
2957 | return 0; | |
2958 | gctx_out->gcm.key = &gctx_out->ks; | |
2959 | } | |
c5307d9c AP |
2960 | if (gctx->iv == c->iv) |
2961 | gctx_out->iv = out->iv; | |
0f113f3e | 2962 | else { |
cdb10bae RS |
2963 | if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) { |
2964 | EVPerr(EVP_F_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 2965 | return 0; |
cdb10bae | 2966 | } |
0f113f3e MC |
2967 | memcpy(gctx_out->iv, gctx->iv, gctx->ivlen); |
2968 | } | |
2969 | return 1; | |
2970 | } | |
2971 | ||
2972 | default: | |
2973 | return -1; | |
2974 | ||
2975 | } | |
2976 | } | |
bdaa5415 DSH |
2977 | |
2978 | static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
2979 | const unsigned char *iv, int enc) |
2980 | { | |
6435f0f6 | 2981 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx); |
0f113f3e MC |
2982 | if (!iv && !key) |
2983 | return 1; | |
2984 | if (key) { | |
2985 | do { | |
5158c763 | 2986 | #ifdef HWAES_CAPABLE |
0f113f3e | 2987 | if (HWAES_CAPABLE) { |
c5307d9c | 2988 | HWAES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks); |
0f113f3e MC |
2989 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
2990 | (block128_f) HWAES_encrypt); | |
5158c763 | 2991 | # ifdef HWAES_ctr32_encrypt_blocks |
0f113f3e | 2992 | gctx->ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks; |
5158c763 | 2993 | # else |
0f113f3e | 2994 | gctx->ctr = NULL; |
5158c763 | 2995 | # endif |
0f113f3e MC |
2996 | break; |
2997 | } else | |
5158c763 MC |
2998 | #endif |
2999 | #ifdef BSAES_CAPABLE | |
0f113f3e | 3000 | if (BSAES_CAPABLE) { |
c5307d9c | 3001 | AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks); |
0f113f3e MC |
3002 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
3003 | (block128_f) AES_encrypt); | |
3004 | gctx->ctr = (ctr128_f) bsaes_ctr32_encrypt_blocks; | |
3005 | break; | |
3006 | } else | |
5158c763 MC |
3007 | #endif |
3008 | #ifdef VPAES_CAPABLE | |
0f113f3e | 3009 | if (VPAES_CAPABLE) { |
c5307d9c | 3010 | vpaes_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks); |
0f113f3e MC |
3011 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
3012 | (block128_f) vpaes_encrypt); | |
3013 | gctx->ctr = NULL; | |
3014 | break; | |
3015 | } else | |
5158c763 | 3016 | #endif |
0f113f3e MC |
3017 | (void)0; /* terminate potentially open 'else' */ |
3018 | ||
c5307d9c | 3019 | AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks); |
0f113f3e MC |
3020 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
3021 | (block128_f) AES_encrypt); | |
5158c763 | 3022 | #ifdef AES_CTR_ASM |
0f113f3e | 3023 | gctx->ctr = (ctr128_f) AES_ctr32_encrypt; |
5158c763 | 3024 | #else |
0f113f3e | 3025 | gctx->ctr = NULL; |
5158c763 | 3026 | #endif |
0f113f3e MC |
3027 | } while (0); |
3028 | ||
3029 | /* | |
3030 | * If we have an iv can set it directly, otherwise use saved IV. | |
3031 | */ | |
3032 | if (iv == NULL && gctx->iv_set) | |
3033 | iv = gctx->iv; | |
3034 | if (iv) { | |
3035 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | |
3036 | gctx->iv_set = 1; | |
3037 | } | |
3038 | gctx->key_set = 1; | |
3039 | } else { | |
3040 | /* If key set use IV, otherwise copy */ | |
3041 | if (gctx->key_set) | |
3042 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); | |
3043 | else | |
3044 | memcpy(gctx->iv, iv, gctx->ivlen); | |
3045 | gctx->iv_set = 1; | |
3046 | gctx->iv_gen = 0; | |
3047 | } | |
3048 | return 1; | |
3049 | } | |
3050 | ||
3051 | /* | |
3052 | * Handle TLS GCM packet format. This consists of the last portion of the IV | |
28dd49fa DSH |
3053 | * followed by the payload and finally the tag. On encrypt generate IV, |
3054 | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload | |
3055 | * and verify tag. | |
3056 | */ | |
3057 | ||
3058 | static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
3059 | const unsigned char *in, size_t len) |
3060 | { | |
6435f0f6 | 3061 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx); |
0f113f3e MC |
3062 | int rv = -1; |
3063 | /* Encrypt/decrypt must be performed in place */ | |
3064 | if (out != in | |
3065 | || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) | |
3066 | return -1; | |
d6b34570 P |
3067 | |
3068 | /* | |
3069 | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness | |
3070 | * Requirements from SP 800-38D". The requirements is for one party to the | |
3071 | * communication to fail after 2^64 - 1 keys. We do this on the encrypting | |
3072 | * side only. | |
3073 | */ | |
3074 | if (ctx->encrypt && ++gctx->tls_enc_records == 0) { | |
3075 | EVPerr(EVP_F_AES_GCM_TLS_CIPHER, EVP_R_TOO_MANY_RECORDS); | |
3076 | goto err; | |
3077 | } | |
3078 | ||
0f113f3e MC |
3079 | /* |
3080 | * Set IV from start of buffer or generate IV and write to start of | |
3081 | * buffer. | |
3082 | */ | |
c5307d9c AP |
3083 | if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? EVP_CTRL_GCM_IV_GEN |
3084 | : EVP_CTRL_GCM_SET_IV_INV, | |
0f113f3e MC |
3085 | EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) |
3086 | goto err; | |
3087 | /* Use saved AAD */ | |
c5307d9c | 3088 | if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len)) |
0f113f3e MC |
3089 | goto err; |
3090 | /* Fix buffer and length to point to payload */ | |
3091 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
3092 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
3093 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; | |
c5307d9c | 3094 | if (ctx->encrypt) { |
0f113f3e MC |
3095 | /* Encrypt payload */ |
3096 | if (gctx->ctr) { | |
3097 | size_t bulk = 0; | |
5158c763 | 3098 | #if defined(AES_GCM_ASM) |
0f113f3e MC |
3099 | if (len >= 32 && AES_GCM_ASM(gctx)) { |
3100 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0)) | |
3101 | return -1; | |
3102 | ||
3103 | bulk = AES_gcm_encrypt(in, out, len, | |
3104 | gctx->gcm.key, | |
3105 | gctx->gcm.Yi.c, gctx->gcm.Xi.u); | |
3106 | gctx->gcm.len.u[1] += bulk; | |
3107 | } | |
5158c763 | 3108 | #endif |
0f113f3e MC |
3109 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, |
3110 | in + bulk, | |
3111 | out + bulk, | |
3112 | len - bulk, gctx->ctr)) | |
3113 | goto err; | |
3114 | } else { | |
3115 | size_t bulk = 0; | |
5158c763 | 3116 | #if defined(AES_GCM_ASM2) |
0f113f3e MC |
3117 | if (len >= 32 && AES_GCM_ASM2(gctx)) { |
3118 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0)) | |
3119 | return -1; | |
3120 | ||
3121 | bulk = AES_gcm_encrypt(in, out, len, | |
3122 | gctx->gcm.key, | |
3123 | gctx->gcm.Yi.c, gctx->gcm.Xi.u); | |
3124 | gctx->gcm.len.u[1] += bulk; | |
3125 | } | |
5158c763 | 3126 | #endif |
0f113f3e MC |
3127 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, |
3128 | in + bulk, out + bulk, len - bulk)) | |
3129 | goto err; | |
3130 | } | |
3131 | out += len; | |
3132 | /* Finally write tag */ | |
3133 | CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); | |
3134 | rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; | |
3135 | } else { | |
3136 | /* Decrypt */ | |
3137 | if (gctx->ctr) { | |
3138 | size_t bulk = 0; | |
5158c763 | 3139 | #if defined(AES_GCM_ASM) |
0f113f3e MC |
3140 | if (len >= 16 && AES_GCM_ASM(gctx)) { |
3141 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0)) | |
3142 | return -1; | |
3143 | ||
3144 | bulk = AES_gcm_decrypt(in, out, len, | |
3145 | gctx->gcm.key, | |
3146 | gctx->gcm.Yi.c, gctx->gcm.Xi.u); | |
3147 | gctx->gcm.len.u[1] += bulk; | |
3148 | } | |
5158c763 | 3149 | #endif |
0f113f3e MC |
3150 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, |
3151 | in + bulk, | |
3152 | out + bulk, | |
3153 | len - bulk, gctx->ctr)) | |
3154 | goto err; | |
3155 | } else { | |
3156 | size_t bulk = 0; | |
5158c763 | 3157 | #if defined(AES_GCM_ASM2) |
0f113f3e MC |
3158 | if (len >= 16 && AES_GCM_ASM2(gctx)) { |
3159 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0)) | |
3160 | return -1; | |
3161 | ||
3162 | bulk = AES_gcm_decrypt(in, out, len, | |
3163 | gctx->gcm.key, | |
3164 | gctx->gcm.Yi.c, gctx->gcm.Xi.u); | |
3165 | gctx->gcm.len.u[1] += bulk; | |
3166 | } | |
5158c763 | 3167 | #endif |
0f113f3e MC |
3168 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, |
3169 | in + bulk, out + bulk, len - bulk)) | |
3170 | goto err; | |
3171 | } | |
3172 | /* Retrieve tag */ | |
c5307d9c | 3173 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN); |
0f113f3e | 3174 | /* If tag mismatch wipe buffer */ |
c5307d9c | 3175 | if (CRYPTO_memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) { |
0f113f3e MC |
3176 | OPENSSL_cleanse(out, len); |
3177 | goto err; | |
3178 | } | |
3179 | rv = len; | |
3180 | } | |
3181 | ||
3182 | err: | |
3183 | gctx->iv_set = 0; | |
3184 | gctx->tls_aad_len = -1; | |
3185 | return rv; | |
3186 | } | |
28dd49fa | 3187 | |
17f121de | 3188 | static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e MC |
3189 | const unsigned char *in, size_t len) |
3190 | { | |
6435f0f6 | 3191 | EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx); |
0f113f3e MC |
3192 | /* If not set up, return error */ |
3193 | if (!gctx->key_set) | |
3194 | return -1; | |
3195 | ||
3196 | if (gctx->tls_aad_len >= 0) | |
3197 | return aes_gcm_tls_cipher(ctx, out, in, len); | |
3198 | ||
3199 | if (!gctx->iv_set) | |
3200 | return -1; | |
3201 | if (in) { | |
3202 | if (out == NULL) { | |
3203 | if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) | |
3204 | return -1; | |
c5307d9c | 3205 | } else if (ctx->encrypt) { |
0f113f3e MC |
3206 | if (gctx->ctr) { |
3207 | size_t bulk = 0; | |
5158c763 | 3208 | #if defined(AES_GCM_ASM) |
0f113f3e MC |
3209 | if (len >= 32 && AES_GCM_ASM(gctx)) { |
3210 | size_t res = (16 - gctx->gcm.mres) % 16; | |
3211 | ||
3212 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res)) | |
3213 | return -1; | |
3214 | ||
3215 | bulk = AES_gcm_encrypt(in + res, | |
3216 | out + res, len - res, | |
3217 | gctx->gcm.key, gctx->gcm.Yi.c, | |
3218 | gctx->gcm.Xi.u); | |
3219 | gctx->gcm.len.u[1] += bulk; | |
3220 | bulk += res; | |
3221 | } | |
5158c763 | 3222 | #endif |
0f113f3e MC |
3223 | if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, |
3224 | in + bulk, | |
3225 | out + bulk, | |
3226 | len - bulk, gctx->ctr)) | |
3227 | return -1; | |
3228 | } else { | |
3229 | size_t bulk = 0; | |
5158c763 | 3230 | #if defined(AES_GCM_ASM2) |
0f113f3e MC |
3231 | if (len >= 32 && AES_GCM_ASM2(gctx)) { |
3232 | size_t res = (16 - gctx->gcm.mres) % 16; | |
3233 | ||
3234 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res)) | |
3235 | return -1; | |
3236 | ||
3237 | bulk = AES_gcm_encrypt(in + res, | |
3238 | out + res, len - res, | |
3239 | gctx->gcm.key, gctx->gcm.Yi.c, | |
3240 | gctx->gcm.Xi.u); | |
3241 | gctx->gcm.len.u[1] += bulk; | |
3242 | bulk += res; | |
3243 | } | |
5158c763 | 3244 | #endif |
0f113f3e MC |
3245 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, |
3246 | in + bulk, out + bulk, len - bulk)) | |
3247 | return -1; | |
3248 | } | |
3249 | } else { | |
3250 | if (gctx->ctr) { | |
3251 | size_t bulk = 0; | |
5158c763 | 3252 | #if defined(AES_GCM_ASM) |
0f113f3e MC |
3253 | if (len >= 16 && AES_GCM_ASM(gctx)) { |
3254 | size_t res = (16 - gctx->gcm.mres) % 16; | |
3255 | ||
3256 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res)) | |
3257 | return -1; | |
3258 | ||
3259 | bulk = AES_gcm_decrypt(in + res, | |
3260 | out + res, len - res, | |
3261 | gctx->gcm.key, | |
3262 | gctx->gcm.Yi.c, gctx->gcm.Xi.u); | |
3263 | gctx->gcm.len.u[1] += bulk; | |
3264 | bulk += res; | |
3265 | } | |
5158c763 | 3266 | #endif |
0f113f3e MC |
3267 | if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, |
3268 | in + bulk, | |
3269 | out + bulk, | |
3270 | len - bulk, gctx->ctr)) | |
3271 | return -1; | |
3272 | } else { | |
3273 | size_t bulk = 0; | |
5158c763 | 3274 | #if defined(AES_GCM_ASM2) |
0f113f3e MC |
3275 | if (len >= 16 && AES_GCM_ASM2(gctx)) { |
3276 | size_t res = (16 - gctx->gcm.mres) % 16; | |
3277 | ||
3278 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res)) | |
3279 | return -1; | |
3280 | ||
3281 | bulk = AES_gcm_decrypt(in + res, | |
3282 | out + res, len - res, | |
3283 | gctx->gcm.key, | |
3284 | gctx->gcm.Yi.c, gctx->gcm.Xi.u); | |
3285 | gctx->gcm.len.u[1] += bulk; | |
3286 | bulk += res; | |
3287 | } | |
5158c763 | 3288 | #endif |
0f113f3e MC |
3289 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, |
3290 | in + bulk, out + bulk, len - bulk)) | |
3291 | return -1; | |
3292 | } | |
3293 | } | |
3294 | return len; | |
3295 | } else { | |
c5307d9c | 3296 | if (!ctx->encrypt) { |
0f113f3e MC |
3297 | if (gctx->taglen < 0) |
3298 | return -1; | |
c5307d9c | 3299 | if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen) != 0) |
0f113f3e MC |
3300 | return -1; |
3301 | gctx->iv_set = 0; | |
3302 | return 0; | |
3303 | } | |
c5307d9c | 3304 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); |
0f113f3e MC |
3305 | gctx->taglen = 16; |
3306 | /* Don't reuse the IV */ | |
3307 | gctx->iv_set = 0; | |
3308 | return 0; | |
3309 | } | |
3310 | ||
3311 | } | |
3312 | ||
5158c763 | 3313 | #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ |
0f113f3e MC |
3314 | | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ |
3315 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \ | |
3316 | | EVP_CIPH_CUSTOM_COPY) | |
3317 | ||
3318 | BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, gcm, GCM, | |
3319 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
3320 | BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, gcm, GCM, | |
3321 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
3322 | BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, gcm, GCM, | |
3323 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
32a2d8dd DSH |
3324 | |
3325 | static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |
0f113f3e | 3326 | { |
6435f0f6 | 3327 | EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,c); |
0f113f3e MC |
3328 | if (type == EVP_CTRL_COPY) { |
3329 | EVP_CIPHER_CTX *out = ptr; | |
6435f0f6 | 3330 | EVP_AES_XTS_CTX *xctx_out = EVP_C_DATA(EVP_AES_XTS_CTX,out); |
0f113f3e MC |
3331 | if (xctx->xts.key1) { |
3332 | if (xctx->xts.key1 != &xctx->ks1) | |
3333 | return 0; | |
3334 | xctx_out->xts.key1 = &xctx_out->ks1; | |
3335 | } | |
3336 | if (xctx->xts.key2) { | |
3337 | if (xctx->xts.key2 != &xctx->ks2) | |
3338 | return 0; | |
3339 | xctx_out->xts.key2 = &xctx_out->ks2; | |
3340 | } | |
3341 | return 1; | |
3342 | } else if (type != EVP_CTRL_INIT) | |
3343 | return -1; | |
3344 | /* key1 and key2 are used as an indicator both key and IV are set */ | |
3345 | xctx->xts.key1 = NULL; | |
3346 | xctx->xts.key2 = NULL; | |
3347 | return 1; | |
3348 | } | |
32a2d8dd DSH |
3349 | |
3350 | static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
3351 | const unsigned char *iv, int enc) |
3352 | { | |
6435f0f6 | 3353 | EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx); |
0f113f3e MC |
3354 | if (!iv && !key) |
3355 | return 1; | |
3356 | ||
3357 | if (key) | |
3358 | do { | |
5158c763 | 3359 | #ifdef AES_XTS_ASM |
0f113f3e | 3360 | xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt; |
5158c763 | 3361 | #else |
0f113f3e | 3362 | xctx->stream = NULL; |
5158c763 | 3363 | #endif |
0f113f3e | 3364 | /* key_len is two AES keys */ |
5158c763 | 3365 | #ifdef HWAES_CAPABLE |
0f113f3e MC |
3366 | if (HWAES_CAPABLE) { |
3367 | if (enc) { | |
6435f0f6 RL |
3368 | HWAES_set_encrypt_key(key, |
3369 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
0f113f3e MC |
3370 | &xctx->ks1.ks); |
3371 | xctx->xts.block1 = (block128_f) HWAES_encrypt; | |
46f047d7 AP |
3372 | # ifdef HWAES_xts_encrypt |
3373 | xctx->stream = HWAES_xts_encrypt; | |
3374 | # endif | |
0f113f3e | 3375 | } else { |
6435f0f6 RL |
3376 | HWAES_set_decrypt_key(key, |
3377 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
0f113f3e MC |
3378 | &xctx->ks1.ks); |
3379 | xctx->xts.block1 = (block128_f) HWAES_decrypt; | |
46f047d7 AP |
3380 | # ifdef HWAES_xts_decrypt |
3381 | xctx->stream = HWAES_xts_decrypt; | |
3382 | #endif | |
0f113f3e MC |
3383 | } |
3384 | ||
6435f0f6 RL |
3385 | HWAES_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2, |
3386 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
3387 | &xctx->ks2.ks); | |
0f113f3e MC |
3388 | xctx->xts.block2 = (block128_f) HWAES_encrypt; |
3389 | ||
3390 | xctx->xts.key1 = &xctx->ks1; | |
3391 | break; | |
3392 | } else | |
5158c763 MC |
3393 | #endif |
3394 | #ifdef BSAES_CAPABLE | |
0f113f3e MC |
3395 | if (BSAES_CAPABLE) |
3396 | xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt; | |
3397 | else | |
5158c763 MC |
3398 | #endif |
3399 | #ifdef VPAES_CAPABLE | |
0f113f3e MC |
3400 | if (VPAES_CAPABLE) { |
3401 | if (enc) { | |
6435f0f6 RL |
3402 | vpaes_set_encrypt_key(key, |
3403 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
0f113f3e MC |
3404 | &xctx->ks1.ks); |
3405 | xctx->xts.block1 = (block128_f) vpaes_encrypt; | |
3406 | } else { | |
6435f0f6 RL |
3407 | vpaes_set_decrypt_key(key, |
3408 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
0f113f3e MC |
3409 | &xctx->ks1.ks); |
3410 | xctx->xts.block1 = (block128_f) vpaes_decrypt; | |
3411 | } | |
3412 | ||
6435f0f6 RL |
3413 | vpaes_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2, |
3414 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
3415 | &xctx->ks2.ks); | |
0f113f3e MC |
3416 | xctx->xts.block2 = (block128_f) vpaes_encrypt; |
3417 | ||
3418 | xctx->xts.key1 = &xctx->ks1; | |
3419 | break; | |
3420 | } else | |
5158c763 | 3421 | #endif |
0f113f3e MC |
3422 | (void)0; /* terminate potentially open 'else' */ |
3423 | ||
3424 | if (enc) { | |
6435f0f6 RL |
3425 | AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4, |
3426 | &xctx->ks1.ks); | |
0f113f3e MC |
3427 | xctx->xts.block1 = (block128_f) AES_encrypt; |
3428 | } else { | |
6435f0f6 RL |
3429 | AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4, |
3430 | &xctx->ks1.ks); | |
0f113f3e MC |
3431 | xctx->xts.block1 = (block128_f) AES_decrypt; |
3432 | } | |
3433 | ||
6435f0f6 RL |
3434 | AES_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2, |
3435 | EVP_CIPHER_CTX_key_length(ctx) * 4, | |
3436 | &xctx->ks2.ks); | |
0f113f3e MC |
3437 | xctx->xts.block2 = (block128_f) AES_encrypt; |
3438 | ||
3439 | xctx->xts.key1 = &xctx->ks1; | |
3440 | } while (0); | |
3441 | ||
3442 | if (iv) { | |
3443 | xctx->xts.key2 = &xctx->ks2; | |
6435f0f6 | 3444 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16); |
0f113f3e MC |
3445 | } |
3446 | ||
3447 | return 1; | |
3448 | } | |
32a2d8dd | 3449 | |
17f121de | 3450 | static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e MC |
3451 | const unsigned char *in, size_t len) |
3452 | { | |
6435f0f6 | 3453 | EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx); |
95eda4f0 P |
3454 | |
3455 | if (xctx->xts.key1 == NULL | |
3456 | || xctx->xts.key2 == NULL | |
3457 | || out == NULL | |
3458 | || in == NULL | |
3459 | || len < AES_BLOCK_SIZE) | |
0f113f3e | 3460 | return 0; |
95eda4f0 P |
3461 | |
3462 | /* | |
3463 | * Verify that the two keys are different. | |
3464 | * | |
3465 | * This addresses the vulnerability described in Rogaway's September 2004 | |
3466 | * paper (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf): | |
3467 | * "Efficient Instantiations of Tweakable Blockciphers and Refinements | |
3468 | * to Modes OCB and PMAC". | |
3469 | * | |
3470 | * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states that: | |
3471 | * "The check for Key_1 != Key_2 shall be done at any place BEFORE | |
3472 | * using the keys in the XTS-AES algorithm to process data with them." | |
3473 | */ | |
3474 | if (CRYPTO_memcmp(xctx->xts.key1, xctx->xts.key2, | |
3475 | EVP_CIPHER_CTX_key_length(ctx) / 2) == 0) | |
0f113f3e | 3476 | return 0; |
95eda4f0 | 3477 | |
0f113f3e MC |
3478 | if (xctx->stream) |
3479 | (*xctx->stream) (in, out, len, | |
6435f0f6 RL |
3480 | xctx->xts.key1, xctx->xts.key2, |
3481 | EVP_CIPHER_CTX_iv_noconst(ctx)); | |
3482 | else if (CRYPTO_xts128_encrypt(&xctx->xts, EVP_CIPHER_CTX_iv_noconst(ctx), | |
3483 | in, out, len, | |
3484 | EVP_CIPHER_CTX_encrypting(ctx))) | |
0f113f3e MC |
3485 | return 0; |
3486 | return 1; | |
3487 | } | |
3488 | ||
5158c763 | 3489 | #define aes_xts_cleanup NULL |
0f113f3e | 3490 | |
5158c763 | 3491 | #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \ |
0f113f3e MC |
3492 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \ |
3493 | | EVP_CIPH_CUSTOM_COPY) | |
3494 | ||
3495 | BLOCK_CIPHER_custom(NID_aes, 128, 1, 16, xts, XTS, XTS_FLAGS) | |
3496 | BLOCK_CIPHER_custom(NID_aes, 256, 1, 16, xts, XTS, XTS_FLAGS) | |
23916810 DSH |
3497 | |
3498 | static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |
0f113f3e | 3499 | { |
6435f0f6 | 3500 | EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,c); |
0f113f3e MC |
3501 | switch (type) { |
3502 | case EVP_CTRL_INIT: | |
3503 | cctx->key_set = 0; | |
3504 | cctx->iv_set = 0; | |
3505 | cctx->L = 8; | |
3506 | cctx->M = 12; | |
3507 | cctx->tag_set = 0; | |
3508 | cctx->len_set = 0; | |
e75c5a79 DSH |
3509 | cctx->tls_aad_len = -1; |
3510 | return 1; | |
3511 | ||
3512 | case EVP_CTRL_AEAD_TLS1_AAD: | |
3513 | /* Save the AAD for later use */ | |
3514 | if (arg != EVP_AEAD_TLS1_AAD_LEN) | |
3515 | return 0; | |
6435f0f6 | 3516 | memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
e75c5a79 DSH |
3517 | cctx->tls_aad_len = arg; |
3518 | { | |
6435f0f6 RL |
3519 | uint16_t len = |
3520 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8 | |
3521 | | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1]; | |
e75c5a79 | 3522 | /* Correct length for explicit IV */ |
2198b3a5 AP |
3523 | if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN) |
3524 | return 0; | |
e75c5a79 DSH |
3525 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN; |
3526 | /* If decrypting correct for tag too */ | |
2198b3a5 AP |
3527 | if (!EVP_CIPHER_CTX_encrypting(c)) { |
3528 | if (len < cctx->M) | |
3529 | return 0; | |
e75c5a79 | 3530 | len -= cctx->M; |
2198b3a5 | 3531 | } |
6435f0f6 RL |
3532 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8; |
3533 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff; | |
e75c5a79 DSH |
3534 | } |
3535 | /* Extra padding: tag appended to record */ | |
3536 | return cctx->M; | |
3537 | ||
3538 | case EVP_CTRL_CCM_SET_IV_FIXED: | |
3539 | /* Sanity check length */ | |
3540 | if (arg != EVP_CCM_TLS_FIXED_IV_LEN) | |
3541 | return 0; | |
3542 | /* Just copy to first part of IV */ | |
6435f0f6 | 3543 | memcpy(EVP_CIPHER_CTX_iv_noconst(c), ptr, arg); |
0f113f3e MC |
3544 | return 1; |
3545 | ||
e640fa02 | 3546 | case EVP_CTRL_AEAD_SET_IVLEN: |
0f113f3e | 3547 | arg = 15 - arg; |
018fcbec | 3548 | /* fall thru */ |
0f113f3e MC |
3549 | case EVP_CTRL_CCM_SET_L: |
3550 | if (arg < 2 || arg > 8) | |
3551 | return 0; | |
3552 | cctx->L = arg; | |
3553 | return 1; | |
3554 | ||
e640fa02 | 3555 | case EVP_CTRL_AEAD_SET_TAG: |
0f113f3e MC |
3556 | if ((arg & 1) || arg < 4 || arg > 16) |
3557 | return 0; | |
6435f0f6 | 3558 | if (EVP_CIPHER_CTX_encrypting(c) && ptr) |
0f113f3e MC |
3559 | return 0; |
3560 | if (ptr) { | |
3561 | cctx->tag_set = 1; | |
6435f0f6 | 3562 | memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
0f113f3e MC |
3563 | } |
3564 | cctx->M = arg; | |
3565 | return 1; | |
3566 | ||
e640fa02 | 3567 | case EVP_CTRL_AEAD_GET_TAG: |
6435f0f6 | 3568 | if (!EVP_CIPHER_CTX_encrypting(c) || !cctx->tag_set) |
0f113f3e MC |
3569 | return 0; |
3570 | if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) | |
3571 | return 0; | |
3572 | cctx->tag_set = 0; | |
3573 | cctx->iv_set = 0; | |
3574 | cctx->len_set = 0; | |
3575 | return 1; | |
3576 | ||
3577 | case EVP_CTRL_COPY: | |
3578 | { | |
3579 | EVP_CIPHER_CTX *out = ptr; | |
6435f0f6 | 3580 | EVP_AES_CCM_CTX *cctx_out = EVP_C_DATA(EVP_AES_CCM_CTX,out); |
0f113f3e MC |
3581 | if (cctx->ccm.key) { |
3582 | if (cctx->ccm.key != &cctx->ks) | |
3583 | return 0; | |
3584 | cctx_out->ccm.key = &cctx_out->ks; | |
3585 | } | |
3586 | return 1; | |
3587 | } | |
3588 | ||
3589 | default: | |
3590 | return -1; | |
3591 | ||
3592 | } | |
3593 | } | |
23916810 DSH |
3594 | |
3595 | static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
3596 | const unsigned char *iv, int enc) |
3597 | { | |
6435f0f6 | 3598 | EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx); |
0f113f3e MC |
3599 | if (!iv && !key) |
3600 | return 1; | |
3601 | if (key) | |
3602 | do { | |
5158c763 | 3603 | #ifdef HWAES_CAPABLE |
0f113f3e | 3604 | if (HWAES_CAPABLE) { |
6435f0f6 RL |
3605 | HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
3606 | &cctx->ks.ks); | |
0f113f3e MC |
3607 | |
3608 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, | |
3609 | &cctx->ks, (block128_f) HWAES_encrypt); | |
3610 | cctx->str = NULL; | |
3611 | cctx->key_set = 1; | |
3612 | break; | |
3613 | } else | |
5158c763 MC |
3614 | #endif |
3615 | #ifdef VPAES_CAPABLE | |
0f113f3e | 3616 | if (VPAES_CAPABLE) { |
6435f0f6 RL |
3617 | vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
3618 | &cctx->ks.ks); | |
0f113f3e MC |
3619 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
3620 | &cctx->ks, (block128_f) vpaes_encrypt); | |
3621 | cctx->str = NULL; | |
3622 | cctx->key_set = 1; | |
3623 | break; | |
3624 | } | |
5158c763 | 3625 | #endif |
6435f0f6 RL |
3626 | AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
3627 | &cctx->ks.ks); | |
0f113f3e MC |
3628 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
3629 | &cctx->ks, (block128_f) AES_encrypt); | |
3630 | cctx->str = NULL; | |
3631 | cctx->key_set = 1; | |
3632 | } while (0); | |
3633 | if (iv) { | |
6435f0f6 | 3634 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L); |
0f113f3e MC |
3635 | cctx->iv_set = 1; |
3636 | } | |
3637 | return 1; | |
3638 | } | |
23916810 | 3639 | |
e75c5a79 DSH |
3640 | static int aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
3641 | const unsigned char *in, size_t len) | |
3642 | { | |
6435f0f6 | 3643 | EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx); |
e75c5a79 DSH |
3644 | CCM128_CONTEXT *ccm = &cctx->ccm; |
3645 | /* Encrypt/decrypt must be performed in place */ | |
3646 | if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M)) | |
3647 | return -1; | |
3648 | /* If encrypting set explicit IV from sequence number (start of AAD) */ | |
6435f0f6 RL |
3649 | if (EVP_CIPHER_CTX_encrypting(ctx)) |
3650 | memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx), | |
3651 | EVP_CCM_TLS_EXPLICIT_IV_LEN); | |
e75c5a79 | 3652 | /* Get rest of IV from explicit IV */ |
6435f0f6 RL |
3653 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx) + EVP_CCM_TLS_FIXED_IV_LEN, in, |
3654 | EVP_CCM_TLS_EXPLICIT_IV_LEN); | |
e75c5a79 DSH |
3655 | /* Correct length value */ |
3656 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M; | |
6435f0f6 RL |
3657 | if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), 15 - cctx->L, |
3658 | len)) | |
e75c5a79 DSH |
3659 | return -1; |
3660 | /* Use saved AAD */ | |
6435f0f6 | 3661 | CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), cctx->tls_aad_len); |
e75c5a79 DSH |
3662 | /* Fix buffer to point to payload */ |
3663 | in += EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
3664 | out += EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
6435f0f6 | 3665 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
e75c5a79 DSH |
3666 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, |
3667 | cctx->str) : | |
3668 | CRYPTO_ccm128_encrypt(ccm, in, out, len)) | |
3669 | return -1; | |
3670 | if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M)) | |
3671 | return -1; | |
3672 | return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M; | |
3673 | } else { | |
3674 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, | |
3675 | cctx->str) : | |
3676 | !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { | |
3677 | unsigned char tag[16]; | |
3678 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { | |
3679 | if (!CRYPTO_memcmp(tag, in + len, cctx->M)) | |
3680 | return len; | |
3681 | } | |
3682 | } | |
3683 | OPENSSL_cleanse(out, len); | |
3684 | return -1; | |
3685 | } | |
3686 | } | |
3687 | ||
17f121de | 3688 | static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e MC |
3689 | const unsigned char *in, size_t len) |
3690 | { | |
6435f0f6 | 3691 | EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx); |
0f113f3e MC |
3692 | CCM128_CONTEXT *ccm = &cctx->ccm; |
3693 | /* If not set up, return error */ | |
e75c5a79 DSH |
3694 | if (!cctx->key_set) |
3695 | return -1; | |
3696 | ||
3697 | if (cctx->tls_aad_len >= 0) | |
3698 | return aes_ccm_tls_cipher(ctx, out, in, len); | |
3699 | ||
197421b1 DSH |
3700 | /* EVP_*Final() doesn't return any data */ |
3701 | if (in == NULL && out != NULL) | |
3702 | return 0; | |
3703 | ||
e75c5a79 | 3704 | if (!cctx->iv_set) |
0f113f3e | 3705 | return -1; |
e75c5a79 | 3706 | |
6435f0f6 | 3707 | if (!EVP_CIPHER_CTX_encrypting(ctx) && !cctx->tag_set) |
0f113f3e MC |
3708 | return -1; |
3709 | if (!out) { | |
3710 | if (!in) { | |
6435f0f6 RL |
3711 | if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), |
3712 | 15 - cctx->L, len)) | |
0f113f3e MC |
3713 | return -1; |
3714 | cctx->len_set = 1; | |
3715 | return len; | |
3716 | } | |
3717 | /* If have AAD need message length */ | |
3718 | if (!cctx->len_set && len) | |
3719 | return -1; | |
3720 | CRYPTO_ccm128_aad(ccm, in, len); | |
3721 | return len; | |
3722 | } | |
0f113f3e MC |
3723 | /* If not set length yet do it */ |
3724 | if (!cctx->len_set) { | |
6435f0f6 RL |
3725 | if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), |
3726 | 15 - cctx->L, len)) | |
0f113f3e MC |
3727 | return -1; |
3728 | cctx->len_set = 1; | |
3729 | } | |
6435f0f6 | 3730 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
0f113f3e MC |
3731 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, |
3732 | cctx->str) : | |
3733 | CRYPTO_ccm128_encrypt(ccm, in, out, len)) | |
3734 | return -1; | |
3735 | cctx->tag_set = 1; | |
3736 | return len; | |
3737 | } else { | |
3738 | int rv = -1; | |
3739 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, | |
3740 | cctx->str) : | |
3741 | !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { | |
3742 | unsigned char tag[16]; | |
3743 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { | |
6435f0f6 RL |
3744 | if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx), |
3745 | cctx->M)) | |
0f113f3e MC |
3746 | rv = len; |
3747 | } | |
3748 | } | |
3749 | if (rv == -1) | |
3750 | OPENSSL_cleanse(out, len); | |
3751 | cctx->iv_set = 0; | |
3752 | cctx->tag_set = 0; | |
3753 | cctx->len_set = 0; | |
3754 | return rv; | |
3755 | } | |
0f113f3e MC |
3756 | } |
3757 | ||
5158c763 | 3758 | #define aes_ccm_cleanup NULL |
0f113f3e | 3759 | |
e75c5a79 DSH |
3760 | BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, ccm, CCM, |
3761 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
3762 | BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, ccm, CCM, | |
3763 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
3764 | BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, ccm, CCM, | |
3765 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
0f113f3e MC |
3766 | |
3767 | typedef struct { | |
3768 | union { | |
3769 | double align; | |
3770 | AES_KEY ks; | |
3771 | } ks; | |
3772 | /* Indicates if IV has been set */ | |
3773 | unsigned char *iv; | |
3774 | } EVP_AES_WRAP_CTX; | |
97cf1f6c DSH |
3775 | |
3776 | static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
0f113f3e MC |
3777 | const unsigned char *iv, int enc) |
3778 | { | |
6435f0f6 | 3779 | EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx); |
0f113f3e MC |
3780 | if (!iv && !key) |
3781 | return 1; | |
3782 | if (key) { | |
6435f0f6 RL |
3783 | if (EVP_CIPHER_CTX_encrypting(ctx)) |
3784 | AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, | |
3785 | &wctx->ks.ks); | |
0f113f3e | 3786 | else |
6435f0f6 RL |
3787 | AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
3788 | &wctx->ks.ks); | |
0f113f3e MC |
3789 | if (!iv) |
3790 | wctx->iv = NULL; | |
3791 | } | |
3792 | if (iv) { | |
6435f0f6 RL |
3793 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, EVP_CIPHER_CTX_iv_length(ctx)); |
3794 | wctx->iv = EVP_CIPHER_CTX_iv_noconst(ctx); | |
0f113f3e MC |
3795 | } |
3796 | return 1; | |
3797 | } | |
97cf1f6c DSH |
3798 | |
3799 | static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
3800 | const unsigned char *in, size_t inlen) |
3801 | { | |
6435f0f6 | 3802 | EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx); |
0f113f3e MC |
3803 | size_t rv; |
3804 | /* AES wrap with padding has IV length of 4, without padding 8 */ | |
3805 | int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4; | |
3806 | /* No final operation so always return zero length */ | |
3807 | if (!in) | |
3808 | return 0; | |
3809 | /* Input length must always be non-zero */ | |
3810 | if (!inlen) | |
3811 | return -1; | |
3812 | /* If decrypting need at least 16 bytes and multiple of 8 */ | |
6435f0f6 | 3813 | if (!EVP_CIPHER_CTX_encrypting(ctx) && (inlen < 16 || inlen & 0x7)) |
0f113f3e MC |
3814 | return -1; |
3815 | /* If not padding input must be multiple of 8 */ | |
3816 | if (!pad && inlen & 0x7) | |
3817 | return -1; | |
7141ba31 MC |
3818 | if (is_partially_overlapping(out, in, inlen)) { |
3819 | EVPerr(EVP_F_AES_WRAP_CIPHER, EVP_R_PARTIALLY_OVERLAPPING); | |
3820 | return 0; | |
3821 | } | |
0f113f3e | 3822 | if (!out) { |
6435f0f6 | 3823 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
0f113f3e MC |
3824 | /* If padding round up to multiple of 8 */ |
3825 | if (pad) | |
3826 | inlen = (inlen + 7) / 8 * 8; | |
3827 | /* 8 byte prefix */ | |
3828 | return inlen + 8; | |
3829 | } else { | |
3830 | /* | |
3831 | * If not padding output will be exactly 8 bytes smaller than | |
3832 | * input. If padding it will be at least 8 bytes smaller but we | |
3833 | * don't know how much. | |
3834 | */ | |
3835 | return inlen - 8; | |
3836 | } | |
3837 | } | |
3838 | if (pad) { | |
6435f0f6 | 3839 | if (EVP_CIPHER_CTX_encrypting(ctx)) |
0f113f3e MC |
3840 | rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv, |
3841 | out, in, inlen, | |
3842 | (block128_f) AES_encrypt); | |
3843 | else | |
3844 | rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv, | |
3845 | out, in, inlen, | |
3846 | (block128_f) AES_decrypt); | |
3847 | } else { | |
6435f0f6 | 3848 | if (EVP_CIPHER_CTX_encrypting(ctx)) |
0f113f3e MC |
3849 | rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, |
3850 | out, in, inlen, (block128_f) AES_encrypt); | |
3851 | else | |
3852 | rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, | |
3853 | out, in, inlen, (block128_f) AES_decrypt); | |
3854 | } | |
3855 | return rv ? (int)rv : -1; | |
3856 | } | |
3857 | ||
5158c763 | 3858 | #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \ |
0f113f3e MC |
3859 | | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ |
3860 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1) | |
97cf1f6c DSH |
3861 | |
3862 | static const EVP_CIPHER aes_128_wrap = { | |
0f113f3e MC |
3863 | NID_id_aes128_wrap, |
3864 | 8, 16, 8, WRAP_FLAGS, | |
3865 | aes_wrap_init_key, aes_wrap_cipher, | |
3866 | NULL, | |
3867 | sizeof(EVP_AES_WRAP_CTX), | |
3868 | NULL, NULL, NULL, NULL | |
3869 | }; | |
97cf1f6c DSH |
3870 | |
3871 | const EVP_CIPHER *EVP_aes_128_wrap(void) | |
0f113f3e MC |
3872 | { |
3873 | return &aes_128_wrap; | |
3874 | } | |
97cf1f6c DSH |
3875 | |
3876 | static const EVP_CIPHER aes_192_wrap = { | |
0f113f3e MC |
3877 | NID_id_aes192_wrap, |
3878 | 8, 24, 8, WRAP_FLAGS, | |
3879 | aes_wrap_init_key, aes_wrap_cipher, | |
3880 | NULL, | |
3881 | sizeof(EVP_AES_WRAP_CTX), | |
3882 | NULL, NULL, NULL, NULL | |
3883 | }; | |
97cf1f6c DSH |
3884 | |
3885 | const EVP_CIPHER *EVP_aes_192_wrap(void) | |
0f113f3e MC |
3886 | { |
3887 | return &aes_192_wrap; | |
3888 | } | |
97cf1f6c DSH |
3889 | |
3890 | static const EVP_CIPHER aes_256_wrap = { | |
0f113f3e MC |
3891 | NID_id_aes256_wrap, |
3892 | 8, 32, 8, WRAP_FLAGS, | |
3893 | aes_wrap_init_key, aes_wrap_cipher, | |
3894 | NULL, | |
3895 | sizeof(EVP_AES_WRAP_CTX), | |
3896 | NULL, NULL, NULL, NULL | |
3897 | }; | |
97cf1f6c DSH |
3898 | |
3899 | const EVP_CIPHER *EVP_aes_256_wrap(void) | |
0f113f3e MC |
3900 | { |
3901 | return &aes_256_wrap; | |
3902 | } | |
97cf1f6c | 3903 | |
d31fed73 | 3904 | static const EVP_CIPHER aes_128_wrap_pad = { |
0f113f3e MC |
3905 | NID_id_aes128_wrap_pad, |
3906 | 8, 16, 4, WRAP_FLAGS, | |
3907 | aes_wrap_init_key, aes_wrap_cipher, | |
3908 | NULL, | |
3909 | sizeof(EVP_AES_WRAP_CTX), | |
3910 | NULL, NULL, NULL, NULL | |
3911 | }; | |
d31fed73 DSH |
3912 | |
3913 | const EVP_CIPHER *EVP_aes_128_wrap_pad(void) | |
0f113f3e MC |
3914 | { |
3915 | return &aes_128_wrap_pad; | |
3916 | } | |
d31fed73 DSH |
3917 | |
3918 | static const EVP_CIPHER aes_192_wrap_pad = { | |
0f113f3e MC |
3919 | NID_id_aes192_wrap_pad, |
3920 | 8, 24, 4, WRAP_FLAGS, | |
3921 | aes_wrap_init_key, aes_wrap_cipher, | |
3922 | NULL, | |
3923 | sizeof(EVP_AES_WRAP_CTX), | |
3924 | NULL, NULL, NULL, NULL | |
3925 | }; | |
d31fed73 DSH |
3926 | |
3927 | const EVP_CIPHER *EVP_aes_192_wrap_pad(void) | |
0f113f3e MC |
3928 | { |
3929 | return &aes_192_wrap_pad; | |
3930 | } | |
d31fed73 DSH |
3931 | |
3932 | static const EVP_CIPHER aes_256_wrap_pad = { | |
0f113f3e MC |
3933 | NID_id_aes256_wrap_pad, |
3934 | 8, 32, 4, WRAP_FLAGS, | |
3935 | aes_wrap_init_key, aes_wrap_cipher, | |
3936 | NULL, | |
3937 | sizeof(EVP_AES_WRAP_CTX), | |
3938 | NULL, NULL, NULL, NULL | |
3939 | }; | |
d31fed73 DSH |
3940 | |
3941 | const EVP_CIPHER *EVP_aes_256_wrap_pad(void) | |
0f113f3e MC |
3942 | { |
3943 | return &aes_256_wrap_pad; | |
3944 | } | |
d31fed73 | 3945 | |
5158c763 | 3946 | #ifndef OPENSSL_NO_OCB |
e6b336ef | 3947 | static int aes_ocb_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
0f113f3e | 3948 | { |
6435f0f6 | 3949 | EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c); |
0f113f3e MC |
3950 | EVP_CIPHER_CTX *newc; |
3951 | EVP_AES_OCB_CTX *new_octx; | |
3952 | ||
3953 | switch (type) { | |
3954 | case EVP_CTRL_INIT: | |
3955 | octx->key_set = 0; | |
3956 | octx->iv_set = 0; | |
6435f0f6 RL |
3957 | octx->ivlen = EVP_CIPHER_CTX_iv_length(c); |
3958 | octx->iv = EVP_CIPHER_CTX_iv_noconst(c); | |
0f113f3e MC |
3959 | octx->taglen = 16; |
3960 | octx->data_buf_len = 0; | |
3961 | octx->aad_buf_len = 0; | |
3962 | return 1; | |
3963 | ||
e640fa02 | 3964 | case EVP_CTRL_AEAD_SET_IVLEN: |
0f113f3e MC |
3965 | /* IV len must be 1 to 15 */ |
3966 | if (arg <= 0 || arg > 15) | |
3967 | return 0; | |
3968 | ||
3969 | octx->ivlen = arg; | |
3970 | return 1; | |
3971 | ||
e640fa02 | 3972 | case EVP_CTRL_AEAD_SET_TAG: |
d57d135c MC |
3973 | if (!ptr) { |
3974 | /* Tag len must be 0 to 16 */ | |
3975 | if (arg < 0 || arg > 16) | |
3976 | return 0; | |
3977 | ||
3978 | octx->taglen = arg; | |
3979 | return 1; | |
3980 | } | |
6435f0f6 | 3981 | if (arg != octx->taglen || EVP_CIPHER_CTX_encrypting(c)) |
0f113f3e MC |
3982 | return 0; |
3983 | memcpy(octx->tag, ptr, arg); | |
3984 | return 1; | |
3985 | ||
e640fa02 | 3986 | case EVP_CTRL_AEAD_GET_TAG: |
6435f0f6 | 3987 | if (arg != octx->taglen || !EVP_CIPHER_CTX_encrypting(c)) |
0f113f3e MC |
3988 | return 0; |
3989 | ||
3990 | memcpy(ptr, octx->tag, arg); | |
3991 | return 1; | |
3992 | ||
3993 | case EVP_CTRL_COPY: | |
3994 | newc = (EVP_CIPHER_CTX *)ptr; | |
6435f0f6 | 3995 | new_octx = EVP_C_DATA(EVP_AES_OCB_CTX,newc); |
0f113f3e | 3996 | return CRYPTO_ocb128_copy_ctx(&new_octx->ocb, &octx->ocb, |
bdc985b1 AP |
3997 | &new_octx->ksenc.ks, |
3998 | &new_octx->ksdec.ks); | |
0f113f3e MC |
3999 | |
4000 | default: | |
4001 | return -1; | |
4002 | ||
4003 | } | |
4004 | } | |
e6b336ef | 4005 | |
5158c763 MC |
4006 | # ifdef HWAES_CAPABLE |
4007 | # ifdef HWAES_ocb_encrypt | |
02dc0b82 AP |
4008 | void HWAES_ocb_encrypt(const unsigned char *in, unsigned char *out, |
4009 | size_t blocks, const void *key, | |
4010 | size_t start_block_num, | |
4011 | unsigned char offset_i[16], | |
4012 | const unsigned char L_[][16], | |
4013 | unsigned char checksum[16]); | |
5158c763 | 4014 | # else |
365f95ad | 4015 | # define HWAES_ocb_encrypt ((ocb128_f)NULL) |
5158c763 MC |
4016 | # endif |
4017 | # ifdef HWAES_ocb_decrypt | |
02dc0b82 AP |
4018 | void HWAES_ocb_decrypt(const unsigned char *in, unsigned char *out, |
4019 | size_t blocks, const void *key, | |
4020 | size_t start_block_num, | |
4021 | unsigned char offset_i[16], | |
4022 | const unsigned char L_[][16], | |
4023 | unsigned char checksum[16]); | |
5158c763 | 4024 | # else |
365f95ad | 4025 | # define HWAES_ocb_decrypt ((ocb128_f)NULL) |
02dc0b82 | 4026 | # endif |
5158c763 | 4027 | # endif |
02dc0b82 | 4028 | |
e6b336ef | 4029 | static int aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
4030 | const unsigned char *iv, int enc) |
4031 | { | |
6435f0f6 | 4032 | EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx); |
0f113f3e MC |
4033 | if (!iv && !key) |
4034 | return 1; | |
4035 | if (key) { | |
4036 | do { | |
4037 | /* | |
4038 | * We set both the encrypt and decrypt key here because decrypt | |
4039 | * needs both. We could possibly optimise to remove setting the | |
4040 | * decrypt for an encryption operation. | |
4041 | */ | |
5158c763 | 4042 | # ifdef HWAES_CAPABLE |
02dc0b82 | 4043 | if (HWAES_CAPABLE) { |
6435f0f6 RL |
4044 | HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
4045 | &octx->ksenc.ks); | |
4046 | HWAES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, | |
4047 | &octx->ksdec.ks); | |
02dc0b82 AP |
4048 | if (!CRYPTO_ocb128_init(&octx->ocb, |
4049 | &octx->ksenc.ks, &octx->ksdec.ks, | |
4050 | (block128_f) HWAES_encrypt, | |
4051 | (block128_f) HWAES_decrypt, | |
4052 | enc ? HWAES_ocb_encrypt | |
4053 | : HWAES_ocb_decrypt)) | |
4054 | return 0; | |
4055 | break; | |
4056 | } | |
5158c763 MC |
4057 | # endif |
4058 | # ifdef VPAES_CAPABLE | |
0f113f3e | 4059 | if (VPAES_CAPABLE) { |
6435f0f6 RL |
4060 | vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
4061 | &octx->ksenc.ks); | |
4062 | vpaes_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, | |
4063 | &octx->ksdec.ks); | |
bdc985b1 AP |
4064 | if (!CRYPTO_ocb128_init(&octx->ocb, |
4065 | &octx->ksenc.ks, &octx->ksdec.ks, | |
4066 | (block128_f) vpaes_encrypt, | |
bd30091c AP |
4067 | (block128_f) vpaes_decrypt, |
4068 | NULL)) | |
0f113f3e MC |
4069 | return 0; |
4070 | break; | |
4071 | } | |
5158c763 | 4072 | # endif |
6435f0f6 RL |
4073 | AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
4074 | &octx->ksenc.ks); | |
4075 | AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, | |
4076 | &octx->ksdec.ks); | |
bdc985b1 AP |
4077 | if (!CRYPTO_ocb128_init(&octx->ocb, |
4078 | &octx->ksenc.ks, &octx->ksdec.ks, | |
0f113f3e | 4079 | (block128_f) AES_encrypt, |
bd30091c AP |
4080 | (block128_f) AES_decrypt, |
4081 | NULL)) | |
0f113f3e MC |
4082 | return 0; |
4083 | } | |
4084 | while (0); | |
4085 | ||
4086 | /* | |
4087 | * If we have an iv we can set it directly, otherwise use saved IV. | |
4088 | */ | |
4089 | if (iv == NULL && octx->iv_set) | |
4090 | iv = octx->iv; | |
4091 | if (iv) { | |
4092 | if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen) | |
4093 | != 1) | |
4094 | return 0; | |
4095 | octx->iv_set = 1; | |
4096 | } | |
4097 | octx->key_set = 1; | |
4098 | } else { | |
4099 | /* If key set use IV, otherwise copy */ | |
4100 | if (octx->key_set) | |
4101 | CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen); | |
4102 | else | |
4103 | memcpy(octx->iv, iv, octx->ivlen); | |
4104 | octx->iv_set = 1; | |
4105 | } | |
4106 | return 1; | |
4107 | } | |
e6b336ef MC |
4108 | |
4109 | static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
4110 | const unsigned char *in, size_t len) |
4111 | { | |
4112 | unsigned char *buf; | |
4113 | int *buf_len; | |
4114 | int written_len = 0; | |
4115 | size_t trailing_len; | |
6435f0f6 | 4116 | EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx); |
0f113f3e MC |
4117 | |
4118 | /* If IV or Key not set then return error */ | |
4119 | if (!octx->iv_set) | |
4120 | return -1; | |
4121 | ||
4122 | if (!octx->key_set) | |
4123 | return -1; | |
4124 | ||
0ba5a9ea | 4125 | if (in != NULL) { |
0f113f3e MC |
4126 | /* |
4127 | * Need to ensure we are only passing full blocks to low level OCB | |
4128 | * routines. We do it here rather than in EVP_EncryptUpdate/ | |
4129 | * EVP_DecryptUpdate because we need to pass full blocks of AAD too | |
4130 | * and those routines don't support that | |
4131 | */ | |
4132 | ||
4133 | /* Are we dealing with AAD or normal data here? */ | |
4134 | if (out == NULL) { | |
4135 | buf = octx->aad_buf; | |
4136 | buf_len = &(octx->aad_buf_len); | |
4137 | } else { | |
4138 | buf = octx->data_buf; | |
4139 | buf_len = &(octx->data_buf_len); | |
7141ba31 MC |
4140 | |
4141 | if (is_partially_overlapping(out + *buf_len, in, len)) { | |
4142 | EVPerr(EVP_F_AES_OCB_CIPHER, EVP_R_PARTIALLY_OVERLAPPING); | |
4143 | return 0; | |
4144 | } | |
0f113f3e MC |
4145 | } |
4146 | ||
4147 | /* | |
4148 | * If we've got a partially filled buffer from a previous call then | |
4149 | * use that data first | |
4150 | */ | |
0ba5a9ea | 4151 | if (*buf_len > 0) { |
0f113f3e MC |
4152 | unsigned int remaining; |
4153 | ||
0ba5a9ea | 4154 | remaining = AES_BLOCK_SIZE - (*buf_len); |
0f113f3e MC |
4155 | if (remaining > len) { |
4156 | memcpy(buf + (*buf_len), in, len); | |
4157 | *(buf_len) += len; | |
4158 | return 0; | |
4159 | } | |
4160 | memcpy(buf + (*buf_len), in, remaining); | |
4161 | ||
4162 | /* | |
4163 | * If we get here we've filled the buffer, so process it | |
4164 | */ | |
4165 | len -= remaining; | |
4166 | in += remaining; | |
4167 | if (out == NULL) { | |
0ba5a9ea | 4168 | if (!CRYPTO_ocb128_aad(&octx->ocb, buf, AES_BLOCK_SIZE)) |
0f113f3e | 4169 | return -1; |
6435f0f6 | 4170 | } else if (EVP_CIPHER_CTX_encrypting(ctx)) { |
0ba5a9ea MC |
4171 | if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out, |
4172 | AES_BLOCK_SIZE)) | |
0f113f3e MC |
4173 | return -1; |
4174 | } else { | |
0ba5a9ea MC |
4175 | if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out, |
4176 | AES_BLOCK_SIZE)) | |
0f113f3e MC |
4177 | return -1; |
4178 | } | |
0ba5a9ea | 4179 | written_len = AES_BLOCK_SIZE; |
0f113f3e | 4180 | *buf_len = 0; |
7c12c7b6 MC |
4181 | if (out != NULL) |
4182 | out += AES_BLOCK_SIZE; | |
0f113f3e MC |
4183 | } |
4184 | ||
4185 | /* Do we have a partial block to handle at the end? */ | |
0ba5a9ea | 4186 | trailing_len = len % AES_BLOCK_SIZE; |
0f113f3e MC |
4187 | |
4188 | /* | |
4189 | * If we've got some full blocks to handle, then process these first | |
4190 | */ | |
4191 | if (len != trailing_len) { | |
4192 | if (out == NULL) { | |
4193 | if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len)) | |
4194 | return -1; | |
6435f0f6 | 4195 | } else if (EVP_CIPHER_CTX_encrypting(ctx)) { |
0f113f3e MC |
4196 | if (!CRYPTO_ocb128_encrypt |
4197 | (&octx->ocb, in, out, len - trailing_len)) | |
4198 | return -1; | |
4199 | } else { | |
4200 | if (!CRYPTO_ocb128_decrypt | |
4201 | (&octx->ocb, in, out, len - trailing_len)) | |
4202 | return -1; | |
4203 | } | |
4204 | written_len += len - trailing_len; | |
4205 | in += len - trailing_len; | |
4206 | } | |
4207 | ||
4208 | /* Handle any trailing partial block */ | |
0ba5a9ea | 4209 | if (trailing_len > 0) { |
0f113f3e MC |
4210 | memcpy(buf, in, trailing_len); |
4211 | *buf_len = trailing_len; | |
4212 | } | |
4213 | ||
4214 | return written_len; | |
4215 | } else { | |
4216 | /* | |
4217 | * First of all empty the buffer of any partial block that we might | |
4218 | * have been provided - both for data and AAD | |
4219 | */ | |
0ba5a9ea | 4220 | if (octx->data_buf_len > 0) { |
6435f0f6 | 4221 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
0f113f3e MC |
4222 | if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out, |
4223 | octx->data_buf_len)) | |
4224 | return -1; | |
4225 | } else { | |
4226 | if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out, | |
4227 | octx->data_buf_len)) | |
4228 | return -1; | |
4229 | } | |
4230 | written_len = octx->data_buf_len; | |
4231 | octx->data_buf_len = 0; | |
4232 | } | |
0ba5a9ea | 4233 | if (octx->aad_buf_len > 0) { |
0f113f3e MC |
4234 | if (!CRYPTO_ocb128_aad |
4235 | (&octx->ocb, octx->aad_buf, octx->aad_buf_len)) | |
4236 | return -1; | |
4237 | octx->aad_buf_len = 0; | |
4238 | } | |
4239 | /* If decrypting then verify */ | |
6435f0f6 | 4240 | if (!EVP_CIPHER_CTX_encrypting(ctx)) { |
0f113f3e MC |
4241 | if (octx->taglen < 0) |
4242 | return -1; | |
4243 | if (CRYPTO_ocb128_finish(&octx->ocb, | |
4244 | octx->tag, octx->taglen) != 0) | |
4245 | return -1; | |
4246 | octx->iv_set = 0; | |
4247 | return written_len; | |
4248 | } | |
4249 | /* If encrypting then just get the tag */ | |
4250 | if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1) | |
4251 | return -1; | |
4252 | /* Don't reuse the IV */ | |
4253 | octx->iv_set = 0; | |
4254 | return written_len; | |
4255 | } | |
4256 | } | |
e6b336ef MC |
4257 | |
4258 | static int aes_ocb_cleanup(EVP_CIPHER_CTX *c) | |
0f113f3e | 4259 | { |
6435f0f6 | 4260 | EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c); |
0f113f3e MC |
4261 | CRYPTO_ocb128_cleanup(&octx->ocb); |
4262 | return 1; | |
4263 | } | |
e6b336ef | 4264 | |
c4aede20 MC |
4265 | BLOCK_CIPHER_custom(NID_aes, 128, 16, 12, ocb, OCB, |
4266 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
4267 | BLOCK_CIPHER_custom(NID_aes, 192, 16, 12, ocb, OCB, | |
4268 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
4269 | BLOCK_CIPHER_custom(NID_aes, 256, 16, 12, ocb, OCB, | |
4270 | EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS) | |
5158c763 | 4271 | #endif /* OPENSSL_NO_OCB */ |
b1ceb439 TS |
4272 | |
4273 | /* AES-SIV mode */ | |
4274 | #ifndef OPENSSL_NO_SIV | |
4275 | ||
4276 | typedef SIV128_CONTEXT EVP_AES_SIV_CTX; | |
4277 | ||
4278 | #define aesni_siv_init_key aes_siv_init_key | |
4279 | static int aes_siv_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
4280 | const unsigned char *iv, int enc) | |
4281 | { | |
4282 | const EVP_CIPHER *ctr; | |
4283 | const EVP_CIPHER *cbc; | |
4284 | SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, ctx); | |
4285 | int klen = EVP_CIPHER_CTX_key_length(ctx) / 2; | |
4286 | ||
4287 | if (key == NULL) | |
4288 | return 1; | |
4289 | ||
4290 | switch (klen) { | |
4291 | case 16: | |
4292 | cbc = EVP_aes_128_cbc(); | |
4293 | ctr = EVP_aes_128_ctr(); | |
4294 | break; | |
4295 | case 24: | |
4296 | cbc = EVP_aes_192_cbc(); | |
4297 | ctr = EVP_aes_192_ctr(); | |
4298 | break; | |
4299 | case 32: | |
4300 | cbc = EVP_aes_256_cbc(); | |
4301 | ctr = EVP_aes_256_ctr(); | |
4302 | break; | |
4303 | default: | |
4304 | return 0; | |
4305 | } | |
4306 | ||
4307 | /* klen is the length of the underlying cipher, not the input key, | |
4308 | which should be twice as long */ | |
4309 | return CRYPTO_siv128_init(sctx, key, klen, cbc, ctr); | |
4310 | } | |
4311 | ||
4312 | #define aesni_siv_cipher aes_siv_cipher | |
4313 | static int aes_siv_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
4314 | const unsigned char *in, size_t len) | |
4315 | { | |
4316 | SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, ctx); | |
4317 | ||
4318 | /* EncryptFinal or DecryptFinal */ | |
4319 | if (in == NULL) | |
4320 | return CRYPTO_siv128_finish(sctx); | |
4321 | ||
4322 | /* Deal with associated data */ | |
4323 | if (out == NULL) | |
4324 | return CRYPTO_siv128_aad(sctx, in, len); | |
4325 | ||
4326 | if (EVP_CIPHER_CTX_encrypting(ctx)) | |
4327 | return CRYPTO_siv128_encrypt(sctx, in, out, len); | |
4328 | ||
4329 | return CRYPTO_siv128_decrypt(sctx, in, out, len); | |
4330 | } | |
4331 | ||
4332 | #define aesni_siv_cleanup aes_siv_cleanup | |
4333 | static int aes_siv_cleanup(EVP_CIPHER_CTX *c) | |
4334 | { | |
4335 | SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, c); | |
4336 | ||
4337 | return CRYPTO_siv128_cleanup(sctx); | |
4338 | } | |
4339 | ||
4340 | ||
4341 | #define aesni_siv_ctrl aes_siv_ctrl | |
4342 | static int aes_siv_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |
4343 | { | |
4344 | SIV128_CONTEXT *sctx = EVP_C_DATA(SIV128_CONTEXT, c); | |
4345 | SIV128_CONTEXT *sctx_out; | |
4346 | ||
4347 | switch (type) { | |
4348 | case EVP_CTRL_INIT: | |
4349 | return CRYPTO_siv128_cleanup(sctx); | |
4350 | ||
4351 | case EVP_CTRL_SET_SPEED: | |
4352 | return CRYPTO_siv128_speed(sctx, arg); | |
4353 | ||
4354 | case EVP_CTRL_AEAD_SET_TAG: | |
4355 | if (!EVP_CIPHER_CTX_encrypting(c)) | |
4356 | return CRYPTO_siv128_set_tag(sctx, ptr, arg); | |
4357 | return 1; | |
4358 | ||
4359 | case EVP_CTRL_AEAD_GET_TAG: | |
4360 | if (!EVP_CIPHER_CTX_encrypting(c)) | |
4361 | return 0; | |
4362 | return CRYPTO_siv128_get_tag(sctx, ptr, arg); | |
4363 | ||
4364 | case EVP_CTRL_COPY: | |
4365 | sctx_out = EVP_C_DATA(SIV128_CONTEXT, (EVP_CIPHER_CTX*)ptr); | |
4366 | return CRYPTO_siv128_copy_ctx(sctx_out, sctx); | |
4367 | ||
4368 | default: | |
4369 | return -1; | |
4370 | ||
4371 | } | |
4372 | } | |
4373 | ||
4374 | #define SIV_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1 \ | |
4375 | | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ | |
4376 | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_COPY \ | |
4377 | | EVP_CIPH_CTRL_INIT) | |
4378 | ||
4379 | BLOCK_CIPHER_custom(NID_aes, 128, 1, 0, siv, SIV, SIV_FLAGS) | |
4380 | BLOCK_CIPHER_custom(NID_aes, 192, 1, 0, siv, SIV, SIV_FLAGS) | |
4381 | BLOCK_CIPHER_custom(NID_aes, 256, 1, 0, siv, SIV, SIV_FLAGS) | |
4382 | #endif |