/src/openssl/providers/implementations/ciphers/cipher_chacha20.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-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 | | |
11 | | /* Dispatch functions for chacha20 cipher */ |
12 | | |
13 | | #include <openssl/proverr.h> |
14 | | #include <openssl/params.h> |
15 | | #include "cipher_chacha20.h" |
16 | | #include "prov/implementations.h" |
17 | | #include "prov/providercommon.h" |
18 | | |
19 | 16 | #define CHACHA20_KEYLEN (CHACHA_KEY_SIZE) |
20 | 16 | #define CHACHA20_BLKLEN (1) |
21 | 16 | #define CHACHA20_IVLEN (CHACHA_CTR_SIZE) |
22 | 16 | #define CHACHA20_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV) |
23 | | |
24 | | static OSSL_FUNC_cipher_newctx_fn chacha20_newctx; |
25 | | static OSSL_FUNC_cipher_freectx_fn chacha20_freectx; |
26 | | static OSSL_FUNC_cipher_dupctx_fn chacha20_dupctx; |
27 | | static OSSL_FUNC_cipher_get_params_fn chacha20_get_params; |
28 | | static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params; |
29 | | static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params; |
30 | | static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params; |
31 | | static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_settable_ctx_params; |
32 | | #define chacha20_cipher ossl_cipher_generic_cipher |
33 | | #define chacha20_update ossl_cipher_generic_stream_update |
34 | | #define chacha20_final ossl_cipher_generic_stream_final |
35 | | #define chacha20_gettable_params ossl_cipher_generic_gettable_params |
36 | | #define CHACHA_U32TOU8(ct, st) \ |
37 | 0 | do { \ |
38 | 0 | (ct)[3] = (unsigned char)((st) >> 24); \ |
39 | 0 | (ct)[2] = (unsigned char)((st) >> 16); \ |
40 | 0 | (ct)[1] = (unsigned char)((st) >> 8); \ |
41 | 0 | (ct)[0] = (unsigned char)(st); \ |
42 | 0 | } while (0) |
43 | | |
44 | | void ossl_chacha20_initctx(PROV_CHACHA20_CTX *ctx) |
45 | 0 | { |
46 | 0 | ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8, |
47 | 0 | CHACHA20_BLKLEN * 8, |
48 | 0 | CHACHA20_IVLEN * 8, |
49 | 0 | 0, CHACHA20_FLAGS, |
50 | 0 | ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8), |
51 | 0 | NULL); |
52 | 0 | } |
53 | | |
54 | | static void *chacha20_newctx(void *provctx) |
55 | 0 | { |
56 | 0 | PROV_CHACHA20_CTX *ctx; |
57 | |
|
58 | 0 | if (!ossl_prov_is_running()) |
59 | 0 | return NULL; |
60 | | |
61 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
62 | 0 | if (ctx != NULL) |
63 | 0 | ossl_chacha20_initctx(ctx); |
64 | 0 | return ctx; |
65 | 0 | } |
66 | | |
67 | | static void chacha20_freectx(void *vctx) |
68 | 0 | { |
69 | 0 | PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx; |
70 | |
|
71 | 0 | if (ctx != NULL) { |
72 | 0 | ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
73 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | static void *chacha20_dupctx(void *vctx) |
78 | 0 | { |
79 | 0 | PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx; |
80 | 0 | PROV_CHACHA20_CTX *dupctx = NULL; |
81 | |
|
82 | 0 | if (ctx != NULL) { |
83 | 0 | dupctx = OPENSSL_memdup(ctx, sizeof(*dupctx)); |
84 | 0 | if (dupctx != NULL && dupctx->base.tlsmac != NULL && dupctx->base.alloced) { |
85 | 0 | dupctx->base.tlsmac = OPENSSL_memdup(dupctx->base.tlsmac, |
86 | 0 | dupctx->base.tlsmacsize); |
87 | 0 | if (dupctx->base.tlsmac == NULL) { |
88 | 0 | OPENSSL_free(dupctx); |
89 | 0 | dupctx = NULL; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | 0 | return dupctx; |
94 | 0 | } |
95 | | |
96 | | static int chacha20_get_params(OSSL_PARAM params[]) |
97 | 16 | { |
98 | 16 | return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS, |
99 | 16 | CHACHA20_KEYLEN * 8, |
100 | 16 | CHACHA20_BLKLEN * 8, |
101 | 16 | CHACHA20_IVLEN * 8); |
102 | 16 | } |
103 | | |
104 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
105 | | #ifndef chacha20_get_ctx_params_list |
106 | | static const OSSL_PARAM chacha20_get_ctx_params_list[] = { |
107 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
108 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
109 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0), |
110 | | OSSL_PARAM_END |
111 | | }; |
112 | | #endif |
113 | | |
114 | | #ifndef chacha20_get_ctx_params_st |
115 | | struct chacha20_get_ctx_params_st { |
116 | | OSSL_PARAM *ivlen; |
117 | | OSSL_PARAM *keylen; |
118 | | OSSL_PARAM *upd_iv; |
119 | | }; |
120 | | #endif |
121 | | |
122 | | #ifndef chacha20_get_ctx_params_decoder |
123 | | static int chacha20_get_ctx_params_decoder |
124 | | (const OSSL_PARAM *p, struct chacha20_get_ctx_params_st *r) |
125 | 0 | { |
126 | 0 | const char *s; |
127 | |
|
128 | 0 | memset(r, 0, sizeof(*r)); |
129 | 0 | if (p != NULL) |
130 | 0 | for (; (s = p->key) != NULL; p++) |
131 | 0 | switch(s[0]) { |
132 | 0 | default: |
133 | 0 | break; |
134 | 0 | case 'i': |
135 | 0 | if (ossl_likely(strcmp("vlen", s + 1) == 0)) { |
136 | | /* OSSL_CIPHER_PARAM_IVLEN */ |
137 | 0 | if (ossl_unlikely(r->ivlen != NULL)) { |
138 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
139 | 0 | "param %s is repeated", s); |
140 | 0 | return 0; |
141 | 0 | } |
142 | 0 | r->ivlen = (OSSL_PARAM *)p; |
143 | 0 | } |
144 | 0 | break; |
145 | 0 | case 'k': |
146 | 0 | if (ossl_likely(strcmp("eylen", s + 1) == 0)) { |
147 | | /* OSSL_CIPHER_PARAM_KEYLEN */ |
148 | 0 | if (ossl_unlikely(r->keylen != NULL)) { |
149 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
150 | 0 | "param %s is repeated", s); |
151 | 0 | return 0; |
152 | 0 | } |
153 | 0 | r->keylen = (OSSL_PARAM *)p; |
154 | 0 | } |
155 | 0 | break; |
156 | 0 | case 'u': |
157 | 0 | if (ossl_likely(strcmp("pdated-iv", s + 1) == 0)) { |
158 | | /* OSSL_CIPHER_PARAM_UPDATED_IV */ |
159 | 0 | if (ossl_unlikely(r->upd_iv != NULL)) { |
160 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
161 | 0 | "param %s is repeated", s); |
162 | 0 | return 0; |
163 | 0 | } |
164 | 0 | r->upd_iv = (OSSL_PARAM *)p; |
165 | 0 | } |
166 | 0 | } |
167 | 0 | return 1; |
168 | 0 | } |
169 | | #endif |
170 | | /* End of machine generated */ |
171 | | |
172 | | static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
173 | 0 | { |
174 | 0 | PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx; |
175 | 0 | unsigned char ivbuf[CHACHA20_IVLEN]; |
176 | 0 | struct chacha20_get_ctx_params_st p; |
177 | |
|
178 | 0 | if (ctx == NULL || !chacha20_get_ctx_params_decoder(params, &p)) |
179 | 0 | return 0; |
180 | | |
181 | 0 | if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_IVLEN)) { |
182 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
183 | 0 | return 0; |
184 | 0 | } |
185 | | |
186 | 0 | if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_KEYLEN)) { |
187 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | 0 | if (p.upd_iv != NULL) { |
192 | 0 | CHACHA_U32TOU8(ivbuf, ctx->counter[0]); |
193 | 0 | CHACHA_U32TOU8(ivbuf + 4, ctx->counter[1]); |
194 | 0 | CHACHA_U32TOU8(ivbuf + 8, ctx->counter[2]); |
195 | 0 | CHACHA_U32TOU8(ivbuf + 12, ctx->counter[3]); |
196 | 0 | if (!OSSL_PARAM_set_octet_string(p.upd_iv, ivbuf, CHACHA20_IVLEN)) { |
197 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
198 | 0 | return 0; |
199 | 0 | } |
200 | 0 | } |
201 | 0 | return 1; |
202 | 0 | } |
203 | | |
204 | | const OSSL_PARAM *chacha20_gettable_ctx_params(ossl_unused void *cctx, |
205 | | ossl_unused void *provctx) |
206 | 16 | { |
207 | 16 | return chacha20_get_ctx_params_list; |
208 | 16 | } |
209 | | |
210 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
211 | | #ifndef chacha20_set_ctx_params_list |
212 | | static const OSSL_PARAM chacha20_set_ctx_params_list[] = { |
213 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
214 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
215 | | OSSL_PARAM_END |
216 | | }; |
217 | | #endif |
218 | | |
219 | | #ifndef chacha20_set_ctx_params_st |
220 | | struct chacha20_set_ctx_params_st { |
221 | | OSSL_PARAM *ivlen; |
222 | | OSSL_PARAM *keylen; |
223 | | }; |
224 | | #endif |
225 | | |
226 | | #ifndef chacha20_set_ctx_params_decoder |
227 | | static int chacha20_set_ctx_params_decoder |
228 | | (const OSSL_PARAM *p, struct chacha20_set_ctx_params_st *r) |
229 | 0 | { |
230 | 0 | const char *s; |
231 | |
|
232 | 0 | memset(r, 0, sizeof(*r)); |
233 | 0 | if (p != NULL) |
234 | 0 | for (; (s = p->key) != NULL; p++) |
235 | 0 | switch(s[0]) { |
236 | 0 | default: |
237 | 0 | break; |
238 | 0 | case 'i': |
239 | 0 | if (ossl_likely(strcmp("vlen", s + 1) == 0)) { |
240 | | /* OSSL_CIPHER_PARAM_IVLEN */ |
241 | 0 | if (ossl_unlikely(r->ivlen != NULL)) { |
242 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
243 | 0 | "param %s is repeated", s); |
244 | 0 | return 0; |
245 | 0 | } |
246 | 0 | r->ivlen = (OSSL_PARAM *)p; |
247 | 0 | } |
248 | 0 | break; |
249 | 0 | case 'k': |
250 | 0 | if (ossl_likely(strcmp("eylen", s + 1) == 0)) { |
251 | | /* OSSL_CIPHER_PARAM_KEYLEN */ |
252 | 0 | if (ossl_unlikely(r->keylen != NULL)) { |
253 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
254 | 0 | "param %s is repeated", s); |
255 | 0 | return 0; |
256 | 0 | } |
257 | 0 | r->keylen = (OSSL_PARAM *)p; |
258 | 0 | } |
259 | 0 | } |
260 | 0 | return 1; |
261 | 0 | } |
262 | | #endif |
263 | | /* End of machine generated */ |
264 | | |
265 | | static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
266 | 0 | { |
267 | 0 | PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx; |
268 | 0 | struct chacha20_set_ctx_params_st p; |
269 | 0 | size_t len; |
270 | |
|
271 | 0 | if (ctx == NULL || !chacha20_set_ctx_params_decoder(params, &p)) |
272 | 0 | return 0; |
273 | | |
274 | 0 | if (p.keylen != NULL) { |
275 | 0 | if (!OSSL_PARAM_get_size_t(p.keylen, &len)) { |
276 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
277 | 0 | return 0; |
278 | 0 | } |
279 | 0 | if (len != CHACHA20_KEYLEN) { |
280 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
281 | 0 | return 0; |
282 | 0 | } |
283 | 0 | } |
284 | | |
285 | 0 | if (p.ivlen != NULL) { |
286 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) { |
287 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
288 | 0 | return 0; |
289 | 0 | } |
290 | 0 | if (len != CHACHA20_IVLEN) { |
291 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
292 | 0 | return 0; |
293 | 0 | } |
294 | 0 | } |
295 | 0 | return 1; |
296 | 0 | } |
297 | | |
298 | | const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx, |
299 | | ossl_unused void *provctx) |
300 | 0 | { |
301 | 0 | return chacha20_set_ctx_params_list; |
302 | 0 | } |
303 | | |
304 | | int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen, |
305 | | const unsigned char *iv, size_t ivlen, |
306 | | const OSSL_PARAM params[]) |
307 | 0 | { |
308 | 0 | int ret; |
309 | | |
310 | | /* The generic function checks for ossl_prov_is_running() */ |
311 | 0 | ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); |
312 | 0 | if (ret && iv != NULL) { |
313 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
314 | 0 | PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw; |
315 | |
|
316 | 0 | hw->initiv(ctx); |
317 | 0 | } |
318 | 0 | if (ret && !chacha20_set_ctx_params(vctx, params)) |
319 | 0 | ret = 0; |
320 | 0 | return ret; |
321 | 0 | } |
322 | | |
323 | | int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen, |
324 | | const unsigned char *iv, size_t ivlen, |
325 | | const OSSL_PARAM params[]) |
326 | 0 | { |
327 | 0 | int ret; |
328 | | |
329 | | /* The generic function checks for ossl_prov_is_running() */ |
330 | 0 | ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); |
331 | 0 | if (ret && iv != NULL) { |
332 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
333 | 0 | PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw; |
334 | |
|
335 | 0 | hw->initiv(ctx); |
336 | 0 | } |
337 | 0 | if (ret && !chacha20_set_ctx_params(vctx, params)) |
338 | 0 | ret = 0; |
339 | 0 | return ret; |
340 | 0 | } |
341 | | |
342 | | /* ossl_chacha20_functions */ |
343 | | const OSSL_DISPATCH ossl_chacha20_functions[] = { |
344 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx }, |
345 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx }, |
346 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_dupctx }, |
347 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit }, |
348 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit }, |
349 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update }, |
350 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final }, |
351 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher}, |
352 | | { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params }, |
353 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))chacha20_gettable_params }, |
354 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params }, |
355 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, |
356 | | (void (*)(void))chacha20_gettable_ctx_params }, |
357 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params }, |
358 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, |
359 | | (void (*)(void))chacha20_settable_ctx_params }, |
360 | | OSSL_DISPATCH_END |
361 | | }; |
362 | | |