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