evp.h 37 KB

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