bf_enc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /*
  61. * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From
  62. * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE
  63. * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
  64. */
  65. #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
  66. # error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
  67. to modify the code.
  68. #endif
  69. void BF_encrypt(BF_LONG *data, const BF_KEY *key)
  70. {
  71. #ifndef BF_PTR2
  72. register BF_LONG l, r;
  73. register const BF_LONG *p, *s;
  74. p = key->P;
  75. s = &(key->S[0]);
  76. l = data[0];
  77. r = data[1];
  78. l ^= p[0];
  79. BF_ENC(r, l, s, p[1]);
  80. BF_ENC(l, r, s, p[2]);
  81. BF_ENC(r, l, s, p[3]);
  82. BF_ENC(l, r, s, p[4]);
  83. BF_ENC(r, l, s, p[5]);
  84. BF_ENC(l, r, s, p[6]);
  85. BF_ENC(r, l, s, p[7]);
  86. BF_ENC(l, r, s, p[8]);
  87. BF_ENC(r, l, s, p[9]);
  88. BF_ENC(l, r, s, p[10]);
  89. BF_ENC(r, l, s, p[11]);
  90. BF_ENC(l, r, s, p[12]);
  91. BF_ENC(r, l, s, p[13]);
  92. BF_ENC(l, r, s, p[14]);
  93. BF_ENC(r, l, s, p[15]);
  94. BF_ENC(l, r, s, p[16]);
  95. # if BF_ROUNDS == 20
  96. BF_ENC(r, l, s, p[17]);
  97. BF_ENC(l, r, s, p[18]);
  98. BF_ENC(r, l, s, p[19]);
  99. BF_ENC(l, r, s, p[20]);
  100. # endif
  101. r ^= p[BF_ROUNDS + 1];
  102. data[1] = l & 0xffffffffL;
  103. data[0] = r & 0xffffffffL;
  104. #else
  105. register BF_LONG l, r, t, *k;
  106. l = data[0];
  107. r = data[1];
  108. k = (BF_LONG *)key;
  109. l ^= k[0];
  110. BF_ENC(r, l, k, 1);
  111. BF_ENC(l, r, k, 2);
  112. BF_ENC(r, l, k, 3);
  113. BF_ENC(l, r, k, 4);
  114. BF_ENC(r, l, k, 5);
  115. BF_ENC(l, r, k, 6);
  116. BF_ENC(r, l, k, 7);
  117. BF_ENC(l, r, k, 8);
  118. BF_ENC(r, l, k, 9);
  119. BF_ENC(l, r, k, 10);
  120. BF_ENC(r, l, k, 11);
  121. BF_ENC(l, r, k, 12);
  122. BF_ENC(r, l, k, 13);
  123. BF_ENC(l, r, k, 14);
  124. BF_ENC(r, l, k, 15);
  125. BF_ENC(l, r, k, 16);
  126. # if BF_ROUNDS == 20
  127. BF_ENC(r, l, k, 17);
  128. BF_ENC(l, r, k, 18);
  129. BF_ENC(r, l, k, 19);
  130. BF_ENC(l, r, k, 20);
  131. # endif
  132. r ^= k[BF_ROUNDS + 1];
  133. data[1] = l & 0xffffffffL;
  134. data[0] = r & 0xffffffffL;
  135. #endif
  136. }
  137. #ifndef BF_DEFAULT_OPTIONS
  138. void BF_decrypt(BF_LONG *data, const BF_KEY *key)
  139. {
  140. # ifndef BF_PTR2
  141. register BF_LONG l, r;
  142. register const BF_LONG *p, *s;
  143. p = key->P;
  144. s = &(key->S[0]);
  145. l = data[0];
  146. r = data[1];
  147. l ^= p[BF_ROUNDS + 1];
  148. # if BF_ROUNDS == 20
  149. BF_ENC(r, l, s, p[20]);
  150. BF_ENC(l, r, s, p[19]);
  151. BF_ENC(r, l, s, p[18]);
  152. BF_ENC(l, r, s, p[17]);
  153. # endif
  154. BF_ENC(r, l, s, p[16]);
  155. BF_ENC(l, r, s, p[15]);
  156. BF_ENC(r, l, s, p[14]);
  157. BF_ENC(l, r, s, p[13]);
  158. BF_ENC(r, l, s, p[12]);
  159. BF_ENC(l, r, s, p[11]);
  160. BF_ENC(r, l, s, p[10]);
  161. BF_ENC(l, r, s, p[9]);
  162. BF_ENC(r, l, s, p[8]);
  163. BF_ENC(l, r, s, p[7]);
  164. BF_ENC(r, l, s, p[6]);
  165. BF_ENC(l, r, s, p[5]);
  166. BF_ENC(r, l, s, p[4]);
  167. BF_ENC(l, r, s, p[3]);
  168. BF_ENC(r, l, s, p[2]);
  169. BF_ENC(l, r, s, p[1]);
  170. r ^= p[0];
  171. data[1] = l & 0xffffffffL;
  172. data[0] = r & 0xffffffffL;
  173. # else
  174. register BF_LONG l, r, t, *k;
  175. l = data[0];
  176. r = data[1];
  177. k = (BF_LONG *)key;
  178. l ^= k[BF_ROUNDS + 1];
  179. # if BF_ROUNDS == 20
  180. BF_ENC(r, l, k, 20);
  181. BF_ENC(l, r, k, 19);
  182. BF_ENC(r, l, k, 18);
  183. BF_ENC(l, r, k, 17);
  184. # endif
  185. BF_ENC(r, l, k, 16);
  186. BF_ENC(l, r, k, 15);
  187. BF_ENC(r, l, k, 14);
  188. BF_ENC(l, r, k, 13);
  189. BF_ENC(r, l, k, 12);
  190. BF_ENC(l, r, k, 11);
  191. BF_ENC(r, l, k, 10);
  192. BF_ENC(l, r, k, 9);
  193. BF_ENC(r, l, k, 8);
  194. BF_ENC(l, r, k, 7);
  195. BF_ENC(r, l, k, 6);
  196. BF_ENC(l, r, k, 5);
  197. BF_ENC(r, l, k, 4);
  198. BF_ENC(l, r, k, 3);
  199. BF_ENC(r, l, k, 2);
  200. BF_ENC(l, r, k, 1);
  201. r ^= k[0];
  202. data[1] = l & 0xffffffffL;
  203. data[0] = r & 0xffffffffL;
  204. # endif
  205. }
  206. void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  207. const BF_KEY *schedule, unsigned char *ivec, int encrypt)
  208. {
  209. register BF_LONG tin0, tin1;
  210. register BF_LONG tout0, tout1, xor0, xor1;
  211. register long l = length;
  212. BF_LONG tin[2];
  213. if (encrypt) {
  214. n2l(ivec, tout0);
  215. n2l(ivec, tout1);
  216. ivec -= 8;
  217. for (l -= 8; l >= 0; l -= 8) {
  218. n2l(in, tin0);
  219. n2l(in, tin1);
  220. tin0 ^= tout0;
  221. tin1 ^= tout1;
  222. tin[0] = tin0;
  223. tin[1] = tin1;
  224. BF_encrypt(tin, schedule);
  225. tout0 = tin[0];
  226. tout1 = tin[1];
  227. l2n(tout0, out);
  228. l2n(tout1, out);
  229. }
  230. if (l != -8) {
  231. n2ln(in, tin0, tin1, l + 8);
  232. tin0 ^= tout0;
  233. tin1 ^= tout1;
  234. tin[0] = tin0;
  235. tin[1] = tin1;
  236. BF_encrypt(tin, schedule);
  237. tout0 = tin[0];
  238. tout1 = tin[1];
  239. l2n(tout0, out);
  240. l2n(tout1, out);
  241. }
  242. l2n(tout0, ivec);
  243. l2n(tout1, ivec);
  244. } else {
  245. n2l(ivec, xor0);
  246. n2l(ivec, xor1);
  247. ivec -= 8;
  248. for (l -= 8; l >= 0; l -= 8) {
  249. n2l(in, tin0);
  250. n2l(in, tin1);
  251. tin[0] = tin0;
  252. tin[1] = tin1;
  253. BF_decrypt(tin, schedule);
  254. tout0 = tin[0] ^ xor0;
  255. tout1 = tin[1] ^ xor1;
  256. l2n(tout0, out);
  257. l2n(tout1, out);
  258. xor0 = tin0;
  259. xor1 = tin1;
  260. }
  261. if (l != -8) {
  262. n2l(in, tin0);
  263. n2l(in, tin1);
  264. tin[0] = tin0;
  265. tin[1] = tin1;
  266. BF_decrypt(tin, schedule);
  267. tout0 = tin[0] ^ xor0;
  268. tout1 = tin[1] ^ xor1;
  269. l2nn(tout0, tout1, out, l + 8);
  270. xor0 = tin0;
  271. xor1 = tin1;
  272. }
  273. l2n(xor0, ivec);
  274. l2n(xor1, ivec);
  275. }
  276. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  277. tin[0] = tin[1] = 0;
  278. }
  279. #endif