/src/openssl/providers/implementations/ciphers/ciphercommon_ccm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
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 |
8 | | */ |
9 | | |
10 | | |
11 | | /* Dispatch functions for ccm mode */ |
12 | | |
13 | | #include <openssl/proverr.h> |
14 | | #include "prov/ciphercommon.h" |
15 | | #include "prov/ciphercommon_ccm.h" |
16 | | #include "prov/providercommon.h" |
17 | | |
18 | | |
19 | | static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out, |
20 | | size_t *padlen, const unsigned char *in, |
21 | | size_t len); |
22 | | |
23 | | static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen) |
24 | 0 | { |
25 | 0 | size_t len; |
26 | |
|
27 | 0 | if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN) |
28 | 0 | return 0; |
29 | | |
30 | | /* Save the aad for later use. */ |
31 | 0 | memcpy(ctx->buf, aad, alen); |
32 | 0 | ctx->tls_aad_len = alen; |
33 | |
|
34 | 0 | len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1]; |
35 | 0 | if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN) |
36 | 0 | return 0; |
37 | | |
38 | | /* Correct length for explicit iv. */ |
39 | 0 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN; |
40 | |
|
41 | 0 | if (!ctx->enc) { |
42 | 0 | if (len < ctx->m) |
43 | 0 | return 0; |
44 | | /* Correct length for tag. */ |
45 | 0 | len -= ctx->m; |
46 | 0 | } |
47 | 0 | ctx->buf[alen - 2] = (unsigned char)(len >> 8); |
48 | 0 | ctx->buf[alen - 1] = (unsigned char)(len & 0xff); |
49 | | |
50 | | /* Extra padding: tag appended to record. */ |
51 | 0 | return (int)ctx->m; |
52 | 0 | } |
53 | | |
54 | | static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed, |
55 | | size_t flen) |
56 | 0 | { |
57 | 0 | if (flen != EVP_CCM_TLS_FIXED_IV_LEN) |
58 | 0 | return 0; |
59 | | |
60 | | /* Copy to first part of the iv. */ |
61 | 0 | memcpy(ctx->iv, fixed, flen); |
62 | 0 | return 1; |
63 | 0 | } |
64 | | |
65 | | static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx) |
66 | 0 | { |
67 | 0 | return 15 - ctx->l; |
68 | 0 | } |
69 | | |
70 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
71 | | #ifndef ossl_cipher_ccm_set_ctx_params_list |
72 | | static const OSSL_PARAM ossl_cipher_ccm_set_ctx_params_list[] = { |
73 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL), |
74 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
75 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), |
76 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0), |
77 | | OSSL_PARAM_END |
78 | | }; |
79 | | #endif |
80 | | |
81 | | #ifndef ossl_cipher_ccm_set_ctx_params_st |
82 | | struct ossl_cipher_ccm_set_ctx_params_st { |
83 | | OSSL_PARAM *aad; |
84 | | OSSL_PARAM *fixed; |
85 | | OSSL_PARAM *ivlen; |
86 | | OSSL_PARAM *tag; |
87 | | }; |
88 | | #endif |
89 | | |
90 | | #ifndef ossl_cipher_ccm_set_ctx_params_decoder |
91 | | static int ossl_cipher_ccm_set_ctx_params_decoder |
92 | | (const OSSL_PARAM *p, struct ossl_cipher_ccm_set_ctx_params_st *r) |
93 | 0 | { |
94 | 0 | const char *s; |
95 | |
|
96 | 0 | memset(r, 0, sizeof(*r)); |
97 | 0 | if (p != NULL) |
98 | 0 | for (; (s = p->key) != NULL; p++) |
99 | 0 | switch(s[0]) { |
100 | 0 | default: |
101 | 0 | break; |
102 | 0 | case 'i': |
103 | 0 | if (ossl_likely(strcmp("vlen", s + 1) == 0)) { |
104 | 0 | if (ossl_likely(r->ivlen == NULL)) |
105 | 0 | r->ivlen = (OSSL_PARAM *)p; |
106 | 0 | } |
107 | 0 | break; |
108 | 0 | case 't': |
109 | 0 | switch(s[1]) { |
110 | 0 | default: |
111 | 0 | break; |
112 | 0 | case 'a': |
113 | 0 | if (ossl_likely(strcmp("g", s + 2) == 0)) { |
114 | 0 | if (ossl_likely(r->tag == NULL)) |
115 | 0 | r->tag = (OSSL_PARAM *)p; |
116 | 0 | } |
117 | 0 | break; |
118 | 0 | case 'l': |
119 | 0 | switch(s[2]) { |
120 | 0 | default: |
121 | 0 | break; |
122 | 0 | case 's': |
123 | 0 | switch(s[3]) { |
124 | 0 | default: |
125 | 0 | break; |
126 | 0 | case 'a': |
127 | 0 | if (ossl_likely(strcmp("ad", s + 4) == 0)) { |
128 | 0 | if (ossl_likely(r->aad == NULL)) |
129 | 0 | r->aad = (OSSL_PARAM *)p; |
130 | 0 | } |
131 | 0 | break; |
132 | 0 | case 'i': |
133 | 0 | if (ossl_likely(strcmp("vfixed", s + 4) == 0)) { |
134 | 0 | if (ossl_likely(r->fixed == NULL)) |
135 | 0 | r->fixed = (OSSL_PARAM *)p; |
136 | 0 | } |
137 | 0 | } |
138 | 0 | } |
139 | 0 | } |
140 | 0 | } |
141 | 0 | return 1; |
142 | 0 | } |
143 | | #endif |
144 | | /* End of machine generated */ |
145 | | |
146 | | const OSSL_PARAM *ossl_ccm_settable_ctx_params( |
147 | | ossl_unused void *cctx, ossl_unused void *provctx |
148 | | ) |
149 | 0 | { |
150 | 0 | return ossl_cipher_ccm_set_ctx_params_list; |
151 | 0 | } |
152 | | |
153 | | int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
154 | 0 | { |
155 | 0 | PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; |
156 | 0 | size_t sz, ivlen; |
157 | 0 | struct ossl_cipher_ccm_set_ctx_params_st p; |
158 | |
|
159 | 0 | if (ctx == NULL || !ossl_cipher_ccm_set_ctx_params_decoder(params, &p)) |
160 | 0 | return 0; |
161 | | |
162 | 0 | if (p.tag != NULL) { |
163 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
164 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
165 | 0 | return 0; |
166 | 0 | } |
167 | 0 | if ((p.tag->data_size & 1) || (p.tag->data_size < 4) || p.tag->data_size > 16) { |
168 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
169 | 0 | return 0; |
170 | 0 | } |
171 | | |
172 | 0 | if (p.tag->data != NULL) { |
173 | 0 | if (ctx->enc) { |
174 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
175 | 0 | return 0; |
176 | 0 | } |
177 | 0 | memcpy(ctx->buf, p.tag->data, p.tag->data_size); |
178 | 0 | ctx->tag_set = 1; |
179 | 0 | } |
180 | 0 | ctx->m = p.tag->data_size; |
181 | 0 | } |
182 | | |
183 | 0 | if (p.ivlen != NULL) { |
184 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) { |
185 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | ivlen = 15 - sz; |
189 | 0 | if (ivlen < 2 || ivlen > 8) { |
190 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
191 | 0 | return 0; |
192 | 0 | } |
193 | 0 | if (ctx->l != ivlen) { |
194 | 0 | ctx->l = ivlen; |
195 | 0 | ctx->iv_set = 0; |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | 0 | if (p.aad != NULL) { |
200 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
201 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | sz = ccm_tls_init(ctx, p.aad->data, p.aad->data_size); |
205 | 0 | if (sz == 0) { |
206 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
207 | 0 | return 0; |
208 | 0 | } |
209 | 0 | ctx->tls_aad_pad_sz = sz; |
210 | 0 | } |
211 | | |
212 | 0 | if (p.fixed != NULL) { |
213 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
214 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
215 | 0 | return 0; |
216 | 0 | } |
217 | 0 | if (ccm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) { |
218 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
219 | 0 | return 0; |
220 | 0 | } |
221 | 0 | } |
222 | 0 | return 1; |
223 | 0 | } |
224 | | |
225 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
226 | | #ifndef ossl_cipher_ccm_get_ctx_params_list |
227 | | static const OSSL_PARAM ossl_cipher_ccm_get_ctx_params_list[] = { |
228 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
229 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
230 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL), |
231 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), |
232 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0), |
233 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
234 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), |
235 | | OSSL_PARAM_END |
236 | | }; |
237 | | #endif |
238 | | |
239 | | #ifndef ossl_cipher_ccm_get_ctx_params_st |
240 | | struct ossl_cipher_ccm_get_ctx_params_st { |
241 | | OSSL_PARAM *iv; |
242 | | OSSL_PARAM *ivlen; |
243 | | OSSL_PARAM *keylen; |
244 | | OSSL_PARAM *pad; |
245 | | OSSL_PARAM *tag; |
246 | | OSSL_PARAM *taglen; |
247 | | OSSL_PARAM *updiv; |
248 | | }; |
249 | | #endif |
250 | | |
251 | | #ifndef ossl_cipher_ccm_get_ctx_params_decoder |
252 | | static int ossl_cipher_ccm_get_ctx_params_decoder |
253 | | (const OSSL_PARAM *p, struct ossl_cipher_ccm_get_ctx_params_st *r) |
254 | 0 | { |
255 | 0 | const char *s; |
256 | |
|
257 | 0 | memset(r, 0, sizeof(*r)); |
258 | 0 | if (p != NULL) |
259 | 0 | for (; (s = p->key) != NULL; p++) |
260 | 0 | switch(s[0]) { |
261 | 0 | default: |
262 | 0 | break; |
263 | 0 | case 'i': |
264 | 0 | switch(s[1]) { |
265 | 0 | default: |
266 | 0 | break; |
267 | 0 | case 'v': |
268 | 0 | switch(s[2]) { |
269 | 0 | default: |
270 | 0 | break; |
271 | 0 | case 'l': |
272 | 0 | if (ossl_likely(strcmp("en", s + 3) == 0)) { |
273 | 0 | if (ossl_likely(r->ivlen == NULL)) |
274 | 0 | r->ivlen = (OSSL_PARAM *)p; |
275 | 0 | } |
276 | 0 | break; |
277 | 0 | case '\0': |
278 | 0 | if (ossl_likely(r->iv == NULL)) |
279 | 0 | r->iv = (OSSL_PARAM *)p; |
280 | 0 | } |
281 | 0 | } |
282 | 0 | break; |
283 | 0 | case 'k': |
284 | 0 | if (ossl_likely(strcmp("eylen", s + 1) == 0)) { |
285 | 0 | if (ossl_likely(r->keylen == NULL)) |
286 | 0 | r->keylen = (OSSL_PARAM *)p; |
287 | 0 | } |
288 | 0 | break; |
289 | 0 | case 't': |
290 | 0 | switch(s[1]) { |
291 | 0 | default: |
292 | 0 | break; |
293 | 0 | case 'a': |
294 | 0 | switch(s[2]) { |
295 | 0 | default: |
296 | 0 | break; |
297 | 0 | case 'g': |
298 | 0 | switch(s[3]) { |
299 | 0 | default: |
300 | 0 | break; |
301 | 0 | case 'l': |
302 | 0 | if (ossl_likely(strcmp("en", s + 4) == 0)) { |
303 | 0 | if (ossl_likely(r->taglen == NULL)) |
304 | 0 | r->taglen = (OSSL_PARAM *)p; |
305 | 0 | } |
306 | 0 | break; |
307 | 0 | case '\0': |
308 | 0 | if (ossl_likely(r->tag == NULL)) |
309 | 0 | r->tag = (OSSL_PARAM *)p; |
310 | 0 | } |
311 | 0 | } |
312 | 0 | break; |
313 | 0 | case 'l': |
314 | 0 | if (ossl_likely(strcmp("saadpad", s + 2) == 0)) { |
315 | 0 | if (ossl_likely(r->pad == NULL)) |
316 | 0 | r->pad = (OSSL_PARAM *)p; |
317 | 0 | } |
318 | 0 | } |
319 | 0 | break; |
320 | 0 | case 'u': |
321 | 0 | if (ossl_likely(strcmp("pdated-iv", s + 1) == 0)) { |
322 | 0 | if (ossl_likely(r->updiv == NULL)) |
323 | 0 | r->updiv = (OSSL_PARAM *)p; |
324 | 0 | } |
325 | 0 | } |
326 | 0 | return 1; |
327 | 0 | } |
328 | | #endif |
329 | | /* End of machine generated */ |
330 | | |
331 | | const OSSL_PARAM *ossl_ccm_gettable_ctx_params( |
332 | | ossl_unused void *cctx, ossl_unused void *provctx |
333 | | ) |
334 | 112 | { |
335 | 112 | return ossl_cipher_ccm_get_ctx_params_list; |
336 | 112 | } |
337 | | |
338 | | int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
339 | 0 | { |
340 | 0 | PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; |
341 | 0 | struct ossl_cipher_ccm_get_ctx_params_st p; |
342 | |
|
343 | 0 | if (ctx == NULL || !ossl_cipher_ccm_get_ctx_params_decoder(params, &p)) |
344 | 0 | return 0; |
345 | | |
346 | 0 | if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, ccm_get_ivlen(ctx))) { |
347 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
348 | 0 | return 0; |
349 | 0 | } |
350 | | |
351 | 0 | if (p.taglen != NULL && !OSSL_PARAM_set_size_t(p.taglen, ctx->m)) { |
352 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
353 | 0 | return 0; |
354 | 0 | } |
355 | | |
356 | 0 | if (p.iv != NULL) { |
357 | 0 | if (ccm_get_ivlen(ctx) > p.iv->data_size) { |
358 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
359 | 0 | return 0; |
360 | 0 | } |
361 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.iv, ctx->iv, p.iv->data_size)) { |
362 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
363 | 0 | return 0; |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | 0 | if (p.updiv != NULL) { |
368 | 0 | if (ccm_get_ivlen(ctx) > p.updiv->data_size) { |
369 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
370 | 0 | return 0; |
371 | 0 | } |
372 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.updiv, ctx->iv, p.updiv->data_size)) { |
373 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
374 | 0 | return 0; |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | 0 | if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) { |
379 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
380 | 0 | return 0; |
381 | 0 | } |
382 | | |
383 | 0 | if (p.pad != NULL && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
384 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
385 | 0 | return 0; |
386 | 0 | } |
387 | | |
388 | 0 | if (p.tag != NULL) { |
389 | 0 | if (!ctx->enc || !ctx->tag_set) { |
390 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
391 | 0 | return 0; |
392 | 0 | } |
393 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
394 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
395 | 0 | return 0; |
396 | 0 | } |
397 | 0 | if (!ctx->hw->gettag(ctx, p.tag->data, p.tag->data_size)) |
398 | 0 | return 0; |
399 | 0 | ctx->tag_set = 0; |
400 | 0 | ctx->iv_set = 0; |
401 | 0 | ctx->len_set = 0; |
402 | 0 | } |
403 | | |
404 | 0 | return 1; |
405 | 0 | } |
406 | | |
407 | | static int ccm_init(void *vctx, const unsigned char *key, size_t keylen, |
408 | | const unsigned char *iv, size_t ivlen, |
409 | | const OSSL_PARAM params[], int enc) |
410 | 0 | { |
411 | 0 | PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; |
412 | |
|
413 | 0 | if (!ossl_prov_is_running()) |
414 | 0 | return 0; |
415 | | |
416 | 0 | ctx->enc = enc; |
417 | |
|
418 | 0 | if (iv != NULL) { |
419 | 0 | if (ivlen != ccm_get_ivlen(ctx)) { |
420 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
421 | 0 | return 0; |
422 | 0 | } |
423 | 0 | memcpy(ctx->iv, iv, ivlen); |
424 | 0 | ctx->iv_set = 1; |
425 | 0 | } |
426 | 0 | if (key != NULL) { |
427 | 0 | if (keylen != ctx->keylen) { |
428 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
429 | 0 | return 0; |
430 | 0 | } |
431 | 0 | if (!ctx->hw->setkey(ctx, key, keylen)) |
432 | 0 | return 0; |
433 | 0 | } |
434 | 0 | return ossl_ccm_set_ctx_params(ctx, params); |
435 | 0 | } |
436 | | |
437 | | int ossl_ccm_einit(void *vctx, const unsigned char *key, size_t keylen, |
438 | | const unsigned char *iv, size_t ivlen, |
439 | | const OSSL_PARAM params[]) |
440 | 0 | { |
441 | 0 | return ccm_init(vctx, key, keylen, iv, ivlen, params, 1); |
442 | 0 | } |
443 | | |
444 | | int ossl_ccm_dinit(void *vctx, const unsigned char *key, size_t keylen, |
445 | | const unsigned char *iv, size_t ivlen, |
446 | | const OSSL_PARAM params[]) |
447 | 0 | { |
448 | 0 | return ccm_init(vctx, key, keylen, iv, ivlen, params, 0); |
449 | 0 | } |
450 | | |
451 | | int ossl_ccm_stream_update(void *vctx, unsigned char *out, size_t *outl, |
452 | | size_t outsize, const unsigned char *in, |
453 | | size_t inl) |
454 | 0 | { |
455 | 0 | PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; |
456 | |
|
457 | 0 | if (outsize < inl) { |
458 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
459 | 0 | return 0; |
460 | 0 | } |
461 | | |
462 | 0 | if (!ccm_cipher_internal(ctx, out, outl, in, inl)) { |
463 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
464 | 0 | return 0; |
465 | 0 | } |
466 | 0 | return 1; |
467 | 0 | } |
468 | | |
469 | | int ossl_ccm_stream_final(void *vctx, unsigned char *out, size_t *outl, |
470 | | size_t outsize) |
471 | 0 | { |
472 | 0 | PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; |
473 | 0 | int i; |
474 | |
|
475 | 0 | if (!ossl_prov_is_running()) |
476 | 0 | return 0; |
477 | | |
478 | 0 | i = ccm_cipher_internal(ctx, out, outl, NULL, 0); |
479 | 0 | if (i <= 0) |
480 | 0 | return 0; |
481 | | |
482 | 0 | *outl = 0; |
483 | 0 | return 1; |
484 | 0 | } |
485 | | |
486 | | int ossl_ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize, |
487 | | const unsigned char *in, size_t inl) |
488 | 0 | { |
489 | 0 | PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; |
490 | |
|
491 | 0 | if (!ossl_prov_is_running()) |
492 | 0 | return 0; |
493 | | |
494 | 0 | if (outsize < inl) { |
495 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
496 | 0 | return 0; |
497 | 0 | } |
498 | | |
499 | 0 | if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0) |
500 | 0 | return 0; |
501 | | |
502 | 0 | *outl = inl; |
503 | 0 | return 1; |
504 | 0 | } |
505 | | |
506 | | /* Copy the buffered iv */ |
507 | | static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen) |
508 | 0 | { |
509 | 0 | const PROV_CCM_HW *hw = ctx->hw; |
510 | |
|
511 | 0 | if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen)) |
512 | 0 | return 0; |
513 | 0 | ctx->len_set = 1; |
514 | 0 | return 1; |
515 | 0 | } |
516 | | |
517 | | static int ccm_tls_cipher(PROV_CCM_CTX *ctx, |
518 | | unsigned char *out, size_t *padlen, |
519 | | const unsigned char *in, size_t len) |
520 | 0 | { |
521 | 0 | int rv = 0; |
522 | 0 | size_t olen = 0; |
523 | |
|
524 | 0 | if (!ossl_prov_is_running()) |
525 | 0 | goto err; |
526 | | |
527 | | /* Encrypt/decrypt must be performed in place */ |
528 | 0 | if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m) |
529 | 0 | goto err; |
530 | | |
531 | | /* If encrypting set explicit IV from sequence number (start of AAD) */ |
532 | 0 | if (ctx->enc) |
533 | 0 | memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN); |
534 | | /* Get rest of IV from explicit IV */ |
535 | 0 | memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN); |
536 | | /* Correct length value */ |
537 | 0 | len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m; |
538 | 0 | if (!ccm_set_iv(ctx, len)) |
539 | 0 | goto err; |
540 | | |
541 | | /* Use saved AAD */ |
542 | 0 | if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len)) |
543 | 0 | goto err; |
544 | | |
545 | | /* Fix buffer to point to payload */ |
546 | 0 | in += EVP_CCM_TLS_EXPLICIT_IV_LEN; |
547 | 0 | out += EVP_CCM_TLS_EXPLICIT_IV_LEN; |
548 | 0 | if (ctx->enc) { |
549 | 0 | if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m)) |
550 | 0 | goto err; |
551 | 0 | olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m; |
552 | 0 | } else { |
553 | 0 | if (!ctx->hw->auth_decrypt(ctx, in, out, len, |
554 | 0 | (unsigned char *)in + len, ctx->m)) |
555 | 0 | goto err; |
556 | 0 | olen = len; |
557 | 0 | } |
558 | 0 | rv = 1; |
559 | 0 | err: |
560 | 0 | *padlen = olen; |
561 | 0 | return rv; |
562 | 0 | } |
563 | | |
564 | | static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out, |
565 | | size_t *padlen, const unsigned char *in, |
566 | | size_t len) |
567 | 0 | { |
568 | 0 | int rv = 0; |
569 | 0 | size_t olen = 0; |
570 | 0 | const PROV_CCM_HW *hw = ctx->hw; |
571 | | |
572 | | /* If no key set, return error */ |
573 | 0 | if (!ctx->key_set) |
574 | 0 | return 0; |
575 | | |
576 | 0 | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
577 | 0 | return ccm_tls_cipher(ctx, out, padlen, in, len); |
578 | | |
579 | | /* EVP_*Final() doesn't return any data */ |
580 | 0 | if (in == NULL && out != NULL) |
581 | 0 | goto finish; |
582 | | |
583 | 0 | if (!ctx->iv_set) |
584 | 0 | goto err; |
585 | | |
586 | 0 | if (out == NULL) { |
587 | 0 | if (in == NULL) { |
588 | 0 | if (!ccm_set_iv(ctx, len)) |
589 | 0 | goto err; |
590 | 0 | } else { |
591 | | /* If we have AAD, we need a message length */ |
592 | 0 | if (!ctx->len_set && len) |
593 | 0 | goto err; |
594 | 0 | if (!hw->setaad(ctx, in, len)) |
595 | 0 | goto err; |
596 | 0 | } |
597 | 0 | } else { |
598 | | /* If not set length yet do it */ |
599 | 0 | if (!ctx->len_set && !ccm_set_iv(ctx, len)) |
600 | 0 | goto err; |
601 | | |
602 | 0 | if (ctx->enc) { |
603 | 0 | if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0)) |
604 | 0 | goto err; |
605 | 0 | ctx->tag_set = 1; |
606 | 0 | } else { |
607 | | /* The tag must be set before actually decrypting data */ |
608 | 0 | if (!ctx->tag_set) |
609 | 0 | goto err; |
610 | | |
611 | 0 | if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m)) |
612 | 0 | goto err; |
613 | | /* Finished - reset flags so calling this method again will fail */ |
614 | 0 | ctx->iv_set = 0; |
615 | 0 | ctx->tag_set = 0; |
616 | 0 | ctx->len_set = 0; |
617 | 0 | } |
618 | 0 | } |
619 | 0 | olen = len; |
620 | 0 | finish: |
621 | 0 | rv = 1; |
622 | 0 | err: |
623 | 0 | *padlen = olen; |
624 | 0 | return rv; |
625 | 0 | } |
626 | | |
627 | | void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw) |
628 | 0 | { |
629 | 0 | ctx->keylen = keybits / 8; |
630 | 0 | ctx->key_set = 0; |
631 | 0 | ctx->iv_set = 0; |
632 | 0 | ctx->tag_set = 0; |
633 | 0 | ctx->len_set = 0; |
634 | 0 | ctx->l = 8; |
635 | 0 | ctx->m = 12; |
636 | 0 | ctx->tls_aad_len = UNINITIALISED_SIZET; |
637 | 0 | ctx->hw = hw; |
638 | 0 | } |