]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/signature/mac_legacy.c
Improve code reuse in the provider MAC bridge
[thirdparty/openssl.git] / providers / implementations / signature / mac_legacy.c
CommitLineData
409910be
MC
1/*
2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
2ef9a7ac
MC
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
409910be
MC
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"
22
23static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
24static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
25static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
26static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
27static OSSL_FUNC_signature_freectx_fn mac_freectx;
28static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
29
30typedef struct {
31 OPENSSL_CTX *libctx;
32 char *propq;
33 MAC_KEY *key;
34 EVP_MAC_CTX *macctx;
35} PROV_MAC_CTX;
36
37static void *mac_newctx(void *provctx, const char *propq, const char *macname)
38{
39 PROV_MAC_CTX *pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
40 EVP_MAC *mac = NULL;
41
42 if (pmacctx == NULL)
43 return NULL;
44
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);
48 goto err;
49 }
50
51 mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
52 if (mac == NULL)
53 goto err;
54
55 pmacctx->macctx = EVP_MAC_CTX_new(mac);
56 if (pmacctx->macctx == NULL)
57 goto err;
58
59 EVP_MAC_free(mac);
60
61 return pmacctx;
62
63 err:
64 OPENSSL_free(pmacctx);
65 EVP_MAC_free(mac);
66 return NULL;
67}
68
69#define MAC_NEWCTX(funcname, macname) \
70 static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
71 { \
72 return mac_newctx(provctx, propq, macname); \
73 }
74
75MAC_NEWCTX(hmac, "HMAC")
b27b31b6 76MAC_NEWCTX(siphash, "SIPHASH")
4db71d01 77MAC_NEWCTX(poly1305, "POLY1305")
a540ef90 78MAC_NEWCTX(cmac, "CMAC")
409910be
MC
79
80static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey)
81{
82 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
2ef9a7ac 83 const char *ciphername = NULL, *engine = NULL;
409910be
MC
84
85 if (pmacctx == NULL || vkey == NULL || !mac_key_up_ref(vkey))
86 return 0;
87
88
89 mac_key_free(pmacctx->key);
90 pmacctx->key = vkey;
91
2ef9a7ac
MC
92 if (pmacctx->key->cipher.cipher != NULL)
93 ciphername = (char *)EVP_CIPHER_name(pmacctx->key->cipher.cipher);
94#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
95 if (pmacctx->key->cipher.engine != NULL)
96 engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
97#endif
98
99 if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,
100 (char *)ciphername,
101 (char *)mdname,
102 (char *)engine,
103 pmacctx->key->properties,
104 pmacctx->key->priv_key,
105 pmacctx->key->priv_key_len))
409910be
MC
106 return 0;
107
108 if (!EVP_MAC_init(pmacctx->macctx))
109 return 0;
110
111 return 1;
112}
113
114int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
115 size_t datalen)
116{
117 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
118
119 if (pmacctx == NULL || pmacctx->macctx == NULL)
120 return 0;
121
122 return EVP_MAC_update(pmacctx->macctx, data, datalen);
123}
124
125int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
126 size_t macsize)
127{
128 PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
129
130 if (pmacctx == NULL || pmacctx->macctx == NULL)
131 return 0;
132
133 return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
134}
135
136static void mac_freectx(void *vpmacctx)
137{
138 PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
139
140 OPENSSL_free(ctx->propq);
141 EVP_MAC_CTX_free(ctx->macctx);
142 mac_key_free(ctx->key);
143 OPENSSL_free(ctx);
144}
145
146static void *mac_dupctx(void *vpmacctx)
147{
148 PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
149 PROV_MAC_CTX *dstctx;
150
151 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
152 if (dstctx == NULL)
153 return NULL;
154
155 *dstctx = *srcctx;
156 dstctx->key = NULL;
157 dstctx->macctx = NULL;
158
159 if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key))
160 goto err;
161 dstctx->key = srcctx->key;
162
163 if (srcctx->macctx != NULL) {
164 dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
165 if (dstctx->macctx == NULL)
166 goto err;
167 }
168
169 return dstctx;
170 err:
171 mac_freectx(dstctx);
172 return NULL;
173}
174
175#define MAC_SIGNATURE_FUNCTIONS(funcname) \
176 const OSSL_DISPATCH mac_##funcname##_signature_functions[] = { \
177 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
178 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
179 (void (*)(void))mac_digest_sign_init }, \
180 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
181 (void (*)(void))mac_digest_sign_update }, \
182 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
183 (void (*)(void))mac_digest_sign_final }, \
184 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
185 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
186 { 0, NULL } \
187 };
188
b27b31b6
MC
189MAC_SIGNATURE_FUNCTIONS(hmac)
190MAC_SIGNATURE_FUNCTIONS(siphash)
4db71d01 191MAC_SIGNATURE_FUNCTIONS(poly1305)
a540ef90 192MAC_SIGNATURE_FUNCTIONS(cmac)