Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
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
78.2k
{
23
78.2k
    size_t len;
24
25
78.2k
    if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
26
0
        return 0;
27
28
    /* Save the aad for later use. */
29
78.2k
    memcpy(ctx->buf, aad, alen);
30
78.2k
    ctx->tls_aad_len = alen;
31
32
78.2k
    len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
33
78.2k
    if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
34
78
        return 0;
35
36
    /* Correct length for explicit iv. */
37
78.2k
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
38
39
78.2k
    if (!ctx->enc) {
40
77.2k
        if (len < ctx->m)
41
29
            return 0;
42
        /* Correct length for tag. */
43
77.2k
        len -= ctx->m;
44
77.2k
    }
45
78.1k
    ctx->buf[alen - 2] = (unsigned char)(len >> 8);
46
78.1k
    ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
47
48
    /* Extra padding: tag appended to record. */
49
78.1k
    return ctx->m;
50
78.2k
}
51
52
static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed,
53
    size_t flen)
54
1.99k
{
55
1.99k
    if (flen != EVP_CCM_TLS_FIXED_IV_LEN)
56
0
        return 0;
57
58
    /* Copy to first part of the iv. */
59
1.99k
    memcpy(ctx->iv, fixed, flen);
60
1.99k
    return 1;
61
1.99k
}
62
63
static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx)
64
78.2k
{
65
78.2k
    return 15 - ctx->l;
66
78.2k
}
67
68
int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
69
16.7k
{
70
16.7k
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
71
16.7k
    const OSSL_PARAM *p;
72
16.7k
    size_t sz;
73
74
16.7k
    if (params == NULL)
75
1.22k
        return 1;
76
77
15.5k
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
78
15.5k
    if (p != NULL) {
79
611
        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
611
        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
611
        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
611
        ctx->m = p->data_size;
97
611
    }
98
99
15.5k
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
100
15.5k
    if (p != NULL) {
101
611
        size_t ivlen;
102
103
611
        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
611
        ivlen = 15 - sz;
108
611
        if (ivlen < 2 || ivlen > 8) {
109
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
110
0
            return 0;
111
0
        }
112
611
        if (ctx->l != ivlen) {
113
611
            ctx->l = ivlen;
114
611
            ctx->iv_set = 0;
115
611
        }
116
611
    }
117
118
15.5k
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
119
15.5k
    if (p != NULL) {
120
13.0k
        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
13.0k
        sz = ccm_tls_init(ctx, p->data, p->data_size);
125
13.0k
        if (sz == 0) {
126
23
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
127
23
            return 0;
128
23
        }
129
13.0k
        ctx->tls_aad_pad_sz = sz;
130
13.0k
    }
131
132
15.4k
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
133
15.4k
    if (p != NULL) {
134
611
        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
611
        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
611
    }
143
144
15.4k
    return 1;
145
15.4k
}
146
147
int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
148
39.4k
{
149
39.4k
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
150
39.4k
    OSSL_PARAM *p;
151
152
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
153
39.4k
    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
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
159
39.4k
    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
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
169
39.4k
    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
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
182
39.4k
    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
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
195
39.4k
    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
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
201
39.4k
    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
39.4k
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
207
39.4k
    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
39.4k
    return 1;
223
39.4k
}
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
4.03k
{
229
4.03k
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
230
231
4.03k
    if (!ossl_prov_is_running())
232
0
        return 0;
233
234
4.03k
    ctx->enc = enc;
235
236
4.03k
    if (iv != NULL) {
237
14
        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
14
        memcpy(ctx->iv, iv, ivlen);
242
14
        ctx->iv_set = 1;
243
14
    }
244
4.03k
    if (key != NULL) {
245
2.01k
        if (keylen != ctx->keylen) {
246
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
247
0
            return 0;
248
0
        }
249
2.01k
        if (!ctx->hw->setkey(ctx, key, keylen))
250
0
            return 0;
251
2.01k
    }
252
4.03k
    return ossl_ccm_set_ctx_params(ctx, params);
253
4.03k
}
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
1.59k
{
259
1.59k
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 1);
260
1.59k
}
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
2.44k
{
266
2.44k
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 0);
267
2.44k
}
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
78.1k
{
273
78.1k
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
274
275
78.1k
    if (outsize < inl) {
276
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
277
0
        return 0;
278
0
    }
279
280
78.1k
    if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
281
77.1k
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
282
77.1k
        return 0;
283
77.1k
    }
284
1.03k
    return 1;
285
78.1k
}
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
    unsigned char dummy_in = 0, dummy_out = 0;
292
293
0
    if (!ossl_prov_is_running())
294
0
        return 0;
295
296
    /*
297
     * Encryption sets tag_set after processing the payload, while successful
298
     * decryption clears iv_set. Use those transitions to avoid processing an
299
     * operation twice.
300
     */
301
0
    if (!ctx->key_set
302
0
        || (ctx->iv_set && (!ctx->enc || !ctx->tag_set)
303
0
            && ccm_cipher_internal(ctx, &dummy_out, outl, &dummy_in, 0) <= 0))
304
0
        return 0;
305
306
0
    *outl = 0;
307
0
    return 1;
308
0
}
309
310
int ossl_ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize,
311
    const unsigned char *in, size_t inl)
312
0
{
313
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
314
315
0
    if (!ossl_prov_is_running())
316
0
        return 0;
317
318
0
    if (in == NULL)
319
0
        return ossl_ccm_stream_final(vctx, out, outl, outsize);
320
321
0
    if (outsize < inl) {
322
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
323
0
        return 0;
324
0
    }
325
326
0
    if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
327
0
        return 0;
328
329
0
    *outl = inl;
330
0
    return 1;
331
0
}
332
333
/* Copy the buffered iv */
334
static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
335
78.1k
{
336
78.1k
    const PROV_CCM_HW *hw = ctx->hw;
337
338
78.1k
    if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
339
0
        return 0;
340
78.1k
    ctx->len_set = 1;
341
78.1k
    return 1;
342
78.1k
}
343
344
static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
345
    unsigned char *out, size_t *padlen,
346
    const unsigned char *in, size_t len)
347
78.1k
{
348
78.1k
    int rv = 0;
349
78.1k
    size_t olen = 0;
350
351
78.1k
    if (!ossl_prov_is_running())
352
0
        goto err;
353
354
    /* Encrypt/decrypt must be performed in place */
355
78.1k
    if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
356
0
        goto err;
357
358
    /* If encrypting set explicit IV from sequence number (start of AAD) */
359
78.1k
    if (ctx->enc)
360
958
        memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
361
    /* Get rest of IV from explicit IV */
362
78.1k
    memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
363
    /* Correct length value */
364
78.1k
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
365
78.1k
    if (!ccm_set_iv(ctx, len))
366
0
        goto err;
367
368
    /* Use saved AAD */
369
78.1k
    if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
370
0
        goto err;
371
372
    /* Fix buffer to point to payload */
373
78.1k
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
374
78.1k
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
375
78.1k
    if (ctx->enc) {
376
958
        if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m))
377
0
            goto err;
378
958
        olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
379
77.2k
    } else {
380
77.2k
        if (!ctx->hw->auth_decrypt(ctx, in, out, len,
381
77.2k
                (unsigned char *)in + len, ctx->m))
382
77.1k
            goto err;
383
78
        olen = len;
384
78
    }
385
1.03k
    rv = 1;
386
78.1k
err:
387
78.1k
    *padlen = olen;
388
78.1k
    return rv;
389
1.03k
}
390
391
static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
392
    size_t *padlen, const unsigned char *in,
393
    size_t len)
394
78.1k
{
395
78.1k
    int rv = 0;
396
78.1k
    size_t olen = 0;
397
78.1k
    const PROV_CCM_HW *hw = ctx->hw;
398
399
    /* If no key set, return error */
400
78.1k
    if (!ctx->key_set)
401
0
        return 0;
402
403
78.1k
    if (ctx->tls_aad_len != UNINITIALISED_SIZET)
404
78.1k
        return ccm_tls_cipher(ctx, out, padlen, in, len);
405
406
    /* EVP_*Final() doesn't return any data */
407
2
    if (in == NULL && out != NULL)
408
0
        goto finish;
409
410
2
    if (!ctx->iv_set)
411
2
        goto err;
412
413
0
    if (out == NULL) {
414
0
        if (in == NULL) {
415
0
            if (!ccm_set_iv(ctx, len))
416
0
                goto err;
417
0
        } else {
418
            /* If we have AAD, we need a message length */
419
0
            if (!ctx->len_set && len)
420
0
                goto err;
421
0
            if (!hw->setaad(ctx, in, len))
422
0
                goto err;
423
0
        }
424
0
    } else {
425
        /* If not set length yet do it */
426
0
        if (!ctx->len_set && !ccm_set_iv(ctx, len))
427
0
            goto err;
428
429
0
        if (ctx->enc) {
430
0
            if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
431
0
                goto err;
432
0
            ctx->tag_set = 1;
433
0
        } else {
434
            /* The tag must be set before actually decrypting data */
435
0
            if (!ctx->tag_set)
436
0
                goto err;
437
438
0
            if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
439
0
                goto err;
440
            /* Finished - reset flags so calling this method again will fail */
441
0
            ctx->iv_set = 0;
442
0
            ctx->tag_set = 0;
443
0
            ctx->len_set = 0;
444
0
        }
445
0
    }
446
0
    olen = len;
447
0
finish:
448
0
    rv = 1;
449
2
err:
450
2
    *padlen = olen;
451
2
    return rv;
452
0
}
453
454
void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
455
2.04k
{
456
2.04k
    ctx->keylen = keybits / 8;
457
2.04k
    ctx->key_set = 0;
458
2.04k
    ctx->iv_set = 0;
459
2.04k
    ctx->tag_set = 0;
460
2.04k
    ctx->len_set = 0;
461
2.04k
    ctx->l = 8;
462
2.04k
    ctx->m = 12;
463
2.04k
    ctx->tls_aad_len = UNINITIALISED_SIZET;
464
2.04k
    ctx->hw = hw;
465
2.04k
}