des_enc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/crypto.h>
  10. #include "des_local.h"
  11. #include "spr.h"
  12. void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
  13. {
  14. register DES_LONG l, r, t, u;
  15. register DES_LONG *s;
  16. r = data[0];
  17. l = data[1];
  18. IP(r, l);
  19. /*
  20. * Things have been modified so that the initial rotate is done outside
  21. * the loop. This required the DES_SPtrans values in sp.h to be rotated
  22. * 1 bit to the right. One perl script later and things have a 5% speed
  23. * up on a sparc2. Thanks to Richard Outerbridge for pointing this out.
  24. */
  25. /* clear the top bits on machines with 8byte longs */
  26. /* shift left by 2 */
  27. r = ROTATE(r, 29) & 0xffffffffL;
  28. l = ROTATE(l, 29) & 0xffffffffL;
  29. s = ks->ks->deslong;
  30. /*
  31. * I don't know if it is worth the effort of loop unrolling the inner
  32. * loop
  33. */
  34. if (enc) {
  35. D_ENCRYPT(l, r, 0); /* 1 */
  36. D_ENCRYPT(r, l, 2); /* 2 */
  37. D_ENCRYPT(l, r, 4); /* 3 */
  38. D_ENCRYPT(r, l, 6); /* 4 */
  39. D_ENCRYPT(l, r, 8); /* 5 */
  40. D_ENCRYPT(r, l, 10); /* 6 */
  41. D_ENCRYPT(l, r, 12); /* 7 */
  42. D_ENCRYPT(r, l, 14); /* 8 */
  43. D_ENCRYPT(l, r, 16); /* 9 */
  44. D_ENCRYPT(r, l, 18); /* 10 */
  45. D_ENCRYPT(l, r, 20); /* 11 */
  46. D_ENCRYPT(r, l, 22); /* 12 */
  47. D_ENCRYPT(l, r, 24); /* 13 */
  48. D_ENCRYPT(r, l, 26); /* 14 */
  49. D_ENCRYPT(l, r, 28); /* 15 */
  50. D_ENCRYPT(r, l, 30); /* 16 */
  51. } else {
  52. D_ENCRYPT(l, r, 30); /* 16 */
  53. D_ENCRYPT(r, l, 28); /* 15 */
  54. D_ENCRYPT(l, r, 26); /* 14 */
  55. D_ENCRYPT(r, l, 24); /* 13 */
  56. D_ENCRYPT(l, r, 22); /* 12 */
  57. D_ENCRYPT(r, l, 20); /* 11 */
  58. D_ENCRYPT(l, r, 18); /* 10 */
  59. D_ENCRYPT(r, l, 16); /* 9 */
  60. D_ENCRYPT(l, r, 14); /* 8 */
  61. D_ENCRYPT(r, l, 12); /* 7 */
  62. D_ENCRYPT(l, r, 10); /* 6 */
  63. D_ENCRYPT(r, l, 8); /* 5 */
  64. D_ENCRYPT(l, r, 6); /* 4 */
  65. D_ENCRYPT(r, l, 4); /* 3 */
  66. D_ENCRYPT(l, r, 2); /* 2 */
  67. D_ENCRYPT(r, l, 0); /* 1 */
  68. }
  69. /* rotate and clear the top bits on machines with 8byte longs */
  70. l = ROTATE(l, 3) & 0xffffffffL;
  71. r = ROTATE(r, 3) & 0xffffffffL;
  72. FP(r, l);
  73. data[0] = l;
  74. data[1] = r;
  75. l = r = t = u = 0;
  76. }
  77. void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
  78. {
  79. register DES_LONG l, r, t, u;
  80. register DES_LONG *s;
  81. r = data[0];
  82. l = data[1];
  83. /*
  84. * Things have been modified so that the initial rotate is done outside
  85. * the loop. This required the DES_SPtrans values in sp.h to be rotated
  86. * 1 bit to the right. One perl script later and things have a 5% speed
  87. * up on a sparc2. Thanks to Richard Outerbridge for pointing this out.
  88. */
  89. /* clear the top bits on machines with 8byte longs */
  90. r = ROTATE(r, 29) & 0xffffffffL;
  91. l = ROTATE(l, 29) & 0xffffffffL;
  92. s = ks->ks->deslong;
  93. /*
  94. * I don't know if it is worth the effort of loop unrolling the inner
  95. * loop
  96. */
  97. if (enc) {
  98. D_ENCRYPT(l, r, 0); /* 1 */
  99. D_ENCRYPT(r, l, 2); /* 2 */
  100. D_ENCRYPT(l, r, 4); /* 3 */
  101. D_ENCRYPT(r, l, 6); /* 4 */
  102. D_ENCRYPT(l, r, 8); /* 5 */
  103. D_ENCRYPT(r, l, 10); /* 6 */
  104. D_ENCRYPT(l, r, 12); /* 7 */
  105. D_ENCRYPT(r, l, 14); /* 8 */
  106. D_ENCRYPT(l, r, 16); /* 9 */
  107. D_ENCRYPT(r, l, 18); /* 10 */
  108. D_ENCRYPT(l, r, 20); /* 11 */
  109. D_ENCRYPT(r, l, 22); /* 12 */
  110. D_ENCRYPT(l, r, 24); /* 13 */
  111. D_ENCRYPT(r, l, 26); /* 14 */
  112. D_ENCRYPT(l, r, 28); /* 15 */
  113. D_ENCRYPT(r, l, 30); /* 16 */
  114. } else {
  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. }
  132. /* rotate and clear the top bits on machines with 8byte longs */
  133. data[0] = ROTATE(l, 3) & 0xffffffffL;
  134. data[1] = ROTATE(r, 3) & 0xffffffffL;
  135. l = r = t = u = 0;
  136. }
  137. void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
  138. DES_key_schedule *ks2, DES_key_schedule *ks3)
  139. {
  140. register DES_LONG l, r;
  141. l = data[0];
  142. r = data[1];
  143. IP(l, r);
  144. data[0] = l;
  145. data[1] = r;
  146. DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT);
  147. DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT);
  148. DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT);
  149. l = data[0];
  150. r = data[1];
  151. FP(r, l);
  152. data[0] = l;
  153. data[1] = r;
  154. }
  155. void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
  156. DES_key_schedule *ks2, DES_key_schedule *ks3)
  157. {
  158. register DES_LONG l, r;
  159. l = data[0];
  160. r = data[1];
  161. IP(l, r);
  162. data[0] = l;
  163. data[1] = r;
  164. DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT);
  165. DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT);
  166. DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT);
  167. l = data[0];
  168. r = data[1];
  169. FP(r, l);
  170. data[0] = l;
  171. data[1] = r;
  172. }
  173. #ifndef DES_DEFAULT_OPTIONS
  174. # undef CBC_ENC_C__DONT_UPDATE_IV
  175. # include "ncbc_enc.c" /* DES_ncbc_encrypt */
  176. void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
  177. long length, DES_key_schedule *ks1,
  178. DES_key_schedule *ks2, DES_key_schedule *ks3,
  179. DES_cblock *ivec, int enc)
  180. {
  181. register DES_LONG tin0, tin1;
  182. register DES_LONG tout0, tout1, xor0, xor1;
  183. register const unsigned char *in;
  184. unsigned char *out;
  185. register long l = length;
  186. DES_LONG tin[2];
  187. unsigned char *iv;
  188. in = input;
  189. out = output;
  190. iv = &(*ivec)[0];
  191. if (enc) {
  192. c2l(iv, tout0);
  193. c2l(iv, tout1);
  194. for (l -= 8; l >= 0; l -= 8) {
  195. c2l(in, tin0);
  196. c2l(in, tin1);
  197. tin0 ^= tout0;
  198. tin1 ^= tout1;
  199. tin[0] = tin0;
  200. tin[1] = tin1;
  201. DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  202. tout0 = tin[0];
  203. tout1 = tin[1];
  204. l2c(tout0, out);
  205. l2c(tout1, out);
  206. }
  207. if (l != -8) {
  208. c2ln(in, tin0, tin1, l + 8);
  209. tin0 ^= tout0;
  210. tin1 ^= tout1;
  211. tin[0] = tin0;
  212. tin[1] = tin1;
  213. DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  214. tout0 = tin[0];
  215. tout1 = tin[1];
  216. l2c(tout0, out);
  217. l2c(tout1, out);
  218. }
  219. iv = &(*ivec)[0];
  220. l2c(tout0, iv);
  221. l2c(tout1, iv);
  222. } else {
  223. register DES_LONG t0, t1;
  224. c2l(iv, xor0);
  225. c2l(iv, xor1);
  226. for (l -= 8; l >= 0; l -= 8) {
  227. c2l(in, tin0);
  228. c2l(in, tin1);
  229. t0 = tin0;
  230. t1 = tin1;
  231. tin[0] = tin0;
  232. tin[1] = tin1;
  233. DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  234. tout0 = tin[0];
  235. tout1 = tin[1];
  236. tout0 ^= xor0;
  237. tout1 ^= xor1;
  238. l2c(tout0, out);
  239. l2c(tout1, out);
  240. xor0 = t0;
  241. xor1 = t1;
  242. }
  243. if (l != -8) {
  244. c2l(in, tin0);
  245. c2l(in, tin1);
  246. t0 = tin0;
  247. t1 = tin1;
  248. tin[0] = tin0;
  249. tin[1] = tin1;
  250. DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
  251. tout0 = tin[0];
  252. tout1 = tin[1];
  253. tout0 ^= xor0;
  254. tout1 ^= xor1;
  255. l2cn(tout0, tout1, out, l + 8);
  256. xor0 = t0;
  257. xor1 = t1;
  258. }
  259. iv = &(*ivec)[0];
  260. l2c(xor0, iv);
  261. l2c(xor1, iv);
  262. }
  263. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  264. tin[0] = tin[1] = 0;
  265. }
  266. #endif /* DES_DEFAULT_OPTIONS */