Coverage Report

Created: 2026-10-28 06:28

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