/src/openssl36/providers/implementations/macs/poly1305_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 | | |
15 | | #include <openssl/core_dispatch.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/params.h> |
18 | | #include <openssl/evp.h> |
19 | | #include <openssl/err.h> |
20 | | #include <openssl/proverr.h> |
21 | | |
22 | | #include "internal/cryptlib.h" |
23 | | #include "crypto/poly1305.h" |
24 | | |
25 | | #include "prov/implementations.h" |
26 | | #include "prov/providercommon.h" |
27 | | |
28 | | /* |
29 | | * Forward declaration of everything implemented here. This is not strictly |
30 | | * necessary for the compiler, but provides an assurance that the signatures |
31 | | * of the functions in the dispatch table are correct. |
32 | | */ |
33 | | static OSSL_FUNC_mac_newctx_fn poly1305_new; |
34 | | static OSSL_FUNC_mac_dupctx_fn poly1305_dup; |
35 | | static OSSL_FUNC_mac_freectx_fn poly1305_free; |
36 | | static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params; |
37 | | static OSSL_FUNC_mac_get_params_fn poly1305_get_params; |
38 | | static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params; |
39 | | static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params; |
40 | | static OSSL_FUNC_mac_init_fn poly1305_init; |
41 | | static OSSL_FUNC_mac_update_fn poly1305_update; |
42 | | static OSSL_FUNC_mac_final_fn poly1305_final; |
43 | | |
44 | | struct poly1305_data_st { |
45 | | void *provctx; |
46 | | int updated; |
47 | | POLY1305 poly1305; /* Poly1305 data */ |
48 | | }; |
49 | | |
50 | | static void *poly1305_new(void *provctx) |
51 | 75 | { |
52 | 75 | struct poly1305_data_st *ctx; |
53 | | |
54 | 75 | if (!ossl_prov_is_running()) |
55 | 0 | return NULL; |
56 | 75 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
57 | 75 | if (ctx != NULL) |
58 | 75 | ctx->provctx = provctx; |
59 | 75 | return ctx; |
60 | 75 | } |
61 | | |
62 | | static void poly1305_free(void *vmacctx) |
63 | 75 | { |
64 | 75 | OPENSSL_free(vmacctx); |
65 | 75 | } |
66 | | |
67 | | static void *poly1305_dup(void *vsrc) |
68 | 0 | { |
69 | 0 | struct poly1305_data_st *src = vsrc; |
70 | 0 | struct poly1305_data_st *dst; |
71 | |
|
72 | 0 | if (!ossl_prov_is_running()) |
73 | 0 | return NULL; |
74 | 0 | dst = OPENSSL_malloc(sizeof(*dst)); |
75 | 0 | if (dst == NULL) |
76 | 0 | return NULL; |
77 | | |
78 | 0 | *dst = *src; |
79 | 0 | return dst; |
80 | 0 | } |
81 | | |
82 | | static size_t poly1305_size(void) |
83 | 560 | { |
84 | 560 | return POLY1305_DIGEST_SIZE; |
85 | 560 | } |
86 | | |
87 | | static int poly1305_setkey(struct poly1305_data_st *ctx, |
88 | | const unsigned char *key, size_t keylen) |
89 | 357 | { |
90 | 357 | if (keylen != POLY1305_KEY_SIZE) { |
91 | 73 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
92 | 73 | return 0; |
93 | 73 | } |
94 | 284 | Poly1305_Init(&ctx->poly1305, key); |
95 | 284 | ctx->updated = 0; |
96 | 284 | return 1; |
97 | 357 | } |
98 | | |
99 | | static int poly1305_init(void *vmacctx, const unsigned char *key, |
100 | | size_t keylen, const OSSL_PARAM params[]) |
101 | 353 | { |
102 | 353 | struct poly1305_data_st *ctx = vmacctx; |
103 | | |
104 | | /* initialize the context in MAC_ctrl function */ |
105 | 353 | if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params)) |
106 | 68 | return 0; |
107 | 285 | if (key != NULL) |
108 | 285 | return poly1305_setkey(ctx, key, keylen); |
109 | | /* no reinitialization of context with the same key is allowed */ |
110 | 0 | return ctx->updated == 0; |
111 | 285 | } |
112 | | |
113 | | static int poly1305_update(void *vmacctx, const unsigned char *data, |
114 | | size_t datalen) |
115 | 310 | { |
116 | 310 | struct poly1305_data_st *ctx = vmacctx; |
117 | | |
118 | 310 | ctx->updated = 1; |
119 | 310 | if (datalen == 0) |
120 | 0 | return 1; |
121 | | |
122 | | /* poly1305 has nothing to return in its update function */ |
123 | 310 | Poly1305_Update(&ctx->poly1305, data, datalen); |
124 | 310 | return 1; |
125 | 310 | } |
126 | | |
127 | | static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl, |
128 | | size_t outsize) |
129 | 280 | { |
130 | 280 | struct poly1305_data_st *ctx = vmacctx; |
131 | | |
132 | 280 | if (!ossl_prov_is_running()) |
133 | 0 | return 0; |
134 | 280 | ctx->updated = 1; |
135 | 280 | Poly1305_Final(&ctx->poly1305, out); |
136 | 280 | *outl = poly1305_size(); |
137 | 280 | return 1; |
138 | 280 | } |
139 | | |
140 | | /* clang-format off */ |
141 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
142 | | #ifndef poly1305_get_params_list |
143 | | static const OSSL_PARAM poly1305_get_params_list[] = { |
144 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
145 | | OSSL_PARAM_END |
146 | | }; |
147 | | #endif |
148 | | |
149 | | #ifndef poly1305_get_params_st |
150 | | struct poly1305_get_params_st { |
151 | | OSSL_PARAM *size; |
152 | | }; |
153 | | #endif |
154 | | |
155 | | #ifndef poly1305_get_params_decoder |
156 | | static int poly1305_get_params_decoder |
157 | | (const OSSL_PARAM *p, struct poly1305_get_params_st *r) |
158 | 0 | { |
159 | 0 | const char *s; |
160 | |
|
161 | 0 | memset(r, 0, sizeof(*r)); |
162 | 0 | if (p != NULL) |
163 | 0 | for (; (s = p->key) != NULL; p++) |
164 | 0 | if (ossl_likely(strcmp("size", s + 0) == 0)) { |
165 | | /* OSSL_MAC_PARAM_SIZE */ |
166 | 0 | if (ossl_unlikely(r->size != NULL)) { |
167 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
168 | 0 | "param %s is repeated", s); |
169 | 0 | return 0; |
170 | 0 | } |
171 | 0 | r->size = (OSSL_PARAM *)p; |
172 | 0 | } |
173 | 0 | return 1; |
174 | 0 | } |
175 | | #endif |
176 | | /* End of machine generated */ |
177 | | /* clang-format on */ |
178 | | |
179 | | static const OSSL_PARAM *poly1305_gettable_params(void *provctx) |
180 | 0 | { |
181 | 0 | return poly1305_get_params_list; |
182 | 0 | } |
183 | | |
184 | | static int poly1305_get_params(OSSL_PARAM params[]) |
185 | 0 | { |
186 | 0 | struct poly1305_get_params_st p; |
187 | |
|
188 | 0 | if (!poly1305_get_params_decoder(params, &p)) |
189 | 0 | return 0; |
190 | | |
191 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, poly1305_size())) |
192 | 0 | return 0; |
193 | | |
194 | 0 | return 1; |
195 | 0 | } |
196 | | |
197 | | /* clang-format off */ |
198 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
199 | | #ifndef poly1305_set_ctx_params_list |
200 | | static const OSSL_PARAM poly1305_set_ctx_params_list[] = { |
201 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
202 | | OSSL_PARAM_END |
203 | | }; |
204 | | #endif |
205 | | |
206 | | #ifndef poly1305_set_ctx_params_st |
207 | | struct poly1305_set_ctx_params_st { |
208 | | OSSL_PARAM *key; |
209 | | }; |
210 | | #endif |
211 | | |
212 | | #ifndef poly1305_set_ctx_params_decoder |
213 | | static int poly1305_set_ctx_params_decoder |
214 | | (const OSSL_PARAM *p, struct poly1305_set_ctx_params_st *r) |
215 | 38 | { |
216 | 38 | const char *s; |
217 | | |
218 | 38 | memset(r, 0, sizeof(*r)); |
219 | 38 | if (p != NULL) |
220 | 76 | for (; (s = p->key) != NULL; p++) |
221 | 38 | if (ossl_likely(strcmp("key", s + 0) == 0)) { |
222 | | /* OSSL_MAC_PARAM_KEY */ |
223 | 38 | if (ossl_unlikely(r->key != NULL)) { |
224 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
225 | 0 | "param %s is repeated", s); |
226 | 0 | return 0; |
227 | 0 | } |
228 | 38 | r->key = (OSSL_PARAM *)p; |
229 | 38 | } |
230 | 38 | return 1; |
231 | 38 | } |
232 | | #endif |
233 | | /* End of machine generated */ |
234 | | /* clang-format on */ |
235 | | |
236 | | static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx, |
237 | | ossl_unused void *provctx) |
238 | 82 | { |
239 | 82 | return poly1305_set_ctx_params_list; |
240 | 82 | } |
241 | | |
242 | | static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params) |
243 | 38 | { |
244 | 38 | struct poly1305_data_st *ctx = vmacctx; |
245 | 38 | struct poly1305_set_ctx_params_st p; |
246 | | |
247 | 38 | if (ctx == NULL || !poly1305_set_ctx_params_decoder(params, &p)) |
248 | 0 | return 0; |
249 | | |
250 | 38 | if (p.key != NULL) |
251 | 38 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING |
252 | 38 | || !poly1305_setkey(ctx, p.key->data, p.key->data_size)) |
253 | 36 | return 0; |
254 | 2 | return 1; |
255 | 38 | } |
256 | | |
257 | | const OSSL_DISPATCH ossl_poly1305_functions[] = { |
258 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new }, |
259 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup }, |
260 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free }, |
261 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init }, |
262 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update }, |
263 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final }, |
264 | | { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params }, |
265 | | { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params }, |
266 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
267 | | (void (*)(void))poly1305_settable_ctx_params }, |
268 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params }, |
269 | | OSSL_DISPATCH_END |
270 | | }; |