/src/openssl/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 | | |
10 | | #include <string.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/params.h> |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/err.h> |
16 | | #include <openssl/proverr.h> |
17 | | |
18 | | #include "internal/cryptlib.h" |
19 | | #include "crypto/siphash.h" |
20 | | |
21 | | #include "prov/implementations.h" |
22 | | #include "prov/providercommon.h" |
23 | | #include "providers/implementations/macs/siphash_prov.inc" |
24 | | |
25 | | /* |
26 | | * Forward declaration of everything implemented here. This is not strictly |
27 | | * necessary for the compiler, but provides an assurance that the signatures |
28 | | * of the functions in the dispatch table are correct. |
29 | | */ |
30 | | static OSSL_FUNC_mac_newctx_fn siphash_new; |
31 | | static OSSL_FUNC_mac_dupctx_fn siphash_dup; |
32 | | static OSSL_FUNC_mac_freectx_fn siphash_free; |
33 | | static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params; |
34 | | static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params; |
35 | | static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_ctx_params; |
36 | | static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params; |
37 | | static OSSL_FUNC_mac_init_fn siphash_init; |
38 | | static OSSL_FUNC_mac_update_fn siphash_update; |
39 | | static OSSL_FUNC_mac_final_fn siphash_final; |
40 | | |
41 | | struct siphash_data_st { |
42 | | void *provctx; |
43 | | SIPHASH siphash; /* Siphash data */ |
44 | | SIPHASH sipcopy; /* Siphash data copy for reinitialization */ |
45 | | unsigned int crounds, drounds; |
46 | | }; |
47 | | |
48 | | static unsigned int crounds(struct siphash_data_st *ctx) |
49 | 0 | { |
50 | 0 | return ctx->crounds != 0 ? ctx->crounds : SIPHASH_C_ROUNDS; |
51 | 0 | } |
52 | | |
53 | | static unsigned int drounds(struct siphash_data_st *ctx) |
54 | 0 | { |
55 | 0 | return ctx->drounds != 0 ? ctx->drounds : SIPHASH_D_ROUNDS; |
56 | 0 | } |
57 | | |
58 | | static void *siphash_new(void *provctx) |
59 | 0 | { |
60 | 0 | struct siphash_data_st *ctx; |
61 | |
|
62 | 0 | if (!ossl_prov_is_running()) |
63 | 0 | return NULL; |
64 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
65 | 0 | if (ctx != NULL) |
66 | 0 | ctx->provctx = provctx; |
67 | 0 | return ctx; |
68 | 0 | } |
69 | | |
70 | | static void siphash_free(void *vmacctx) |
71 | 0 | { |
72 | 0 | OPENSSL_free(vmacctx); |
73 | 0 | } |
74 | | |
75 | | static void *siphash_dup(void *vsrc) |
76 | 0 | { |
77 | 0 | struct siphash_data_st *ssrc = vsrc; |
78 | 0 | struct siphash_data_st *sdst; |
79 | |
|
80 | 0 | if (!ossl_prov_is_running()) |
81 | 0 | return NULL; |
82 | 0 | sdst = OPENSSL_malloc(sizeof(*sdst)); |
83 | 0 | if (sdst == NULL) |
84 | 0 | return NULL; |
85 | | |
86 | 0 | *sdst = *ssrc; |
87 | 0 | return sdst; |
88 | 0 | } |
89 | | |
90 | | static size_t siphash_size(void *vmacctx) |
91 | 0 | { |
92 | 0 | struct siphash_data_st *ctx = vmacctx; |
93 | |
|
94 | 0 | return SipHash_hash_size(&ctx->siphash); |
95 | 0 | } |
96 | | |
97 | | static int siphash_setkey(struct siphash_data_st *ctx, |
98 | | const unsigned char *key, size_t keylen) |
99 | 0 | { |
100 | 0 | int ret; |
101 | |
|
102 | 0 | if (keylen != SIPHASH_KEY_SIZE) |
103 | 0 | return 0; |
104 | 0 | ret = SipHash_Init(&ctx->siphash, key, crounds(ctx), drounds(ctx)); |
105 | 0 | if (ret) |
106 | 0 | ctx->sipcopy = ctx->siphash; |
107 | 0 | return ret; |
108 | 0 | } |
109 | | |
110 | | static int siphash_init(void *vmacctx, const unsigned char *key, size_t keylen, |
111 | | const OSSL_PARAM params[]) |
112 | 0 | { |
113 | 0 | struct siphash_data_st *ctx = vmacctx; |
114 | |
|
115 | 0 | if (!ossl_prov_is_running() || !siphash_set_params(ctx, params)) |
116 | 0 | return 0; |
117 | | /* |
118 | | * Without a key, there is not much to do here, |
119 | | * The actual initialization happens through controls. |
120 | | */ |
121 | 0 | if (key == NULL) { |
122 | 0 | ctx->siphash = ctx->sipcopy; |
123 | 0 | return 1; |
124 | 0 | } |
125 | 0 | return siphash_setkey(ctx, key, keylen); |
126 | 0 | } |
127 | | |
128 | | static int siphash_update(void *vmacctx, const unsigned char *data, |
129 | | size_t datalen) |
130 | 0 | { |
131 | 0 | struct siphash_data_st *ctx = vmacctx; |
132 | |
|
133 | 0 | if (datalen == 0) |
134 | 0 | return 1; |
135 | | |
136 | 0 | SipHash_Update(&ctx->siphash, data, datalen); |
137 | 0 | return 1; |
138 | 0 | } |
139 | | |
140 | | static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl, |
141 | | size_t outsize) |
142 | 0 | { |
143 | 0 | struct siphash_data_st *ctx = vmacctx; |
144 | 0 | size_t hlen = siphash_size(ctx); |
145 | |
|
146 | 0 | if (!ossl_prov_is_running() || outsize < hlen) |
147 | 0 | return 0; |
148 | | |
149 | 0 | *outl = hlen; |
150 | 0 | return SipHash_Final(&ctx->siphash, out, hlen); |
151 | 0 | } |
152 | | |
153 | | static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx, |
154 | | ossl_unused void *provctx) |
155 | 0 | { |
156 | 0 | return siphash_get_ctx_params_list; |
157 | 0 | } |
158 | | |
159 | | static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
160 | 0 | { |
161 | 0 | struct siphash_data_st *ctx = vmacctx; |
162 | 0 | struct siphash_get_ctx_params_st p; |
163 | |
|
164 | 0 | if (ctx == NULL || !siphash_get_ctx_params_decoder(params, &p)) |
165 | 0 | return 0; |
166 | | |
167 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, siphash_size(vmacctx))) |
168 | 0 | return 0; |
169 | 0 | if (p.c != NULL && !OSSL_PARAM_set_uint(p.c, crounds(ctx))) |
170 | 0 | return 0; |
171 | 0 | if (p.d != NULL && !OSSL_PARAM_set_uint(p.d, drounds(ctx))) |
172 | 0 | return 0; |
173 | 0 | return 1; |
174 | 0 | } |
175 | | |
176 | | static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx, |
177 | | void *provctx) |
178 | 0 | { |
179 | 0 | return siphash_set_params_list; |
180 | 0 | } |
181 | | |
182 | | static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params) |
183 | 0 | { |
184 | 0 | struct siphash_data_st *ctx = vmacctx; |
185 | 0 | struct siphash_set_params_st p; |
186 | 0 | size_t size; |
187 | |
|
188 | 0 | if (ctx == NULL || !siphash_set_params_decoder(params, &p)) |
189 | 0 | return 0; |
190 | | |
191 | 0 | if (p.size != NULL) { |
192 | 0 | if (!OSSL_PARAM_get_size_t(p.size, &size) |
193 | 0 | || !SipHash_set_hash_size(&ctx->siphash, size) |
194 | 0 | || !SipHash_set_hash_size(&ctx->sipcopy, size)) |
195 | 0 | return 0; |
196 | 0 | } |
197 | 0 | if (p.c != NULL && !OSSL_PARAM_get_uint(p.c, &ctx->crounds)) |
198 | 0 | return 0; |
199 | 0 | if (p.d != NULL && !OSSL_PARAM_get_uint(p.d, &ctx->drounds)) |
200 | 0 | return 0; |
201 | 0 | if (p.key != NULL) |
202 | 0 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING |
203 | 0 | || !siphash_setkey(ctx, p.key->data, p.key->data_size)) |
204 | 0 | return 0; |
205 | 0 | return 1; |
206 | 0 | } |
207 | | |
208 | | const OSSL_DISPATCH ossl_siphash_functions[] = { |
209 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new }, |
210 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup }, |
211 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free }, |
212 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init }, |
213 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update }, |
214 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final }, |
215 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
216 | | (void (*)(void))siphash_gettable_ctx_params }, |
217 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params }, |
218 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
219 | | (void (*)(void))siphash_settable_ctx_params }, |
220 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params }, |
221 | | OSSL_DISPATCH_END |
222 | | }; |