THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

612 lines
23KB

  1. /*
  2. * Copyright 2015-2018 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. #include <openssl/evp.h>
  10. #include <openssl/core_numbers.h>
  11. #include "internal/refcount.h"
  12. /*
  13. * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
  14. * values in evp.h
  15. */
  16. #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
  17. struct evp_pkey_ctx_st {
  18. EVP_KEYEXCH *exchange;
  19. void *exchprovctx;
  20. /* Legacy fields below */
  21. /* Method associated with this operation */
  22. const EVP_PKEY_METHOD *pmeth;
  23. /* Engine that implements this method or NULL if builtin */
  24. ENGINE *engine;
  25. /* Key: may be NULL */
  26. EVP_PKEY *pkey;
  27. /* Peer key for key agreement, may be NULL */
  28. EVP_PKEY *peerkey;
  29. /* Actual operation */
  30. int operation;
  31. /* Algorithm specific data */
  32. void *data;
  33. /* Application specific data */
  34. void *app_data;
  35. /* Keygen callback */
  36. EVP_PKEY_gen_cb *pkey_gencb;
  37. /* implementation specific keygen data */
  38. int *keygen_info;
  39. int keygen_info_count;
  40. } /* EVP_PKEY_CTX */ ;
  41. #define EVP_PKEY_FLAG_DYNAMIC 1
  42. struct evp_pkey_method_st {
  43. int pkey_id;
  44. int flags;
  45. int (*init) (EVP_PKEY_CTX *ctx);
  46. int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
  47. void (*cleanup) (EVP_PKEY_CTX *ctx);
  48. int (*paramgen_init) (EVP_PKEY_CTX *ctx);
  49. int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  50. int (*keygen_init) (EVP_PKEY_CTX *ctx);
  51. int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  52. int (*sign_init) (EVP_PKEY_CTX *ctx);
  53. int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  54. const unsigned char *tbs, size_t tbslen);
  55. int (*verify_init) (EVP_PKEY_CTX *ctx);
  56. int (*verify) (EVP_PKEY_CTX *ctx,
  57. const unsigned char *sig, size_t siglen,
  58. const unsigned char *tbs, size_t tbslen);
  59. int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
  60. int (*verify_recover) (EVP_PKEY_CTX *ctx,
  61. unsigned char *rout, size_t *routlen,
  62. const unsigned char *sig, size_t siglen);
  63. int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  64. int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  65. EVP_MD_CTX *mctx);
  66. int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  67. int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
  68. EVP_MD_CTX *mctx);
  69. int (*encrypt_init) (EVP_PKEY_CTX *ctx);
  70. int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  71. const unsigned char *in, size_t inlen);
  72. int (*decrypt_init) (EVP_PKEY_CTX *ctx);
  73. int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  74. const unsigned char *in, size_t inlen);
  75. int (*derive_init) (EVP_PKEY_CTX *ctx);
  76. int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  77. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  78. int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
  79. int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  80. const unsigned char *tbs, size_t tbslen);
  81. int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  82. size_t siglen, const unsigned char *tbs,
  83. size_t tbslen);
  84. int (*check) (EVP_PKEY *pkey);
  85. int (*public_check) (EVP_PKEY *pkey);
  86. int (*param_check) (EVP_PKEY *pkey);
  87. int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  88. } /* EVP_PKEY_METHOD */ ;
  89. DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
  90. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
  91. extern const EVP_PKEY_METHOD cmac_pkey_meth;
  92. extern const EVP_PKEY_METHOD dh_pkey_meth;
  93. extern const EVP_PKEY_METHOD dhx_pkey_meth;
  94. extern const EVP_PKEY_METHOD dsa_pkey_meth;
  95. extern const EVP_PKEY_METHOD ec_pkey_meth;
  96. extern const EVP_PKEY_METHOD sm2_pkey_meth;
  97. extern const EVP_PKEY_METHOD ecx25519_pkey_meth;
  98. extern const EVP_PKEY_METHOD ecx448_pkey_meth;
  99. extern const EVP_PKEY_METHOD ed25519_pkey_meth;
  100. extern const EVP_PKEY_METHOD ed448_pkey_meth;
  101. extern const EVP_PKEY_METHOD hmac_pkey_meth;
  102. extern const EVP_PKEY_METHOD rsa_pkey_meth;
  103. extern const EVP_PKEY_METHOD rsa_pss_pkey_meth;
  104. extern const EVP_PKEY_METHOD scrypt_pkey_meth;
  105. extern const EVP_PKEY_METHOD tls1_prf_pkey_meth;
  106. extern const EVP_PKEY_METHOD hkdf_pkey_meth;
  107. extern const EVP_PKEY_METHOD poly1305_pkey_meth;
  108. extern const EVP_PKEY_METHOD siphash_pkey_meth;
  109. /* struct evp_mac_impl_st is defined by the implementation */
  110. typedef struct evp_mac_impl_st EVP_MAC_IMPL;
  111. struct evp_mac_st {
  112. OSSL_PROVIDER *prov;
  113. char *name;
  114. CRYPTO_REF_COUNT refcnt;
  115. CRYPTO_RWLOCK *lock;
  116. OSSL_OP_mac_newctx_fn *newctx;
  117. OSSL_OP_mac_dupctx_fn *dupctx;
  118. OSSL_OP_mac_freectx_fn *freectx;
  119. OSSL_OP_mac_size_fn *size;
  120. OSSL_OP_mac_init_fn *init;
  121. OSSL_OP_mac_update_fn *update;
  122. OSSL_OP_mac_final_fn *final;
  123. OSSL_OP_mac_gettable_params_fn *gettable_params;
  124. OSSL_OP_mac_gettable_ctx_params_fn *gettable_ctx_params;
  125. OSSL_OP_mac_settable_ctx_params_fn *settable_ctx_params;
  126. OSSL_OP_mac_get_params_fn *get_params;
  127. OSSL_OP_mac_ctx_get_params_fn *ctx_get_params;
  128. OSSL_OP_mac_ctx_set_params_fn *ctx_set_params;
  129. };
  130. /* Internal keccak algorithms used for KMAC */
  131. const EVP_MD *evp_keccak_kmac128(void);
  132. const EVP_MD *evp_keccak_kmac256(void);
  133. /*
  134. * This function is internal for now, but can be made external when needed.
  135. * The documentation would read:
  136. *
  137. * EVP_add_mac() adds the MAC implementation C<mac> to the internal
  138. * object database.
  139. */
  140. int EVP_add_mac(const EVP_MAC *mac);
  141. int EVP_add_kdf(const EVP_KDF *kdf);
  142. /* struct evp_kdf_impl_st is defined by the implementation */
  143. typedef struct evp_kdf_impl_st EVP_KDF_IMPL;
  144. struct evp_kdf_st {
  145. int type;
  146. EVP_KDF_IMPL *(*new) (void);
  147. void (*free) (EVP_KDF_IMPL *impl);
  148. void (*reset) (EVP_KDF_IMPL *impl);
  149. int (*ctrl) (EVP_KDF_IMPL *impl, int cmd, va_list args);
  150. int (*ctrl_str) (EVP_KDF_IMPL *impl, const char *type, const char *value);
  151. size_t (*size) (EVP_KDF_IMPL *impl);
  152. int (*derive) (EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen);
  153. };
  154. extern const EVP_KDF pbkdf2_kdf_meth;
  155. extern const EVP_KDF scrypt_kdf_meth;
  156. extern const EVP_KDF tls1_prf_kdf_meth;
  157. extern const EVP_KDF hkdf_kdf_meth;
  158. extern const EVP_KDF sshkdf_kdf_meth;
  159. extern const EVP_KDF ss_kdf_meth;
  160. extern const EVP_KDF x963_kdf_meth;
  161. extern const EVP_KDF x942_kdf_meth;
  162. struct evp_md_st {
  163. /* nid */
  164. int type;
  165. /* Legacy structure members */
  166. /* TODO(3.0): Remove these */
  167. int pkey_type;
  168. int md_size;
  169. unsigned long flags;
  170. int (*init) (EVP_MD_CTX *ctx);
  171. int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
  172. int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
  173. int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
  174. int (*cleanup) (EVP_MD_CTX *ctx);
  175. int block_size;
  176. int ctx_size; /* how big does the ctx->md_data need to be */
  177. /* control function */
  178. int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
  179. /* New structure members */
  180. /* TODO(3.0): Remove above comment when legacy has gone */
  181. char *name;
  182. OSSL_PROVIDER *prov;
  183. CRYPTO_REF_COUNT refcnt;
  184. CRYPTO_RWLOCK *lock;
  185. OSSL_OP_digest_newctx_fn *newctx;
  186. OSSL_OP_digest_init_fn *dinit;
  187. OSSL_OP_digest_update_fn *dupdate;
  188. OSSL_OP_digest_final_fn *dfinal;
  189. OSSL_OP_digest_digest_fn *digest;
  190. OSSL_OP_digest_freectx_fn *freectx;
  191. OSSL_OP_digest_dupctx_fn *dupctx;
  192. OSSL_OP_digest_get_params_fn *get_params;
  193. OSSL_OP_digest_ctx_set_params_fn *ctx_set_params;
  194. OSSL_OP_digest_ctx_get_params_fn *ctx_get_params;
  195. OSSL_OP_digest_gettable_params_fn *gettable_params;
  196. OSSL_OP_digest_settable_ctx_params_fn *settable_ctx_params;
  197. OSSL_OP_digest_gettable_ctx_params_fn *gettable_ctx_params;
  198. } /* EVP_MD */ ;
  199. struct evp_cipher_st {
  200. int nid;
  201. int block_size;
  202. /* Default value for variable length ciphers */
  203. int key_len;
  204. int iv_len;
  205. /* Legacy structure members */
  206. /* TODO(3.0): Remove these */
  207. /* Various flags */
  208. unsigned long flags;
  209. /* init key */
  210. int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
  211. const unsigned char *iv, int enc);
  212. /* encrypt/decrypt data */
  213. int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
  214. const unsigned char *in, size_t inl);
  215. /* cleanup ctx */
  216. int (*cleanup) (EVP_CIPHER_CTX *);
  217. /* how big ctx->cipher_data needs to be */
  218. int ctx_size;
  219. /* Populate a ASN1_TYPE with parameters */
  220. int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
  221. /* Get parameters from a ASN1_TYPE */
  222. int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
  223. /* Miscellaneous operations */
  224. int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
  225. /* Application data */
  226. void *app_data;
  227. /* New structure members */
  228. /* TODO(3.0): Remove above comment when legacy has gone */
  229. char *name;
  230. OSSL_PROVIDER *prov;
  231. CRYPTO_REF_COUNT refcnt;
  232. CRYPTO_RWLOCK *lock;
  233. OSSL_OP_cipher_newctx_fn *newctx;
  234. OSSL_OP_cipher_encrypt_init_fn *einit;
  235. OSSL_OP_cipher_decrypt_init_fn *dinit;
  236. OSSL_OP_cipher_update_fn *cupdate;
  237. OSSL_OP_cipher_final_fn *cfinal;
  238. OSSL_OP_cipher_cipher_fn *ccipher;
  239. OSSL_OP_cipher_freectx_fn *freectx;
  240. OSSL_OP_cipher_dupctx_fn *dupctx;
  241. OSSL_OP_cipher_get_params_fn *get_params;
  242. OSSL_OP_cipher_ctx_get_params_fn *ctx_get_params;
  243. OSSL_OP_cipher_ctx_set_params_fn *ctx_set_params;
  244. OSSL_OP_cipher_gettable_params_fn *gettable_params;
  245. OSSL_OP_cipher_gettable_ctx_params_fn *gettable_ctx_params;
  246. OSSL_OP_cipher_settable_ctx_params_fn *settable_ctx_params;
  247. } /* EVP_CIPHER */ ;
  248. /* Macros to code block cipher wrappers */
  249. /* Wrapper functions for each cipher mode */
  250. #define EVP_C_DATA(kstruct, ctx) \
  251. ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  252. #define BLOCK_CIPHER_ecb_loop() \
  253. size_t i, bl; \
  254. bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
  255. if (inl < bl) return 1;\
  256. inl -= bl; \
  257. for (i=0; i <= inl; i+=bl)
  258. #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  259. static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  260. {\
  261. BLOCK_CIPHER_ecb_loop() \
  262. cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
  263. return 1;\
  264. }
  265. #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
  266. #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
  267. static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  268. {\
  269. while(inl>=EVP_MAXCHUNK) {\
  270. int num = EVP_CIPHER_CTX_num(ctx);\
  271. cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
  272. EVP_CIPHER_CTX_set_num(ctx, num);\
  273. inl-=EVP_MAXCHUNK;\
  274. in +=EVP_MAXCHUNK;\
  275. out+=EVP_MAXCHUNK;\
  276. }\
  277. if (inl) {\
  278. int num = EVP_CIPHER_CTX_num(ctx);\
  279. cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
  280. EVP_CIPHER_CTX_set_num(ctx, num);\
  281. }\
  282. return 1;\
  283. }
  284. #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  285. static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  286. {\
  287. while(inl>=EVP_MAXCHUNK) \
  288. {\
  289. cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\
  290. inl-=EVP_MAXCHUNK;\
  291. in +=EVP_MAXCHUNK;\
  292. out+=EVP_MAXCHUNK;\
  293. }\
  294. if (inl)\
  295. cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\
  296. return 1;\
  297. }
  298. #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  299. static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  300. {\
  301. size_t chunk = EVP_MAXCHUNK;\
  302. if (cbits == 1) chunk >>= 3;\
  303. if (inl < chunk) chunk = inl;\
  304. while (inl && inl >= chunk)\
  305. {\
  306. int num = EVP_CIPHER_CTX_num(ctx);\
  307. cprefix##_cfb##cbits##_encrypt(in, out, (long) \
  308. ((cbits == 1) \
  309. && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
  310. ? chunk*8 : chunk), \
  311. &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx),\
  312. &num, EVP_CIPHER_CTX_encrypting(ctx));\
  313. EVP_CIPHER_CTX_set_num(ctx, num);\
  314. inl -= chunk;\
  315. in += chunk;\
  316. out += chunk;\
  317. if (inl < chunk) chunk = inl;\
  318. }\
  319. return 1;\
  320. }
  321. #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  322. BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  323. BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  324. BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  325. BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
  326. #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
  327. key_len, iv_len, flags, init_key, cleanup, \
  328. set_asn1, get_asn1, ctrl) \
  329. static const EVP_CIPHER cname##_##mode = { \
  330. nid##_##nmode, block_size, key_len, iv_len, \
  331. flags | EVP_CIPH_##MODE##_MODE, \
  332. init_key, \
  333. cname##_##mode##_cipher, \
  334. cleanup, \
  335. sizeof(kstruct), \
  336. set_asn1, get_asn1,\
  337. ctrl, \
  338. NULL \
  339. }; \
  340. const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
  341. #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
  342. iv_len, flags, init_key, cleanup, set_asn1, \
  343. get_asn1, ctrl) \
  344. BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
  345. iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  346. #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
  347. iv_len, cbits, flags, init_key, cleanup, \
  348. set_asn1, get_asn1, ctrl) \
  349. BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
  350. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  351. get_asn1, ctrl)
  352. #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
  353. iv_len, cbits, flags, init_key, cleanup, \
  354. set_asn1, get_asn1, ctrl) \
  355. BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
  356. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  357. get_asn1, ctrl)
  358. #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
  359. flags, init_key, cleanup, set_asn1, \
  360. get_asn1, ctrl) \
  361. BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
  362. 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  363. #define BLOCK_CIPHER_defs(cname, kstruct, \
  364. nid, block_size, key_len, iv_len, cbits, flags, \
  365. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  366. BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
  367. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  368. BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
  369. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  370. BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
  371. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  372. BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
  373. init_key, cleanup, set_asn1, get_asn1, ctrl)
  374. /*-
  375. #define BLOCK_CIPHER_defs(cname, kstruct, \
  376. nid, block_size, key_len, iv_len, flags,\
  377. init_key, cleanup, set_asn1, get_asn1, ctrl)\
  378. static const EVP_CIPHER cname##_cbc = {\
  379. nid##_cbc, block_size, key_len, iv_len, \
  380. flags | EVP_CIPH_CBC_MODE,\
  381. init_key,\
  382. cname##_cbc_cipher,\
  383. cleanup,\
  384. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  385. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  386. set_asn1, get_asn1,\
  387. ctrl, \
  388. NULL \
  389. };\
  390. const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
  391. static const EVP_CIPHER cname##_cfb = {\
  392. nid##_cfb64, 1, key_len, iv_len, \
  393. flags | EVP_CIPH_CFB_MODE,\
  394. init_key,\
  395. cname##_cfb_cipher,\
  396. cleanup,\
  397. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  398. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  399. set_asn1, get_asn1,\
  400. ctrl,\
  401. NULL \
  402. };\
  403. const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
  404. static const EVP_CIPHER cname##_ofb = {\
  405. nid##_ofb64, 1, key_len, iv_len, \
  406. flags | EVP_CIPH_OFB_MODE,\
  407. init_key,\
  408. cname##_ofb_cipher,\
  409. cleanup,\
  410. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  411. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  412. set_asn1, get_asn1,\
  413. ctrl,\
  414. NULL \
  415. };\
  416. const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
  417. static const EVP_CIPHER cname##_ecb = {\
  418. nid##_ecb, block_size, key_len, iv_len, \
  419. flags | EVP_CIPH_ECB_MODE,\
  420. init_key,\
  421. cname##_ecb_cipher,\
  422. cleanup,\
  423. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  424. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  425. set_asn1, get_asn1,\
  426. ctrl,\
  427. NULL \
  428. };\
  429. const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
  430. */
  431. #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
  432. block_size, key_len, iv_len, cbits, \
  433. flags, init_key, \
  434. cleanup, set_asn1, get_asn1, ctrl) \
  435. BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  436. BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
  437. cbits, flags, init_key, cleanup, set_asn1, \
  438. get_asn1, ctrl)
  439. #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
  440. BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
  441. BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
  442. NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
  443. (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
  444. cipher##_init_key, NULL, NULL, NULL, NULL)
  445. # ifndef OPENSSL_NO_EC
  446. #define X25519_KEYLEN 32
  447. #define X448_KEYLEN 56
  448. #define ED448_KEYLEN 57
  449. #define MAX_KEYLEN ED448_KEYLEN
  450. typedef struct {
  451. unsigned char pubkey[MAX_KEYLEN];
  452. unsigned char *privkey;
  453. } ECX_KEY;
  454. #endif
  455. /*
  456. * Type needs to be a bit field Sub-type needs to be for variations on the
  457. * method, as in, can it do arbitrary encryption....
  458. */
  459. struct evp_pkey_st {
  460. /* == Legacy attributes == */
  461. int type;
  462. int save_type;
  463. const EVP_PKEY_ASN1_METHOD *ameth;
  464. ENGINE *engine;
  465. ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
  466. union {
  467. void *ptr;
  468. # ifndef OPENSSL_NO_RSA
  469. struct rsa_st *rsa; /* RSA */
  470. # endif
  471. # ifndef OPENSSL_NO_DSA
  472. struct dsa_st *dsa; /* DSA */
  473. # endif
  474. # ifndef OPENSSL_NO_DH
  475. struct dh_st *dh; /* DH */
  476. # endif
  477. # ifndef OPENSSL_NO_EC
  478. struct ec_key_st *ec; /* ECC */
  479. ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
  480. # endif
  481. } pkey;
  482. /* == Common attributes == */
  483. CRYPTO_REF_COUNT references;
  484. CRYPTO_RWLOCK *lock;
  485. STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
  486. int save_parameters;
  487. /* == Provider attributes == */
  488. /*
  489. * To support transparent export/import between providers that
  490. * support the methods for it, and still not having to do the
  491. * export/import every time a key is used, we maintain a cache
  492. * of imported key, indexed by provider address.
  493. * pkeys[0] is *always* the "original" key.
  494. */
  495. struct {
  496. EVP_KEYMGMT *keymgmt;
  497. void *provkey;
  498. } pkeys[10];
  499. /*
  500. * If there is a legacy key assigned to this structure, we keep
  501. * a copy of that key's dirty count.
  502. */
  503. size_t dirty_cnt_copy;
  504. } /* EVP_PKEY */ ;
  505. void openssl_add_all_ciphers_int(void);
  506. void openssl_add_all_digests_int(void);
  507. void openssl_add_all_macs_int(void);
  508. void openssl_add_all_kdfs_int(void);
  509. void evp_cleanup_int(void);
  510. void evp_app_cleanup_int(void);
  511. /* KEYMGMT helper functions */
  512. void *evp_keymgmt_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
  513. void evp_keymgmt_clear_pkey_cache(EVP_PKEY *pk);
  514. /* KEYMGMT provider interface functions */
  515. void *evp_keymgmt_importdomparams(const EVP_KEYMGMT *keymgmt,
  516. const OSSL_PARAM params[]);
  517. void *evp_keymgmt_gendomparams(const EVP_KEYMGMT *keymgmt,
  518. const OSSL_PARAM params[]);
  519. void evp_keymgmt_freedomparams(const EVP_KEYMGMT *keymgmt,
  520. void *provdomparams);
  521. int evp_keymgmt_exportdomparams(const EVP_KEYMGMT *keymgmt,
  522. void *provdomparams, OSSL_PARAM params[]);
  523. const OSSL_PARAM *
  524. evp_keymgmt_importdomparam_types(const EVP_KEYMGMT *keymgmt);
  525. const OSSL_PARAM *
  526. evp_keymgmt_exportdomparam_types(const EVP_KEYMGMT *keymgmt);
  527. void *evp_keymgmt_importkey(const EVP_KEYMGMT *keymgmt,
  528. const OSSL_PARAM params[]);
  529. void *evp_keymgmt_genkey(const EVP_KEYMGMT *keymgmt, void *domparams,
  530. const OSSL_PARAM params[]);
  531. void *evp_keymgmt_loadkey(const EVP_KEYMGMT *keymgmt,
  532. void *id, size_t idlen);
  533. void evp_keymgmt_freekey(const EVP_KEYMGMT *keymgmt, void *provkey);
  534. int evp_keymgmt_exportkey(const EVP_KEYMGMT *keymgmt,
  535. void *provkey, OSSL_PARAM params[]);
  536. const OSSL_PARAM *evp_keymgmt_importkey_types(const EVP_KEYMGMT *keymgmt);
  537. const OSSL_PARAM *evp_keymgmt_exportkey_types(const EVP_KEYMGMT *keymgmt);
  538. /* Pulling defines out of C source files */
  539. #define EVP_RC4_KEY_SIZE 16
  540. #ifndef TLS1_1_VERSION
  541. # define TLS1_1_VERSION 0x0302
  542. #endif
  543. void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
  544. /* EVP_ENCODE_CTX flags */
  545. /* Don't generate new lines when encoding */
  546. #define EVP_ENCODE_CTX_NO_NEWLINES 1
  547. /* Use the SRP base64 alphabet instead of the standard one */
  548. #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2