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