2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Dispatch functions for chacha20 cipher */
12 #include "cipher_chacha20.h"
13 #include "prov/implementations.h"
14 #include "prov/providercommonerr.h"
16 #define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
17 #define CHACHA20_BLKLEN (1)
18 #define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
19 /* TODO(3.0) Figure out what flags are required */
20 #define CHACHA20_FLAGS (EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT)
22 static OSSL_OP_cipher_newctx_fn chacha20_newctx;
23 static OSSL_OP_cipher_freectx_fn chacha20_freectx;
24 static OSSL_OP_cipher_get_params_fn chacha20_get_params;
25 static OSSL_OP_cipher_get_ctx_params_fn chacha20_get_ctx_params;
26 static OSSL_OP_cipher_set_ctx_params_fn chacha20_set_ctx_params;
27 static OSSL_OP_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
28 static OSSL_OP_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
29 #define chacha20_cipher cipher_generic_cipher
30 #define chacha20_update cipher_generic_stream_update
31 #define chacha20_final cipher_generic_stream_final
32 #define chacha20_gettable_params cipher_generic_gettable_params
34 void chacha20_initctx(PROV_CHACHA20_CTX *ctx)
36 cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
40 PROV_CIPHER_HW_chacha20(CHACHA20_KEYLEN * 8),
44 static void *chacha20_newctx(void *provctx)
46 PROV_CHACHA20_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
49 chacha20_initctx(ctx);
53 static void chacha20_freectx(void *vctx)
55 PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
58 OPENSSL_clear_free(ctx, sizeof(*ctx));
62 static int chacha20_get_params(OSSL_PARAM params[])
64 return cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
70 static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
74 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
75 if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) {
76 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
79 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
80 if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) {
81 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
88 static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
89 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
90 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
93 const OSSL_PARAM *chacha20_gettable_ctx_params(void)
95 return chacha20_known_gettable_ctx_params;
98 static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
103 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
105 if (!OSSL_PARAM_get_size_t(p, &len)) {
106 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
109 if (len != CHACHA20_KEYLEN) {
110 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
114 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
116 if (!OSSL_PARAM_get_size_t(p, &len)) {
117 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
120 if (len != CHACHA20_IVLEN) {
121 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
128 static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
129 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
130 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
133 const OSSL_PARAM *chacha20_settable_ctx_params(void)
135 return chacha20_known_settable_ctx_params;
138 int chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
139 const unsigned char *iv, size_t ivlen)
143 ret= cipher_generic_einit(vctx, key, keylen, iv, ivlen);
144 if (ret && iv != NULL) {
145 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
146 PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
153 int chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
154 const unsigned char *iv, size_t ivlen)
158 ret= cipher_generic_dinit(vctx, key, keylen, iv, ivlen);
159 if (ret && iv != NULL) {
160 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
161 PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
168 /* chacha20_functions */
169 const OSSL_DISPATCH chacha20_functions[] = {
170 { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
171 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
172 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_einit },
173 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_dinit },
174 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
175 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
176 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
177 { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
178 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,(void (*)(void))chacha20_gettable_params },
179 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
180 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
181 (void (*)(void))chacha20_gettable_ctx_params },
182 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
183 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
184 (void (*)(void))chacha20_settable_ctx_params },