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