]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_local.h
rand: core APIs for provider friendly random.
[thirdparty/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_numbers.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 seed source */
73 size_t max_request; /* Cached: maximum number of bytes generated */
74 unsigned int strength; /* Cache: bit strenght of generator */
75 } /* EVP_RAND_CTX */ ;
76
77 struct evp_rand_st {
78 OSSL_PROVIDER *prov;
79 int name_id;
80 CRYPTO_REF_COUNT refcnt;
81 CRYPTO_RWLOCK *lock;
82
83 const OSSL_DISPATCH *dispatch;
84 OSSL_OP_rand_newctx_fn *newctx;
85 OSSL_OP_rand_freectx_fn *freectx;
86 OSSL_OP_rand_instantiate_fn *instantiate;
87 OSSL_OP_rand_uninstantiate_fn *uninstantiate;
88 OSSL_OP_rand_generate_fn *generate;
89 OSSL_OP_rand_reseed_fn *reseed;
90 OSSL_OP_rand_nonce_fn *nonce;
91 OSSL_OP_rand_set_callbacks_fn *set_callbacks;
92 OSSL_OP_rand_enable_locking_fn *enable_prov_locking;
93 OSSL_OP_rand_lock_fn *prov_lock;
94 OSSL_OP_rand_unlock_fn *prov_unlock;
95 OSSL_OP_rand_gettable_params_fn *gettable_params;
96 OSSL_OP_rand_gettable_ctx_params_fn *gettable_ctx_params;
97 OSSL_OP_rand_settable_ctx_params_fn *settable_ctx_params;
98 OSSL_OP_rand_get_params_fn *get_params;
99 OSSL_OP_rand_get_ctx_params_fn *get_ctx_params;
100 OSSL_OP_rand_set_ctx_params_fn *set_ctx_params;
101 OSSL_OP_rand_verify_zeroization_fn *verify_zeroization;
102 } /* EVP_RAND */ ;
103
104 struct evp_keymgmt_st {
105 int id; /* libcrypto internal */
106
107 int name_id;
108 OSSL_PROVIDER *prov;
109 CRYPTO_REF_COUNT refcnt;
110 CRYPTO_RWLOCK *lock;
111
112 /* Constructor(s), destructor, information */
113 OSSL_OP_keymgmt_new_fn *new;
114 OSSL_OP_keymgmt_free_fn *free;
115 OSSL_OP_keymgmt_get_params_fn *get_params;
116 OSSL_OP_keymgmt_gettable_params_fn *gettable_params;
117 OSSL_OP_keymgmt_set_params_fn *set_params;
118 OSSL_OP_keymgmt_settable_params_fn *settable_params;
119
120 /* Generation, a complex constructor */
121 OSSL_OP_keymgmt_gen_init_fn *gen_init;
122 OSSL_OP_keymgmt_gen_set_template_fn *gen_set_template;
123 OSSL_OP_keymgmt_gen_set_params_fn *gen_set_params;
124 OSSL_OP_keymgmt_gen_settable_params_fn *gen_settable_params;
125 OSSL_OP_keymgmt_gen_fn *gen;
126 OSSL_OP_keymgmt_gen_cleanup_fn *gen_cleanup;
127
128 /* Key object checking */
129 OSSL_OP_keymgmt_query_operation_name_fn *query_operation_name;
130 OSSL_OP_keymgmt_has_fn *has;
131 OSSL_OP_keymgmt_validate_fn *validate;
132 OSSL_OP_keymgmt_match_fn *match;
133
134 /* Import and export routines */
135 OSSL_OP_keymgmt_import_fn *import;
136 OSSL_OP_keymgmt_import_types_fn *import_types;
137 OSSL_OP_keymgmt_export_fn *export;
138 OSSL_OP_keymgmt_export_types_fn *export_types;
139 OSSL_OP_keymgmt_copy_fn *copy;
140 } /* EVP_KEYMGMT */ ;
141
142 struct evp_keyexch_st {
143 int name_id;
144 OSSL_PROVIDER *prov;
145 CRYPTO_REF_COUNT refcnt;
146 CRYPTO_RWLOCK *lock;
147
148 OSSL_OP_keyexch_newctx_fn *newctx;
149 OSSL_OP_keyexch_init_fn *init;
150 OSSL_OP_keyexch_set_peer_fn *set_peer;
151 OSSL_OP_keyexch_derive_fn *derive;
152 OSSL_OP_keyexch_freectx_fn *freectx;
153 OSSL_OP_keyexch_dupctx_fn *dupctx;
154 OSSL_OP_keyexch_set_ctx_params_fn *set_ctx_params;
155 OSSL_OP_keyexch_settable_ctx_params_fn *settable_ctx_params;
156 OSSL_OP_keyexch_get_ctx_params_fn *get_ctx_params;
157 OSSL_OP_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
158 } /* EVP_KEYEXCH */;
159
160 struct evp_signature_st {
161 int name_id;
162 OSSL_PROVIDER *prov;
163 CRYPTO_REF_COUNT refcnt;
164 CRYPTO_RWLOCK *lock;
165
166 OSSL_OP_signature_newctx_fn *newctx;
167 OSSL_OP_signature_sign_init_fn *sign_init;
168 OSSL_OP_signature_sign_fn *sign;
169 OSSL_OP_signature_verify_init_fn *verify_init;
170 OSSL_OP_signature_verify_fn *verify;
171 OSSL_OP_signature_verify_recover_init_fn *verify_recover_init;
172 OSSL_OP_signature_verify_recover_fn *verify_recover;
173 OSSL_OP_signature_digest_sign_init_fn *digest_sign_init;
174 OSSL_OP_signature_digest_sign_update_fn *digest_sign_update;
175 OSSL_OP_signature_digest_sign_final_fn *digest_sign_final;
176 OSSL_OP_signature_digest_sign_fn *digest_sign;
177 OSSL_OP_signature_digest_verify_init_fn *digest_verify_init;
178 OSSL_OP_signature_digest_verify_update_fn *digest_verify_update;
179 OSSL_OP_signature_digest_verify_final_fn *digest_verify_final;
180 OSSL_OP_signature_digest_verify_fn *digest_verify;
181 OSSL_OP_signature_freectx_fn *freectx;
182 OSSL_OP_signature_dupctx_fn *dupctx;
183 OSSL_OP_signature_get_ctx_params_fn *get_ctx_params;
184 OSSL_OP_signature_gettable_ctx_params_fn *gettable_ctx_params;
185 OSSL_OP_signature_set_ctx_params_fn *set_ctx_params;
186 OSSL_OP_signature_settable_ctx_params_fn *settable_ctx_params;
187 OSSL_OP_signature_get_ctx_md_params_fn *get_ctx_md_params;
188 OSSL_OP_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
189 OSSL_OP_signature_set_ctx_md_params_fn *set_ctx_md_params;
190 OSSL_OP_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
191 } /* EVP_SIGNATURE */;
192
193 struct evp_asym_cipher_st {
194 int name_id;
195 OSSL_PROVIDER *prov;
196 CRYPTO_REF_COUNT refcnt;
197 CRYPTO_RWLOCK *lock;
198
199 OSSL_OP_asym_cipher_newctx_fn *newctx;
200 OSSL_OP_asym_cipher_encrypt_init_fn *encrypt_init;
201 OSSL_OP_asym_cipher_encrypt_fn *encrypt;
202 OSSL_OP_asym_cipher_decrypt_init_fn *decrypt_init;
203 OSSL_OP_asym_cipher_decrypt_fn *decrypt;
204 OSSL_OP_asym_cipher_freectx_fn *freectx;
205 OSSL_OP_asym_cipher_dupctx_fn *dupctx;
206 OSSL_OP_asym_cipher_get_ctx_params_fn *get_ctx_params;
207 OSSL_OP_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
208 OSSL_OP_asym_cipher_set_ctx_params_fn *set_ctx_params;
209 OSSL_OP_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
210 } /* EVP_ASYM_CIPHER */;
211
212 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
213 int passlen, ASN1_TYPE *param,
214 const EVP_CIPHER *c, const EVP_MD *md,
215 int en_de);
216
217 struct evp_Encode_Ctx_st {
218 /* number saved in a partial encode/decode */
219 int num;
220 /*
221 * The length is either the output line length (in input bytes) or the
222 * shortest input line length that is ok. Once decoding begins, the
223 * length is adjusted up each time a longer line is decoded
224 */
225 int length;
226 /* data to encode */
227 unsigned char enc_data[80];
228 /* number read on current line */
229 int line_num;
230 unsigned int flags;
231 };
232
233 typedef struct evp_pbe_st EVP_PBE_CTL;
234 DEFINE_STACK_OF(EVP_PBE_CTL)
235
236 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
237
238 #include <openssl/types.h>
239 #include <openssl/core.h>
240
241 void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id,
242 const char *name, const char *properties,
243 void *(*new_method)(int name_id,
244 const OSSL_DISPATCH *fns,
245 OSSL_PROVIDER *prov),
246 int (*up_ref_method)(void *),
247 void (*free_method)(void *));
248 void *evp_generic_fetch_by_number(OPENSSL_CTX *ctx, int operation_id,
249 int name_id, const char *properties,
250 void *(*new_method)(int name_id,
251 const OSSL_DISPATCH *fns,
252 OSSL_PROVIDER *prov),
253 int (*up_ref_method)(void *),
254 void (*free_method)(void *));
255 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
256 void (*user_fn)(void *method, void *arg),
257 void *user_arg,
258 void *(*new_method)(int name_id,
259 const OSSL_DISPATCH *fns,
260 OSSL_PROVIDER *prov),
261 void (*free_method)(void *));
262
263 /* Internal fetchers for method types that are to be combined with others */
264 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id,
265 const char *properties);
266
267 /* Internal structure constructors for fetched methods */
268 EVP_MD *evp_md_new(void);
269 EVP_CIPHER *evp_cipher_new(void);
270
271 /* Helper functions to avoid duplicating code */
272
273 /*
274 * These methods implement different ways to pass a params array to the
275 * provider. They will return one of these values:
276 *
277 * -2 if the method doesn't come from a provider
278 * (evp_do_param will return this to the called)
279 * -1 if the provider doesn't offer the desired function
280 * (evp_do_param will raise an error and return 0)
281 * or the return value from the desired function
282 * (evp_do_param will return it to the caller)
283 */
284 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
285 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
286 OSSL_PARAM params[]);
287 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
288 OSSL_PARAM params[]);
289 int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
290 int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
291 OSSL_PARAM params[]);
292 int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
293 OSSL_PARAM params[]);
294
295 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
296
297 #define M_check_autoarg(ctx, arg, arglen, err) \
298 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
299 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
300 \
301 if (pksize == 0) { \
302 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
303 return 0; \
304 } \
305 if (arg == NULL) { \
306 *arglen = pksize; \
307 return 1; \
308 } \
309 if (*arglen < pksize) { \
310 ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
311 return 0; \
312 } \
313 }
314
315 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
316
317 /* OSSL_PROVIDER * is only used to get the library context */
318 const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id);
319 int evp_is_a(OSSL_PROVIDER *prov, int number,
320 const char *legacy_name, const char *name);
321 void evp_names_do_all(OSSL_PROVIDER *prov, int number,
322 void (*fn)(const char *name, void *data),
323 void *data);
324 int evp_cipher_cache_constants(EVP_CIPHER *cipher);