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