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