]>
| Commit | Line | Data |
|---|---|---|
| 27af42f9 | 1 | /* |
| 0c679f55 | 2 | * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 27af42f9 | 3 | * |
| 48f4ad77 | 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 | |
| 27af42f9 DSH |
8 | */ |
| 9 | ||
| 80ce21fe F |
10 | #ifndef OSSL_CRYPTO_EVP_H |
| 11 | # define OSSL_CRYPTO_EVP_H | |
| 12 | # pragma once | |
| 13 | ||
| 14 | # include <openssl/evp.h> | |
| 15 | # include <openssl/core_dispatch.h> | |
| 16 | # include "internal/refcount.h" | |
| 17 | # include "crypto/ecx.h" | |
| 2f545ae4 | 18 | |
| 3859a027 | 19 | /* |
| 20 | * Default PKCS5 PBE KDF salt lengths | |
| 21 | * In RFC 8018, PBE1 uses 8 bytes (64 bits) for its salt length. | |
| 22 | * It also specifies to use at least 8 bytes for PBES2. | |
| 23 | * The NIST requirement for PBKDF2 is 128 bits so we use this as the | |
| 24 | * default for PBE2 (scrypt and HKDF2) | |
| 25 | */ | |
| 26 | # define PKCS5_DEFAULT_PBE1_SALT_LEN PKCS5_SALT_LEN | |
| 27 | # define PKCS5_DEFAULT_PBE2_SALT_LEN 16 | |
| 4803717f PY |
28 | /* |
| 29 | * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag | |
| 30 | * values in evp.h | |
| 31 | */ | |
| 32 | #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400 | |
| 3fc2b7d6 | 33 | #define EVP_MD_CTX_FLAG_FINALISED 0x0800 |
| 4803717f | 34 | |
| f21c9c64 | 35 | #define evp_pkey_ctx_is_legacy(ctx) \ |
| 929f651e | 36 | ((ctx)->keymgmt == NULL) |
| f21c9c64 RL |
37 | #define evp_pkey_ctx_is_provided(ctx) \ |
| 38 | (!evp_pkey_ctx_is_legacy(ctx)) | |
| 39 | ||
| 27af42f9 | 40 | struct evp_pkey_ctx_st { |
| 864b89ce MC |
41 | /* Actual operation */ |
| 42 | int operation; | |
| 43 | ||
| 3ee348b0 | 44 | /* |
| 4b9e90f4 RL |
45 | * Library context, property query, keytype and keymgmt associated with |
| 46 | * this context | |
| 3ee348b0 | 47 | */ |
| b4250010 | 48 | OSSL_LIB_CTX *libctx; |
| ddfd7182 | 49 | char *propquery; |
| 4b9e90f4 | 50 | const char *keytype; |
| 5246183e | 51 | /* If |pkey| below is set, this field is always a reference to its keymgmt */ |
| c0e0984f RL |
52 | EVP_KEYMGMT *keymgmt; |
| 53 | ||
| 864b89ce | 54 | union { |
| 62924755 RL |
55 | struct { |
| 56 | void *genctx; | |
| 57 | } keymgmt; | |
| 58 | ||
| 864b89ce MC |
59 | struct { |
| 60 | EVP_KEYEXCH *exchange; | |
| 7c14d0c1 SL |
61 | /* |
| 62 | * Opaque ctx returned from a providers exchange algorithm | |
| 63 | * implementation OSSL_FUNC_keyexch_newctx() | |
| 64 | */ | |
| 65 | void *algctx; | |
| 864b89ce | 66 | } kex; |
| ff64702b | 67 | |
| 864b89ce MC |
68 | struct { |
| 69 | EVP_SIGNATURE *signature; | |
| 7c14d0c1 SL |
70 | /* |
| 71 | * Opaque ctx returned from a providers signature algorithm | |
| 72 | * implementation OSSL_FUNC_signature_newctx() | |
| 73 | */ | |
| 74 | void *algctx; | |
| 864b89ce | 75 | } sig; |
| 2c938e2e MC |
76 | |
| 77 | struct { | |
| 78 | EVP_ASYM_CIPHER *cipher; | |
| 7c14d0c1 SL |
79 | /* |
| 80 | * Opaque ctx returned from a providers asymmetric cipher algorithm | |
| 81 | * implementation OSSL_FUNC_asym_cipher_newctx() | |
| 82 | */ | |
| 83 | void *algctx; | |
| 2c938e2e | 84 | } ciph; |
| 80f4fd18 SL |
85 | struct { |
| 86 | EVP_KEM *kem; | |
| 7c14d0c1 SL |
87 | /* |
| 88 | * Opaque ctx returned from a providers KEM algorithm | |
| 89 | * implementation OSSL_FUNC_kem_newctx() | |
| 90 | */ | |
| 91 | void *algctx; | |
| 80f4fd18 | 92 | } encap; |
| 864b89ce | 93 | } op; |
| dfcb5d29 | 94 | |
| 86df26b3 RL |
95 | /* |
| 96 | * Cached parameters. Inits of operations that depend on these should | |
| 97 | * call evp_pkey_ctx_use_delayed_data() when the operation has been set | |
| 98 | * up properly. | |
| 99 | */ | |
| 100 | struct { | |
| 101 | /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */ | |
| 102 | char *dist_id_name; /* The name used with EVP_PKEY_CTX_ctrl_str() */ | |
| 103 | void *dist_id; /* The distinguishing ID itself */ | |
| 104 | size_t dist_id_len; /* The length of the distinguishing ID */ | |
| 105 | ||
| 106 | /* Indicators of what has been set. Keep them together! */ | |
| 107 | unsigned int dist_id_set : 1; | |
| 108 | } cached_parameters; | |
| 109 | ||
| 62924755 RL |
110 | /* Application specific data, usually used by the callback */ |
| 111 | void *app_data; | |
| 112 | /* Keygen callback */ | |
| 113 | EVP_PKEY_gen_cb *pkey_gencb; | |
| 114 | /* implementation specific keygen data */ | |
| 115 | int *keygen_info; | |
| 116 | int keygen_info_count; | |
| 117 | ||
| ff64702b MC |
118 | /* Legacy fields below */ |
| 119 | ||
| 50914496 RL |
120 | /* EVP_PKEY identity */ |
| 121 | int legacy_keytype; | |
| 27af42f9 DSH |
122 | /* Method associated with this operation */ |
| 123 | const EVP_PKEY_METHOD *pmeth; | |
| 124 | /* Engine that implements this method or NULL if builtin */ | |
| 125 | ENGINE *engine; | |
| 126 | /* Key: may be NULL */ | |
| 127 | EVP_PKEY *pkey; | |
| 128 | /* Peer key for key agreement, may be NULL */ | |
| 129 | EVP_PKEY *peerkey; | |
| 27af42f9 DSH |
130 | /* Algorithm specific data */ |
| 131 | void *data; | |
| a5ce329e RL |
132 | /* Indicator if digest_custom needs to be called */ |
| 133 | unsigned int flag_call_digest_custom:1; | |
| 3786d748 | 134 | /* |
| 135 | * Used to support taking custody of memory in the case of a provider being | |
| 136 | * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This | |
| 137 | * member should NOT be used for any other purpose and should be removed | |
| 138 | * when said deprecated API is excised completely. | |
| 139 | */ | |
| 140 | BIGNUM *rsa_pubexp; | |
| 27af42f9 DSH |
141 | } /* EVP_PKEY_CTX */ ; |
| 142 | ||
| 143 | #define EVP_PKEY_FLAG_DYNAMIC 1 | |
| 144 | ||
| 145 | struct evp_pkey_method_st { | |
| 146 | int pkey_id; | |
| 147 | int flags; | |
| 148 | int (*init) (EVP_PKEY_CTX *ctx); | |
| 9fdcc21f | 149 | int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src); |
| 27af42f9 DSH |
150 | void (*cleanup) (EVP_PKEY_CTX *ctx); |
| 151 | int (*paramgen_init) (EVP_PKEY_CTX *ctx); | |
| 152 | int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); | |
| 153 | int (*keygen_init) (EVP_PKEY_CTX *ctx); | |
| 154 | int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); | |
| 155 | int (*sign_init) (EVP_PKEY_CTX *ctx); | |
| 156 | int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |
| 157 | const unsigned char *tbs, size_t tbslen); | |
| 158 | int (*verify_init) (EVP_PKEY_CTX *ctx); | |
| 159 | int (*verify) (EVP_PKEY_CTX *ctx, | |
| 160 | const unsigned char *sig, size_t siglen, | |
| 161 | const unsigned char *tbs, size_t tbslen); | |
| 162 | int (*verify_recover_init) (EVP_PKEY_CTX *ctx); | |
| 163 | int (*verify_recover) (EVP_PKEY_CTX *ctx, | |
| 164 | unsigned char *rout, size_t *routlen, | |
| 165 | const unsigned char *sig, size_t siglen); | |
| 166 | int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
| 167 | int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |
| 168 | EVP_MD_CTX *mctx); | |
| 169 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
| 170 | int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen, | |
| 171 | EVP_MD_CTX *mctx); | |
| 172 | int (*encrypt_init) (EVP_PKEY_CTX *ctx); | |
| 173 | int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, | |
| 174 | const unsigned char *in, size_t inlen); | |
| 175 | int (*decrypt_init) (EVP_PKEY_CTX *ctx); | |
| 176 | int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, | |
| 177 | const unsigned char *in, size_t inlen); | |
| 178 | int (*derive_init) (EVP_PKEY_CTX *ctx); | |
| 179 | int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); | |
| 180 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2); | |
| 181 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value); | |
| f723c98e DSH |
182 | int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
| 183 | const unsigned char *tbs, size_t tbslen); | |
| 184 | int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, | |
| 185 | size_t siglen, const unsigned char *tbs, | |
| 186 | size_t tbslen); | |
| 2aee35d3 | 187 | int (*check) (EVP_PKEY *pkey); |
| b0004708 PY |
188 | int (*public_check) (EVP_PKEY *pkey); |
| 189 | int (*param_check) (EVP_PKEY *pkey); | |
| 0a8fdef7 PY |
190 | |
| 191 | int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
| 27af42f9 DSH |
192 | } /* EVP_PKEY_METHOD */ ; |
| 193 | ||
| a8eba56e | 194 | DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD) |
| 4a1f3f27 | 195 | |
| 27af42f9 | 196 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); |
| 8f463dbd | 197 | |
| 19dbb742 SL |
198 | const EVP_PKEY_METHOD *ossl_dh_pkey_method(void); |
| 199 | const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void); | |
| 5af02212 | 200 | const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void); |
| 32ab57cb SL |
201 | const EVP_PKEY_METHOD *ossl_ec_pkey_method(void); |
| 202 | const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void); | |
| 203 | const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void); | |
| 204 | const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void); | |
| 205 | const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void); | |
| 23b2fc0b P |
206 | const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void); |
| 207 | const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void); | |
| 2db6bf6f | 208 | |
| 567db2c1 | 209 | struct evp_mac_st { |
| e74bd290 | 210 | OSSL_PROVIDER *prov; |
| f7c16d48 | 211 | int name_id; |
| 6c9bc258 | 212 | char *type_name; |
| 309a78aa | 213 | const char *description; |
| e74bd290 RL |
214 | |
| 215 | CRYPTO_REF_COUNT refcnt; | |
| e74bd290 | 216 | |
| 363b1e5d DMSP |
217 | OSSL_FUNC_mac_newctx_fn *newctx; |
| 218 | OSSL_FUNC_mac_dupctx_fn *dupctx; | |
| 219 | OSSL_FUNC_mac_freectx_fn *freectx; | |
| 363b1e5d DMSP |
220 | OSSL_FUNC_mac_init_fn *init; |
| 221 | OSSL_FUNC_mac_update_fn *update; | |
| 222 | OSSL_FUNC_mac_final_fn *final; | |
| 223 | OSSL_FUNC_mac_gettable_params_fn *gettable_params; | |
| 224 | OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params; | |
| 225 | OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params; | |
| 226 | OSSL_FUNC_mac_get_params_fn *get_params; | |
| 227 | OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params; | |
| 228 | OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params; | |
| 759570bf | 229 | OSSL_FUNC_mac_init_skey_fn *init_skey; |
| 567db2c1 RL |
230 | }; |
| 231 | ||
| d2ba8123 | 232 | struct evp_kdf_st { |
| fb9e6dd6 | 233 | OSSL_PROVIDER *prov; |
| f7c16d48 | 234 | int name_id; |
| 6c9bc258 | 235 | char *type_name; |
| 309a78aa | 236 | const char *description; |
| fb9e6dd6 | 237 | CRYPTO_REF_COUNT refcnt; |
| fb9e6dd6 | 238 | |
| 363b1e5d DMSP |
239 | OSSL_FUNC_kdf_newctx_fn *newctx; |
| 240 | OSSL_FUNC_kdf_dupctx_fn *dupctx; | |
| 241 | OSSL_FUNC_kdf_freectx_fn *freectx; | |
| 242 | OSSL_FUNC_kdf_reset_fn *reset; | |
| 243 | OSSL_FUNC_kdf_derive_fn *derive; | |
| 244 | OSSL_FUNC_kdf_gettable_params_fn *gettable_params; | |
| 245 | OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params; | |
| 246 | OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params; | |
| 247 | OSSL_FUNC_kdf_get_params_fn *get_params; | |
| 248 | OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params; | |
| 249 | OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params; | |
| 55b2bf1a | 250 | OSSL_FUNC_kdf_set_skey_fn *set_skey; |
| b5d0d061 | 251 | OSSL_FUNC_kdf_derive_skey_fn *derive_skey; |
| d2ba8123 | 252 | }; |
| 5a285add | 253 | |
| f6c95e46 RS |
254 | #define EVP_ORIG_DYNAMIC 0 |
| 255 | #define EVP_ORIG_GLOBAL 1 | |
| 256 | #define EVP_ORIG_METH 2 | |
| 257 | ||
| 2db6bf6f | 258 | struct evp_md_st { |
| 3653d0c2 | 259 | /* nid */ |
| 2db6bf6f | 260 | int type; |
| 3653d0c2 MC |
261 | |
| 262 | /* Legacy structure members */ | |
| 2db6bf6f RL |
263 | int pkey_type; |
| 264 | int md_size; | |
| 265 | unsigned long flags; | |
| f6c95e46 | 266 | int origin; |
| 2db6bf6f RL |
267 | int (*init) (EVP_MD_CTX *ctx); |
| 268 | int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); | |
| 269 | int (*final) (EVP_MD_CTX *ctx, unsigned char *md); | |
| 270 | int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from); | |
| 271 | int (*cleanup) (EVP_MD_CTX *ctx); | |
| 272 | int block_size; | |
| 273 | int ctx_size; /* how big does the ctx->md_data need to be */ | |
| 274 | /* control function */ | |
| 275 | int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2); | |
| 3653d0c2 MC |
276 | |
| 277 | /* New structure members */ | |
| 946bdd12 | 278 | /* Above comment to be removed when legacy has gone */ |
| f7c16d48 | 279 | int name_id; |
| 6c9bc258 | 280 | char *type_name; |
| 309a78aa | 281 | const char *description; |
| 3653d0c2 MC |
282 | OSSL_PROVIDER *prov; |
| 283 | CRYPTO_REF_COUNT refcnt; | |
| 363b1e5d DMSP |
284 | OSSL_FUNC_digest_newctx_fn *newctx; |
| 285 | OSSL_FUNC_digest_init_fn *dinit; | |
| 286 | OSSL_FUNC_digest_update_fn *dupdate; | |
| 287 | OSSL_FUNC_digest_final_fn *dfinal; | |
| 53664908 | 288 | OSSL_FUNC_digest_squeeze_fn *dsqueeze; |
| 363b1e5d DMSP |
289 | OSSL_FUNC_digest_digest_fn *digest; |
| 290 | OSSL_FUNC_digest_freectx_fn *freectx; | |
| 4c41aa4b | 291 | OSSL_FUNC_digest_copyctx_fn *copyctx; |
| 363b1e5d DMSP |
292 | OSSL_FUNC_digest_dupctx_fn *dupctx; |
| 293 | OSSL_FUNC_digest_get_params_fn *get_params; | |
| 294 | OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params; | |
| 295 | OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params; | |
| 296 | OSSL_FUNC_digest_gettable_params_fn *gettable_params; | |
| 297 | OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params; | |
| 298 | OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params; | |
| 3653d0c2 | 299 | |
| 2db6bf6f RL |
300 | } /* EVP_MD */ ; |
| 301 | ||
| e79f8773 RL |
302 | struct evp_cipher_st { |
| 303 | int nid; | |
| df05f2ce | 304 | |
| e79f8773 RL |
305 | int block_size; |
| 306 | /* Default value for variable length ciphers */ | |
| 307 | int key_len; | |
| 308 | int iv_len; | |
| df05f2ce MC |
309 | |
| 310 | /* Legacy structure members */ | |
| e79f8773 RL |
311 | /* Various flags */ |
| 312 | unsigned long flags; | |
| f6c95e46 RS |
313 | /* How the EVP_CIPHER was created. */ |
| 314 | int origin; | |
| e79f8773 RL |
315 | /* init key */ |
| 316 | int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
| 317 | const unsigned char *iv, int enc); | |
| 318 | /* encrypt/decrypt data */ | |
| 319 | int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out, | |
| 320 | const unsigned char *in, size_t inl); | |
| 321 | /* cleanup ctx */ | |
| 322 | int (*cleanup) (EVP_CIPHER_CTX *); | |
| 323 | /* how big ctx->cipher_data needs to be */ | |
| 324 | int ctx_size; | |
| 325 | /* Populate a ASN1_TYPE with parameters */ | |
| 326 | int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); | |
| 327 | /* Get parameters from a ASN1_TYPE */ | |
| 328 | int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); | |
| 329 | /* Miscellaneous operations */ | |
| 330 | int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr); | |
| 331 | /* Application data */ | |
| 332 | void *app_data; | |
| df05f2ce MC |
333 | |
| 334 | /* New structure members */ | |
| 946bdd12 | 335 | /* Above comment to be removed when legacy has gone */ |
| f7c16d48 | 336 | int name_id; |
| 6c9bc258 | 337 | char *type_name; |
| 309a78aa | 338 | const char *description; |
| df05f2ce MC |
339 | OSSL_PROVIDER *prov; |
| 340 | CRYPTO_REF_COUNT refcnt; | |
| 363b1e5d DMSP |
341 | OSSL_FUNC_cipher_newctx_fn *newctx; |
| 342 | OSSL_FUNC_cipher_encrypt_init_fn *einit; | |
| 343 | OSSL_FUNC_cipher_decrypt_init_fn *dinit; | |
| 344 | OSSL_FUNC_cipher_update_fn *cupdate; | |
| 345 | OSSL_FUNC_cipher_final_fn *cfinal; | |
| 346 | OSSL_FUNC_cipher_cipher_fn *ccipher; | |
| ef7967d0 R |
347 | OSSL_FUNC_cipher_pipeline_encrypt_init_fn *p_einit; |
| 348 | OSSL_FUNC_cipher_pipeline_decrypt_init_fn *p_dinit; | |
| 349 | OSSL_FUNC_cipher_pipeline_update_fn *p_cupdate; | |
| 350 | OSSL_FUNC_cipher_pipeline_final_fn *p_cfinal; | |
| 363b1e5d DMSP |
351 | OSSL_FUNC_cipher_freectx_fn *freectx; |
| 352 | OSSL_FUNC_cipher_dupctx_fn *dupctx; | |
| 353 | OSSL_FUNC_cipher_get_params_fn *get_params; | |
| 354 | OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params; | |
| 355 | OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params; | |
| 356 | OSSL_FUNC_cipher_gettable_params_fn *gettable_params; | |
| 357 | OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params; | |
| 358 | OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params; | |
| d46e010c DB |
359 | OSSL_FUNC_cipher_encrypt_skey_init_fn *einit_skey; |
| 360 | OSSL_FUNC_cipher_decrypt_skey_init_fn *dinit_skey; | |
| 361 | } /* EVP_CIPHER */; | |
| e79f8773 RL |
362 | |
| 363 | /* Macros to code block cipher wrappers */ | |
| 364 | ||
| 365 | /* Wrapper functions for each cipher mode */ | |
| 366 | ||
| 44ab2dfd MC |
367 | #define EVP_C_DATA(kstruct, ctx) \ |
| 368 | ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx)) | |
| e79f8773 RL |
369 | |
| 370 | #define BLOCK_CIPHER_ecb_loop() \ | |
| 371 | size_t i, bl; \ | |
| f6c95e46 | 372 | bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \ |
| e8aa8b6c | 373 | if (inl < bl) return 1;\ |
| e79f8773 | 374 | inl -= bl; \ |
| e8aa8b6c | 375 | for (i=0; i <= inl; i+=bl) |
| e79f8773 RL |
376 | |
| 377 | #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | |
| 378 | static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
| 379 | {\ | |
| 380 | BLOCK_CIPHER_ecb_loop() \ | |
| ed576acd | 381 | cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \ |
| e79f8773 RL |
382 | return 1;\ |
| 383 | } | |
| 384 | ||
| 709d4be7 | 385 | #define EVP_MAXCHUNK ((size_t)1 << 30) |
| e79f8773 RL |
386 | |
| 387 | #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ | |
| 388 | static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
| 389 | {\ | |
| 390 | while(inl>=EVP_MAXCHUNK) {\ | |
| ed576acd | 391 | int num = EVP_CIPHER_CTX_get_num(ctx);\ |
| 2f5c405a | 392 | cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \ |
| e79f8773 RL |
393 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
| 394 | inl-=EVP_MAXCHUNK;\ | |
| 395 | in +=EVP_MAXCHUNK;\ | |
| 396 | out+=EVP_MAXCHUNK;\ | |
| 397 | }\ | |
| 398 | if (inl) {\ | |
| ed576acd | 399 | int num = EVP_CIPHER_CTX_get_num(ctx);\ |
| 2f5c405a | 400 | cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \ |
| e79f8773 RL |
401 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
| 402 | }\ | |
| 403 | return 1;\ | |
| 404 | } | |
| 405 | ||
| 406 | #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | |
| 407 | static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
| 408 | {\ | |
| 962431d5 | 409 | while(inl>=EVP_MAXCHUNK) {\ |
| ed576acd | 410 | cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\ |
| e79f8773 RL |
411 | inl-=EVP_MAXCHUNK;\ |
| 412 | in +=EVP_MAXCHUNK;\ | |
| 413 | out+=EVP_MAXCHUNK;\ | |
| 962431d5 | 414 | }\ |
| e79f8773 | 415 | if (inl)\ |
| ed576acd | 416 | cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\ |
| e79f8773 RL |
417 | return 1;\ |
| 418 | } | |
| 419 | ||
| 420 | #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | |
| 421 | static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
| 422 | {\ | |
| e8aa8b6c F |
423 | size_t chunk = EVP_MAXCHUNK;\ |
| 424 | if (cbits == 1) chunk >>= 3;\ | |
| 425 | if (inl < chunk) chunk = inl;\ | |
| 962431d5 | 426 | while (inl && inl >= chunk) {\ |
| ed576acd | 427 | int num = EVP_CIPHER_CTX_get_num(ctx);\ |
| e8aa8b6c F |
428 | cprefix##_cfb##cbits##_encrypt(in, out, (long) \ |
| 429 | ((cbits == 1) \ | |
| 430 | && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \ | |
| 604e591e | 431 | ? chunk*8 : chunk), \ |
| 2f5c405a | 432 | &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\ |
| ed576acd | 433 | &num, EVP_CIPHER_CTX_is_encrypting(ctx));\ |
| e8aa8b6c F |
434 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
| 435 | inl -= chunk;\ | |
| 436 | in += chunk;\ | |
| 437 | out += chunk;\ | |
| 438 | if (inl < chunk) chunk = inl;\ | |
| 439 | }\ | |
| 440 | return 1;\ | |
| e79f8773 RL |
441 | } |
| 442 | ||
| 443 | #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ | |
| 444 | BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | |
| 445 | BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | |
| 446 | BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | |
| 447 | BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) | |
| 448 | ||
| 449 | #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \ | |
| 450 | key_len, iv_len, flags, init_key, cleanup, \ | |
| 451 | set_asn1, get_asn1, ctrl) \ | |
| 452 | static const EVP_CIPHER cname##_##mode = { \ | |
| 453 | nid##_##nmode, block_size, key_len, iv_len, \ | |
| 454 | flags | EVP_CIPH_##MODE##_MODE, \ | |
| f6c95e46 | 455 | EVP_ORIG_GLOBAL, \ |
| e79f8773 RL |
456 | init_key, \ |
| 457 | cname##_##mode##_cipher, \ | |
| 458 | cleanup, \ | |
| 459 | sizeof(kstruct), \ | |
| 460 | set_asn1, get_asn1,\ | |
| 461 | ctrl, \ | |
| 462 | NULL \ | |
| 463 | }; \ | |
| 464 | const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; } | |
| 465 | ||
| 466 | #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \ | |
| 467 | iv_len, flags, init_key, cleanup, set_asn1, \ | |
| 468 | get_asn1, ctrl) \ | |
| 469 | BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \ | |
| 470 | iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | |
| 471 | ||
| 472 | #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \ | |
| 473 | iv_len, cbits, flags, init_key, cleanup, \ | |
| 474 | set_asn1, get_asn1, ctrl) \ | |
| 475 | BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \ | |
| 476 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ | |
| 477 | get_asn1, ctrl) | |
| 478 | ||
| 479 | #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \ | |
| 480 | iv_len, cbits, flags, init_key, cleanup, \ | |
| 481 | set_asn1, get_asn1, ctrl) \ | |
| 482 | BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \ | |
| 483 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ | |
| 484 | get_asn1, ctrl) | |
| 485 | ||
| 486 | #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ | |
| 487 | flags, init_key, cleanup, set_asn1, \ | |
| 488 | get_asn1, ctrl) \ | |
| 489 | BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ | |
| 490 | 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | |
| 491 | ||
| 492 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | |
| 493 | nid, block_size, key_len, iv_len, cbits, flags, \ | |
| 494 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
| 495 | BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \ | |
| 496 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
| 497 | BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |
| 498 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
| 499 | BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |
| 500 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
| 501 | BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \ | |
| 502 | init_key, cleanup, set_asn1, get_asn1, ctrl) | |
| 503 | ||
| 504 | /*- | |
| 505 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | |
| 506 | nid, block_size, key_len, iv_len, flags,\ | |
| 507 | init_key, cleanup, set_asn1, get_asn1, ctrl)\ | |
| 508 | static const EVP_CIPHER cname##_cbc = {\ | |
| 509 | nid##_cbc, block_size, key_len, iv_len, \ | |
| 510 | flags | EVP_CIPH_CBC_MODE,\ | |
| f6c95e46 | 511 | EVP_ORIG_GLOBAL,\ |
| e79f8773 RL |
512 | init_key,\ |
| 513 | cname##_cbc_cipher,\ | |
| 514 | cleanup,\ | |
| 515 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
| 516 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
| 517 | set_asn1, get_asn1,\ | |
| 518 | ctrl, \ | |
| 519 | NULL \ | |
| 520 | };\ | |
| 521 | const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\ | |
| 522 | static const EVP_CIPHER cname##_cfb = {\ | |
| 523 | nid##_cfb64, 1, key_len, iv_len, \ | |
| 524 | flags | EVP_CIPH_CFB_MODE,\ | |
| f6c95e46 | 525 | EVP_ORIG_GLOBAL,\ |
| e79f8773 RL |
526 | init_key,\ |
| 527 | cname##_cfb_cipher,\ | |
| 528 | cleanup,\ | |
| 529 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
| 530 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
| 531 | set_asn1, get_asn1,\ | |
| 532 | ctrl,\ | |
| 533 | NULL \ | |
| 534 | };\ | |
| 535 | const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\ | |
| 536 | static const EVP_CIPHER cname##_ofb = {\ | |
| 537 | nid##_ofb64, 1, key_len, iv_len, \ | |
| 538 | flags | EVP_CIPH_OFB_MODE,\ | |
| f6c95e46 | 539 | EVP_ORIG_GLOBAL,\ |
| e79f8773 RL |
540 | init_key,\ |
| 541 | cname##_ofb_cipher,\ | |
| 542 | cleanup,\ | |
| 543 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
| 544 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
| 545 | set_asn1, get_asn1,\ | |
| 546 | ctrl,\ | |
| 547 | NULL \ | |
| 548 | };\ | |
| 549 | const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\ | |
| 550 | static const EVP_CIPHER cname##_ecb = {\ | |
| 551 | nid##_ecb, block_size, key_len, iv_len, \ | |
| 552 | flags | EVP_CIPH_ECB_MODE,\ | |
| f6c95e46 | 553 | EVP_ORIG_GLOBAL,\ |
| e79f8773 RL |
554 | init_key,\ |
| 555 | cname##_ecb_cipher,\ | |
| 556 | cleanup,\ | |
| 557 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
| 558 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
| 559 | set_asn1, get_asn1,\ | |
| 560 | ctrl,\ | |
| 561 | NULL \ | |
| 562 | };\ | |
| 563 | const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } | |
| 564 | */ | |
| 565 | ||
| 566 | #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \ | |
| 567 | block_size, key_len, iv_len, cbits, \ | |
| 568 | flags, init_key, \ | |
| 569 | cleanup, set_asn1, get_asn1, ctrl) \ | |
| 570 | BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ | |
| 571 | BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \ | |
| 572 | cbits, flags, init_key, cleanup, set_asn1, \ | |
| 573 | get_asn1, ctrl) | |
| 574 | ||
| 575 | #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \ | |
| 576 | BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ | |
| 577 | BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ | |
| 578 | NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ | |
| 579 | (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \ | |
| 580 | cipher##_init_key, NULL, NULL, NULL, NULL) | |
| 581 | ||
| 924663c3 JZ |
582 | typedef struct { |
| 583 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
| 584 | unsigned int iv_len; | |
| 585 | unsigned int tag_len; | |
| 586 | } evp_cipher_aead_asn1_params; | |
| 587 | ||
| 588 | int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type, | |
| 589 | evp_cipher_aead_asn1_params *params); | |
| 590 | ||
| 591 | int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type, | |
| 592 | evp_cipher_aead_asn1_params *params); | |
| 593 | ||
| 64954e2f P |
594 | /* |
| 595 | * To support transparent execution of operation in backends other | |
| 596 | * than the "origin" key, we support transparent export/import to | |
| 597 | * those providers, and maintain a cache of the imported keydata, | |
| 598 | * so we don't need to redo the export/import every time we perform | |
| 599 | * the same operation in that same provider. | |
| 600 | * This requires that the "origin" backend (whether it's a legacy or a | |
| 601 | * provider "origin") implements exports, and that the target provider | |
| 602 | * has an EVP_KEYMGMT that implements import. | |
| 603 | */ | |
| 604 | typedef struct { | |
| 605 | EVP_KEYMGMT *keymgmt; | |
| 606 | void *keydata; | |
| 98642df4 | 607 | int selection; |
| 64954e2f P |
608 | } OP_CACHE_ELEM; |
| 609 | ||
| 610 | DEFINE_STACK_OF(OP_CACHE_ELEM) | |
| 611 | ||
| 3aeb9348 | 612 | /* |
| adc9f731 RL |
613 | * An EVP_PKEY can have the following states: |
| 614 | * | |
| 615 | * untyped & empty: | |
| 616 | * | |
| 617 | * type == EVP_PKEY_NONE && keymgmt == NULL | |
| 618 | * | |
| 619 | * typed & empty: | |
| 620 | * | |
| 621 | * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only) | |
| 622 | * || (keymgmt != NULL && keydata == NULL) ## provider side | |
| 623 | * | |
| 624 | * fully assigned: | |
| 625 | * | |
| 626 | * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only) | |
| 627 | * || (keymgmt != NULL && keydata != NULL) ## provider side | |
| 628 | * | |
| 5e5bc836 RL |
629 | * The easiest way to detect a legacy key is: |
| 630 | * | |
| 631 | * keymgmt == NULL && type != EVP_PKEY_NONE | |
| 632 | * | |
| 633 | * The easiest way to detect a provider side key is: | |
| 634 | * | |
| 635 | * keymgmt != NULL | |
| 3aeb9348 | 636 | */ |
| 5e5bc836 RL |
637 | #define evp_pkey_is_blank(pk) \ |
| 638 | ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL) | |
| 639 | #define evp_pkey_is_typed(pk) \ | |
| 640 | ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL) | |
| 4d4928ed RL |
641 | #ifndef FIPS_MODULE |
| 642 | # define evp_pkey_is_assigned(pk) \ | |
| 5e5bc836 | 643 | ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL) |
| 4d4928ed RL |
644 | #else |
| 645 | # define evp_pkey_is_assigned(pk) \ | |
| 646 | ((pk)->keydata != NULL) | |
| 647 | #endif | |
| 5e5bc836 RL |
648 | #define evp_pkey_is_legacy(pk) \ |
| 649 | ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL) | |
| 650 | #define evp_pkey_is_provided(pk) \ | |
| 651 | ((pk)->keymgmt != NULL) | |
| 652 | ||
| b574c6a9 MC |
653 | union legacy_pkey_st { |
| 654 | void *ptr; | |
| 655 | struct rsa_st *rsa; /* RSA */ | |
| 656 | # ifndef OPENSSL_NO_DSA | |
| 657 | struct dsa_st *dsa; /* DSA */ | |
| 658 | # endif | |
| 659 | # ifndef OPENSSL_NO_DH | |
| 660 | struct dh_st *dh; /* DH */ | |
| 661 | # endif | |
| 662 | # ifndef OPENSSL_NO_EC | |
| 663 | struct ec_key_st *ec; /* ECC */ | |
| 4032cd9a | 664 | # ifndef OPENSSL_NO_ECX |
| b574c6a9 | 665 | ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */ |
| 4032cd9a | 666 | # endif |
| eba0e11c | 667 | # endif |
| b574c6a9 MC |
668 | }; |
| 669 | ||
| 3aeb9348 | 670 | struct evp_pkey_st { |
| a94a3e0d | 671 | /* == Legacy attributes == */ |
| 3aeb9348 DSH |
672 | int type; |
| 673 | int save_type; | |
| 3c6ed955 | 674 | |
| f844f9eb | 675 | # ifndef FIPS_MODULE |
| 3c6ed955 RL |
676 | /* |
| 677 | * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD, | |
| 678 | * a pointer to a low level key and possibly a pointer to an engine. | |
| 679 | */ | |
| 3aeb9348 DSH |
680 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 681 | ENGINE *engine; | |
| d19b01ad | 682 | ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */ |
| b574c6a9 MC |
683 | |
| 684 | /* Union to store the reference to an origin legacy key */ | |
| 685 | union legacy_pkey_st pkey; | |
| 686 | ||
| 687 | /* Union to store the reference to a non-origin legacy key */ | |
| 688 | union legacy_pkey_st legacy_cache_pkey; | |
| adc9f731 | 689 | # endif |
| a94a3e0d RL |
690 | |
| 691 | /* == Common attributes == */ | |
| 692 | CRYPTO_REF_COUNT references; | |
| 03273d61 | 693 | CRYPTO_RWLOCK *lock; |
| b247113c | 694 | #ifndef FIPS_MODULE |
| a94a3e0d RL |
695 | STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */ |
| 696 | int save_parameters; | |
| 0588778f | 697 | unsigned int foreign:1; /* the low-level key is using an engine or an app-method */ |
| ff1f7cde AT |
698 | CRYPTO_EX_DATA ex_data; |
| 699 | #endif | |
| a94a3e0d RL |
700 | |
| 701 | /* == Provider attributes == */ | |
| 3c6ed955 RL |
702 | |
| 703 | /* | |
| 704 | * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT | |
| 705 | * and a pointer to the provider side key data. This is never used at | |
| 706 | * the same time as the legacy key data above. | |
| 707 | */ | |
| 708 | EVP_KEYMGMT *keymgmt; | |
| 709 | void *keydata; | |
| 710 | /* | |
| 711 | * If any libcrypto code does anything that may modify the keydata | |
| 712 | * contents, this dirty counter must be incremented. | |
| 713 | */ | |
| 714 | size_t dirty_cnt; | |
| 715 | ||
| a94a3e0d | 716 | /* |
| 3c6ed955 RL |
717 | * To support transparent execution of operation in backends other |
| 718 | * than the "origin" key, we support transparent export/import to | |
| 719 | * those providers, and maintain a cache of the imported keydata, | |
| 720 | * so we don't need to redo the export/import every time we perform | |
| 721 | * the same operation in that same provider. | |
| a94a3e0d | 722 | */ |
| 64954e2f P |
723 | STACK_OF(OP_CACHE_ELEM) *operation_cache; |
| 724 | ||
| 70a1f7b4 | 725 | /* |
| 3c6ed955 RL |
726 | * We keep a copy of that "origin"'s dirty count, so we know if the |
| 727 | * operation cache needs flushing. | |
| 70a1f7b4 RL |
728 | */ |
| 729 | size_t dirty_cnt_copy; | |
| 6508e858 | 730 | |
| b305452f | 731 | /* Cache of key object information */ |
| 6508e858 RL |
732 | struct { |
| 733 | int bits; | |
| 734 | int security_bits; | |
| 8bdb1228 | 735 | int security_category; |
| 6508e858 RL |
736 | int size; |
| 737 | } cache; | |
| 787e1dd9 | 738 | }; /* EVP_PKEY */ |
| 7b9f8f7f | 739 | |
| b96e10b9 RL |
740 | /* The EVP_PKEY_OP_TYPE_ macros are found in include/openssl/evp.h */ |
| 741 | ||
| 787e1dd9 | 742 | # define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \ |
| b96e10b9 | 743 | (((ctx)->operation & EVP_PKEY_OP_TYPE_SIG) != 0) |
| 864b89ce | 744 | |
| 787e1dd9 | 745 | # define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \ |
| b96e10b9 | 746 | (((ctx)->operation & EVP_PKEY_OP_TYPE_DERIVE) != 0) |
| 7b9f8f7f | 747 | |
| 787e1dd9 | 748 | # define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \ |
| b96e10b9 | 749 | (((ctx)->operation & EVP_PKEY_OP_TYPE_CRYPT) != 0) |
| 2c938e2e | 750 | |
| 787e1dd9 | 751 | # define EVP_PKEY_CTX_IS_GEN_OP(ctx) \ |
| b96e10b9 | 752 | (((ctx)->operation & EVP_PKEY_OP_TYPE_GEN) != 0) |
| 62924755 | 753 | |
| 787e1dd9 | 754 | # define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \ |
| b96e10b9 | 755 | (((ctx)->operation & EVP_PKEY_OP_TYPE_DATA) != 0) |
| 9a1c4e41 | 756 | |
| 787e1dd9 | 757 | # define EVP_PKEY_CTX_IS_KEM_OP(ctx) \ |
| b96e10b9 | 758 | (((ctx)->operation & EVP_PKEY_OP_TYPE_KEM) != 0) |
| 80f4fd18 | 759 | |
| d46e010c DB |
760 | struct evp_skey_st { |
| 761 | /* == Common attributes == */ | |
| 762 | CRYPTO_REF_COUNT references; | |
| 763 | CRYPTO_RWLOCK *lock; | |
| 764 | ||
| 765 | void *keydata; /* Alg-specific key data */ | |
| 766 | EVP_SKEYMGMT *skeymgmt; /* Import, export, manage */ | |
| 767 | }; /* EVP_SKEY */ | |
| 768 | ||
| b3599dbb MC |
769 | void openssl_add_all_ciphers_int(void); |
| 770 | void openssl_add_all_digests_int(void); | |
| 771 | void evp_cleanup_int(void); | |
| 0822e89a | 772 | void evp_app_cleanup_int(void); |
| b4250010 | 773 | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx, |
| 3c6ed955 RL |
774 | EVP_KEYMGMT **keymgmt, |
| 775 | const char *propquery); | |
| f844f9eb | 776 | #ifndef FIPS_MODULE |
| 4ce1025a | 777 | int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src); |
| b574c6a9 | 778 | void *evp_pkey_get_legacy(EVP_PKEY *pk); |
| 62924755 | 779 | void evp_pkey_free_legacy(EVP_PKEY *x); |
| 4f0831b8 TM |
780 | EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf, |
| 781 | OSSL_LIB_CTX *libctx, const char *propq); | |
| 62924755 | 782 | #endif |
| 9d6fcd42 | 783 | |
| 68552cde RL |
784 | /* |
| 785 | * KEYMGMT utility functions | |
| 786 | */ | |
| af836c22 RL |
787 | |
| 788 | /* | |
| 789 | * Key import structure and helper function, to be used as an export callback | |
| 790 | */ | |
| 791 | struct evp_keymgmt_util_try_import_data_st { | |
| 792 | EVP_KEYMGMT *keymgmt; | |
| 793 | void *keydata; | |
| 794 | ||
| 795 | int selection; | |
| 796 | }; | |
| 797 | int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg); | |
| 798 | int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt, | |
| 799 | void *keydata); | |
| 800 | EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata); | |
| 801 | ||
| 655f73ce RL |
802 | int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, |
| 803 | OSSL_CALLBACK *export_cb, void *export_cbarg); | |
| 98642df4 SS |
804 | void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt, |
| 805 | int selection); | |
| 64954e2f | 806 | OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk, |
| 98642df4 SS |
807 | EVP_KEYMGMT *keymgmt, |
| 808 | int selection); | |
| 36424806 | 809 | int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk); |
| 98642df4 SS |
810 | int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt, |
| 811 | void *keydata, int selection); | |
| 3c6ed955 | 812 | void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk); |
| 68552cde | 813 | void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, |
| b305452f | 814 | int selection, const OSSL_PARAM params[]); |
| 157ded39 | 815 | int evp_keymgmt_util_has(EVP_PKEY *pk, int selection); |
| 1e9101c4 | 816 | int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection); |
| ff3b59e1 | 817 | int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection); |
| 62924755 RL |
818 | void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, |
| 819 | void *genctx, OSSL_CALLBACK *cb, void *cbarg); | |
| 3b924da0 RL |
820 | int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt, |
| 821 | void *keydata, | |
| 822 | char *mdname, size_t mdname_sz); | |
| 5246183e RL |
823 | const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt, |
| 824 | int op_id); | |
| 70a1f7b4 | 825 | |
| 68552cde RL |
826 | /* |
| 827 | * KEYMGMT provider interface functions | |
| 828 | */ | |
| b305452f RL |
829 | void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt); |
| 830 | void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata); | |
| 831 | int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, | |
| 832 | void *keydata, OSSL_PARAM params[]); | |
| 4fe54d67 NT |
833 | int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, |
| 834 | void *keydata, const OSSL_PARAM params[]); | |
| 1be63b3e P |
835 | void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection, |
| 836 | const OSSL_PARAM params[]); | |
| 1a5632e0 | 837 | int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx, |
| 420a0874 | 838 | void *templ); |
| 1a5632e0 RL |
839 | int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx, |
| 840 | const OSSL_PARAM params[]); | |
| d9346c59 | 841 | int evp_keymgmt_gen_get_params(const EVP_KEYMGMT *keymgmt, |
| 842 | void *genctx, OSSL_PARAM params[]); | |
| 1a5632e0 RL |
843 | void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx, |
| 844 | OSSL_CALLBACK *cb, void *cbarg); | |
| 845 | void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx); | |
| b305452f | 846 | |
| f616ad4b | 847 | int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt); |
| 5dacb38c RL |
848 | void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt, |
| 849 | const void *objref, size_t objref_sz); | |
| 850 | ||
| b305452f RL |
851 | int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection); |
| 852 | int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata, | |
| 899e2564 | 853 | int selection, int checktype); |
| bee5d6cd RL |
854 | int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt, |
| 855 | const void *keydata1, const void *keydata2, | |
| 856 | int selection); | |
| b305452f RL |
857 | |
| 858 | int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata, | |
| 859 | int selection, const OSSL_PARAM params[]); | |
| 860 | const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt, | |
| 861 | int selection); | |
| 862 | int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata, | |
| 863 | int selection, OSSL_CALLBACK *param_cb, void *cbarg); | |
| 864 | const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt, | |
| 865 | int selection); | |
| 4a9fe33c | 866 | void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, |
| b4f447c0 | 867 | const void *keydata_from, int selection); |
| 4cfcc7e1 TM |
868 | EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov, |
| 869 | const char *name, | |
| 870 | const char *properties); | |
| 12603de6 | 871 | |
| d46e010c DB |
872 | /* |
| 873 | * SKEYMGMT provider interface functions | |
| 874 | */ | |
| 3425da50 | 875 | EVP_SKEY *evp_skey_alloc(EVP_SKEYMGMT *skeymgmt); |
| d46e010c DB |
876 | void evp_skeymgmt_freedata(const EVP_SKEYMGMT *keymgmt, void *keyddata); |
| 877 | void *evp_skeymgmt_import(const EVP_SKEYMGMT *skeymgmt, int selection, const OSSL_PARAM params[]); | |
| 878 | int evp_skeymgmt_export(const EVP_SKEYMGMT *skeymgmt, void *keydata, | |
| 879 | int selection, OSSL_CALLBACK *param_cb, void *cbarg); | |
| 880 | void *evp_skeymgmt_generate(const EVP_SKEYMGMT *skeymgmt, const OSSL_PARAM params[]); | |
| 787a083d SS |
881 | EVP_SKEYMGMT *evp_skeymgmt_fetch_from_prov(OSSL_PROVIDER *prov, |
| 882 | const char *name, | |
| 883 | const char *properties); | |
| 884 | ||
| 46f4e1be | 885 | /* Pulling defines out of C source files */ |
| 9d6fcd42 | 886 | |
| 80ce21fe F |
887 | # define EVP_RC4_KEY_SIZE 16 |
| 888 | # ifndef TLS1_1_VERSION | |
| 889 | # define TLS1_1_VERSION 0x0302 | |
| 890 | # endif | |
| c0804614 MC |
891 | |
| 892 | void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags); | |
| 893 | ||
| 894 | /* EVP_ENCODE_CTX flags */ | |
| 3fd59700 MC |
895 | /* Don't generate new lines when encoding */ |
| 896 | #define EVP_ENCODE_CTX_NO_NEWLINES 1 | |
| 897 | /* Use the SRP base64 alphabet instead of the standard one */ | |
| 898 | #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2 | |
| 7606bed9 | 899 | |
| b4250010 DMSP |
900 | const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx, |
| 901 | const char *name); | |
| 902 | const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, | |
| 903 | const char *name); | |
| e683582b | 904 | |
| 4e17fb00 SL |
905 | int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen, |
| 906 | const unsigned char *salt, int saltlen, int iter, | |
| 907 | const EVP_MD *digest, int keylen, | |
| 908 | unsigned char *out, | |
| 909 | OSSL_LIB_CTX *libctx, const char *propq); | |
| 5ccada09 | 910 | |
| 80ce21fe | 911 | # ifndef FIPS_MODULE |
| 4fe54d67 NT |
912 | /* |
| 913 | * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). | |
| 914 | * | |
| 915 | * Return 1 on success, 0 or negative for errors. | |
| 916 | * | |
| 917 | * In particular they return -2 if any of the params is not supported. | |
| 918 | * | |
| f844f9eb | 919 | * They are not available in FIPS_MODULE as they depend on |
| 4fe54d67 NT |
920 | * - EVP_PKEY_CTX_{get,set}_params() |
| 921 | * - EVP_PKEY_CTX_{gettable,settable}_params() | |
| 922 | * | |
| 923 | */ | |
| 924 | int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); | |
| 925 | int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); | |
| 472a88b7 | 926 | |
| d8652be0 | 927 | EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id, |
| b4250010 | 928 | OSSL_LIB_CTX *libctx, const char *propq); |
| 50914496 | 929 | int evp_pkey_name2type(const char *name); |
| 977e95b9 | 930 | const char *evp_pkey_type2name(int type); |
| 86df26b3 | 931 | |
| 86df26b3 | 932 | int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx); |
| 80ce21fe F |
933 | # endif /* !defined(FIPS_MODULE) */ |
| 934 | ||
| 60640d79 | 935 | int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx); |
| 2e4d0677 | 936 | int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov); |
| 60640d79 | 937 | |
| 589fbc18 MC |
938 | int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable, |
| 939 | int loadconfig); | |
| b4250010 | 940 | int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq, |
| 447588b6 MC |
941 | int loadconfig, int mirrored); |
| 942 | char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig); | |
| 3101ab60 | 943 | |
| c0b7dac6 | 944 | void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest); |
| fe5c5cb8 TM |
945 | /* just free the algctx if set, returns 0 on inconsistent state of ctx */ |
| 946 | int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx); | |
| 80ce21fe | 947 | |
| e19246dc RL |
948 | /* Three possible states: */ |
| 949 | # define EVP_PKEY_STATE_UNKNOWN 0 | |
| 950 | # define EVP_PKEY_STATE_LEGACY 1 | |
| 951 | # define EVP_PKEY_STATE_PROVIDER 2 | |
| 952 | int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx); | |
| 953 | ||
| 9a1c4e41 RL |
954 | /* These two must ONLY be called for provider side operations */ |
| 955 | int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx, | |
| 956 | int keytype, int optype, | |
| 957 | int cmd, int p1, void *p2); | |
| 958 | int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx, | |
| 959 | const char *name, const char *value); | |
| 960 | ||
| 961 | /* These two must ONLY be called for legacy operations */ | |
| 56784203 | 962 | int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); |
| 9a1c4e41 RL |
963 | int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); |
| 964 | ||
| 965 | /* This must ONLY be called for legacy EVP_PKEYs */ | |
| 966 | int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params); | |
| 967 | ||
| 7bc0fdd3 MC |
968 | /* Same as the public get0 functions but are not const */ |
| 969 | # ifndef OPENSSL_NO_DEPRECATED_3_0 | |
| 970 | DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey); | |
| 971 | EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey); | |
| 972 | RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey); | |
| 973 | # endif | |
| 974 | ||
| f2e3584d P |
975 | /* Get internal identification number routines */ |
| 976 | int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher); | |
| 977 | int evp_cipher_get_number(const EVP_CIPHER *cipher); | |
| 978 | int evp_kdf_get_number(const EVP_KDF *kdf); | |
| 979 | int evp_kem_get_number(const EVP_KEM *wrap); | |
| 980 | int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch); | |
| 981 | int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt); | |
| 8aa3781b | 982 | int evp_keymgmt_get_legacy_alg(const EVP_KEYMGMT *keymgmt); |
| f2e3584d P |
983 | int evp_mac_get_number(const EVP_MAC *mac); |
| 984 | int evp_md_get_number(const EVP_MD *md); | |
| 985 | int evp_rand_get_number(const EVP_RAND *rand); | |
| 7998e7dc MSP |
986 | int evp_rand_can_seed(EVP_RAND_CTX *ctx); |
| 987 | size_t evp_rand_get_seed(EVP_RAND_CTX *ctx, | |
| 988 | unsigned char **buffer, | |
| 989 | int entropy, size_t min_len, size_t max_len, | |
| 990 | int prediction_resistance, | |
| 991 | const unsigned char *adin, size_t adin_len); | |
| 992 | void evp_rand_clear_seed(EVP_RAND_CTX *ctx, | |
| 993 | unsigned char *buffer, size_t b_len); | |
| f2e3584d P |
994 | int evp_signature_get_number(const EVP_SIGNATURE *signature); |
| 995 | ||
| 36b91a19 DDO |
996 | int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp, |
| 997 | size_t *outlenp, size_t expected_outlen, | |
| 998 | const unsigned char *in, size_t inlen); | |
| 999 | ||
| fe79159b DB |
1000 | int ossl_md2hmacnid(int mdnid); |
| 1001 | int ossl_hmac2mdnid(int hmac_nid); | |
| 1002 | ||
| 80ce21fe | 1003 | #endif /* OSSL_CRYPTO_EVP_H */ |