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 <string.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 "crypto/siphash.h"
19*b077aed3SPierre Pronchery
20*b077aed3SPierre Pronchery #include "prov/implementations.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 siphash_new;
29*b077aed3SPierre Pronchery static OSSL_FUNC_mac_dupctx_fn siphash_dup;
30*b077aed3SPierre Pronchery static OSSL_FUNC_mac_freectx_fn siphash_free;
31*b077aed3SPierre Pronchery static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
32*b077aed3SPierre Pronchery static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params;
33*b077aed3SPierre Pronchery static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_ctx_params;
34*b077aed3SPierre Pronchery static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
35*b077aed3SPierre Pronchery static OSSL_FUNC_mac_init_fn siphash_init;
36*b077aed3SPierre Pronchery static OSSL_FUNC_mac_update_fn siphash_update;
37*b077aed3SPierre Pronchery static OSSL_FUNC_mac_final_fn siphash_final;
38*b077aed3SPierre Pronchery
39*b077aed3SPierre Pronchery struct siphash_data_st {
40*b077aed3SPierre Pronchery void *provctx;
41*b077aed3SPierre Pronchery SIPHASH siphash; /* Siphash data */
42*b077aed3SPierre Pronchery SIPHASH sipcopy; /* Siphash data copy for reinitialization */
43*b077aed3SPierre Pronchery unsigned int crounds, drounds;
44*b077aed3SPierre Pronchery };
45*b077aed3SPierre Pronchery
crounds(struct siphash_data_st * ctx)46*b077aed3SPierre Pronchery static unsigned int crounds(struct siphash_data_st *ctx)
47*b077aed3SPierre Pronchery {
48*b077aed3SPierre Pronchery return ctx->crounds != 0 ? ctx->crounds : SIPHASH_C_ROUNDS;
49*b077aed3SPierre Pronchery }
50*b077aed3SPierre Pronchery
drounds(struct siphash_data_st * ctx)51*b077aed3SPierre Pronchery static unsigned int drounds(struct siphash_data_st *ctx)
52*b077aed3SPierre Pronchery {
53*b077aed3SPierre Pronchery return ctx->drounds != 0 ? ctx->drounds : SIPHASH_D_ROUNDS;
54*b077aed3SPierre Pronchery }
55*b077aed3SPierre Pronchery
siphash_new(void * provctx)56*b077aed3SPierre Pronchery static void *siphash_new(void *provctx)
57*b077aed3SPierre Pronchery {
58*b077aed3SPierre Pronchery struct siphash_data_st *ctx;
59*b077aed3SPierre Pronchery
60*b077aed3SPierre Pronchery if (!ossl_prov_is_running())
61*b077aed3SPierre Pronchery return NULL;
62*b077aed3SPierre Pronchery ctx = OPENSSL_zalloc(sizeof(*ctx));
63*b077aed3SPierre Pronchery if (ctx != NULL)
64*b077aed3SPierre Pronchery ctx->provctx = provctx;
65*b077aed3SPierre Pronchery return ctx;
66*b077aed3SPierre Pronchery }
67*b077aed3SPierre Pronchery
siphash_free(void * vmacctx)68*b077aed3SPierre Pronchery static void siphash_free(void *vmacctx)
69*b077aed3SPierre Pronchery {
70*b077aed3SPierre Pronchery OPENSSL_free(vmacctx);
71*b077aed3SPierre Pronchery }
72*b077aed3SPierre Pronchery
siphash_dup(void * vsrc)73*b077aed3SPierre Pronchery static void *siphash_dup(void *vsrc)
74*b077aed3SPierre Pronchery {
75*b077aed3SPierre Pronchery struct siphash_data_st *ssrc = vsrc;
76*b077aed3SPierre Pronchery struct siphash_data_st *sdst;
77*b077aed3SPierre Pronchery
78*b077aed3SPierre Pronchery if (!ossl_prov_is_running())
79*b077aed3SPierre Pronchery return NULL;
80*b077aed3SPierre Pronchery sdst = OPENSSL_malloc(sizeof(*sdst));
81*b077aed3SPierre Pronchery if (sdst == NULL)
82*b077aed3SPierre Pronchery return NULL;
83*b077aed3SPierre Pronchery
84*b077aed3SPierre Pronchery *sdst = *ssrc;
85*b077aed3SPierre Pronchery return sdst;
86*b077aed3SPierre Pronchery }
87*b077aed3SPierre Pronchery
siphash_size(void * vmacctx)88*b077aed3SPierre Pronchery static size_t siphash_size(void *vmacctx)
89*b077aed3SPierre Pronchery {
90*b077aed3SPierre Pronchery struct siphash_data_st *ctx = vmacctx;
91*b077aed3SPierre Pronchery
92*b077aed3SPierre Pronchery return SipHash_hash_size(&ctx->siphash);
93*b077aed3SPierre Pronchery }
94*b077aed3SPierre Pronchery
siphash_setkey(struct siphash_data_st * ctx,const unsigned char * key,size_t keylen)95*b077aed3SPierre Pronchery static int siphash_setkey(struct siphash_data_st *ctx,
96*b077aed3SPierre Pronchery const unsigned char *key, size_t keylen)
97*b077aed3SPierre Pronchery {
98*b077aed3SPierre Pronchery int ret;
99*b077aed3SPierre Pronchery
100*b077aed3SPierre Pronchery if (keylen != SIPHASH_KEY_SIZE)
101*b077aed3SPierre Pronchery return 0;
102*b077aed3SPierre Pronchery ret = SipHash_Init(&ctx->siphash, key, crounds(ctx), drounds(ctx));
103*b077aed3SPierre Pronchery if (ret)
104*b077aed3SPierre Pronchery ctx->sipcopy = ctx->siphash;
105*b077aed3SPierre Pronchery return ret;
106*b077aed3SPierre Pronchery }
107*b077aed3SPierre Pronchery
siphash_init(void * vmacctx,const unsigned char * key,size_t keylen,const OSSL_PARAM params[])108*b077aed3SPierre Pronchery static int siphash_init(void *vmacctx, const unsigned char *key, size_t keylen,
109*b077aed3SPierre Pronchery const OSSL_PARAM params[])
110*b077aed3SPierre Pronchery {
111*b077aed3SPierre Pronchery struct siphash_data_st *ctx = vmacctx;
112*b077aed3SPierre Pronchery
113*b077aed3SPierre Pronchery if (!ossl_prov_is_running() || !siphash_set_params(ctx, params))
114*b077aed3SPierre Pronchery return 0;
115*b077aed3SPierre Pronchery /*
116*b077aed3SPierre Pronchery * Without a key, there is not much to do here,
117*b077aed3SPierre Pronchery * The actual initialization happens through controls.
118*b077aed3SPierre Pronchery */
119*b077aed3SPierre Pronchery if (key == NULL) {
120*b077aed3SPierre Pronchery ctx->siphash = ctx->sipcopy;
121*b077aed3SPierre Pronchery return 1;
122*b077aed3SPierre Pronchery }
123*b077aed3SPierre Pronchery return siphash_setkey(ctx, key, keylen);
124*b077aed3SPierre Pronchery }
125*b077aed3SPierre Pronchery
siphash_update(void * vmacctx,const unsigned char * data,size_t datalen)126*b077aed3SPierre Pronchery static int siphash_update(void *vmacctx, const unsigned char *data,
127*b077aed3SPierre Pronchery size_t datalen)
128*b077aed3SPierre Pronchery {
129*b077aed3SPierre Pronchery struct siphash_data_st *ctx = vmacctx;
130*b077aed3SPierre Pronchery
131*b077aed3SPierre Pronchery if (datalen == 0)
132*b077aed3SPierre Pronchery return 1;
133*b077aed3SPierre Pronchery
134*b077aed3SPierre Pronchery SipHash_Update(&ctx->siphash, data, datalen);
135*b077aed3SPierre Pronchery return 1;
136*b077aed3SPierre Pronchery }
137*b077aed3SPierre Pronchery
siphash_final(void * vmacctx,unsigned char * out,size_t * outl,size_t outsize)138*b077aed3SPierre Pronchery static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
139*b077aed3SPierre Pronchery size_t outsize)
140*b077aed3SPierre Pronchery {
141*b077aed3SPierre Pronchery struct siphash_data_st *ctx = vmacctx;
142*b077aed3SPierre Pronchery size_t hlen = siphash_size(ctx);
143*b077aed3SPierre Pronchery
144*b077aed3SPierre Pronchery if (!ossl_prov_is_running() || outsize < hlen)
145*b077aed3SPierre Pronchery return 0;
146*b077aed3SPierre Pronchery
147*b077aed3SPierre Pronchery *outl = hlen;
148*b077aed3SPierre Pronchery return SipHash_Final(&ctx->siphash, out, hlen);
149*b077aed3SPierre Pronchery }
150*b077aed3SPierre Pronchery
siphash_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)151*b077aed3SPierre Pronchery static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx,
152*b077aed3SPierre Pronchery ossl_unused void *provctx)
153*b077aed3SPierre Pronchery {
154*b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
155*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
156*b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
157*b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
158*b077aed3SPierre Pronchery OSSL_PARAM_END
159*b077aed3SPierre Pronchery };
160*b077aed3SPierre Pronchery
161*b077aed3SPierre Pronchery return known_gettable_ctx_params;
162*b077aed3SPierre Pronchery }
163*b077aed3SPierre Pronchery
siphash_get_ctx_params(void * vmacctx,OSSL_PARAM params[])164*b077aed3SPierre Pronchery static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
165*b077aed3SPierre Pronchery {
166*b077aed3SPierre Pronchery struct siphash_data_st *ctx = vmacctx;
167*b077aed3SPierre Pronchery OSSL_PARAM *p;
168*b077aed3SPierre Pronchery
169*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
170*b077aed3SPierre Pronchery && !OSSL_PARAM_set_size_t(p, siphash_size(vmacctx)))
171*b077aed3SPierre Pronchery return 0;
172*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL
173*b077aed3SPierre Pronchery && !OSSL_PARAM_set_uint(p, crounds(ctx)))
174*b077aed3SPierre Pronchery return 0;
175*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL
176*b077aed3SPierre Pronchery && !OSSL_PARAM_set_uint(p, drounds(ctx)))
177*b077aed3SPierre Pronchery return 0;
178*b077aed3SPierre Pronchery return 1;
179*b077aed3SPierre Pronchery }
180*b077aed3SPierre Pronchery
siphash_settable_ctx_params(ossl_unused void * ctx,void * provctx)181*b077aed3SPierre Pronchery static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx,
182*b077aed3SPierre Pronchery void *provctx)
183*b077aed3SPierre Pronchery {
184*b077aed3SPierre Pronchery static const OSSL_PARAM known_settable_ctx_params[] = {
185*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
186*b077aed3SPierre Pronchery OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
187*b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
188*b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
189*b077aed3SPierre Pronchery OSSL_PARAM_END
190*b077aed3SPierre Pronchery };
191*b077aed3SPierre Pronchery
192*b077aed3SPierre Pronchery return known_settable_ctx_params;
193*b077aed3SPierre Pronchery }
194*b077aed3SPierre Pronchery
siphash_set_params(void * vmacctx,const OSSL_PARAM * params)195*b077aed3SPierre Pronchery static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
196*b077aed3SPierre Pronchery {
197*b077aed3SPierre Pronchery struct siphash_data_st *ctx = vmacctx;
198*b077aed3SPierre Pronchery const OSSL_PARAM *p = NULL;
199*b077aed3SPierre Pronchery size_t size;
200*b077aed3SPierre Pronchery
201*b077aed3SPierre Pronchery if (params == NULL)
202*b077aed3SPierre Pronchery return 1;
203*b077aed3SPierre Pronchery
204*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
205*b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &size)
206*b077aed3SPierre Pronchery || !SipHash_set_hash_size(&ctx->siphash, size)
207*b077aed3SPierre Pronchery || !SipHash_set_hash_size(&ctx->sipcopy, size))
208*b077aed3SPierre Pronchery return 0;
209*b077aed3SPierre Pronchery }
210*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL
211*b077aed3SPierre Pronchery && !OSSL_PARAM_get_uint(p, &ctx->crounds))
212*b077aed3SPierre Pronchery return 0;
213*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL
214*b077aed3SPierre Pronchery && !OSSL_PARAM_get_uint(p, &ctx->drounds))
215*b077aed3SPierre Pronchery return 0;
216*b077aed3SPierre Pronchery if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
217*b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING
218*b077aed3SPierre Pronchery || !siphash_setkey(ctx, p->data, p->data_size))
219*b077aed3SPierre Pronchery return 0;
220*b077aed3SPierre Pronchery return 1;
221*b077aed3SPierre Pronchery }
222*b077aed3SPierre Pronchery
223*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_siphash_functions[] = {
224*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
225*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
226*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
227*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
228*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
229*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
230*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
231*b077aed3SPierre Pronchery (void (*)(void))siphash_gettable_ctx_params },
232*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
233*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
234*b077aed3SPierre Pronchery (void (*)(void))siphash_settable_ctx_params },
235*b077aed3SPierre Pronchery { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
236*b077aed3SPierre Pronchery { 0, NULL }
237*b077aed3SPierre Pronchery };
238