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