/src/openssl/providers/implementations/ciphers/cipher_chacha20_poly1305.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2023 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_poly1305 cipher */ |
12 | | |
13 | | #include <string.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include "cipher_chacha20_poly1305.h" |
16 | | #include "prov/implementations.h" |
17 | | #include "prov/providercommon.h" |
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 | | #define chacha20_poly1305_update chacha20_poly1305_cipher |
40 | | |
41 | | static void *chacha20_poly1305_newctx(void *provctx) |
42 | 0 | { |
43 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx; |
44 | |
|
45 | 0 | if (!ossl_prov_is_running()) |
46 | 0 | return NULL; |
47 | | |
48 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
49 | 0 | if (ctx != NULL) { |
50 | 0 | ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8, |
51 | 0 | CHACHA20_POLY1305_BLKLEN * 8, |
52 | 0 | CHACHA20_POLY1305_IVLEN * 8, |
53 | 0 | CHACHA20_POLY1305_MODE, |
54 | 0 | CHACHA20_POLY1305_FLAGS, |
55 | 0 | ossl_prov_cipher_hw_chacha20_poly1305( |
56 | 0 | CHACHA20_POLY1305_KEYLEN * 8), |
57 | 0 | NULL); |
58 | 0 | ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH; |
59 | 0 | ossl_chacha20_initctx(&ctx->chacha); |
60 | 0 | } |
61 | 0 | return ctx; |
62 | 0 | } |
63 | | |
64 | | static void *chacha20_poly1305_dupctx(void *provctx) |
65 | 0 | { |
66 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = provctx; |
67 | 0 | PROV_CHACHA20_POLY1305_CTX *dctx = NULL; |
68 | |
|
69 | 0 | if (ctx == NULL) |
70 | 0 | return NULL; |
71 | 0 | dctx = OPENSSL_memdup(ctx, sizeof(*ctx)); |
72 | 0 | if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) { |
73 | 0 | dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac, |
74 | 0 | dctx->base.tlsmacsize); |
75 | 0 | if (dctx->base.tlsmac == NULL) { |
76 | 0 | OPENSSL_free(dctx); |
77 | 0 | dctx = NULL; |
78 | 0 | } |
79 | 0 | } |
80 | 0 | return dctx; |
81 | 0 | } |
82 | | |
83 | | static void chacha20_poly1305_freectx(void *vctx) |
84 | 0 | { |
85 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
86 | |
|
87 | 0 | if (ctx != NULL) { |
88 | 0 | ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
89 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | static int chacha20_poly1305_get_params(OSSL_PARAM params[]) |
94 | 1 | { |
95 | 1 | return ossl_cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS, |
96 | 1 | CHACHA20_POLY1305_KEYLEN * 8, |
97 | 1 | CHACHA20_POLY1305_BLKLEN * 8, |
98 | 1 | CHACHA20_POLY1305_IVLEN * 8); |
99 | 1 | } |
100 | | |
101 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
102 | | #ifndef chacha20_poly1305_get_ctx_params_list |
103 | | static const OSSL_PARAM chacha20_poly1305_get_ctx_params_list[] = { |
104 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
105 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
106 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL), |
107 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
108 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), |
109 | | OSSL_PARAM_END |
110 | | }; |
111 | | #endif |
112 | | |
113 | | #ifndef chacha20_poly1305_get_ctx_params_st |
114 | | struct chacha20_poly1305_get_ctx_params_st { |
115 | | OSSL_PARAM *ivlen; |
116 | | OSSL_PARAM *keylen; |
117 | | OSSL_PARAM *pad; |
118 | | OSSL_PARAM *tag; |
119 | | OSSL_PARAM *taglen; |
120 | | }; |
121 | | #endif |
122 | | |
123 | | #ifndef chacha20_poly1305_get_ctx_params_decoder |
124 | 0 | static struct chacha20_poly1305_get_ctx_params_st chacha20_poly1305_get_ctx_params_decoder(const OSSL_PARAM params[]) { |
125 | 0 | struct chacha20_poly1305_get_ctx_params_st r; |
126 | 0 | const OSSL_PARAM *p; |
127 | 0 | const char *s; |
128 | |
|
129 | 0 | memset(&r, 0, sizeof(r)); |
130 | 0 | for (p = params; (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(r.ivlen == NULL && strcmp("vlen", s + 1) == 0)) |
136 | 0 | r.ivlen = (OSSL_PARAM *)p; |
137 | 0 | break; |
138 | 0 | case 'k': |
139 | 0 | if (ossl_likely(r.keylen == NULL && strcmp("eylen", s + 1) == 0)) |
140 | 0 | r.keylen = (OSSL_PARAM *)p; |
141 | 0 | break; |
142 | 0 | case 't': |
143 | 0 | switch(s[1]) { |
144 | 0 | default: |
145 | 0 | break; |
146 | 0 | case 'a': |
147 | 0 | switch(s[2]) { |
148 | 0 | default: |
149 | 0 | break; |
150 | 0 | case 'g': |
151 | 0 | switch(s[3]) { |
152 | 0 | default: |
153 | 0 | break; |
154 | 0 | case 'l': |
155 | 0 | if (ossl_likely(r.taglen == NULL && strcmp("en", s + 4) == 0)) |
156 | 0 | r.taglen = (OSSL_PARAM *)p; |
157 | 0 | break; |
158 | 0 | case '\0': |
159 | 0 | r.tag = ossl_likely(r.tag == NULL) ? (OSSL_PARAM *)p : r.tag; |
160 | 0 | } |
161 | 0 | } |
162 | 0 | break; |
163 | 0 | case 'l': |
164 | 0 | if (ossl_likely(r.pad == NULL && strcmp("saadpad", s + 2) == 0)) |
165 | 0 | r.pad = (OSSL_PARAM *)p; |
166 | 0 | } |
167 | 0 | } |
168 | 0 | return r; |
169 | 0 | } |
170 | | #endif |
171 | | /* End of machine generated */ |
172 | | |
173 | | static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
174 | 0 | { |
175 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
176 | 0 | struct chacha20_poly1305_get_ctx_params_st p; |
177 | |
|
178 | 0 | p = chacha20_poly1305_get_ctx_params_decoder(params); |
179 | |
|
180 | 0 | if (p.ivlen != NULL |
181 | 0 | && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_POLY1305_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 |
187 | 0 | && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_POLY1305_KEYLEN)) { |
188 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | 0 | if (p.taglen != NULL |
193 | 0 | && !OSSL_PARAM_set_size_t(p.taglen, ctx->tag_len)) { |
194 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
195 | 0 | return 0; |
196 | 0 | } |
197 | | |
198 | 0 | if (p.pad != NULL |
199 | 0 | && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
200 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
201 | 0 | return 0; |
202 | 0 | } |
203 | | |
204 | 0 | if (p.tag != NULL) { |
205 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
206 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
207 | 0 | return 0; |
208 | 0 | } |
209 | 0 | if (!ctx->base.enc) { |
210 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
214 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
215 | 0 | return 0; |
216 | 0 | } |
217 | 0 | memcpy(p.tag->data, ctx->tag, p.tag->data_size); |
218 | 0 | } |
219 | 0 | return 1; |
220 | 0 | } |
221 | | |
222 | | static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params |
223 | | (ossl_unused void *cctx, ossl_unused void *provctx) |
224 | 1 | { |
225 | 1 | return chacha20_poly1305_get_ctx_params_list; |
226 | 1 | } |
227 | | |
228 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
229 | | #ifndef chacha20_poly1305_set_ctx_params_list |
230 | | static const OSSL_PARAM chacha20_poly1305_set_ctx_params_list[] = { |
231 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
232 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
233 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
234 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), |
235 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0), |
236 | | OSSL_PARAM_END |
237 | | }; |
238 | | #endif |
239 | | |
240 | | #ifndef chacha20_poly1305_set_ctx_params_st |
241 | | struct chacha20_poly1305_set_ctx_params_st { |
242 | | OSSL_PARAM *aad; |
243 | | OSSL_PARAM *fixed; |
244 | | OSSL_PARAM *ivlen; |
245 | | OSSL_PARAM *keylen; |
246 | | OSSL_PARAM *tag; |
247 | | }; |
248 | | #endif |
249 | | |
250 | | #ifndef chacha20_poly1305_set_ctx_params_decoder |
251 | 0 | static struct chacha20_poly1305_set_ctx_params_st chacha20_poly1305_set_ctx_params_decoder(const OSSL_PARAM params[]) { |
252 | 0 | struct chacha20_poly1305_set_ctx_params_st r; |
253 | 0 | const OSSL_PARAM *p; |
254 | 0 | const char *s; |
255 | |
|
256 | 0 | memset(&r, 0, sizeof(r)); |
257 | 0 | for (p = params; (s = p->key) != NULL; p++) |
258 | 0 | switch(s[0]) { |
259 | 0 | default: |
260 | 0 | break; |
261 | 0 | case 'i': |
262 | 0 | if (ossl_likely(r.ivlen == NULL && strcmp("vlen", s + 1) == 0)) |
263 | 0 | r.ivlen = (OSSL_PARAM *)p; |
264 | 0 | break; |
265 | 0 | case 'k': |
266 | 0 | if (ossl_likely(r.keylen == NULL && strcmp("eylen", s + 1) == 0)) |
267 | 0 | r.keylen = (OSSL_PARAM *)p; |
268 | 0 | break; |
269 | 0 | case 't': |
270 | 0 | switch(s[1]) { |
271 | 0 | default: |
272 | 0 | break; |
273 | 0 | case 'a': |
274 | 0 | if (ossl_likely(r.tag == NULL && strcmp("g", s + 2) == 0)) |
275 | 0 | r.tag = (OSSL_PARAM *)p; |
276 | 0 | break; |
277 | 0 | case 'l': |
278 | 0 | switch(s[2]) { |
279 | 0 | default: |
280 | 0 | break; |
281 | 0 | case 's': |
282 | 0 | switch(s[3]) { |
283 | 0 | default: |
284 | 0 | break; |
285 | 0 | case 'a': |
286 | 0 | if (ossl_likely(r.aad == NULL && strcmp("ad", s + 4) == 0)) |
287 | 0 | r.aad = (OSSL_PARAM *)p; |
288 | 0 | break; |
289 | 0 | case 'i': |
290 | 0 | if (ossl_likely(r.fixed == NULL && strcmp("vfixed", s + 4) == 0)) |
291 | 0 | r.fixed = (OSSL_PARAM *)p; |
292 | 0 | } |
293 | 0 | } |
294 | 0 | } |
295 | 0 | } |
296 | 0 | return r; |
297 | 0 | } |
298 | | #endif |
299 | | /* End of machine generated */ |
300 | | |
301 | | static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params( |
302 | | ossl_unused void *cctx, ossl_unused void *provctx |
303 | | ) |
304 | 0 | { |
305 | 0 | return chacha20_poly1305_set_ctx_params_list; |
306 | 0 | } |
307 | | |
308 | | static int chacha20_poly1305_set_ctx_params(void *vctx, |
309 | | const OSSL_PARAM params[]) |
310 | 0 | { |
311 | 0 | size_t len; |
312 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
313 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
314 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; |
315 | 0 | struct chacha20_poly1305_set_ctx_params_st p; |
316 | |
|
317 | 0 | if (ossl_param_is_empty(params)) |
318 | 0 | return 1; |
319 | | |
320 | 0 | p = chacha20_poly1305_set_ctx_params_decoder(params); |
321 | | |
322 | |
|
323 | 0 | if (p.keylen != NULL) { |
324 | 0 | if (!OSSL_PARAM_get_size_t(p.keylen, &len)) { |
325 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
326 | 0 | return 0; |
327 | 0 | } |
328 | 0 | if (len != CHACHA20_POLY1305_KEYLEN) { |
329 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
330 | 0 | return 0; |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | 0 | if (p.ivlen != NULL) { |
335 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) { |
336 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
337 | 0 | return 0; |
338 | 0 | } |
339 | 0 | if (len != CHACHA20_POLY1305_MAX_IVLEN) { |
340 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
341 | 0 | return 0; |
342 | 0 | } |
343 | 0 | } |
344 | | |
345 | 0 | if (p.tag != NULL) { |
346 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
347 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
348 | 0 | return 0; |
349 | 0 | } |
350 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
351 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
352 | 0 | return 0; |
353 | 0 | } |
354 | 0 | if (p.tag->data != NULL) { |
355 | 0 | if (ctx->base.enc) { |
356 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
357 | 0 | return 0; |
358 | 0 | } |
359 | 0 | memcpy(ctx->tag, p.tag->data, p.tag->data_size); |
360 | 0 | } |
361 | 0 | ctx->tag_len = p.tag->data_size; |
362 | 0 | } |
363 | | |
364 | 0 | if (p.aad != NULL) { |
365 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
366 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
367 | 0 | return 0; |
368 | 0 | } |
369 | 0 | len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size); |
370 | 0 | if (len == 0) { |
371 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
372 | 0 | return 0; |
373 | 0 | } |
374 | 0 | ctx->tls_aad_pad_sz = len; |
375 | 0 | } |
376 | | |
377 | 0 | if (p.fixed != NULL) { |
378 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
379 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
380 | 0 | return 0; |
381 | 0 | } |
382 | 0 | if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data, |
383 | 0 | p.fixed->data_size) == 0) { |
384 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
385 | 0 | return 0; |
386 | 0 | } |
387 | 0 | } |
388 | 0 | return 1; |
389 | 0 | } |
390 | | |
391 | | static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, |
392 | | size_t keylen, const unsigned char *iv, |
393 | | size_t ivlen, const OSSL_PARAM params[]) |
394 | 0 | { |
395 | 0 | int ret; |
396 | | |
397 | | /* The generic function checks for ossl_prov_is_running() */ |
398 | 0 | ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); |
399 | 0 | if (ret && iv != NULL) { |
400 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
401 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
402 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
403 | |
|
404 | 0 | hw->initiv(ctx); |
405 | 0 | } |
406 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
407 | 0 | ret = 0; |
408 | 0 | return ret; |
409 | 0 | } |
410 | | |
411 | | static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, |
412 | | size_t keylen, const unsigned char *iv, |
413 | | size_t ivlen, const OSSL_PARAM params[]) |
414 | 0 | { |
415 | 0 | int ret; |
416 | | |
417 | | /* The generic function checks for ossl_prov_is_running() */ |
418 | 0 | ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); |
419 | 0 | if (ret && iv != NULL) { |
420 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
421 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
422 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
423 | |
|
424 | 0 | hw->initiv(ctx); |
425 | 0 | } |
426 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
427 | 0 | ret = 0; |
428 | 0 | return ret; |
429 | 0 | } |
430 | | |
431 | | static int chacha20_poly1305_cipher(void *vctx, unsigned char *out, |
432 | | size_t *outl, size_t outsize, |
433 | | const unsigned char *in, size_t inl) |
434 | 0 | { |
435 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
436 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
437 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
438 | |
|
439 | 0 | if (!ossl_prov_is_running()) |
440 | 0 | return 0; |
441 | | |
442 | 0 | if (inl == 0) { |
443 | 0 | *outl = 0; |
444 | 0 | return 1; |
445 | 0 | } |
446 | | |
447 | 0 | if (outsize < inl) { |
448 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
449 | 0 | return 0; |
450 | 0 | } |
451 | | |
452 | 0 | if (!hw->aead_cipher(ctx, out, outl, in, inl)) |
453 | 0 | return 0; |
454 | | |
455 | 0 | return 1; |
456 | 0 | } |
457 | | |
458 | | static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl, |
459 | | size_t outsize) |
460 | 0 | { |
461 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
462 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
463 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
464 | |
|
465 | 0 | if (!ossl_prov_is_running()) |
466 | 0 | return 0; |
467 | | |
468 | 0 | if (hw->aead_cipher(ctx, out, outl, NULL, 0) <= 0) |
469 | 0 | return 0; |
470 | | |
471 | 0 | *outl = 0; |
472 | 0 | return 1; |
473 | 0 | } |
474 | | |
475 | | /* ossl_chacha20_ossl_poly1305_functions */ |
476 | | const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = { |
477 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx }, |
478 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx }, |
479 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx }, |
480 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit }, |
481 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit }, |
482 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update }, |
483 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final }, |
484 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher }, |
485 | | { OSSL_FUNC_CIPHER_GET_PARAMS, |
486 | | (void (*)(void))chacha20_poly1305_get_params }, |
487 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, |
488 | | (void (*)(void))chacha20_poly1305_gettable_params }, |
489 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, |
490 | | (void (*)(void))chacha20_poly1305_get_ctx_params }, |
491 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, |
492 | | (void (*)(void))chacha20_poly1305_gettable_ctx_params }, |
493 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, |
494 | | (void (*)(void))chacha20_poly1305_set_ctx_params }, |
495 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, |
496 | | (void (*)(void))chacha20_poly1305_settable_ctx_params }, |
497 | | OSSL_DISPATCH_END |
498 | | }; |
499 | | |