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/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/cmac.h>
17 #include "internal/provider_algs.h"
18 #include "internal/provider_ctx.h"
21 * Forward declaration of everything implemented here. This is not strictly
22 * necessary for the compiler, but provides an assurance that the signatures
23 * of the functions in the dispatch table are correct.
25 static OSSL_OP_mac_newctx_fn cmac_new;
26 static OSSL_OP_mac_dupctx_fn cmac_dup;
27 static OSSL_OP_mac_freectx_fn cmac_free;
28 static OSSL_OP_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
29 static OSSL_OP_mac_get_ctx_params_fn cmac_get_ctx_params;
30 static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params;
31 static OSSL_OP_mac_set_ctx_params_fn cmac_set_ctx_params;
32 static OSSL_OP_mac_init_fn cmac_init;
33 static OSSL_OP_mac_update_fn cmac_update;
34 static OSSL_OP_mac_final_fn cmac_final;
43 * References to the underlying cipher implementation. tmpcipher
44 * caches the cipher, always. alloc_cipher only holds a reference
45 * to an explicitly fetched cipher.
46 * tmpcipher is cleared after a CMAC_Init call.
48 const EVP_CIPHER *tmpcipher; /* cached CMAC cipher */
49 EVP_CIPHER *alloc_cipher; /* fetched CMAC cipher */
52 * Conditions for legacy EVP_CIPHER uses.
53 * tmpengine is cleared after a CMAC_Init call.
55 ENGINE *tmpengine; /* CMAC cipher engine (legacy) */
58 static void *cmac_new(void *provctx)
60 struct cmac_data_st *macctx;
62 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
63 || (macctx->ctx = CMAC_CTX_new()) == NULL) {
67 macctx->provctx = provctx;
73 static void cmac_free(void *vmacctx)
75 struct cmac_data_st *macctx = vmacctx;
78 CMAC_CTX_free(macctx->ctx);
79 EVP_CIPHER_free(macctx->alloc_cipher);
84 static void *cmac_dup(void *vsrc)
86 struct cmac_data_st *src = vsrc;
87 struct cmac_data_st *dst = cmac_new(src->provctx);
89 if (!CMAC_CTX_copy(dst->ctx, src->ctx)) {
94 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher)) {
99 dst->tmpengine = src->tmpengine;
100 dst->tmpcipher = src->tmpcipher;
101 dst->alloc_cipher = src->alloc_cipher;
105 static size_t cmac_size(void *vmacctx)
107 struct cmac_data_st *macctx = vmacctx;
109 return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
112 static int cmac_init(void *vmacctx)
114 struct cmac_data_st *macctx = vmacctx;
115 int rv = CMAC_Init(macctx->ctx, NULL, 0, macctx->tmpcipher,
116 (ENGINE *)macctx->tmpengine);
118 macctx->tmpcipher = NULL;
119 macctx->tmpengine = NULL;
124 static int cmac_update(void *vmacctx, const unsigned char *data,
127 struct cmac_data_st *macctx = vmacctx;
129 return CMAC_Update(macctx->ctx, data, datalen);
132 static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
135 struct cmac_data_st *macctx = vmacctx;
137 return CMAC_Final(macctx->ctx, out, outl);
140 static const OSSL_PARAM known_gettable_ctx_params[] = {
141 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
144 static const OSSL_PARAM *cmac_gettable_ctx_params(void)
146 return known_gettable_ctx_params;
149 static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
153 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
154 return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
159 static const OSSL_PARAM known_settable_ctx_params[] = {
160 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
161 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
162 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
163 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
166 static const OSSL_PARAM *cmac_settable_ctx_params(void)
168 return known_settable_ctx_params;
172 * ALL parameters should be set before init().
174 static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
176 struct cmac_data_st *macctx = vmacctx;
179 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
180 if (p->data_type != OSSL_PARAM_UTF8_STRING)
184 const char *algoname = p->data;
185 const char *propquery = NULL;
187 /* Inside the FIPS module, we don't support engines */
188 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
189 ENGINE_finish(macctx->tmpengine);
190 macctx->tmpengine = NULL;
192 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
194 if (p->data_type != OSSL_PARAM_UTF8_STRING)
197 macctx->tmpengine = ENGINE_by_id(p->data);
198 if (macctx->tmpengine == NULL)
202 if ((p = OSSL_PARAM_locate_const(params,
203 OSSL_MAC_PARAM_PROPERTIES))
205 if (p->data_type != OSSL_PARAM_UTF8_STRING)
211 EVP_CIPHER_free(macctx->alloc_cipher);
213 macctx->tmpcipher = macctx->alloc_cipher =
214 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
215 algoname, propquery);
217 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
218 /* TODO(3.0) BEGIN legacy stuff, to be removed */
219 if (macctx->tmpcipher == NULL)
220 macctx->tmpcipher = EVP_get_cipherbyname(algoname);
221 /* TODO(3.0) END of legacy stuff */
224 if (macctx->tmpcipher == NULL)
228 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
229 if (p->data_type != OSSL_PARAM_OCTET_STRING)
232 if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
233 macctx->tmpcipher, macctx->tmpengine))
236 macctx->tmpcipher = NULL;
237 macctx->tmpengine = NULL;
242 const OSSL_DISPATCH cmac_functions[] = {
243 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
244 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
245 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
246 { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
247 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
248 { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
249 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
250 (void (*)(void))cmac_gettable_ctx_params },
251 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
252 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
253 (void (*)(void))cmac_settable_ctx_params },
254 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },