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