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