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