2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
11 #ifndef OPENSSL_NO_CMAC
13 # include <openssl/core_numbers.h>
14 # include <openssl/core_names.h>
15 # include <openssl/params.h>
16 # include <openssl/engine.h>
17 # include <openssl/evp.h>
18 # include <openssl/cmac.h>
20 # include "internal/provider_algs.h"
21 # include "internal/provider_ctx.h"
24 * Forward declaration of everything implemented here. This is not strictly
25 * necessary for the compiler, but provides an assurance that the signatures
26 * of the functions in the dispatch table are correct.
28 static OSSL_OP_mac_newctx_fn cmac_new;
29 static OSSL_OP_mac_dupctx_fn cmac_dup;
30 static OSSL_OP_mac_freectx_fn cmac_free;
31 static OSSL_OP_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
32 static OSSL_OP_mac_get_ctx_params_fn cmac_get_ctx_params;
33 static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params;
34 static OSSL_OP_mac_set_ctx_params_fn cmac_set_ctx_params;
35 static OSSL_OP_mac_init_fn cmac_init;
36 static OSSL_OP_mac_update_fn cmac_update;
37 static OSSL_OP_mac_final_fn cmac_final;
46 * References to the underlying cipher implementation. tmpcipher
47 * caches the cipher, always. alloc_cipher only holds a reference
48 * to an explicitly fetched cipher.
49 * tmpcipher is cleared after a CMAC_Init call.
51 const EVP_CIPHER *tmpcipher; /* cached CMAC cipher */
52 EVP_CIPHER *alloc_cipher; /* fetched CMAC cipher */
55 * Conditions for legacy EVP_CIPHER uses.
56 * tmpengine is cleared after a CMAC_Init call.
58 ENGINE *tmpengine; /* CMAC cipher engine (legacy) */
61 static void *cmac_new(void *provctx)
63 struct cmac_data_st *macctx;
65 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
66 || (macctx->ctx = CMAC_CTX_new()) == NULL) {
70 macctx->provctx = provctx;
75 static void cmac_free(void *vmacctx)
77 struct cmac_data_st *macctx = vmacctx;
80 CMAC_CTX_free(macctx->ctx);
81 EVP_CIPHER_meth_free(macctx->alloc_cipher);
86 static void *cmac_dup(void *vsrc)
88 struct cmac_data_st *src = vsrc;
89 struct cmac_data_st *dst = cmac_new(src->provctx);
91 if (!CMAC_CTX_copy(dst->ctx, src->ctx)) {
96 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher)) {
101 dst->tmpengine = src->tmpengine;
102 dst->tmpcipher = src->tmpcipher;
103 dst->alloc_cipher = src->alloc_cipher;
107 static size_t cmac_size(void *vmacctx)
109 struct cmac_data_st *macctx = vmacctx;
111 return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
114 static int cmac_init(void *vmacctx)
116 struct cmac_data_st *macctx = vmacctx;
117 int rv = CMAC_Init(macctx->ctx, NULL, 0, macctx->tmpcipher,
118 (ENGINE *)macctx->tmpengine);
120 macctx->tmpcipher = NULL;
121 macctx->tmpengine = NULL;
126 static int cmac_update(void *vmacctx, const unsigned char *data,
129 struct cmac_data_st *macctx = vmacctx;
131 return CMAC_Update(macctx->ctx, data, datalen);
134 static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
137 struct cmac_data_st *macctx = vmacctx;
139 return CMAC_Final(macctx->ctx, out, outl);
142 static const OSSL_PARAM known_gettable_ctx_params[] = {
143 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
146 static const OSSL_PARAM *cmac_gettable_ctx_params(void)
148 return known_gettable_ctx_params;
151 static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
155 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
156 return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
161 static const OSSL_PARAM known_settable_ctx_params[] = {
162 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
163 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
164 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
165 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
168 static const OSSL_PARAM *cmac_settable_ctx_params(void)
170 return known_settable_ctx_params;
174 * ALL parameters should be set before init().
176 static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
178 struct cmac_data_st *macctx = vmacctx;
181 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
182 if (p->data_type != OSSL_PARAM_UTF8_STRING)
186 const char *algoname = p->data;
187 const char *propquery = NULL;
189 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */
190 ENGINE_finish(macctx->tmpengine);
191 macctx->tmpengine = NULL;
193 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
195 if (p->data_type != OSSL_PARAM_UTF8_STRING)
198 macctx->tmpengine = ENGINE_by_id(p->data);
199 if (macctx->tmpengine == NULL)
203 if ((p = OSSL_PARAM_locate_const(params,
204 OSSL_MAC_PARAM_PROPERTIES))
206 if (p->data_type != OSSL_PARAM_UTF8_STRING)
212 EVP_CIPHER_meth_free(macctx->alloc_cipher);
214 macctx->tmpcipher = macctx->alloc_cipher =
215 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
216 algoname, propquery);
218 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
219 /* TODO(3.0) BEGIN legacy stuff, to be removed */
220 if (macctx->tmpcipher == NULL)
221 macctx->tmpcipher = EVP_get_cipherbyname(algoname);
222 /* TODO(3.0) END of legacy stuff */
225 if (macctx->tmpcipher == NULL)
229 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
230 if (p->data_type != OSSL_PARAM_OCTET_STRING)
233 if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
234 macctx->tmpcipher, macctx->tmpengine))
237 macctx->tmpcipher = NULL;
238 macctx->tmpengine = NULL;
243 const OSSL_DISPATCH cmac_functions[] = {
244 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
245 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
246 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
247 { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
248 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
249 { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
250 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
251 (void (*)(void))cmac_gettable_ctx_params },
252 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
253 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
254 (void (*)(void))cmac_settable_ctx_params },
255 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },