/src/openssl/providers/implementations/ciphers/cipher_chacha20_poly1305.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-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 | | |
10 | | /* Dispatch functions for chacha20_poly1305 cipher */ |
11 | | |
12 | | #include <string.h> |
13 | | #include <openssl/proverr.h> |
14 | | #include "cipher_chacha20_poly1305.h" |
15 | | #include "prov/implementations.h" |
16 | | #include "prov/providercommon.h" |
17 | | #include "providers/implementations/ciphers/cipher_chacha20_poly1305.inc" |
18 | | |
19 | 1 | #define CHACHA20_POLY1305_KEYLEN CHACHA_KEY_SIZE |
20 | 1 | #define CHACHA20_POLY1305_BLKLEN 1 |
21 | 0 | #define CHACHA20_POLY1305_MAX_IVLEN 12 |
22 | 1 | #define CHACHA20_POLY1305_MODE EVP_CIPH_STREAM_CIPHER |
23 | 1 | #define CHACHA20_POLY1305_FLAGS (PROV_CIPHER_FLAG_AEAD \ |
24 | 1 | | PROV_CIPHER_FLAG_CUSTOM_IV) |
25 | | |
26 | | static OSSL_FUNC_cipher_newctx_fn chacha20_poly1305_newctx; |
27 | | static OSSL_FUNC_cipher_freectx_fn chacha20_poly1305_freectx; |
28 | | static OSSL_FUNC_cipher_dupctx_fn chacha20_poly1305_dupctx; |
29 | | static OSSL_FUNC_cipher_encrypt_init_fn chacha20_poly1305_einit; |
30 | | static OSSL_FUNC_cipher_decrypt_init_fn chacha20_poly1305_dinit; |
31 | | static OSSL_FUNC_cipher_get_params_fn chacha20_poly1305_get_params; |
32 | | static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_poly1305_get_ctx_params; |
33 | | static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_poly1305_set_ctx_params; |
34 | | static OSSL_FUNC_cipher_cipher_fn chacha20_poly1305_cipher; |
35 | | static OSSL_FUNC_cipher_final_fn chacha20_poly1305_final; |
36 | | static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_poly1305_gettable_ctx_params; |
37 | | static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_poly1305_settable_ctx_params; |
38 | | #define chacha20_poly1305_gettable_params ossl_cipher_generic_gettable_params |
39 | | |
40 | | static void *chacha20_poly1305_newctx(void *provctx) |
41 | 0 | { |
42 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx; |
43 | |
|
44 | 0 | if (!ossl_prov_is_running()) |
45 | 0 | return NULL; |
46 | | |
47 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
48 | 0 | if (ctx != NULL) { |
49 | 0 | ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8, |
50 | 0 | CHACHA20_POLY1305_BLKLEN * 8, |
51 | 0 | CHACHA20_POLY1305_IVLEN * 8, |
52 | 0 | CHACHA20_POLY1305_MODE, |
53 | 0 | CHACHA20_POLY1305_FLAGS, |
54 | 0 | ossl_prov_cipher_hw_chacha20_poly1305( |
55 | 0 | CHACHA20_POLY1305_KEYLEN * 8), |
56 | 0 | NULL); |
57 | 0 | ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH; |
58 | 0 | ossl_chacha20_initctx(&ctx->chacha); |
59 | 0 | } |
60 | 0 | return ctx; |
61 | 0 | } |
62 | | |
63 | | static void *chacha20_poly1305_dupctx(void *provctx) |
64 | 0 | { |
65 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = provctx; |
66 | 0 | PROV_CHACHA20_POLY1305_CTX *dctx = NULL; |
67 | |
|
68 | 0 | if (ctx == NULL) |
69 | 0 | return NULL; |
70 | 0 | dctx = OPENSSL_memdup(ctx, sizeof(*ctx)); |
71 | 0 | if (dctx != NULL |
72 | 0 | && !ossl_cipher_generic_dupctx_tlsmac(&dctx->base, &ctx->base)) { |
73 | 0 | OPENSSL_clear_free(dctx, sizeof(*dctx)); |
74 | 0 | return NULL; |
75 | 0 | } |
76 | 0 | return dctx; |
77 | 0 | } |
78 | | |
79 | | static void chacha20_poly1305_freectx(void *vctx) |
80 | 0 | { |
81 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
82 | |
|
83 | 0 | if (ctx != NULL) { |
84 | 0 | ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
85 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | static int chacha20_poly1305_get_params(OSSL_PARAM params[]) |
90 | 1 | { |
91 | 1 | return ossl_cipher_generic_get_params(params, |
92 | 1 | CHACHA20_POLY1305_MODE, CHACHA20_POLY1305_FLAGS, |
93 | 1 | CHACHA20_POLY1305_KEYLEN * 8, |
94 | 1 | CHACHA20_POLY1305_BLKLEN * 8, |
95 | 1 | CHACHA20_POLY1305_IVLEN * 8); |
96 | 1 | } |
97 | | |
98 | | static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
99 | 0 | { |
100 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
101 | 0 | struct chacha20_poly1305_get_ctx_params_st p; |
102 | |
|
103 | 0 | if (ctx == NULL || !chacha20_poly1305_get_ctx_params_decoder(params, &p)) |
104 | 0 | return 0; |
105 | | |
106 | 0 | if (p.ivlen != NULL |
107 | 0 | && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_POLY1305_IVLEN)) { |
108 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | 0 | if (p.keylen != NULL |
113 | 0 | && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_POLY1305_KEYLEN)) { |
114 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
115 | 0 | return 0; |
116 | 0 | } |
117 | | |
118 | | /* tag_len == 0 means no tag set yet, report the default */ |
119 | 0 | if (p.taglen != NULL |
120 | 0 | && !OSSL_PARAM_set_size_t(p.taglen, |
121 | 0 | ctx->tag_len == 0 ? POLY1305_BLOCK_SIZE : ctx->tag_len)) { |
122 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
123 | 0 | return 0; |
124 | 0 | } |
125 | | |
126 | 0 | if (p.pad != NULL |
127 | 0 | && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
128 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
129 | 0 | return 0; |
130 | 0 | } |
131 | | |
132 | 0 | if (p.tag != NULL) { |
133 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
134 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
135 | 0 | return 0; |
136 | 0 | } |
137 | 0 | if (!ctx->base.enc) { |
138 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
139 | 0 | return 0; |
140 | 0 | } |
141 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
142 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
143 | 0 | return 0; |
144 | 0 | } |
145 | 0 | memcpy(p.tag->data, ctx->tag, p.tag->data_size); |
146 | 0 | } |
147 | 0 | return 1; |
148 | 0 | } |
149 | | |
150 | | static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx) |
151 | 1 | { |
152 | 1 | return chacha20_poly1305_get_ctx_params_list; |
153 | 1 | } |
154 | | |
155 | | static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params( |
156 | | ossl_unused void *cctx, ossl_unused void *provctx) |
157 | 0 | { |
158 | 0 | return chacha20_poly1305_set_ctx_params_list; |
159 | 0 | } |
160 | | |
161 | | static int chacha20_poly1305_set_ctx_params(void *vctx, |
162 | | const OSSL_PARAM params[]) |
163 | 0 | { |
164 | 0 | size_t len; |
165 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
166 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; |
167 | 0 | struct chacha20_poly1305_set_ctx_params_st p; |
168 | |
|
169 | 0 | if (!chacha20_poly1305_set_ctx_params_decoder(params, &p)) |
170 | 0 | return 0; |
171 | | |
172 | 0 | if (p.keylen != NULL) { |
173 | 0 | if (!OSSL_PARAM_get_size_t(p.keylen, &len)) { |
174 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
175 | 0 | return 0; |
176 | 0 | } |
177 | 0 | if (len != CHACHA20_POLY1305_KEYLEN) { |
178 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
179 | 0 | return 0; |
180 | 0 | } |
181 | 0 | } |
182 | | |
183 | 0 | if (p.ivlen != NULL) { |
184 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) { |
185 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | if (len != CHACHA20_POLY1305_MAX_IVLEN) { |
189 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
190 | 0 | return 0; |
191 | 0 | } |
192 | 0 | ctx->iv_state = IV_STATE_UNINITIALISED; |
193 | 0 | } |
194 | | |
195 | 0 | if (p.tag != NULL) { |
196 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
197 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
198 | 0 | return 0; |
199 | 0 | } |
200 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
201 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | if (p.tag->data != NULL) { |
205 | 0 | if (ctx->base.enc) { |
206 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
207 | 0 | return 0; |
208 | 0 | } |
209 | 0 | memcpy(ctx->tag, p.tag->data, p.tag->data_size); |
210 | 0 | } |
211 | 0 | ctx->tag_len = p.tag->data_size; |
212 | 0 | } |
213 | | |
214 | 0 | if (p.aad != NULL) { |
215 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
216 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
217 | 0 | return 0; |
218 | 0 | } |
219 | 0 | len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size); |
220 | 0 | if (len == 0) { |
221 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
222 | 0 | return 0; |
223 | 0 | } |
224 | 0 | ctx->tls_aad_pad_sz = len; |
225 | 0 | } |
226 | | |
227 | 0 | if (p.fixed != NULL) { |
228 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
229 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data, |
233 | 0 | p.fixed->data_size) |
234 | 0 | == 0) { |
235 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
236 | 0 | return 0; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | return 1; |
240 | 0 | } |
241 | | |
242 | | static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, |
243 | | size_t keylen, const unsigned char *iv, |
244 | | size_t ivlen, const OSSL_PARAM params[]) |
245 | 0 | { |
246 | 0 | int ret; |
247 | | |
248 | | /* The generic function checks for ossl_prov_is_running() */ |
249 | 0 | ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); |
250 | 0 | if (ret && iv != NULL) { |
251 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
252 | 0 | PROV_CHACHA20_POLY1305_CTX *cctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
253 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
254 | |
|
255 | 0 | hw->initiv(ctx); |
256 | 0 | cctx->iv_state = IV_STATE_BUFFERED; |
257 | 0 | } |
258 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
259 | 0 | ret = 0; |
260 | 0 | return ret; |
261 | 0 | } |
262 | | |
263 | | static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, |
264 | | size_t keylen, const unsigned char *iv, |
265 | | size_t ivlen, const OSSL_PARAM params[]) |
266 | 0 | { |
267 | 0 | int ret; |
268 | | |
269 | | /* The generic function checks for ossl_prov_is_running() */ |
270 | 0 | ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); |
271 | 0 | if (ret && iv != NULL) { |
272 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
273 | 0 | PROV_CHACHA20_POLY1305_CTX *cctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
274 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
275 | |
|
276 | 0 | hw->initiv(ctx); |
277 | 0 | cctx->iv_state = IV_STATE_BUFFERED; |
278 | 0 | } |
279 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
280 | 0 | ret = 0; |
281 | 0 | return ret; |
282 | 0 | } |
283 | | |
284 | | static int chacha20_poly1305_cipher(void *vctx, unsigned char *out, |
285 | | size_t *outl, size_t outsize, |
286 | | const unsigned char *in, size_t inl) |
287 | 0 | { |
288 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
289 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
290 | |
|
291 | 0 | if (!ossl_prov_is_running()) |
292 | 0 | return 0; |
293 | | |
294 | 0 | if (outsize < inl) { |
295 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
296 | 0 | return 0; |
297 | 0 | } |
298 | | |
299 | 0 | if (!hw->aead_cipher(ctx, out, outl, in, inl)) |
300 | 0 | return 0; |
301 | | |
302 | 0 | return 1; |
303 | 0 | } |
304 | | |
305 | | static int chacha20_poly1305_update(void *vctx, unsigned char *out, |
306 | | size_t *outl, size_t outsize, |
307 | | const unsigned char *in, size_t inl) |
308 | 0 | { |
309 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
310 | |
|
311 | 0 | if (ctx->iv_state == IV_STATE_FINISHED) |
312 | 0 | return 0; |
313 | | |
314 | | /* |
315 | | * a zero-length update is a nop, ALWAYS SUCCEED via early exit |
316 | | * NB: ONLY EVP_Cipher() / final produce or check the tag |
317 | | */ |
318 | 0 | if (inl == 0) { |
319 | 0 | *outl = 0; |
320 | 0 | return 1; |
321 | 0 | } |
322 | | |
323 | 0 | if (in == NULL) { |
324 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); |
325 | 0 | return 0; |
326 | 0 | } |
327 | | |
328 | 0 | return chacha20_poly1305_cipher(vctx, out, outl, outsize, in, inl); |
329 | 0 | } |
330 | | |
331 | | static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl, |
332 | | size_t outsize) |
333 | 0 | { |
334 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
335 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; |
336 | |
|
337 | 0 | if (!ossl_prov_is_running()) |
338 | 0 | return 0; |
339 | | |
340 | | /* The tag must be set before actually decrypting data */ |
341 | 0 | if (!ctx->base.enc && ctx->tag_len == 0) { |
342 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
343 | 0 | return 0; |
344 | 0 | } |
345 | | |
346 | 0 | if (hw->aead_cipher((PROV_CIPHER_CTX *)ctx, out, outl, NULL, 0) <= 0) |
347 | 0 | return 0; |
348 | | |
349 | 0 | *outl = 0; |
350 | | |
351 | | /* Don't reuse the IV */ |
352 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
353 | 0 | return 1; |
354 | 0 | } |
355 | | |
356 | | /* ossl_chacha20_ossl_poly1305_functions */ |
357 | | const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = { |
358 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx }, |
359 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx }, |
360 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx }, |
361 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit }, |
362 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit }, |
363 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update }, |
364 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final }, |
365 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher }, |
366 | | { OSSL_FUNC_CIPHER_GET_PARAMS, |
367 | | (void (*)(void))chacha20_poly1305_get_params }, |
368 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, |
369 | | (void (*)(void))chacha20_poly1305_gettable_params }, |
370 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, |
371 | | (void (*)(void))chacha20_poly1305_get_ctx_params }, |
372 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, |
373 | | (void (*)(void))chacha20_poly1305_gettable_ctx_params }, |
374 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, |
375 | | (void (*)(void))chacha20_poly1305_set_ctx_params }, |
376 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, |
377 | | (void (*)(void))chacha20_poly1305_settable_ctx_params }, |
378 | | OSSL_DISPATCH_END |
379 | | }; |