2 * Copyright 2019-2020 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/crypto.h>
11 #include <openssl/evp.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/params.h>
15 #include <openssl/err.h>
16 #include "prov/implementations.h"
17 #include "prov/provider_ctx.h"
18 #include "prov/macsignature.h"
20 static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
21 static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
22 static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
23 static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
24 static OSSL_FUNC_signature_freectx_fn mac_freectx;
25 static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
34 static void *mac_newctx(void *provctx, const char *propq, const char *macname)
36 PROV_MAC_CTX *pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
42 pmacctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
43 if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL) {
44 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
48 mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
52 pmacctx->macctx = EVP_MAC_CTX_new(mac);
53 if (pmacctx->macctx == NULL)
61 OPENSSL_free(pmacctx);
66 #define MAC_NEWCTX(funcname, macname) \
67 static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
69 return mac_newctx(provctx, propq, macname); \
72 MAC_NEWCTX(hmac, "HMAC")
74 static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey)
76 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
77 OSSL_PARAM params[4], *p = params;
79 if (pmacctx == NULL || vkey == NULL || !mac_key_up_ref(vkey))
83 mac_key_free(pmacctx->key);
86 /* Read only so cast away of const is safe */
88 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
90 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
91 pmacctx->key->priv_key,
92 pmacctx->key->priv_key_len);
93 *p = OSSL_PARAM_construct_end();
95 if (!EVP_MAC_CTX_set_params(pmacctx->macctx, params))
98 if (!EVP_MAC_init(pmacctx->macctx))
104 int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
107 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
109 if (pmacctx == NULL || pmacctx->macctx == NULL)
112 return EVP_MAC_update(pmacctx->macctx, data, datalen);
115 int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
118 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
120 if (pmacctx == NULL || pmacctx->macctx == NULL)
123 return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
126 static void mac_freectx(void *vpmacctx)
128 PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
130 OPENSSL_free(ctx->propq);
131 EVP_MAC_CTX_free(ctx->macctx);
132 mac_key_free(ctx->key);
136 static void *mac_dupctx(void *vpmacctx)
138 PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
139 PROV_MAC_CTX *dstctx;
141 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
147 dstctx->macctx = NULL;
149 if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key))
151 dstctx->key = srcctx->key;
153 if (srcctx->macctx != NULL) {
154 dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
155 if (dstctx->macctx == NULL)
165 #define MAC_SIGNATURE_FUNCTIONS(funcname) \
166 const OSSL_DISPATCH mac_##funcname##_signature_functions[] = { \
167 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
168 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
169 (void (*)(void))mac_digest_sign_init }, \
170 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
171 (void (*)(void))mac_digest_sign_update }, \
172 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
173 (void (*)(void))mac_digest_sign_final }, \
174 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
175 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
179 MAC_SIGNATURE_FUNCTIONS(hmac)