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 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/crypto.h>
14 #include <openssl/evp.h>
15 #include <openssl/core_dispatch.h>
16 #include <openssl/core_names.h>
17 #include <openssl/params.h>
18 #include <openssl/err.h>
19 #include "prov/implementations.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/macsignature.h"
23 static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
24 static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
25 static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
26 static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
27 static OSSL_FUNC_signature_freectx_fn mac_freectx;
28 static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
37 static void *mac_newctx(void *provctx, const char *propq, const char *macname)
39 PROV_MAC_CTX *pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
45 pmacctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
46 if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL) {
47 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
51 mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
55 pmacctx->macctx = EVP_MAC_CTX_new(mac);
56 if (pmacctx->macctx == NULL)
64 OPENSSL_free(pmacctx);
69 #define MAC_NEWCTX(funcname, macname) \
70 static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
72 return mac_newctx(provctx, propq, macname); \
75 MAC_NEWCTX(hmac, "HMAC")
76 MAC_NEWCTX(siphash, "SIPHASH")
77 MAC_NEWCTX(poly1305, "POLY1305")
78 MAC_NEWCTX(cmac, "CMAC")
80 static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey)
82 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
83 const char *ciphername = NULL, *engine = NULL;
85 if (pmacctx == NULL || vkey == NULL || !mac_key_up_ref(vkey))
88 mac_key_free(pmacctx->key);
91 if (pmacctx->key->cipher.cipher != NULL)
92 ciphername = (char *)EVP_CIPHER_name(pmacctx->key->cipher.cipher);
93 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
94 if (pmacctx->key->cipher.engine != NULL)
95 engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
98 if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,
102 pmacctx->key->properties,
103 pmacctx->key->priv_key,
104 pmacctx->key->priv_key_len))
107 if (!EVP_MAC_init(pmacctx->macctx))
113 int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
116 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
118 if (pmacctx == NULL || pmacctx->macctx == NULL)
121 return EVP_MAC_update(pmacctx->macctx, data, datalen);
124 int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
127 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
129 if (pmacctx == NULL || pmacctx->macctx == NULL)
132 return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
135 static void mac_freectx(void *vpmacctx)
137 PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
139 OPENSSL_free(ctx->propq);
140 EVP_MAC_CTX_free(ctx->macctx);
141 mac_key_free(ctx->key);
145 static void *mac_dupctx(void *vpmacctx)
147 PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
148 PROV_MAC_CTX *dstctx;
150 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
156 dstctx->macctx = NULL;
158 if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key))
160 dstctx->key = srcctx->key;
162 if (srcctx->macctx != NULL) {
163 dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
164 if (dstctx->macctx == NULL)
174 #define MAC_SIGNATURE_FUNCTIONS(funcname) \
175 const OSSL_DISPATCH mac_legacy_##funcname##_signature_functions[] = { \
176 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
177 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
178 (void (*)(void))mac_digest_sign_init }, \
179 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
180 (void (*)(void))mac_digest_sign_update }, \
181 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
182 (void (*)(void))mac_digest_sign_final }, \
183 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
184 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
188 MAC_SIGNATURE_FUNCTIONS(hmac)
189 MAC_SIGNATURE_FUNCTIONS(siphash)
190 MAC_SIGNATURE_FUNCTIONS(poly1305)
191 MAC_SIGNATURE_FUNCTIONS(cmac)