evp.h 34 KB

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