Use read/write locking on Windows
[openssl.git] / crypto / evp / evp_local.h
1 /*
2  * Copyright 2000-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 /* EVP_MD_CTX related stuff */
11
12 #include <openssl/core_dispatch.h>
13 #include "internal/refcount.h"
14
15 #define EVP_CTRL_RET_UNSUPPORTED -1
16
17
18 struct evp_md_ctx_st {
19     const EVP_MD *reqdigest;    /* The original requested digest */
20     const EVP_MD *digest;
21     ENGINE *engine;             /* functional reference if 'digest' is
22                                  * ENGINE-provided */
23     unsigned long flags;
24     void *md_data;
25     /* Public key context for sign/verify */
26     EVP_PKEY_CTX *pctx;
27     /* Update function: usually copied from EVP_MD */
28     int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
29
30     /* Provider ctx */
31     void *provctx;
32     EVP_MD *fetched_digest;
33 } /* EVP_MD_CTX */ ;
34
35 struct evp_cipher_ctx_st {
36     const EVP_CIPHER *cipher;
37     ENGINE *engine;             /* functional reference if 'cipher' is
38                                  * ENGINE-provided */
39     int encrypt;                /* encrypt or decrypt */
40     int buf_len;                /* number we have left */
41     unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
42     unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
43     unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
44     int num;                    /* used by cfb/ofb/ctr mode */
45     /* FIXME: Should this even exist? It appears unused */
46     void *app_data;             /* application stuff */
47     int key_len;                /* May change for variable length cipher */
48     unsigned long flags;        /* Various flags */
49     void *cipher_data;          /* per EVP data */
50     int final_used;
51     int block_mask;
52     unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
53
54     /* Provider ctx */
55     void *provctx;
56     EVP_CIPHER *fetched_cipher;
57 } /* EVP_CIPHER_CTX */ ;
58
59 struct evp_mac_ctx_st {
60     EVP_MAC *meth;               /* Method structure */
61     void *data;                  /* Individual method data */
62 } /* EVP_MAC_CTX */;
63
64 struct evp_kdf_ctx_st {
65     EVP_KDF *meth;              /* Method structure */
66     void *data;                 /* Algorithm-specific data */
67 } /* EVP_KDF_CTX */ ;
68
69 struct evp_rand_ctx_st {
70     EVP_RAND *meth;             /* Method structure */
71     void *data;                 /* Algorithm-specific data */
72     EVP_RAND_CTX *parent;       /* Parent EVP_RAND or NULL if none */
73     CRYPTO_REF_COUNT refcnt;    /* Context reference count */
74     CRYPTO_RWLOCK *refcnt_lock;
75 } /* EVP_RAND_CTX */ ;
76
77 struct evp_keymgmt_st {
78     int id;                      /* libcrypto internal */
79
80     int name_id;
81     OSSL_PROVIDER *prov;
82     CRYPTO_REF_COUNT refcnt;
83     CRYPTO_RWLOCK *lock;
84
85     /* Constructor(s), destructor, information */
86     OSSL_FUNC_keymgmt_new_fn *new;
87     OSSL_FUNC_keymgmt_free_fn *free;
88     OSSL_FUNC_keymgmt_get_params_fn *get_params;
89     OSSL_FUNC_keymgmt_gettable_params_fn *gettable_params;
90     OSSL_FUNC_keymgmt_set_params_fn *set_params;
91     OSSL_FUNC_keymgmt_settable_params_fn *settable_params;
92
93     /* Generation, a complex constructor */
94     OSSL_FUNC_keymgmt_gen_init_fn *gen_init;
95     OSSL_FUNC_keymgmt_gen_set_template_fn *gen_set_template;
96     OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params;
97     OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params;
98     OSSL_FUNC_keymgmt_gen_fn *gen;
99     OSSL_FUNC_keymgmt_gen_cleanup_fn *gen_cleanup;
100
101     OSSL_FUNC_keymgmt_load_fn *load;
102
103     /* Key object checking */
104     OSSL_FUNC_keymgmt_query_operation_name_fn *query_operation_name;
105     OSSL_FUNC_keymgmt_has_fn *has;
106     OSSL_FUNC_keymgmt_validate_fn *validate;
107     OSSL_FUNC_keymgmt_match_fn *match;
108
109     /* Import and export routines */
110     OSSL_FUNC_keymgmt_import_fn *import;
111     OSSL_FUNC_keymgmt_import_types_fn *import_types;
112     OSSL_FUNC_keymgmt_export_fn *export;
113     OSSL_FUNC_keymgmt_export_types_fn *export_types;
114     OSSL_FUNC_keymgmt_copy_fn *copy;
115 } /* EVP_KEYMGMT */ ;
116
117 struct evp_keyexch_st {
118     int name_id;
119     OSSL_PROVIDER *prov;
120     CRYPTO_REF_COUNT refcnt;
121     CRYPTO_RWLOCK *lock;
122
123     OSSL_FUNC_keyexch_newctx_fn *newctx;
124     OSSL_FUNC_keyexch_init_fn *init;
125     OSSL_FUNC_keyexch_set_peer_fn *set_peer;
126     OSSL_FUNC_keyexch_derive_fn *derive;
127     OSSL_FUNC_keyexch_freectx_fn *freectx;
128     OSSL_FUNC_keyexch_dupctx_fn *dupctx;
129     OSSL_FUNC_keyexch_set_ctx_params_fn *set_ctx_params;
130     OSSL_FUNC_keyexch_settable_ctx_params_fn *settable_ctx_params;
131     OSSL_FUNC_keyexch_get_ctx_params_fn *get_ctx_params;
132     OSSL_FUNC_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
133 } /* EVP_KEYEXCH */;
134
135 struct evp_signature_st {
136     int name_id;
137     OSSL_PROVIDER *prov;
138     CRYPTO_REF_COUNT refcnt;
139     CRYPTO_RWLOCK *lock;
140
141     OSSL_FUNC_signature_newctx_fn *newctx;
142     OSSL_FUNC_signature_sign_init_fn *sign_init;
143     OSSL_FUNC_signature_sign_fn *sign;
144     OSSL_FUNC_signature_verify_init_fn *verify_init;
145     OSSL_FUNC_signature_verify_fn *verify;
146     OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init;
147     OSSL_FUNC_signature_verify_recover_fn *verify_recover;
148     OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init;
149     OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update;
150     OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final;
151     OSSL_FUNC_signature_digest_sign_fn *digest_sign;
152     OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init;
153     OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update;
154     OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final;
155     OSSL_FUNC_signature_digest_verify_fn *digest_verify;
156     OSSL_FUNC_signature_freectx_fn *freectx;
157     OSSL_FUNC_signature_dupctx_fn *dupctx;
158     OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params;
159     OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params;
160     OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params;
161     OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params;
162     OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params;
163     OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
164     OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params;
165     OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
166 } /* EVP_SIGNATURE */;
167
168 struct evp_asym_cipher_st {
169     int name_id;
170     OSSL_PROVIDER *prov;
171     CRYPTO_REF_COUNT refcnt;
172     CRYPTO_RWLOCK *lock;
173
174     OSSL_FUNC_asym_cipher_newctx_fn *newctx;
175     OSSL_FUNC_asym_cipher_encrypt_init_fn *encrypt_init;
176     OSSL_FUNC_asym_cipher_encrypt_fn *encrypt;
177     OSSL_FUNC_asym_cipher_decrypt_init_fn *decrypt_init;
178     OSSL_FUNC_asym_cipher_decrypt_fn *decrypt;
179     OSSL_FUNC_asym_cipher_freectx_fn *freectx;
180     OSSL_FUNC_asym_cipher_dupctx_fn *dupctx;
181     OSSL_FUNC_asym_cipher_get_ctx_params_fn *get_ctx_params;
182     OSSL_FUNC_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
183     OSSL_FUNC_asym_cipher_set_ctx_params_fn *set_ctx_params;
184     OSSL_FUNC_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
185 } /* EVP_ASYM_CIPHER */;
186
187 struct evp_kem_st {
188     int name_id;
189     OSSL_PROVIDER *prov;
190     CRYPTO_REF_COUNT refcnt;
191     CRYPTO_RWLOCK *lock;
192
193     OSSL_FUNC_kem_newctx_fn *newctx;
194     OSSL_FUNC_kem_encapsulate_init_fn *encapsulate_init;
195     OSSL_FUNC_kem_encapsulate_fn *encapsulate;
196     OSSL_FUNC_kem_decapsulate_init_fn *decapsulate_init;
197     OSSL_FUNC_kem_decapsulate_fn *decapsulate;
198     OSSL_FUNC_kem_freectx_fn *freectx;
199     OSSL_FUNC_kem_dupctx_fn *dupctx;
200     OSSL_FUNC_kem_get_ctx_params_fn *get_ctx_params;
201     OSSL_FUNC_kem_gettable_ctx_params_fn *gettable_ctx_params;
202     OSSL_FUNC_kem_set_ctx_params_fn *set_ctx_params;
203     OSSL_FUNC_kem_settable_ctx_params_fn *settable_ctx_params;
204 } /* EVP_KEM */;
205
206 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
207                              int passlen, ASN1_TYPE *param,
208                              const EVP_CIPHER *c, const EVP_MD *md,
209                              int en_de);
210
211 struct evp_Encode_Ctx_st {
212     /* number saved in a partial encode/decode */
213     int num;
214     /*
215      * The length is either the output line length (in input bytes) or the
216      * shortest input line length that is ok.  Once decoding begins, the
217      * length is adjusted up each time a longer line is decoded
218      */
219     int length;
220     /* data to encode */
221     unsigned char enc_data[80];
222     /* number read on current line */
223     int line_num;
224     unsigned int flags;
225 };
226
227 typedef struct evp_pbe_st EVP_PBE_CTL;
228 DEFINE_STACK_OF(EVP_PBE_CTL)
229
230 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
231
232 #include <openssl/types.h>
233 #include <openssl/core.h>
234
235 void *evp_generic_fetch(OSSL_LIB_CTX *ctx, int operation_id,
236                         const char *name, const char *properties,
237                         void *(*new_method)(int name_id,
238                                             const OSSL_DISPATCH *fns,
239                                             OSSL_PROVIDER *prov),
240                         int (*up_ref_method)(void *),
241                         void (*free_method)(void *));
242 void *evp_generic_fetch_by_number(OSSL_LIB_CTX *ctx, int operation_id,
243                                   int name_id, const char *properties,
244                                   void *(*new_method)(int name_id,
245                                                       const OSSL_DISPATCH *fns,
246                                                       OSSL_PROVIDER *prov),
247                                   int (*up_ref_method)(void *),
248                                   void (*free_method)(void *));
249 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
250                         void (*user_fn)(void *method, void *arg),
251                         void *user_arg,
252                         void *(*new_method)(int name_id,
253                                             const OSSL_DISPATCH *fns,
254                                             OSSL_PROVIDER *prov),
255                         void (*free_method)(void *));
256
257 /* Internal fetchers for method types that are to be combined with others */
258 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
259                                          const char *properties);
260
261 /* Internal structure constructors for fetched methods */
262 EVP_MD *evp_md_new(void);
263 EVP_CIPHER *evp_cipher_new(void);
264
265 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
266                                     evp_cipher_aead_asn1_params *asn1_params);
267 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
268                                     evp_cipher_aead_asn1_params *asn1_params);
269
270 /* Helper functions to avoid duplicating code */
271
272 /*
273  * These methods implement different ways to pass a params array to the
274  * provider.  They will return one of these values:
275  *
276  * -2 if the method doesn't come from a provider
277  *    (evp_do_param will return this to the called)
278  * -1 if the provider doesn't offer the desired function
279  *    (evp_do_param will raise an error and return 0)
280  * or the return value from the desired function
281  *    (evp_do_param will return it to the caller)
282  */
283 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
284 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
285                               OSSL_PARAM params[]);
286 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
287                               OSSL_PARAM params[]);
288 int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
289 int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
290                             OSSL_PARAM params[]);
291 int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
292                             OSSL_PARAM params[]);
293
294 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
295
296 #define M_check_autoarg(ctx, arg, arglen, err) \
297     if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) {           \
298         size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey);         \
299                                                                   \
300         if (pksize == 0) {                                        \
301             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
302             return 0;                                             \
303         }                                                         \
304         if (arg == NULL) {                                        \
305             *arglen = pksize;                                     \
306             return 1;                                             \
307         }                                                         \
308         if (*arglen < pksize) {                                   \
309             ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
310             return 0;                                             \
311         }                                                         \
312     }
313
314 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
315
316 /* OSSL_PROVIDER * is only used to get the library context */
317 const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id);
318 int evp_is_a(OSSL_PROVIDER *prov, int number,
319              const char *legacy_name, const char *name);
320 int evp_names_do_all(OSSL_PROVIDER *prov, int number,
321                      void (*fn)(const char *name, void *data),
322                      void *data);
323 int evp_cipher_cache_constants(EVP_CIPHER *cipher);