evp_locl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* evp_locl.h */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* Macros to code block cipher wrappers */
  60. /* Wrapper functions for each cipher mode */
  61. #define BLOCK_CIPHER_ecb_loop() \
  62. size_t i, bl; \
  63. bl = ctx->cipher->block_size;\
  64. if(inl < bl) return 1;\
  65. inl -= bl; \
  66. for(i=0; i <= inl; i+=bl)
  67. #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  68. static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  69. {\
  70. BLOCK_CIPHER_ecb_loop() \
  71. cprefix##_ecb_encrypt(in + i, out + i, &((kstruct *)ctx->cipher_data)->ksched, ctx->encrypt);\
  72. return 1;\
  73. }
  74. #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
  75. #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
  76. static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  77. {\
  78. while(inl>=EVP_MAXCHUNK)\
  79. {\
  80. cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\
  81. inl-=EVP_MAXCHUNK;\
  82. in +=EVP_MAXCHUNK;\
  83. out+=EVP_MAXCHUNK;\
  84. }\
  85. if (inl)\
  86. cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\
  87. return 1;\
  88. }
  89. #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  90. static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  91. {\
  92. while(inl>=EVP_MAXCHUNK) \
  93. {\
  94. cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\
  95. inl-=EVP_MAXCHUNK;\
  96. in +=EVP_MAXCHUNK;\
  97. out+=EVP_MAXCHUNK;\
  98. }\
  99. if (inl)\
  100. cprefix##_cbc_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\
  101. return 1;\
  102. }
  103. #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  104. static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
  105. {\
  106. size_t chunk=EVP_MAXCHUNK;\
  107. if (cbits==1) chunk>>=3;\
  108. if (inl<chunk) chunk=inl;\
  109. while(inl && inl>=chunk)\
  110. {\
  111. cprefix##_cfb##cbits##_encrypt(in, out, (long)((cbits==1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ?inl*8:inl), &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num, ctx->encrypt);\
  112. inl-=chunk;\
  113. in +=chunk;\
  114. out+=chunk;\
  115. if(inl<chunk) chunk=inl;\
  116. }\
  117. return 1;\
  118. }
  119. #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  120. BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  121. BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  122. BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  123. BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
  124. #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
  125. key_len, iv_len, flags, init_key, cleanup, \
  126. set_asn1, get_asn1, ctrl) \
  127. static const EVP_CIPHER cname##_##mode = { \
  128. nid##_##nmode, block_size, key_len, iv_len, \
  129. flags | EVP_CIPH_##MODE##_MODE, \
  130. init_key, \
  131. cname##_##mode##_cipher, \
  132. cleanup, \
  133. sizeof(kstruct), \
  134. set_asn1, get_asn1,\
  135. ctrl, \
  136. NULL \
  137. }; \
  138. const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
  139. #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
  140. iv_len, flags, init_key, cleanup, set_asn1, \
  141. get_asn1, ctrl) \
  142. BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
  143. iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  144. #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
  145. iv_len, cbits, flags, init_key, cleanup, \
  146. set_asn1, get_asn1, ctrl) \
  147. BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
  148. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  149. get_asn1, ctrl)
  150. #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
  151. iv_len, cbits, flags, init_key, cleanup, \
  152. set_asn1, get_asn1, ctrl) \
  153. BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
  154. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  155. get_asn1, ctrl)
  156. #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
  157. flags, init_key, cleanup, set_asn1, \
  158. get_asn1, ctrl) \
  159. BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
  160. 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  161. #define BLOCK_CIPHER_defs(cname, kstruct, \
  162. nid, block_size, key_len, iv_len, cbits, flags, \
  163. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  164. BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
  165. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  166. BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
  167. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  168. BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
  169. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  170. BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
  171. init_key, cleanup, set_asn1, get_asn1, ctrl)
  172. /*-
  173. #define BLOCK_CIPHER_defs(cname, kstruct, \
  174. nid, block_size, key_len, iv_len, flags,\
  175. init_key, cleanup, set_asn1, get_asn1, ctrl)\
  176. static const EVP_CIPHER cname##_cbc = {\
  177. nid##_cbc, block_size, key_len, iv_len, \
  178. flags | EVP_CIPH_CBC_MODE,\
  179. init_key,\
  180. cname##_cbc_cipher,\
  181. cleanup,\
  182. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  183. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  184. set_asn1, get_asn1,\
  185. ctrl, \
  186. NULL \
  187. };\
  188. const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
  189. static const EVP_CIPHER cname##_cfb = {\
  190. nid##_cfb64, 1, key_len, iv_len, \
  191. flags | EVP_CIPH_CFB_MODE,\
  192. init_key,\
  193. cname##_cfb_cipher,\
  194. cleanup,\
  195. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  196. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  197. set_asn1, get_asn1,\
  198. ctrl,\
  199. NULL \
  200. };\
  201. const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
  202. static const EVP_CIPHER cname##_ofb = {\
  203. nid##_ofb64, 1, key_len, iv_len, \
  204. flags | EVP_CIPH_OFB_MODE,\
  205. init_key,\
  206. cname##_ofb_cipher,\
  207. cleanup,\
  208. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  209. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  210. set_asn1, get_asn1,\
  211. ctrl,\
  212. NULL \
  213. };\
  214. const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
  215. static const EVP_CIPHER cname##_ecb = {\
  216. nid##_ecb, block_size, key_len, iv_len, \
  217. flags | EVP_CIPH_ECB_MODE,\
  218. init_key,\
  219. cname##_ecb_cipher,\
  220. cleanup,\
  221. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  222. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  223. set_asn1, get_asn1,\
  224. ctrl,\
  225. NULL \
  226. };\
  227. const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
  228. */
  229. #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
  230. block_size, key_len, iv_len, cbits, \
  231. flags, init_key, \
  232. cleanup, set_asn1, get_asn1, ctrl) \
  233. BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  234. BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
  235. cbits, flags, init_key, cleanup, set_asn1, \
  236. get_asn1, ctrl)
  237. #define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data)
  238. #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len) \
  239. BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
  240. BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
  241. NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
  242. 0, cipher##_init_key, NULL, \
  243. EVP_CIPHER_set_asn1_iv, \
  244. EVP_CIPHER_get_asn1_iv, \
  245. NULL)
  246. struct evp_pkey_ctx_st {
  247. /* Method associated with this operation */
  248. const EVP_PKEY_METHOD *pmeth;
  249. /* Engine that implements this method or NULL if builtin */
  250. ENGINE *engine;
  251. /* Key: may be NULL */
  252. EVP_PKEY *pkey;
  253. /* Peer key for key agreement, may be NULL */
  254. EVP_PKEY *peerkey;
  255. /* Actual operation */
  256. int operation;
  257. /* Algorithm specific data */
  258. void *data;
  259. /* Application specific data */
  260. void *app_data;
  261. /* Keygen callback */
  262. EVP_PKEY_gen_cb *pkey_gencb;
  263. /* implementation specific keygen data */
  264. int *keygen_info;
  265. int keygen_info_count;
  266. } /* EVP_PKEY_CTX */ ;
  267. #define EVP_PKEY_FLAG_DYNAMIC 1
  268. struct evp_pkey_method_st {
  269. int pkey_id;
  270. int flags;
  271. int (*init) (EVP_PKEY_CTX *ctx);
  272. int (*copy) (EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
  273. void (*cleanup) (EVP_PKEY_CTX *ctx);
  274. int (*paramgen_init) (EVP_PKEY_CTX *ctx);
  275. int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  276. int (*keygen_init) (EVP_PKEY_CTX *ctx);
  277. int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  278. int (*sign_init) (EVP_PKEY_CTX *ctx);
  279. int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  280. const unsigned char *tbs, size_t tbslen);
  281. int (*verify_init) (EVP_PKEY_CTX *ctx);
  282. int (*verify) (EVP_PKEY_CTX *ctx,
  283. const unsigned char *sig, size_t siglen,
  284. const unsigned char *tbs, size_t tbslen);
  285. int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
  286. int (*verify_recover) (EVP_PKEY_CTX *ctx,
  287. unsigned char *rout, size_t *routlen,
  288. const unsigned char *sig, size_t siglen);
  289. int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  290. int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  291. EVP_MD_CTX *mctx);
  292. int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  293. int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
  294. EVP_MD_CTX *mctx);
  295. int (*encrypt_init) (EVP_PKEY_CTX *ctx);
  296. int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  297. const unsigned char *in, size_t inlen);
  298. int (*decrypt_init) (EVP_PKEY_CTX *ctx);
  299. int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  300. const unsigned char *in, size_t inlen);
  301. int (*derive_init) (EVP_PKEY_CTX *ctx);
  302. int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  303. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  304. int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
  305. } /* EVP_PKEY_METHOD */ ;
  306. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
  307. int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  308. int passlen, ASN1_TYPE *param,
  309. const EVP_CIPHER *c, const EVP_MD *md,
  310. int en_de);
  311. const EVP_MD *evp_get_fips_md(const EVP_MD *md);
  312. const EVP_CIPHER *evp_get_fips_cipher(const EVP_CIPHER *cipher);
  313. #ifdef OPENSSL_FIPS
  314. # ifdef OPENSSL_DOING_MAKEDEPEND
  315. # undef SHA1_Init
  316. # undef SHA1_Update
  317. # undef SHA224_Init
  318. # undef SHA256_Init
  319. # undef SHA384_Init
  320. # undef SHA512_Init
  321. # undef DES_set_key_unchecked
  322. # endif
  323. # define RIPEMD160_Init private_RIPEMD160_Init
  324. # define WHIRLPOOL_Init private_WHIRLPOOL_Init
  325. # define MD5_Init private_MD5_Init
  326. # define MD4_Init private_MD4_Init
  327. # define MD2_Init private_MD2_Init
  328. # define MDC2_Init private_MDC2_Init
  329. # define SHA_Init private_SHA_Init
  330. # define SHA1_Init private_SHA1_Init
  331. # define SHA224_Init private_SHA224_Init
  332. # define SHA256_Init private_SHA256_Init
  333. # define SHA384_Init private_SHA384_Init
  334. # define SHA512_Init private_SHA512_Init
  335. # define BF_set_key private_BF_set_key
  336. # define CAST_set_key private_CAST_set_key
  337. # define idea_set_encrypt_key private_idea_set_encrypt_key
  338. # define SEED_set_key private_SEED_set_key
  339. # define RC2_set_key private_RC2_set_key
  340. # define RC4_set_key private_RC4_set_key
  341. # define DES_set_key_unchecked private_DES_set_key_unchecked
  342. # define Camellia_set_key private_Camellia_set_key
  343. #endif