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