]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/crypto/evp.h
STORE: Move the built-in 'file:' loader to become an engine module
[thirdparty/openssl.git] / include / crypto / evp.h
1 /*
2 * Copyright 2015-2020 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_dispatch.h>
12 #include "internal/refcount.h"
13 #include "crypto/ecx.h"
14
15 /*
16 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
17 * values in evp.h
18 */
19 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
20
21 struct evp_pkey_ctx_st {
22 /* Actual operation */
23 int operation;
24
25 /*
26 * Library context, property query, keytype and keymgmt associated with
27 * this context
28 */
29 OPENSSL_CTX *libctx;
30 const char *propquery;
31 const char *keytype;
32 EVP_KEYMGMT *keymgmt;
33
34 union {
35 struct {
36 void *genctx;
37 } keymgmt;
38
39 struct {
40 EVP_KEYEXCH *exchange;
41 void *exchprovctx;
42 } kex;
43
44 struct {
45 EVP_SIGNATURE *signature;
46 void *sigprovctx;
47 } sig;
48
49 struct {
50 EVP_ASYM_CIPHER *cipher;
51 void *ciphprovctx;
52 } ciph;
53 } op;
54
55 /* Application specific data, usually used by the callback */
56 void *app_data;
57 /* Keygen callback */
58 EVP_PKEY_gen_cb *pkey_gencb;
59 /* implementation specific keygen data */
60 int *keygen_info;
61 int keygen_info_count;
62
63 /* Legacy fields below */
64
65 /* Method associated with this operation */
66 const EVP_PKEY_METHOD *pmeth;
67 /* Engine that implements this method or NULL if builtin */
68 ENGINE *engine;
69 /* Key: may be NULL */
70 EVP_PKEY *pkey;
71 /* Peer key for key agreement, may be NULL */
72 EVP_PKEY *peerkey;
73 /* Algorithm specific data */
74 void *data;
75 /* Indicator if digest_custom needs to be called */
76 unsigned int flag_call_digest_custom:1;
77 } /* EVP_PKEY_CTX */ ;
78
79 #define EVP_PKEY_FLAG_DYNAMIC 1
80
81 struct evp_pkey_method_st {
82 int pkey_id;
83 int flags;
84 int (*init) (EVP_PKEY_CTX *ctx);
85 int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
86 void (*cleanup) (EVP_PKEY_CTX *ctx);
87 int (*paramgen_init) (EVP_PKEY_CTX *ctx);
88 int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
89 int (*keygen_init) (EVP_PKEY_CTX *ctx);
90 int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
91 int (*sign_init) (EVP_PKEY_CTX *ctx);
92 int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
93 const unsigned char *tbs, size_t tbslen);
94 int (*verify_init) (EVP_PKEY_CTX *ctx);
95 int (*verify) (EVP_PKEY_CTX *ctx,
96 const unsigned char *sig, size_t siglen,
97 const unsigned char *tbs, size_t tbslen);
98 int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
99 int (*verify_recover) (EVP_PKEY_CTX *ctx,
100 unsigned char *rout, size_t *routlen,
101 const unsigned char *sig, size_t siglen);
102 int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
103 int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
104 EVP_MD_CTX *mctx);
105 int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
106 int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
107 EVP_MD_CTX *mctx);
108 int (*encrypt_init) (EVP_PKEY_CTX *ctx);
109 int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
110 const unsigned char *in, size_t inlen);
111 int (*decrypt_init) (EVP_PKEY_CTX *ctx);
112 int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
113 const unsigned char *in, size_t inlen);
114 int (*derive_init) (EVP_PKEY_CTX *ctx);
115 int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
116 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
117 int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
118 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
119 const unsigned char *tbs, size_t tbslen);
120 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
121 size_t siglen, const unsigned char *tbs,
122 size_t tbslen);
123 int (*check) (EVP_PKEY *pkey);
124 int (*public_check) (EVP_PKEY *pkey);
125 int (*param_check) (EVP_PKEY *pkey);
126
127 int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
128 } /* EVP_PKEY_METHOD */ ;
129
130 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
131
132 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
133
134 const EVP_PKEY_METHOD *dh_pkey_method(void);
135 const EVP_PKEY_METHOD *dhx_pkey_method(void);
136 const EVP_PKEY_METHOD *dsa_pkey_method(void);
137 const EVP_PKEY_METHOD *ec_pkey_method(void);
138 const EVP_PKEY_METHOD *sm2_pkey_method(void);
139 const EVP_PKEY_METHOD *ecx25519_pkey_method(void);
140 const EVP_PKEY_METHOD *ecx448_pkey_method(void);
141 const EVP_PKEY_METHOD *ed25519_pkey_method(void);
142 const EVP_PKEY_METHOD *ed448_pkey_method(void);
143 const EVP_PKEY_METHOD *rsa_pkey_method(void);
144 const EVP_PKEY_METHOD *rsa_pss_pkey_method(void);
145
146 struct evp_mac_st {
147 OSSL_PROVIDER *prov;
148 int name_id;
149
150 CRYPTO_REF_COUNT refcnt;
151 CRYPTO_RWLOCK *lock;
152
153 OSSL_FUNC_mac_newctx_fn *newctx;
154 OSSL_FUNC_mac_dupctx_fn *dupctx;
155 OSSL_FUNC_mac_freectx_fn *freectx;
156 OSSL_FUNC_mac_size_fn *size;
157 OSSL_FUNC_mac_init_fn *init;
158 OSSL_FUNC_mac_update_fn *update;
159 OSSL_FUNC_mac_final_fn *final;
160 OSSL_FUNC_mac_gettable_params_fn *gettable_params;
161 OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params;
162 OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params;
163 OSSL_FUNC_mac_get_params_fn *get_params;
164 OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params;
165 OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params;
166 };
167
168 struct evp_kdf_st {
169 OSSL_PROVIDER *prov;
170 int name_id;
171 CRYPTO_REF_COUNT refcnt;
172 CRYPTO_RWLOCK *lock;
173
174 OSSL_FUNC_kdf_newctx_fn *newctx;
175 OSSL_FUNC_kdf_dupctx_fn *dupctx;
176 OSSL_FUNC_kdf_freectx_fn *freectx;
177 OSSL_FUNC_kdf_reset_fn *reset;
178 OSSL_FUNC_kdf_derive_fn *derive;
179 OSSL_FUNC_kdf_gettable_params_fn *gettable_params;
180 OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params;
181 OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params;
182 OSSL_FUNC_kdf_get_params_fn *get_params;
183 OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params;
184 OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params;
185 };
186
187 struct evp_md_st {
188 /* nid */
189 int type;
190
191 /* Legacy structure members */
192 /* TODO(3.0): Remove these */
193 int pkey_type;
194 int md_size;
195 unsigned long flags;
196 int (*init) (EVP_MD_CTX *ctx);
197 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
198 int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
199 int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
200 int (*cleanup) (EVP_MD_CTX *ctx);
201 int block_size;
202 int ctx_size; /* how big does the ctx->md_data need to be */
203 /* control function */
204 int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
205
206 /* New structure members */
207 /* TODO(3.0): Remove above comment when legacy has gone */
208 int name_id;
209 OSSL_PROVIDER *prov;
210 CRYPTO_REF_COUNT refcnt;
211 CRYPTO_RWLOCK *lock;
212 OSSL_FUNC_digest_newctx_fn *newctx;
213 OSSL_FUNC_digest_init_fn *dinit;
214 OSSL_FUNC_digest_update_fn *dupdate;
215 OSSL_FUNC_digest_final_fn *dfinal;
216 OSSL_FUNC_digest_digest_fn *digest;
217 OSSL_FUNC_digest_freectx_fn *freectx;
218 OSSL_FUNC_digest_dupctx_fn *dupctx;
219 OSSL_FUNC_digest_get_params_fn *get_params;
220 OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
221 OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
222 OSSL_FUNC_digest_gettable_params_fn *gettable_params;
223 OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
224 OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
225
226 } /* EVP_MD */ ;
227
228 struct evp_cipher_st {
229 int nid;
230
231 int block_size;
232 /* Default value for variable length ciphers */
233 int key_len;
234 int iv_len;
235
236 /* Legacy structure members */
237 /* TODO(3.0): Remove these */
238 /* Various flags */
239 unsigned long flags;
240 /* init key */
241 int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
242 const unsigned char *iv, int enc);
243 /* encrypt/decrypt data */
244 int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
245 const unsigned char *in, size_t inl);
246 /* cleanup ctx */
247 int (*cleanup) (EVP_CIPHER_CTX *);
248 /* how big ctx->cipher_data needs to be */
249 int ctx_size;
250 /* Populate a ASN1_TYPE with parameters */
251 int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
252 /* Get parameters from a ASN1_TYPE */
253 int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
254 /* Miscellaneous operations */
255 int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
256 /* Application data */
257 void *app_data;
258
259 /* New structure members */
260 /* TODO(3.0): Remove above comment when legacy has gone */
261 int name_id;
262 OSSL_PROVIDER *prov;
263 CRYPTO_REF_COUNT refcnt;
264 CRYPTO_RWLOCK *lock;
265 OSSL_FUNC_cipher_newctx_fn *newctx;
266 OSSL_FUNC_cipher_encrypt_init_fn *einit;
267 OSSL_FUNC_cipher_decrypt_init_fn *dinit;
268 OSSL_FUNC_cipher_update_fn *cupdate;
269 OSSL_FUNC_cipher_final_fn *cfinal;
270 OSSL_FUNC_cipher_cipher_fn *ccipher;
271 OSSL_FUNC_cipher_freectx_fn *freectx;
272 OSSL_FUNC_cipher_dupctx_fn *dupctx;
273 OSSL_FUNC_cipher_get_params_fn *get_params;
274 OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
275 OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
276 OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
277 OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
278 OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
279 } /* EVP_CIPHER */ ;
280
281 /* Macros to code block cipher wrappers */
282
283 /* Wrapper functions for each cipher mode */
284
285 #define EVP_C_DATA(kstruct, ctx) \
286 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
287
288 #define BLOCK_CIPHER_ecb_loop() \
289 size_t i, bl; \
290 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
291 if (inl < bl) return 1;\
292 inl -= bl; \
293 for (i=0; i <= inl; i+=bl)
294
295 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
296 static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
297 {\
298 BLOCK_CIPHER_ecb_loop() \
299 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
300 return 1;\
301 }
302
303 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
304
305 #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
306 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
307 {\
308 while(inl>=EVP_MAXCHUNK) {\
309 int num = EVP_CIPHER_CTX_num(ctx);\
310 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
311 EVP_CIPHER_CTX_set_num(ctx, num);\
312 inl-=EVP_MAXCHUNK;\
313 in +=EVP_MAXCHUNK;\
314 out+=EVP_MAXCHUNK;\
315 }\
316 if (inl) {\
317 int num = EVP_CIPHER_CTX_num(ctx);\
318 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
319 EVP_CIPHER_CTX_set_num(ctx, num);\
320 }\
321 return 1;\
322 }
323
324 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
325 static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
326 {\
327 while(inl>=EVP_MAXCHUNK) \
328 {\
329 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
330 inl-=EVP_MAXCHUNK;\
331 in +=EVP_MAXCHUNK;\
332 out+=EVP_MAXCHUNK;\
333 }\
334 if (inl)\
335 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
336 return 1;\
337 }
338
339 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
340 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
341 {\
342 size_t chunk = EVP_MAXCHUNK;\
343 if (cbits == 1) chunk >>= 3;\
344 if (inl < chunk) chunk = inl;\
345 while (inl && inl >= chunk)\
346 {\
347 int num = EVP_CIPHER_CTX_num(ctx);\
348 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
349 ((cbits == 1) \
350 && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
351 ? chunk*8 : chunk), \
352 &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\
353 &num, EVP_CIPHER_CTX_encrypting(ctx));\
354 EVP_CIPHER_CTX_set_num(ctx, num);\
355 inl -= chunk;\
356 in += chunk;\
357 out += chunk;\
358 if (inl < chunk) chunk = inl;\
359 }\
360 return 1;\
361 }
362
363 #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
364 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
365 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
366 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
367 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
368
369 #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
370 key_len, iv_len, flags, init_key, cleanup, \
371 set_asn1, get_asn1, ctrl) \
372 static const EVP_CIPHER cname##_##mode = { \
373 nid##_##nmode, block_size, key_len, iv_len, \
374 flags | EVP_CIPH_##MODE##_MODE, \
375 init_key, \
376 cname##_##mode##_cipher, \
377 cleanup, \
378 sizeof(kstruct), \
379 set_asn1, get_asn1,\
380 ctrl, \
381 NULL \
382 }; \
383 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
384
385 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
386 iv_len, flags, init_key, cleanup, set_asn1, \
387 get_asn1, ctrl) \
388 BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
389 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
390
391 #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
392 iv_len, cbits, flags, init_key, cleanup, \
393 set_asn1, get_asn1, ctrl) \
394 BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
395 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
396 get_asn1, ctrl)
397
398 #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
399 iv_len, cbits, flags, init_key, cleanup, \
400 set_asn1, get_asn1, ctrl) \
401 BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
402 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
403 get_asn1, ctrl)
404
405 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
406 flags, init_key, cleanup, set_asn1, \
407 get_asn1, ctrl) \
408 BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
409 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
410
411 #define BLOCK_CIPHER_defs(cname, kstruct, \
412 nid, block_size, key_len, iv_len, cbits, flags, \
413 init_key, cleanup, set_asn1, get_asn1, ctrl) \
414 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
415 init_key, cleanup, set_asn1, get_asn1, ctrl) \
416 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
417 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
418 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
419 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
420 BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
421 init_key, cleanup, set_asn1, get_asn1, ctrl)
422
423 /*-
424 #define BLOCK_CIPHER_defs(cname, kstruct, \
425 nid, block_size, key_len, iv_len, flags,\
426 init_key, cleanup, set_asn1, get_asn1, ctrl)\
427 static const EVP_CIPHER cname##_cbc = {\
428 nid##_cbc, block_size, key_len, iv_len, \
429 flags | EVP_CIPH_CBC_MODE,\
430 init_key,\
431 cname##_cbc_cipher,\
432 cleanup,\
433 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
434 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
435 set_asn1, get_asn1,\
436 ctrl, \
437 NULL \
438 };\
439 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
440 static const EVP_CIPHER cname##_cfb = {\
441 nid##_cfb64, 1, key_len, iv_len, \
442 flags | EVP_CIPH_CFB_MODE,\
443 init_key,\
444 cname##_cfb_cipher,\
445 cleanup,\
446 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
447 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
448 set_asn1, get_asn1,\
449 ctrl,\
450 NULL \
451 };\
452 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
453 static const EVP_CIPHER cname##_ofb = {\
454 nid##_ofb64, 1, key_len, iv_len, \
455 flags | EVP_CIPH_OFB_MODE,\
456 init_key,\
457 cname##_ofb_cipher,\
458 cleanup,\
459 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
460 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
461 set_asn1, get_asn1,\
462 ctrl,\
463 NULL \
464 };\
465 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
466 static const EVP_CIPHER cname##_ecb = {\
467 nid##_ecb, block_size, key_len, iv_len, \
468 flags | EVP_CIPH_ECB_MODE,\
469 init_key,\
470 cname##_ecb_cipher,\
471 cleanup,\
472 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
473 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
474 set_asn1, get_asn1,\
475 ctrl,\
476 NULL \
477 };\
478 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
479 */
480
481 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
482 block_size, key_len, iv_len, cbits, \
483 flags, init_key, \
484 cleanup, set_asn1, get_asn1, ctrl) \
485 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
486 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
487 cbits, flags, init_key, cleanup, set_asn1, \
488 get_asn1, ctrl)
489
490 #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
491 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
492 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
493 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
494 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
495 cipher##_init_key, NULL, NULL, NULL, NULL)
496
497 /*
498 * An EVP_PKEY can have the following states:
499 *
500 * untyped & empty:
501 *
502 * type == EVP_PKEY_NONE && keymgmt == NULL
503 *
504 * typed & empty:
505 *
506 * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
507 * || (keymgmt != NULL && keydata == NULL) ## provider side
508 *
509 * fully assigned:
510 *
511 * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
512 * || (keymgmt != NULL && keydata != NULL) ## provider side
513 *
514 * The easiest way to detect a legacy key is:
515 *
516 * keymgmt == NULL && type != EVP_PKEY_NONE
517 *
518 * The easiest way to detect a provider side key is:
519 *
520 * keymgmt != NULL
521 */
522 #define evp_pkey_is_blank(pk) \
523 ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
524 #define evp_pkey_is_typed(pk) \
525 ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
526 #define evp_pkey_is_assigned(pk) \
527 ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
528 #define evp_pkey_is_legacy(pk) \
529 ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
530 #define evp_pkey_is_provided(pk) \
531 ((pk)->keymgmt != NULL)
532
533 struct evp_pkey_st {
534 /* == Legacy attributes == */
535 int type;
536 int save_type;
537
538 # ifndef FIPS_MODULE
539 /*
540 * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
541 * a pointer to a low level key and possibly a pointer to an engine.
542 */
543 const EVP_PKEY_ASN1_METHOD *ameth;
544 ENGINE *engine;
545 ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
546 union {
547 void *ptr;
548 # ifndef OPENSSL_NO_RSA
549 struct rsa_st *rsa; /* RSA */
550 # endif
551 # ifndef OPENSSL_NO_DSA
552 struct dsa_st *dsa; /* DSA */
553 # endif
554 # ifndef OPENSSL_NO_DH
555 struct dh_st *dh; /* DH */
556 # endif
557 # ifndef OPENSSL_NO_EC
558 struct ec_key_st *ec; /* ECC */
559 ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
560 # endif
561 } pkey;
562 # endif
563
564 /* == Common attributes == */
565 CRYPTO_REF_COUNT references;
566 CRYPTO_RWLOCK *lock;
567 STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
568 int save_parameters;
569 #ifndef FIPS_MODULE
570 CRYPTO_EX_DATA ex_data;
571 #endif
572
573 /* == Provider attributes == */
574
575 /*
576 * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
577 * and a pointer to the provider side key data. This is never used at
578 * the same time as the legacy key data above.
579 */
580 EVP_KEYMGMT *keymgmt;
581 void *keydata;
582 /*
583 * If any libcrypto code does anything that may modify the keydata
584 * contents, this dirty counter must be incremented.
585 */
586 size_t dirty_cnt;
587
588 /*
589 * To support transparent execution of operation in backends other
590 * than the "origin" key, we support transparent export/import to
591 * those providers, and maintain a cache of the imported keydata,
592 * so we don't need to redo the export/import every time we perform
593 * the same operation in that same provider.
594 * This requires that the "origin" backend (whether it's a legacy or a
595 * provider "origin") implements exports, and that the target provider
596 * has an EVP_KEYMGMT that implements import.
597 *
598 * The cache limit is set at 10 different providers using the same
599 * "origin". It's probably over the top, but is preferable to too
600 * few.
601 */
602 struct {
603 EVP_KEYMGMT *keymgmt;
604 void *keydata;
605 } operation_cache[10];
606 /*
607 * We keep a copy of that "origin"'s dirty count, so we know if the
608 * operation cache needs flushing.
609 */
610 size_t dirty_cnt_copy;
611
612 /* Cache of key object information */
613 struct {
614 int bits;
615 int security_bits;
616 int size;
617 } cache;
618 } /* EVP_PKEY */ ;
619
620 #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
621 ((ctx)->operation == EVP_PKEY_OP_SIGN \
622 || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \
623 || (ctx)->operation == EVP_PKEY_OP_VERIFY \
624 || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \
625 || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER)
626
627 #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
628 ((ctx)->operation == EVP_PKEY_OP_DERIVE)
629
630 #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
631 ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
632 || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
633
634 #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
635 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
636 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
637
638 void openssl_add_all_ciphers_int(void);
639 void openssl_add_all_digests_int(void);
640 void evp_cleanup_int(void);
641 void evp_app_cleanup_int(void);
642 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
643 EVP_KEYMGMT **keymgmt,
644 const char *propquery);
645 #ifndef FIPS_MODULE
646 int evp_pkey_downgrade(EVP_PKEY *pk);
647 void evp_pkey_free_legacy(EVP_PKEY *x);
648 #endif
649
650 /*
651 * KEYMGMT utility functions
652 */
653
654 /*
655 * Key import structure and helper function, to be used as an export callback
656 */
657 struct evp_keymgmt_util_try_import_data_st {
658 EVP_KEYMGMT *keymgmt;
659 void *keydata;
660
661 int selection;
662 };
663 int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
664 int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
665 void *keydata);
666 EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
667
668 void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
669 size_t evp_keymgmt_util_find_operation_cache_index(EVP_PKEY *pk,
670 EVP_KEYMGMT *keymgmt);
671 void evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk);
672 int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, size_t index,
673 EVP_KEYMGMT *keymgmt, void *keydata);
674 void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
675 void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
676 int selection, const OSSL_PARAM params[]);
677 int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
678 int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
679 int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
680 void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
681 void *genctx, OSSL_CALLBACK *cb, void *cbarg);
682 int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
683 void *keydata,
684 char *mdname, size_t mdname_sz);
685
686 /*
687 * KEYMGMT provider interface functions
688 */
689 void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
690 void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
691 int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
692 void *keydata, OSSL_PARAM params[]);
693 int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
694 void *keydata, const OSSL_PARAM params[]);
695 void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection);
696 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
697 void *template);
698 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
699 const OSSL_PARAM params[]);
700 void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
701 OSSL_CALLBACK *cb, void *cbarg);
702 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
703
704 void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
705 const void *objref, size_t objref_sz);
706
707 int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
708 int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
709 int selection);
710 int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
711 const void *keydata1, const void *keydata2,
712 int selection);
713
714 int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
715 int selection, const OSSL_PARAM params[]);
716 const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
717 int selection);
718 int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
719 int selection, OSSL_CALLBACK *param_cb, void *cbarg);
720 const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
721 int selection);
722 int evp_keymgmt_copy(const EVP_KEYMGMT *keymgmt,
723 void *keydata_to, const void *keydata_from,
724 int selection);
725
726 /* Pulling defines out of C source files */
727
728 #define EVP_RC4_KEY_SIZE 16
729 #ifndef TLS1_1_VERSION
730 # define TLS1_1_VERSION 0x0302
731 #endif
732
733 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
734
735 /* EVP_ENCODE_CTX flags */
736 /* Don't generate new lines when encoding */
737 #define EVP_ENCODE_CTX_NO_NEWLINES 1
738 /* Use the SRP base64 alphabet instead of the standard one */
739 #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
740
741 const EVP_CIPHER *evp_get_cipherbyname_ex(OPENSSL_CTX *libctx, const char *name);
742 const EVP_MD *evp_get_digestbyname_ex(OPENSSL_CTX *libctx, const char *name);
743
744 int pkcs5_pbkdf2_hmac_with_libctx(const char *pass, int passlen,
745 const unsigned char *salt, int saltlen,
746 int iter, const EVP_MD *digest, int keylen,
747 unsigned char *out,
748 OPENSSL_CTX *libctx, const char *propq);
749
750 #ifndef FIPS_MODULE
751 /*
752 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
753 *
754 * Return 1 on success, 0 or negative for errors.
755 *
756 * In particular they return -2 if any of the params is not supported.
757 *
758 * They are not available in FIPS_MODULE as they depend on
759 * - EVP_PKEY_CTX_{get,set}_params()
760 * - EVP_PKEY_CTX_{gettable,settable}_params()
761 *
762 */
763 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
764 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
765
766 EVP_MD_CTX *evp_md_ctx_new_with_libctx(EVP_PKEY *pkey,
767 const ASN1_OCTET_STRING *id,
768 OPENSSL_CTX *libctx, const char *propq);
769 #endif /* !defined(FIPS_MODULE) */
770 void evp_method_store_flush(OPENSSL_CTX *libctx);
771 int evp_set_default_properties_int(OPENSSL_CTX *libctx, const char *propq,
772 int loadconfig);