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