Extend the provider MAC bridge for CMAC
[openssl.git] / providers / implementations / signature / mac_legacy.c
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
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"
19
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;
26
27 typedef struct {
28     OPENSSL_CTX *libctx;
29     char *propq;
30     MAC_KEY *key;
31     EVP_MAC_CTX *macctx;
32 } PROV_MAC_CTX;
33
34 static void *mac_newctx(void *provctx, const char *propq, const char *macname)
35 {
36     PROV_MAC_CTX *pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
37     EVP_MAC *mac = NULL;
38
39     if (pmacctx == NULL)
40         return NULL;
41
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);
45         goto err;
46     }
47
48     mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
49     if (mac == NULL)
50         goto err;
51
52     pmacctx->macctx = EVP_MAC_CTX_new(mac);
53     if (pmacctx->macctx == NULL)
54         goto err;
55
56     EVP_MAC_free(mac);
57
58     return pmacctx;
59
60  err:
61     OPENSSL_free(pmacctx);
62     EVP_MAC_free(mac);
63     return NULL;
64 }
65
66 #define MAC_NEWCTX(funcname, macname) \
67     static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
68     { \
69         return mac_newctx(provctx, propq, macname); \
70     }
71
72 MAC_NEWCTX(hmac, "HMAC")
73 MAC_NEWCTX(siphash, "SIPHASH")
74 MAC_NEWCTX(poly1305, "POLY1305")
75 MAC_NEWCTX(cmac, "CMAC")
76
77 static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey)
78 {
79     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
80     OSSL_PARAM params[5], *p = params;
81
82     if (pmacctx == NULL || vkey == NULL || !mac_key_up_ref(vkey))
83         return 0;
84
85
86     mac_key_free(pmacctx->key);
87     pmacctx->key = vkey;
88
89     /* Read only so cast away of const is safe */
90     if (mdname != NULL)
91         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
92                                                 (char *)mdname, 0);
93     if (pmacctx->key->cipher_name != NULL)
94         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
95                                                 pmacctx->key->cipher_name, 0);
96     if (pmacctx->key->engine_name != NULL)
97         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
98                                                 pmacctx->key->engine_name, 0);
99     *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
100                                              pmacctx->key->priv_key,
101                                              pmacctx->key->priv_key_len);
102     *p = OSSL_PARAM_construct_end();
103
104     if (!EVP_MAC_CTX_set_params(pmacctx->macctx, params))
105         return 0;
106
107     if (!EVP_MAC_init(pmacctx->macctx))
108         return 0;
109
110     return 1;
111 }
112
113 int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
114                            size_t datalen)
115 {
116     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
117
118     if (pmacctx == NULL || pmacctx->macctx == NULL)
119         return 0;
120
121     return EVP_MAC_update(pmacctx->macctx, data, datalen);
122 }
123
124 int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
125                           size_t macsize)
126 {
127     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
128
129     if (pmacctx == NULL || pmacctx->macctx == NULL)
130         return 0;
131
132     return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
133 }
134
135 static void mac_freectx(void *vpmacctx)
136 {
137     PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
138
139     OPENSSL_free(ctx->propq);
140     EVP_MAC_CTX_free(ctx->macctx);
141     mac_key_free(ctx->key);
142     OPENSSL_free(ctx);
143 }
144
145 static void *mac_dupctx(void *vpmacctx)
146 {
147     PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
148     PROV_MAC_CTX *dstctx;
149
150     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
151     if (dstctx == NULL)
152         return NULL;
153
154     *dstctx = *srcctx;
155     dstctx->key = NULL;
156     dstctx->macctx = NULL;
157
158     if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key))
159         goto err;
160     dstctx->key = srcctx->key;
161
162     if (srcctx->macctx != NULL) {
163         dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
164         if (dstctx->macctx == NULL)
165             goto err;
166     }
167
168     return dstctx;
169  err:
170     mac_freectx(dstctx);
171     return NULL;
172 }
173
174 #define MAC_SIGNATURE_FUNCTIONS(funcname) \
175     const OSSL_DISPATCH mac_##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 }, \
185         { 0, NULL } \
186     };
187
188 MAC_SIGNATURE_FUNCTIONS(hmac)
189 MAC_SIGNATURE_FUNCTIONS(siphash)
190 MAC_SIGNATURE_FUNCTIONS(poly1305)
191 MAC_SIGNATURE_FUNCTIONS(cmac)