1 | /* |
2 | * Copyright 2015-2018 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/evp.h> |
11 | #include <openssl/core_numbers.h> |
12 | #include "internal/refcount.h" |
13 | |
14 | /* |
15 | * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag |
16 | * values in evp.h |
17 | */ |
18 | #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400 |
19 | |
20 | struct evp_pkey_ctx_st { |
21 | /* Actual operation */ |
22 | int operation; |
23 | |
24 | /* |
25 | * Library context, Algorithm name and properties associated |
26 | * with this context |
27 | */ |
28 | OPENSSL_CTX *libctx; |
29 | const char *algorithm; |
30 | const char *propquery; |
31 | |
32 | /* cached key manager */ |
33 | EVP_KEYMGMT *keymgmt; |
34 | |
35 | union { |
36 | struct { |
37 | EVP_KEYEXCH *exchange; |
38 | void *exchprovctx; |
39 | } kex; |
40 | |
41 | struct { |
42 | EVP_SIGNATURE *signature; |
43 | void *sigprovctx; |
44 | } sig; |
45 | |
46 | struct { |
47 | EVP_ASYM_CIPHER *cipher; |
48 | void *ciphprovctx; |
49 | } ciph; |
50 | } op; |
51 | |
52 | /* Legacy fields below */ |
53 | |
54 | /* Method associated with this operation */ |
55 | const EVP_PKEY_METHOD *pmeth; |
56 | /* Engine that implements this method or NULL if builtin */ |
57 | ENGINE *engine; |
58 | /* Key: may be NULL */ |
59 | EVP_PKEY *pkey; |
60 | /* Peer key for key agreement, may be NULL */ |
61 | EVP_PKEY *peerkey; |
62 | /* Algorithm specific data */ |
63 | void *data; |
64 | /* Application specific data */ |
65 | void *app_data; |
66 | /* Keygen callback */ |
67 | EVP_PKEY_gen_cb *pkey_gencb; |
68 | /* implementation specific keygen data */ |
69 | int *keygen_info; |
70 | int keygen_info_count; |
71 | } /* EVP_PKEY_CTX */ ; |
72 | |
73 | #define EVP_PKEY_FLAG_DYNAMIC 1 |
74 | |
75 | struct evp_pkey_method_st { |
76 | int pkey_id; |
77 | int flags; |
78 | int (*init) (EVP_PKEY_CTX *ctx); |
79 | int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src); |
80 | void (*cleanup) (EVP_PKEY_CTX *ctx); |
81 | int (*paramgen_init) (EVP_PKEY_CTX *ctx); |
82 | int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); |
83 | int (*keygen_init) (EVP_PKEY_CTX *ctx); |
84 | int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); |
85 | int (*sign_init) (EVP_PKEY_CTX *ctx); |
86 | int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, |
87 | const unsigned char *tbs, size_t tbslen); |
88 | int (*verify_init) (EVP_PKEY_CTX *ctx); |
89 | int (*verify) (EVP_PKEY_CTX *ctx, |
90 | const unsigned char *sig, size_t siglen, |
91 | const unsigned char *tbs, size_t tbslen); |
92 | int (*verify_recover_init) (EVP_PKEY_CTX *ctx); |
93 | int (*verify_recover) (EVP_PKEY_CTX *ctx, |
94 | unsigned char *rout, size_t *routlen, |
95 | const unsigned char *sig, size_t siglen); |
96 | int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); |
97 | int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, |
98 | EVP_MD_CTX *mctx); |
99 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); |
100 | int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen, |
101 | EVP_MD_CTX *mctx); |
102 | int (*encrypt_init) (EVP_PKEY_CTX *ctx); |
103 | int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, |
104 | const unsigned char *in, size_t inlen); |
105 | int (*decrypt_init) (EVP_PKEY_CTX *ctx); |
106 | int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, |
107 | const unsigned char *in, size_t inlen); |
108 | int (*derive_init) (EVP_PKEY_CTX *ctx); |
109 | int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); |
110 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2); |
111 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value); |
112 | int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
113 | const unsigned char *tbs, size_t tbslen); |
114 | int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, |
115 | size_t siglen, const unsigned char *tbs, |
116 | size_t tbslen); |
117 | int (*check) (EVP_PKEY *pkey); |
118 | int (*public_check) (EVP_PKEY *pkey); |
119 | int (*param_check) (EVP_PKEY *pkey); |
120 | |
121 | int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); |
122 | } /* EVP_PKEY_METHOD */ ; |
123 | |
124 | DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD) |
125 | |
126 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); |
127 | |
128 | const EVP_PKEY_METHOD *cmac_pkey_method(void); |
129 | const EVP_PKEY_METHOD *dh_pkey_method(void); |
130 | const EVP_PKEY_METHOD *dhx_pkey_method(void); |
131 | const EVP_PKEY_METHOD *dsa_pkey_method(void); |
132 | const EVP_PKEY_METHOD *ec_pkey_method(void); |
133 | const EVP_PKEY_METHOD *sm2_pkey_method(void); |
134 | const EVP_PKEY_METHOD *ecx25519_pkey_method(void); |
135 | const EVP_PKEY_METHOD *ecx448_pkey_method(void); |
136 | const EVP_PKEY_METHOD *ed25519_pkey_method(void); |
137 | const EVP_PKEY_METHOD *ed448_pkey_method(void); |
138 | const EVP_PKEY_METHOD *hmac_pkey_method(void); |
139 | const EVP_PKEY_METHOD *rsa_pkey_method(void); |
140 | const EVP_PKEY_METHOD *rsa_pss_pkey_method(void); |
141 | const EVP_PKEY_METHOD *scrypt_pkey_method(void); |
142 | const EVP_PKEY_METHOD *tls1_prf_pkey_method(void); |
143 | const EVP_PKEY_METHOD *hkdf_pkey_method(void); |
144 | const EVP_PKEY_METHOD *poly1305_pkey_method(void); |
145 | const EVP_PKEY_METHOD *siphash_pkey_method(void); |
146 | |
147 | struct evp_mac_st { |
148 | OSSL_PROVIDER *prov; |
149 | int name_id; |
150 | |
151 | CRYPTO_REF_COUNT refcnt; |
152 | CRYPTO_RWLOCK *lock; |
153 | |
154 | OSSL_OP_mac_newctx_fn *newctx; |
155 | OSSL_OP_mac_dupctx_fn *dupctx; |
156 | OSSL_OP_mac_freectx_fn *freectx; |
157 | OSSL_OP_mac_size_fn *size; |
158 | OSSL_OP_mac_init_fn *init; |
159 | OSSL_OP_mac_update_fn *update; |
160 | OSSL_OP_mac_final_fn *final; |
161 | OSSL_OP_mac_gettable_params_fn *gettable_params; |
162 | OSSL_OP_mac_gettable_ctx_params_fn *gettable_ctx_params; |
163 | OSSL_OP_mac_settable_ctx_params_fn *settable_ctx_params; |
164 | OSSL_OP_mac_get_params_fn *get_params; |
165 | OSSL_OP_mac_get_ctx_params_fn *get_ctx_params; |
166 | OSSL_OP_mac_set_ctx_params_fn *set_ctx_params; |
167 | }; |
168 | |
169 | struct evp_kdf_st { |
170 | OSSL_PROVIDER *prov; |
171 | int name_id; |
172 | CRYPTO_REF_COUNT refcnt; |
173 | CRYPTO_RWLOCK *lock; |
174 | |
175 | OSSL_OP_kdf_newctx_fn *newctx; |
176 | OSSL_OP_kdf_dupctx_fn *dupctx; |
177 | OSSL_OP_kdf_freectx_fn *freectx; |
178 | OSSL_OP_kdf_reset_fn *reset; |
179 | OSSL_OP_kdf_derive_fn *derive; |
180 | OSSL_OP_kdf_gettable_params_fn *gettable_params; |
181 | OSSL_OP_kdf_gettable_ctx_params_fn *gettable_ctx_params; |
182 | OSSL_OP_kdf_settable_ctx_params_fn *settable_ctx_params; |
183 | OSSL_OP_kdf_get_params_fn *get_params; |
184 | OSSL_OP_kdf_get_ctx_params_fn *get_ctx_params; |
185 | OSSL_OP_kdf_set_ctx_params_fn *set_ctx_params; |
186 | }; |
187 | |
188 | struct evp_md_st { |
189 | /* nid */ |
190 | int type; |
191 | |
192 | /* Legacy structure members */ |
193 | /* TODO(3.0): Remove these */ |
194 | int pkey_type; |
195 | int md_size; |
196 | unsigned long flags; |
197 | int (*init) (EVP_MD_CTX *ctx); |
198 | int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); |
199 | int (*final) (EVP_MD_CTX *ctx, unsigned char *md); |
200 | int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from); |
201 | int (*cleanup) (EVP_MD_CTX *ctx); |
202 | int block_size; |
203 | int ctx_size; /* how big does the ctx->md_data need to be */ |
204 | /* control function */ |
205 | int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2); |
206 | |
207 | /* New structure members */ |
208 | /* TODO(3.0): Remove above comment when legacy has gone */ |
209 | int name_id; |
210 | OSSL_PROVIDER *prov; |
211 | CRYPTO_REF_COUNT refcnt; |
212 | CRYPTO_RWLOCK *lock; |
213 | OSSL_OP_digest_newctx_fn *newctx; |
214 | OSSL_OP_digest_init_fn *dinit; |
215 | OSSL_OP_digest_update_fn *dupdate; |
216 | OSSL_OP_digest_final_fn *dfinal; |
217 | OSSL_OP_digest_digest_fn *digest; |
218 | OSSL_OP_digest_freectx_fn *freectx; |
219 | OSSL_OP_digest_dupctx_fn *dupctx; |
220 | OSSL_OP_digest_get_params_fn *get_params; |
221 | OSSL_OP_digest_set_ctx_params_fn *set_ctx_params; |
222 | OSSL_OP_digest_get_ctx_params_fn *get_ctx_params; |
223 | OSSL_OP_digest_gettable_params_fn *gettable_params; |
224 | OSSL_OP_digest_settable_ctx_params_fn *settable_ctx_params; |
225 | OSSL_OP_digest_gettable_ctx_params_fn *gettable_ctx_params; |
226 | |
227 | } /* EVP_MD */ ; |
228 | |
229 | struct evp_cipher_st { |
230 | int nid; |
231 | |
232 | int block_size; |
233 | /* Default value for variable length ciphers */ |
234 | int key_len; |
235 | int iv_len; |
236 | |
237 | /* Legacy structure members */ |
238 | /* TODO(3.0): Remove these */ |
239 | /* Various flags */ |
240 | unsigned long flags; |
241 | /* init key */ |
242 | int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key, |
243 | const unsigned char *iv, int enc); |
244 | /* encrypt/decrypt data */ |
245 | int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out, |
246 | const unsigned char *in, size_t inl); |
247 | /* cleanup ctx */ |
248 | int (*cleanup) (EVP_CIPHER_CTX *); |
249 | /* how big ctx->cipher_data needs to be */ |
250 | int ctx_size; |
251 | /* Populate a ASN1_TYPE with parameters */ |
252 | int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); |
253 | /* Get parameters from a ASN1_TYPE */ |
254 | int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); |
255 | /* Miscellaneous operations */ |
256 | int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr); |
257 | /* Application data */ |
258 | void *app_data; |
259 | |
260 | /* New structure members */ |
261 | /* TODO(3.0): Remove above comment when legacy has gone */ |
262 | int name_id; |
263 | OSSL_PROVIDER *prov; |
264 | CRYPTO_REF_COUNT refcnt; |
265 | CRYPTO_RWLOCK *lock; |
266 | OSSL_OP_cipher_newctx_fn *newctx; |
267 | OSSL_OP_cipher_encrypt_init_fn *einit; |
268 | OSSL_OP_cipher_decrypt_init_fn *dinit; |
269 | OSSL_OP_cipher_update_fn *cupdate; |
270 | OSSL_OP_cipher_final_fn *cfinal; |
271 | OSSL_OP_cipher_cipher_fn *ccipher; |
272 | OSSL_OP_cipher_freectx_fn *freectx; |
273 | OSSL_OP_cipher_dupctx_fn *dupctx; |
274 | OSSL_OP_cipher_get_params_fn *get_params; |
275 | OSSL_OP_cipher_get_ctx_params_fn *get_ctx_params; |
276 | OSSL_OP_cipher_set_ctx_params_fn *set_ctx_params; |
277 | OSSL_OP_cipher_gettable_params_fn *gettable_params; |
278 | OSSL_OP_cipher_gettable_ctx_params_fn *gettable_ctx_params; |
279 | OSSL_OP_cipher_settable_ctx_params_fn *settable_ctx_params; |
280 | } /* EVP_CIPHER */ ; |
281 | |
282 | /* Macros to code block cipher wrappers */ |
283 | |
284 | /* Wrapper functions for each cipher mode */ |
285 | |
286 | #define EVP_C_DATA(kstruct, ctx) \ |
287 | ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx)) |
288 | |
289 | #define BLOCK_CIPHER_ecb_loop() \ |
290 | size_t i, bl; \ |
291 | bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \ |
292 | if (inl < bl) return 1;\ |
293 | inl -= bl; \ |
294 | for (i=0; i <= inl; i+=bl) |
295 | |
296 | #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ |
297 | static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
298 | {\ |
299 | BLOCK_CIPHER_ecb_loop() \ |
300 | cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \ |
301 | return 1;\ |
302 | } |
303 | |
304 | #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2)) |
305 | |
306 | #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ |
307 | static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
308 | {\ |
309 | while(inl>=EVP_MAXCHUNK) {\ |
310 | int num = EVP_CIPHER_CTX_num(ctx);\ |
311 | cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \ |
312 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
313 | inl-=EVP_MAXCHUNK;\ |
314 | in +=EVP_MAXCHUNK;\ |
315 | out+=EVP_MAXCHUNK;\ |
316 | }\ |
317 | if (inl) {\ |
318 | int num = EVP_CIPHER_CTX_num(ctx);\ |
319 | cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \ |
320 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
321 | }\ |
322 | return 1;\ |
323 | } |
324 | |
325 | #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ |
326 | static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
327 | {\ |
328 | while(inl>=EVP_MAXCHUNK) \ |
329 | {\ |
330 | cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\ |
331 | inl-=EVP_MAXCHUNK;\ |
332 | in +=EVP_MAXCHUNK;\ |
333 | out+=EVP_MAXCHUNK;\ |
334 | }\ |
335 | if (inl)\ |
336 | cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\ |
337 | return 1;\ |
338 | } |
339 | |
340 | #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ |
341 | static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
342 | {\ |
343 | size_t chunk = EVP_MAXCHUNK;\ |
344 | if (cbits == 1) chunk >>= 3;\ |
345 | if (inl < chunk) chunk = inl;\ |
346 | while (inl && inl >= chunk)\ |
347 | {\ |
348 | int num = EVP_CIPHER_CTX_num(ctx);\ |
349 | cprefix##_cfb##cbits##_encrypt(in, out, (long) \ |
350 | ((cbits == 1) \ |
351 | && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \ |
352 | ? chunk*8 : chunk), \ |
353 | &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx),\ |
354 | &num, EVP_CIPHER_CTX_encrypting(ctx));\ |
355 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
356 | inl -= chunk;\ |
357 | in += chunk;\ |
358 | out += chunk;\ |
359 | if (inl < chunk) chunk = inl;\ |
360 | }\ |
361 | return 1;\ |
362 | } |
363 | |
364 | #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ |
365 | BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ |
366 | BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ |
367 | BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ |
368 | BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) |
369 | |
370 | #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \ |
371 | key_len, iv_len, flags, init_key, cleanup, \ |
372 | set_asn1, get_asn1, ctrl) \ |
373 | static const EVP_CIPHER cname##_##mode = { \ |
374 | nid##_##nmode, block_size, key_len, iv_len, \ |
375 | flags | EVP_CIPH_##MODE##_MODE, \ |
376 | init_key, \ |
377 | cname##_##mode##_cipher, \ |
378 | cleanup, \ |
379 | sizeof(kstruct), \ |
380 | set_asn1, get_asn1,\ |
381 | ctrl, \ |
382 | NULL \ |
383 | }; \ |
384 | const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; } |
385 | |
386 | #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \ |
387 | iv_len, flags, init_key, cleanup, set_asn1, \ |
388 | get_asn1, ctrl) \ |
389 | BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \ |
390 | iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) |
391 | |
392 | #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \ |
393 | iv_len, cbits, flags, init_key, cleanup, \ |
394 | set_asn1, get_asn1, ctrl) \ |
395 | BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \ |
396 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ |
397 | get_asn1, ctrl) |
398 | |
399 | #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \ |
400 | iv_len, cbits, flags, init_key, cleanup, \ |
401 | set_asn1, get_asn1, ctrl) \ |
402 | BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \ |
403 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ |
404 | get_asn1, ctrl) |
405 | |
406 | #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ |
407 | flags, init_key, cleanup, set_asn1, \ |
408 | get_asn1, ctrl) \ |
409 | BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ |
410 | 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) |
411 | |
412 | #define BLOCK_CIPHER_defs(cname, kstruct, \ |
413 | nid, block_size, key_len, iv_len, cbits, flags, \ |
414 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ |
415 | BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \ |
416 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ |
417 | BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \ |
418 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ |
419 | BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ |
420 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ |
421 | BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \ |
422 | init_key, cleanup, set_asn1, get_asn1, ctrl) |
423 | |
424 | /*- |
425 | #define BLOCK_CIPHER_defs(cname, kstruct, \ |
426 | nid, block_size, key_len, iv_len, flags,\ |
427 | init_key, cleanup, set_asn1, get_asn1, ctrl)\ |
428 | static const EVP_CIPHER cname##_cbc = {\ |
429 | nid##_cbc, block_size, key_len, iv_len, \ |
430 | flags | EVP_CIPH_CBC_MODE,\ |
431 | init_key,\ |
432 | cname##_cbc_cipher,\ |
433 | cleanup,\ |
434 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ |
435 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ |
436 | set_asn1, get_asn1,\ |
437 | ctrl, \ |
438 | NULL \ |
439 | };\ |
440 | const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\ |
441 | static const EVP_CIPHER cname##_cfb = {\ |
442 | nid##_cfb64, 1, key_len, iv_len, \ |
443 | flags | EVP_CIPH_CFB_MODE,\ |
444 | init_key,\ |
445 | cname##_cfb_cipher,\ |
446 | cleanup,\ |
447 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ |
448 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ |
449 | set_asn1, get_asn1,\ |
450 | ctrl,\ |
451 | NULL \ |
452 | };\ |
453 | const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\ |
454 | static const EVP_CIPHER cname##_ofb = {\ |
455 | nid##_ofb64, 1, key_len, iv_len, \ |
456 | flags | EVP_CIPH_OFB_MODE,\ |
457 | init_key,\ |
458 | cname##_ofb_cipher,\ |
459 | cleanup,\ |
460 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ |
461 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ |
462 | set_asn1, get_asn1,\ |
463 | ctrl,\ |
464 | NULL \ |
465 | };\ |
466 | const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\ |
467 | static const EVP_CIPHER cname##_ecb = {\ |
468 | nid##_ecb, block_size, key_len, iv_len, \ |
469 | flags | EVP_CIPH_ECB_MODE,\ |
470 | init_key,\ |
471 | cname##_ecb_cipher,\ |
472 | cleanup,\ |
473 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ |
474 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ |
475 | set_asn1, get_asn1,\ |
476 | ctrl,\ |
477 | NULL \ |
478 | };\ |
479 | const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } |
480 | */ |
481 | |
482 | #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \ |
483 | block_size, key_len, iv_len, cbits, \ |
484 | flags, init_key, \ |
485 | cleanup, set_asn1, get_asn1, ctrl) \ |
486 | BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ |
487 | BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \ |
488 | cbits, flags, init_key, cleanup, set_asn1, \ |
489 | get_asn1, ctrl) |
490 | |
491 | #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \ |
492 | BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ |
493 | BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ |
494 | NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ |
495 | (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \ |
496 | cipher##_init_key, NULL, NULL, NULL, NULL) |
497 | |
498 | |
499 | # ifndef OPENSSL_NO_EC |
500 | |
501 | #define X25519_KEYLEN 32 |
502 | #define X448_KEYLEN 56 |
503 | #define ED25519_KEYLEN 32 |
504 | #define ED448_KEYLEN 57 |
505 | |
506 | #define MAX_KEYLEN ED448_KEYLEN |
507 | |
508 | typedef struct { |
509 | unsigned char pubkey[MAX_KEYLEN]; |
510 | unsigned char *privkey; |
511 | } ECX_KEY; |
512 | |
513 | #endif |
514 | |
515 | /* |
516 | * Type needs to be a bit field Sub-type needs to be for variations on the |
517 | * method, as in, can it do arbitrary encryption.... |
518 | */ |
519 | struct evp_pkey_st { |
520 | /* == Legacy attributes == */ |
521 | int type; |
522 | int save_type; |
523 | const EVP_PKEY_ASN1_METHOD *ameth; |
524 | ENGINE *engine; |
525 | ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */ |
526 | union { |
527 | void *ptr; |
528 | # ifndef OPENSSL_NO_RSA |
529 | struct rsa_st *rsa; /* RSA */ |
530 | # endif |
531 | # ifndef OPENSSL_NO_DSA |
532 | struct dsa_st *dsa; /* DSA */ |
533 | # endif |
534 | # ifndef OPENSSL_NO_DH |
535 | struct dh_st *dh; /* DH */ |
536 | # endif |
537 | # ifndef OPENSSL_NO_EC |
538 | struct ec_key_st *ec; /* ECC */ |
539 | ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */ |
540 | # endif |
541 | } pkey; |
542 | |
543 | /* == Common attributes == */ |
544 | CRYPTO_REF_COUNT references; |
545 | CRYPTO_RWLOCK *lock; |
546 | STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */ |
547 | int save_parameters; |
548 | |
549 | /* == Provider attributes == */ |
550 | /* |
551 | * To support transparent export/import between providers that |
552 | * support the methods for it, and still not having to do the |
553 | * export/import every time a key or domain params are used, we |
554 | * maintain a cache of imported key / domain params, indexed by |
555 | * provider address. pkeys[0] is *always* the "original" data. |
556 | */ |
557 | struct { |
558 | EVP_KEYMGMT *keymgmt; |
559 | void *provdata; |
560 | /* 0 = provdata is a key, 1 = provdata is domain params */ |
561 | int domainparams; |
562 | } pkeys[10]; |
563 | /* |
564 | * If there is a legacy key assigned to this structure, we keep |
565 | * a copy of that key's dirty count. |
566 | */ |
567 | size_t dirty_cnt_copy; |
568 | } /* EVP_PKEY */ ; |
569 | |
570 | #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \ |
571 | ((ctx)->operation == EVP_PKEY_OP_SIGN \ |
572 | || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \ |
573 | || (ctx)->operation == EVP_PKEY_OP_VERIFY \ |
574 | || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \ |
575 | || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER) |
576 | |
577 | #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \ |
578 | ((ctx)->operation == EVP_PKEY_OP_DERIVE) |
579 | |
580 | #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \ |
581 | ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \ |
582 | || (ctx)->operation == EVP_PKEY_OP_DECRYPT) |
583 | |
584 | void openssl_add_all_ciphers_int(void); |
585 | void openssl_add_all_digests_int(void); |
586 | void evp_cleanup_int(void); |
587 | void evp_app_cleanup_int(void); |
588 | |
589 | /* KEYMGMT helper functions */ |
590 | void *evp_keymgmt_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt, |
591 | int domainparams); |
592 | void evp_keymgmt_clear_pkey_cache(EVP_PKEY *pk); |
593 | void *evp_keymgmt_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, |
594 | const OSSL_PARAM params[], int domainparams); |
595 | |
596 | |
597 | /* KEYMGMT provider interface functions */ |
598 | void *evp_keymgmt_importdomparams(const EVP_KEYMGMT *keymgmt, |
599 | const OSSL_PARAM params[]); |
600 | void *evp_keymgmt_gendomparams(const EVP_KEYMGMT *keymgmt, |
601 | const OSSL_PARAM params[]); |
602 | void evp_keymgmt_freedomparams(const EVP_KEYMGMT *keymgmt, |
603 | void *provdomparams); |
604 | int evp_keymgmt_exportdomparams(const EVP_KEYMGMT *keymgmt, |
605 | void *provdomparams, |
606 | OSSL_CALLBACK *param_cb, void *cbarg); |
607 | const OSSL_PARAM * |
608 | evp_keymgmt_importdomparam_types(const EVP_KEYMGMT *keymgmt); |
609 | const OSSL_PARAM * |
610 | evp_keymgmt_exportdomparam_types(const EVP_KEYMGMT *keymgmt); |
611 | |
612 | void *evp_keymgmt_importkey(const EVP_KEYMGMT *keymgmt, |
613 | const OSSL_PARAM params[]); |
614 | void *evp_keymgmt_genkey(const EVP_KEYMGMT *keymgmt, void *domparams, |
615 | const OSSL_PARAM params[]); |
616 | void *evp_keymgmt_loadkey(const EVP_KEYMGMT *keymgmt, |
617 | void *id, size_t idlen); |
618 | void evp_keymgmt_freekey(const EVP_KEYMGMT *keymgmt, void *provkey); |
619 | int evp_keymgmt_exportkey(const EVP_KEYMGMT *keymgmt, void *provkey, |
620 | OSSL_CALLBACK *param_cb, void *cbarg); |
621 | const OSSL_PARAM *evp_keymgmt_importkey_types(const EVP_KEYMGMT *keymgmt); |
622 | const OSSL_PARAM *evp_keymgmt_exportkey_types(const EVP_KEYMGMT *keymgmt); |
623 | |
624 | /* Pulling defines out of C source files */ |
625 | |
626 | #define EVP_RC4_KEY_SIZE 16 |
627 | #ifndef TLS1_1_VERSION |
628 | # define TLS1_1_VERSION 0x0302 |
629 | #endif |
630 | |
631 | void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags); |
632 | |
633 | /* EVP_ENCODE_CTX flags */ |
634 | /* Don't generate new lines when encoding */ |
635 | #define EVP_ENCODE_CTX_NO_NEWLINES 1 |
636 | /* Use the SRP base64 alphabet instead of the standard one */ |
637 | #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2 |
638 | |
639 | const EVP_CIPHER *evp_get_cipherbyname_ex(OPENSSL_CTX *libctx, const char *name); |
640 | const EVP_MD *evp_get_digestbyname_ex(OPENSSL_CTX *libctx, const char *name); |
641 | |