Extend the provider MAC bridge for SIPHASH
[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
75 static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey)
76 {
77     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
78     OSSL_PARAM params[4], *p = params;
79
80     if (pmacctx == NULL || vkey == NULL || !mac_key_up_ref(vkey))
81         return 0;
82
83
84     mac_key_free(pmacctx->key);
85     pmacctx->key = vkey;
86
87     /* Read only so cast away of const is safe */
88     if (mdname != NULL)
89         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
90                                                 (char *)mdname, 0);
91     *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
92                                              pmacctx->key->priv_key,
93                                              pmacctx->key->priv_key_len);
94     *p = OSSL_PARAM_construct_end();
95
96     if (!EVP_MAC_CTX_set_params(pmacctx->macctx, params))
97         return 0;
98
99     if (!EVP_MAC_init(pmacctx->macctx))
100         return 0;
101
102     return 1;
103 }
104
105 int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
106                            size_t datalen)
107 {
108     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
109
110     if (pmacctx == NULL || pmacctx->macctx == NULL)
111         return 0;
112
113     return EVP_MAC_update(pmacctx->macctx, data, datalen);
114 }
115
116 int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
117                           size_t macsize)
118 {
119     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
120
121     if (pmacctx == NULL || pmacctx->macctx == NULL)
122         return 0;
123
124     return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
125 }
126
127 static void mac_freectx(void *vpmacctx)
128 {
129     PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
130
131     OPENSSL_free(ctx->propq);
132     EVP_MAC_CTX_free(ctx->macctx);
133     mac_key_free(ctx->key);
134     OPENSSL_free(ctx);
135 }
136
137 static void *mac_dupctx(void *vpmacctx)
138 {
139     PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
140     PROV_MAC_CTX *dstctx;
141
142     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
143     if (dstctx == NULL)
144         return NULL;
145
146     *dstctx = *srcctx;
147     dstctx->key = NULL;
148     dstctx->macctx = NULL;
149
150     if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key))
151         goto err;
152     dstctx->key = srcctx->key;
153
154     if (srcctx->macctx != NULL) {
155         dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
156         if (dstctx->macctx == NULL)
157             goto err;
158     }
159
160     return dstctx;
161  err:
162     mac_freectx(dstctx);
163     return NULL;
164 }
165
166 #define MAC_SIGNATURE_FUNCTIONS(funcname) \
167     const OSSL_DISPATCH mac_##funcname##_signature_functions[] = { \
168         { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
169         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
170         (void (*)(void))mac_digest_sign_init }, \
171         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
172         (void (*)(void))mac_digest_sign_update }, \
173         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
174         (void (*)(void))mac_digest_sign_final }, \
175         { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
176         { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
177         { 0, NULL } \
178     };
179
180 MAC_SIGNATURE_FUNCTIONS(hmac)
181 MAC_SIGNATURE_FUNCTIONS(siphash)