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