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