VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/providers/implementations/macs/cmac_prov.c@ 96317

Last change on this file since 96317 was 95219, checked in by vboxsync, 3 years ago

libs/openssl: Switched to v3.0.3, bugref:10128

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette