xref: /freebsd-src/crypto/openssl/providers/implementations/ciphers/cipher_chacha20.c (revision 6f1af0d7d2af54b339b5212434cd6d4fda628d80)
1b077aed3SPierre Pronchery /*
2*6f1af0d7SPierre Pronchery  * Copyright 2019-2023 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 cipher */
11b077aed3SPierre Pronchery 
12b077aed3SPierre Pronchery #include <openssl/proverr.h>
13b077aed3SPierre Pronchery #include "cipher_chacha20.h"
14b077aed3SPierre Pronchery #include "prov/implementations.h"
15b077aed3SPierre Pronchery #include "prov/providercommon.h"
16b077aed3SPierre Pronchery 
17b077aed3SPierre Pronchery #define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
18b077aed3SPierre Pronchery #define CHACHA20_BLKLEN (1)
19b077aed3SPierre Pronchery #define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
20b077aed3SPierre Pronchery #define CHACHA20_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
21b077aed3SPierre Pronchery 
22b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn chacha20_newctx;
23b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn chacha20_freectx;
24*6f1af0d7SPierre Pronchery static OSSL_FUNC_cipher_dupctx_fn chacha20_dupctx;
25b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn chacha20_get_params;
26b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params;
27b077aed3SPierre Pronchery static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params;
28b077aed3SPierre Pronchery static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
29b077aed3SPierre Pronchery static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
30b077aed3SPierre Pronchery #define chacha20_cipher ossl_cipher_generic_cipher
31b077aed3SPierre Pronchery #define chacha20_update ossl_cipher_generic_stream_update
32b077aed3SPierre Pronchery #define chacha20_final ossl_cipher_generic_stream_final
33b077aed3SPierre Pronchery #define chacha20_gettable_params ossl_cipher_generic_gettable_params
34b077aed3SPierre Pronchery 
ossl_chacha20_initctx(PROV_CHACHA20_CTX * ctx)35b077aed3SPierre Pronchery void ossl_chacha20_initctx(PROV_CHACHA20_CTX *ctx)
36b077aed3SPierre Pronchery {
37b077aed3SPierre Pronchery     ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
38b077aed3SPierre Pronchery                                 CHACHA20_BLKLEN * 8,
39b077aed3SPierre Pronchery                                 CHACHA20_IVLEN * 8,
40b077aed3SPierre Pronchery                                 0, CHACHA20_FLAGS,
41b077aed3SPierre Pronchery                                 ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8),
42b077aed3SPierre Pronchery                                 NULL);
43b077aed3SPierre Pronchery }
44b077aed3SPierre Pronchery 
chacha20_newctx(void * provctx)45b077aed3SPierre Pronchery static void *chacha20_newctx(void *provctx)
46b077aed3SPierre Pronchery {
47b077aed3SPierre Pronchery     PROV_CHACHA20_CTX *ctx;
48b077aed3SPierre Pronchery 
49b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
50b077aed3SPierre Pronchery         return NULL;
51b077aed3SPierre Pronchery 
52b077aed3SPierre Pronchery     ctx = OPENSSL_zalloc(sizeof(*ctx));
53b077aed3SPierre Pronchery     if (ctx != NULL)
54b077aed3SPierre Pronchery         ossl_chacha20_initctx(ctx);
55b077aed3SPierre Pronchery     return ctx;
56b077aed3SPierre Pronchery }
57b077aed3SPierre Pronchery 
chacha20_freectx(void * vctx)58b077aed3SPierre Pronchery static void chacha20_freectx(void *vctx)
59b077aed3SPierre Pronchery {
60b077aed3SPierre Pronchery     PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
61b077aed3SPierre Pronchery 
62b077aed3SPierre Pronchery     if (ctx != NULL) {
63b077aed3SPierre Pronchery         ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
64b077aed3SPierre Pronchery         OPENSSL_clear_free(ctx, sizeof(*ctx));
65b077aed3SPierre Pronchery     }
66b077aed3SPierre Pronchery }
67b077aed3SPierre Pronchery 
chacha20_dupctx(void * vctx)68*6f1af0d7SPierre Pronchery static void *chacha20_dupctx(void *vctx)
69*6f1af0d7SPierre Pronchery {
70*6f1af0d7SPierre Pronchery     PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
71*6f1af0d7SPierre Pronchery     PROV_CHACHA20_CTX *dupctx = NULL;
72*6f1af0d7SPierre Pronchery 
73*6f1af0d7SPierre Pronchery     if (ctx != NULL) {
74*6f1af0d7SPierre Pronchery         dupctx = OPENSSL_memdup(ctx, sizeof(*dupctx));
75*6f1af0d7SPierre Pronchery         if (dupctx != NULL && dupctx->base.tlsmac != NULL && dupctx->base.alloced) {
76*6f1af0d7SPierre Pronchery             dupctx->base.tlsmac = OPENSSL_memdup(dupctx->base.tlsmac,
77*6f1af0d7SPierre Pronchery                                                  dupctx->base.tlsmacsize);
78*6f1af0d7SPierre Pronchery             if (dupctx->base.tlsmac == NULL) {
79*6f1af0d7SPierre Pronchery                 OPENSSL_free(dupctx);
80*6f1af0d7SPierre Pronchery                 dupctx = NULL;
81*6f1af0d7SPierre Pronchery             }
82*6f1af0d7SPierre Pronchery         }
83*6f1af0d7SPierre Pronchery     }
84*6f1af0d7SPierre Pronchery     return dupctx;
85*6f1af0d7SPierre Pronchery }
86*6f1af0d7SPierre Pronchery 
chacha20_get_params(OSSL_PARAM params[])87b077aed3SPierre Pronchery static int chacha20_get_params(OSSL_PARAM params[])
88b077aed3SPierre Pronchery {
89b077aed3SPierre Pronchery     return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
90b077aed3SPierre Pronchery                                           CHACHA20_KEYLEN * 8,
91b077aed3SPierre Pronchery                                           CHACHA20_BLKLEN * 8,
92b077aed3SPierre Pronchery                                           CHACHA20_IVLEN * 8);
93b077aed3SPierre Pronchery }
94b077aed3SPierre Pronchery 
chacha20_get_ctx_params(void * vctx,OSSL_PARAM params[])95b077aed3SPierre Pronchery static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
96b077aed3SPierre Pronchery {
97b077aed3SPierre Pronchery     OSSL_PARAM *p;
98b077aed3SPierre Pronchery 
99b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
100b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) {
101b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
102b077aed3SPierre Pronchery         return 0;
103b077aed3SPierre Pronchery     }
104b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
105b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) {
106b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
107b077aed3SPierre Pronchery         return 0;
108b077aed3SPierre Pronchery     }
109b077aed3SPierre Pronchery 
110b077aed3SPierre Pronchery     return 1;
111b077aed3SPierre Pronchery }
112b077aed3SPierre Pronchery 
113b077aed3SPierre Pronchery static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
114b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
115b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
116b077aed3SPierre Pronchery     OSSL_PARAM_END
117b077aed3SPierre Pronchery };
chacha20_gettable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)118b077aed3SPierre Pronchery const OSSL_PARAM *chacha20_gettable_ctx_params(ossl_unused void *cctx,
119b077aed3SPierre Pronchery                                                ossl_unused void *provctx)
120b077aed3SPierre Pronchery {
121b077aed3SPierre Pronchery     return chacha20_known_gettable_ctx_params;
122b077aed3SPierre Pronchery }
123b077aed3SPierre Pronchery 
chacha20_set_ctx_params(void * vctx,const OSSL_PARAM params[])124b077aed3SPierre Pronchery static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
125b077aed3SPierre Pronchery {
126b077aed3SPierre Pronchery     const OSSL_PARAM *p;
127b077aed3SPierre Pronchery     size_t len;
128b077aed3SPierre Pronchery 
129b077aed3SPierre Pronchery     if (params == NULL)
130b077aed3SPierre Pronchery         return 1;
131b077aed3SPierre Pronchery 
132b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
133b077aed3SPierre Pronchery     if (p != NULL) {
134b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_size_t(p, &len)) {
135b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
136b077aed3SPierre Pronchery             return 0;
137b077aed3SPierre Pronchery         }
138b077aed3SPierre Pronchery         if (len != CHACHA20_KEYLEN) {
139b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
140b077aed3SPierre Pronchery             return 0;
141b077aed3SPierre Pronchery         }
142b077aed3SPierre Pronchery     }
143b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
144b077aed3SPierre Pronchery     if (p != NULL) {
145b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_size_t(p, &len)) {
146b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
147b077aed3SPierre Pronchery             return 0;
148b077aed3SPierre Pronchery         }
149b077aed3SPierre Pronchery         if (len != CHACHA20_IVLEN) {
150b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
151b077aed3SPierre Pronchery             return 0;
152b077aed3SPierre Pronchery         }
153b077aed3SPierre Pronchery     }
154b077aed3SPierre Pronchery     return 1;
155b077aed3SPierre Pronchery }
156b077aed3SPierre Pronchery 
157b077aed3SPierre Pronchery static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
158b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
159b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
160b077aed3SPierre Pronchery     OSSL_PARAM_END
161b077aed3SPierre Pronchery };
chacha20_settable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)162b077aed3SPierre Pronchery const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx,
163b077aed3SPierre Pronchery                                                ossl_unused void *provctx)
164b077aed3SPierre Pronchery {
165b077aed3SPierre Pronchery     return chacha20_known_settable_ctx_params;
166b077aed3SPierre Pronchery }
167b077aed3SPierre Pronchery 
ossl_chacha20_einit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])168b077aed3SPierre Pronchery int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
169b077aed3SPierre Pronchery                         const unsigned char *iv, size_t ivlen,
170b077aed3SPierre Pronchery                         const OSSL_PARAM params[])
171b077aed3SPierre Pronchery {
172b077aed3SPierre Pronchery     int ret;
173b077aed3SPierre Pronchery 
174b077aed3SPierre Pronchery     /* The generic function checks for ossl_prov_is_running() */
175b077aed3SPierre Pronchery     ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
176b077aed3SPierre Pronchery     if (ret && iv != NULL) {
177b077aed3SPierre Pronchery         PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
178b077aed3SPierre Pronchery         PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
179b077aed3SPierre Pronchery 
180b077aed3SPierre Pronchery         hw->initiv(ctx);
181b077aed3SPierre Pronchery     }
182b077aed3SPierre Pronchery     if (ret && !chacha20_set_ctx_params(vctx, params))
183b077aed3SPierre Pronchery         ret = 0;
184b077aed3SPierre Pronchery     return ret;
185b077aed3SPierre Pronchery }
186b077aed3SPierre Pronchery 
ossl_chacha20_dinit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])187b077aed3SPierre Pronchery int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
188b077aed3SPierre Pronchery                         const unsigned char *iv, size_t ivlen,
189b077aed3SPierre Pronchery                         const OSSL_PARAM params[])
190b077aed3SPierre Pronchery {
191b077aed3SPierre Pronchery     int ret;
192b077aed3SPierre Pronchery 
193b077aed3SPierre Pronchery     /* The generic function checks for ossl_prov_is_running() */
194b077aed3SPierre Pronchery     ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
195b077aed3SPierre Pronchery     if (ret && iv != NULL) {
196b077aed3SPierre Pronchery         PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
197b077aed3SPierre Pronchery         PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
198b077aed3SPierre Pronchery 
199b077aed3SPierre Pronchery         hw->initiv(ctx);
200b077aed3SPierre Pronchery     }
201b077aed3SPierre Pronchery     if (ret && !chacha20_set_ctx_params(vctx, params))
202b077aed3SPierre Pronchery         ret = 0;
203b077aed3SPierre Pronchery     return ret;
204b077aed3SPierre Pronchery }
205b077aed3SPierre Pronchery 
206b077aed3SPierre Pronchery /* ossl_chacha20_functions */
207b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_chacha20_functions[] = {
208b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
209b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
210*6f1af0d7SPierre Pronchery     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_dupctx },
211b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit },
212b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit },
213b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
214b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
215b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
216b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
217b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,(void (*)(void))chacha20_gettable_params },
218b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
219b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
220b077aed3SPierre Pronchery         (void (*)(void))chacha20_gettable_ctx_params },
221b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
222b077aed3SPierre Pronchery     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
223b077aed3SPierre Pronchery         (void (*)(void))chacha20_settable_ctx_params },
224b077aed3SPierre Pronchery     { 0, NULL }
225b077aed3SPierre Pronchery };
226b077aed3SPierre Pronchery 
227