xref: /src/crypto/openssl/crypto/evp/evp_local.h (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2000-2025 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 #include <openssl/core_dispatch.h>
11 #include "internal/refcount.h"
12 
13 #define EVP_CTRL_RET_UNSUPPORTED -1
14 
15 struct evp_md_ctx_st {
16     const EVP_MD *reqdigest; /* The original requested digest */
17     const EVP_MD *digest;
18     ENGINE *engine; /* functional reference if 'digest' is
19                      * ENGINE-provided */
20     unsigned long flags;
21     void *md_data;
22     /* Public key context for sign/verify */
23     EVP_PKEY_CTX *pctx;
24     /* Update function: usually copied from EVP_MD */
25     int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
26 
27     /*
28      * Opaque ctx returned from a providers digest algorithm implementation
29      * OSSL_FUNC_digest_newctx()
30      */
31     void *algctx;
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     int iv_len; /* IV length */
49     unsigned long flags; /* Various flags */
50     void *cipher_data; /* per EVP data */
51     int final_used;
52     int block_mask;
53     unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
54     size_t numpipes;
55 
56     /*
57      * Opaque ctx returned from a providers cipher algorithm implementation
58      * OSSL_FUNC_cipher_newctx()
59      */
60     void *algctx;
61     EVP_CIPHER *fetched_cipher;
62 } /* EVP_CIPHER_CTX */;
63 
64 struct evp_mac_ctx_st {
65     EVP_MAC *meth; /* Method structure */
66     /*
67      * Opaque ctx returned from a providers MAC algorithm implementation
68      * OSSL_FUNC_mac_newctx()
69      */
70     void *algctx;
71 } /* EVP_MAC_CTX */;
72 
73 struct evp_kdf_ctx_st {
74     EVP_KDF *meth; /* Method structure */
75     /*
76      * Opaque ctx returned from a providers KDF algorithm implementation
77      * OSSL_FUNC_kdf_newctx()
78      */
79     void *algctx;
80 } /* EVP_KDF_CTX */;
81 
82 struct evp_rand_ctx_st {
83     EVP_RAND *meth; /* Method structure */
84     /*
85      * Opaque ctx returned from a providers rand algorithm implementation
86      * OSSL_FUNC_rand_newctx()
87      */
88     void *algctx;
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;
92 } /* EVP_RAND_CTX */;
93 
94 struct evp_keymgmt_st {
95     int id; /* libcrypto internal */
96 
97     int name_id;
98     /* NID for the legacy alg if there is one */
99     int legacy_alg;
100     char *type_name;
101     const char *description;
102     OSSL_PROVIDER *prov;
103     CRYPTO_REF_COUNT refcnt;
104 
105     /* Constructor(s), destructor, information */
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;
112 
113     /* Generation, a complex constructor */
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_get_params_fn *gen_get_params;
117     OSSL_FUNC_keymgmt_gen_gettable_params_fn *gen_gettable_params;
118     OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params;
119     OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params;
120     OSSL_FUNC_keymgmt_gen_fn *gen;
121     OSSL_FUNC_keymgmt_gen_cleanup_fn *gen_cleanup;
122 
123     OSSL_FUNC_keymgmt_load_fn *load;
124 
125     /* Key object checking */
126     OSSL_FUNC_keymgmt_query_operation_name_fn *query_operation_name;
127     OSSL_FUNC_keymgmt_has_fn *has;
128     OSSL_FUNC_keymgmt_validate_fn *validate;
129     OSSL_FUNC_keymgmt_match_fn *match;
130 
131     /* Import and export routines */
132     OSSL_FUNC_keymgmt_import_fn *import;
133     OSSL_FUNC_keymgmt_import_types_fn *import_types;
134     OSSL_FUNC_keymgmt_import_types_ex_fn *import_types_ex;
135     OSSL_FUNC_keymgmt_export_fn *export;
136     OSSL_FUNC_keymgmt_export_types_fn *export_types;
137     OSSL_FUNC_keymgmt_export_types_ex_fn *export_types_ex;
138     OSSL_FUNC_keymgmt_dup_fn *dup;
139 } /* EVP_KEYMGMT */;
140 
141 struct evp_keyexch_st {
142     int name_id;
143     char *type_name;
144     const char *description;
145     OSSL_PROVIDER *prov;
146     CRYPTO_REF_COUNT refcnt;
147 
148     OSSL_FUNC_keyexch_newctx_fn *newctx;
149     OSSL_FUNC_keyexch_init_fn *init;
150     OSSL_FUNC_keyexch_set_peer_fn *set_peer;
151     OSSL_FUNC_keyexch_derive_fn *derive;
152     OSSL_FUNC_keyexch_freectx_fn *freectx;
153     OSSL_FUNC_keyexch_dupctx_fn *dupctx;
154     OSSL_FUNC_keyexch_set_ctx_params_fn *set_ctx_params;
155     OSSL_FUNC_keyexch_settable_ctx_params_fn *settable_ctx_params;
156     OSSL_FUNC_keyexch_get_ctx_params_fn *get_ctx_params;
157     OSSL_FUNC_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
158 } /* EVP_KEYEXCH */;
159 
160 struct evp_signature_st {
161     int name_id;
162     char *type_name;
163     const char *description;
164     OSSL_PROVIDER *prov;
165     CRYPTO_REF_COUNT refcnt;
166 
167     OSSL_FUNC_signature_newctx_fn *newctx;
168     OSSL_FUNC_signature_sign_init_fn *sign_init;
169     OSSL_FUNC_signature_sign_fn *sign;
170     OSSL_FUNC_signature_sign_message_init_fn *sign_message_init;
171     OSSL_FUNC_signature_sign_message_update_fn *sign_message_update;
172     OSSL_FUNC_signature_sign_message_final_fn *sign_message_final;
173     OSSL_FUNC_signature_verify_init_fn *verify_init;
174     OSSL_FUNC_signature_verify_fn *verify;
175     OSSL_FUNC_signature_verify_message_init_fn *verify_message_init;
176     OSSL_FUNC_signature_verify_message_update_fn *verify_message_update;
177     OSSL_FUNC_signature_verify_message_final_fn *verify_message_final;
178     OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init;
179     OSSL_FUNC_signature_verify_recover_fn *verify_recover;
180     OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init;
181     OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update;
182     OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final;
183     OSSL_FUNC_signature_digest_sign_fn *digest_sign;
184     OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init;
185     OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update;
186     OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final;
187     OSSL_FUNC_signature_digest_verify_fn *digest_verify;
188     OSSL_FUNC_signature_freectx_fn *freectx;
189     OSSL_FUNC_signature_dupctx_fn *dupctx;
190     OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params;
191     OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params;
192     OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params;
193     OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params;
194     OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params;
195     OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
196     OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params;
197     OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
198 
199     /* Signature object checking */
200     OSSL_FUNC_signature_query_key_types_fn *query_key_types;
201 } /* EVP_SIGNATURE */;
202 
203 struct evp_skeymgmt_st {
204     int name_id;
205     char *type_name;
206     const char *description;
207     OSSL_PROVIDER *prov;
208     CRYPTO_REF_COUNT refcnt;
209 
210     /* Import and export routines */
211     OSSL_FUNC_skeymgmt_imp_settable_params_fn *imp_params;
212     OSSL_FUNC_skeymgmt_import_fn *import;
213     OSSL_FUNC_skeymgmt_export_fn *export;
214 
215     /* Key generation */
216     OSSL_FUNC_skeymgmt_gen_settable_params_fn *gen_params;
217     OSSL_FUNC_skeymgmt_generate_fn *generate;
218 
219     /* Key identifier */
220     OSSL_FUNC_skeymgmt_get_key_id_fn *get_key_id;
221 
222     /* destructor */
223     OSSL_FUNC_skeymgmt_free_fn *free;
224 } /* EVP_SKEYMGMT */;
225 
226 struct evp_asym_cipher_st {
227     int name_id;
228     char *type_name;
229     const char *description;
230     OSSL_PROVIDER *prov;
231     CRYPTO_REF_COUNT refcnt;
232 
233     OSSL_FUNC_asym_cipher_newctx_fn *newctx;
234     OSSL_FUNC_asym_cipher_encrypt_init_fn *encrypt_init;
235     OSSL_FUNC_asym_cipher_encrypt_fn *encrypt;
236     OSSL_FUNC_asym_cipher_decrypt_init_fn *decrypt_init;
237     OSSL_FUNC_asym_cipher_decrypt_fn *decrypt;
238     OSSL_FUNC_asym_cipher_freectx_fn *freectx;
239     OSSL_FUNC_asym_cipher_dupctx_fn *dupctx;
240     OSSL_FUNC_asym_cipher_get_ctx_params_fn *get_ctx_params;
241     OSSL_FUNC_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
242     OSSL_FUNC_asym_cipher_set_ctx_params_fn *set_ctx_params;
243     OSSL_FUNC_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
244 } /* EVP_ASYM_CIPHER */;
245 
246 struct evp_kem_st {
247     int name_id;
248     char *type_name;
249     const char *description;
250     OSSL_PROVIDER *prov;
251     CRYPTO_REF_COUNT refcnt;
252 
253     OSSL_FUNC_kem_newctx_fn *newctx;
254     OSSL_FUNC_kem_encapsulate_init_fn *encapsulate_init;
255     OSSL_FUNC_kem_encapsulate_fn *encapsulate;
256     OSSL_FUNC_kem_decapsulate_init_fn *decapsulate_init;
257     OSSL_FUNC_kem_decapsulate_fn *decapsulate;
258     OSSL_FUNC_kem_freectx_fn *freectx;
259     OSSL_FUNC_kem_dupctx_fn *dupctx;
260     OSSL_FUNC_kem_get_ctx_params_fn *get_ctx_params;
261     OSSL_FUNC_kem_gettable_ctx_params_fn *gettable_ctx_params;
262     OSSL_FUNC_kem_set_ctx_params_fn *set_ctx_params;
263     OSSL_FUNC_kem_settable_ctx_params_fn *settable_ctx_params;
264     OSSL_FUNC_kem_auth_encapsulate_init_fn *auth_encapsulate_init;
265     OSSL_FUNC_kem_auth_decapsulate_init_fn *auth_decapsulate_init;
266 } /* EVP_KEM */;
267 
268 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
269     int passlen, ASN1_TYPE *param,
270     const EVP_CIPHER *c, const EVP_MD *md,
271     int en_de);
272 int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
273     int passlen, ASN1_TYPE *param,
274     const EVP_CIPHER *c, const EVP_MD *md,
275     int en_de, OSSL_LIB_CTX *libctx, const char *propq);
276 
277 struct evp_Encode_Ctx_st {
278     /* number saved in a partial encode/decode */
279     int num;
280     /*
281      * The length is either the output line length (in input bytes) or the
282      * shortest input line length that is ok.  Once decoding begins, the
283      * length is adjusted up each time a longer line is decoded
284      */
285     int length;
286     /* data to encode */
287     unsigned char enc_data[80];
288     /* number read on current line */
289     int line_num;
290     unsigned int flags;
291 };
292 
293 typedef struct evp_pbe_st EVP_PBE_CTL;
294 DEFINE_STACK_OF(EVP_PBE_CTL)
295 
296 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
297 
298 #include <openssl/types.h>
299 #include <openssl/core.h>
300 
301 void *evp_generic_fetch(OSSL_LIB_CTX *ctx, int operation_id,
302     const char *name, const char *properties,
303     void *(*new_method)(int name_id,
304         const OSSL_ALGORITHM *algodef,
305         OSSL_PROVIDER *prov),
306     int (*up_ref_method)(void *),
307     void (*free_method)(void *));
308 void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
309     const char *name, const char *properties,
310     void *(*new_method)(int name_id,
311         const OSSL_ALGORITHM *algodef,
312         OSSL_PROVIDER *prov),
313     int (*up_ref_method)(void *),
314     void (*free_method)(void *));
315 void evp_generic_do_all_prefetched(OSSL_LIB_CTX *libctx, int operation_id,
316     void (*user_fn)(void *method, void *arg),
317     void *user_arg);
318 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
319     void (*user_fn)(void *method, void *arg),
320     void *user_arg,
321     void *(*new_method)(int name_id,
322         const OSSL_ALGORITHM *algodef,
323         OSSL_PROVIDER *prov),
324     int (*up_ref_method)(void *),
325     void (*free_method)(void *));
326 
327 /* Internal fetchers for method types that are to be combined with others */
328 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
329     const char *properties);
330 EVP_SIGNATURE *evp_signature_fetch_from_prov(OSSL_PROVIDER *prov,
331     const char *name,
332     const char *properties);
333 EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
334     const char *name,
335     const char *properties);
336 EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
337     const char *name,
338     const char *properties);
339 EVP_KEM *evp_kem_fetch_from_prov(OSSL_PROVIDER *prov,
340     const char *name,
341     const char *properties);
342 EVP_CIPHER *evp_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
343     const char *algorithm,
344     const char *properties);
345 EVP_MD *evp_digest_fetch_from_prov(OSSL_PROVIDER *prov,
346     const char *algorithm,
347     const char *properties);
348 EVP_MAC *evp_mac_fetch_from_prov(OSSL_PROVIDER *prov,
349     const char *algorithm,
350     const char *properties);
351 
352 /* Internal structure constructors for fetched methods */
353 EVP_MD *evp_md_new(void);
354 EVP_CIPHER *evp_cipher_new(void);
355 
356 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
357     evp_cipher_aead_asn1_params *asn1_params);
358 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
359     evp_cipher_aead_asn1_params *asn1_params);
360 
361 /* Helper functions to avoid duplicating code */
362 
363 /*
364  * These methods implement different ways to pass a params array to the
365  * provider.  They will return one of these values:
366  *
367  * -2 if the method doesn't come from a provider
368  *    (evp_do_param will return this to the called)
369  * -1 if the provider doesn't offer the desired function
370  *    (evp_do_param will raise an error and return 0)
371  * or the return value from the desired function
372  *    (evp_do_param will return it to the caller)
373  */
374 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
375 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
376     OSSL_PARAM params[]);
377 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
378     OSSL_PARAM params[]);
379 int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
380 int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
381     OSSL_PARAM params[]);
382 int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
383     OSSL_PARAM params[]);
384 
385 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
386 
387 #define M_check_autoarg(ctx, arg, arglen, err)                               \
388     if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) {                      \
389         size_t pksize = (size_t)EVP_PKEY_get_size(ctx->pkey);                \
390                                                                              \
391         if (pksize == 0) {                                                   \
392             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/      \
393             return 0;                                                        \
394         }                                                                    \
395         if (arg == NULL) {                                                   \
396             *arglen = pksize;                                                \
397             return 1;                                                        \
398         }                                                                    \
399         if (*arglen < pksize) {                                              \
400             ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
401             return 0;                                                        \
402         }                                                                    \
403     }
404 
405 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
406 void evp_cipher_free_int(EVP_CIPHER *md);
407 void evp_md_free_int(EVP_MD *md);
408 
409 /* OSSL_PROVIDER * is only used to get the library context */
410 int evp_is_a(OSSL_PROVIDER *prov, int number,
411     const char *legacy_name, const char *name);
412 int evp_names_do_all(OSSL_PROVIDER *prov, int number,
413     void (*fn)(const char *name, void *data),
414     void *data);
415 int evp_cipher_cache_constants(EVP_CIPHER *cipher);
416