Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/providers/implementations/macs/cmac_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2018-2023 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
 * CMAC low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/core_dispatch.h>
17
#include <openssl/core_names.h>
18
#include <openssl/params.h>
19
#include <openssl/evp.h>
20
#include <openssl/cmac.h>
21
#include <openssl/err.h>
22
#include <openssl/proverr.h>
23
24
#include "prov/implementations.h"
25
#include "prov/provider_ctx.h"
26
#include "prov/provider_util.h"
27
#include "prov/providercommon.h"
28
29
/*
30
 * Forward declaration of everything implemented here.  This is not strictly
31
 * necessary for the compiler, but provides an assurance that the signatures
32
 * of the functions in the dispatch table are correct.
33
 */
34
static OSSL_FUNC_mac_newctx_fn cmac_new;
35
static OSSL_FUNC_mac_dupctx_fn cmac_dup;
36
static OSSL_FUNC_mac_freectx_fn cmac_free;
37
static OSSL_FUNC_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
38
static OSSL_FUNC_mac_get_ctx_params_fn cmac_get_ctx_params;
39
static OSSL_FUNC_mac_settable_ctx_params_fn cmac_settable_ctx_params;
40
static OSSL_FUNC_mac_set_ctx_params_fn cmac_set_ctx_params;
41
static OSSL_FUNC_mac_init_fn cmac_init;
42
static OSSL_FUNC_mac_update_fn cmac_update;
43
static OSSL_FUNC_mac_final_fn cmac_final;
44
45
/* local CMAC data */
46
47
struct cmac_data_st {
48
    void *provctx;
49
    CMAC_CTX *ctx;
50
    PROV_CIPHER cipher;
51
};
52
53
static void *cmac_new(void *provctx)
54
833
{
55
833
    struct cmac_data_st *macctx;
56
57
833
    if (!ossl_prov_is_running())
58
0
        return NULL;
59
60
833
    if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
61
833
        || (macctx->ctx = CMAC_CTX_new()) == NULL) {
62
0
        OPENSSL_free(macctx);
63
0
        macctx = NULL;
64
833
    } else {
65
833
        macctx->provctx = provctx;
66
833
    }
67
68
833
    return macctx;
69
833
}
70
71
static void cmac_free(void *vmacctx)
72
833
{
73
833
    struct cmac_data_st *macctx = vmacctx;
74
75
833
    if (macctx != NULL) {
76
833
        CMAC_CTX_free(macctx->ctx);
77
833
        ossl_prov_cipher_reset(&macctx->cipher);
78
833
        OPENSSL_free(macctx);
79
833
    }
80
833
}
81
82
static void *cmac_dup(void *vsrc)
83
376
{
84
376
    struct cmac_data_st *src = vsrc;
85
376
    struct cmac_data_st *dst;
86
87
376
    if (!ossl_prov_is_running())
88
0
        return NULL;
89
90
376
    dst = cmac_new(src->provctx);
91
376
    if (dst == NULL)
92
0
        return NULL;
93
376
    if (!CMAC_CTX_copy(dst->ctx, src->ctx)
94
376
        || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
95
0
        cmac_free(dst);
96
0
        return NULL;
97
0
    }
98
376
    return dst;
99
376
}
100
101
static size_t cmac_size(void *vmacctx)
102
547
{
103
547
    struct cmac_data_st *macctx = vmacctx;
104
547
    const EVP_CIPHER_CTX *cipherctx = CMAC_CTX_get0_cipher_ctx(macctx->ctx);
105
106
547
    if (EVP_CIPHER_CTX_get0_cipher(cipherctx) == NULL)
107
10
        return 0;
108
109
537
    return EVP_CIPHER_CTX_get_block_size(cipherctx);
110
547
}
111
112
static int cmac_setkey(struct cmac_data_st *macctx,
113
    const unsigned char *key, size_t keylen)
114
483
{
115
483
    int rv = CMAC_Init(macctx->ctx, key, keylen,
116
483
        ossl_prov_cipher_cipher(&macctx->cipher),
117
483
        ossl_prov_cipher_engine(&macctx->cipher));
118
483
    ossl_prov_cipher_reset(&macctx->cipher);
119
483
    return rv;
120
483
}
121
122
static int cmac_init(void *vmacctx, const unsigned char *key,
123
    size_t keylen, const OSSL_PARAM params[])
124
538
{
125
538
    struct cmac_data_st *macctx = vmacctx;
126
127
538
    if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params))
128
227
        return 0;
129
311
    if (key != NULL)
130
311
        return cmac_setkey(macctx, key, keylen);
131
    /* Reinitialize the CMAC context */
132
0
    return CMAC_Init(macctx->ctx, NULL, 0, NULL, NULL);
133
311
}
134
135
static int cmac_update(void *vmacctx, const unsigned char *data,
136
    size_t datalen)
137
1.56k
{
138
1.56k
    struct cmac_data_st *macctx = vmacctx;
139
140
1.56k
    return CMAC_Update(macctx->ctx, data, datalen);
141
1.56k
}
142
143
static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
144
    size_t outsize)
145
376
{
146
376
    struct cmac_data_st *macctx = vmacctx;
147
148
376
    if (!ossl_prov_is_running())
149
0
        return 0;
150
151
376
    return CMAC_Final(macctx->ctx, out, outl);
152
376
}
153
154
static const OSSL_PARAM known_gettable_ctx_params[] = {
155
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
156
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
157
    OSSL_PARAM_END
158
};
159
static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx,
160
    ossl_unused void *provctx)
161
0
{
162
0
    return known_gettable_ctx_params;
163
0
}
164
165
static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
166
{
167
    OSSL_PARAM *p;
168
169
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
170
        && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
171
        return 0;
172
173
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
174
        && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
175
        return 0;
176
177
    return 1;
178
}
179
180
static const OSSL_PARAM known_settable_ctx_params[] = {
181
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
182
    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
183
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
184
    OSSL_PARAM_END
185
};
186
static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx,
187
    ossl_unused void *provctx)
188
276
{
189
276
    return known_settable_ctx_params;
190
276
}
191
192
/*
193
 * ALL parameters should be set before init().
194
 */
195
static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
196
{
197
    struct cmac_data_st *macctx = vmacctx;
198
    OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
199
    const OSSL_PARAM *p;
200
201
    if (params == NULL)
202
        return 1;
203
204
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
205
        if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
206
            return 0;
207
208
        if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))
209
            != EVP_CIPH_CBC_MODE) {
210
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
211
            return 0;
212
        }
213
    }
214
215
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
216
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
217
            return 0;
218
        return cmac_setkey(macctx, p->data, p->data_size);
219
    }
220
    return 1;
221
}
222
223
const OSSL_DISPATCH ossl_cmac_functions[] = {
224
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
225
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
226
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
227
    { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
228
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
229
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
230
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
231
        (void (*)(void))cmac_gettable_ctx_params },
232
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
233
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
234
        (void (*)(void))cmac_settable_ctx_params },
235
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
236
    OSSL_DISPATCH_END
237
};