des_enc.c 12 KB

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