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