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