1/*
2 * Copyright 2000-2019 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
14#define EVP_CTRL_RET_UNSUPPORTED -1
15
16
17struct evp_md_ctx_st {
18 const EVP_MD *reqdigest; /* The original requested digest */
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);
28
29 /* Provider ctx */
30 void *provctx;
31 EVP_MD *fetched_digest;
32} /* EVP_MD_CTX */ ;
33
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 */
52
53 /* Provider ctx */
54 void *provctx;
55 EVP_CIPHER *fetched_cipher;
56} /* EVP_CIPHER_CTX */ ;
57
58struct evp_mac_ctx_st {
59 EVP_MAC *meth; /* Method structure */
60 void *data; /* Individual method data */
61} /* EVP_MAC_CTX */;
62
63struct evp_kdf_ctx_st {
64 EVP_KDF *meth; /* Method structure */
65 void *data; /* Algorithm-specific data */
66} /* EVP_KDF_CTX */ ;
67
68struct evp_keymgmt_st {
69 int id; /* libcrypto internal */
70
71 int name_id;
72 OSSL_PROVIDER *prov;
73 CRYPTO_REF_COUNT refcnt;
74 CRYPTO_RWLOCK *lock;
75
76 /* Domain parameter routines */
77 OSSL_OP_keymgmt_importdomparams_fn *importdomparams;
78 OSSL_OP_keymgmt_gendomparams_fn *gendomparams;
79 OSSL_OP_keymgmt_freedomparams_fn *freedomparams;
80 OSSL_OP_keymgmt_exportdomparams_fn *exportdomparams;
81 OSSL_OP_keymgmt_importdomparam_types_fn *importdomparam_types;
82 OSSL_OP_keymgmt_exportdomparam_types_fn *exportdomparam_types;
83
84 /* Key routines */
85 OSSL_OP_keymgmt_importkey_fn *importkey;
86 OSSL_OP_keymgmt_genkey_fn *genkey;
87 OSSL_OP_keymgmt_loadkey_fn *loadkey;
88 OSSL_OP_keymgmt_freekey_fn *freekey;
89 OSSL_OP_keymgmt_exportkey_fn *exportkey;
90 OSSL_OP_keymgmt_importkey_types_fn *importkey_types;
91 OSSL_OP_keymgmt_exportkey_types_fn *exportkey_types;
92} /* EVP_KEYMGMT */ ;
93
94struct keymgmt_data_st {
95 OPENSSL_CTX *ctx;
96 const char *properties;
97};
98
99struct evp_keyexch_st {
100 int name_id;
101 OSSL_PROVIDER *prov;
102 CRYPTO_REF_COUNT refcnt;
103 CRYPTO_RWLOCK *lock;
104
105 OSSL_OP_keyexch_newctx_fn *newctx;
106 OSSL_OP_keyexch_init_fn *init;
107 OSSL_OP_keyexch_set_peer_fn *set_peer;
108 OSSL_OP_keyexch_derive_fn *derive;
109 OSSL_OP_keyexch_freectx_fn *freectx;
110 OSSL_OP_keyexch_dupctx_fn *dupctx;
111 OSSL_OP_keyexch_set_ctx_params_fn *set_ctx_params;
112 OSSL_OP_keyexch_settable_ctx_params_fn *settable_ctx_params;
113} /* EVP_KEYEXCH */;
114
115struct evp_signature_st {
116 int name_id;
117 OSSL_PROVIDER *prov;
118 CRYPTO_REF_COUNT refcnt;
119 CRYPTO_RWLOCK *lock;
120
121 OSSL_OP_signature_newctx_fn *newctx;
122 OSSL_OP_signature_sign_init_fn *sign_init;
123 OSSL_OP_signature_sign_fn *sign;
124 OSSL_OP_signature_verify_init_fn *verify_init;
125 OSSL_OP_signature_verify_fn *verify;
126 OSSL_OP_signature_verify_recover_init_fn *verify_recover_init;
127 OSSL_OP_signature_verify_recover_fn *verify_recover;
128 OSSL_OP_signature_digest_sign_init_fn *digest_sign_init;
129 OSSL_OP_signature_digest_sign_update_fn *digest_sign_update;
130 OSSL_OP_signature_digest_sign_final_fn *digest_sign_final;
131 OSSL_OP_signature_digest_verify_init_fn *digest_verify_init;
132 OSSL_OP_signature_digest_verify_update_fn *digest_verify_update;
133 OSSL_OP_signature_digest_verify_final_fn *digest_verify_final;
134 OSSL_OP_signature_freectx_fn *freectx;
135 OSSL_OP_signature_dupctx_fn *dupctx;
136 OSSL_OP_signature_get_ctx_params_fn *get_ctx_params;
137 OSSL_OP_signature_gettable_ctx_params_fn *gettable_ctx_params;
138 OSSL_OP_signature_set_ctx_params_fn *set_ctx_params;
139 OSSL_OP_signature_settable_ctx_params_fn *settable_ctx_params;
140 OSSL_OP_signature_get_ctx_md_params_fn *get_ctx_md_params;
141 OSSL_OP_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
142 OSSL_OP_signature_set_ctx_md_params_fn *set_ctx_md_params;
143 OSSL_OP_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
144} /* EVP_SIGNATURE */;
145
146struct evp_asym_cipher_st {
147 int name_id;
148 OSSL_PROVIDER *prov;
149 CRYPTO_REF_COUNT refcnt;
150 CRYPTO_RWLOCK *lock;
151
152 OSSL_OP_asym_cipher_newctx_fn *newctx;
153 OSSL_OP_asym_cipher_encrypt_init_fn *encrypt_init;
154 OSSL_OP_asym_cipher_encrypt_fn *encrypt;
155 OSSL_OP_asym_cipher_decrypt_init_fn *decrypt_init;
156 OSSL_OP_asym_cipher_decrypt_fn *decrypt;
157 OSSL_OP_asym_cipher_freectx_fn *freectx;
158 OSSL_OP_asym_cipher_dupctx_fn *dupctx;
159 OSSL_OP_asym_cipher_get_ctx_params_fn *get_ctx_params;
160 OSSL_OP_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
161 OSSL_OP_asym_cipher_set_ctx_params_fn *set_ctx_params;
162 OSSL_OP_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
163} /* EVP_ASYM_CIPHER */;
164
165int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
166 int passlen, ASN1_TYPE *param,
167 const EVP_CIPHER *c, const EVP_MD *md,
168 int en_de);
169
170struct evp_Encode_Ctx_st {
171 /* number saved in a partial encode/decode */
172 int num;
173 /*
174 * The length is either the output line length (in input bytes) or the
175 * shortest input line length that is ok. Once decoding begins, the
176 * length is adjusted up each time a longer line is decoded
177 */
178 int length;
179 /* data to encode */
180 unsigned char enc_data[80];
181 /* number read on current line */
182 int line_num;
183 unsigned int flags;
184};
185
186typedef struct evp_pbe_st EVP_PBE_CTL;
187DEFINE_STACK_OF(EVP_PBE_CTL)
188
189int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
190
191#include <openssl/types.h>
192#include <openssl/core.h>
193
194void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id,
195 const char *name, const char *properties,
196 void *(*new_method)(int name_id,
197 const OSSL_DISPATCH *fns,
198 OSSL_PROVIDER *prov),
199 int (*up_ref_method)(void *),
200 void (*free_method)(void *));
201void *evp_generic_fetch_by_number(OPENSSL_CTX *ctx, int operation_id,
202 int name_id, const char *properties,
203 void *(*new_method)(int name_id,
204 const OSSL_DISPATCH *fns,
205 OSSL_PROVIDER *prov),
206 int (*up_ref_method)(void *),
207 void (*free_method)(void *));
208void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
209 void (*user_fn)(void *method, void *arg),
210 void *user_arg,
211 void *(*new_method)(int name_id,
212 const OSSL_DISPATCH *fns,
213 OSSL_PROVIDER *prov),
214 void (*free_method)(void *));
215
216/* Internal fetchers for method types that are to be combined with others */
217EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id,
218 const char *properties);
219
220/* Internal structure constructors for fetched methods */
221EVP_MD *evp_md_new(void);
222EVP_CIPHER *evp_cipher_new(void);
223
224/* Helper functions to avoid duplicating code */
225
226/*
227 * These methods implement different ways to pass a params array to the
228 * provider. They will return one of these values:
229 *
230 * -2 if the method doesn't come from a provider
231 * (evp_do_param will return this to the called)
232 * -1 if the provider doesn't offer the desired function
233 * (evp_do_param will raise an error and return 0)
234 * or the return value from the desired function
235 * (evp_do_param will return it to the caller)
236 */
237int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
238int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
239 OSSL_PARAM params[]);
240int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
241 OSSL_PARAM params[]);
242int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
243int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
244 OSSL_PARAM params[]);
245int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
246 OSSL_PARAM params[]);
247
248OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
249
250#define M_check_autoarg(ctx, arg, arglen, err) \
251 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
252 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
253 \
254 if (pksize == 0) { \
255 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
256 return 0; \
257 } \
258 if (arg == NULL) { \
259 *arglen = pksize; \
260 return 1; \
261 } \
262 if (*arglen < pksize) { \
263 ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
264 return 0; \
265 } \
266 }
267
268void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
269
270/* OSSL_PROVIDER * is only used to get the library context */
271const char *evp_first_name(OSSL_PROVIDER *prov, int name_id);
272int evp_is_a(OSSL_PROVIDER *prov, int number, const char *name);
273void evp_names_do_all(OSSL_PROVIDER *prov, int number,
274 void (*fn)(const char *name, void *data),
275 void *data);
276int evp_cipher_cache_constants(EVP_CIPHER *cipher);
277