Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/macs/cmac_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2018-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
/* clang-format off */
10
11
/* clang-format on */
12
13
/*
14
 * CMAC low level APIs are deprecated for public use, but still ok for internal
15
 * use.
16
 */
17
#include "internal/deprecated.h"
18
19
#include <openssl/core_dispatch.h>
20
#include <openssl/core_names.h>
21
#include <openssl/params.h>
22
#include <openssl/evp.h>
23
#include <openssl/cmac.h>
24
#include <openssl/err.h>
25
#include <openssl/proverr.h>
26
27
#include "internal/cryptlib.h"
28
#include "prov/securitycheck.h"
29
#include "prov/implementations.h"
30
#include "prov/provider_ctx.h"
31
#include "prov/provider_util.h"
32
#include "prov/providercommon.h"
33
#include "crypto/cmac.h"
34
35
/*
36
 * Forward declaration of everything implemented here.  This is not strictly
37
 * necessary for the compiler, but provides an assurance that the signatures
38
 * of the functions in the dispatch table are correct.
39
 */
40
static OSSL_FUNC_mac_newctx_fn cmac_new;
41
static OSSL_FUNC_mac_dupctx_fn cmac_dup;
42
static OSSL_FUNC_mac_freectx_fn cmac_free;
43
static OSSL_FUNC_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
44
static OSSL_FUNC_mac_get_ctx_params_fn cmac_get_ctx_params;
45
static OSSL_FUNC_mac_settable_ctx_params_fn cmac_settable_ctx_params;
46
static OSSL_FUNC_mac_set_ctx_params_fn cmac_set_ctx_params;
47
static OSSL_FUNC_mac_init_fn cmac_init;
48
static OSSL_FUNC_mac_update_fn cmac_update;
49
static OSSL_FUNC_mac_final_fn cmac_final;
50
51
/* local CMAC data */
52
53
struct cmac_data_st {
54
    void *provctx;
55
    CMAC_CTX *ctx;
56
    PROV_CIPHER cipher;
57
    OSSL_FIPS_IND_DECLARE
58
};
59
60
static void *cmac_new(void *provctx)
61
833
{
62
833
    struct cmac_data_st *macctx;
63
64
833
    if (!ossl_prov_is_running())
65
0
        return NULL;
66
67
833
    if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
68
833
        || (macctx->ctx = CMAC_CTX_new()) == NULL) {
69
0
        OPENSSL_free(macctx);
70
0
        macctx = NULL;
71
833
    } else {
72
833
        macctx->provctx = provctx;
73
833
        OSSL_FIPS_IND_INIT(macctx)
74
833
    }
75
76
833
    return macctx;
77
833
}
78
79
static void cmac_free(void *vmacctx)
80
833
{
81
833
    struct cmac_data_st *macctx = vmacctx;
82
83
833
    if (macctx != NULL) {
84
833
        CMAC_CTX_free(macctx->ctx);
85
833
        ossl_prov_cipher_reset(&macctx->cipher);
86
833
        OPENSSL_free(macctx);
87
833
    }
88
833
}
89
90
static void *cmac_dup(void *vsrc)
91
376
{
92
376
    struct cmac_data_st *src = vsrc;
93
376
    struct cmac_data_st *dst;
94
95
376
    if (!ossl_prov_is_running())
96
0
        return NULL;
97
98
376
    dst = cmac_new(src->provctx);
99
376
    if (dst == NULL)
100
0
        return NULL;
101
376
    if (!CMAC_CTX_copy(dst->ctx, src->ctx)
102
376
        || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
103
0
        cmac_free(dst);
104
0
        return NULL;
105
0
    }
106
376
    OSSL_FIPS_IND_COPY(dst, src)
107
376
    return dst;
108
376
}
109
110
static size_t cmac_size(void *vmacctx)
111
547
{
112
547
    struct cmac_data_st *macctx = vmacctx;
113
547
    const EVP_CIPHER_CTX *cipherctx = CMAC_CTX_get0_cipher_ctx(macctx->ctx);
114
115
547
    if (EVP_CIPHER_CTX_get0_cipher(cipherctx) == NULL)
116
10
        return 0;
117
118
537
    return EVP_CIPHER_CTX_get_block_size(cipherctx);
119
547
}
120
121
#ifdef FIPS_MODULE
122
/*
123
 * TDES Encryption is not approved in FIPS 140-3.
124
 *
125
 * In strict approved mode we just fail here (by returning 0).
126
 * If we are going to bypass it using a FIPS indicator then we need to pass that
127
 * information down to the cipher also.
128
 * This function returns the param to pass down in 'p'.
129
 * state will return OSSL_FIPS_IND_STATE_UNKNOWN if the param has not been set.
130
 *
131
 * The name 'OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK' used below matches the
132
 * key name used by the Triple-DES.
133
 */
134
static int tdes_check_param(struct cmac_data_st *macctx, OSSL_PARAM *p,
135
    int *state)
136
{
137
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(macctx->provctx);
138
    const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&macctx->cipher);
139
140
    *state = OSSL_FIPS_IND_STATE_UNKNOWN;
141
    if (EVP_CIPHER_is_a(cipher, "DES-EDE3-CBC")) {
142
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(macctx, OSSL_FIPS_IND_SETTABLE0,
143
                libctx, "CMAC", "Triple-DES",
144
                ossl_fips_config_tdes_encrypt_disallowed))
145
            return 0;
146
        OSSL_FIPS_IND_GET_PARAM(macctx, p, state, OSSL_FIPS_IND_SETTABLE0,
147
            OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK)
148
    }
149
    return 1;
150
}
151
#endif
152
153
static int cmac_setkey(struct cmac_data_st *macctx,
154
    const unsigned char *key, size_t keylen)
155
483
{
156
483
    int rv;
157
483
    OSSL_PARAM *p = NULL;
158
#ifdef FIPS_MODULE
159
    int state = OSSL_FIPS_IND_STATE_UNKNOWN;
160
    OSSL_PARAM prms[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
161
162
    if (!tdes_check_param(macctx, &prms[0], &state))
163
        return 0;
164
    if (state != OSSL_FIPS_IND_STATE_UNKNOWN)
165
        p = prms;
166
#endif
167
483
    rv = ossl_cmac_init(macctx->ctx, key, keylen,
168
483
        ossl_prov_cipher_cipher(&macctx->cipher),
169
483
        ossl_prov_cipher_engine(&macctx->cipher), p);
170
483
    ossl_prov_cipher_reset(&macctx->cipher);
171
483
    return rv;
172
483
}
173
174
static int cmac_init(void *vmacctx, const unsigned char *key,
175
    size_t keylen, const OSSL_PARAM params[])
176
538
{
177
538
    struct cmac_data_st *macctx = vmacctx;
178
179
538
    if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params))
180
227
        return 0;
181
311
    if (key != NULL)
182
311
        return cmac_setkey(macctx, key, keylen);
183
    /* Reinitialize the CMAC context */
184
0
    return CMAC_Init(macctx->ctx, NULL, 0, NULL, NULL);
185
311
}
186
187
static int cmac_update(void *vmacctx, const unsigned char *data,
188
    size_t datalen)
189
1.56k
{
190
1.56k
    struct cmac_data_st *macctx = vmacctx;
191
192
1.56k
    return CMAC_Update(macctx->ctx, data, datalen);
193
1.56k
}
194
195
static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
196
    size_t outsize)
197
376
{
198
376
    struct cmac_data_st *macctx = vmacctx;
199
200
376
    if (!ossl_prov_is_running())
201
0
        return 0;
202
203
376
    return CMAC_Final(macctx->ctx, out, outl);
204
376
}
205
206
/* clang-format off */
207
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
208
#ifndef cmac_get_ctx_params_list
209
static const OSSL_PARAM cmac_get_ctx_params_list[] = {
210
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
211
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
212
# if defined(FIPS_MODULE)
213
    OSSL_PARAM_int(OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR, NULL),
214
# endif
215
    OSSL_PARAM_END
216
};
217
#endif
218
219
#ifndef cmac_get_ctx_params_st
220
struct cmac_get_ctx_params_st {
221
    OSSL_PARAM *bsize;
222
# if defined(FIPS_MODULE)
223
    OSSL_PARAM *ind;
224
# endif
225
    OSSL_PARAM *size;
226
};
227
#endif
228
229
#ifndef cmac_get_ctx_params_decoder
230
static int cmac_get_ctx_params_decoder
231
    (const OSSL_PARAM *p, struct cmac_get_ctx_params_st *r)
232
356
{
233
356
    const char *s;
234
235
356
    memset(r, 0, sizeof(*r));
236
356
    if (p != NULL)
237
712
        for (; (s = p->key) != NULL; p++)
238
356
            switch(s[0]) {
239
0
            default:
240
0
                break;
241
0
            case 'b':
242
0
                if (ossl_likely(strcmp("lock-size", s + 1) == 0)) {
243
                    /* OSSL_MAC_PARAM_BLOCK_SIZE */
244
0
                    if (ossl_unlikely(r->bsize != NULL)) {
245
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
246
0
                                       "param %s is repeated", s);
247
0
                        return 0;
248
0
                    }
249
0
                    r->bsize = (OSSL_PARAM *)p;
250
0
                }
251
0
                break;
252
0
            case 'f':
253
# if defined(FIPS_MODULE)
254
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
255
                    /* OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR */
256
                    if (ossl_unlikely(r->ind != NULL)) {
257
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
258
                                       "param %s is repeated", s);
259
                        return 0;
260
                    }
261
                    r->ind = (OSSL_PARAM *)p;
262
                }
263
# endif
264
0
                break;
265
356
            case 's':
266
356
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
267
                    /* OSSL_MAC_PARAM_SIZE */
268
356
                    if (ossl_unlikely(r->size != NULL)) {
269
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
270
0
                                       "param %s is repeated", s);
271
0
                        return 0;
272
0
                    }
273
356
                    r->size = (OSSL_PARAM *)p;
274
356
                }
275
356
            }
276
356
    return 1;
277
356
}
278
#endif
279
/* End of machine generated */
280
/* clang-format on */
281
282
static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx,
283
    ossl_unused void *provctx)
284
0
{
285
0
    return cmac_get_ctx_params_list;
286
0
}
287
288
static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
289
356
{
290
356
    struct cmac_data_st *macctx = vmacctx;
291
356
    struct cmac_get_ctx_params_st p;
292
293
356
    if (macctx == NULL || !cmac_get_ctx_params_decoder(params, &p))
294
0
        return 0;
295
296
356
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, cmac_size(vmacctx)))
297
0
        return 0;
298
299
356
    if (p.bsize != NULL && !OSSL_PARAM_set_size_t(p.bsize, cmac_size(vmacctx)))
300
0
        return 0;
301
302
356
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(macctx, p.ind))
303
0
        return 0;
304
305
356
    return 1;
306
356
}
307
308
/* clang-format off */
309
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
310
#ifndef cmac_set_ctx_params_list
311
static const OSSL_PARAM cmac_set_ctx_params_list[] = {
312
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
313
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
314
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
315
# if defined(FIPS_MODULE)
316
    OSSL_PARAM_int(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK, NULL),
317
# endif
318
    OSSL_PARAM_END
319
};
320
#endif
321
322
#ifndef cmac_set_ctx_params_st
323
struct cmac_set_ctx_params_st {
324
    OSSL_PARAM *cipher;
325
    OSSL_PARAM *engine;
326
# if defined(FIPS_MODULE)
327
    OSSL_PARAM *ind_ec;
328
# endif
329
    OSSL_PARAM *key;
330
    OSSL_PARAM *propq;
331
};
332
#endif
333
334
#ifndef cmac_set_ctx_params_decoder
335
static int cmac_set_ctx_params_decoder
336
    (const OSSL_PARAM *p, struct cmac_set_ctx_params_st *r)
337
367
{
338
367
    const char *s;
339
340
367
    memset(r, 0, sizeof(*r));
341
367
    if (p != NULL)
342
713
        for (; (s = p->key) != NULL; p++)
343
482
            switch(s[0]) {
344
76
            default:
345
76
                break;
346
158
            case 'c':
347
158
                if (ossl_likely(strcmp("ipher", s + 1) == 0)) {
348
                    /* OSSL_MAC_PARAM_CIPHER */
349
158
                    if (ossl_unlikely(r->cipher != NULL)) {
350
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
351
0
                                       "param %s is repeated", s);
352
0
                        return 0;
353
0
                    }
354
158
                    r->cipher = (OSSL_PARAM *)p;
355
158
                }
356
158
                break;
357
158
            case 'e':
358
0
                switch(s[1]) {
359
0
                default:
360
0
                    break;
361
0
                case 'n':
362
0
                    switch(s[2]) {
363
0
                    default:
364
0
                        break;
365
0
                    case 'c':
366
# if defined(FIPS_MODULE)
367
                        if (ossl_likely(strcmp("rypt-check", s + 3) == 0)) {
368
                            /* OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK */
369
                            if (ossl_unlikely(r->ind_ec != NULL)) {
370
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
371
                                               "param %s is repeated", s);
372
                                return 0;
373
                            }
374
                            r->ind_ec = (OSSL_PARAM *)p;
375
                        }
376
# endif
377
0
                        break;
378
0
                    case 'g':
379
0
                        if (ossl_likely(strcmp("ine", s + 3) == 0)) {
380
                            /* OSSL_ALG_PARAM_ENGINE */
381
0
                            if (ossl_unlikely(r->engine != NULL)) {
382
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
383
0
                                               "param %s is repeated", s);
384
0
                                return 0;
385
0
                            }
386
0
                            r->engine = (OSSL_PARAM *)p;
387
0
                        }
388
0
                    }
389
0
                }
390
0
                break;
391
86
            case 'k':
392
86
                if (ossl_likely(strcmp("ey", s + 1) == 0)) {
393
                    /* OSSL_MAC_PARAM_KEY */
394
86
                    if (ossl_unlikely(r->key != NULL)) {
395
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
396
0
                                       "param %s is repeated", s);
397
0
                        return 0;
398
0
                    }
399
86
                    r->key = (OSSL_PARAM *)p;
400
86
                }
401
86
                break;
402
162
            case 'p':
403
162
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
404
                    /* OSSL_MAC_PARAM_PROPERTIES */
405
162
                    if (ossl_unlikely(r->propq != NULL)) {
406
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
407
0
                                       "param %s is repeated", s);
408
0
                        return 0;
409
0
                    }
410
162
                    r->propq = (OSSL_PARAM *)p;
411
162
                }
412
482
            }
413
367
    return 1;
414
367
}
415
#endif
416
/* End of machine generated */
417
/* clang-format on */
418
419
static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx,
420
    ossl_unused void *provctx)
421
276
{
422
276
    return cmac_set_ctx_params_list;
423
276
}
424
425
/*
426
 * ALL parameters should be set before init().
427
 */
428
static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
429
563
{
430
563
    struct cmac_data_st *macctx = vmacctx;
431
563
    OSSL_LIB_CTX *ctx;
432
563
    struct cmac_set_ctx_params_st p;
433
434
563
    if (macctx == NULL || !cmac_set_ctx_params_decoder(params, &p))
435
0
        return 0;
436
437
563
    ctx = PROV_LIBCTX_OF(macctx->provctx);
438
439
563
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(macctx, OSSL_FIPS_IND_SETTABLE0, p.ind_ec))
440
0
        return 0;
441
442
563
    if (p.cipher != NULL) {
443
253
        if (!ossl_prov_cipher_load(&macctx->cipher, p.cipher, p.propq,
444
253
                p.engine, ctx))
445
55
            return 0;
446
447
198
        if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))
448
198
            != EVP_CIPH_CBC_MODE) {
449
2
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
450
2
            return 0;
451
2
        }
452
#ifdef FIPS_MODULE
453
        {
454
            const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&macctx->cipher);
455
456
            if (!EVP_CIPHER_is_a(cipher, "AES-256-CBC")
457
                && !EVP_CIPHER_is_a(cipher, "AES-192-CBC")
458
                && !EVP_CIPHER_is_a(cipher, "AES-128-CBC")
459
                && !EVP_CIPHER_is_a(cipher, "DES-EDE3-CBC")) {
460
                ERR_raise(ERR_LIB_PROV, EVP_R_UNSUPPORTED_CIPHER);
461
                return 0;
462
            }
463
        }
464
#endif
465
198
    }
466
467
506
    if (p.key != NULL) {
468
86
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING)
469
0
            return 0;
470
86
        return cmac_setkey(macctx, p.key->data, p.key->data_size);
471
86
    }
472
420
    return 1;
473
506
}
474
475
const OSSL_DISPATCH ossl_cmac_functions[] = {
476
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
477
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
478
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
479
    { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
480
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
481
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
482
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
483
        (void (*)(void))cmac_gettable_ctx_params },
484
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
485
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
486
        (void (*)(void))cmac_settable_ctx_params },
487
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
488
    OSSL_DISPATCH_END
489
};