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