bf_enc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* crypto/bf/bf_enc.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <openssl/blowfish.h>
  59. #include "bf_locl.h"
  60. /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
  61. * (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
  62. * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
  63. */
  64. #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
  65. #error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
  66. to modify the code.
  67. #endif
  68. void BF_encrypt(BF_LONG *data, const BF_KEY *key)
  69. {
  70. #ifndef BF_PTR2
  71. register BF_LONG l,r;
  72. register const BF_LONG *p,*s;
  73. p=key->P;
  74. s= &(key->S[0]);
  75. l=data[0];
  76. r=data[1];
  77. l^=p[0];
  78. BF_ENC(r,l,s,p[ 1]);
  79. BF_ENC(l,r,s,p[ 2]);
  80. BF_ENC(r,l,s,p[ 3]);
  81. BF_ENC(l,r,s,p[ 4]);
  82. BF_ENC(r,l,s,p[ 5]);
  83. BF_ENC(l,r,s,p[ 6]);
  84. BF_ENC(r,l,s,p[ 7]);
  85. BF_ENC(l,r,s,p[ 8]);
  86. BF_ENC(r,l,s,p[ 9]);
  87. BF_ENC(l,r,s,p[10]);
  88. BF_ENC(r,l,s,p[11]);
  89. BF_ENC(l,r,s,p[12]);
  90. BF_ENC(r,l,s,p[13]);
  91. BF_ENC(l,r,s,p[14]);
  92. BF_ENC(r,l,s,p[15]);
  93. BF_ENC(l,r,s,p[16]);
  94. #if BF_ROUNDS == 20
  95. BF_ENC(r,l,s,p[17]);
  96. BF_ENC(l,r,s,p[18]);
  97. BF_ENC(r,l,s,p[19]);
  98. BF_ENC(l,r,s,p[20]);
  99. #endif
  100. r^=p[BF_ROUNDS+1];
  101. data[1]=l&0xffffffffL;
  102. data[0]=r&0xffffffffL;
  103. #else
  104. register BF_LONG l,r,t,*k;
  105. l=data[0];
  106. r=data[1];
  107. k=(BF_LONG*)key;
  108. l^=k[0];
  109. BF_ENC(r,l,k, 1);
  110. BF_ENC(l,r,k, 2);
  111. BF_ENC(r,l,k, 3);
  112. BF_ENC(l,r,k, 4);
  113. BF_ENC(r,l,k, 5);
  114. BF_ENC(l,r,k, 6);
  115. BF_ENC(r,l,k, 7);
  116. BF_ENC(l,r,k, 8);
  117. BF_ENC(r,l,k, 9);
  118. BF_ENC(l,r,k,10);
  119. BF_ENC(r,l,k,11);
  120. BF_ENC(l,r,k,12);
  121. BF_ENC(r,l,k,13);
  122. BF_ENC(l,r,k,14);
  123. BF_ENC(r,l,k,15);
  124. BF_ENC(l,r,k,16);
  125. #if BF_ROUNDS == 20
  126. BF_ENC(r,l,k,17);
  127. BF_ENC(l,r,k,18);
  128. BF_ENC(r,l,k,19);
  129. BF_ENC(l,r,k,20);
  130. #endif
  131. r^=k[BF_ROUNDS+1];
  132. data[1]=l&0xffffffffL;
  133. data[0]=r&0xffffffffL;
  134. #endif
  135. }
  136. #ifndef BF_DEFAULT_OPTIONS
  137. void BF_decrypt(BF_LONG *data, const BF_KEY *key)
  138. {
  139. #ifndef BF_PTR2
  140. register BF_LONG l,r;
  141. register const BF_LONG *p,*s;
  142. p=key->P;
  143. s= &(key->S[0]);
  144. l=data[0];
  145. r=data[1];
  146. l^=p[BF_ROUNDS+1];
  147. #if BF_ROUNDS == 20
  148. BF_ENC(r,l,s,p[20]);
  149. BF_ENC(l,r,s,p[19]);
  150. BF_ENC(r,l,s,p[18]);
  151. BF_ENC(l,r,s,p[17]);
  152. #endif
  153. BF_ENC(r,l,s,p[16]);
  154. BF_ENC(l,r,s,p[15]);
  155. BF_ENC(r,l,s,p[14]);
  156. BF_ENC(l,r,s,p[13]);
  157. BF_ENC(r,l,s,p[12]);
  158. BF_ENC(l,r,s,p[11]);
  159. BF_ENC(r,l,s,p[10]);
  160. BF_ENC(l,r,s,p[ 9]);
  161. BF_ENC(r,l,s,p[ 8]);
  162. BF_ENC(l,r,s,p[ 7]);
  163. BF_ENC(r,l,s,p[ 6]);
  164. BF_ENC(l,r,s,p[ 5]);
  165. BF_ENC(r,l,s,p[ 4]);
  166. BF_ENC(l,r,s,p[ 3]);
  167. BF_ENC(r,l,s,p[ 2]);
  168. BF_ENC(l,r,s,p[ 1]);
  169. r^=p[0];
  170. data[1]=l&0xffffffffL;
  171. data[0]=r&0xffffffffL;
  172. #else
  173. register BF_LONG l,r,t,*k;
  174. l=data[0];
  175. r=data[1];
  176. k=(BF_LONG *)key;
  177. l^=k[BF_ROUNDS+1];
  178. #if BF_ROUNDS == 20
  179. BF_ENC(r,l,k,20);
  180. BF_ENC(l,r,k,19);
  181. BF_ENC(r,l,k,18);
  182. BF_ENC(l,r,k,17);
  183. #endif
  184. BF_ENC(r,l,k,16);
  185. BF_ENC(l,r,k,15);
  186. BF_ENC(r,l,k,14);
  187. BF_ENC(l,r,k,13);
  188. BF_ENC(r,l,k,12);
  189. BF_ENC(l,r,k,11);
  190. BF_ENC(r,l,k,10);
  191. BF_ENC(l,r,k, 9);
  192. BF_ENC(r,l,k, 8);
  193. BF_ENC(l,r,k, 7);
  194. BF_ENC(r,l,k, 6);
  195. BF_ENC(l,r,k, 5);
  196. BF_ENC(r,l,k, 4);
  197. BF_ENC(l,r,k, 3);
  198. BF_ENC(r,l,k, 2);
  199. BF_ENC(l,r,k, 1);
  200. r^=k[0];
  201. data[1]=l&0xffffffffL;
  202. data[0]=r&0xffffffffL;
  203. #endif
  204. }
  205. void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  206. const BF_KEY *schedule, unsigned char *ivec, int encrypt)
  207. {
  208. register BF_LONG tin0,tin1;
  209. register BF_LONG tout0,tout1,xor0,xor1;
  210. register long l=length;
  211. BF_LONG tin[2];
  212. if (encrypt)
  213. {
  214. n2l(ivec,tout0);
  215. n2l(ivec,tout1);
  216. ivec-=8;
  217. for (l-=8; l>=0; l-=8)
  218. {
  219. n2l(in,tin0);
  220. n2l(in,tin1);
  221. tin0^=tout0;
  222. tin1^=tout1;
  223. tin[0]=tin0;
  224. tin[1]=tin1;
  225. BF_encrypt(tin,schedule);
  226. tout0=tin[0];
  227. tout1=tin[1];
  228. l2n(tout0,out);
  229. l2n(tout1,out);
  230. }
  231. if (l != -8)
  232. {
  233. n2ln(in,tin0,tin1,l+8);
  234. tin0^=tout0;
  235. tin1^=tout1;
  236. tin[0]=tin0;
  237. tin[1]=tin1;
  238. BF_encrypt(tin,schedule);
  239. tout0=tin[0];
  240. tout1=tin[1];
  241. l2n(tout0,out);
  242. l2n(tout1,out);
  243. }
  244. l2n(tout0,ivec);
  245. l2n(tout1,ivec);
  246. }
  247. else
  248. {
  249. n2l(ivec,xor0);
  250. n2l(ivec,xor1);
  251. ivec-=8;
  252. for (l-=8; l>=0; l-=8)
  253. {
  254. n2l(in,tin0);
  255. n2l(in,tin1);
  256. tin[0]=tin0;
  257. tin[1]=tin1;
  258. BF_decrypt(tin,schedule);
  259. tout0=tin[0]^xor0;
  260. tout1=tin[1]^xor1;
  261. l2n(tout0,out);
  262. l2n(tout1,out);
  263. xor0=tin0;
  264. xor1=tin1;
  265. }
  266. if (l != -8)
  267. {
  268. n2l(in,tin0);
  269. n2l(in,tin1);
  270. tin[0]=tin0;
  271. tin[1]=tin1;
  272. BF_decrypt(tin,schedule);
  273. tout0=tin[0]^xor0;
  274. tout1=tin[1]^xor1;
  275. l2nn(tout0,tout1,out,l+8);
  276. xor0=tin0;
  277. xor1=tin1;
  278. }
  279. l2n(xor0,ivec);
  280. l2n(xor1,ivec);
  281. }
  282. tin0=tin1=tout0=tout1=xor0=xor1=0;
  283. tin[0]=tin[1]=0;
  284. }
  285. #endif