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 | #include <openssl/params.h> |
11 | #include <openssl/core_numbers.h> |
12 | #include <openssl/core_names.h> |
13 | #include <openssl/evp.h> |
14 | #include "internal/cryptlib.h" |
15 | #include "crypto/modes.h" |
16 | #include "crypto/ciphermode_platform.h" |
17 | |
18 | #define MAXCHUNK ((size_t)1 << (sizeof(long) * 8 - 2)) |
19 | #define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4)) |
20 | |
21 | #define GENERIC_BLOCK_SIZE 16 |
22 | #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */ |
23 | #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */ |
24 | #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */ |
25 | #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */ |
26 | |
27 | #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args |
28 | |
29 | typedef struct prov_cipher_hw_st PROV_CIPHER_HW; |
30 | typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX; |
31 | |
32 | typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out, |
33 | const unsigned char *in, size_t len); |
34 | |
35 | struct prov_cipher_ctx_st { |
36 | block128_f block; |
37 | union { |
38 | cbc128_f cbc; |
39 | ctr128_f ctr; |
40 | ecb128_f ecb; |
41 | } stream; |
42 | |
43 | unsigned int mode; |
44 | size_t keylen; /* key size (in bytes) */ |
45 | size_t ivlen; |
46 | size_t blocksize; |
47 | size_t bufsz; /* Number of bytes in buf */ |
48 | unsigned int pad : 1; /* Whether padding should be used or not */ |
49 | unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */ |
50 | unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */ |
51 | |
52 | /* |
53 | * num contains the number of bytes of |iv| which are valid for modes that |
54 | * manage partial blocks themselves. |
55 | */ |
56 | unsigned int num; |
57 | uint64_t flags; |
58 | |
59 | /* The original value of the iv */ |
60 | unsigned char oiv[GENERIC_BLOCK_SIZE]; |
61 | /* Buffer of partial blocks processed via update calls */ |
62 | unsigned char buf[GENERIC_BLOCK_SIZE]; |
63 | unsigned char iv[GENERIC_BLOCK_SIZE]; |
64 | const PROV_CIPHER_HW *hw; /* hardware specific functions */ |
65 | const void *ks; /* Pointer to algorithm specific key data */ |
66 | OPENSSL_CTX *libctx; |
67 | }; |
68 | |
69 | struct prov_cipher_hw_st { |
70 | int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen); |
71 | PROV_CIPHER_HW_FN *cipher; |
72 | void (*copyctx)(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src); |
73 | }; |
74 | |
75 | OSSL_OP_cipher_encrypt_init_fn cipher_generic_einit; |
76 | OSSL_OP_cipher_decrypt_init_fn cipher_generic_dinit; |
77 | OSSL_OP_cipher_update_fn cipher_generic_block_update; |
78 | OSSL_OP_cipher_final_fn cipher_generic_block_final; |
79 | OSSL_OP_cipher_update_fn cipher_generic_stream_update; |
80 | OSSL_OP_cipher_final_fn cipher_generic_stream_final; |
81 | OSSL_OP_cipher_cipher_fn cipher_generic_cipher; |
82 | OSSL_OP_cipher_get_ctx_params_fn cipher_generic_get_ctx_params; |
83 | OSSL_OP_cipher_set_ctx_params_fn cipher_generic_set_ctx_params; |
84 | OSSL_OP_cipher_gettable_params_fn cipher_generic_gettable_params; |
85 | OSSL_OP_cipher_gettable_ctx_params_fn cipher_generic_gettable_ctx_params; |
86 | OSSL_OP_cipher_settable_ctx_params_fn cipher_generic_settable_ctx_params; |
87 | OSSL_OP_cipher_set_ctx_params_fn cipher_var_keylen_set_ctx_params; |
88 | OSSL_OP_cipher_settable_ctx_params_fn cipher_var_keylen_settable_ctx_params; |
89 | OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params; |
90 | OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params; |
91 | int cipher_generic_get_params(OSSL_PARAM params[], unsigned int md, |
92 | unsigned long flags, |
93 | size_t kbits, size_t blkbits, size_t ivbits); |
94 | void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits, |
95 | size_t ivbits, unsigned int mode, uint64_t flags, |
96 | const PROV_CIPHER_HW *hw, void *provctx); |
97 | |
98 | #define IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,\ |
99 | blkbits, ivbits, typ) \ |
100 | const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \ |
101 | { OSSL_FUNC_CIPHER_NEWCTX, \ |
102 | (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ |
103 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ |
104 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ |
105 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \ |
106 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \ |
107 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\ |
108 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \ |
109 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ |
110 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ |
111 | (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \ |
112 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ |
113 | (void (*)(void))cipher_generic_get_ctx_params }, \ |
114 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ |
115 | (void (*)(void))cipher_generic_set_ctx_params }, \ |
116 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ |
117 | (void (*)(void))cipher_generic_gettable_params }, \ |
118 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ |
119 | (void (*)(void))cipher_generic_gettable_ctx_params }, \ |
120 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ |
121 | (void (*)(void))cipher_generic_settable_ctx_params }, \ |
122 | { 0, NULL } \ |
123 | }; |
124 | |
125 | #define IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, \ |
126 | kbits, blkbits, ivbits, typ) \ |
127 | const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \ |
128 | { OSSL_FUNC_CIPHER_NEWCTX, \ |
129 | (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ |
130 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ |
131 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ |
132 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \ |
133 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \ |
134 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\ |
135 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \ |
136 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ |
137 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ |
138 | (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \ |
139 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ |
140 | (void (*)(void))cipher_generic_get_ctx_params }, \ |
141 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ |
142 | (void (*)(void))cipher_var_keylen_set_ctx_params }, \ |
143 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ |
144 | (void (*)(void))cipher_generic_gettable_params }, \ |
145 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ |
146 | (void (*)(void))cipher_generic_gettable_ctx_params }, \ |
147 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ |
148 | (void (*)(void))cipher_var_keylen_settable_ctx_params }, \ |
149 | { 0, NULL } \ |
150 | }; |
151 | |
152 | |
153 | #define IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, \ |
154 | kbits, blkbits, ivbits, typ) \ |
155 | static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \ |
156 | static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ |
157 | { \ |
158 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ |
159 | kbits, blkbits, ivbits); \ |
160 | } \ |
161 | static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \ |
162 | static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \ |
163 | { \ |
164 | PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \ |
165 | if (ctx != NULL) { \ |
166 | cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \ |
167 | EVP_CIPH_##UCMODE##_MODE, flags, \ |
168 | PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \ |
169 | } \ |
170 | return ctx; \ |
171 | } \ |
172 | |
173 | #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ |
174 | blkbits, ivbits, typ) \ |
175 | IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \ |
176 | blkbits, ivbits, typ) \ |
177 | IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \ |
178 | blkbits, ivbits, typ) |
179 | |
180 | #define IMPLEMENT_var_keylen_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ |
181 | blkbits, ivbits, typ) \ |
182 | IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \ |
183 | blkbits, ivbits, typ) \ |
184 | IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \ |
185 | blkbits, ivbits, typ) |
186 | |
187 | PROV_CIPHER_HW_FN cipher_hw_generic_cbc; |
188 | PROV_CIPHER_HW_FN cipher_hw_generic_ecb; |
189 | PROV_CIPHER_HW_FN cipher_hw_generic_ofb128; |
190 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb128; |
191 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb8; |
192 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb1; |
193 | PROV_CIPHER_HW_FN cipher_hw_generic_ctr; |
194 | PROV_CIPHER_HW_FN cipher_hw_chunked_cbc; |
195 | PROV_CIPHER_HW_FN cipher_hw_chunked_cfb8; |
196 | PROV_CIPHER_HW_FN cipher_hw_chunked_cfb128; |
197 | PROV_CIPHER_HW_FN cipher_hw_chunked_ofb128; |
198 | #define cipher_hw_chunked_ecb cipher_hw_generic_ecb |
199 | #define cipher_hw_chunked_ctr cipher_hw_generic_ctr |
200 | #define cipher_hw_chunked_cfb1 cipher_hw_generic_cfb1 |
201 | |
202 | #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ |
203 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ |
204 | unsigned char *out, \ |
205 | const unsigned char *in, size_t len) \ |
206 | { \ |
207 | int num = ctx->num; \ |
208 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ |
209 | \ |
210 | while (len >= MAXCHUNK) { \ |
211 | FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \ |
212 | len -= MAXCHUNK; \ |
213 | in += MAXCHUNK; \ |
214 | out += MAXCHUNK; \ |
215 | } \ |
216 | if (len > 0) { \ |
217 | FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \ |
218 | } \ |
219 | ctx->num = num; \ |
220 | return 1; \ |
221 | } |
222 | |
223 | #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ |
224 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ |
225 | unsigned char *out, \ |
226 | const unsigned char *in, size_t len) \ |
227 | { \ |
228 | size_t i, bl = ctx->blocksize; \ |
229 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ |
230 | \ |
231 | if (len < bl) \ |
232 | return 1; \ |
233 | for (i = 0, len -= bl; i <= len; i += bl) \ |
234 | FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \ |
235 | return 1; \ |
236 | } |
237 | |
238 | #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ |
239 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ |
240 | unsigned char *out, \ |
241 | const unsigned char *in, size_t len) \ |
242 | { \ |
243 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ |
244 | \ |
245 | while (len >= MAXCHUNK) { \ |
246 | FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \ |
247 | len -= MAXCHUNK; \ |
248 | in += MAXCHUNK; \ |
249 | out += MAXCHUNK; \ |
250 | } \ |
251 | if (len > 0) \ |
252 | FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \ |
253 | return 1; \ |
254 | } |
255 | |
256 | #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ |
257 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ |
258 | unsigned char *out, \ |
259 | const unsigned char *in, size_t len) \ |
260 | { \ |
261 | size_t chunk = MAXCHUNK; \ |
262 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ |
263 | int num = ctx->num; \ |
264 | \ |
265 | if (len < chunk) \ |
266 | chunk = len; \ |
267 | while (len > 0 && len >= chunk) { \ |
268 | FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \ |
269 | ctx->enc); \ |
270 | len -= chunk; \ |
271 | in += chunk; \ |
272 | out += chunk; \ |
273 | if (len < chunk) \ |
274 | chunk = len; \ |
275 | } \ |
276 | ctx->num = num; \ |
277 | return 1; \ |
278 | } |
279 | |
280 | #define IMPLEMENT_CIPHER_HW_COPYCTX(name, CTX_TYPE) \ |
281 | static void name(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src) \ |
282 | { \ |
283 | CTX_TYPE *sctx = (CTX_TYPE *)src; \ |
284 | CTX_TYPE *dctx = (CTX_TYPE *)dst; \ |
285 | \ |
286 | *dctx = *sctx; \ |
287 | dst->ks = &dctx->ks.ks; \ |
288 | } |
289 | |
290 | #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \ |
291 | static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \ |
292 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \ |
293 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \ |
294 | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \ |
295 | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \ |
296 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), |
297 | |
298 | #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \ |
299 | OSSL_PARAM_END \ |
300 | }; \ |
301 | const OSSL_PARAM * name##_gettable_ctx_params(void) \ |
302 | { \ |
303 | return name##_known_gettable_ctx_params; \ |
304 | } |
305 | |
306 | #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \ |
307 | static const OSSL_PARAM name##_known_settable_ctx_params[] = { \ |
308 | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \ |
309 | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), |
310 | #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \ |
311 | OSSL_PARAM_END \ |
312 | }; \ |
313 | const OSSL_PARAM * name##_settable_ctx_params(void) \ |
314 | { \ |
315 | return name##_known_settable_ctx_params; \ |
316 | } |
317 | |
318 | int cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv, |
319 | size_t ivlen); |
320 | |
321 | size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize, |
322 | const unsigned char **in, size_t *inlen); |
323 | int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize, |
324 | const unsigned char **in, size_t *inlen); |
325 | |
326 | |