evp.h 37 KB

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