/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 | | static struct chacha20_poly1305_get_ctx_params_st |
125 | 0 | chacha20_poly1305_get_ctx_params_decoder(const OSSL_PARAM params[]) { |
126 | 0 | struct chacha20_poly1305_get_ctx_params_st r; |
127 | 0 | const OSSL_PARAM *p; |
128 | 0 | const char *s; |
129 | |
|
130 | 0 | memset(&r, 0, sizeof(r)); |
131 | 0 | for (p = params; (s = p->key) != NULL; p++) |
132 | 0 | switch(s[0]) { |
133 | 0 | default: |
134 | 0 | break; |
135 | 0 | case 'i': |
136 | 0 | if (ossl_likely(r.ivlen == NULL && strcmp("vlen", s + 1) == 0)) |
137 | 0 | r.ivlen = (OSSL_PARAM *)p; |
138 | 0 | break; |
139 | 0 | case 'k': |
140 | 0 | if (ossl_likely(r.keylen == NULL && strcmp("eylen", s + 1) == 0)) |
141 | 0 | r.keylen = (OSSL_PARAM *)p; |
142 | 0 | break; |
143 | 0 | case 't': |
144 | 0 | switch(s[1]) { |
145 | 0 | default: |
146 | 0 | break; |
147 | 0 | case 'a': |
148 | 0 | switch(s[2]) { |
149 | 0 | default: |
150 | 0 | break; |
151 | 0 | case 'g': |
152 | 0 | switch(s[3]) { |
153 | 0 | default: |
154 | 0 | break; |
155 | 0 | case 'l': |
156 | 0 | if (ossl_likely(r.taglen == NULL && strcmp("en", s + 4) == 0)) |
157 | 0 | r.taglen = (OSSL_PARAM *)p; |
158 | 0 | break; |
159 | 0 | case '\0': |
160 | 0 | r.tag = ossl_likely(r.tag == NULL) ? (OSSL_PARAM *)p : r.tag; |
161 | 0 | } |
162 | 0 | } |
163 | 0 | break; |
164 | 0 | case 'l': |
165 | 0 | if (ossl_likely(r.pad == NULL && strcmp("saadpad", s + 2) == 0)) |
166 | 0 | r.pad = (OSSL_PARAM *)p; |
167 | 0 | } |
168 | 0 | } |
169 | 0 | return r; |
170 | 0 | } |
171 | | #endif |
172 | | /* End of machine generated */ |
173 | | |
174 | | static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
175 | 0 | { |
176 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
177 | 0 | struct chacha20_poly1305_get_ctx_params_st p; |
178 | |
|
179 | 0 | p = chacha20_poly1305_get_ctx_params_decoder(params); |
180 | |
|
181 | 0 | if (p.ivlen != NULL |
182 | 0 | && !OSSL_PARAM_set_size_t(p.ivlen, CHACHA20_POLY1305_IVLEN)) { |
183 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
184 | 0 | return 0; |
185 | 0 | } |
186 | | |
187 | 0 | if (p.keylen != NULL |
188 | 0 | && !OSSL_PARAM_set_size_t(p.keylen, CHACHA20_POLY1305_KEYLEN)) { |
189 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
190 | 0 | return 0; |
191 | 0 | } |
192 | | |
193 | 0 | if (p.taglen != NULL |
194 | 0 | && !OSSL_PARAM_set_size_t(p.taglen, ctx->tag_len)) { |
195 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
196 | 0 | return 0; |
197 | 0 | } |
198 | | |
199 | 0 | if (p.pad != NULL |
200 | 0 | && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
201 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
202 | 0 | return 0; |
203 | 0 | } |
204 | | |
205 | 0 | if (p.tag != NULL) { |
206 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
207 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
208 | 0 | return 0; |
209 | 0 | } |
210 | 0 | if (!ctx->base.enc) { |
211 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); |
212 | 0 | return 0; |
213 | 0 | } |
214 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
215 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
216 | 0 | return 0; |
217 | 0 | } |
218 | 0 | memcpy(p.tag->data, ctx->tag, p.tag->data_size); |
219 | 0 | } |
220 | 0 | return 1; |
221 | 0 | } |
222 | | |
223 | | static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params |
224 | | (ossl_unused void *cctx, ossl_unused void *provctx) |
225 | 1 | { |
226 | 1 | return chacha20_poly1305_get_ctx_params_list; |
227 | 1 | } |
228 | | |
229 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
230 | | #ifndef chacha20_poly1305_set_ctx_params_list |
231 | | static const OSSL_PARAM chacha20_poly1305_set_ctx_params_list[] = { |
232 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
233 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
234 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
235 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), |
236 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0), |
237 | | OSSL_PARAM_END |
238 | | }; |
239 | | #endif |
240 | | |
241 | | #ifndef chacha20_poly1305_set_ctx_params_st |
242 | | struct chacha20_poly1305_set_ctx_params_st { |
243 | | OSSL_PARAM *aad; |
244 | | OSSL_PARAM *fixed; |
245 | | OSSL_PARAM *ivlen; |
246 | | OSSL_PARAM *keylen; |
247 | | OSSL_PARAM *tag; |
248 | | }; |
249 | | #endif |
250 | | |
251 | | #ifndef chacha20_poly1305_set_ctx_params_decoder |
252 | | static struct chacha20_poly1305_set_ctx_params_st |
253 | 0 | chacha20_poly1305_set_ctx_params_decoder(const OSSL_PARAM params[]) { |
254 | 0 | struct chacha20_poly1305_set_ctx_params_st r; |
255 | 0 | const OSSL_PARAM *p; |
256 | 0 | const char *s; |
257 | |
|
258 | 0 | memset(&r, 0, sizeof(r)); |
259 | 0 | for (p = params; (s = p->key) != NULL; p++) |
260 | 0 | switch(s[0]) { |
261 | 0 | default: |
262 | 0 | break; |
263 | 0 | case 'i': |
264 | 0 | if (ossl_likely(r.ivlen == NULL && strcmp("vlen", s + 1) == 0)) |
265 | 0 | r.ivlen = (OSSL_PARAM *)p; |
266 | 0 | break; |
267 | 0 | case 'k': |
268 | 0 | if (ossl_likely(r.keylen == NULL && strcmp("eylen", s + 1) == 0)) |
269 | 0 | r.keylen = (OSSL_PARAM *)p; |
270 | 0 | break; |
271 | 0 | case 't': |
272 | 0 | switch(s[1]) { |
273 | 0 | default: |
274 | 0 | break; |
275 | 0 | case 'a': |
276 | 0 | if (ossl_likely(r.tag == NULL && strcmp("g", s + 2) == 0)) |
277 | 0 | r.tag = (OSSL_PARAM *)p; |
278 | 0 | break; |
279 | 0 | case 'l': |
280 | 0 | switch(s[2]) { |
281 | 0 | default: |
282 | 0 | break; |
283 | 0 | case 's': |
284 | 0 | switch(s[3]) { |
285 | 0 | default: |
286 | 0 | break; |
287 | 0 | case 'a': |
288 | 0 | if (ossl_likely(r.aad == NULL && strcmp("ad", s + 4) == 0)) |
289 | 0 | r.aad = (OSSL_PARAM *)p; |
290 | 0 | break; |
291 | 0 | case 'i': |
292 | 0 | if (ossl_likely(r.fixed == NULL && strcmp("vfixed", s + 4) == 0)) |
293 | 0 | r.fixed = (OSSL_PARAM *)p; |
294 | 0 | } |
295 | 0 | } |
296 | 0 | } |
297 | 0 | } |
298 | 0 | return r; |
299 | 0 | } |
300 | | #endif |
301 | | /* End of machine generated */ |
302 | | |
303 | | static const OSSL_PARAM *chacha20_poly1305_settable_ctx_params( |
304 | | ossl_unused void *cctx, ossl_unused void *provctx |
305 | | ) |
306 | 0 | { |
307 | 0 | return chacha20_poly1305_set_ctx_params_list; |
308 | 0 | } |
309 | | |
310 | | static int chacha20_poly1305_set_ctx_params(void *vctx, |
311 | | const OSSL_PARAM params[]) |
312 | 0 | { |
313 | 0 | size_t len; |
314 | 0 | PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; |
315 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
316 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; |
317 | 0 | struct chacha20_poly1305_set_ctx_params_st p; |
318 | |
|
319 | 0 | if (ossl_param_is_empty(params)) |
320 | 0 | return 1; |
321 | | |
322 | 0 | p = chacha20_poly1305_set_ctx_params_decoder(params); |
323 | | |
324 | |
|
325 | 0 | if (p.keylen != NULL) { |
326 | 0 | if (!OSSL_PARAM_get_size_t(p.keylen, &len)) { |
327 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
328 | 0 | return 0; |
329 | 0 | } |
330 | 0 | if (len != CHACHA20_POLY1305_KEYLEN) { |
331 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
332 | 0 | return 0; |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | 0 | if (p.ivlen != NULL) { |
337 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &len)) { |
338 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
339 | 0 | return 0; |
340 | 0 | } |
341 | 0 | if (len != CHACHA20_POLY1305_MAX_IVLEN) { |
342 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
343 | 0 | return 0; |
344 | 0 | } |
345 | 0 | } |
346 | | |
347 | 0 | if (p.tag != NULL) { |
348 | 0 | if (p.tag->data_type != OSSL_PARAM_OCTET_STRING) { |
349 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
350 | 0 | return 0; |
351 | 0 | } |
352 | 0 | if (p.tag->data_size == 0 || p.tag->data_size > POLY1305_BLOCK_SIZE) { |
353 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); |
354 | 0 | return 0; |
355 | 0 | } |
356 | 0 | if (p.tag->data != NULL) { |
357 | 0 | if (ctx->base.enc) { |
358 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); |
359 | 0 | return 0; |
360 | 0 | } |
361 | 0 | memcpy(ctx->tag, p.tag->data, p.tag->data_size); |
362 | 0 | } |
363 | 0 | ctx->tag_len = p.tag->data_size; |
364 | 0 | } |
365 | | |
366 | 0 | if (p.aad != NULL) { |
367 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
368 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
369 | 0 | return 0; |
370 | 0 | } |
371 | 0 | len = hw->tls_init(&ctx->base, p.aad->data, p.aad->data_size); |
372 | 0 | if (len == 0) { |
373 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
374 | 0 | return 0; |
375 | 0 | } |
376 | 0 | ctx->tls_aad_pad_sz = len; |
377 | 0 | } |
378 | | |
379 | 0 | if (p.fixed != NULL) { |
380 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
381 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
382 | 0 | return 0; |
383 | 0 | } |
384 | 0 | if (hw->tls_iv_set_fixed(&ctx->base, p.fixed->data, |
385 | 0 | p.fixed->data_size) == 0) { |
386 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
387 | 0 | return 0; |
388 | 0 | } |
389 | 0 | } |
390 | 0 | return 1; |
391 | 0 | } |
392 | | |
393 | | static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, |
394 | | size_t keylen, const unsigned char *iv, |
395 | | size_t ivlen, const OSSL_PARAM params[]) |
396 | 0 | { |
397 | 0 | int ret; |
398 | | |
399 | | /* The generic function checks for ossl_prov_is_running() */ |
400 | 0 | ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); |
401 | 0 | if (ret && iv != NULL) { |
402 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
403 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
404 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
405 | |
|
406 | 0 | hw->initiv(ctx); |
407 | 0 | } |
408 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
409 | 0 | ret = 0; |
410 | 0 | return ret; |
411 | 0 | } |
412 | | |
413 | | static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, |
414 | | size_t keylen, const unsigned char *iv, |
415 | | size_t ivlen, const OSSL_PARAM params[]) |
416 | 0 | { |
417 | 0 | int ret; |
418 | | |
419 | | /* The generic function checks for ossl_prov_is_running() */ |
420 | 0 | ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); |
421 | 0 | if (ret && iv != NULL) { |
422 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
423 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
424 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
425 | |
|
426 | 0 | hw->initiv(ctx); |
427 | 0 | } |
428 | 0 | if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) |
429 | 0 | ret = 0; |
430 | 0 | return ret; |
431 | 0 | } |
432 | | |
433 | | static int chacha20_poly1305_cipher(void *vctx, unsigned char *out, |
434 | | size_t *outl, size_t outsize, |
435 | | const unsigned char *in, size_t inl) |
436 | 0 | { |
437 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
438 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
439 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
440 | |
|
441 | 0 | if (!ossl_prov_is_running()) |
442 | 0 | return 0; |
443 | | |
444 | 0 | if (inl == 0) { |
445 | 0 | *outl = 0; |
446 | 0 | return 1; |
447 | 0 | } |
448 | | |
449 | 0 | if (outsize < inl) { |
450 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
451 | 0 | return 0; |
452 | 0 | } |
453 | | |
454 | 0 | if (!hw->aead_cipher(ctx, out, outl, in, inl)) |
455 | 0 | return 0; |
456 | | |
457 | 0 | return 1; |
458 | 0 | } |
459 | | |
460 | | static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl, |
461 | | size_t outsize) |
462 | 0 | { |
463 | 0 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; |
464 | 0 | PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = |
465 | 0 | (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; |
466 | |
|
467 | 0 | if (!ossl_prov_is_running()) |
468 | 0 | return 0; |
469 | | |
470 | 0 | if (hw->aead_cipher(ctx, out, outl, NULL, 0) <= 0) |
471 | 0 | return 0; |
472 | | |
473 | 0 | *outl = 0; |
474 | 0 | return 1; |
475 | 0 | } |
476 | | |
477 | | /* ossl_chacha20_ossl_poly1305_functions */ |
478 | | const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = { |
479 | | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx }, |
480 | | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx }, |
481 | | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx }, |
482 | | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit }, |
483 | | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit }, |
484 | | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update }, |
485 | | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final }, |
486 | | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher }, |
487 | | { OSSL_FUNC_CIPHER_GET_PARAMS, |
488 | | (void (*)(void))chacha20_poly1305_get_params }, |
489 | | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, |
490 | | (void (*)(void))chacha20_poly1305_gettable_params }, |
491 | | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, |
492 | | (void (*)(void))chacha20_poly1305_get_ctx_params }, |
493 | | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, |
494 | | (void (*)(void))chacha20_poly1305_gettable_ctx_params }, |
495 | | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, |
496 | | (void (*)(void))chacha20_poly1305_set_ctx_params }, |
497 | | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, |
498 | | (void (*)(void))chacha20_poly1305_settable_ctx_params }, |
499 | | OSSL_DISPATCH_END |
500 | | }; |
501 | | |