2
0

evp_locl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. unsigned int 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, unsigned int 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 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
  75. static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
  76. {\
  77. cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\
  78. return 1;\
  79. }
  80. #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  81. static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
  82. {\
  83. cprefix##_cbc_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\
  84. return 1;\
  85. }
  86. #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  87. static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \
  88. {\
  89. 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);\
  90. return 1;\
  91. }
  92. #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  93. BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
  94. BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
  95. BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
  96. BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
  97. #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
  98. key_len, iv_len, flags, init_key, cleanup, \
  99. set_asn1, get_asn1, ctrl) \
  100. static const EVP_CIPHER cname##_##mode = { \
  101. nid##_##nmode, block_size, key_len, iv_len, \
  102. flags | EVP_CIPH_##MODE##_MODE, \
  103. init_key, \
  104. cname##_##mode##_cipher, \
  105. cleanup, \
  106. sizeof(kstruct), \
  107. set_asn1, get_asn1,\
  108. ctrl, \
  109. NULL \
  110. }; \
  111. const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
  112. #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
  113. iv_len, flags, init_key, cleanup, set_asn1, \
  114. get_asn1, ctrl) \
  115. BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
  116. iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  117. #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
  118. iv_len, cbits, flags, init_key, cleanup, \
  119. set_asn1, get_asn1, ctrl) \
  120. BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
  121. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  122. get_asn1, ctrl)
  123. #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
  124. iv_len, cbits, flags, init_key, cleanup, \
  125. set_asn1, get_asn1, ctrl) \
  126. BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
  127. key_len, iv_len, flags, init_key, cleanup, set_asn1, \
  128. get_asn1, ctrl)
  129. #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
  130. flags, init_key, cleanup, set_asn1, \
  131. get_asn1, ctrl) \
  132. BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
  133. 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
  134. #define BLOCK_CIPHER_defs(cname, kstruct, \
  135. nid, block_size, key_len, iv_len, cbits, flags, \
  136. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  137. BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
  138. init_key, cleanup, set_asn1, get_asn1, ctrl) \
  139. BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
  140. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  141. BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
  142. flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
  143. BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
  144. init_key, cleanup, set_asn1, get_asn1, ctrl)
  145. /*-
  146. #define BLOCK_CIPHER_defs(cname, kstruct, \
  147. nid, block_size, key_len, iv_len, flags,\
  148. init_key, cleanup, set_asn1, get_asn1, ctrl)\
  149. static const EVP_CIPHER cname##_cbc = {\
  150. nid##_cbc, block_size, key_len, iv_len, \
  151. flags | EVP_CIPH_CBC_MODE,\
  152. init_key,\
  153. cname##_cbc_cipher,\
  154. cleanup,\
  155. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  156. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  157. set_asn1, get_asn1,\
  158. ctrl, \
  159. NULL \
  160. };\
  161. const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
  162. static const EVP_CIPHER cname##_cfb = {\
  163. nid##_cfb64, 1, key_len, iv_len, \
  164. flags | EVP_CIPH_CFB_MODE,\
  165. init_key,\
  166. cname##_cfb_cipher,\
  167. cleanup,\
  168. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  169. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  170. set_asn1, get_asn1,\
  171. ctrl,\
  172. NULL \
  173. };\
  174. const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
  175. static const EVP_CIPHER cname##_ofb = {\
  176. nid##_ofb64, 1, key_len, iv_len, \
  177. flags | EVP_CIPH_OFB_MODE,\
  178. init_key,\
  179. cname##_ofb_cipher,\
  180. cleanup,\
  181. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  182. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  183. set_asn1, get_asn1,\
  184. ctrl,\
  185. NULL \
  186. };\
  187. const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
  188. static const EVP_CIPHER cname##_ecb = {\
  189. nid##_ecb, block_size, key_len, iv_len, \
  190. flags | EVP_CIPH_ECB_MODE,\
  191. init_key,\
  192. cname##_ecb_cipher,\
  193. cleanup,\
  194. sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
  195. sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
  196. set_asn1, get_asn1,\
  197. ctrl,\
  198. NULL \
  199. };\
  200. const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
  201. */
  202. #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
  203. block_size, key_len, iv_len, cbits, \
  204. flags, init_key, \
  205. cleanup, set_asn1, get_asn1, ctrl) \
  206. BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
  207. BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
  208. cbits, flags, init_key, cleanup, set_asn1, \
  209. get_asn1, ctrl)
  210. #define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data)
  211. #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
  212. BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
  213. BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
  214. NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
  215. (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
  216. cipher##_init_key, NULL, NULL, NULL, NULL)
  217. #ifdef OPENSSL_FIPS
  218. # define RC2_set_key private_RC2_set_key
  219. # define RC4_set_key private_RC4_set_key
  220. # define CAST_set_key private_CAST_set_key
  221. # define RC5_32_set_key private_RC5_32_set_key
  222. # define BF_set_key private_BF_set_key
  223. # define Camellia_set_key private_Camellia_set_key
  224. # define idea_set_encrypt_key private_idea_set_encrypt_key
  225. # define MD5_Init private_MD5_Init
  226. # define MD4_Init private_MD4_Init
  227. # define MD2_Init private_MD2_Init
  228. # define MDC2_Init private_MDC2_Init
  229. # define SHA_Init private_SHA_Init
  230. #endif