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"
19 #include "internal/provider_util.h"
22 * Forward declaration of everything implemented here. This is not strictly
23 * necessary for the compiler, but provides an assurance that the signatures
24 * of the functions in the dispatch table are correct.
26 static OSSL_OP_mac_newctx_fn cmac_new;
27 static OSSL_OP_mac_dupctx_fn cmac_dup;
28 static OSSL_OP_mac_freectx_fn cmac_free;
29 static OSSL_OP_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
30 static OSSL_OP_mac_get_ctx_params_fn cmac_get_ctx_params;
31 static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params;
32 static OSSL_OP_mac_set_ctx_params_fn cmac_set_ctx_params;
33 static OSSL_OP_mac_init_fn cmac_init;
34 static OSSL_OP_mac_update_fn cmac_update;
35 static OSSL_OP_mac_final_fn cmac_final;
45 static void *cmac_new(void *provctx)
47 struct cmac_data_st *macctx;
49 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
50 || (macctx->ctx = CMAC_CTX_new()) == NULL) {
54 macctx->provctx = provctx;
60 static void cmac_free(void *vmacctx)
62 struct cmac_data_st *macctx = vmacctx;
65 CMAC_CTX_free(macctx->ctx);
66 ossl_prov_cipher_reset(&macctx->cipher);
71 static void *cmac_dup(void *vsrc)
73 struct cmac_data_st *src = vsrc;
74 struct cmac_data_st *dst = cmac_new(src->provctx);
76 if (!CMAC_CTX_copy(dst->ctx, src->ctx)
77 || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
84 static size_t cmac_size(void *vmacctx)
86 struct cmac_data_st *macctx = vmacctx;
88 return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
91 static int cmac_init(void *vmacctx)
93 struct cmac_data_st *macctx = vmacctx;
94 int rv = CMAC_Init(macctx->ctx, NULL, 0,
95 ossl_prov_cipher_cipher(&macctx->cipher),
96 ossl_prov_cipher_engine(&macctx->cipher));
98 ossl_prov_cipher_reset(&macctx->cipher);
102 static int cmac_update(void *vmacctx, const unsigned char *data,
105 struct cmac_data_st *macctx = vmacctx;
107 return CMAC_Update(macctx->ctx, data, datalen);
110 static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
113 struct cmac_data_st *macctx = vmacctx;
115 return CMAC_Final(macctx->ctx, out, outl);
118 static const OSSL_PARAM known_gettable_ctx_params[] = {
119 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
122 static const OSSL_PARAM *cmac_gettable_ctx_params(void)
124 return known_gettable_ctx_params;
127 static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
131 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
132 return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
137 static const OSSL_PARAM known_settable_ctx_params[] = {
138 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
139 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
140 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
143 static const OSSL_PARAM *cmac_settable_ctx_params(void)
145 return known_settable_ctx_params;
149 * ALL parameters should be set before init().
151 static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
153 struct cmac_data_st *macctx = vmacctx;
154 OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
157 if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
160 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
161 if (p->data_type != OSSL_PARAM_OCTET_STRING)
164 if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
165 ossl_prov_cipher_cipher(&macctx->cipher),
166 ossl_prov_cipher_engine(&macctx->cipher)))
169 ossl_prov_cipher_reset(&macctx->cipher);
174 const OSSL_DISPATCH cmac_functions[] = {
175 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
176 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
177 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
178 { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
179 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
180 { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
181 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
182 (void (*)(void))cmac_gettable_ctx_params },
183 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
184 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
185 (void (*)(void))cmac_settable_ctx_params },
186 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },