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_ctx_get_params_fn cmac_ctx_get_params;
33 static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params;
34 static OSSL_OP_mac_ctx_set_params_fn cmac_ctx_set_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_OUTLEN, NULL),
144 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */
147 static const OSSL_PARAM *cmac_gettable_ctx_params(void)
149 return known_gettable_ctx_params;
152 static int cmac_ctx_get_params(void *vmacctx, OSSL_PARAM params[])
156 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL
157 || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
158 return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
163 static const OSSL_PARAM known_settable_ctx_params[] = {
164 /* "algorithm" and "cipher" are the same parameter */
165 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ALGORITHM, NULL, 0),
166 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
167 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
168 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
169 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
172 static const OSSL_PARAM *cmac_settable_ctx_params(void)
174 return known_settable_ctx_params;
178 * ALL parameters should be set before init().
180 static int cmac_ctx_set_params(void *vmacctx, const OSSL_PARAM params[])
182 struct cmac_data_st *macctx = vmacctx;
185 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL
186 || ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ALGORITHM))
188 if (p->data_type != OSSL_PARAM_UTF8_STRING)
192 const char *algoname = p->data;
193 const char *propquery = NULL;
195 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */
196 ENGINE_finish(macctx->tmpengine);
197 macctx->tmpengine = NULL;
199 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
201 if (p->data_type != OSSL_PARAM_UTF8_STRING)
204 macctx->tmpengine = ENGINE_by_id(p->data);
205 if (macctx->tmpengine == NULL)
209 if ((p = OSSL_PARAM_locate_const(params,
210 OSSL_MAC_PARAM_PROPERTIES))
212 if (p->data_type != OSSL_PARAM_UTF8_STRING)
218 EVP_CIPHER_meth_free(macctx->alloc_cipher);
220 macctx->tmpcipher = macctx->alloc_cipher =
221 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
222 algoname, propquery);
224 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
225 /* TODO(3.0) BEGIN legacy stuff, to be removed */
226 if (macctx->tmpcipher == NULL)
227 macctx->tmpcipher = EVP_get_cipherbyname(algoname);
228 /* TODO(3.0) END of legacy stuff */
231 if (macctx->tmpcipher == NULL)
235 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
236 if (p->data_type != OSSL_PARAM_OCTET_STRING)
239 if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
240 macctx->tmpcipher, macctx->tmpengine))
243 macctx->tmpcipher = NULL;
244 macctx->tmpengine = NULL;
249 const OSSL_DISPATCH cmac_functions[] = {
250 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
251 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
252 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
253 { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
254 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
255 { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
256 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
257 (void (*)(void))cmac_gettable_ctx_params },
258 { OSSL_FUNC_MAC_CTX_GET_PARAMS, (void (*)(void))cmac_ctx_get_params },
259 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
260 (void (*)(void))cmac_settable_ctx_params },
261 { OSSL_FUNC_MAC_CTX_SET_PARAMS, (void (*)(void))cmac_ctx_set_params },