2
0

des_enc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* crypto/des/des_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 "des_locl.h"
  59. #include "spr.h"
  60. void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
  61. {
  62. register DES_LONG l, r, t, u;
  63. #ifdef DES_PTR
  64. register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans;
  65. #endif
  66. #ifndef DES_UNROLL
  67. register int i;
  68. #endif
  69. register DES_LONG *s;
  70. r = data[0];
  71. l = data[1];
  72. IP(r, l);
  73. /*
  74. * Things have been modified so that the initial rotate is done outside
  75. * the loop. This required the DES_SPtrans values in sp.h to be rotated
  76. * 1 bit to the right. One perl script later and things have a 5% speed
  77. * up on a sparc2. Thanks to Richard Outerbridge
  78. * <71755.204@CompuServe.COM> for pointing this out.
  79. */
  80. /* clear the top bits on machines with 8byte longs */
  81. /* shift left by 2 */
  82. r = ROTATE(r, 29) & 0xffffffffL;
  83. l = ROTATE(l, 29) & 0xffffffffL;
  84. s = ks->ks->deslong;
  85. /*
  86. * I don't know if it is worth the effort of loop unrolling the inner
  87. * loop
  88. */
  89. if (enc) {
  90. #ifdef DES_UNROLL
  91. D_ENCRYPT(l, r, 0); /* 1 */
  92. D_ENCRYPT(r, l, 2); /* 2 */
  93. D_ENCRYPT(l, r, 4); /* 3 */
  94. D_ENCRYPT(r, l, 6); /* 4 */
  95. D_ENCRYPT(l, r, 8); /* 5 */
  96. D_ENCRYPT(r, l, 10); /* 6 */
  97. D_ENCRYPT(l, r, 12); /* 7 */
  98. D_ENCRYPT(r, l, 14); /* 8 */
  99. D_ENCRYPT(l, r, 16); /* 9 */
  100. D_ENCRYPT(r, l, 18); /* 10 */
  101. D_ENCRYPT(l, r, 20); /* 11 */
  102. D_ENCRYPT(r, l, 22); /* 12 */
  103. D_ENCRYPT(l, r, 24); /* 13 */
  104. D_ENCRYPT(r, l, 26); /* 14 */
  105. D_ENCRYPT(l, r, 28); /* 15 */
  106. D_ENCRYPT(r, l, 30); /* 16 */
  107. #else
  108. for (i = 0; i < 32; i += 4) {
  109. D_ENCRYPT(l, r, i + 0); /* 1 */
  110. D_ENCRYPT(r, l, i + 2); /* 2 */
  111. }
  112. #endif
  113. } else {
  114. #ifdef DES_UNROLL
  115. D_ENCRYPT(l, r, 30); /* 16 */
  116. D_ENCRYPT(r, l, 28); /* 15 */
  117. D_ENCRYPT(l, r, 26); /* 14 */
  118. D_ENCRYPT(r, l, 24); /* 13 */
  119. D_ENCRYPT(l, r, 22); /* 12 */
  120. D_ENCRYPT(r, l, 20); /* 11 */
  121. D_ENCRYPT(l, r, 18); /* 10 */
  122. D_ENCRYPT(r, l, 16); /* 9 */
  123. D_ENCRYPT(l, r, 14); /* 8 */
  124. D_ENCRYPT(r, l, 12); /* 7 */
  125. D_ENCRYPT(l, r, 10); /* 6 */
  126. D_ENCRYPT(r, l, 8); /* 5 */
  127. D_ENCRYPT(l, r, 6); /* 4 */
  128. D_ENCRYPT(r, l, 4); /* 3 */
  129. D_ENCRYPT(l, r, 2); /* 2 */
  130. D_ENCRYPT(r, l, 0); /* 1 */
  131. #else
  132. for (i = 30; i > 0; i -= 4) {
  133. D_ENCRYPT(l, r, i - 0); /* 16 */
  134. D_ENCRYPT(r, l, i - 2); /* 15 */
  135. }
  136. #endif
  137. }
  138. /* rotate and clear the top bits on machines with 8byte longs */
  139. l = ROTATE(l, 3) & 0xffffffffL;
  140. r = ROTATE(r, 3) & 0xffffffffL;
  141. FP(r, l);
  142. data[0] = l;
  143. data[1] = r;
  144. l = r = t = u = 0;
  145. }
  146. void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
  147. {
  148. register DES_LONG l, r, t, u;
  149. #ifdef DES_PTR
  150. register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans;
  151. #endif
  152. #ifndef DES_UNROLL
  153. register int i;
  154. #endif
  155. register DES_LONG *s;
  156. r = data[0];
  157. l = data[1];
  158. /*
  159. * Things have been modified so that the initial rotate is done outside
  160. * the loop. This required the DES_SPtrans values in sp.h to be rotated
  161. * 1 bit to the right. One perl script later and things have a 5% speed
  162. * up on a sparc2. Thanks to Richard Outerbridge
  163. * <71755.204@CompuServe.COM> for pointing this out.
  164. */
  165. /* clear the top bits on machines with 8byte longs */
  166. r = ROTATE(r, 29) & 0xffffffffL;
  167. l = ROTATE(l, 29) & 0xffffffffL;
  168. s = ks->ks->deslong;
  169. /*
  170. * I don't know if it is worth the effort of loop unrolling the inner
  171. * loop
  172. */
  173. if (enc) {
  174. #ifdef DES_UNROLL
  175. D_ENCRYPT(l, r, 0); /* 1 */
  176. D_ENCRYPT(r, l, 2); /* 2 */
  177. D_ENCRYPT(l, r, 4); /* 3 */
  178. D_ENCRYPT(r, l, 6); /* 4 */
  179. D_ENCRYPT(l, r, 8); /* 5 */
  180. D_ENCRYPT(r, l, 10); /* 6 */
  181. D_ENCRYPT(l, r, 12); /* 7 */
  182. D_ENCRYPT(r, l, 14); /* 8 */
  183. D_ENCRYPT(l, r, 16); /* 9 */
  184. D_ENCRYPT(r, l, 18); /* 10 */
  185. D_ENCRYPT(l, r, 20); /* 11 */
  186. D_ENCRYPT(r, l, 22); /* 12 */
  187. D_ENCRYPT(l, r, 24); /* 13 */
  188. D_ENCRYPT(r, l, 26); /* 14 */
  189. D_ENCRYPT(l, r, 28); /* 15 */
  190. D_ENCRYPT(r, l, 30); /* 16 */
  191. #else
  192. for (i = 0; i < 32; i += 4) {
  193. D_ENCRYPT(l, r, i + 0); /* 1 */
  194. D_ENCRYPT(r, l, i + 2); /* 2 */
  195. }
  196. #endif
  197. } else {
  198. #ifdef DES_UNROLL
  199. D_ENCRYPT(l, r, 30); /* 16 */
  200. D_ENCRYPT(r, l, 28); /* 15 */
  201. D_ENCRYPT(l, r, 26); /* 14 */
  202. D_ENCRYPT(r, l, 24); /* 13 */
  203. D_ENCRYPT(l, r, 22); /* 12 */
  204. D_ENCRYPT(r, l, 20); /* 11 */
  205. D_ENCRYPT(l, r, 18); /* 10 */
  206. D_ENCRYPT(r, l, 16); /* 9 */
  207. D_ENCRYPT(l, r, 14); /* 8 */
  208. D_ENCRYPT(r, l, 12); /* 7 */
  209. D_ENCRYPT(l, r, 10); /* 6 */
  210. D_ENCRYPT(r, l, 8); /* 5 */
  211. D_ENCRYPT(l, r, 6); /* 4 */
  212. D_ENCRYPT(r, l, 4); /* 3 */
  213. D_ENCRYPT(l, r, 2); /* 2 */
  214. D_ENCRYPT(r, l, 0); /* 1 */
  215. #else
  216. for (i = 30; i > 0; i -= 4) {
  217. D_ENCRYPT(l, r, i - 0); /* 16 */
  218. D_ENCRYPT(r, l, i - 2); /* 15 */
  219. }
  220. #endif
  221. }
  222. /* rotate and clear the top bits on machines with 8byte longs */
  223. data[0] = ROTATE(l, 3) & 0xffffffffL;
  224. data[1] = ROTATE(r, 3) & 0xffffffffL;
  225. l = r = t = u = 0;
  226. }
  227. void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
  228. DES_key_schedule *ks2, DES_key_schedule *ks3)
  229. {
  230. register DES_LONG l, r;
  231. l = data[0];
  232. r = data[1];
  233. IP(l, r);
  234. data[0] = l;
  235. data[1] = r;
  236. DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT);
  237. DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT);
  238. DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT);
  239. l = data[0];
  240. r = data[1];
  241. FP(r, l);
  242. data[0] = l;
  243. data[1] = r;
  244. }
  245. void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
  246. DES_key_schedule *ks2, DES_key_schedule *ks3)
  247. {
  248. register DES_LONG l, r;
  249. l = data[0];
  250. r = data[1];
  251. IP(l, r);
  252. data[0] = l;
  253. data[1] = r;
  254. DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT);
  255. DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT);
  256. DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT);
  257. l = data[0];
  258. r = data[1];
  259. FP(r, l);
  260. data[0] = l;
  261. data[1] = r;
  262. }
  263. #ifndef DES_DEFAULT_OPTIONS
  264. # undef CBC_ENC_C__DONT_UPDATE_IV
  265. # include "ncbc_enc.c" /* DES_ncbc_encrypt */
  266. void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
  267. long length, DES_key_schedule *ks1,
  268. DES_key_schedule *ks2, DES_key_schedule *ks3,
  269. DES_cblock *ivec, int enc)
  270. {
  271. register DES_LONG tin0, tin1;
  272. register DES_LONG tout0, tout1, xor0, xor1;
  273. register const unsigned char *in;
  274. unsigned char *out;
  275. register long l = length;
  276. DES_LONG tin[2];
  277. unsigned char *iv;
  278. in = input;
  279. out = output;
  280. iv = &(*ivec)[0];
  281. if (enc) {
  282. c2l(iv, tout0);
  283. c2l(iv, tout1);
  284. for (l -= 8; l >= 0; l -= 8) {
  285. c2l(in, tin0);
  286. c2l(in, tin1);
  287. tin0 ^= tout0;
  288. tin1 ^= tout1;
  289. tin[0] = tin0;
  290. tin[1] = tin1;
  291. DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  292. tout0 = tin[0];
  293. tout1 = tin[1];
  294. l2c(tout0, out);
  295. l2c(tout1, out);
  296. }
  297. if (l != -8) {
  298. c2ln(in, tin0, tin1, l + 8);
  299. tin0 ^= tout0;
  300. tin1 ^= tout1;
  301. tin[0] = tin0;
  302. tin[1] = tin1;
  303. DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  304. tout0 = tin[0];
  305. tout1 = tin[1];
  306. l2c(tout0, out);
  307. l2c(tout1, out);
  308. }
  309. iv = &(*ivec)[0];
  310. l2c(tout0, iv);
  311. l2c(tout1, iv);
  312. } else {
  313. register DES_LONG t0, t1;
  314. c2l(iv, xor0);
  315. c2l(iv, xor1);
  316. for (l -= 8; l >= 0; l -= 8) {
  317. c2l(in, tin0);
  318. c2l(in, tin1);
  319. t0 = tin0;
  320. t1 = tin1;
  321. tin[0] = tin0;
  322. tin[1] = tin1;
  323. DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  324. tout0 = tin[0];
  325. tout1 = tin[1];
  326. tout0 ^= xor0;
  327. tout1 ^= xor1;
  328. l2c(tout0, out);
  329. l2c(tout1, out);
  330. xor0 = t0;
  331. xor1 = t1;
  332. }
  333. if (l != -8) {
  334. c2l(in, tin0);
  335. c2l(in, tin1);
  336. t0 = tin0;
  337. t1 = tin1;
  338. tin[0] = tin0;
  339. tin[1] = tin1;
  340. DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  341. tout0 = tin[0];
  342. tout1 = tin[1];
  343. tout0 ^= xor0;
  344. tout1 ^= xor1;
  345. l2cn(tout0, tout1, out, l + 8);
  346. xor0 = t0;
  347. xor1 = t1;
  348. }
  349. iv = &(*ivec)[0];
  350. l2c(xor0, iv);
  351. l2c(xor1, iv);
  352. }
  353. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  354. tin[0] = tin[1] = 0;
  355. }
  356. #endif /* DES_DEFAULT_OPTIONS */