/src/openssl/crypto/evp/e_aria.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
4 | | * |
5 | | * Licensed under the OpenSSL license (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include "internal/cryptlib.h" |
12 | | #ifndef OPENSSL_NO_ARIA |
13 | | # include <openssl/evp.h> |
14 | | # include <openssl/modes.h> |
15 | | # include <openssl/rand.h> |
16 | | # include <openssl/rand_drbg.h> |
17 | | # include "internal/aria.h" |
18 | | # include "internal/evp_int.h" |
19 | | # include "modes_lcl.h" |
20 | | # include "evp_locl.h" |
21 | | |
22 | | /* ARIA subkey Structure */ |
23 | | typedef struct { |
24 | | ARIA_KEY ks; |
25 | | } EVP_ARIA_KEY; |
26 | | |
27 | | /* ARIA GCM context */ |
28 | | typedef struct { |
29 | | union { |
30 | | double align; |
31 | | ARIA_KEY ks; |
32 | | } ks; /* ARIA subkey to use */ |
33 | | int key_set; /* Set if key initialised */ |
34 | | int iv_set; /* Set if an iv is set */ |
35 | | GCM128_CONTEXT gcm; |
36 | | unsigned char *iv; /* Temporary IV store */ |
37 | | int ivlen; /* IV length */ |
38 | | int taglen; |
39 | | int iv_gen; /* It is OK to generate IVs */ |
40 | | int tls_aad_len; /* TLS AAD length */ |
41 | | } EVP_ARIA_GCM_CTX; |
42 | | |
43 | | /* ARIA CCM context */ |
44 | | typedef struct { |
45 | | union { |
46 | | double align; |
47 | | ARIA_KEY ks; |
48 | | } ks; /* ARIA key schedule to use */ |
49 | | int key_set; /* Set if key initialised */ |
50 | | int iv_set; /* Set if an iv is set */ |
51 | | int tag_set; /* Set if tag is valid */ |
52 | | int len_set; /* Set if message length set */ |
53 | | int L, M; /* L and M parameters from RFC3610 */ |
54 | | int tls_aad_len; /* TLS AAD length */ |
55 | | CCM128_CONTEXT ccm; |
56 | | ccm128_f str; |
57 | | } EVP_ARIA_CCM_CTX; |
58 | | |
59 | | /* The subkey for ARIA is generated. */ |
60 | | static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
61 | | const unsigned char *iv, int enc) |
62 | 0 | { |
63 | 0 | int ret; |
64 | 0 | int mode = EVP_CIPHER_CTX_mode(ctx); |
65 | 0 |
|
66 | 0 | if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE)) |
67 | 0 | ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
68 | 0 | EVP_CIPHER_CTX_get_cipher_data(ctx)); |
69 | 0 | else |
70 | 0 | ret = aria_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
71 | 0 | EVP_CIPHER_CTX_get_cipher_data(ctx)); |
72 | 0 | if (ret < 0) { |
73 | 0 | EVPerr(EVP_F_ARIA_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED); |
74 | 0 | return 0; |
75 | 0 | } |
76 | 0 | return 1; |
77 | 0 | } |
78 | | |
79 | | static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out, |
80 | | size_t len, const ARIA_KEY *key, |
81 | | unsigned char *ivec, const int enc) |
82 | 0 | { |
83 | 0 |
|
84 | 0 | if (enc) |
85 | 0 | CRYPTO_cbc128_encrypt(in, out, len, key, ivec, |
86 | 0 | (block128_f) aria_encrypt); |
87 | 0 | else |
88 | 0 | CRYPTO_cbc128_decrypt(in, out, len, key, ivec, |
89 | 0 | (block128_f) aria_encrypt); |
90 | 0 | } |
91 | | |
92 | | static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out, |
93 | | size_t length, const ARIA_KEY *key, |
94 | | unsigned char *ivec, int *num, const int enc) |
95 | 0 | { |
96 | 0 |
|
97 | 0 | CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, |
98 | 0 | (block128_f) aria_encrypt); |
99 | 0 | } |
100 | | |
101 | | static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out, |
102 | | size_t length, const ARIA_KEY *key, |
103 | | unsigned char *ivec, int *num, const int enc) |
104 | 0 | { |
105 | 0 | CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc, |
106 | 0 | (block128_f) aria_encrypt); |
107 | 0 | } |
108 | | |
109 | | static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out, |
110 | | size_t length, const ARIA_KEY *key, |
111 | | unsigned char *ivec, int *num, const int enc) |
112 | 0 | { |
113 | 0 | CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc, |
114 | 0 | (block128_f) aria_encrypt); |
115 | 0 | } |
116 | | |
117 | | static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out, |
118 | | const ARIA_KEY *key, const int enc) |
119 | 0 | { |
120 | 0 | aria_encrypt(in, out, key); |
121 | 0 | } |
122 | | |
123 | | static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out, |
124 | | size_t length, const ARIA_KEY *key, |
125 | | unsigned char *ivec, int *num) |
126 | 0 | { |
127 | 0 | CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, |
128 | 0 | (block128_f) aria_encrypt); |
129 | 0 | } |
130 | | |
131 | | IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY, |
132 | | NID_aria_128, 16, 16, 16, 128, |
133 | | 0, aria_init_key, NULL, |
134 | | EVP_CIPHER_set_asn1_iv, |
135 | | EVP_CIPHER_get_asn1_iv, |
136 | | NULL) |
137 | | IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY, |
138 | | NID_aria_192, 16, 24, 16, 128, |
139 | | 0, aria_init_key, NULL, |
140 | | EVP_CIPHER_set_asn1_iv, |
141 | | EVP_CIPHER_get_asn1_iv, |
142 | | NULL) |
143 | | IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY, |
144 | | NID_aria_256, 16, 32, 16, 128, |
145 | | 0, aria_init_key, NULL, |
146 | | EVP_CIPHER_set_asn1_iv, |
147 | | EVP_CIPHER_get_asn1_iv, |
148 | | NULL) |
149 | | |
150 | | # define IMPLEMENT_ARIA_CFBR(ksize,cbits) \ |
151 | | IMPLEMENT_CFBR(aria,aria,EVP_ARIA_KEY,ks,ksize,cbits,16,0) |
152 | | IMPLEMENT_ARIA_CFBR(128,1) |
153 | | IMPLEMENT_ARIA_CFBR(192,1) |
154 | | IMPLEMENT_ARIA_CFBR(256,1) |
155 | | IMPLEMENT_ARIA_CFBR(128,8) |
156 | | IMPLEMENT_ARIA_CFBR(192,8) |
157 | | IMPLEMENT_ARIA_CFBR(256,8) |
158 | | |
159 | | # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ |
160 | | static const EVP_CIPHER aria_##keylen##_##mode = { \ |
161 | | nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \ |
162 | | flags|EVP_CIPH_##MODE##_MODE, \ |
163 | | aria_init_key, \ |
164 | | aria_##mode##_cipher, \ |
165 | | NULL, \ |
166 | | sizeof(EVP_ARIA_KEY), \ |
167 | | NULL,NULL,NULL,NULL }; \ |
168 | 24 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ |
169 | 24 | { return &aria_##keylen##_##mode; } Line | Count | Source | 168 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 169 | 8 | { return &aria_##keylen##_##mode; } |
Line | Count | Source | 168 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 169 | 8 | { return &aria_##keylen##_##mode; } |
Line | Count | Source | 168 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 169 | 8 | { return &aria_##keylen##_##mode; } |
|
170 | | |
171 | | static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
172 | | const unsigned char *in, size_t len) |
173 | 0 | { |
174 | 0 | unsigned int num = EVP_CIPHER_CTX_num(ctx); |
175 | 0 | EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY,ctx); |
176 | 0 |
|
177 | 0 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, |
178 | 0 | EVP_CIPHER_CTX_iv_noconst(ctx), |
179 | 0 | EVP_CIPHER_CTX_buf_noconst(ctx), &num, |
180 | 0 | (block128_f) aria_encrypt); |
181 | 0 | EVP_CIPHER_CTX_set_num(ctx, num); |
182 | 0 | return 1; |
183 | 0 | } |
184 | | |
185 | | BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0) |
186 | | BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0) |
187 | | BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0) |
188 | | |
189 | | /* Authenticated cipher modes (GCM/CCM) */ |
190 | | |
191 | | /* increment counter (64-bit int) by 1 */ |
192 | | static void ctr64_inc(unsigned char *counter) |
193 | 0 | { |
194 | 0 | int n = 8; |
195 | 0 | unsigned char c; |
196 | 0 |
|
197 | 0 | do { |
198 | 0 | --n; |
199 | 0 | c = counter[n]; |
200 | 0 | ++c; |
201 | 0 | counter[n] = c; |
202 | 0 | if (c) |
203 | 0 | return; |
204 | 0 | } while (n); |
205 | 0 | } |
206 | | |
207 | | static int aria_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
208 | | const unsigned char *iv, int enc) |
209 | 0 | { |
210 | 0 | int ret; |
211 | 0 | EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx); |
212 | 0 |
|
213 | 0 | if (!iv && !key) |
214 | 0 | return 1; |
215 | 0 | if (key) { |
216 | 0 | ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
217 | 0 | &gctx->ks.ks); |
218 | 0 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, |
219 | 0 | (block128_f) aria_encrypt); |
220 | 0 | if (ret < 0) { |
221 | 0 | EVPerr(EVP_F_ARIA_GCM_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED); |
222 | 0 | return 0; |
223 | 0 | } |
224 | 0 |
|
225 | 0 | /* |
226 | 0 | * If we have an iv can set it directly, otherwise use saved IV. |
227 | 0 | */ |
228 | 0 | if (iv == NULL && gctx->iv_set) |
229 | 0 | iv = gctx->iv; |
230 | 0 | if (iv) { |
231 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
232 | 0 | gctx->iv_set = 1; |
233 | 0 | } |
234 | 0 | gctx->key_set = 1; |
235 | 0 | } else { |
236 | 0 | /* If key set use IV, otherwise copy */ |
237 | 0 | if (gctx->key_set) |
238 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); |
239 | 0 | else |
240 | 0 | memcpy(gctx->iv, iv, gctx->ivlen); |
241 | 0 | gctx->iv_set = 1; |
242 | 0 | gctx->iv_gen = 0; |
243 | 0 | } |
244 | 0 | return 1; |
245 | 0 | } |
246 | | |
247 | | static int aria_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
248 | 0 | { |
249 | 0 | EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,c); |
250 | 0 |
|
251 | 0 | switch (type) { |
252 | 0 | case EVP_CTRL_INIT: |
253 | 0 | gctx->key_set = 0; |
254 | 0 | gctx->iv_set = 0; |
255 | 0 | gctx->ivlen = EVP_CIPHER_CTX_iv_length(c); |
256 | 0 | gctx->iv = EVP_CIPHER_CTX_iv_noconst(c); |
257 | 0 | gctx->taglen = -1; |
258 | 0 | gctx->iv_gen = 0; |
259 | 0 | gctx->tls_aad_len = -1; |
260 | 0 | return 1; |
261 | 0 |
|
262 | 0 | case EVP_CTRL_AEAD_SET_IVLEN: |
263 | 0 | if (arg <= 0) |
264 | 0 | return 0; |
265 | 0 | /* Allocate memory for IV if needed */ |
266 | 0 | if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { |
267 | 0 | if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c)) |
268 | 0 | OPENSSL_free(gctx->iv); |
269 | 0 | if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) { |
270 | 0 | EVPerr(EVP_F_ARIA_GCM_CTRL, ERR_R_MALLOC_FAILURE); |
271 | 0 | return 0; |
272 | 0 | } |
273 | 0 | } |
274 | 0 | gctx->ivlen = arg; |
275 | 0 | return 1; |
276 | 0 |
|
277 | 0 | case EVP_CTRL_AEAD_SET_TAG: |
278 | 0 | if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_encrypting(c)) |
279 | 0 | return 0; |
280 | 0 | memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
281 | 0 | gctx->taglen = arg; |
282 | 0 | return 1; |
283 | 0 |
|
284 | 0 | case EVP_CTRL_AEAD_GET_TAG: |
285 | 0 | if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_encrypting(c) |
286 | 0 | || gctx->taglen < 0) |
287 | 0 | return 0; |
288 | 0 | memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg); |
289 | 0 | return 1; |
290 | 0 |
|
291 | 0 | case EVP_CTRL_GCM_SET_IV_FIXED: |
292 | 0 | /* Special case: -1 length restores whole IV */ |
293 | 0 | if (arg == -1) { |
294 | 0 | memcpy(gctx->iv, ptr, gctx->ivlen); |
295 | 0 | gctx->iv_gen = 1; |
296 | 0 | return 1; |
297 | 0 | } |
298 | 0 | /* |
299 | 0 | * Fixed field must be at least 4 bytes and invocation field at least |
300 | 0 | * 8. |
301 | 0 | */ |
302 | 0 | if ((arg < 4) || (gctx->ivlen - arg) < 8) |
303 | 0 | return 0; |
304 | 0 | if (arg) |
305 | 0 | memcpy(gctx->iv, ptr, arg); |
306 | 0 | if (EVP_CIPHER_CTX_encrypting(c) |
307 | 0 | && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) |
308 | 0 | return 0; |
309 | 0 | gctx->iv_gen = 1; |
310 | 0 | return 1; |
311 | 0 |
|
312 | 0 | case EVP_CTRL_GCM_IV_GEN: |
313 | 0 | if (gctx->iv_gen == 0 || gctx->key_set == 0) |
314 | 0 | return 0; |
315 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); |
316 | 0 | if (arg <= 0 || arg > gctx->ivlen) |
317 | 0 | arg = gctx->ivlen; |
318 | 0 | memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); |
319 | 0 | /* |
320 | 0 | * Invocation field will be at least 8 bytes in size and so no need |
321 | 0 | * to check wrap around or increment more than last 8 bytes. |
322 | 0 | */ |
323 | 0 | ctr64_inc(gctx->iv + gctx->ivlen - 8); |
324 | 0 | gctx->iv_set = 1; |
325 | 0 | return 1; |
326 | 0 |
|
327 | 0 | case EVP_CTRL_GCM_SET_IV_INV: |
328 | 0 | if (gctx->iv_gen == 0 || gctx->key_set == 0 |
329 | 0 | || EVP_CIPHER_CTX_encrypting(c)) |
330 | 0 | return 0; |
331 | 0 | memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); |
332 | 0 | CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen); |
333 | 0 | gctx->iv_set = 1; |
334 | 0 | return 1; |
335 | 0 |
|
336 | 0 | case EVP_CTRL_AEAD_TLS1_AAD: |
337 | 0 | /* Save the AAD for later use */ |
338 | 0 | if (arg != EVP_AEAD_TLS1_AAD_LEN) |
339 | 0 | return 0; |
340 | 0 | memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
341 | 0 | gctx->tls_aad_len = arg; |
342 | 0 | { |
343 | 0 | unsigned int len = |
344 | 0 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8 |
345 | 0 | | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1]; |
346 | 0 | /* Correct length for explicit IV */ |
347 | 0 | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
348 | 0 | return 0; |
349 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
350 | 0 | /* If decrypting correct for tag too */ |
351 | 0 | if (!EVP_CIPHER_CTX_encrypting(c)) { |
352 | 0 | if (len < EVP_GCM_TLS_TAG_LEN) |
353 | 0 | return 0; |
354 | 0 | len -= EVP_GCM_TLS_TAG_LEN; |
355 | 0 | } |
356 | 0 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8; |
357 | 0 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff; |
358 | 0 | } |
359 | 0 | /* Extra padding: tag appended to record */ |
360 | 0 | return EVP_GCM_TLS_TAG_LEN; |
361 | 0 |
|
362 | 0 | case EVP_CTRL_COPY: |
363 | 0 | { |
364 | 0 | EVP_CIPHER_CTX *out = ptr; |
365 | 0 | EVP_ARIA_GCM_CTX *gctx_out = EVP_C_DATA(EVP_ARIA_GCM_CTX,out); |
366 | 0 | if (gctx->gcm.key) { |
367 | 0 | if (gctx->gcm.key != &gctx->ks) |
368 | 0 | return 0; |
369 | 0 | gctx_out->gcm.key = &gctx_out->ks; |
370 | 0 | } |
371 | 0 | if (gctx->iv == EVP_CIPHER_CTX_iv_noconst(c)) |
372 | 0 | gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out); |
373 | 0 | else { |
374 | 0 | if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) { |
375 | 0 | EVPerr(EVP_F_ARIA_GCM_CTRL, ERR_R_MALLOC_FAILURE); |
376 | 0 | return 0; |
377 | 0 | } |
378 | 0 | memcpy(gctx_out->iv, gctx->iv, gctx->ivlen); |
379 | 0 | } |
380 | 0 | return 1; |
381 | 0 | } |
382 | 0 |
|
383 | 0 | default: |
384 | 0 | return -1; |
385 | 0 |
|
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | static int aria_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
390 | | const unsigned char *in, size_t len) |
391 | 0 | { |
392 | 0 | EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx); |
393 | 0 | int rv = -1; |
394 | 0 |
|
395 | 0 | /* Encrypt/decrypt must be performed in place */ |
396 | 0 | if (out != in |
397 | 0 | || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
398 | 0 | return -1; |
399 | 0 | /* |
400 | 0 | * Set IV from start of buffer or generate IV and write to start of |
401 | 0 | * buffer. |
402 | 0 | */ |
403 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_encrypting(ctx) ? |
404 | 0 | EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, |
405 | 0 | EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) |
406 | 0 | goto err; |
407 | 0 | /* Use saved AAD */ |
408 | 0 | if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), |
409 | 0 | gctx->tls_aad_len)) |
410 | 0 | goto err; |
411 | 0 | /* Fix buffer and length to point to payload */ |
412 | 0 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
413 | 0 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
414 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
415 | 0 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
416 | 0 | /* Encrypt payload */ |
417 | 0 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) |
418 | 0 | goto err; |
419 | 0 | out += len; |
420 | 0 | /* Finally write tag */ |
421 | 0 | CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); |
422 | 0 | rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
423 | 0 | } else { |
424 | 0 | /* Decrypt */ |
425 | 0 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) |
426 | 0 | goto err; |
427 | 0 | /* Retrieve tag */ |
428 | 0 | CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), |
429 | 0 | EVP_GCM_TLS_TAG_LEN); |
430 | 0 | /* If tag mismatch wipe buffer */ |
431 | 0 | if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len, |
432 | 0 | EVP_GCM_TLS_TAG_LEN)) { |
433 | 0 | OPENSSL_cleanse(out, len); |
434 | 0 | goto err; |
435 | 0 | } |
436 | 0 | rv = len; |
437 | 0 | } |
438 | 0 |
|
439 | 0 | err: |
440 | 0 | gctx->iv_set = 0; |
441 | 0 | gctx->tls_aad_len = -1; |
442 | 0 | return rv; |
443 | 0 | } |
444 | | |
445 | | static int aria_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
446 | | const unsigned char *in, size_t len) |
447 | 0 | { |
448 | 0 | EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx); |
449 | 0 |
|
450 | 0 | /* If not set up, return error */ |
451 | 0 | if (!gctx->key_set) |
452 | 0 | return -1; |
453 | 0 | |
454 | 0 | if (gctx->tls_aad_len >= 0) |
455 | 0 | return aria_gcm_tls_cipher(ctx, out, in, len); |
456 | 0 | |
457 | 0 | if (!gctx->iv_set) |
458 | 0 | return -1; |
459 | 0 | if (in) { |
460 | 0 | if (out == NULL) { |
461 | 0 | if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) |
462 | 0 | return -1; |
463 | 0 | } else if (EVP_CIPHER_CTX_encrypting(ctx)) { |
464 | 0 | if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) |
465 | 0 | return -1; |
466 | 0 | } else { |
467 | 0 | if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) |
468 | 0 | return -1; |
469 | 0 | } |
470 | 0 | return len; |
471 | 0 | } |
472 | 0 | if (!EVP_CIPHER_CTX_encrypting(ctx)) { |
473 | 0 | if (gctx->taglen < 0) |
474 | 0 | return -1; |
475 | 0 | if (CRYPTO_gcm128_finish(&gctx->gcm, |
476 | 0 | EVP_CIPHER_CTX_buf_noconst(ctx), |
477 | 0 | gctx->taglen) != 0) |
478 | 0 | return -1; |
479 | 0 | gctx->iv_set = 0; |
480 | 0 | return 0; |
481 | 0 | } |
482 | 0 | CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16); |
483 | 0 | gctx->taglen = 16; |
484 | 0 | /* Don't reuse the IV */ |
485 | 0 | gctx->iv_set = 0; |
486 | 0 | return 0; |
487 | 0 | } |
488 | | |
489 | | static int aria_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
490 | | const unsigned char *iv, int enc) |
491 | 0 | { |
492 | 0 | int ret; |
493 | 0 | EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx); |
494 | 0 |
|
495 | 0 | if (!iv && !key) |
496 | 0 | return 1; |
497 | 0 | |
498 | 0 | if (key) { |
499 | 0 | ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, |
500 | 0 | &cctx->ks.ks); |
501 | 0 | CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, |
502 | 0 | &cctx->ks, (block128_f) aria_encrypt); |
503 | 0 | if (ret < 0) { |
504 | 0 | EVPerr(EVP_F_ARIA_CCM_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED); |
505 | 0 | return 0; |
506 | 0 | } |
507 | 0 | cctx->str = NULL; |
508 | 0 | cctx->key_set = 1; |
509 | 0 | } |
510 | 0 | if (iv) { |
511 | 0 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L); |
512 | 0 | cctx->iv_set = 1; |
513 | 0 | } |
514 | 0 | return 1; |
515 | 0 | } |
516 | | |
517 | | static int aria_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
518 | 0 | { |
519 | 0 | EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,c); |
520 | 0 |
|
521 | 0 | switch (type) { |
522 | 0 | case EVP_CTRL_INIT: |
523 | 0 | cctx->key_set = 0; |
524 | 0 | cctx->iv_set = 0; |
525 | 0 | cctx->L = 8; |
526 | 0 | cctx->M = 12; |
527 | 0 | cctx->tag_set = 0; |
528 | 0 | cctx->len_set = 0; |
529 | 0 | cctx->tls_aad_len = -1; |
530 | 0 | return 1; |
531 | 0 |
|
532 | 0 | case EVP_CTRL_AEAD_TLS1_AAD: |
533 | 0 | /* Save the AAD for later use */ |
534 | 0 | if (arg != EVP_AEAD_TLS1_AAD_LEN) |
535 | 0 | return 0; |
536 | 0 | memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
537 | 0 | cctx->tls_aad_len = arg; |
538 | 0 | { |
539 | 0 | uint16_t len = |
540 | 0 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8 |
541 | 0 | | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1]; |
542 | 0 | /* Correct length for explicit IV */ |
543 | 0 | if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN) |
544 | 0 | return 0; |
545 | 0 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN; |
546 | 0 | /* If decrypting correct for tag too */ |
547 | 0 | if (!EVP_CIPHER_CTX_encrypting(c)) { |
548 | 0 | if (len < cctx->M) |
549 | 0 | return 0; |
550 | 0 | len -= cctx->M; |
551 | 0 | } |
552 | 0 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8; |
553 | 0 | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff; |
554 | 0 | } |
555 | 0 | /* Extra padding: tag appended to record */ |
556 | 0 | return cctx->M; |
557 | 0 |
|
558 | 0 | case EVP_CTRL_CCM_SET_IV_FIXED: |
559 | 0 | /* Sanity check length */ |
560 | 0 | if (arg != EVP_CCM_TLS_FIXED_IV_LEN) |
561 | 0 | return 0; |
562 | 0 | /* Just copy to first part of IV */ |
563 | 0 | memcpy(EVP_CIPHER_CTX_iv_noconst(c), ptr, arg); |
564 | 0 | return 1; |
565 | 0 |
|
566 | 0 | case EVP_CTRL_AEAD_SET_IVLEN: |
567 | 0 | arg = 15 - arg; |
568 | 0 | /* fall thru */ |
569 | 0 | case EVP_CTRL_CCM_SET_L: |
570 | 0 | if (arg < 2 || arg > 8) |
571 | 0 | return 0; |
572 | 0 | cctx->L = arg; |
573 | 0 | return 1; |
574 | 0 | case EVP_CTRL_AEAD_SET_TAG: |
575 | 0 | if ((arg & 1) || arg < 4 || arg > 16) |
576 | 0 | return 0; |
577 | 0 | if (EVP_CIPHER_CTX_encrypting(c) && ptr) |
578 | 0 | return 0; |
579 | 0 | if (ptr) { |
580 | 0 | cctx->tag_set = 1; |
581 | 0 | memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg); |
582 | 0 | } |
583 | 0 | cctx->M = arg; |
584 | 0 | return 1; |
585 | 0 |
|
586 | 0 | case EVP_CTRL_AEAD_GET_TAG: |
587 | 0 | if (!EVP_CIPHER_CTX_encrypting(c) || !cctx->tag_set) |
588 | 0 | return 0; |
589 | 0 | if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) |
590 | 0 | return 0; |
591 | 0 | cctx->tag_set = 0; |
592 | 0 | cctx->iv_set = 0; |
593 | 0 | cctx->len_set = 0; |
594 | 0 | return 1; |
595 | 0 |
|
596 | 0 | case EVP_CTRL_COPY: |
597 | 0 | { |
598 | 0 | EVP_CIPHER_CTX *out = ptr; |
599 | 0 | EVP_ARIA_CCM_CTX *cctx_out = EVP_C_DATA(EVP_ARIA_CCM_CTX,out); |
600 | 0 | if (cctx->ccm.key) { |
601 | 0 | if (cctx->ccm.key != &cctx->ks) |
602 | 0 | return 0; |
603 | 0 | cctx_out->ccm.key = &cctx_out->ks; |
604 | 0 | } |
605 | 0 | return 1; |
606 | 0 | } |
607 | 0 |
|
608 | 0 | default: |
609 | 0 | return -1; |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | static int aria_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
614 | | const unsigned char *in, size_t len) |
615 | 0 | { |
616 | 0 | EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx); |
617 | 0 | CCM128_CONTEXT *ccm = &cctx->ccm; |
618 | 0 |
|
619 | 0 | /* Encrypt/decrypt must be performed in place */ |
620 | 0 | if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M)) |
621 | 0 | return -1; |
622 | 0 | /* If encrypting set explicit IV from sequence number (start of AAD) */ |
623 | 0 | if (EVP_CIPHER_CTX_encrypting(ctx)) |
624 | 0 | memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx), |
625 | 0 | EVP_CCM_TLS_EXPLICIT_IV_LEN); |
626 | 0 | /* Get rest of IV from explicit IV */ |
627 | 0 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx) + EVP_CCM_TLS_FIXED_IV_LEN, in, |
628 | 0 | EVP_CCM_TLS_EXPLICIT_IV_LEN); |
629 | 0 | /* Correct length value */ |
630 | 0 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M; |
631 | 0 | if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), 15 - cctx->L, |
632 | 0 | len)) |
633 | 0 | return -1; |
634 | 0 | /* Use saved AAD */ |
635 | 0 | CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), cctx->tls_aad_len); |
636 | 0 | /* Fix buffer to point to payload */ |
637 | 0 | in += EVP_CCM_TLS_EXPLICIT_IV_LEN; |
638 | 0 | out += EVP_CCM_TLS_EXPLICIT_IV_LEN; |
639 | 0 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
640 | 0 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str) |
641 | 0 | : CRYPTO_ccm128_encrypt(ccm, in, out, len)) |
642 | 0 | return -1; |
643 | 0 | if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M)) |
644 | 0 | return -1; |
645 | 0 | return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M; |
646 | 0 | } else { |
647 | 0 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, cctx->str) |
648 | 0 | : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { |
649 | 0 | unsigned char tag[16]; |
650 | 0 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { |
651 | 0 | if (!CRYPTO_memcmp(tag, in + len, cctx->M)) |
652 | 0 | return len; |
653 | 0 | } |
654 | 0 | } |
655 | 0 | OPENSSL_cleanse(out, len); |
656 | 0 | return -1; |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | | static int aria_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
661 | | const unsigned char *in, size_t len) |
662 | 0 | { |
663 | 0 | EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx); |
664 | 0 | CCM128_CONTEXT *ccm = &cctx->ccm; |
665 | 0 |
|
666 | 0 | /* If not set up, return error */ |
667 | 0 | if (!cctx->key_set) |
668 | 0 | return -1; |
669 | 0 | |
670 | 0 | if (cctx->tls_aad_len >= 0) |
671 | 0 | return aria_ccm_tls_cipher(ctx, out, in, len); |
672 | 0 | |
673 | 0 | /* EVP_*Final() doesn't return any data */ |
674 | 0 | if (in == NULL && out != NULL) |
675 | 0 | return 0; |
676 | 0 | |
677 | 0 | if (!cctx->iv_set) |
678 | 0 | return -1; |
679 | 0 | |
680 | 0 | if (!EVP_CIPHER_CTX_encrypting(ctx) && !cctx->tag_set) |
681 | 0 | return -1; |
682 | 0 | if (!out) { |
683 | 0 | if (!in) { |
684 | 0 | if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), |
685 | 0 | 15 - cctx->L, len)) |
686 | 0 | return -1; |
687 | 0 | cctx->len_set = 1; |
688 | 0 | return len; |
689 | 0 | } |
690 | 0 | /* If have AAD need message length */ |
691 | 0 | if (!cctx->len_set && len) |
692 | 0 | return -1; |
693 | 0 | CRYPTO_ccm128_aad(ccm, in, len); |
694 | 0 | return len; |
695 | 0 | } |
696 | 0 | /* If not set length yet do it */ |
697 | 0 | if (!cctx->len_set) { |
698 | 0 | if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), |
699 | 0 | 15 - cctx->L, len)) |
700 | 0 | return -1; |
701 | 0 | cctx->len_set = 1; |
702 | 0 | } |
703 | 0 | if (EVP_CIPHER_CTX_encrypting(ctx)) { |
704 | 0 | if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str) |
705 | 0 | : CRYPTO_ccm128_encrypt(ccm, in, out, len)) |
706 | 0 | return -1; |
707 | 0 | cctx->tag_set = 1; |
708 | 0 | return len; |
709 | 0 | } else { |
710 | 0 | int rv = -1; |
711 | 0 | if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, |
712 | 0 | cctx->str) : |
713 | 0 | !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { |
714 | 0 | unsigned char tag[16]; |
715 | 0 | if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { |
716 | 0 | if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx), |
717 | 0 | cctx->M)) |
718 | 0 | rv = len; |
719 | 0 | } |
720 | 0 | } |
721 | 0 | if (rv == -1) |
722 | 0 | OPENSSL_cleanse(out, len); |
723 | 0 | cctx->iv_set = 0; |
724 | 0 | cctx->tag_set = 0; |
725 | 0 | cctx->len_set = 0; |
726 | 0 | return rv; |
727 | 0 | } |
728 | 0 | } |
729 | | |
730 | | #define ARIA_AUTH_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ |
731 | | | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ |
732 | | | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \ |
733 | | | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER) |
734 | | |
735 | | #define BLOCK_CIPHER_aead(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ |
736 | | static const EVP_CIPHER aria_##keylen##_##mode = { \ |
737 | | nid##_##keylen##_##nmode, \ |
738 | | blocksize, keylen/8, ivlen, \ |
739 | | ARIA_AUTH_FLAGS|EVP_CIPH_##MODE##_MODE, \ |
740 | | aria_##mode##_init_key, \ |
741 | | aria_##mode##_cipher, \ |
742 | | NULL, \ |
743 | | sizeof(EVP_ARIA_##MODE##_CTX), \ |
744 | | NULL,NULL,aria_##mode##_ctrl,NULL }; \ |
745 | 48 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ |
746 | 48 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } Line | Count | Source | 745 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 746 | 8 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } |
Line | Count | Source | 745 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 746 | 8 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } |
Line | Count | Source | 745 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 746 | 8 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } |
Line | Count | Source | 745 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 746 | 8 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } |
Line | Count | Source | 745 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 746 | 8 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } |
Line | Count | Source | 745 | 8 | const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \ | 746 | 8 | { return (EVP_CIPHER*)&aria_##keylen##_##mode; } |
|
747 | | |
748 | | BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, gcm, gcm, GCM, 0) |
749 | | BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, gcm, gcm, GCM, 0) |
750 | | BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, gcm, gcm, GCM, 0) |
751 | | |
752 | | BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, ccm, ccm, CCM, 0) |
753 | | BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, ccm, ccm, CCM, 0) |
754 | | BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, ccm, ccm, CCM, 0) |
755 | | |
756 | | #endif |