/src/openssl36/providers/implementations/macs/siphash_prov.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018-2025 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 | | /* clang-format off */ |
10 | | |
11 | | /* clang-format on */ |
12 | | |
13 | | #include <string.h> |
14 | | #include <openssl/core_dispatch.h> |
15 | | #include <openssl/core_names.h> |
16 | | #include <openssl/params.h> |
17 | | #include <openssl/evp.h> |
18 | | #include <openssl/err.h> |
19 | | #include <openssl/proverr.h> |
20 | | |
21 | | #include "internal/cryptlib.h" |
22 | | #include "crypto/siphash.h" |
23 | | |
24 | | #include "prov/implementations.h" |
25 | | #include "prov/providercommon.h" |
26 | | |
27 | | /* |
28 | | * Forward declaration of everything implemented here. This is not strictly |
29 | | * necessary for the compiler, but provides an assurance that the signatures |
30 | | * of the functions in the dispatch table are correct. |
31 | | */ |
32 | | static OSSL_FUNC_mac_newctx_fn siphash_new; |
33 | | static OSSL_FUNC_mac_dupctx_fn siphash_dup; |
34 | | static OSSL_FUNC_mac_freectx_fn siphash_free; |
35 | | static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params; |
36 | | static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params; |
37 | | static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_ctx_params; |
38 | | static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params; |
39 | | static OSSL_FUNC_mac_init_fn siphash_init; |
40 | | static OSSL_FUNC_mac_update_fn siphash_update; |
41 | | static OSSL_FUNC_mac_final_fn siphash_final; |
42 | | |
43 | | struct siphash_data_st { |
44 | | void *provctx; |
45 | | SIPHASH siphash; /* Siphash data */ |
46 | | SIPHASH sipcopy; /* Siphash data copy for reinitialization */ |
47 | | unsigned int crounds, drounds; |
48 | | }; |
49 | | |
50 | | static unsigned int crounds(struct siphash_data_st *ctx) |
51 | 2.18k | { |
52 | 2.18k | return ctx->crounds != 0 ? ctx->crounds : SIPHASH_C_ROUNDS; |
53 | 2.18k | } |
54 | | |
55 | | static unsigned int drounds(struct siphash_data_st *ctx) |
56 | 2.18k | { |
57 | 2.18k | return ctx->drounds != 0 ? ctx->drounds : SIPHASH_D_ROUNDS; |
58 | 2.18k | } |
59 | | |
60 | | static void *siphash_new(void *provctx) |
61 | 136 | { |
62 | 136 | struct siphash_data_st *ctx; |
63 | | |
64 | 136 | if (!ossl_prov_is_running()) |
65 | 0 | return NULL; |
66 | 136 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
67 | 136 | if (ctx != NULL) |
68 | 136 | ctx->provctx = provctx; |
69 | 136 | return ctx; |
70 | 136 | } |
71 | | |
72 | | static void siphash_free(void *vmacctx) |
73 | 136 | { |
74 | 136 | OPENSSL_free(vmacctx); |
75 | 136 | } |
76 | | |
77 | | static void *siphash_dup(void *vsrc) |
78 | 0 | { |
79 | 0 | struct siphash_data_st *ssrc = vsrc; |
80 | 0 | struct siphash_data_st *sdst; |
81 | |
|
82 | 0 | if (!ossl_prov_is_running()) |
83 | 0 | return NULL; |
84 | 0 | sdst = OPENSSL_malloc(sizeof(*sdst)); |
85 | 0 | if (sdst == NULL) |
86 | 0 | return NULL; |
87 | | |
88 | 0 | *sdst = *ssrc; |
89 | 0 | return sdst; |
90 | 0 | } |
91 | | |
92 | | static size_t siphash_size(void *vmacctx) |
93 | 4.28k | { |
94 | 4.28k | struct siphash_data_st *ctx = vmacctx; |
95 | | |
96 | 4.28k | return SipHash_hash_size(&ctx->siphash); |
97 | 4.28k | } |
98 | | |
99 | | static int siphash_setkey(struct siphash_data_st *ctx, |
100 | | const unsigned char *key, size_t keylen) |
101 | 2.26k | { |
102 | 2.26k | int ret; |
103 | | |
104 | 2.26k | if (keylen != SIPHASH_KEY_SIZE) |
105 | 82 | return 0; |
106 | 2.18k | ret = SipHash_Init(&ctx->siphash, key, crounds(ctx), drounds(ctx)); |
107 | 2.18k | if (ret) |
108 | 2.18k | ctx->sipcopy = ctx->siphash; |
109 | 2.18k | return ret; |
110 | 2.26k | } |
111 | | |
112 | | static int siphash_init(void *vmacctx, const unsigned char *key, size_t keylen, |
113 | | const OSSL_PARAM params[]) |
114 | 2.26k | { |
115 | 2.26k | struct siphash_data_st *ctx = vmacctx; |
116 | | |
117 | 2.26k | if (!ossl_prov_is_running() || !siphash_set_params(ctx, params)) |
118 | 83 | return 0; |
119 | | /* |
120 | | * Without a key, there is not much to do here, |
121 | | * The actual initialization happens through controls. |
122 | | */ |
123 | 2.18k | if (key == NULL) { |
124 | 0 | ctx->siphash = ctx->sipcopy; |
125 | 0 | return 1; |
126 | 0 | } |
127 | 2.18k | return siphash_setkey(ctx, key, keylen); |
128 | 2.18k | } |
129 | | |
130 | | static int siphash_update(void *vmacctx, const unsigned char *data, |
131 | | size_t datalen) |
132 | 2.26k | { |
133 | 2.26k | struct siphash_data_st *ctx = vmacctx; |
134 | | |
135 | 2.26k | if (datalen == 0) |
136 | 0 | return 1; |
137 | | |
138 | 2.26k | SipHash_Update(&ctx->siphash, data, datalen); |
139 | 2.26k | return 1; |
140 | 2.26k | } |
141 | | |
142 | | static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl, |
143 | | size_t outsize) |
144 | 2.14k | { |
145 | 2.14k | struct siphash_data_st *ctx = vmacctx; |
146 | 2.14k | size_t hlen = siphash_size(ctx); |
147 | | |
148 | 2.14k | if (!ossl_prov_is_running() || outsize < hlen) |
149 | 0 | return 0; |
150 | | |
151 | 2.14k | *outl = hlen; |
152 | 2.14k | return SipHash_Final(&ctx->siphash, out, hlen); |
153 | 2.14k | } |
154 | | |
155 | | /* clang-format off */ |
156 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
157 | | #ifndef siphash_get_ctx_params_list |
158 | | static const OSSL_PARAM siphash_get_ctx_params_list[] = { |
159 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
160 | | OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL), |
161 | | OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL), |
162 | | OSSL_PARAM_END |
163 | | }; |
164 | | #endif |
165 | | |
166 | | #ifndef siphash_get_ctx_params_st |
167 | | struct siphash_get_ctx_params_st { |
168 | | OSSL_PARAM *c; |
169 | | OSSL_PARAM *d; |
170 | | OSSL_PARAM *size; |
171 | | }; |
172 | | #endif |
173 | | |
174 | | #ifndef siphash_get_ctx_params_decoder |
175 | | static int siphash_get_ctx_params_decoder |
176 | | (const OSSL_PARAM *p, struct siphash_get_ctx_params_st *r) |
177 | 536 | { |
178 | 536 | const char *s; |
179 | | |
180 | 536 | memset(r, 0, sizeof(*r)); |
181 | 536 | if (p != NULL) |
182 | 1.07k | for (; (s = p->key) != NULL; p++) |
183 | 536 | switch(s[0]) { |
184 | 0 | default: |
185 | 0 | break; |
186 | 0 | case 'c': |
187 | 0 | if (ossl_likely(strcmp("-rounds", s + 1) == 0)) { |
188 | | /* OSSL_MAC_PARAM_C_ROUNDS */ |
189 | 0 | if (ossl_unlikely(r->c != NULL)) { |
190 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
191 | 0 | "param %s is repeated", s); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 | r->c = (OSSL_PARAM *)p; |
195 | 0 | } |
196 | 0 | break; |
197 | 0 | case 'd': |
198 | 0 | if (ossl_likely(strcmp("-rounds", s + 1) == 0)) { |
199 | | /* OSSL_MAC_PARAM_D_ROUNDS */ |
200 | 0 | if (ossl_unlikely(r->d != NULL)) { |
201 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
202 | 0 | "param %s is repeated", s); |
203 | 0 | return 0; |
204 | 0 | } |
205 | 0 | r->d = (OSSL_PARAM *)p; |
206 | 0 | } |
207 | 0 | break; |
208 | 536 | case 's': |
209 | 536 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
210 | | /* OSSL_MAC_PARAM_SIZE */ |
211 | 536 | if (ossl_unlikely(r->size != NULL)) { |
212 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
213 | 0 | "param %s is repeated", s); |
214 | 0 | return 0; |
215 | 0 | } |
216 | 536 | r->size = (OSSL_PARAM *)p; |
217 | 536 | } |
218 | 536 | } |
219 | 536 | return 1; |
220 | 536 | } |
221 | | #endif |
222 | | /* End of machine generated */ |
223 | | /* clang-format on */ |
224 | | |
225 | | static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx, |
226 | | ossl_unused void *provctx) |
227 | 0 | { |
228 | 0 | return siphash_get_ctx_params_list; |
229 | 0 | } |
230 | | |
231 | | static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
232 | 536 | { |
233 | 536 | struct siphash_data_st *ctx = vmacctx; |
234 | 536 | struct siphash_get_ctx_params_st p; |
235 | | |
236 | 536 | if (ctx == NULL || !siphash_get_ctx_params_decoder(params, &p)) |
237 | 0 | return 0; |
238 | | |
239 | 536 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, siphash_size(vmacctx))) |
240 | 0 | return 0; |
241 | 536 | if (p.c != NULL && !OSSL_PARAM_set_uint(p.c, crounds(ctx))) |
242 | 0 | return 0; |
243 | 536 | if (p.d != NULL && !OSSL_PARAM_set_uint(p.d, drounds(ctx))) |
244 | 0 | return 0; |
245 | 536 | return 1; |
246 | 536 | } |
247 | | |
248 | | /* clang-format off */ |
249 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
250 | | #ifndef siphash_set_params_list |
251 | | static const OSSL_PARAM siphash_set_params_list[] = { |
252 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
253 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
254 | | OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL), |
255 | | OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL), |
256 | | OSSL_PARAM_END |
257 | | }; |
258 | | #endif |
259 | | |
260 | | #ifndef siphash_set_params_st |
261 | | struct siphash_set_params_st { |
262 | | OSSL_PARAM *c; |
263 | | OSSL_PARAM *d; |
264 | | OSSL_PARAM *key; |
265 | | OSSL_PARAM *size; |
266 | | }; |
267 | | #endif |
268 | | |
269 | | #ifndef siphash_set_params_decoder |
270 | | static int siphash_set_params_decoder |
271 | | (const OSSL_PARAM *p, struct siphash_set_params_st *r) |
272 | 610 | { |
273 | 610 | const char *s; |
274 | | |
275 | 610 | memset(r, 0, sizeof(*r)); |
276 | 610 | if (p != NULL) |
277 | 357 | for (; (s = p->key) != NULL; p++) |
278 | 284 | switch(s[0]) { |
279 | 5 | default: |
280 | 5 | break; |
281 | 70 | case 'c': |
282 | 70 | if (ossl_likely(strcmp("-rounds", s + 1) == 0)) { |
283 | | /* OSSL_MAC_PARAM_C_ROUNDS */ |
284 | 68 | if (ossl_unlikely(r->c != NULL)) { |
285 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
286 | 0 | "param %s is repeated", s); |
287 | 0 | return 0; |
288 | 0 | } |
289 | 68 | r->c = (OSSL_PARAM *)p; |
290 | 68 | } |
291 | 70 | break; |
292 | 73 | case 'd': |
293 | 73 | if (ossl_likely(strcmp("-rounds", s + 1) == 0)) { |
294 | | /* OSSL_MAC_PARAM_D_ROUNDS */ |
295 | 68 | if (ossl_unlikely(r->d != NULL)) { |
296 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
297 | 0 | "param %s is repeated", s); |
298 | 0 | return 0; |
299 | 0 | } |
300 | 68 | r->d = (OSSL_PARAM *)p; |
301 | 68 | } |
302 | 73 | break; |
303 | 73 | case 'k': |
304 | 68 | if (ossl_likely(strcmp("ey", s + 1) == 0)) { |
305 | | /* OSSL_MAC_PARAM_KEY */ |
306 | 68 | if (ossl_unlikely(r->key != NULL)) { |
307 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
308 | 0 | "param %s is repeated", s); |
309 | 0 | return 0; |
310 | 0 | } |
311 | 68 | r->key = (OSSL_PARAM *)p; |
312 | 68 | } |
313 | 68 | break; |
314 | 68 | case 's': |
315 | 68 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
316 | | /* OSSL_MAC_PARAM_SIZE */ |
317 | 68 | if (ossl_unlikely(r->size != NULL)) { |
318 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
319 | 0 | "param %s is repeated", s); |
320 | 0 | return 0; |
321 | 0 | } |
322 | 68 | r->size = (OSSL_PARAM *)p; |
323 | 68 | } |
324 | 284 | } |
325 | 610 | return 1; |
326 | 610 | } |
327 | | #endif |
328 | | /* End of machine generated */ |
329 | | /* clang-format on */ |
330 | | |
331 | | static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx, |
332 | | void *provctx) |
333 | 124 | { |
334 | 124 | return siphash_set_params_list; |
335 | 124 | } |
336 | | |
337 | | static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params) |
338 | 610 | { |
339 | 610 | struct siphash_data_st *ctx = vmacctx; |
340 | 610 | struct siphash_set_params_st p; |
341 | 610 | size_t size; |
342 | | |
343 | 610 | if (ctx == NULL || !siphash_set_params_decoder(params, &p)) |
344 | 0 | return 0; |
345 | | |
346 | 610 | if (p.size != NULL) { |
347 | 68 | if (!OSSL_PARAM_get_size_t(p.size, &size) |
348 | 68 | || !SipHash_set_hash_size(&ctx->siphash, size) |
349 | 49 | || !SipHash_set_hash_size(&ctx->sipcopy, size)) |
350 | 19 | return 0; |
351 | 68 | } |
352 | 591 | if (p.c != NULL && !OSSL_PARAM_get_uint(p.c, &ctx->crounds)) |
353 | 0 | return 0; |
354 | 591 | if (p.d != NULL && !OSSL_PARAM_get_uint(p.d, &ctx->drounds)) |
355 | 0 | return 0; |
356 | 591 | if (p.key != NULL) |
357 | 49 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING |
358 | 49 | || !siphash_setkey(ctx, p.key->data, p.key->data_size)) |
359 | 32 | return 0; |
360 | 559 | return 1; |
361 | 591 | } |
362 | | |
363 | | const OSSL_DISPATCH ossl_siphash_functions[] = { |
364 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new }, |
365 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup }, |
366 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free }, |
367 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init }, |
368 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update }, |
369 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final }, |
370 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
371 | | (void (*)(void))siphash_gettable_ctx_params }, |
372 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params }, |
373 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
374 | | (void (*)(void))siphash_settable_ctx_params }, |
375 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params }, |
376 | | OSSL_DISPATCH_END |
377 | | }; |