/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 | 0 | #define CHACHA20_POLY1305_MODE 0 |
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, 0, CHACHA20_POLY1305_FLAGS, |
92 | 1 | CHACHA20_POLY1305_KEYLEN * 8, |
93 | 1 | CHACHA20_POLY1305_BLKLEN * 8, |
94 | 1 | CHACHA20_POLY1305_IVLEN * 8); |
95 | 1 | } |
96 | | |
97 | | static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
98 | 0 | { |
99 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
100 | 0 | struct chacha20_poly1305_get_ctx_params_st p; |
101 | |
|
102 | 0 | if (ctx == NULL || !chacha20_poly1305_get_ctx_params_decoder(params, &p)) |
103 | 0 | return 0; |
104 | | |
105 | 0 | if (p.ivlen != NULL |
106 | 0 | && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_POLY1305_IVLEN)) { |
107 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
108 | 0 | return 0; |
109 | 0 | } |
110 | | |
111 | 0 | if (p.keylen != NULL |
112 | 0 | && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_POLY1305_KEYLEN)) { |
113 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | | /* tag_len == 0 means no tag set yet, report the default */ |
118 | 0 | if (p.taglen != NULL |
119 | 0 | && !OSSL_PARAM_set_size_t(p.taglen, |
120 | 0 | ctx->tag_len == 0 ? POLY1305_BLOCK_SIZE : ctx->tag_len)) { |
121 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | 0 | if (p.pad != NULL |
126 | 0 | && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
127 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
128 | 0 | return 0; |
129 | 0 | } |
130 | | |
131 | 0 | if (p.tag != NULL) { |
132 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
133 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
134 | 0 | return 0; |
135 | 0 | } |
136 | 0 | if (!ctx->base.enc) { |
137 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
138 | 0 | return 0; |
139 | 0 | } |
140 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
141 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
142 | 0 | return 0; |
143 | 0 | } |
144 | 0 | memcpy(p.tag->data, ctx->tag, p.tag->data_size); |
145 | 0 | } |
146 | 0 | return 1; |
147 | 0 | } |
148 | | |
149 | | static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params(ossl_unused void *cctx, ossl_unused void *provctx) |
150 | 1 | { |
151 | 1 | return chacha20_poly1305_get_ctx_params_list; |
152 | 1 | } |
153 | | |
154 | | static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params( |
155 | | ossl_unused void *cctx, ossl_unused void *provctx) |
156 | 0 | { |
157 | 0 | return chacha20_poly1305_set_ctx_params_list; |
158 | 0 | } |
159 | | |
160 | | static int chacha20_poly1305_set_ctx_params(void *vctx, |
161 | | const OSSL_PARAM params[]) |
162 | 0 | { |
163 | 0 | size_t len; |
164 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
165 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; |
166 | 0 | struct chacha20_poly1305_set_ctx_params_st p; |
167 | |
|
168 | 0 | if (!chacha20_poly1305_set_ctx_params_decoder(params, &p)) |
169 | 0 | return 0; |
170 | | |
171 | 0 | if (p.keylen != NULL) { |
172 | 0 | if (!OSSL_PARAM_get_size_t(p.keylen, &len)) { |
173 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | if (len != CHACHA20_POLY1305_KEYLEN) { |
177 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
178 | 0 | return 0; |
179 | 0 | } |
180 | 0 | } |
181 | | |
182 | 0 | if (p.ivlen != NULL) { |
183 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) { |
184 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
185 | 0 | return 0; |
186 | 0 | } |
187 | 0 | if (len != CHACHA20_POLY1305_MAX_IVLEN) { |
188 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
189 | 0 | return 0; |
190 | 0 | } |
191 | 0 | ctx->iv_state = IV_STATE_UNINITIALISED; |
192 | 0 | } |
193 | | |
194 | 0 | if (p.tag != NULL) { |
195 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
196 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
197 | 0 | return 0; |
198 | 0 | } |
199 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
200 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
201 | 0 | return 0; |
202 | 0 | } |
203 | 0 | if (p.tag->data != NULL) { |
204 | 0 | if (ctx->base.enc) { |
205 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
206 | 0 | return 0; |
207 | 0 | } |
208 | 0 | memcpy(ctx->tag, p.tag->data, p.tag->data_size); |
209 | 0 | } |
210 | 0 | ctx->tag_len = p.tag->data_size; |
211 | 0 | } |
212 | | |
213 | 0 | if (p.aad != NULL) { |
214 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
215 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
216 | 0 | return 0; |
217 | 0 | } |
218 | 0 | len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size); |
219 | 0 | if (len == 0) { |
220 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
221 | 0 | return 0; |
222 | 0 | } |
223 | 0 | ctx->tls_aad_pad_sz = len; |
224 | 0 | } |
225 | | |
226 | 0 | if (p.fixed != NULL) { |
227 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
228 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
229 | 0 | return 0; |
230 | 0 | } |
231 | 0 | if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data, |
232 | 0 | p.fixed->data_size) |
233 | 0 | == 0) { |
234 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
235 | 0 | return 0; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | return 1; |
239 | 0 | } |
240 | | |
241 | | static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, |
242 | | size_t keylen, const unsigned char *iv, |
243 | | size_t ivlen, const OSSL_PARAM params[]) |
244 | 0 | { |
245 | 0 | int ret; |
246 | | |
247 | | /* The generic function checks for ossl_prov_is_running() */ |
248 | 0 | ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); |
249 | 0 | if (ret && iv != NULL) { |
250 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
251 | 0 | PROV_CHACHA20_POLY1305_CTX *cctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
252 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
253 | |
|
254 | 0 | hw->initiv(ctx); |
255 | 0 | cctx->iv_state = IV_STATE_BUFFERED; |
256 | 0 | } |
257 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
258 | 0 | ret = 0; |
259 | 0 | return ret; |
260 | 0 | } |
261 | | |
262 | | static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, |
263 | | size_t keylen, const unsigned char *iv, |
264 | | size_t ivlen, const OSSL_PARAM params[]) |
265 | 0 | { |
266 | 0 | int ret; |
267 | | |
268 | | /* The generic function checks for ossl_prov_is_running() */ |
269 | 0 | ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); |
270 | 0 | if (ret && iv != NULL) { |
271 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
272 | 0 | PROV_CHACHA20_POLY1305_CTX *cctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
273 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
274 | |
|
275 | 0 | hw->initiv(ctx); |
276 | 0 | cctx->iv_state = IV_STATE_BUFFERED; |
277 | 0 | } |
278 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
279 | 0 | ret = 0; |
280 | 0 | return ret; |
281 | 0 | } |
282 | | |
283 | | static int chacha20_poly1305_cipher(void *vctx, unsigned char *out, |
284 | | size_t *outl, size_t outsize, |
285 | | const unsigned char *in, size_t inl) |
286 | 0 | { |
287 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
288 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
289 | |
|
290 | 0 | if (!ossl_prov_is_running()) |
291 | 0 | return 0; |
292 | | |
293 | 0 | if (outsize < inl) { |
294 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | 0 | if (!hw->aead_cipher(ctx, out, outl, in, inl)) |
299 | 0 | return 0; |
300 | | |
301 | 0 | return 1; |
302 | 0 | } |
303 | | |
304 | | static int chacha20_poly1305_update(void *vctx, unsigned char *out, |
305 | | size_t *outl, size_t outsize, |
306 | | const unsigned char *in, size_t inl) |
307 | 0 | { |
308 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
309 | |
|
310 | 0 | if (ctx->iv_state == IV_STATE_FINISHED) |
311 | 0 | return 0; |
312 | | |
313 | | /* |
314 | | * a zero-length update is a nop, ALWAYS SUCCEED via early exit |
315 | | * NB: ONLY EVP_Cipher() / final produce or check the tag |
316 | | */ |
317 | 0 | if (inl == 0) { |
318 | 0 | *outl = 0; |
319 | 0 | return 1; |
320 | 0 | } |
321 | | |
322 | 0 | return chacha20_poly1305_cipher(vctx, out, outl, outsize, in, inl); |
323 | 0 | } |
324 | | |
325 | | static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl, |
326 | | size_t outsize) |
327 | 0 | { |
328 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
329 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; |
330 | |
|
331 | 0 | if (!ossl_prov_is_running()) |
332 | 0 | return 0; |
333 | | |
334 | | /* The tag must be set before actually decrypting data */ |
335 | 0 | if (!ctx->base.enc && ctx->tag_len == 0) { |
336 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | 0 | if (hw->aead_cipher((PROV_CIPHER_CTX *)ctx, out, outl, NULL, 0) <= 0) |
341 | 0 | return 0; |
342 | | |
343 | 0 | *outl = 0; |
344 | | |
345 | | /* Don't reuse the IV */ |
346 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
347 | 0 | return 1; |
348 | 0 | } |
349 | | |
350 | | /* ossl_chacha20_ossl_poly1305_functions */ |
351 | | const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = { |
352 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx }, |
353 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx }, |
354 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx }, |
355 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit }, |
356 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit }, |
357 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update }, |
358 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final }, |
359 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher }, |
360 | | { OSSL_FUNC_CIPHER_GET_PARAMS, |
361 | | (void (*)(void))chacha20_poly1305_get_params }, |
362 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, |
363 | | (void (*)(void))chacha20_poly1305_gettable_params }, |
364 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, |
365 | | (void (*)(void))chacha20_poly1305_get_ctx_params }, |
366 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, |
367 | | (void (*)(void))chacha20_poly1305_gettable_ctx_params }, |
368 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, |
369 | | (void (*)(void))chacha20_poly1305_set_ctx_params }, |
370 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, |
371 | | (void (*)(void))chacha20_poly1305_settable_ctx_params }, |
372 | | OSSL_DISPATCH_END |
373 | | }; |