prov: prefix all OSSL_DISPATCH tables names with ossl_
[openssl.git] / providers / implementations / macs / gmac_prov.c
1 /*
2  * Copyright 2018-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 <stdlib.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
17
18 #include "prov/providercommonerr.h"
19 #include "prov/implementations.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/provider_util.h"
22 #include "prov/providercommon.h"
23
24 /*
25  * Forward declaration of everything implemented here.  This is not strictly
26  * necessary for the compiler, but provides an assurance that the signatures
27  * of the functions in the dispatch table are correct.
28  */
29 static OSSL_FUNC_mac_newctx_fn gmac_new;
30 static OSSL_FUNC_mac_dupctx_fn gmac_dup;
31 static OSSL_FUNC_mac_freectx_fn gmac_free;
32 static OSSL_FUNC_mac_gettable_params_fn gmac_gettable_params;
33 static OSSL_FUNC_mac_get_params_fn gmac_get_params;
34 static OSSL_FUNC_mac_settable_ctx_params_fn gmac_settable_ctx_params;
35 static OSSL_FUNC_mac_set_ctx_params_fn gmac_set_ctx_params;
36 static OSSL_FUNC_mac_init_fn gmac_init;
37 static OSSL_FUNC_mac_update_fn gmac_update;
38 static OSSL_FUNC_mac_final_fn gmac_final;
39
40 /* local GMAC pkey structure */
41
42 struct gmac_data_st {
43     void *provctx;
44     EVP_CIPHER_CTX *ctx;         /* Cipher context */
45     PROV_CIPHER cipher;
46 };
47
48 static size_t gmac_size(void);
49
50 static void gmac_free(void *vmacctx)
51 {
52     struct gmac_data_st *macctx = vmacctx;
53
54     if (macctx != NULL) {
55         EVP_CIPHER_CTX_free(macctx->ctx);
56         ossl_prov_cipher_reset(&macctx->cipher);
57         OPENSSL_free(macctx);
58     }
59 }
60
61 static void *gmac_new(void *provctx)
62 {
63     struct gmac_data_st *macctx;
64
65     if (!ossl_prov_is_running())
66         return NULL;
67
68     if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
69         || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
70         gmac_free(macctx);
71         return NULL;
72     }
73     macctx->provctx = provctx;
74
75     return macctx;
76 }
77
78 static void *gmac_dup(void *vsrc)
79 {
80     struct gmac_data_st *src = vsrc;
81     struct gmac_data_st *dst;
82
83     if (!ossl_prov_is_running())
84         return NULL;
85
86     dst = gmac_new(src->provctx);
87     if (dst == NULL)
88         return NULL;
89
90     if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
91         || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
92         gmac_free(dst);
93         return NULL;
94     }
95     return dst;
96 }
97
98 static int gmac_init(void *vmacctx)
99 {
100     return ossl_prov_is_running();
101 }
102
103 static int gmac_update(void *vmacctx, const unsigned char *data,
104                        size_t datalen)
105 {
106     struct gmac_data_st *macctx = vmacctx;
107     EVP_CIPHER_CTX *ctx = macctx->ctx;
108     int outlen;
109
110     if (datalen == 0)
111         return 1;
112
113     while (datalen > INT_MAX) {
114         if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
115             return 0;
116         data += INT_MAX;
117         datalen -= INT_MAX;
118     }
119     return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
120 }
121
122 static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
123                       size_t outsize)
124 {
125     struct gmac_data_st *macctx = vmacctx;
126     int hlen = 0;
127
128     if (!ossl_prov_is_running())
129         return 0;
130
131     if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
132         return 0;
133
134     /* TODO(3.0) Use params */
135     hlen = gmac_size();
136     if (!EVP_CIPHER_CTX_ctrl(macctx->ctx, EVP_CTRL_AEAD_GET_TAG,
137                              hlen, out))
138         return 0;
139
140     *outl = hlen;
141     return 1;
142 }
143
144 static size_t gmac_size(void)
145 {
146     return EVP_GCM_TLS_TAG_LEN;
147 }
148
149 static const OSSL_PARAM known_gettable_params[] = {
150     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
151     OSSL_PARAM_END
152 };
153 static const OSSL_PARAM *gmac_gettable_params(void *provctx)
154 {
155     return known_gettable_params;
156 }
157
158 static int gmac_get_params(OSSL_PARAM params[])
159 {
160     OSSL_PARAM *p;
161
162     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
163         return OSSL_PARAM_set_size_t(p, gmac_size());
164
165     return 1;
166 }
167
168 static const OSSL_PARAM known_settable_ctx_params[] = {
169     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
170     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
171     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
172     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
173     OSSL_PARAM_END
174 };
175 static const OSSL_PARAM *gmac_settable_ctx_params(ossl_unused void *provctx)
176 {
177     return known_settable_ctx_params;
178 }
179
180 /*
181  * ALL parameters should be set before init().
182  */
183 static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
184 {
185     struct gmac_data_st *macctx = vmacctx;
186     EVP_CIPHER_CTX *ctx = macctx->ctx;
187     OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
188     const OSSL_PARAM *p;
189
190    if (ctx == NULL
191         || !ossl_prov_cipher_load_from_params(&macctx->cipher, params, provctx))
192         return 0;
193
194     if (EVP_CIPHER_mode(ossl_prov_cipher_cipher(&macctx->cipher))
195         != EVP_CIPH_GCM_MODE) {
196         ERR_raise(ERR_LIB_PROV, EVP_R_CIPHER_NOT_GCM_MODE);
197         return 0;
198     }
199     if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher),
200                             ossl_prov_cipher_engine(&macctx->cipher), NULL,
201                             NULL))
202         return 0;
203
204     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
205         if (p->data_type != OSSL_PARAM_OCTET_STRING)
206             return 0;
207
208         if (p->data_size != (size_t)EVP_CIPHER_CTX_key_length(ctx)) {
209             ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY_LENGTH);
210             return 0;
211         }
212         if (!EVP_EncryptInit_ex(ctx, NULL, NULL, p->data, NULL))
213             return 0;
214     }
215     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
216         if (p->data_type != OSSL_PARAM_OCTET_STRING)
217             return 0;
218
219         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
220                                  p->data_size, NULL)
221             || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
222             return 0;
223     }
224     return 1;
225 }
226
227 const OSSL_DISPATCH ossl_gmac_functions[] = {
228     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
229     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
230     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
231     { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
232     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
233     { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
234     { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
235     { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
236     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
237       (void (*)(void))gmac_settable_ctx_params },
238     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },
239     { 0, NULL }
240 };