]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_local.h
Move discovery of the legacy alg type into the keymgmt
[thirdparty/openssl.git] / crypto / evp / evp_local.h
CommitLineData
0f113f3e 1/*
da1c088f 2 * Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
5da2f69f 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
5da2f69f
DSH
8 */
9
23c48d94 10#include <openssl/core_dispatch.h>
15dfa092 11#include "internal/refcount.h"
ff64702b 12
e870791a
SL
13#define EVP_CTRL_RET_UNSUPPORTED -1
14
15
7638370c 16struct evp_md_ctx_st {
b7c913c8 17 const EVP_MD *reqdigest; /* The original requested digest */
7638370c
RL
18 const EVP_MD *digest;
19 ENGINE *engine; /* functional reference if 'digest' is
20 * ENGINE-provided */
21 unsigned long flags;
22 void *md_data;
23 /* Public key context for sign/verify */
24 EVP_PKEY_CTX *pctx;
25 /* Update function: usually copied from EVP_MD */
26 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
8c8cf0d9 27
7c14d0c1
SL
28 /*
29 * Opaque ctx returned from a providers digest algorithm implementation
30 * OSSL_FUNC_digest_newctx()
31 */
32 void *algctx;
8c8cf0d9 33 EVP_MD *fetched_digest;
7638370c
RL
34} /* EVP_MD_CTX */ ;
35
8baf9968
RL
36struct evp_cipher_ctx_st {
37 const EVP_CIPHER *cipher;
38 ENGINE *engine; /* functional reference if 'cipher' is
39 * ENGINE-provided */
40 int encrypt; /* encrypt or decrypt */
41 int buf_len; /* number we have left */
42 unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
43 unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
44 unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
45 int num; /* used by cfb/ofb/ctr mode */
46 /* FIXME: Should this even exist? It appears unused */
47 void *app_data; /* application stuff */
48 int key_len; /* May change for variable length cipher */
b30b45b7 49 int iv_len; /* IV length */
8baf9968
RL
50 unsigned long flags; /* Various flags */
51 void *cipher_data; /* per EVP data */
52 int final_used;
53 int block_mask;
54 unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
df05f2ce 55
7c14d0c1
SL
56 /*
57 * Opaque ctx returned from a providers cipher algorithm implementation
58 * OSSL_FUNC_cipher_newctx()
59 */
60 void *algctx;
df05f2ce 61 EVP_CIPHER *fetched_cipher;
8baf9968
RL
62} /* EVP_CIPHER_CTX */ ;
63
567db2c1 64struct evp_mac_ctx_st {
e74bd290 65 EVP_MAC *meth; /* Method structure */
7c14d0c1
SL
66 /*
67 * Opaque ctx returned from a providers MAC algorithm implementation
68 * OSSL_FUNC_mac_newctx()
69 */
70 void *algctx;
567db2c1
RL
71} /* EVP_MAC_CTX */;
72
5a285add 73struct evp_kdf_ctx_st {
fb9e6dd6 74 EVP_KDF *meth; /* Method structure */
7c14d0c1
SL
75 /*
76 * Opaque ctx returned from a providers KDF algorithm implementation
77 * OSSL_FUNC_kdf_newctx()
78 */
79 void *algctx;
5a285add
DM
80} /* EVP_KDF_CTX */ ;
81
15dfa092
P
82struct evp_rand_ctx_st {
83 EVP_RAND *meth; /* Method structure */
7c14d0c1
SL
84 /*
85 * Opaque ctx returned from a providers rand algorithm implementation
86 * OSSL_FUNC_rand_newctx()
87 */
88 void *algctx;
4640cd00
P
89 EVP_RAND_CTX *parent; /* Parent EVP_RAND or NULL if none */
90 CRYPTO_REF_COUNT refcnt; /* Context reference count */
91 CRYPTO_RWLOCK *refcnt_lock;
15dfa092
P
92} /* EVP_RAND_CTX */ ;
93
a94a3e0d
RL
94struct evp_keymgmt_st {
95 int id; /* libcrypto internal */
96
f7c16d48 97 int name_id;
8aa3781b
MC
98 /* NID for the legacy alg if there is one */
99 int legacy_alg;
6c9bc258 100 char *type_name;
309a78aa 101 const char *description;
a94a3e0d
RL
102 OSSL_PROVIDER *prov;
103 CRYPTO_REF_COUNT refcnt;
a94a3e0d 104
b305452f 105 /* Constructor(s), destructor, information */
363b1e5d
DMSP
106 OSSL_FUNC_keymgmt_new_fn *new;
107 OSSL_FUNC_keymgmt_free_fn *free;
108 OSSL_FUNC_keymgmt_get_params_fn *get_params;
109 OSSL_FUNC_keymgmt_gettable_params_fn *gettable_params;
110 OSSL_FUNC_keymgmt_set_params_fn *set_params;
111 OSSL_FUNC_keymgmt_settable_params_fn *settable_params;
6508e858 112
1a5632e0 113 /* Generation, a complex constructor */
363b1e5d
DMSP
114 OSSL_FUNC_keymgmt_gen_init_fn *gen_init;
115 OSSL_FUNC_keymgmt_gen_set_template_fn *gen_set_template;
116 OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params;
117 OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params;
118 OSSL_FUNC_keymgmt_gen_fn *gen;
119 OSSL_FUNC_keymgmt_gen_cleanup_fn *gen_cleanup;
1a5632e0 120
5dacb38c
RL
121 OSSL_FUNC_keymgmt_load_fn *load;
122
b305452f 123 /* Key object checking */
363b1e5d
DMSP
124 OSSL_FUNC_keymgmt_query_operation_name_fn *query_operation_name;
125 OSSL_FUNC_keymgmt_has_fn *has;
126 OSSL_FUNC_keymgmt_validate_fn *validate;
127 OSSL_FUNC_keymgmt_match_fn *match;
b305452f
RL
128
129 /* Import and export routines */
363b1e5d
DMSP
130 OSSL_FUNC_keymgmt_import_fn *import;
131 OSSL_FUNC_keymgmt_import_types_fn *import_types;
5e3b8450 132 OSSL_FUNC_keymgmt_import_types_ex_fn *import_types_ex;
363b1e5d
DMSP
133 OSSL_FUNC_keymgmt_export_fn *export;
134 OSSL_FUNC_keymgmt_export_types_fn *export_types;
5e3b8450 135 OSSL_FUNC_keymgmt_export_types_ex_fn *export_types_ex;
4a9fe33c 136 OSSL_FUNC_keymgmt_dup_fn *dup;
a94a3e0d
RL
137} /* EVP_KEYMGMT */ ;
138
ff64702b 139struct evp_keyexch_st {
f7c16d48 140 int name_id;
6c9bc258 141 char *type_name;
309a78aa 142 const char *description;
ff64702b
MC
143 OSSL_PROVIDER *prov;
144 CRYPTO_REF_COUNT refcnt;
ff64702b 145
363b1e5d
DMSP
146 OSSL_FUNC_keyexch_newctx_fn *newctx;
147 OSSL_FUNC_keyexch_init_fn *init;
148 OSSL_FUNC_keyexch_set_peer_fn *set_peer;
149 OSSL_FUNC_keyexch_derive_fn *derive;
150 OSSL_FUNC_keyexch_freectx_fn *freectx;
151 OSSL_FUNC_keyexch_dupctx_fn *dupctx;
152 OSSL_FUNC_keyexch_set_ctx_params_fn *set_ctx_params;
153 OSSL_FUNC_keyexch_settable_ctx_params_fn *settable_ctx_params;
154 OSSL_FUNC_keyexch_get_ctx_params_fn *get_ctx_params;
155 OSSL_FUNC_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
ff64702b
MC
156} /* EVP_KEYEXCH */;
157
dfcb5d29 158struct evp_signature_st {
f7c16d48 159 int name_id;
6c9bc258 160 char *type_name;
309a78aa 161 const char *description;
dfcb5d29
MC
162 OSSL_PROVIDER *prov;
163 CRYPTO_REF_COUNT refcnt;
dfcb5d29 164
363b1e5d
DMSP
165 OSSL_FUNC_signature_newctx_fn *newctx;
166 OSSL_FUNC_signature_sign_init_fn *sign_init;
167 OSSL_FUNC_signature_sign_fn *sign;
168 OSSL_FUNC_signature_verify_init_fn *verify_init;
169 OSSL_FUNC_signature_verify_fn *verify;
170 OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init;
171 OSSL_FUNC_signature_verify_recover_fn *verify_recover;
172 OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init;
173 OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update;
174 OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final;
175 OSSL_FUNC_signature_digest_sign_fn *digest_sign;
176 OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init;
177 OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update;
178 OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final;
179 OSSL_FUNC_signature_digest_verify_fn *digest_verify;
180 OSSL_FUNC_signature_freectx_fn *freectx;
181 OSSL_FUNC_signature_dupctx_fn *dupctx;
182 OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params;
183 OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params;
184 OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params;
185 OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params;
186 OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params;
187 OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
188 OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params;
189 OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
dfcb5d29
MC
190} /* EVP_SIGNATURE */;
191
2c938e2e
MC
192struct evp_asym_cipher_st {
193 int name_id;
6c9bc258 194 char *type_name;
309a78aa 195 const char *description;
2c938e2e
MC
196 OSSL_PROVIDER *prov;
197 CRYPTO_REF_COUNT refcnt;
2c938e2e 198
363b1e5d
DMSP
199 OSSL_FUNC_asym_cipher_newctx_fn *newctx;
200 OSSL_FUNC_asym_cipher_encrypt_init_fn *encrypt_init;
201 OSSL_FUNC_asym_cipher_encrypt_fn *encrypt;
202 OSSL_FUNC_asym_cipher_decrypt_init_fn *decrypt_init;
203 OSSL_FUNC_asym_cipher_decrypt_fn *decrypt;
204 OSSL_FUNC_asym_cipher_freectx_fn *freectx;
205 OSSL_FUNC_asym_cipher_dupctx_fn *dupctx;
206 OSSL_FUNC_asym_cipher_get_ctx_params_fn *get_ctx_params;
207 OSSL_FUNC_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
208 OSSL_FUNC_asym_cipher_set_ctx_params_fn *set_ctx_params;
209 OSSL_FUNC_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
2c938e2e
MC
210} /* EVP_ASYM_CIPHER */;
211
80f4fd18
SL
212struct evp_kem_st {
213 int name_id;
6c9bc258 214 char *type_name;
309a78aa 215 const char *description;
80f4fd18
SL
216 OSSL_PROVIDER *prov;
217 CRYPTO_REF_COUNT refcnt;
80f4fd18
SL
218
219 OSSL_FUNC_kem_newctx_fn *newctx;
220 OSSL_FUNC_kem_encapsulate_init_fn *encapsulate_init;
221 OSSL_FUNC_kem_encapsulate_fn *encapsulate;
222 OSSL_FUNC_kem_decapsulate_init_fn *decapsulate_init;
223 OSSL_FUNC_kem_decapsulate_fn *decapsulate;
224 OSSL_FUNC_kem_freectx_fn *freectx;
225 OSSL_FUNC_kem_dupctx_fn *dupctx;
226 OSSL_FUNC_kem_get_ctx_params_fn *get_ctx_params;
227 OSSL_FUNC_kem_gettable_ctx_params_fn *gettable_ctx_params;
228 OSSL_FUNC_kem_set_ctx_params_fn *set_ctx_params;
229 OSSL_FUNC_kem_settable_ctx_params_fn *settable_ctx_params;
78c44b05 230 OSSL_FUNC_kem_auth_encapsulate_init_fn *auth_encapsulate_init;
231 OSSL_FUNC_kem_auth_decapsulate_init_fn *auth_decapsulate_init;
80f4fd18
SL
232} /* EVP_KEM */;
233
0f113f3e
MC
234int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
235 int passlen, ASN1_TYPE *param,
236 const EVP_CIPHER *c, const EVP_MD *md,
237 int en_de);
b536880c
JS
238int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
239 int passlen, ASN1_TYPE *param,
240 const EVP_CIPHER *c, const EVP_MD *md,
241 int en_de, OSSL_LIB_CTX *libctx, const char *propq);
a0be4fd1
RL
242
243struct evp_Encode_Ctx_st {
244 /* number saved in a partial encode/decode */
245 int num;
246 /*
247 * The length is either the output line length (in input bytes) or the
248 * shortest input line length that is ok. Once decoding begins, the
249 * length is adjusted up each time a longer line is decoded
250 */
251 int length;
252 /* data to encode */
253 unsigned char enc_data[80];
254 /* number read on current line */
255 int line_num;
c0804614 256 unsigned int flags;
a0be4fd1 257};
4a1f3f27
DSH
258
259typedef struct evp_pbe_st EVP_PBE_CTL;
260DEFINE_STACK_OF(EVP_PBE_CTL)
7141ba31 261
6d777689 262int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
c13d2ab4 263
50cd4768 264#include <openssl/types.h>
c13d2ab4
RL
265#include <openssl/core.h>
266
b4250010 267void *evp_generic_fetch(OSSL_LIB_CTX *ctx, int operation_id,
f7c16d48
RL
268 const char *name, const char *properties,
269 void *(*new_method)(int name_id,
309a78aa 270 const OSSL_ALGORITHM *algodef,
0ddf74bf 271 OSSL_PROVIDER *prov),
7c95390e 272 int (*up_ref_method)(void *),
0211740f 273 void (*free_method)(void *));
2fd3392c
RL
274void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
275 const char *name, const char *properties,
276 void *(*new_method)(int name_id,
277 const OSSL_ALGORITHM *algodef,
278 OSSL_PROVIDER *prov),
279 int (*up_ref_method)(void *),
280 void (*free_method)(void *));
793b0586
RL
281void evp_generic_do_all_prefetched(OSSL_LIB_CTX *libctx, int operation_id,
282 void (*user_fn)(void *method, void *arg),
283 void *user_arg);
b4250010 284void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
3d96a51c
RL
285 void (*user_fn)(void *method, void *arg),
286 void *user_arg,
f7c16d48 287 void *(*new_method)(int name_id,
309a78aa 288 const OSSL_ALGORITHM *algodef,
0ddf74bf 289 OSSL_PROVIDER *prov),
793b0586 290 int (*up_ref_method)(void *),
3d96a51c 291 void (*free_method)(void *));
13273237 292
f7c16d48 293/* Internal fetchers for method types that are to be combined with others */
b4250010 294EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
f7c16d48 295 const char *properties);
ff778146
RL
296EVP_SIGNATURE *evp_signature_fetch_from_prov(OSSL_PROVIDER *prov,
297 const char *name,
298 const char *properties);
299EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
300 const char *name,
301 const char *properties);
302EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
303 const char *name,
304 const char *properties);
305EVP_KEM *evp_kem_fetch_from_prov(OSSL_PROVIDER *prov,
306 const char *name,
307 const char *properties);
f7c16d48 308
3fd70262
RL
309/* Internal structure constructors for fetched methods */
310EVP_MD *evp_md_new(void);
550f974a 311EVP_CIPHER *evp_cipher_new(void);
3fd70262 312
924663c3
JZ
313int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
314 evp_cipher_aead_asn1_params *asn1_params);
315int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
316 evp_cipher_aead_asn1_params *asn1_params);
317
13273237
RL
318/* Helper functions to avoid duplicating code */
319
320/*
459b15d4 321 * These methods implement different ways to pass a params array to the
13273237
RL
322 * provider. They will return one of these values:
323 *
324 * -2 if the method doesn't come from a provider
325 * (evp_do_param will return this to the called)
326 * -1 if the provider doesn't offer the desired function
327 * (evp_do_param will raise an error and return 0)
328 * or the return value from the desired function
329 * (evp_do_param will return it to the caller)
330 */
459b15d4
SL
331int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
332int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
13273237 333 OSSL_PARAM params[]);
459b15d4 334int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
13273237 335 OSSL_PARAM params[]);
6a3b7c68
RL
336int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
337int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
338 OSSL_PARAM params[]);
339int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
340 OSSL_PARAM params[]);
ff64702b
MC
341
342OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
343
344#define M_check_autoarg(ctx, arg, arglen, err) \
345 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
ed576acd 346 size_t pksize = (size_t)EVP_PKEY_get_size(ctx->pkey); \
ff64702b
MC
347 \
348 if (pksize == 0) { \
51ba9ebd 349 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
ff64702b
MC
350 return 0; \
351 } \
352 if (arg == NULL) { \
353 *arglen = pksize; \
354 return 1; \
355 } \
356 if (*arglen < pksize) { \
51ba9ebd 357 ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
ff64702b
MC
358 return 0; \
359 } \
360 }
864b89ce
MC
361
362void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
f6c95e46
RS
363void evp_cipher_free_int(EVP_CIPHER *md);
364void evp_md_free_int(EVP_MD *md);
f7c16d48
RL
365
366/* OSSL_PROVIDER * is only used to get the library context */
e4a1d023
RL
367int evp_is_a(OSSL_PROVIDER *prov, int number,
368 const char *legacy_name, const char *name);
d84f5515
MC
369int evp_names_do_all(OSSL_PROVIDER *prov, int number,
370 void (*fn)(const char *name, void *data),
371 void *data);
3c957bcd 372int evp_cipher_cache_constants(EVP_CIPHER *cipher);