2
0

evp_int.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. /* Method associated with this operation */
  19. const EVP_PKEY_METHOD *pmeth;
  20. /* Engine that implements this method or NULL if builtin */
  21. ENGINE *engine;
  22. /* Key: may be NULL */
  23. EVP_PKEY *pkey;
  24. /* Peer key for key agreement, may be NULL */
  25. EVP_PKEY *peerkey;
  26. /* Actual operation */
  27. int operation;
  28. /* Algorithm specific data */
  29. void *data;
  30. /* Application specific data */
  31. void *app_data;
  32. /* Keygen callback */
  33. EVP_PKEY_gen_cb *pkey_gencb;
  34. /* implementation specific keygen data */
  35. int *keygen_info;
  36. int keygen_info_count;
  37. } /* EVP_PKEY_CTX */ ;
  38. #define EVP_PKEY_FLAG_DYNAMIC 1
  39. struct evp_pkey_method_st {
  40. int pkey_id;
  41. int flags;
  42. int (*init) (EVP_PKEY_CTX *ctx);
  43. int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
  44. void (*cleanup) (EVP_PKEY_CTX *ctx);
  45. int (*paramgen_init) (EVP_PKEY_CTX *ctx);
  46. int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  47. int (*keygen_init) (EVP_PKEY_CTX *ctx);
  48. int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  49. int (*sign_init) (EVP_PKEY_CTX *ctx);
  50. int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  51. const unsigned char *tbs, size_t tbslen);
  52. int (*verify_init) (EVP_PKEY_CTX *ctx);
  53. int (*verify) (EVP_PKEY_CTX *ctx,
  54. const unsigned char *sig, size_t siglen,
  55. const unsigned char *tbs, size_t tbslen);
  56. int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
  57. int (*verify_recover) (EVP_PKEY_CTX *ctx,
  58. unsigned char *rout, size_t *routlen,
  59. const unsigned char *sig, size_t siglen);
  60. int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  61. int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  62. EVP_MD_CTX *mctx);
  63. int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  64. int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
  65. EVP_MD_CTX *mctx);
  66. int (*encrypt_init) (EVP_PKEY_CTX *ctx);
  67. int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  68. const unsigned char *in, size_t inlen);
  69. int (*decrypt_init) (EVP_PKEY_CTX *ctx);
  70. int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  71. const unsigned char *in, size_t inlen);
  72. int (*derive_init) (EVP_PKEY_CTX *ctx);
  73. int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  74. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  75. int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
  76. int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  77. const unsigned char *tbs, size_t tbslen);
  78. int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  79. size_t siglen, const unsigned char *tbs,
  80. size_t tbslen);
  81. int (*check) (EVP_PKEY *pkey);
  82. int (*public_check) (EVP_PKEY *pkey);
  83. int (*param_check) (EVP_PKEY *pkey);
  84. int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  85. } /* EVP_PKEY_METHOD */ ;
  86. DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
  87. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
  88. extern const EVP_PKEY_METHOD cmac_pkey_meth;
  89. extern const EVP_PKEY_METHOD dh_pkey_meth;
  90. extern const EVP_PKEY_METHOD dhx_pkey_meth;
  91. extern const EVP_PKEY_METHOD dsa_pkey_meth;
  92. extern const EVP_PKEY_METHOD ec_pkey_meth;
  93. extern const EVP_PKEY_METHOD sm2_pkey_meth;
  94. extern const EVP_PKEY_METHOD ecx25519_pkey_meth;
  95. extern const EVP_PKEY_METHOD ecx448_pkey_meth;
  96. extern const EVP_PKEY_METHOD ed25519_pkey_meth;
  97. extern const EVP_PKEY_METHOD ed448_pkey_meth;
  98. extern const EVP_PKEY_METHOD hmac_pkey_meth;
  99. extern const EVP_PKEY_METHOD rsa_pkey_meth;
  100. extern const EVP_PKEY_METHOD rsa_pss_pkey_meth;
  101. extern const EVP_PKEY_METHOD scrypt_pkey_meth;
  102. extern const EVP_PKEY_METHOD tls1_prf_pkey_meth;
  103. extern const EVP_PKEY_METHOD hkdf_pkey_meth;
  104. extern const EVP_PKEY_METHOD poly1305_pkey_meth;
  105. extern const EVP_PKEY_METHOD siphash_pkey_meth;
  106. /* struct evp_mac_impl_st is defined by the implementation */
  107. typedef struct evp_mac_impl_st EVP_MAC_IMPL;
  108. struct evp_mac_st {
  109. int type;
  110. EVP_MAC_IMPL *(*new) (void);
  111. int (*copy) (EVP_MAC_IMPL *macdst, EVP_MAC_IMPL *macsrc);
  112. void (*free) (EVP_MAC_IMPL *macctx);
  113. size_t (*size) (EVP_MAC_IMPL *macctx);
  114. int (*init) (EVP_MAC_IMPL *macctx);
  115. int (*update) (EVP_MAC_IMPL *macctx, const unsigned char *data,
  116. size_t datalen);
  117. int (*final) (EVP_MAC_IMPL *macctx, unsigned char *out);
  118. int (*ctrl) (EVP_MAC_IMPL *macctx, int cmd, va_list args);
  119. int (*ctrl_str) (EVP_MAC_IMPL *macctx, const char *type, const char *value);
  120. };
  121. extern const EVP_MAC blake2b_mac_meth;
  122. extern const EVP_MAC blake2s_mac_meth;
  123. extern const EVP_MAC cmac_meth;
  124. extern const EVP_MAC gmac_meth;
  125. extern const EVP_MAC hmac_meth;
  126. extern const EVP_MAC kmac128_meth;
  127. extern const EVP_MAC kmac256_meth;
  128. extern const EVP_MAC siphash_meth;
  129. extern const EVP_MAC poly1305_meth;
  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. /* struct evp_kdf_impl_st is defined by the implementation */
  142. typedef struct evp_kdf_impl_st EVP_KDF_IMPL;
  143. typedef struct {
  144. int type;
  145. EVP_KDF_IMPL *(*new) (void);
  146. void (*free) (EVP_KDF_IMPL *impl);
  147. void (*reset) (EVP_KDF_IMPL *impl);
  148. int (*ctrl) (EVP_KDF_IMPL *impl, int cmd, va_list args);
  149. int (*ctrl_str) (EVP_KDF_IMPL *impl, const char *type, const char *value);
  150. size_t (*size) (EVP_KDF_IMPL *impl);
  151. int (*derive) (EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen);
  152. } EVP_KDF_METHOD;
  153. extern const EVP_KDF_METHOD pbkdf2_kdf_meth;
  154. extern const EVP_KDF_METHOD scrypt_kdf_meth;
  155. extern const EVP_KDF_METHOD tls1_prf_kdf_meth;
  156. extern const EVP_KDF_METHOD hkdf_kdf_meth;
  157. extern const EVP_KDF_METHOD sshkdf_kdf_meth;
  158. extern const EVP_KDF_METHOD ss_kdf_meth;
  159. struct evp_md_st {
  160. /* nid */
  161. int type;
  162. /* Legacy structure members */
  163. /* TODO(3.0): Remove these */
  164. int pkey_type;
  165. int md_size;
  166. unsigned long flags;
  167. int (*init) (EVP_MD_CTX *ctx);
  168. int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
  169. int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
  170. int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
  171. int (*cleanup) (EVP_MD_CTX *ctx);
  172. int block_size;
  173. int ctx_size; /* how big does the ctx->md_data need to be */
  174. /* control function */
  175. int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
  176. /* New structure members */
  177. /* TODO(3.0): Remove above comment when legacy has gone */
  178. OSSL_PROVIDER *prov;
  179. CRYPTO_REF_COUNT refcnt;
  180. CRYPTO_RWLOCK *lock;
  181. OSSL_OP_digest_newctx_fn *newctx;
  182. OSSL_OP_digest_init_fn *dinit;
  183. OSSL_OP_digest_update_fn *dupdate;
  184. OSSL_OP_digest_final_fn *dfinal;
  185. OSSL_OP_digest_digest_fn *digest;
  186. OSSL_OP_digest_freectx_fn *freectx;
  187. OSSL_OP_digest_dupctx_fn *dupctx;
  188. OSSL_OP_digest_size_fn *size;
  189. OSSL_OP_digest_block_size_fn *dblock_size;
  190. } /* EVP_MD */ ;
  191. struct evp_cipher_st {
  192. int nid;
  193. int block_size;
  194. /* Default value for variable length ciphers */
  195. int key_len;
  196. int iv_len;
  197. /* Legacy structure members */
  198. /* TODO(3.0): Remove these */
  199. /* Various flags */
  200. unsigned long flags;
  201. /* init key */
  202. int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
  203. const unsigned char *iv, int enc);
  204. /* encrypt/decrypt data */
  205. int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
  206. const unsigned char *in, size_t inl);
  207. /* cleanup ctx */
  208. int (*cleanup) (EVP_CIPHER_CTX *);
  209. /* how big ctx->cipher_data needs to be */
  210. int ctx_size;
  211. /* Populate a ASN1_TYPE with parameters */
  212. int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
  213. /* Get parameters from a ASN1_TYPE */
  214. int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
  215. /* Miscellaneous operations */
  216. int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
  217. /* Application data */
  218. void *app_data;
  219. /* New structure members */
  220. /* TODO(3.0): Remove above comment when legacy has gone */
  221. OSSL_PROVIDER *prov;
  222. CRYPTO_REF_COUNT refcnt;
  223. CRYPTO_RWLOCK *lock;
  224. OSSL_OP_cipher_newctx_fn *newctx;
  225. OSSL_OP_cipher_encrypt_init_fn *einit;
  226. OSSL_OP_cipher_decrypt_init_fn *dinit;
  227. OSSL_OP_cipher_update_fn *cupdate;
  228. OSSL_OP_cipher_final_fn *cfinal;
  229. OSSL_OP_cipher_cipher_fn *ccipher;
  230. OSSL_OP_cipher_freectx_fn *freectx;
  231. OSSL_OP_cipher_dupctx_fn *dupctx;
  232. OSSL_OP_cipher_key_length_fn *key_length;
  233. OSSL_OP_cipher_iv_length_fn *iv_length;
  234. OSSL_OP_cipher_block_size_fn *blocksize;
  235. OSSL_OP_cipher_get_params_fn *get_params;
  236. OSSL_OP_cipher_ctx_get_params_fn *ctx_get_params;
  237. OSSL_OP_cipher_ctx_set_params_fn *ctx_set_params;
  238. } /* EVP_CIPHER */ ;
  239. /* Macros to code block cipher wrappers */
  240. /* Wrapper functions for each cipher mode */
  241. #define EVP_C_DATA(kstruct, ctx) \
  242. ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  243. #define BLOCK_CIPHER_ecb_loop() \
  244. size_t i, bl; \
  245. bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
  246. if (inl < bl) return 1;\
  247. inl -= bl; \
  248. for (i=0; i <= inl; i+=bl)
  249. #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  250. static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  251. {\
  252. BLOCK_CIPHER_ecb_loop() \
  253. cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
  254. return 1;\
  255. }
  256. #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
  257. #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
  258. static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  259. {\
  260. while(inl>=EVP_MAXCHUNK) {\
  261. int num = EVP_CIPHER_CTX_num(ctx);\
  262. cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
  263. EVP_CIPHER_CTX_set_num(ctx, num);\
  264. inl-=EVP_MAXCHUNK;\
  265. in +=EVP_MAXCHUNK;\
  266. out+=EVP_MAXCHUNK;\
  267. }\
  268. if (inl) {\
  269. int num = EVP_CIPHER_CTX_num(ctx);\
  270. cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), &num); \
  271. EVP_CIPHER_CTX_set_num(ctx, num);\
  272. }\
  273. return 1;\
  274. }
  275. #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  276. static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  277. {\
  278. while(inl>=EVP_MAXCHUNK) \
  279. {\
  280. 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));\
  281. inl-=EVP_MAXCHUNK;\
  282. in +=EVP_MAXCHUNK;\
  283. out+=EVP_MAXCHUNK;\
  284. }\
  285. if (inl)\
  286. cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_encrypting(ctx));\
  287. return 1;\
  288. }
  289. #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  290. static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  291. {\
  292. size_t chunk = EVP_MAXCHUNK;\
  293. if (cbits == 1) chunk >>= 3;\
  294. if (inl < chunk) chunk = inl;\
  295. while (inl && inl >= chunk)\
  296. {\
  297. int num = EVP_CIPHER_CTX_num(ctx);\
  298. cprefix##_cfb##cbits##_encrypt(in, out, (long) \
  299. ((cbits == 1) \
  300. && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
  301. ? chunk*8 : chunk), \
  302. &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_iv_noconst(ctx),\
  303. &num, EVP_CIPHER_CTX_encrypting(ctx));\
  304. EVP_CIPHER_CTX_set_num(ctx, num);\
  305. inl -= chunk;\
  306. in += chunk;\
  307. out += chunk;\
  308. if (inl < chunk) chunk = inl;\
  309. }\
  310. return 1;\
  311. }
  312. #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  313. BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  314. BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  315. BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  316. BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
  317. #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
  318. key_len, iv_len, flags, init_key, cleanup, \
  319. set_asn1, get_asn1, ctrl) \
  320. static const EVP_CIPHER cname##_##mode = { \
  321. nid##_##nmode, block_size, key_len, iv_len, \
  322. flags | EVP_CIPH_##MODE##_MODE, \
  323. init_key, \
  324. cname##_##mode##_cipher, \
  325. cleanup, \
  326. sizeof(kstruct), \
  327. set_asn1, get_asn1,\
  328. ctrl, \
  329. NULL \
  330. }; \
  331. const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
  332. #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
  333. iv_len, flags, init_key, cleanup, set_asn1, \
  334. get_asn1, ctrl) \
  335. BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
  336. iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  337. #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
  338. iv_len, cbits, flags, init_key, cleanup, \
  339. set_asn1, get_asn1, ctrl) \
  340. BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
  341. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  342. get_asn1, ctrl)
  343. #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
  344. iv_len, cbits, flags, init_key, cleanup, \
  345. set_asn1, get_asn1, ctrl) \
  346. BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
  347. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  348. get_asn1, ctrl)
  349. #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
  350. flags, init_key, cleanup, set_asn1, \
  351. get_asn1, ctrl) \
  352. BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
  353. 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  354. #define BLOCK_CIPHER_defs(cname, kstruct, \
  355. nid, block_size, key_len, iv_len, cbits, flags, \
  356. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  357. BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
  358. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  359. BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
  360. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  361. BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
  362. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  363. BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
  364. init_key, cleanup, set_asn1, get_asn1, ctrl)
  365. /*-
  366. #define BLOCK_CIPHER_defs(cname, kstruct, \
  367. nid, block_size, key_len, iv_len, flags,\
  368. init_key, cleanup, set_asn1, get_asn1, ctrl)\
  369. static const EVP_CIPHER cname##_cbc = {\
  370. nid##_cbc, block_size, key_len, iv_len, \
  371. flags | EVP_CIPH_CBC_MODE,\
  372. init_key,\
  373. cname##_cbc_cipher,\
  374. cleanup,\
  375. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  376. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  377. set_asn1, get_asn1,\
  378. ctrl, \
  379. NULL \
  380. };\
  381. const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
  382. static const EVP_CIPHER cname##_cfb = {\
  383. nid##_cfb64, 1, key_len, iv_len, \
  384. flags | EVP_CIPH_CFB_MODE,\
  385. init_key,\
  386. cname##_cfb_cipher,\
  387. cleanup,\
  388. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  389. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  390. set_asn1, get_asn1,\
  391. ctrl,\
  392. NULL \
  393. };\
  394. const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
  395. static const EVP_CIPHER cname##_ofb = {\
  396. nid##_ofb64, 1, key_len, iv_len, \
  397. flags | EVP_CIPH_OFB_MODE,\
  398. init_key,\
  399. cname##_ofb_cipher,\
  400. cleanup,\
  401. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  402. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  403. set_asn1, get_asn1,\
  404. ctrl,\
  405. NULL \
  406. };\
  407. const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
  408. static const EVP_CIPHER cname##_ecb = {\
  409. nid##_ecb, block_size, key_len, iv_len, \
  410. flags | EVP_CIPH_ECB_MODE,\
  411. init_key,\
  412. cname##_ecb_cipher,\
  413. cleanup,\
  414. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  415. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  416. set_asn1, get_asn1,\
  417. ctrl,\
  418. NULL \
  419. };\
  420. const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
  421. */
  422. #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
  423. block_size, key_len, iv_len, cbits, \
  424. flags, init_key, \
  425. cleanup, set_asn1, get_asn1, ctrl) \
  426. BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  427. BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
  428. cbits, flags, init_key, cleanup, set_asn1, \
  429. get_asn1, ctrl)
  430. #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
  431. BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
  432. BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
  433. NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
  434. (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
  435. cipher##_init_key, NULL, NULL, NULL, NULL)
  436. # ifndef OPENSSL_NO_EC
  437. #define X25519_KEYLEN 32
  438. #define X448_KEYLEN 56
  439. #define ED448_KEYLEN 57
  440. #define MAX_KEYLEN ED448_KEYLEN
  441. typedef struct {
  442. unsigned char pubkey[MAX_KEYLEN];
  443. unsigned char *privkey;
  444. } ECX_KEY;
  445. #endif
  446. /*
  447. * Type needs to be a bit field Sub-type needs to be for variations on the
  448. * method, as in, can it do arbitrary encryption....
  449. */
  450. struct evp_pkey_st {
  451. int type;
  452. int save_type;
  453. CRYPTO_REF_COUNT references;
  454. const EVP_PKEY_ASN1_METHOD *ameth;
  455. ENGINE *engine;
  456. ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
  457. union {
  458. void *ptr;
  459. # ifndef OPENSSL_NO_RSA
  460. struct rsa_st *rsa; /* RSA */
  461. # endif
  462. # ifndef OPENSSL_NO_DSA
  463. struct dsa_st *dsa; /* DSA */
  464. # endif
  465. # ifndef OPENSSL_NO_DH
  466. struct dh_st *dh; /* DH */
  467. # endif
  468. # ifndef OPENSSL_NO_EC
  469. struct ec_key_st *ec; /* ECC */
  470. ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
  471. # endif
  472. } pkey;
  473. int save_parameters;
  474. STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
  475. CRYPTO_RWLOCK *lock;
  476. } /* EVP_PKEY */ ;
  477. void openssl_add_all_ciphers_int(void);
  478. void openssl_add_all_digests_int(void);
  479. void openssl_add_all_macs_int(void);
  480. void evp_cleanup_int(void);
  481. void evp_app_cleanup_int(void);
  482. /* Pulling defines out of C source files */
  483. #define EVP_RC4_KEY_SIZE 16
  484. #ifndef TLS1_1_VERSION
  485. # define TLS1_1_VERSION 0x0302
  486. #endif
  487. void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
  488. /* EVP_ENCODE_CTX flags */
  489. /* Don't generate new lines when encoding */
  490. #define EVP_ENCODE_CTX_NO_NEWLINES 1
  491. /* Use the SRP base64 alphabet instead of the standard one */
  492. #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2