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