]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_local.h
Refactor evp_pkey_make_provided() to do legacy to provider export
[thirdparty/openssl.git] / crypto / evp / evp_local.h
CommitLineData
0f113f3e 1/*
c13d2ab4 2 * Copyright 2000-2019 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
7638370c
RL
10/* EVP_MD_CTX related stuff */
11
ff64702b
MC
12#include <openssl/core_numbers.h>
13
e870791a
SL
14#define EVP_CTRL_RET_UNSUPPORTED -1
15
16
7638370c 17struct evp_md_ctx_st {
b7c913c8 18 const EVP_MD *reqdigest; /* The original requested digest */
7638370c
RL
19 const EVP_MD *digest;
20 ENGINE *engine; /* functional reference if 'digest' is
21 * ENGINE-provided */
22 unsigned long flags;
23 void *md_data;
24 /* Public key context for sign/verify */
25 EVP_PKEY_CTX *pctx;
26 /* Update function: usually copied from EVP_MD */
27 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
8c8cf0d9
MC
28
29 /* Provider ctx */
30 void *provctx;
31 EVP_MD *fetched_digest;
7638370c
RL
32} /* EVP_MD_CTX */ ;
33
8baf9968
RL
34struct evp_cipher_ctx_st {
35 const EVP_CIPHER *cipher;
36 ENGINE *engine; /* functional reference if 'cipher' is
37 * ENGINE-provided */
38 int encrypt; /* encrypt or decrypt */
39 int buf_len; /* number we have left */
40 unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
41 unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
42 unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
43 int num; /* used by cfb/ofb/ctr mode */
44 /* FIXME: Should this even exist? It appears unused */
45 void *app_data; /* application stuff */
46 int key_len; /* May change for variable length cipher */
47 unsigned long flags; /* Various flags */
48 void *cipher_data; /* per EVP data */
49 int final_used;
50 int block_mask;
51 unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
df05f2ce
MC
52
53 /* Provider ctx */
54 void *provctx;
55 EVP_CIPHER *fetched_cipher;
8baf9968
RL
56} /* EVP_CIPHER_CTX */ ;
57
567db2c1 58struct evp_mac_ctx_st {
e74bd290 59 EVP_MAC *meth; /* Method structure */
567db2c1
RL
60 void *data; /* Individual method data */
61} /* EVP_MAC_CTX */;
62
5a285add 63struct evp_kdf_ctx_st {
fb9e6dd6
P
64 EVP_KDF *meth; /* Method structure */
65 void *data; /* Algorithm-specific data */
5a285add
DM
66} /* EVP_KDF_CTX */ ;
67
a94a3e0d
RL
68struct evp_keymgmt_st {
69 int id; /* libcrypto internal */
70
f7c16d48 71 int name_id;
a94a3e0d
RL
72 OSSL_PROVIDER *prov;
73 CRYPTO_REF_COUNT refcnt;
74 CRYPTO_RWLOCK *lock;
75
b305452f
RL
76 /* Constructor(s), destructor, information */
77 OSSL_OP_keymgmt_new_fn *new;
78 OSSL_OP_keymgmt_free_fn *free;
79 OSSL_OP_keymgmt_get_params_fn *get_params;
80 OSSL_OP_keymgmt_gettable_params_fn *gettable_params;
4fe54d67
NT
81 OSSL_OP_keymgmt_set_params_fn *set_params;
82 OSSL_OP_keymgmt_settable_params_fn *settable_params;
6508e858 83
b305452f 84 /* Key object checking */
e62a45b6 85 OSSL_OP_keymgmt_query_operation_name_fn *query_operation_name;
b305452f
RL
86 OSSL_OP_keymgmt_has_fn *has;
87 OSSL_OP_keymgmt_validate_fn *validate;
88
89 /* Import and export routines */
90 OSSL_OP_keymgmt_import_fn *import;
91 OSSL_OP_keymgmt_import_types_fn *import_types;
92 OSSL_OP_keymgmt_export_fn *export;
93 OSSL_OP_keymgmt_export_types_fn *export_types;
a94a3e0d
RL
94} /* EVP_KEYMGMT */ ;
95
ff64702b 96struct evp_keyexch_st {
f7c16d48 97 int name_id;
ff64702b
MC
98 OSSL_PROVIDER *prov;
99 CRYPTO_REF_COUNT refcnt;
100 CRYPTO_RWLOCK *lock;
101
102 OSSL_OP_keyexch_newctx_fn *newctx;
103 OSSL_OP_keyexch_init_fn *init;
104 OSSL_OP_keyexch_set_peer_fn *set_peer;
105 OSSL_OP_keyexch_derive_fn *derive;
106 OSSL_OP_keyexch_freectx_fn *freectx;
107 OSSL_OP_keyexch_dupctx_fn *dupctx;
9c45222d
MC
108 OSSL_OP_keyexch_set_ctx_params_fn *set_ctx_params;
109 OSSL_OP_keyexch_settable_ctx_params_fn *settable_ctx_params;
4fe54d67
NT
110 OSSL_OP_keyexch_get_ctx_params_fn *get_ctx_params;
111 OSSL_OP_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
ff64702b
MC
112} /* EVP_KEYEXCH */;
113
dfcb5d29 114struct evp_signature_st {
f7c16d48 115 int name_id;
dfcb5d29
MC
116 OSSL_PROVIDER *prov;
117 CRYPTO_REF_COUNT refcnt;
118 CRYPTO_RWLOCK *lock;
119
dfcb5d29
MC
120 OSSL_OP_signature_newctx_fn *newctx;
121 OSSL_OP_signature_sign_init_fn *sign_init;
122 OSSL_OP_signature_sign_fn *sign;
390acbeb
MC
123 OSSL_OP_signature_verify_init_fn *verify_init;
124 OSSL_OP_signature_verify_fn *verify;
125 OSSL_OP_signature_verify_recover_init_fn *verify_recover_init;
126 OSSL_OP_signature_verify_recover_fn *verify_recover;
d8c98d79
MC
127 OSSL_OP_signature_digest_sign_init_fn *digest_sign_init;
128 OSSL_OP_signature_digest_sign_update_fn *digest_sign_update;
129 OSSL_OP_signature_digest_sign_final_fn *digest_sign_final;
130 OSSL_OP_signature_digest_verify_init_fn *digest_verify_init;
131 OSSL_OP_signature_digest_verify_update_fn *digest_verify_update;
132 OSSL_OP_signature_digest_verify_final_fn *digest_verify_final;
dfcb5d29
MC
133 OSSL_OP_signature_freectx_fn *freectx;
134 OSSL_OP_signature_dupctx_fn *dupctx;
9c45222d
MC
135 OSSL_OP_signature_get_ctx_params_fn *get_ctx_params;
136 OSSL_OP_signature_gettable_ctx_params_fn *gettable_ctx_params;
137 OSSL_OP_signature_set_ctx_params_fn *set_ctx_params;
138 OSSL_OP_signature_settable_ctx_params_fn *settable_ctx_params;
d8c98d79
MC
139 OSSL_OP_signature_get_ctx_md_params_fn *get_ctx_md_params;
140 OSSL_OP_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
141 OSSL_OP_signature_set_ctx_md_params_fn *set_ctx_md_params;
142 OSSL_OP_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
dfcb5d29
MC
143} /* EVP_SIGNATURE */;
144
2c938e2e
MC
145struct evp_asym_cipher_st {
146 int name_id;
147 OSSL_PROVIDER *prov;
148 CRYPTO_REF_COUNT refcnt;
149 CRYPTO_RWLOCK *lock;
150
151 OSSL_OP_asym_cipher_newctx_fn *newctx;
152 OSSL_OP_asym_cipher_encrypt_init_fn *encrypt_init;
153 OSSL_OP_asym_cipher_encrypt_fn *encrypt;
154 OSSL_OP_asym_cipher_decrypt_init_fn *decrypt_init;
155 OSSL_OP_asym_cipher_decrypt_fn *decrypt;
156 OSSL_OP_asym_cipher_freectx_fn *freectx;
157 OSSL_OP_asym_cipher_dupctx_fn *dupctx;
158 OSSL_OP_asym_cipher_get_ctx_params_fn *get_ctx_params;
159 OSSL_OP_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
160 OSSL_OP_asym_cipher_set_ctx_params_fn *set_ctx_params;
161 OSSL_OP_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
162} /* EVP_ASYM_CIPHER */;
163
0f113f3e
MC
164int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
165 int passlen, ASN1_TYPE *param,
166 const EVP_CIPHER *c, const EVP_MD *md,
167 int en_de);
a0be4fd1
RL
168
169struct evp_Encode_Ctx_st {
170 /* number saved in a partial encode/decode */
171 int num;
172 /*
173 * The length is either the output line length (in input bytes) or the
174 * shortest input line length that is ok. Once decoding begins, the
175 * length is adjusted up each time a longer line is decoded
176 */
177 int length;
178 /* data to encode */
179 unsigned char enc_data[80];
180 /* number read on current line */
181 int line_num;
c0804614 182 unsigned int flags;
a0be4fd1 183};
4a1f3f27
DSH
184
185typedef struct evp_pbe_st EVP_PBE_CTL;
186DEFINE_STACK_OF(EVP_PBE_CTL)
7141ba31
MC
187
188int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
c13d2ab4 189
50cd4768 190#include <openssl/types.h>
c13d2ab4
RL
191#include <openssl/core.h>
192
193void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id,
f7c16d48
RL
194 const char *name, const char *properties,
195 void *(*new_method)(int name_id,
6b9e3724 196 const OSSL_DISPATCH *fns,
0ddf74bf 197 OSSL_PROVIDER *prov),
7c95390e 198 int (*up_ref_method)(void *),
0211740f 199 void (*free_method)(void *));
f7c16d48
RL
200void *evp_generic_fetch_by_number(OPENSSL_CTX *ctx, int operation_id,
201 int name_id, const char *properties,
202 void *(*new_method)(int name_id,
203 const OSSL_DISPATCH *fns,
0ddf74bf 204 OSSL_PROVIDER *prov),
f7c16d48
RL
205 int (*up_ref_method)(void *),
206 void (*free_method)(void *));
3d96a51c
RL
207void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
208 void (*user_fn)(void *method, void *arg),
209 void *user_arg,
f7c16d48 210 void *(*new_method)(int name_id,
3d96a51c 211 const OSSL_DISPATCH *fns,
0ddf74bf 212 OSSL_PROVIDER *prov),
3d96a51c 213 void (*free_method)(void *));
13273237 214
f7c16d48
RL
215/* Internal fetchers for method types that are to be combined with others */
216EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id,
217 const char *properties);
218
3fd70262
RL
219/* Internal structure constructors for fetched methods */
220EVP_MD *evp_md_new(void);
550f974a 221EVP_CIPHER *evp_cipher_new(void);
3fd70262 222
13273237
RL
223/* Helper functions to avoid duplicating code */
224
225/*
459b15d4 226 * These methods implement different ways to pass a params array to the
13273237
RL
227 * provider. They will return one of these values:
228 *
229 * -2 if the method doesn't come from a provider
230 * (evp_do_param will return this to the called)
231 * -1 if the provider doesn't offer the desired function
232 * (evp_do_param will raise an error and return 0)
233 * or the return value from the desired function
234 * (evp_do_param will return it to the caller)
235 */
459b15d4
SL
236int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
237int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
13273237 238 OSSL_PARAM params[]);
459b15d4 239int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
13273237 240 OSSL_PARAM params[]);
6a3b7c68
RL
241int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
242int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
243 OSSL_PARAM params[]);
244int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
245 OSSL_PARAM params[]);
ff64702b
MC
246
247OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
248
249#define M_check_autoarg(ctx, arg, arglen, err) \
250 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
251 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
252 \
253 if (pksize == 0) { \
51ba9ebd 254 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
ff64702b
MC
255 return 0; \
256 } \
257 if (arg == NULL) { \
258 *arglen = pksize; \
259 return 1; \
260 } \
261 if (*arglen < pksize) { \
51ba9ebd 262 ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
ff64702b
MC
263 return 0; \
264 } \
265 }
864b89ce
MC
266
267void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
f7c16d48
RL
268
269/* OSSL_PROVIDER * is only used to get the library context */
3f7ce7f1 270const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id);
e4a1d023
RL
271int evp_is_a(OSSL_PROVIDER *prov, int number,
272 const char *legacy_name, const char *name);
f651c727
RL
273void evp_names_do_all(OSSL_PROVIDER *prov, int number,
274 void (*fn)(const char *name, void *data),
275 void *data);
3c957bcd 276int evp_cipher_cache_constants(EVP_CIPHER *cipher);