des_enc.c 8.6 KB

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