evp.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * Copyright 2015-2024 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_squeeze_fn *dsqueeze;
  256. OSSL_FUNC_digest_digest_fn *digest;
  257. OSSL_FUNC_digest_freectx_fn *freectx;
  258. OSSL_FUNC_digest_dupctx_fn *dupctx;
  259. OSSL_FUNC_digest_get_params_fn *get_params;
  260. OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
  261. OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
  262. OSSL_FUNC_digest_gettable_params_fn *gettable_params;
  263. OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
  264. OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
  265. } /* EVP_MD */ ;
  266. struct evp_cipher_st {
  267. int nid;
  268. int block_size;
  269. /* Default value for variable length ciphers */
  270. int key_len;
  271. int iv_len;
  272. /* Legacy structure members */
  273. /* Various flags */
  274. unsigned long flags;
  275. /* How the EVP_CIPHER was created. */
  276. int origin;
  277. /* init key */
  278. int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
  279. const unsigned char *iv, int enc);
  280. /* encrypt/decrypt data */
  281. int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
  282. const unsigned char *in, size_t inl);
  283. /* cleanup ctx */
  284. int (*cleanup) (EVP_CIPHER_CTX *);
  285. /* how big ctx->cipher_data needs to be */
  286. int ctx_size;
  287. /* Populate a ASN1_TYPE with parameters */
  288. int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
  289. /* Get parameters from a ASN1_TYPE */
  290. int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
  291. /* Miscellaneous operations */
  292. int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
  293. /* Application data */
  294. void *app_data;
  295. /* New structure members */
  296. /* Above comment to be removed when legacy has gone */
  297. int name_id;
  298. char *type_name;
  299. const char *description;
  300. OSSL_PROVIDER *prov;
  301. CRYPTO_REF_COUNT refcnt;
  302. OSSL_FUNC_cipher_newctx_fn *newctx;
  303. OSSL_FUNC_cipher_encrypt_init_fn *einit;
  304. OSSL_FUNC_cipher_decrypt_init_fn *dinit;
  305. OSSL_FUNC_cipher_update_fn *cupdate;
  306. OSSL_FUNC_cipher_final_fn *cfinal;
  307. OSSL_FUNC_cipher_cipher_fn *ccipher;
  308. OSSL_FUNC_cipher_freectx_fn *freectx;
  309. OSSL_FUNC_cipher_dupctx_fn *dupctx;
  310. OSSL_FUNC_cipher_get_params_fn *get_params;
  311. OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
  312. OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
  313. OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
  314. OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
  315. OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
  316. } /* EVP_CIPHER */ ;
  317. /* Macros to code block cipher wrappers */
  318. /* Wrapper functions for each cipher mode */
  319. #define EVP_C_DATA(kstruct, ctx) \
  320. ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  321. #define BLOCK_CIPHER_ecb_loop() \
  322. size_t i, bl; \
  323. bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \
  324. if (inl < bl) return 1;\
  325. inl -= bl; \
  326. for (i=0; i <= inl; i+=bl)
  327. #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  328. static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  329. {\
  330. BLOCK_CIPHER_ecb_loop() \
  331. cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \
  332. return 1;\
  333. }
  334. #define EVP_MAXCHUNK ((size_t)1 << 30)
  335. #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
  336. static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  337. {\
  338. while(inl>=EVP_MAXCHUNK) {\
  339. int num = EVP_CIPHER_CTX_get_num(ctx);\
  340. cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
  341. EVP_CIPHER_CTX_set_num(ctx, num);\
  342. inl-=EVP_MAXCHUNK;\
  343. in +=EVP_MAXCHUNK;\
  344. out+=EVP_MAXCHUNK;\
  345. }\
  346. if (inl) {\
  347. int num = EVP_CIPHER_CTX_get_num(ctx);\
  348. cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
  349. EVP_CIPHER_CTX_set_num(ctx, num);\
  350. }\
  351. return 1;\
  352. }
  353. #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  354. static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  355. {\
  356. while(inl>=EVP_MAXCHUNK) \
  357. {\
  358. cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\
  359. inl-=EVP_MAXCHUNK;\
  360. in +=EVP_MAXCHUNK;\
  361. out+=EVP_MAXCHUNK;\
  362. }\
  363. if (inl)\
  364. cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\
  365. return 1;\
  366. }
  367. #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  368. static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  369. {\
  370. size_t chunk = EVP_MAXCHUNK;\
  371. if (cbits == 1) chunk >>= 3;\
  372. if (inl < chunk) chunk = inl;\
  373. while (inl && inl >= chunk)\
  374. {\
  375. int num = EVP_CIPHER_CTX_get_num(ctx);\
  376. cprefix##_cfb##cbits##_encrypt(in, out, (long) \
  377. ((cbits == 1) \
  378. && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
  379. ? chunk*8 : chunk), \
  380. &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\
  381. &num, EVP_CIPHER_CTX_is_encrypting(ctx));\
  382. EVP_CIPHER_CTX_set_num(ctx, num);\
  383. inl -= chunk;\
  384. in += chunk;\
  385. out += chunk;\
  386. if (inl < chunk) chunk = inl;\
  387. }\
  388. return 1;\
  389. }
  390. #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  391. BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  392. BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  393. BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  394. BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
  395. #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
  396. key_len, iv_len, flags, init_key, cleanup, \
  397. set_asn1, get_asn1, ctrl) \
  398. static const EVP_CIPHER cname##_##mode = { \
  399. nid##_##nmode, block_size, key_len, iv_len, \
  400. flags | EVP_CIPH_##MODE##_MODE, \
  401. EVP_ORIG_GLOBAL, \
  402. init_key, \
  403. cname##_##mode##_cipher, \
  404. cleanup, \
  405. sizeof(kstruct), \
  406. set_asn1, get_asn1,\
  407. ctrl, \
  408. NULL \
  409. }; \
  410. const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
  411. #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
  412. iv_len, flags, init_key, cleanup, set_asn1, \
  413. get_asn1, ctrl) \
  414. BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
  415. iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  416. #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
  417. iv_len, cbits, flags, init_key, cleanup, \
  418. set_asn1, get_asn1, ctrl) \
  419. BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
  420. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  421. get_asn1, ctrl)
  422. #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
  423. iv_len, cbits, flags, init_key, cleanup, \
  424. set_asn1, get_asn1, ctrl) \
  425. BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
  426. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  427. get_asn1, ctrl)
  428. #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
  429. flags, init_key, cleanup, set_asn1, \
  430. get_asn1, ctrl) \
  431. BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
  432. 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  433. #define BLOCK_CIPHER_defs(cname, kstruct, \
  434. nid, block_size, key_len, iv_len, cbits, flags, \
  435. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  436. BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
  437. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  438. BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
  439. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  440. BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
  441. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  442. BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
  443. init_key, cleanup, set_asn1, get_asn1, ctrl)
  444. /*-
  445. #define BLOCK_CIPHER_defs(cname, kstruct, \
  446. nid, block_size, key_len, iv_len, flags,\
  447. init_key, cleanup, set_asn1, get_asn1, ctrl)\
  448. static const EVP_CIPHER cname##_cbc = {\
  449. nid##_cbc, block_size, key_len, iv_len, \
  450. flags | EVP_CIPH_CBC_MODE,\
  451. EVP_ORIG_GLOBAL,\
  452. init_key,\
  453. cname##_cbc_cipher,\
  454. cleanup,\
  455. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  456. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  457. set_asn1, get_asn1,\
  458. ctrl, \
  459. NULL \
  460. };\
  461. const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
  462. static const EVP_CIPHER cname##_cfb = {\
  463. nid##_cfb64, 1, key_len, iv_len, \
  464. flags | EVP_CIPH_CFB_MODE,\
  465. EVP_ORIG_GLOBAL,\
  466. init_key,\
  467. cname##_cfb_cipher,\
  468. cleanup,\
  469. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  470. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  471. set_asn1, get_asn1,\
  472. ctrl,\
  473. NULL \
  474. };\
  475. const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
  476. static const EVP_CIPHER cname##_ofb = {\
  477. nid##_ofb64, 1, key_len, iv_len, \
  478. flags | EVP_CIPH_OFB_MODE,\
  479. EVP_ORIG_GLOBAL,\
  480. init_key,\
  481. cname##_ofb_cipher,\
  482. cleanup,\
  483. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  484. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  485. set_asn1, get_asn1,\
  486. ctrl,\
  487. NULL \
  488. };\
  489. const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
  490. static const EVP_CIPHER cname##_ecb = {\
  491. nid##_ecb, block_size, key_len, iv_len, \
  492. flags | EVP_CIPH_ECB_MODE,\
  493. EVP_ORIG_GLOBAL,\
  494. init_key,\
  495. cname##_ecb_cipher,\
  496. cleanup,\
  497. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  498. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  499. set_asn1, get_asn1,\
  500. ctrl,\
  501. NULL \
  502. };\
  503. const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
  504. */
  505. #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
  506. block_size, key_len, iv_len, cbits, \
  507. flags, init_key, \
  508. cleanup, set_asn1, get_asn1, ctrl) \
  509. BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  510. BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
  511. cbits, flags, init_key, cleanup, set_asn1, \
  512. get_asn1, ctrl)
  513. #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
  514. BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
  515. BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
  516. NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
  517. (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
  518. cipher##_init_key, NULL, NULL, NULL, NULL)
  519. typedef struct {
  520. unsigned char iv[EVP_MAX_IV_LENGTH];
  521. unsigned int iv_len;
  522. unsigned int tag_len;
  523. } evp_cipher_aead_asn1_params;
  524. int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  525. evp_cipher_aead_asn1_params *params);
  526. int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  527. evp_cipher_aead_asn1_params *params);
  528. /*
  529. * To support transparent execution of operation in backends other
  530. * than the "origin" key, we support transparent export/import to
  531. * those providers, and maintain a cache of the imported keydata,
  532. * so we don't need to redo the export/import every time we perform
  533. * the same operation in that same provider.
  534. * This requires that the "origin" backend (whether it's a legacy or a
  535. * provider "origin") implements exports, and that the target provider
  536. * has an EVP_KEYMGMT that implements import.
  537. */
  538. typedef struct {
  539. EVP_KEYMGMT *keymgmt;
  540. void *keydata;
  541. int selection;
  542. } OP_CACHE_ELEM;
  543. DEFINE_STACK_OF(OP_CACHE_ELEM)
  544. /*
  545. * An EVP_PKEY can have the following states:
  546. *
  547. * untyped & empty:
  548. *
  549. * type == EVP_PKEY_NONE && keymgmt == NULL
  550. *
  551. * typed & empty:
  552. *
  553. * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
  554. * || (keymgmt != NULL && keydata == NULL) ## provider side
  555. *
  556. * fully assigned:
  557. *
  558. * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
  559. * || (keymgmt != NULL && keydata != NULL) ## provider side
  560. *
  561. * The easiest way to detect a legacy key is:
  562. *
  563. * keymgmt == NULL && type != EVP_PKEY_NONE
  564. *
  565. * The easiest way to detect a provider side key is:
  566. *
  567. * keymgmt != NULL
  568. */
  569. #define evp_pkey_is_blank(pk) \
  570. ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
  571. #define evp_pkey_is_typed(pk) \
  572. ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
  573. #ifndef FIPS_MODULE
  574. # define evp_pkey_is_assigned(pk) \
  575. ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
  576. #else
  577. # define evp_pkey_is_assigned(pk) \
  578. ((pk)->keydata != NULL)
  579. #endif
  580. #define evp_pkey_is_legacy(pk) \
  581. ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
  582. #define evp_pkey_is_provided(pk) \
  583. ((pk)->keymgmt != NULL)
  584. union legacy_pkey_st {
  585. void *ptr;
  586. struct rsa_st *rsa; /* RSA */
  587. # ifndef OPENSSL_NO_DSA
  588. struct dsa_st *dsa; /* DSA */
  589. # endif
  590. # ifndef OPENSSL_NO_DH
  591. struct dh_st *dh; /* DH */
  592. # endif
  593. # ifndef OPENSSL_NO_EC
  594. struct ec_key_st *ec; /* ECC */
  595. # ifndef OPENSSL_NO_ECX
  596. ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
  597. # endif
  598. # endif
  599. };
  600. struct evp_pkey_st {
  601. /* == Legacy attributes == */
  602. int type;
  603. int save_type;
  604. # ifndef FIPS_MODULE
  605. /*
  606. * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
  607. * a pointer to a low level key and possibly a pointer to an engine.
  608. */
  609. const EVP_PKEY_ASN1_METHOD *ameth;
  610. ENGINE *engine;
  611. ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
  612. /* Union to store the reference to an origin legacy key */
  613. union legacy_pkey_st pkey;
  614. /* Union to store the reference to a non-origin legacy key */
  615. union legacy_pkey_st legacy_cache_pkey;
  616. # endif
  617. /* == Common attributes == */
  618. CRYPTO_REF_COUNT references;
  619. CRYPTO_RWLOCK *lock;
  620. #ifndef FIPS_MODULE
  621. STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
  622. int save_parameters;
  623. unsigned int foreign:1; /* the low-level key is using an engine or an app-method */
  624. CRYPTO_EX_DATA ex_data;
  625. #endif
  626. /* == Provider attributes == */
  627. /*
  628. * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
  629. * and a pointer to the provider side key data. This is never used at
  630. * the same time as the legacy key data above.
  631. */
  632. EVP_KEYMGMT *keymgmt;
  633. void *keydata;
  634. /*
  635. * If any libcrypto code does anything that may modify the keydata
  636. * contents, this dirty counter must be incremented.
  637. */
  638. size_t dirty_cnt;
  639. /*
  640. * To support transparent execution of operation in backends other
  641. * than the "origin" key, we support transparent export/import to
  642. * those providers, and maintain a cache of the imported keydata,
  643. * so we don't need to redo the export/import every time we perform
  644. * the same operation in that same provider.
  645. */
  646. STACK_OF(OP_CACHE_ELEM) *operation_cache;
  647. /*
  648. * We keep a copy of that "origin"'s dirty count, so we know if the
  649. * operation cache needs flushing.
  650. */
  651. size_t dirty_cnt_copy;
  652. /* Cache of key object information */
  653. struct {
  654. int bits;
  655. int security_bits;
  656. int size;
  657. } cache;
  658. } /* EVP_PKEY */ ;
  659. #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
  660. ((ctx)->operation == EVP_PKEY_OP_SIGN \
  661. || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \
  662. || (ctx)->operation == EVP_PKEY_OP_VERIFY \
  663. || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \
  664. || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER)
  665. #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
  666. ((ctx)->operation == EVP_PKEY_OP_DERIVE)
  667. #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
  668. ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
  669. || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
  670. #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
  671. ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
  672. || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
  673. #define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \
  674. ((ctx)->operation == EVP_PKEY_OP_FROMDATA)
  675. #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
  676. ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
  677. || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
  678. void openssl_add_all_ciphers_int(void);
  679. void openssl_add_all_digests_int(void);
  680. void evp_cleanup_int(void);
  681. void evp_app_cleanup_int(void);
  682. void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
  683. EVP_KEYMGMT **keymgmt,
  684. const char *propquery);
  685. #ifndef FIPS_MODULE
  686. int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
  687. void *evp_pkey_get_legacy(EVP_PKEY *pk);
  688. void evp_pkey_free_legacy(EVP_PKEY *x);
  689. EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf,
  690. OSSL_LIB_CTX *libctx, const char *propq);
  691. #endif
  692. /*
  693. * KEYMGMT utility functions
  694. */
  695. /*
  696. * Key import structure and helper function, to be used as an export callback
  697. */
  698. struct evp_keymgmt_util_try_import_data_st {
  699. EVP_KEYMGMT *keymgmt;
  700. void *keydata;
  701. int selection;
  702. };
  703. int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
  704. int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
  705. void *keydata);
  706. EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
  707. int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
  708. OSSL_CALLBACK *export_cb, void *export_cbarg);
  709. void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
  710. int selection);
  711. OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
  712. EVP_KEYMGMT *keymgmt,
  713. int selection);
  714. int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk);
  715. int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
  716. void *keydata, int selection);
  717. void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
  718. void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
  719. int selection, const OSSL_PARAM params[]);
  720. int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
  721. int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
  722. int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
  723. void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
  724. void *genctx, OSSL_CALLBACK *cb, void *cbarg);
  725. int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
  726. void *keydata,
  727. char *mdname, size_t mdname_sz);
  728. const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
  729. int op_id);
  730. /*
  731. * KEYMGMT provider interface functions
  732. */
  733. void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
  734. void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
  735. int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
  736. void *keydata, OSSL_PARAM params[]);
  737. int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
  738. void *keydata, const OSSL_PARAM params[]);
  739. void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
  740. const OSSL_PARAM params[]);
  741. int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
  742. void *templ);
  743. int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
  744. const OSSL_PARAM params[]);
  745. void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
  746. OSSL_CALLBACK *cb, void *cbarg);
  747. void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
  748. int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt);
  749. void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
  750. const void *objref, size_t objref_sz);
  751. int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
  752. int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
  753. int selection, int checktype);
  754. int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
  755. const void *keydata1, const void *keydata2,
  756. int selection);
  757. int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
  758. int selection, const OSSL_PARAM params[]);
  759. const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
  760. int selection);
  761. int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
  762. int selection, OSSL_CALLBACK *param_cb, void *cbarg);
  763. const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
  764. int selection);
  765. void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt,
  766. const void *keydata_from, int selection);
  767. EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
  768. const char *name,
  769. const char *properties);
  770. /* Pulling defines out of C source files */
  771. # define EVP_RC4_KEY_SIZE 16
  772. # ifndef TLS1_1_VERSION
  773. # define TLS1_1_VERSION 0x0302
  774. # endif
  775. void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
  776. /* EVP_ENCODE_CTX flags */
  777. /* Don't generate new lines when encoding */
  778. #define EVP_ENCODE_CTX_NO_NEWLINES 1
  779. /* Use the SRP base64 alphabet instead of the standard one */
  780. #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
  781. const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
  782. const char *name);
  783. const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx,
  784. const char *name);
  785. int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
  786. const unsigned char *salt, int saltlen, int iter,
  787. const EVP_MD *digest, int keylen,
  788. unsigned char *out,
  789. OSSL_LIB_CTX *libctx, const char *propq);
  790. # ifndef FIPS_MODULE
  791. /*
  792. * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
  793. *
  794. * Return 1 on success, 0 or negative for errors.
  795. *
  796. * In particular they return -2 if any of the params is not supported.
  797. *
  798. * They are not available in FIPS_MODULE as they depend on
  799. * - EVP_PKEY_CTX_{get,set}_params()
  800. * - EVP_PKEY_CTX_{gettable,settable}_params()
  801. *
  802. */
  803. int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
  804. int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
  805. EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
  806. OSSL_LIB_CTX *libctx, const char *propq);
  807. int evp_pkey_name2type(const char *name);
  808. const char *evp_pkey_type2name(int type);
  809. int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
  810. # endif /* !defined(FIPS_MODULE) */
  811. int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx);
  812. int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov);
  813. int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
  814. int loadconfig);
  815. int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
  816. int loadconfig, int mirrored);
  817. char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig);
  818. void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest);
  819. /* just free the algctx if set, returns 0 on inconsistent state of ctx */
  820. int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx);
  821. /* Three possible states: */
  822. # define EVP_PKEY_STATE_UNKNOWN 0
  823. # define EVP_PKEY_STATE_LEGACY 1
  824. # define EVP_PKEY_STATE_PROVIDER 2
  825. int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx);
  826. /* These two must ONLY be called for provider side operations */
  827. int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx,
  828. int keytype, int optype,
  829. int cmd, int p1, void *p2);
  830. int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx,
  831. const char *name, const char *value);
  832. /* These two must ONLY be called for legacy operations */
  833. int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params);
  834. int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
  835. /* This must ONLY be called for legacy EVP_PKEYs */
  836. int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params);
  837. /* Same as the public get0 functions but are not const */
  838. # ifndef OPENSSL_NO_DEPRECATED_3_0
  839. DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey);
  840. EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey);
  841. RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey);
  842. # endif
  843. /* Get internal identification number routines */
  844. int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher);
  845. int evp_cipher_get_number(const EVP_CIPHER *cipher);
  846. int evp_kdf_get_number(const EVP_KDF *kdf);
  847. int evp_kem_get_number(const EVP_KEM *wrap);
  848. int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch);
  849. int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt);
  850. int evp_keymgmt_get_legacy_alg(const EVP_KEYMGMT *keymgmt);
  851. int evp_mac_get_number(const EVP_MAC *mac);
  852. int evp_md_get_number(const EVP_MD *md);
  853. int evp_rand_get_number(const EVP_RAND *rand);
  854. int evp_rand_can_seed(EVP_RAND_CTX *ctx);
  855. size_t evp_rand_get_seed(EVP_RAND_CTX *ctx,
  856. unsigned char **buffer,
  857. int entropy, size_t min_len, size_t max_len,
  858. int prediction_resistance,
  859. const unsigned char *adin, size_t adin_len);
  860. void evp_rand_clear_seed(EVP_RAND_CTX *ctx,
  861. unsigned char *buffer, size_t b_len);
  862. int evp_signature_get_number(const EVP_SIGNATURE *signature);
  863. int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp,
  864. size_t *outlenp, size_t expected_outlen,
  865. const unsigned char *in, size_t inlen);
  866. #endif /* OSSL_CRYPTO_EVP_H */