Coverage Report

Created: 2025-10-28 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/ciphercommon_ccm.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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
#include "providers/implementations/ciphers/ciphercommon_ccm.inc"
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
const OSSL_PARAM *ossl_ccm_settable_ctx_params(
71
        ossl_unused void *cctx, ossl_unused void *provctx
72
    )
73
0
{
74
0
    return ossl_cipher_ccm_set_ctx_params_list;
75
0
}
76
77
int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
78
0
{
79
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
80
0
    size_t sz, ivlen;
81
0
    struct ossl_cipher_ccm_set_ctx_params_st p;
82
83
0
    if (ctx == NULL || !ossl_cipher_ccm_set_ctx_params_decoder(params, &p))
84
0
        return 0;
85
86
0
    if (p.tag != NULL) {
87
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
88
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
89
0
            return 0;
90
0
        }
91
0
        if ((p.tag->data_size & 1) || (p.tag->data_size < 4) || p.tag->data_size > 16) {
92
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
93
0
            return 0;
94
0
        }
95
96
0
        if (p.tag->data != NULL) {
97
0
            if (ctx->enc) {
98
0
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
99
0
                return 0;
100
0
            }
101
0
            memcpy(ctx->buf, p.tag->data, p.tag->data_size);
102
0
            ctx->tag_set = 1;
103
0
        }
104
0
        ctx->m = p.tag->data_size;
105
0
    }
106
107
0
    if (p.ivlen != NULL) {
108
0
        if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) {
109
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
110
0
            return 0;
111
0
        }
112
0
        ivlen = 15 - sz;
113
0
        if (ivlen < 2 || ivlen > 8) {
114
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
115
0
            return 0;
116
0
        }
117
0
        if (ctx->l != ivlen) {
118
0
            ctx->l = ivlen;
119
0
            ctx->iv_set = 0;
120
0
        }
121
0
    }
122
123
0
    if (p.aad != NULL) {
124
0
        if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) {
125
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
126
0
            return 0;
127
0
        }
128
0
        sz = ccm_tls_init(ctx, p.aad->data, p.aad->data_size);
129
0
        if (sz == 0) {
130
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
131
0
            return 0;
132
0
        }
133
0
        ctx->tls_aad_pad_sz = sz;
134
0
    }
135
136
0
    if (p.fixed != NULL) {
137
0
        if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) {
138
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
139
0
            return 0;
140
0
        }
141
0
        if (ccm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) {
142
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
143
0
            return 0;
144
0
        }
145
0
    }
146
0
    return 1;
147
0
}
148
149
const OSSL_PARAM *ossl_ccm_gettable_ctx_params(
150
        ossl_unused void *cctx, ossl_unused void *provctx
151
    )
152
112
{
153
112
    return ossl_cipher_ccm_get_ctx_params_list;
154
112
}
155
156
int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
157
0
{
158
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
159
0
    struct ossl_cipher_ccm_get_ctx_params_st p;
160
161
0
    if (ctx == NULL || !ossl_cipher_ccm_get_ctx_params_decoder(params, &p))
162
0
        return 0;
163
164
0
    if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, ccm_get_ivlen(ctx))) {
165
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
166
0
        return 0;
167
0
    }
168
169
0
    if (p.taglen != NULL && !OSSL_PARAM_set_size_t(p.taglen, ctx->m)) {
170
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
171
0
        return 0;
172
0
    }
173
174
0
    if (p.iv != NULL) {
175
0
        if (ccm_get_ivlen(ctx) > p.iv->data_size) {
176
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
177
0
            return 0;
178
0
        }
179
0
        if (!OSSL_PARAM_set_octet_string_or_ptr(p.iv, ctx->iv, p.iv->data_size)) {
180
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
181
0
            return 0;
182
0
        }
183
0
    }
184
185
0
    if (p.updiv != NULL) {
186
0
        if (ccm_get_ivlen(ctx) > p.updiv->data_size) {
187
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
188
0
            return 0;
189
0
        }
190
0
        if (!OSSL_PARAM_set_octet_string_or_ptr(p.updiv, ctx->iv, p.updiv->data_size)) {
191
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
192
0
            return 0;
193
0
        }
194
0
    }
195
196
0
    if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) {
197
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
198
0
        return 0;
199
0
    }
200
201
0
    if (p.pad != NULL && !OSSL_PARAM_set_size_t(p.pad, 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
0
    if (p.tag != NULL) {
207
0
        if (!ctx->enc || !ctx->tag_set) {
208
0
            ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
209
0
            return 0;
210
0
        }
211
0
        if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) {
212
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
213
0
            return 0;
214
0
        }
215
0
        if (!ctx->hw->gettag(ctx, p.tag->data, p.tag->data_size))
216
0
            return 0;
217
0
        ctx->tag_set = 0;
218
0
        ctx->iv_set = 0;
219
0
        ctx->len_set = 0;
220
0
    }
221
222
0
    return 1;
223
0
}
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
0
{
229
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
230
231
0
    if (!ossl_prov_is_running())
232
0
        return 0;
233
234
0
    ctx->enc = enc;
235
236
0
    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
0
    if (key != NULL) {
245
0
        if (keylen != ctx->keylen) {
246
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
247
0
            return 0;
248
0
        }
249
0
        if (!ctx->hw->setkey(ctx, key, keylen))
250
0
            return 0;
251
0
    }
252
0
    return ossl_ccm_set_ctx_params(ctx, params);
253
0
}
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
0
{
259
0
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 1);
260
0
}
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
0
{
266
0
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 0);
267
0
}
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
0
{
273
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
274
275
0
    if (outsize < inl) {
276
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
277
0
        return 0;
278
0
    }
279
280
0
    if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
281
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
282
0
        return 0;
283
0
    }
284
0
    return 1;
285
0
}
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
0
{
327
0
    const PROV_CCM_HW *hw = ctx->hw;
328
329
0
    if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
330
0
        return 0;
331
0
    ctx->len_set = 1;
332
0
    return 1;
333
0
}
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
0
{
339
0
    int rv = 0;
340
0
    size_t olen = 0;
341
342
0
    if (!ossl_prov_is_running())
343
0
        goto err;
344
345
    /* Encrypt/decrypt must be performed in place */
346
0
    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
0
    if (ctx->enc)
351
0
        memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
352
    /* Get rest of IV from explicit IV */
353
0
    memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
354
    /* Correct length value */
355
0
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
356
0
    if (!ccm_set_iv(ctx, len))
357
0
        goto err;
358
359
    /* Use saved AAD */
360
0
    if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
361
0
        goto err;
362
363
    /* Fix buffer to point to payload */
364
0
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
365
0
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
366
0
    if (ctx->enc) {
367
0
        if (!ctx->hw->auth_encrypt(ctx, in, out, len,  out + len, ctx->m))
368
0
            goto err;
369
0
        olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
370
0
    } else {
371
0
        if (!ctx->hw->auth_decrypt(ctx, in, out, len,
372
0
                                   (unsigned char *)in + len, ctx->m))
373
0
            goto err;
374
0
        olen = len;
375
0
    }
376
0
    rv = 1;
377
0
err:
378
0
    *padlen = olen;
379
0
    return rv;
380
0
}
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
0
{
386
0
    int rv = 0;
387
0
    size_t olen = 0;
388
0
    const PROV_CCM_HW *hw = ctx->hw;
389
390
    /* If no key set, return error */
391
0
    if (!ctx->key_set)
392
0
        return 0;
393
394
0
    if (ctx->tls_aad_len != UNINITIALISED_SIZET)
395
0
        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
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
428
0
                goto err;
429
0
            }
430
431
0
            if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
432
0
                goto err;
433
            /* Finished - reset flags so calling this method again will fail */
434
0
            ctx->iv_set = 0;
435
0
            ctx->tag_set = 0;
436
0
            ctx->len_set = 0;
437
0
        }
438
0
    }
439
0
    olen = len;
440
0
finish:
441
0
    rv = 1;
442
0
err:
443
0
    *padlen = olen;
444
0
    return rv;
445
0
}
446
447
void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
448
0
{
449
0
    ctx->keylen = keybits / 8;
450
0
    ctx->key_set = 0;
451
0
    ctx->iv_set = 0;
452
0
    ctx->tag_set = 0;
453
0
    ctx->len_set = 0;
454
0
    ctx->l = 8;
455
0
    ctx->m = 12;
456
0
    ctx->tls_aad_len = UNINITIALISED_SIZET;
457
0
    ctx->hw = hw;
458
0
}