xref: /freebsd-src/crypto/openssl/providers/implementations/ciphers/ciphercommon_ccm.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1b077aed3SPierre Pronchery /*
2b077aed3SPierre Pronchery  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /* Dispatch functions for ccm mode */
11b077aed3SPierre Pronchery 
12b077aed3SPierre Pronchery #include <openssl/proverr.h>
13b077aed3SPierre Pronchery #include "prov/ciphercommon.h"
14b077aed3SPierre Pronchery #include "prov/ciphercommon_ccm.h"
15b077aed3SPierre Pronchery #include "prov/providercommon.h"
16b077aed3SPierre Pronchery 
17b077aed3SPierre Pronchery static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
18b077aed3SPierre Pronchery                                size_t *padlen, const unsigned char *in,
19b077aed3SPierre Pronchery                                size_t len);
20b077aed3SPierre Pronchery 
ccm_tls_init(PROV_CCM_CTX * ctx,unsigned char * aad,size_t alen)21b077aed3SPierre Pronchery static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen)
22b077aed3SPierre Pronchery {
23b077aed3SPierre Pronchery     size_t len;
24b077aed3SPierre Pronchery 
25b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
26b077aed3SPierre Pronchery         return 0;
27b077aed3SPierre Pronchery 
28b077aed3SPierre Pronchery     /* Save the aad for later use. */
29b077aed3SPierre Pronchery     memcpy(ctx->buf, aad, alen);
30b077aed3SPierre Pronchery     ctx->tls_aad_len = alen;
31b077aed3SPierre Pronchery 
32b077aed3SPierre Pronchery     len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
33b077aed3SPierre Pronchery     if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
34b077aed3SPierre Pronchery         return 0;
35b077aed3SPierre Pronchery 
36b077aed3SPierre Pronchery     /* Correct length for explicit iv. */
37b077aed3SPierre Pronchery     len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
38b077aed3SPierre Pronchery 
39b077aed3SPierre Pronchery     if (!ctx->enc) {
40b077aed3SPierre Pronchery         if (len < ctx->m)
41b077aed3SPierre Pronchery             return 0;
42b077aed3SPierre Pronchery         /* Correct length for tag. */
43b077aed3SPierre Pronchery         len -= ctx->m;
44b077aed3SPierre Pronchery     }
45b077aed3SPierre Pronchery     ctx->buf[alen - 2] = (unsigned char)(len >> 8);
46b077aed3SPierre Pronchery     ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
47b077aed3SPierre Pronchery 
48b077aed3SPierre Pronchery     /* Extra padding: tag appended to record. */
49b077aed3SPierre Pronchery     return ctx->m;
50b077aed3SPierre Pronchery }
51b077aed3SPierre Pronchery 
ccm_tls_iv_set_fixed(PROV_CCM_CTX * ctx,unsigned char * fixed,size_t flen)52b077aed3SPierre Pronchery static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed,
53b077aed3SPierre Pronchery                                 size_t flen)
54b077aed3SPierre Pronchery {
55b077aed3SPierre Pronchery     if (flen != EVP_CCM_TLS_FIXED_IV_LEN)
56b077aed3SPierre Pronchery         return 0;
57b077aed3SPierre Pronchery 
58b077aed3SPierre Pronchery     /* Copy to first part of the iv. */
59b077aed3SPierre Pronchery     memcpy(ctx->iv, fixed, flen);
60b077aed3SPierre Pronchery     return 1;
61b077aed3SPierre Pronchery }
62b077aed3SPierre Pronchery 
ccm_get_ivlen(PROV_CCM_CTX * ctx)63b077aed3SPierre Pronchery static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx)
64b077aed3SPierre Pronchery {
65b077aed3SPierre Pronchery     return 15 - ctx->l;
66b077aed3SPierre Pronchery }
67b077aed3SPierre Pronchery 
ossl_ccm_set_ctx_params(void * vctx,const OSSL_PARAM params[])68b077aed3SPierre Pronchery int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
69b077aed3SPierre Pronchery {
70b077aed3SPierre Pronchery     PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
71b077aed3SPierre Pronchery     const OSSL_PARAM *p;
72b077aed3SPierre Pronchery     size_t sz;
73b077aed3SPierre Pronchery 
74b077aed3SPierre Pronchery     if (params == NULL)
75b077aed3SPierre Pronchery         return 1;
76b077aed3SPierre Pronchery 
77b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
78b077aed3SPierre Pronchery     if (p != NULL) {
79b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
80b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
81b077aed3SPierre Pronchery             return 0;
82b077aed3SPierre Pronchery         }
83b077aed3SPierre Pronchery         if ((p->data_size & 1) || (p->data_size < 4) || p->data_size > 16) {
84b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
85b077aed3SPierre Pronchery             return 0;
86b077aed3SPierre Pronchery         }
87b077aed3SPierre Pronchery 
88b077aed3SPierre Pronchery         if (p->data != NULL) {
89b077aed3SPierre Pronchery             if (ctx->enc) {
90b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
91b077aed3SPierre Pronchery                 return 0;
92b077aed3SPierre Pronchery             }
93b077aed3SPierre Pronchery             memcpy(ctx->buf, p->data, p->data_size);
94b077aed3SPierre Pronchery             ctx->tag_set = 1;
95b077aed3SPierre Pronchery         }
96b077aed3SPierre Pronchery         ctx->m = p->data_size;
97b077aed3SPierre Pronchery     }
98b077aed3SPierre Pronchery 
99b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
100b077aed3SPierre Pronchery     if (p != NULL) {
101b077aed3SPierre Pronchery         size_t ivlen;
102b077aed3SPierre Pronchery 
103b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_size_t(p, &sz)) {
104b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
105b077aed3SPierre Pronchery             return 0;
106b077aed3SPierre Pronchery         }
107b077aed3SPierre Pronchery         ivlen = 15 - sz;
108b077aed3SPierre Pronchery         if (ivlen < 2 || ivlen > 8) {
109b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
110b077aed3SPierre Pronchery             return 0;
111b077aed3SPierre Pronchery         }
112*e0c4386eSCy Schubert         if (ctx->l != ivlen) {
113b077aed3SPierre Pronchery             ctx->l = ivlen;
114*e0c4386eSCy Schubert             ctx->iv_set = 0;
115*e0c4386eSCy Schubert         }
116b077aed3SPierre Pronchery     }
117b077aed3SPierre Pronchery 
118b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
119b077aed3SPierre Pronchery     if (p != NULL) {
120b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
121b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
122b077aed3SPierre Pronchery             return 0;
123b077aed3SPierre Pronchery         }
124b077aed3SPierre Pronchery         sz = ccm_tls_init(ctx, p->data, p->data_size);
125b077aed3SPierre Pronchery         if (sz == 0) {
126b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
127b077aed3SPierre Pronchery             return 0;
128b077aed3SPierre Pronchery         }
129b077aed3SPierre Pronchery         ctx->tls_aad_pad_sz = sz;
130b077aed3SPierre Pronchery     }
131b077aed3SPierre Pronchery 
132b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
133b077aed3SPierre Pronchery     if (p != NULL) {
134b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
135b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
136b077aed3SPierre Pronchery             return 0;
137b077aed3SPierre Pronchery         }
138b077aed3SPierre Pronchery         if (ccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
139b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
140b077aed3SPierre Pronchery             return 0;
141b077aed3SPierre Pronchery         }
142b077aed3SPierre Pronchery     }
143b077aed3SPierre Pronchery 
144b077aed3SPierre Pronchery     return 1;
145b077aed3SPierre Pronchery }
146b077aed3SPierre Pronchery 
ossl_ccm_get_ctx_params(void * vctx,OSSL_PARAM params[])147b077aed3SPierre Pronchery int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
148b077aed3SPierre Pronchery {
149b077aed3SPierre Pronchery     PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
150b077aed3SPierre Pronchery     OSSL_PARAM *p;
151b077aed3SPierre Pronchery 
152b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
153b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, ccm_get_ivlen(ctx))) {
154b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
155b077aed3SPierre Pronchery         return 0;
156b077aed3SPierre Pronchery     }
157b077aed3SPierre Pronchery 
158b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
159b077aed3SPierre Pronchery     if (p != NULL) {
160b077aed3SPierre Pronchery         size_t m = ctx->m;
161b077aed3SPierre Pronchery 
162b077aed3SPierre Pronchery         if (!OSSL_PARAM_set_size_t(p, m)) {
163b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
164b077aed3SPierre Pronchery             return 0;
165b077aed3SPierre Pronchery         }
166b077aed3SPierre Pronchery     }
167b077aed3SPierre Pronchery 
168b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
169b077aed3SPierre Pronchery     if (p != NULL) {
170b077aed3SPierre Pronchery         if (ccm_get_ivlen(ctx) > p->data_size) {
171b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
172b077aed3SPierre Pronchery             return 0;
173b077aed3SPierre Pronchery         }
174b077aed3SPierre Pronchery         if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
175b077aed3SPierre Pronchery             && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
176b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
177b077aed3SPierre Pronchery             return 0;
178b077aed3SPierre Pronchery         }
179b077aed3SPierre Pronchery     }
180b077aed3SPierre Pronchery 
181b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
182b077aed3SPierre Pronchery     if (p != NULL) {
183b077aed3SPierre Pronchery         if (ccm_get_ivlen(ctx) > p->data_size) {
184b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
185b077aed3SPierre Pronchery             return 0;
186b077aed3SPierre Pronchery         }
187b077aed3SPierre Pronchery         if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
188b077aed3SPierre Pronchery             && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
189b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
190b077aed3SPierre Pronchery             return 0;
191b077aed3SPierre Pronchery         }
192b077aed3SPierre Pronchery     }
193b077aed3SPierre Pronchery 
194b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
195b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
196b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
197b077aed3SPierre Pronchery         return 0;
198b077aed3SPierre Pronchery     }
199b077aed3SPierre Pronchery 
200b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
201b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
202b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
203b077aed3SPierre Pronchery         return 0;
204b077aed3SPierre Pronchery     }
205b077aed3SPierre Pronchery 
206b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
207b077aed3SPierre Pronchery     if (p != NULL) {
208b077aed3SPierre Pronchery         if (!ctx->enc || !ctx->tag_set) {
209b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
210b077aed3SPierre Pronchery             return 0;
211b077aed3SPierre Pronchery         }
212b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
213b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
214b077aed3SPierre Pronchery             return 0;
215b077aed3SPierre Pronchery         }
216b077aed3SPierre Pronchery         if (!ctx->hw->gettag(ctx, p->data, p->data_size))
217b077aed3SPierre Pronchery             return 0;
218b077aed3SPierre Pronchery         ctx->tag_set = 0;
219b077aed3SPierre Pronchery         ctx->iv_set = 0;
220b077aed3SPierre Pronchery         ctx->len_set = 0;
221b077aed3SPierre Pronchery     }
222b077aed3SPierre Pronchery     return 1;
223b077aed3SPierre Pronchery }
224b077aed3SPierre Pronchery 
ccm_init(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[],int enc)225b077aed3SPierre Pronchery static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
226b077aed3SPierre Pronchery                     const unsigned char *iv, size_t ivlen,
227b077aed3SPierre Pronchery                     const OSSL_PARAM params[], int enc)
228b077aed3SPierre Pronchery {
229b077aed3SPierre Pronchery     PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
230b077aed3SPierre Pronchery 
231b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
232b077aed3SPierre Pronchery         return 0;
233b077aed3SPierre Pronchery 
234b077aed3SPierre Pronchery     ctx->enc = enc;
235b077aed3SPierre Pronchery 
236b077aed3SPierre Pronchery     if (iv != NULL) {
237b077aed3SPierre Pronchery         if (ivlen != ccm_get_ivlen(ctx)) {
238b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
239b077aed3SPierre Pronchery             return 0;
240b077aed3SPierre Pronchery         }
241b077aed3SPierre Pronchery         memcpy(ctx->iv, iv, ivlen);
242b077aed3SPierre Pronchery         ctx->iv_set = 1;
243b077aed3SPierre Pronchery     }
244b077aed3SPierre Pronchery     if (key != NULL) {
245b077aed3SPierre Pronchery         if (keylen != ctx->keylen) {
246b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
247b077aed3SPierre Pronchery             return 0;
248b077aed3SPierre Pronchery         }
249b077aed3SPierre Pronchery         if (!ctx->hw->setkey(ctx, key, keylen))
250b077aed3SPierre Pronchery             return 0;
251b077aed3SPierre Pronchery     }
252b077aed3SPierre Pronchery     return ossl_ccm_set_ctx_params(ctx, params);
253b077aed3SPierre Pronchery }
254b077aed3SPierre Pronchery 
ossl_ccm_einit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])255b077aed3SPierre Pronchery int ossl_ccm_einit(void *vctx, const unsigned char *key, size_t keylen,
256b077aed3SPierre Pronchery                    const unsigned char *iv, size_t ivlen,
257b077aed3SPierre Pronchery                    const OSSL_PARAM params[])
258b077aed3SPierre Pronchery {
259b077aed3SPierre Pronchery     return ccm_init(vctx, key, keylen, iv, ivlen, params, 1);
260b077aed3SPierre Pronchery }
261b077aed3SPierre Pronchery 
ossl_ccm_dinit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])262b077aed3SPierre Pronchery int ossl_ccm_dinit(void *vctx, const unsigned char *key, size_t keylen,
263b077aed3SPierre Pronchery                    const unsigned char *iv, size_t ivlen,
264b077aed3SPierre Pronchery                    const OSSL_PARAM params[])
265b077aed3SPierre Pronchery {
266b077aed3SPierre Pronchery     return ccm_init(vctx, key, keylen, iv, ivlen, params, 0);
267b077aed3SPierre Pronchery }
268b077aed3SPierre Pronchery 
ossl_ccm_stream_update(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)269b077aed3SPierre Pronchery int ossl_ccm_stream_update(void *vctx, unsigned char *out, size_t *outl,
270b077aed3SPierre Pronchery                            size_t outsize, const unsigned char *in,
271b077aed3SPierre Pronchery                            size_t inl)
272b077aed3SPierre Pronchery {
273b077aed3SPierre Pronchery     PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
274b077aed3SPierre Pronchery 
275b077aed3SPierre Pronchery     if (outsize < inl) {
276b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
277b077aed3SPierre Pronchery         return 0;
278b077aed3SPierre Pronchery     }
279b077aed3SPierre Pronchery 
280b077aed3SPierre Pronchery     if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
281b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
282b077aed3SPierre Pronchery         return 0;
283b077aed3SPierre Pronchery     }
284b077aed3SPierre Pronchery     return 1;
285b077aed3SPierre Pronchery }
286b077aed3SPierre Pronchery 
ossl_ccm_stream_final(void * vctx,unsigned char * out,size_t * outl,size_t outsize)287b077aed3SPierre Pronchery int ossl_ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,
288b077aed3SPierre Pronchery                           size_t outsize)
289b077aed3SPierre Pronchery {
290b077aed3SPierre Pronchery     PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
291b077aed3SPierre Pronchery     int i;
292b077aed3SPierre Pronchery 
293b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
294b077aed3SPierre Pronchery         return 0;
295b077aed3SPierre Pronchery 
296b077aed3SPierre Pronchery     i = ccm_cipher_internal(ctx, out, outl, NULL, 0);
297b077aed3SPierre Pronchery     if (i <= 0)
298b077aed3SPierre Pronchery         return 0;
299b077aed3SPierre Pronchery 
300b077aed3SPierre Pronchery     *outl = 0;
301b077aed3SPierre Pronchery     return 1;
302b077aed3SPierre Pronchery }
303b077aed3SPierre Pronchery 
ossl_ccm_cipher(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)304b077aed3SPierre Pronchery int ossl_ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize,
305b077aed3SPierre Pronchery                     const unsigned char *in, size_t inl)
306b077aed3SPierre Pronchery {
307b077aed3SPierre Pronchery     PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
308b077aed3SPierre Pronchery 
309b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
310b077aed3SPierre Pronchery         return 0;
311b077aed3SPierre Pronchery 
312b077aed3SPierre Pronchery     if (outsize < inl) {
313b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
314b077aed3SPierre Pronchery         return 0;
315b077aed3SPierre Pronchery     }
316b077aed3SPierre Pronchery 
317b077aed3SPierre Pronchery     if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
318b077aed3SPierre Pronchery         return 0;
319b077aed3SPierre Pronchery 
320b077aed3SPierre Pronchery     *outl = inl;
321b077aed3SPierre Pronchery     return 1;
322b077aed3SPierre Pronchery }
323b077aed3SPierre Pronchery 
324b077aed3SPierre Pronchery /* Copy the buffered iv */
ccm_set_iv(PROV_CCM_CTX * ctx,size_t mlen)325b077aed3SPierre Pronchery static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
326b077aed3SPierre Pronchery {
327b077aed3SPierre Pronchery     const PROV_CCM_HW *hw = ctx->hw;
328b077aed3SPierre Pronchery 
329b077aed3SPierre Pronchery     if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
330b077aed3SPierre Pronchery         return 0;
331b077aed3SPierre Pronchery     ctx->len_set = 1;
332b077aed3SPierre Pronchery     return 1;
333b077aed3SPierre Pronchery }
334b077aed3SPierre Pronchery 
ccm_tls_cipher(PROV_CCM_CTX * ctx,unsigned char * out,size_t * padlen,const unsigned char * in,size_t len)335b077aed3SPierre Pronchery static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
336b077aed3SPierre Pronchery                           unsigned char *out, size_t *padlen,
337b077aed3SPierre Pronchery                           const unsigned char *in, size_t len)
338b077aed3SPierre Pronchery {
339b077aed3SPierre Pronchery     int rv = 0;
340b077aed3SPierre Pronchery     size_t olen = 0;
341b077aed3SPierre Pronchery 
342b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
343b077aed3SPierre Pronchery         goto err;
344b077aed3SPierre Pronchery 
345b077aed3SPierre Pronchery     /* Encrypt/decrypt must be performed in place */
346b077aed3SPierre Pronchery     if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
347b077aed3SPierre Pronchery         goto err;
348b077aed3SPierre Pronchery 
349b077aed3SPierre Pronchery     /* If encrypting set explicit IV from sequence number (start of AAD) */
350b077aed3SPierre Pronchery     if (ctx->enc)
351b077aed3SPierre Pronchery         memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
352b077aed3SPierre Pronchery     /* Get rest of IV from explicit IV */
353b077aed3SPierre Pronchery     memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
354b077aed3SPierre Pronchery     /* Correct length value */
355b077aed3SPierre Pronchery     len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
356b077aed3SPierre Pronchery     if (!ccm_set_iv(ctx, len))
357b077aed3SPierre Pronchery         goto err;
358b077aed3SPierre Pronchery 
359b077aed3SPierre Pronchery     /* Use saved AAD */
360b077aed3SPierre Pronchery     if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
361b077aed3SPierre Pronchery         goto err;
362b077aed3SPierre Pronchery 
363b077aed3SPierre Pronchery     /* Fix buffer to point to payload */
364b077aed3SPierre Pronchery     in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
365b077aed3SPierre Pronchery     out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
366b077aed3SPierre Pronchery     if (ctx->enc) {
367b077aed3SPierre Pronchery         if (!ctx->hw->auth_encrypt(ctx, in, out, len,  out + len, ctx->m))
368b077aed3SPierre Pronchery             goto err;
369b077aed3SPierre Pronchery         olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
370b077aed3SPierre Pronchery     } else {
371b077aed3SPierre Pronchery         if (!ctx->hw->auth_decrypt(ctx, in, out, len,
372b077aed3SPierre Pronchery                                    (unsigned char *)in + len, ctx->m))
373b077aed3SPierre Pronchery             goto err;
374b077aed3SPierre Pronchery         olen = len;
375b077aed3SPierre Pronchery     }
376b077aed3SPierre Pronchery     rv = 1;
377b077aed3SPierre Pronchery err:
378b077aed3SPierre Pronchery     *padlen = olen;
379b077aed3SPierre Pronchery     return rv;
380b077aed3SPierre Pronchery }
381b077aed3SPierre Pronchery 
ccm_cipher_internal(PROV_CCM_CTX * ctx,unsigned char * out,size_t * padlen,const unsigned char * in,size_t len)382b077aed3SPierre Pronchery static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
383b077aed3SPierre Pronchery                                size_t *padlen, const unsigned char *in,
384b077aed3SPierre Pronchery                                size_t len)
385b077aed3SPierre Pronchery {
386b077aed3SPierre Pronchery     int rv = 0;
387b077aed3SPierre Pronchery     size_t olen = 0;
388b077aed3SPierre Pronchery     const PROV_CCM_HW *hw = ctx->hw;
389b077aed3SPierre Pronchery 
390b077aed3SPierre Pronchery     /* If no key set, return error */
391b077aed3SPierre Pronchery     if (!ctx->key_set)
392b077aed3SPierre Pronchery         return 0;
393b077aed3SPierre Pronchery 
394b077aed3SPierre Pronchery     if (ctx->tls_aad_len != UNINITIALISED_SIZET)
395b077aed3SPierre Pronchery         return ccm_tls_cipher(ctx, out, padlen, in, len);
396b077aed3SPierre Pronchery 
397b077aed3SPierre Pronchery     /* EVP_*Final() doesn't return any data */
398b077aed3SPierre Pronchery     if (in == NULL && out != NULL)
399b077aed3SPierre Pronchery         goto finish;
400b077aed3SPierre Pronchery 
401b077aed3SPierre Pronchery     if (!ctx->iv_set)
402b077aed3SPierre Pronchery         goto err;
403b077aed3SPierre Pronchery 
404b077aed3SPierre Pronchery     if (out == NULL) {
405b077aed3SPierre Pronchery         if (in == NULL) {
406b077aed3SPierre Pronchery             if (!ccm_set_iv(ctx, len))
407b077aed3SPierre Pronchery                 goto err;
408b077aed3SPierre Pronchery         } else {
409b077aed3SPierre Pronchery             /* If we have AAD, we need a message length */
410b077aed3SPierre Pronchery             if (!ctx->len_set && len)
411b077aed3SPierre Pronchery                 goto err;
412b077aed3SPierre Pronchery             if (!hw->setaad(ctx, in, len))
413b077aed3SPierre Pronchery                 goto err;
414b077aed3SPierre Pronchery         }
415b077aed3SPierre Pronchery     } else {
416b077aed3SPierre Pronchery         /* If not set length yet do it */
417b077aed3SPierre Pronchery         if (!ctx->len_set && !ccm_set_iv(ctx, len))
418b077aed3SPierre Pronchery             goto err;
419b077aed3SPierre Pronchery 
420b077aed3SPierre Pronchery         if (ctx->enc) {
421b077aed3SPierre Pronchery             if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
422b077aed3SPierre Pronchery                 goto err;
423b077aed3SPierre Pronchery             ctx->tag_set = 1;
424b077aed3SPierre Pronchery         } else {
425b077aed3SPierre Pronchery             /* The tag must be set before actually decrypting data */
426b077aed3SPierre Pronchery             if (!ctx->tag_set)
427b077aed3SPierre Pronchery                 goto err;
428b077aed3SPierre Pronchery 
429b077aed3SPierre Pronchery             if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
430b077aed3SPierre Pronchery                 goto err;
431b077aed3SPierre Pronchery             /* Finished - reset flags so calling this method again will fail */
432b077aed3SPierre Pronchery             ctx->iv_set = 0;
433b077aed3SPierre Pronchery             ctx->tag_set = 0;
434b077aed3SPierre Pronchery             ctx->len_set = 0;
435b077aed3SPierre Pronchery         }
436b077aed3SPierre Pronchery     }
437b077aed3SPierre Pronchery     olen = len;
438b077aed3SPierre Pronchery finish:
439b077aed3SPierre Pronchery     rv = 1;
440b077aed3SPierre Pronchery err:
441b077aed3SPierre Pronchery     *padlen = olen;
442b077aed3SPierre Pronchery     return rv;
443b077aed3SPierre Pronchery }
444b077aed3SPierre Pronchery 
ossl_ccm_initctx(PROV_CCM_CTX * ctx,size_t keybits,const PROV_CCM_HW * hw)445b077aed3SPierre Pronchery void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
446b077aed3SPierre Pronchery {
447b077aed3SPierre Pronchery     ctx->keylen = keybits / 8;
448b077aed3SPierre Pronchery     ctx->key_set = 0;
449b077aed3SPierre Pronchery     ctx->iv_set = 0;
450b077aed3SPierre Pronchery     ctx->tag_set = 0;
451b077aed3SPierre Pronchery     ctx->len_set = 0;
452b077aed3SPierre Pronchery     ctx->l = 8;
453b077aed3SPierre Pronchery     ctx->m = 12;
454b077aed3SPierre Pronchery     ctx->tls_aad_len = UNINITIALISED_SIZET;
455b077aed3SPierre Pronchery     ctx->hw = hw;
456b077aed3SPierre Pronchery }
457