1 | /* |
2 | * Copyright 2019 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 "cipher_chacha20.h" |
13 | #include "prov/implementations.h" |
14 | #include "prov/providercommonerr.h" |
15 | |
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) |
21 | |
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 |
33 | |
34 | void chacha20_initctx(PROV_CHACHA20_CTX *ctx) |
35 | { |
36 | cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8, |
37 | CHACHA20_BLKLEN * 8, |
38 | CHACHA20_IVLEN * 8, |
39 | 0, CHACHA20_FLAGS, |
40 | PROV_CIPHER_HW_chacha20(CHACHA20_KEYLEN * 8), |
41 | NULL); |
42 | } |
43 | |
44 | static void *chacha20_newctx(void *provctx) |
45 | { |
46 | PROV_CHACHA20_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
47 | |
48 | if (ctx != NULL) |
49 | chacha20_initctx(ctx); |
50 | return ctx; |
51 | } |
52 | |
53 | static void chacha20_freectx(void *vctx) |
54 | { |
55 | PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx; |
56 | |
57 | if (ctx != NULL) { |
58 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
59 | } |
60 | } |
61 | |
62 | static int chacha20_get_params(OSSL_PARAM params[]) |
63 | { |
64 | return cipher_generic_get_params(params, 0, CHACHA20_FLAGS, |
65 | CHACHA20_KEYLEN * 8, |
66 | CHACHA20_BLKLEN * 8, |
67 | CHACHA20_IVLEN * 8); |
68 | } |
69 | |
70 | static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
71 | { |
72 | OSSL_PARAM *p; |
73 | |
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); |
77 | return 0; |
78 | } |
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); |
82 | return 0; |
83 | } |
84 | |
85 | return 1; |
86 | } |
87 | |
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), |
91 | OSSL_PARAM_END |
92 | }; |
93 | const OSSL_PARAM *chacha20_gettable_ctx_params(void) |
94 | { |
95 | return chacha20_known_gettable_ctx_params; |
96 | } |
97 | |
98 | static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
99 | { |
100 | const OSSL_PARAM *p; |
101 | size_t len; |
102 | |
103 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); |
104 | if (p != NULL) { |
105 | if (!OSSL_PARAM_get_size_t(p, &len)) { |
106 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
107 | return 0; |
108 | } |
109 | if (len != CHACHA20_KEYLEN) { |
110 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
111 | return 0; |
112 | } |
113 | } |
114 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); |
115 | if (p != NULL) { |
116 | if (!OSSL_PARAM_get_size_t(p, &len)) { |
117 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
118 | return 0; |
119 | } |
120 | if (len != CHACHA20_IVLEN) { |
121 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
122 | return 0; |
123 | } |
124 | } |
125 | return 1; |
126 | } |
127 | |
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), |
131 | OSSL_PARAM_END |
132 | }; |
133 | const OSSL_PARAM *chacha20_settable_ctx_params(void) |
134 | { |
135 | return chacha20_known_settable_ctx_params; |
136 | } |
137 | |
138 | int chacha20_einit(void *vctx, const unsigned char *key, size_t keylen, |
139 | const unsigned char *iv, size_t ivlen) |
140 | { |
141 | int ret; |
142 | |
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; |
147 | |
148 | hw->initiv(ctx); |
149 | } |
150 | return ret; |
151 | } |
152 | |
153 | int chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen, |
154 | const unsigned char *iv, size_t ivlen) |
155 | { |
156 | int ret; |
157 | |
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; |
162 | |
163 | hw->initiv(ctx); |
164 | } |
165 | return ret; |
166 | } |
167 | |
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 }, |
185 | { 0, NULL } |
186 | }; |
187 | |
188 | |