1b077aed3SPierre Pronchery /*
2*e0c4386eSCy Schubert * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery */
9b077aed3SPierre Pronchery
10b077aed3SPierre Pronchery /* Dispatch functions for chacha20_poly1305 cipher */
11b077aed3SPierre Pronchery
12b077aed3SPierre Pronchery #include <openssl/proverr.h>
13b077aed3SPierre Pronchery #include "cipher_chacha20_poly1305.h"
14b077aed3SPierre Pronchery #include "prov/implementations.h"
15b077aed3SPierre Pronchery #include "prov/providercommon.h"
16b077aed3SPierre Pronchery
17b077aed3SPierre Pronchery #define CHACHA20_POLY1305_KEYLEN CHACHA_KEY_SIZE
18b077aed3SPierre Pronchery #define CHACHA20_POLY1305_BLKLEN 1
19b077aed3SPierre Pronchery #define CHACHA20_POLY1305_MAX_IVLEN 12
20b077aed3SPierre Pronchery #define CHACHA20_POLY1305_MODE 0
21b077aed3SPierre Pronchery #define CHACHA20_POLY1305_FLAGS (PROV_CIPHER_FLAG_AEAD \
22b077aed3SPierre Pronchery | PROV_CIPHER_FLAG_CUSTOM_IV)
23b077aed3SPierre Pronchery
24b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn chacha20_poly1305_newctx;
25b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn chacha20_poly1305_freectx;
26*e0c4386eSCy Schubert static OSSL_FUNC_cipher_dupctx_fn chacha20_poly1305_dupctx;
27b077aed3SPierre Pronchery static OSSL_FUNC_cipher_encrypt_init_fn chacha20_poly1305_einit;
28b077aed3SPierre Pronchery static OSSL_FUNC_cipher_decrypt_init_fn chacha20_poly1305_dinit;
29b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn chacha20_poly1305_get_params;
30b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_poly1305_get_ctx_params;
31b077aed3SPierre Pronchery static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_poly1305_set_ctx_params;
32b077aed3SPierre Pronchery static OSSL_FUNC_cipher_cipher_fn chacha20_poly1305_cipher;
33b077aed3SPierre Pronchery static OSSL_FUNC_cipher_final_fn chacha20_poly1305_final;
34b077aed3SPierre Pronchery static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_poly1305_gettable_ctx_params;
35b077aed3SPierre Pronchery #define chacha20_poly1305_settable_ctx_params ossl_cipher_aead_settable_ctx_params
36b077aed3SPierre Pronchery #define chacha20_poly1305_gettable_params ossl_cipher_generic_gettable_params
37b077aed3SPierre Pronchery #define chacha20_poly1305_update chacha20_poly1305_cipher
38b077aed3SPierre Pronchery
chacha20_poly1305_newctx(void * provctx)39b077aed3SPierre Pronchery static void *chacha20_poly1305_newctx(void *provctx)
40b077aed3SPierre Pronchery {
41b077aed3SPierre Pronchery PROV_CHACHA20_POLY1305_CTX *ctx;
42b077aed3SPierre Pronchery
43b077aed3SPierre Pronchery if (!ossl_prov_is_running())
44b077aed3SPierre Pronchery return NULL;
45b077aed3SPierre Pronchery
46b077aed3SPierre Pronchery ctx = OPENSSL_zalloc(sizeof(*ctx));
47b077aed3SPierre Pronchery if (ctx != NULL) {
48b077aed3SPierre Pronchery ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8,
49b077aed3SPierre Pronchery CHACHA20_POLY1305_BLKLEN * 8,
50b077aed3SPierre Pronchery CHACHA20_POLY1305_IVLEN * 8,
51b077aed3SPierre Pronchery CHACHA20_POLY1305_MODE,
52b077aed3SPierre Pronchery CHACHA20_POLY1305_FLAGS,
53b077aed3SPierre Pronchery ossl_prov_cipher_hw_chacha20_poly1305(
54b077aed3SPierre Pronchery CHACHA20_POLY1305_KEYLEN * 8),
55b077aed3SPierre Pronchery NULL);
56b077aed3SPierre Pronchery ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
57b077aed3SPierre Pronchery ossl_chacha20_initctx(&ctx->chacha);
58b077aed3SPierre Pronchery }
59b077aed3SPierre Pronchery return ctx;
60b077aed3SPierre Pronchery }
61b077aed3SPierre Pronchery
chacha20_poly1305_dupctx(void * provctx)62*e0c4386eSCy Schubert static void *chacha20_poly1305_dupctx(void *provctx)
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert PROV_CHACHA20_POLY1305_CTX *ctx = provctx;
65*e0c4386eSCy Schubert PROV_CHACHA20_POLY1305_CTX *dctx = NULL;
66*e0c4386eSCy Schubert
67*e0c4386eSCy Schubert if (ctx == NULL)
68*e0c4386eSCy Schubert return NULL;
69*e0c4386eSCy Schubert dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
70*e0c4386eSCy Schubert if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) {
71*e0c4386eSCy Schubert dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac,
72*e0c4386eSCy Schubert dctx->base.tlsmacsize);
73*e0c4386eSCy Schubert if (dctx->base.tlsmac == NULL) {
74*e0c4386eSCy Schubert OPENSSL_free(dctx);
75*e0c4386eSCy Schubert dctx = NULL;
76*e0c4386eSCy Schubert }
77*e0c4386eSCy Schubert }
78*e0c4386eSCy Schubert return dctx;
79*e0c4386eSCy Schubert }
80*e0c4386eSCy Schubert
chacha20_poly1305_freectx(void * vctx)81b077aed3SPierre Pronchery static void chacha20_poly1305_freectx(void *vctx)
82b077aed3SPierre Pronchery {
83b077aed3SPierre Pronchery PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
84b077aed3SPierre Pronchery
85b077aed3SPierre Pronchery if (ctx != NULL) {
86b077aed3SPierre Pronchery ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
87b077aed3SPierre Pronchery OPENSSL_clear_free(ctx, sizeof(*ctx));
88b077aed3SPierre Pronchery }
89b077aed3SPierre Pronchery }
90b077aed3SPierre Pronchery
chacha20_poly1305_get_params(OSSL_PARAM params[])91b077aed3SPierre Pronchery static int chacha20_poly1305_get_params(OSSL_PARAM params[])
92b077aed3SPierre Pronchery {
93b077aed3SPierre Pronchery return ossl_cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS,
94b077aed3SPierre Pronchery CHACHA20_POLY1305_KEYLEN * 8,
95b077aed3SPierre Pronchery CHACHA20_POLY1305_BLKLEN * 8,
96b077aed3SPierre Pronchery CHACHA20_POLY1305_IVLEN * 8);
97b077aed3SPierre Pronchery }
98b077aed3SPierre Pronchery
chacha20_poly1305_get_ctx_params(void * vctx,OSSL_PARAM params[])99b077aed3SPierre Pronchery static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[])
100b077aed3SPierre Pronchery {
101b077aed3SPierre Pronchery PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
102b077aed3SPierre Pronchery OSSL_PARAM *p;
103b077aed3SPierre Pronchery
104b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
105b077aed3SPierre Pronchery if (p != NULL) {
106b077aed3SPierre Pronchery if (!OSSL_PARAM_set_size_t(p, CHACHA20_POLY1305_IVLEN)) {
107b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
108b077aed3SPierre Pronchery return 0;
109b077aed3SPierre Pronchery }
110b077aed3SPierre Pronchery }
111b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
112b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_POLY1305_KEYLEN)) {
113b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
114b077aed3SPierre Pronchery return 0;
115b077aed3SPierre Pronchery }
116b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
117b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tag_len)) {
118b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
119b077aed3SPierre Pronchery return 0;
120b077aed3SPierre Pronchery }
121b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
122b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
123b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
124b077aed3SPierre Pronchery return 0;
125b077aed3SPierre Pronchery }
126b077aed3SPierre Pronchery
127b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
128b077aed3SPierre Pronchery if (p != NULL) {
129b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
130b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
131b077aed3SPierre Pronchery return 0;
132b077aed3SPierre Pronchery }
133b077aed3SPierre Pronchery if (!ctx->base.enc) {
134b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
135b077aed3SPierre Pronchery return 0;
136b077aed3SPierre Pronchery }
137b077aed3SPierre Pronchery if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) {
138b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
139b077aed3SPierre Pronchery return 0;
140b077aed3SPierre Pronchery }
141b077aed3SPierre Pronchery memcpy(p->data, ctx->tag, p->data_size);
142b077aed3SPierre Pronchery }
143b077aed3SPierre Pronchery
144b077aed3SPierre Pronchery return 1;
145b077aed3SPierre Pronchery }
146b077aed3SPierre Pronchery
147b077aed3SPierre Pronchery static const OSSL_PARAM chacha20_poly1305_known_gettable_ctx_params[] = {
148b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
149b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
150b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
151b077aed3SPierre Pronchery OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
152b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
153b077aed3SPierre Pronchery OSSL_PARAM_END
154b077aed3SPierre Pronchery };
chacha20_poly1305_gettable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)155b077aed3SPierre Pronchery static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params
156b077aed3SPierre Pronchery (ossl_unused void *cctx, ossl_unused void *provctx)
157b077aed3SPierre Pronchery {
158b077aed3SPierre Pronchery return chacha20_poly1305_known_gettable_ctx_params;
159b077aed3SPierre Pronchery }
160b077aed3SPierre Pronchery
chacha20_poly1305_set_ctx_params(void * vctx,const OSSL_PARAM params[])161b077aed3SPierre Pronchery static int chacha20_poly1305_set_ctx_params(void *vctx,
162b077aed3SPierre Pronchery const OSSL_PARAM params[])
163b077aed3SPierre Pronchery {
164b077aed3SPierre Pronchery const OSSL_PARAM *p;
165b077aed3SPierre Pronchery size_t len;
166b077aed3SPierre Pronchery PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
167b077aed3SPierre Pronchery PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
168b077aed3SPierre Pronchery (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
169b077aed3SPierre Pronchery
170b077aed3SPierre Pronchery if (params == NULL)
171b077aed3SPierre Pronchery return 1;
172b077aed3SPierre Pronchery
173b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
174b077aed3SPierre Pronchery if (p != NULL) {
175b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &len)) {
176b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
177b077aed3SPierre Pronchery return 0;
178b077aed3SPierre Pronchery }
179b077aed3SPierre Pronchery if (len != CHACHA20_POLY1305_KEYLEN) {
180b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
181b077aed3SPierre Pronchery return 0;
182b077aed3SPierre Pronchery }
183b077aed3SPierre Pronchery }
184b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
185b077aed3SPierre Pronchery if (p != NULL) {
186b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &len)) {
187b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
188b077aed3SPierre Pronchery return 0;
189b077aed3SPierre Pronchery }
190b077aed3SPierre Pronchery if (len != CHACHA20_POLY1305_MAX_IVLEN) {
191b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
192b077aed3SPierre Pronchery return 0;
193b077aed3SPierre Pronchery }
194b077aed3SPierre Pronchery }
195b077aed3SPierre Pronchery
196b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
197b077aed3SPierre Pronchery if (p != NULL) {
198b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
199b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
200b077aed3SPierre Pronchery return 0;
201b077aed3SPierre Pronchery }
202b077aed3SPierre Pronchery if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) {
203b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
204b077aed3SPierre Pronchery return 0;
205b077aed3SPierre Pronchery }
206b077aed3SPierre Pronchery if (p->data != NULL) {
207b077aed3SPierre Pronchery if (ctx->base.enc) {
208b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
209b077aed3SPierre Pronchery return 0;
210b077aed3SPierre Pronchery }
211b077aed3SPierre Pronchery memcpy(ctx->tag, p->data, p->data_size);
212b077aed3SPierre Pronchery }
213b077aed3SPierre Pronchery ctx->tag_len = p->data_size;
214b077aed3SPierre Pronchery }
215b077aed3SPierre Pronchery
216b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
217b077aed3SPierre Pronchery if (p != NULL) {
218b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
219b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
220b077aed3SPierre Pronchery return 0;
221b077aed3SPierre Pronchery }
222b077aed3SPierre Pronchery len = hw->tls_init(&ctx->base, p->data, p->data_size);
223b077aed3SPierre Pronchery if (len == 0) {
224b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
225b077aed3SPierre Pronchery return 0;
226b077aed3SPierre Pronchery }
227b077aed3SPierre Pronchery ctx->tls_aad_pad_sz = len;
228b077aed3SPierre Pronchery }
229b077aed3SPierre Pronchery
230b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
231b077aed3SPierre Pronchery if (p != NULL) {
232b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
233b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
234b077aed3SPierre Pronchery return 0;
235b077aed3SPierre Pronchery }
236b077aed3SPierre Pronchery if (hw->tls_iv_set_fixed(&ctx->base, p->data, p->data_size) == 0) {
237b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
238b077aed3SPierre Pronchery return 0;
239b077aed3SPierre Pronchery }
240b077aed3SPierre Pronchery }
241b077aed3SPierre Pronchery /* ignore OSSL_CIPHER_PARAM_AEAD_MAC_KEY */
242b077aed3SPierre Pronchery return 1;
243b077aed3SPierre Pronchery }
244b077aed3SPierre Pronchery
chacha20_poly1305_einit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])245b077aed3SPierre Pronchery static int chacha20_poly1305_einit(void *vctx, const unsigned char *key,
246b077aed3SPierre Pronchery size_t keylen, const unsigned char *iv,
247b077aed3SPierre Pronchery size_t ivlen, const OSSL_PARAM params[])
248b077aed3SPierre Pronchery {
249b077aed3SPierre Pronchery int ret;
250b077aed3SPierre Pronchery
251b077aed3SPierre Pronchery /* The generic function checks for ossl_prov_is_running() */
252b077aed3SPierre Pronchery ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
253b077aed3SPierre Pronchery if (ret && iv != NULL) {
254b077aed3SPierre Pronchery PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
255b077aed3SPierre Pronchery PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
256b077aed3SPierre Pronchery (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
257b077aed3SPierre Pronchery
258b077aed3SPierre Pronchery hw->initiv(ctx);
259b077aed3SPierre Pronchery }
260b077aed3SPierre Pronchery if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
261b077aed3SPierre Pronchery ret = 0;
262b077aed3SPierre Pronchery return ret;
263b077aed3SPierre Pronchery }
264b077aed3SPierre Pronchery
chacha20_poly1305_dinit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])265b077aed3SPierre Pronchery static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key,
266b077aed3SPierre Pronchery size_t keylen, const unsigned char *iv,
267b077aed3SPierre Pronchery size_t ivlen, const OSSL_PARAM params[])
268b077aed3SPierre Pronchery {
269b077aed3SPierre Pronchery int ret;
270b077aed3SPierre Pronchery
271b077aed3SPierre Pronchery /* The generic function checks for ossl_prov_is_running() */
272b077aed3SPierre Pronchery ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
273b077aed3SPierre Pronchery if (ret && iv != NULL) {
274b077aed3SPierre Pronchery PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
275b077aed3SPierre Pronchery PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
276b077aed3SPierre Pronchery (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
277b077aed3SPierre Pronchery
278b077aed3SPierre Pronchery hw->initiv(ctx);
279b077aed3SPierre Pronchery }
280b077aed3SPierre Pronchery if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
281b077aed3SPierre Pronchery ret = 0;
282b077aed3SPierre Pronchery return ret;
283b077aed3SPierre Pronchery }
284b077aed3SPierre Pronchery
chacha20_poly1305_cipher(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)285b077aed3SPierre Pronchery static int chacha20_poly1305_cipher(void *vctx, unsigned char *out,
286b077aed3SPierre Pronchery size_t *outl, size_t outsize,
287b077aed3SPierre Pronchery const unsigned char *in, size_t inl)
288b077aed3SPierre Pronchery {
289b077aed3SPierre Pronchery PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
290b077aed3SPierre Pronchery PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
291b077aed3SPierre Pronchery (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
292b077aed3SPierre Pronchery
293b077aed3SPierre Pronchery if (!ossl_prov_is_running())
294b077aed3SPierre Pronchery return 0;
295b077aed3SPierre Pronchery
296b077aed3SPierre Pronchery if (inl == 0) {
297b077aed3SPierre Pronchery *outl = 0;
298b077aed3SPierre Pronchery return 1;
299b077aed3SPierre Pronchery }
300b077aed3SPierre Pronchery
301b077aed3SPierre Pronchery if (outsize < inl) {
302b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
303b077aed3SPierre Pronchery return 0;
304b077aed3SPierre Pronchery }
305b077aed3SPierre Pronchery
306b077aed3SPierre Pronchery if (!hw->aead_cipher(ctx, out, outl, in, inl))
307b077aed3SPierre Pronchery return 0;
308b077aed3SPierre Pronchery
309b077aed3SPierre Pronchery return 1;
310b077aed3SPierre Pronchery }
311b077aed3SPierre Pronchery
chacha20_poly1305_final(void * vctx,unsigned char * out,size_t * outl,size_t outsize)312b077aed3SPierre Pronchery static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl,
313b077aed3SPierre Pronchery size_t outsize)
314b077aed3SPierre Pronchery {
315b077aed3SPierre Pronchery PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
316b077aed3SPierre Pronchery PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
317b077aed3SPierre Pronchery (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
318b077aed3SPierre Pronchery
319b077aed3SPierre Pronchery if (!ossl_prov_is_running())
320b077aed3SPierre Pronchery return 0;
321b077aed3SPierre Pronchery
322b077aed3SPierre Pronchery if (hw->aead_cipher(ctx, out, outl, NULL, 0) <= 0)
323b077aed3SPierre Pronchery return 0;
324b077aed3SPierre Pronchery
325b077aed3SPierre Pronchery *outl = 0;
326b077aed3SPierre Pronchery return 1;
327b077aed3SPierre Pronchery }
328b077aed3SPierre Pronchery
329b077aed3SPierre Pronchery /* ossl_chacha20_ossl_poly1305_functions */
330b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = {
331b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx },
332b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx },
333*e0c4386eSCy Schubert { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx },
334b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit },
335b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit },
336b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update },
337b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final },
338b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher },
339b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_PARAMS,
340b077aed3SPierre Pronchery (void (*)(void))chacha20_poly1305_get_params },
341b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
342b077aed3SPierre Pronchery (void (*)(void))chacha20_poly1305_gettable_params },
343b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
344b077aed3SPierre Pronchery (void (*)(void))chacha20_poly1305_get_ctx_params },
345b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
346b077aed3SPierre Pronchery (void (*)(void))chacha20_poly1305_gettable_ctx_params },
347b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
348b077aed3SPierre Pronchery (void (*)(void))chacha20_poly1305_set_ctx_params },
349b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
350b077aed3SPierre Pronchery (void (*)(void))chacha20_poly1305_settable_ctx_params },
351b077aed3SPierre Pronchery { 0, NULL }
352b077aed3SPierre Pronchery };
353b077aed3SPierre Pronchery
354