Move GMAC to providers
[openssl.git] / providers / common / macs / gmac_prov.c
1 /*
2  * Copyright 2018 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_numbers.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 "internal/providercommonerr.h"
19 #include "internal/provider_algs.h"
20 #include "internal/provider_ctx.h"
21
22 /*
23  * Forward declaration of everything implemented here.  This is not strictly
24  * necessary for the compiler, but provides an assurance that the signatures
25  * of the functions in the dispatch table are correct.
26  */
27 static OSSL_OP_mac_newctx_fn gmac_new;
28 static OSSL_OP_mac_dupctx_fn gmac_dup;
29 static OSSL_OP_mac_freectx_fn gmac_free;
30 static OSSL_OP_mac_gettable_params_fn gmac_gettable_params;
31 static OSSL_OP_mac_get_params_fn gmac_get_params;
32 static OSSL_OP_mac_settable_ctx_params_fn gmac_settable_ctx_params;
33 static OSSL_OP_mac_ctx_set_params_fn gmac_ctx_set_params;
34 static OSSL_OP_mac_init_fn gmac_init;
35 static OSSL_OP_mac_update_fn gmac_update;
36 static OSSL_OP_mac_final_fn gmac_final;
37
38 /* local GMAC pkey structure */
39
40 struct gmac_data_st {
41     void *provctx;
42     EVP_CIPHER_CTX *ctx;         /* Cipher context */
43
44     /*
45      * References to the underlying cipher implementation.  |cipher| caches
46      * the cipher, always.  |alloc_cipher| only holds a reference to an
47      * explicitly fetched cipher.
48      * |cipher| is cleared after a CMAC_Init call.
49      */
50     const EVP_CIPHER *cipher;    /* Cache GCM cipher */
51     EVP_CIPHER *alloc_cipher;    /* Fetched cipher */
52
53     /*
54      * Conditions for legacy EVP_CIPHER uses.
55      */
56     ENGINE *engine;              /* Engine implementing the algorithm */
57 };
58
59 static size_t gmac_size(void);
60
61 static void gmac_free(void *vmacctx)
62 {
63     struct gmac_data_st *macctx = vmacctx;
64
65     if (macctx != NULL) {
66         EVP_CIPHER_CTX_free(macctx->ctx);
67         EVP_CIPHER_meth_free(macctx->alloc_cipher);
68         OPENSSL_free(macctx);
69     }
70 }
71
72 static void *gmac_new(void *provctx)
73 {
74     struct gmac_data_st *macctx;
75
76     if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
77         || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
78         gmac_free(macctx);
79         return NULL;
80     }
81     macctx->provctx = provctx;
82
83     return macctx;
84 }
85
86 static void *gmac_dup(void *vsrc)
87 {
88     struct gmac_data_st *src = vsrc;
89     struct gmac_data_st *dst = gmac_new(src->provctx);
90
91     if (dst == NULL)
92         return NULL;
93
94     if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
95         || (src->alloc_cipher != NULL
96             && !EVP_CIPHER_up_ref(src->alloc_cipher))) {
97         gmac_free(dst);
98         return NULL;
99     }
100
101     dst->cipher = src->cipher;
102     dst->alloc_cipher = src->alloc_cipher;
103     dst->engine = src->engine;
104     return dst;
105 }
106
107 static int gmac_init(void *vmacctx)
108 {
109     return 1;
110 }
111
112 static int gmac_update(void *vmacctx, const unsigned char *data,
113                        size_t datalen)
114 {
115     struct gmac_data_st *macctx = vmacctx;
116     EVP_CIPHER_CTX *ctx = macctx->ctx;
117     int outlen;
118
119     while (datalen > INT_MAX) {
120         if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
121             return 0;
122         data += INT_MAX;
123         datalen -= INT_MAX;
124     }
125     return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
126 }
127
128 static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
129                       size_t outsize)
130 {
131     struct gmac_data_st *macctx = vmacctx;
132     int hlen = 0;
133
134     if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
135         return 0;
136
137     /* TODO(3.0) Use params */
138     hlen = gmac_size();
139     if (!EVP_CIPHER_CTX_ctrl(macctx->ctx, EVP_CTRL_AEAD_GET_TAG,
140                              hlen, out))
141         return 0;
142
143     *outl = hlen;
144     return 1;
145 }
146
147 static size_t gmac_size(void)
148 {
149     return EVP_GCM_TLS_TAG_LEN;
150 }
151
152 static const OSSL_PARAM known_gettable_params[] = {
153     OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL),
154     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */
155     OSSL_PARAM_END
156 };
157 static const OSSL_PARAM *gmac_gettable_params(void)
158 {
159     return known_gettable_params;
160 }
161
162 static int gmac_get_params(OSSL_PARAM params[])
163 {
164     OSSL_PARAM *p;
165
166     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL
167         || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
168         return OSSL_PARAM_set_size_t(p, gmac_size());
169
170     return 1;
171 }
172
173 static const OSSL_PARAM known_settable_ctx_params[] = {
174     /* "algorithm" and "cipher" are the same parameter */
175     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ALGORITHM, NULL, 0),
176     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
177     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
178     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
179     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
180     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
181     OSSL_PARAM_END
182 };
183 static const OSSL_PARAM *gmac_settable_ctx_params(void)
184 {
185     return known_settable_ctx_params;
186 }
187
188 /*
189  * ALL parameters should be set before init().
190  */
191 static int gmac_ctx_set_params(void *vmacctx, const OSSL_PARAM params[])
192 {
193     struct gmac_data_st *macctx = vmacctx;
194     EVP_CIPHER_CTX *ctx = macctx->ctx;
195     const OSSL_PARAM *p;
196
197     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL
198         || (p = OSSL_PARAM_locate_const(params,
199                                         OSSL_MAC_PARAM_ALGORITHM)) != NULL) {
200         if (p->data_type != OSSL_PARAM_UTF8_STRING)
201             return 0;
202
203         {
204             const char *algoname = p->data;
205             const char *propquery = NULL;
206
207 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */
208             ENGINE_finish(macctx->engine);
209             macctx->engine = NULL;
210
211             if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
212                 != NULL) {
213                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
214                     return 0;
215
216                 macctx->engine = ENGINE_by_id(p->data);
217                 if (macctx->engine == NULL)
218                     return 0;
219             }
220 #endif
221             if ((p = OSSL_PARAM_locate_const(params,
222                                              OSSL_MAC_PARAM_PROPERTIES))
223                 != NULL) {
224                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
225                     return 0;
226
227                 propquery = p->data;
228             }
229
230             EVP_CIPHER_meth_free(macctx->alloc_cipher);
231             macctx->cipher = macctx->alloc_cipher = NULL;
232
233             macctx->cipher = macctx->alloc_cipher =
234                 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
235                                  algoname, propquery);
236 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
237             /* TODO(3.0) BEGIN legacy stuff, to be removed */
238             if (macctx->cipher == NULL)
239                 macctx->cipher = EVP_get_cipherbyname(algoname);
240             /* TODO(3.0) END of legacy stuff */
241 #endif
242
243             if (macctx->cipher == NULL)
244                 return 0;
245
246             if (EVP_CIPHER_mode(macctx->cipher) != EVP_CIPH_GCM_MODE) {
247                 ERR_raise(ERR_LIB_PROV, EVP_R_CIPHER_NOT_GCM_MODE);
248                 return 0;
249             }
250         }
251         if (!EVP_EncryptInit_ex(ctx, macctx->cipher, macctx->engine,
252                                 NULL, NULL))
253             return 0;
254     }
255     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
256         if (p->data_type != OSSL_PARAM_OCTET_STRING)
257             return 0;
258
259         if (p->data_size != (size_t)EVP_CIPHER_CTX_key_length(ctx)) {
260             ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY_LENGTH);
261             return 0;
262         }
263         if (!EVP_EncryptInit_ex(ctx, NULL, NULL, p->data, NULL))
264             return 0;
265     }
266     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
267         if (p->data_type != OSSL_PARAM_OCTET_STRING)
268             return 0;
269
270         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
271                                  p->data_size, NULL)
272             || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
273             return 0;
274     }
275     return 1;
276 }
277
278 const OSSL_DISPATCH gmac_functions[] = {
279     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
280     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
281     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
282     { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
283     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
284     { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
285     { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
286     { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
287     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
288       (void (*)(void))gmac_settable_ctx_params },
289     { OSSL_FUNC_MAC_CTX_SET_PARAMS, (void (*)(void))gmac_ctx_set_params },
290     { 0, NULL }
291 };