Coverage Report

Created: 2025-08-25 06:30

/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
                    /* CIPHER_PARAM_AEAD_IVLEN */
105
0
                    if (ossl_unlikely(r->ivlen != NULL)) {
106
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
107
0
                                       "param %s is repeated", s);
108
0
                        return 0;
109
0
                    }
110
0
                    r->ivlen = (OSSL_PARAM *)p;
111
0
                }
112
0
                break;
113
0
            case 't':
114
0
                switch(s[1]) {
115
0
                default:
116
0
                    break;
117
0
                case 'a':
118
0
                    if (ossl_likely(strcmp("g", s + 2) == 0)) {
119
                        /* CIPHER_PARAM_AEAD_TAG */
120
0
                        if (ossl_unlikely(r->tag != NULL)) {
121
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
122
0
                                           "param %s is repeated", s);
123
0
                            return 0;
124
0
                        }
125
0
                        r->tag = (OSSL_PARAM *)p;
126
0
                    }
127
0
                    break;
128
0
                case 'l':
129
0
                    switch(s[2]) {
130
0
                    default:
131
0
                        break;
132
0
                    case 's':
133
0
                        switch(s[3]) {
134
0
                        default:
135
0
                            break;
136
0
                        case 'a':
137
0
                            if (ossl_likely(strcmp("ad", s + 4) == 0)) {
138
                                /* CIPHER_PARAM_AEAD_TLS1_AAD */
139
0
                                if (ossl_unlikely(r->aad != NULL)) {
140
0
                                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
141
0
                                                   "param %s is repeated", s);
142
0
                                    return 0;
143
0
                                }
144
0
                                r->aad = (OSSL_PARAM *)p;
145
0
                            }
146
0
                            break;
147
0
                        case 'i':
148
0
                            if (ossl_likely(strcmp("vfixed", s + 4) == 0)) {
149
                                /* CIPHER_PARAM_AEAD_TLS1_IV_FIXED */
150
0
                                if (ossl_unlikely(r->fixed != NULL)) {
151
0
                                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
152
0
                                                   "param %s is repeated", s);
153
0
                                    return 0;
154
0
                                }
155
0
                                r->fixed = (OSSL_PARAM *)p;
156
0
                            }
157
0
                        }
158
0
                    }
159
0
                }
160
0
            }
161
0
    return 1;
162
0
}
163
#endif
164
/* End of machine generated */
165
166
const OSSL_PARAM *ossl_ccm_settable_ctx_params(
167
        ossl_unused void *cctx, ossl_unused void *provctx
168
    )
169
0
{
170
0
    return ossl_cipher_ccm_set_ctx_params_list;
171
0
}
172
173
int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
174
0
{
175
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
176
0
    size_t sz, ivlen;
177
0
    struct ossl_cipher_ccm_set_ctx_params_st p;
178
179
0
    if (ctx == NULL || !ossl_cipher_ccm_set_ctx_params_decoder(params, &p))
180
0
        return 0;
181
182
0
    if (p.tag != NULL) {
183
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
184
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
185
0
            return 0;
186
0
        }
187
0
        if ((p.tag->data_size & 1) || (p.tag->data_size < 4) || p.tag->data_size > 16) {
188
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
189
0
            return 0;
190
0
        }
191
192
0
        if (p.tag->data != NULL) {
193
0
            if (ctx->enc) {
194
0
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
195
0
                return 0;
196
0
            }
197
0
            memcpy(ctx->buf, p.tag->data, p.tag->data_size);
198
0
            ctx->tag_set = 1;
199
0
        }
200
0
        ctx->m = p.tag->data_size;
201
0
    }
202
203
0
    if (p.ivlen != NULL) {
204
0
        if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) {
205
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
206
0
            return 0;
207
0
        }
208
0
        ivlen = 15 - sz;
209
0
        if (ivlen < 2 || ivlen > 8) {
210
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
211
0
            return 0;
212
0
        }
213
0
        if (ctx->l != ivlen) {
214
0
            ctx->l = ivlen;
215
0
            ctx->iv_set = 0;
216
0
        }
217
0
    }
218
219
0
    if (p.aad != NULL) {
220
0
        if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) {
221
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
222
0
            return 0;
223
0
        }
224
0
        sz = ccm_tls_init(ctx, p.aad->data, p.aad->data_size);
225
0
        if (sz == 0) {
226
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
227
0
            return 0;
228
0
        }
229
0
        ctx->tls_aad_pad_sz = sz;
230
0
    }
231
232
0
    if (p.fixed != NULL) {
233
0
        if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) {
234
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
235
0
            return 0;
236
0
        }
237
0
        if (ccm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) {
238
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
239
0
            return 0;
240
0
        }
241
0
    }
242
0
    return 1;
243
0
}
244
245
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
246
#ifndef ossl_cipher_ccm_get_ctx_params_list
247
static const OSSL_PARAM ossl_cipher_ccm_get_ctx_params_list[] = {
248
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
249
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
250
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
251
    OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
252
    OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
253
    OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
254
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
255
    OSSL_PARAM_END
256
};
257
#endif
258
259
#ifndef ossl_cipher_ccm_get_ctx_params_st
260
struct ossl_cipher_ccm_get_ctx_params_st {
261
    OSSL_PARAM *iv;
262
    OSSL_PARAM *ivlen;
263
    OSSL_PARAM *keylen;
264
    OSSL_PARAM *pad;
265
    OSSL_PARAM *tag;
266
    OSSL_PARAM *taglen;
267
    OSSL_PARAM *updiv;
268
};
269
#endif
270
271
#ifndef ossl_cipher_ccm_get_ctx_params_decoder
272
static int ossl_cipher_ccm_get_ctx_params_decoder
273
    (const OSSL_PARAM *p, struct ossl_cipher_ccm_get_ctx_params_st *r)
274
0
{
275
0
    const char *s;
276
277
0
    memset(r, 0, sizeof(*r));
278
0
    if (p != NULL)
279
0
        for (; (s = p->key) != NULL; p++)
280
0
            switch(s[0]) {
281
0
            default:
282
0
                break;
283
0
            case 'i':
284
0
                switch(s[1]) {
285
0
                default:
286
0
                    break;
287
0
                case 'v':
288
0
                    switch(s[2]) {
289
0
                    default:
290
0
                        break;
291
0
                    case 'l':
292
0
                        if (ossl_likely(strcmp("en", s + 3) == 0)) {
293
                            /* CIPHER_PARAM_IVLEN */
294
0
                            if (ossl_unlikely(r->ivlen != NULL)) {
295
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
296
0
                                               "param %s is repeated", s);
297
0
                                return 0;
298
0
                            }
299
0
                            r->ivlen = (OSSL_PARAM *)p;
300
0
                        }
301
0
                        break;
302
0
                    case '\0':
303
0
                        if (ossl_unlikely(r->iv != NULL)) {
304
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
305
0
                                           "param %s is repeated", s);
306
0
                            return 0;
307
0
                        }
308
0
                        r->iv = (OSSL_PARAM *)p;
309
0
                    }
310
0
                }
311
0
                break;
312
0
            case 'k':
313
0
                if (ossl_likely(strcmp("eylen", s + 1) == 0)) {
314
                    /* CIPHER_PARAM_KEYLEN */
315
0
                    if (ossl_unlikely(r->keylen != NULL)) {
316
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
317
0
                                       "param %s is repeated", s);
318
0
                        return 0;
319
0
                    }
320
0
                    r->keylen = (OSSL_PARAM *)p;
321
0
                }
322
0
                break;
323
0
            case 't':
324
0
                switch(s[1]) {
325
0
                default:
326
0
                    break;
327
0
                case 'a':
328
0
                    switch(s[2]) {
329
0
                    default:
330
0
                        break;
331
0
                    case 'g':
332
0
                        switch(s[3]) {
333
0
                        default:
334
0
                            break;
335
0
                        case 'l':
336
0
                            if (ossl_likely(strcmp("en", s + 4) == 0)) {
337
                                /* CIPHER_PARAM_AEAD_TAGLEN */
338
0
                                if (ossl_unlikely(r->taglen != NULL)) {
339
0
                                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
340
0
                                                   "param %s is repeated", s);
341
0
                                    return 0;
342
0
                                }
343
0
                                r->taglen = (OSSL_PARAM *)p;
344
0
                            }
345
0
                            break;
346
0
                        case '\0':
347
0
                            if (ossl_unlikely(r->tag != NULL)) {
348
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
349
0
                                               "param %s is repeated", s);
350
0
                                return 0;
351
0
                            }
352
0
                            r->tag = (OSSL_PARAM *)p;
353
0
                        }
354
0
                    }
355
0
                    break;
356
0
                case 'l':
357
0
                    if (ossl_likely(strcmp("saadpad", s + 2) == 0)) {
358
                        /* CIPHER_PARAM_AEAD_TLS1_AAD_PAD */
359
0
                        if (ossl_unlikely(r->pad != NULL)) {
360
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
361
0
                                           "param %s is repeated", s);
362
0
                            return 0;
363
0
                        }
364
0
                        r->pad = (OSSL_PARAM *)p;
365
0
                    }
366
0
                }
367
0
                break;
368
0
            case 'u':
369
0
                if (ossl_likely(strcmp("pdated-iv", s + 1) == 0)) {
370
                    /* CIPHER_PARAM_UPDATED_IV */
371
0
                    if (ossl_unlikely(r->updiv != NULL)) {
372
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
373
0
                                       "param %s is repeated", s);
374
0
                        return 0;
375
0
                    }
376
0
                    r->updiv = (OSSL_PARAM *)p;
377
0
                }
378
0
            }
379
0
    return 1;
380
0
}
381
#endif
382
/* End of machine generated */
383
384
const OSSL_PARAM *ossl_ccm_gettable_ctx_params(
385
        ossl_unused void *cctx, ossl_unused void *provctx
386
    )
387
112
{
388
112
    return ossl_cipher_ccm_get_ctx_params_list;
389
112
}
390
391
int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
392
0
{
393
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
394
0
    struct ossl_cipher_ccm_get_ctx_params_st p;
395
396
0
    if (ctx == NULL || !ossl_cipher_ccm_get_ctx_params_decoder(params, &p))
397
0
        return 0;
398
399
0
    if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, ccm_get_ivlen(ctx))) {
400
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
401
0
        return 0;
402
0
    }
403
404
0
    if (p.taglen != NULL && !OSSL_PARAM_set_size_t(p.taglen, ctx->m)) {
405
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
406
0
        return 0;
407
0
    }
408
409
0
    if (p.iv != NULL) {
410
0
        if (ccm_get_ivlen(ctx) > p.iv->data_size) {
411
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
412
0
            return 0;
413
0
        }
414
0
        if (!OSSL_PARAM_set_octet_string_or_ptr(p.iv, ctx->iv, p.iv->data_size)) {
415
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
416
0
            return 0;
417
0
        }
418
0
    }
419
420
0
    if (p.updiv != NULL) {
421
0
        if (ccm_get_ivlen(ctx) > p.updiv->data_size) {
422
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
423
0
            return 0;
424
0
        }
425
0
        if (!OSSL_PARAM_set_octet_string_or_ptr(p.updiv, ctx->iv, p.updiv->data_size)) {
426
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
427
0
            return 0;
428
0
        }
429
0
    }
430
431
0
    if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) {
432
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
433
0
        return 0;
434
0
    }
435
436
0
    if (p.pad != NULL && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) {
437
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
438
0
        return 0;
439
0
    }
440
441
0
    if (p.tag != NULL) {
442
0
        if (!ctx->enc || !ctx->tag_set) {
443
0
            ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
444
0
            return 0;
445
0
        }
446
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
447
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
448
0
            return 0;
449
0
        }
450
0
        if (!ctx->hw->gettag(ctx, p.tag->data, p.tag->data_size))
451
0
            return 0;
452
0
        ctx->tag_set = 0;
453
0
        ctx->iv_set = 0;
454
0
        ctx->len_set = 0;
455
0
    }
456
457
0
    return 1;
458
0
}
459
460
static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
461
                    const unsigned char *iv, size_t ivlen,
462
                    const OSSL_PARAM params[], int enc)
463
0
{
464
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
465
466
0
    if (!ossl_prov_is_running())
467
0
        return 0;
468
469
0
    ctx->enc = enc;
470
471
0
    if (iv != NULL) {
472
0
        if (ivlen != ccm_get_ivlen(ctx)) {
473
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
474
0
            return 0;
475
0
        }
476
0
        memcpy(ctx->iv, iv, ivlen);
477
0
        ctx->iv_set = 1;
478
0
    }
479
0
    if (key != NULL) {
480
0
        if (keylen != ctx->keylen) {
481
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
482
0
            return 0;
483
0
        }
484
0
        if (!ctx->hw->setkey(ctx, key, keylen))
485
0
            return 0;
486
0
    }
487
0
    return ossl_ccm_set_ctx_params(ctx, params);
488
0
}
489
490
int ossl_ccm_einit(void *vctx, const unsigned char *key, size_t keylen,
491
                   const unsigned char *iv, size_t ivlen,
492
                   const OSSL_PARAM params[])
493
0
{
494
0
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 1);
495
0
}
496
497
int ossl_ccm_dinit(void *vctx, const unsigned char *key, size_t keylen,
498
                   const unsigned char *iv, size_t ivlen,
499
                   const OSSL_PARAM params[])
500
0
{
501
0
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 0);
502
0
}
503
504
int ossl_ccm_stream_update(void *vctx, unsigned char *out, size_t *outl,
505
                           size_t outsize, const unsigned char *in,
506
                           size_t inl)
507
0
{
508
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
509
510
0
    if (outsize < inl) {
511
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
512
0
        return 0;
513
0
    }
514
515
0
    if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
516
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
517
0
        return 0;
518
0
    }
519
0
    return 1;
520
0
}
521
522
int ossl_ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,
523
                          size_t outsize)
524
0
{
525
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
526
0
    int i;
527
528
0
    if (!ossl_prov_is_running())
529
0
        return 0;
530
531
0
    i = ccm_cipher_internal(ctx, out, outl, NULL, 0);
532
0
    if (i <= 0)
533
0
        return 0;
534
535
0
    *outl = 0;
536
0
    return 1;
537
0
}
538
539
int ossl_ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize,
540
                    const unsigned char *in, size_t inl)
541
0
{
542
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
543
544
0
    if (!ossl_prov_is_running())
545
0
        return 0;
546
547
0
    if (outsize < inl) {
548
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
549
0
        return 0;
550
0
    }
551
552
0
    if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
553
0
        return 0;
554
555
0
    *outl = inl;
556
0
    return 1;
557
0
}
558
559
/* Copy the buffered iv */
560
static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
561
0
{
562
0
    const PROV_CCM_HW *hw = ctx->hw;
563
564
0
    if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
565
0
        return 0;
566
0
    ctx->len_set = 1;
567
0
    return 1;
568
0
}
569
570
static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
571
                          unsigned char *out, size_t *padlen,
572
                          const unsigned char *in, size_t len)
573
0
{
574
0
    int rv = 0;
575
0
    size_t olen = 0;
576
577
0
    if (!ossl_prov_is_running())
578
0
        goto err;
579
580
    /* Encrypt/decrypt must be performed in place */
581
0
    if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
582
0
        goto err;
583
584
    /* If encrypting set explicit IV from sequence number (start of AAD) */
585
0
    if (ctx->enc)
586
0
        memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
587
    /* Get rest of IV from explicit IV */
588
0
    memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
589
    /* Correct length value */
590
0
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
591
0
    if (!ccm_set_iv(ctx, len))
592
0
        goto err;
593
594
    /* Use saved AAD */
595
0
    if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
596
0
        goto err;
597
598
    /* Fix buffer to point to payload */
599
0
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
600
0
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
601
0
    if (ctx->enc) {
602
0
        if (!ctx->hw->auth_encrypt(ctx, in, out, len,  out + len, ctx->m))
603
0
            goto err;
604
0
        olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
605
0
    } else {
606
0
        if (!ctx->hw->auth_decrypt(ctx, in, out, len,
607
0
                                   (unsigned char *)in + len, ctx->m))
608
0
            goto err;
609
0
        olen = len;
610
0
    }
611
0
    rv = 1;
612
0
err:
613
0
    *padlen = olen;
614
0
    return rv;
615
0
}
616
617
static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
618
                               size_t *padlen, const unsigned char *in,
619
                               size_t len)
620
0
{
621
0
    int rv = 0;
622
0
    size_t olen = 0;
623
0
    const PROV_CCM_HW *hw = ctx->hw;
624
625
    /* If no key set, return error */
626
0
    if (!ctx->key_set)
627
0
        return 0;
628
629
0
    if (ctx->tls_aad_len != UNINITIALISED_SIZET)
630
0
        return ccm_tls_cipher(ctx, out, padlen, in, len);
631
632
    /* EVP_*Final() doesn't return any data */
633
0
    if (in == NULL && out != NULL)
634
0
        goto finish;
635
636
0
    if (!ctx->iv_set)
637
0
        goto err;
638
639
0
    if (out == NULL) {
640
0
        if (in == NULL) {
641
0
            if (!ccm_set_iv(ctx, len))
642
0
                goto err;
643
0
        } else {
644
            /* If we have AAD, we need a message length */
645
0
            if (!ctx->len_set && len)
646
0
                goto err;
647
0
            if (!hw->setaad(ctx, in, len))
648
0
                goto err;
649
0
        }
650
0
    } else {
651
        /* If not set length yet do it */
652
0
        if (!ctx->len_set && !ccm_set_iv(ctx, len))
653
0
            goto err;
654
655
0
        if (ctx->enc) {
656
0
            if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
657
0
                goto err;
658
0
            ctx->tag_set = 1;
659
0
        } else {
660
            /* The tag must be set before actually decrypting data */
661
0
            if (!ctx->tag_set)
662
0
                goto err;
663
664
0
            if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
665
0
                goto err;
666
            /* Finished - reset flags so calling this method again will fail */
667
0
            ctx->iv_set = 0;
668
0
            ctx->tag_set = 0;
669
0
            ctx->len_set = 0;
670
0
        }
671
0
    }
672
0
    olen = len;
673
0
finish:
674
0
    rv = 1;
675
0
err:
676
0
    *padlen = olen;
677
0
    return rv;
678
0
}
679
680
void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
681
0
{
682
0
    ctx->keylen = keybits / 8;
683
0
    ctx->key_set = 0;
684
0
    ctx->iv_set = 0;
685
0
    ctx->tag_set = 0;
686
0
    ctx->len_set = 0;
687
0
    ctx->l = 8;
688
0
    ctx->m = 12;
689
0
    ctx->tls_aad_len = UNINITIALISED_SIZET;
690
0
    ctx->hw = hw;
691
0
}