des.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #include "../lib9.h"
  2. #include "../libsec/libsec.h"
  3. /*
  4. * integrated sbox & p perm
  5. */
  6. static u32int spbox[] = {
  7. 0x00808200,0x00000000,0x00008000,0x00808202,0x00808002,0x00008202,0x00000002,0x00008000,
  8. 0x00000200,0x00808200,0x00808202,0x00000200,0x00800202,0x00808002,0x00800000,0x00000002,
  9. 0x00000202,0x00800200,0x00800200,0x00008200,0x00008200,0x00808000,0x00808000,0x00800202,
  10. 0x00008002,0x00800002,0x00800002,0x00008002,0x00000000,0x00000202,0x00008202,0x00800000,
  11. 0x00008000,0x00808202,0x00000002,0x00808000,0x00808200,0x00800000,0x00800000,0x00000200,
  12. 0x00808002,0x00008000,0x00008200,0x00800002,0x00000200,0x00000002,0x00800202,0x00008202,
  13. 0x00808202,0x00008002,0x00808000,0x00800202,0x00800002,0x00000202,0x00008202,0x00808200,
  14. 0x00000202,0x00800200,0x00800200,0x00000000,0x00008002,0x00008200,0x00000000,0x00808002,
  15. 0x40084010,0x40004000,0x00004000,0x00084010,0x00080000,0x00000010,0x40080010,0x40004010,
  16. 0x40000010,0x40084010,0x40084000,0x40000000,0x40004000,0x00080000,0x00000010,0x40080010,
  17. 0x00084000,0x00080010,0x40004010,0x00000000,0x40000000,0x00004000,0x00084010,0x40080000,
  18. 0x00080010,0x40000010,0x00000000,0x00084000,0x00004010,0x40084000,0x40080000,0x00004010,
  19. 0x00000000,0x00084010,0x40080010,0x00080000,0x40004010,0x40080000,0x40084000,0x00004000,
  20. 0x40080000,0x40004000,0x00000010,0x40084010,0x00084010,0x00000010,0x00004000,0x40000000,
  21. 0x00004010,0x40084000,0x00080000,0x40000010,0x00080010,0x40004010,0x40000010,0x00080010,
  22. 0x00084000,0x00000000,0x40004000,0x00004010,0x40000000,0x40080010,0x40084010,0x00084000,
  23. 0x00000104,0x04010100,0x00000000,0x04010004,0x04000100,0x00000000,0x00010104,0x04000100,
  24. 0x00010004,0x04000004,0x04000004,0x00010000,0x04010104,0x00010004,0x04010000,0x00000104,
  25. 0x04000000,0x00000004,0x04010100,0x00000100,0x00010100,0x04010000,0x04010004,0x00010104,
  26. 0x04000104,0x00010100,0x00010000,0x04000104,0x00000004,0x04010104,0x00000100,0x04000000,
  27. 0x04010100,0x04000000,0x00010004,0x00000104,0x00010000,0x04010100,0x04000100,0x00000000,
  28. 0x00000100,0x00010004,0x04010104,0x04000100,0x04000004,0x00000100,0x00000000,0x04010004,
  29. 0x04000104,0x00010000,0x04000000,0x04010104,0x00000004,0x00010104,0x00010100,0x04000004,
  30. 0x04010000,0x04000104,0x00000104,0x04010000,0x00010104,0x00000004,0x04010004,0x00010100,
  31. 0x80401000,0x80001040,0x80001040,0x00000040,0x00401040,0x80400040,0x80400000,0x80001000,
  32. 0x00000000,0x00401000,0x00401000,0x80401040,0x80000040,0x00000000,0x00400040,0x80400000,
  33. 0x80000000,0x00001000,0x00400000,0x80401000,0x00000040,0x00400000,0x80001000,0x00001040,
  34. 0x80400040,0x80000000,0x00001040,0x00400040,0x00001000,0x00401040,0x80401040,0x80000040,
  35. 0x00400040,0x80400000,0x00401000,0x80401040,0x80000040,0x00000000,0x00000000,0x00401000,
  36. 0x00001040,0x00400040,0x80400040,0x80000000,0x80401000,0x80001040,0x80001040,0x00000040,
  37. 0x80401040,0x80000040,0x80000000,0x00001000,0x80400000,0x80001000,0x00401040,0x80400040,
  38. 0x80001000,0x00001040,0x00400000,0x80401000,0x00000040,0x00400000,0x00001000,0x00401040,
  39. 0x00000080,0x01040080,0x01040000,0x21000080,0x00040000,0x00000080,0x20000000,0x01040000,
  40. 0x20040080,0x00040000,0x01000080,0x20040080,0x21000080,0x21040000,0x00040080,0x20000000,
  41. 0x01000000,0x20040000,0x20040000,0x00000000,0x20000080,0x21040080,0x21040080,0x01000080,
  42. 0x21040000,0x20000080,0x00000000,0x21000000,0x01040080,0x01000000,0x21000000,0x00040080,
  43. 0x00040000,0x21000080,0x00000080,0x01000000,0x20000000,0x01040000,0x21000080,0x20040080,
  44. 0x01000080,0x20000000,0x21040000,0x01040080,0x20040080,0x00000080,0x01000000,0x21040000,
  45. 0x21040080,0x00040080,0x21000000,0x21040080,0x01040000,0x00000000,0x20040000,0x21000000,
  46. 0x00040080,0x01000080,0x20000080,0x00040000,0x00000000,0x20040000,0x01040080,0x20000080,
  47. 0x10000008,0x10200000,0x00002000,0x10202008,0x10200000,0x00000008,0x10202008,0x00200000,
  48. 0x10002000,0x00202008,0x00200000,0x10000008,0x00200008,0x10002000,0x10000000,0x00002008,
  49. 0x00000000,0x00200008,0x10002008,0x00002000,0x00202000,0x10002008,0x00000008,0x10200008,
  50. 0x10200008,0x00000000,0x00202008,0x10202000,0x00002008,0x00202000,0x10202000,0x10000000,
  51. 0x10002000,0x00000008,0x10200008,0x00202000,0x10202008,0x00200000,0x00002008,0x10000008,
  52. 0x00200000,0x10002000,0x10000000,0x00002008,0x10000008,0x10202008,0x00202000,0x10200000,
  53. 0x00202008,0x10202000,0x00000000,0x10200008,0x00000008,0x00002000,0x10200000,0x00202008,
  54. 0x00002000,0x00200008,0x10002008,0x00000000,0x10202000,0x10000000,0x00200008,0x10002008,
  55. 0x00100000,0x02100001,0x02000401,0x00000000,0x00000400,0x02000401,0x00100401,0x02100400,
  56. 0x02100401,0x00100000,0x00000000,0x02000001,0x00000001,0x02000000,0x02100001,0x00000401,
  57. 0x02000400,0x00100401,0x00100001,0x02000400,0x02000001,0x02100000,0x02100400,0x00100001,
  58. 0x02100000,0x00000400,0x00000401,0x02100401,0x00100400,0x00000001,0x02000000,0x00100400,
  59. 0x02000000,0x00100400,0x00100000,0x02000401,0x02000401,0x02100001,0x02100001,0x00000001,
  60. 0x00100001,0x02000000,0x02000400,0x00100000,0x02100400,0x00000401,0x00100401,0x02100400,
  61. 0x00000401,0x02000001,0x02100401,0x02100000,0x00100400,0x00000000,0x00000001,0x02100401,
  62. 0x00000000,0x00100401,0x02100000,0x00000400,0x02000001,0x02000400,0x00000400,0x00100001,
  63. 0x08000820,0x00000800,0x00020000,0x08020820,0x08000000,0x08000820,0x00000020,0x08000000,
  64. 0x00020020,0x08020000,0x08020820,0x00020800,0x08020800,0x00020820,0x00000800,0x00000020,
  65. 0x08020000,0x08000020,0x08000800,0x00000820,0x00020800,0x00020020,0x08020020,0x08020800,
  66. 0x00000820,0x00000000,0x00000000,0x08020020,0x08000020,0x08000800,0x00020820,0x00020000,
  67. 0x00020820,0x00020000,0x08020800,0x00000800,0x00000020,0x08020020,0x00000800,0x00020820,
  68. 0x08000800,0x00000020,0x08000020,0x08020000,0x08020020,0x08000000,0x00020000,0x08000820,
  69. 0x00000000,0x08020820,0x00020020,0x08000020,0x08020000,0x08000800,0x08000820,0x00000000,
  70. 0x08020820,0x00020800,0x00020800,0x00000820,0x00000820,0x00020020,0x08000000,0x08020800,
  71. };
  72. /*
  73. * for manual index calculation
  74. * #define fetch(box, i, sh) (*((u32int*)((uchar*)spbox + (box << 8) + ((i >> (sh)) & 0xfc))))
  75. */
  76. #define fetch(box, i, sh) ((spbox+(box << 6))[((i >> (sh + 2)) & 0x3f)])
  77. /*
  78. * DES electronic codebook encryption of one block
  79. */
  80. void
  81. block_cipher(ulong key[32], uchar text[8], int decrypting)
  82. {
  83. u32int right, left, v0, v1;
  84. int i, keystep;
  85. /*
  86. * initial permutation
  87. */
  88. v0 = text[0] | ((u32int)text[2]<<8) | ((u32int)text[4]<<16) | ((u32int)text[6]<<24);
  89. left = text[1] | ((u32int)text[3]<<8) | ((u32int)text[5]<<16) | ((u32int)text[7]<<24);
  90. right = (left & 0xaaaaaaaa) | ((v0 >> 1) & 0x55555555);
  91. left = ((left << 1) & 0xaaaaaaaa) | (v0 & 0x55555555);
  92. left = ((left << 6) & 0x33003300)
  93. | (left & 0xcc33cc33)
  94. | ((left >> 6) & 0x00cc00cc);
  95. left = ((left << 12) & 0x0f0f0000)
  96. | (left & 0xf0f00f0f)
  97. | ((left >> 12) & 0x0000f0f0);
  98. right = ((right << 6) & 0x33003300)
  99. | (right & 0xcc33cc33)
  100. | ((right >> 6) & 0x00cc00cc);
  101. right = ((right << 12) & 0x0f0f0000)
  102. | (right & 0xf0f00f0f)
  103. | ((right >> 12) & 0x0000f0f0);
  104. if (decrypting) {
  105. keystep = -2;
  106. key = key + 32 - 2;
  107. } else
  108. keystep = 2;
  109. for (i = 0; i < 8; i++) {
  110. v0 = key[0];
  111. v0 ^= (right >> 1) | (right << 31);
  112. left ^= fetch(0, v0, 24)
  113. ^ fetch(2, v0, 16)
  114. ^ fetch(4, v0, 8)
  115. ^ fetch(6, v0, 0);
  116. v1 = key[1];
  117. v1 ^= (right << 3) | (right >> 29);
  118. left ^= fetch(1, v1, 24)
  119. ^ fetch(3, v1, 16)
  120. ^ fetch(5, v1, 8)
  121. ^ fetch(7, v1, 0);
  122. key += keystep;
  123. v0 = key[0];
  124. v0 ^= (left >> 1) | (left << 31);
  125. right ^= fetch(0, v0, 24)
  126. ^ fetch(2, v0, 16)
  127. ^ fetch(4, v0, 8)
  128. ^ fetch(6, v0, 0);
  129. v1 = key[1];
  130. v1 ^= (left << 3) | (left >> 29);
  131. right ^= fetch(1, v1, 24)
  132. ^ fetch(3, v1, 16)
  133. ^ fetch(5, v1, 8)
  134. ^ fetch(7, v1, 0);
  135. key += keystep;
  136. }
  137. /*
  138. * final permutation, inverse initial permutation
  139. */
  140. v0 = ((left << 1) & 0xaaaaaaaa) | (right & 0x55555555);
  141. v1 = (left & 0xaaaaaaaa) | ((right >> 1) & 0x55555555);
  142. v1 = ((v1 << 6) & 0x33003300)
  143. | (v1 & 0xcc33cc33)
  144. | ((v1 >> 6) & 0x00cc00cc);
  145. v1 = ((v1 << 12) & 0x0f0f0000)
  146. | (v1 & 0xf0f00f0f)
  147. | ((v1 >> 12) & 0x0000f0f0);
  148. v0 = ((v0 << 6) & 0x33003300)
  149. | (v0 & 0xcc33cc33)
  150. | ((v0 >> 6) & 0x00cc00cc);
  151. v0 = ((v0 << 12) & 0x0f0f0000)
  152. | (v0 & 0xf0f00f0f)
  153. | ((v0 >> 12) & 0x0000f0f0);
  154. text[0] = v0;
  155. text[2] = v0 >> 8;
  156. text[4] = v0 >> 16;
  157. text[6] = v0 >> 24;
  158. text[1] = v1;
  159. text[3] = v1 >> 8;
  160. text[5] = v1 >> 16;
  161. text[7] = v1 >> 24;
  162. }
  163. /*
  164. * triple DES electronic codebook encryption of one block
  165. */
  166. void
  167. triple_block_cipher(ulong expanded_key[3][32], uchar text[8], int ende)
  168. {
  169. ulong *key;
  170. u32int right, left, v0, v1;
  171. int i, j, keystep;
  172. /*
  173. * initial permutation
  174. */
  175. v0 = text[0] | ((u32int)text[2]<<8) | ((u32int)text[4]<<16) | ((u32int)text[6]<<24);
  176. left = text[1] | ((u32int)text[3]<<8) | ((u32int)text[5]<<16) | ((u32int)text[7]<<24);
  177. right = (left & 0xaaaaaaaa) | ((v0 >> 1) & 0x55555555);
  178. left = ((left << 1) & 0xaaaaaaaa) | (v0 & 0x55555555);
  179. left = ((left << 6) & 0x33003300)
  180. | (left & 0xcc33cc33)
  181. | ((left >> 6) & 0x00cc00cc);
  182. left = ((left << 12) & 0x0f0f0000)
  183. | (left & 0xf0f00f0f)
  184. | ((left >> 12) & 0x0000f0f0);
  185. right = ((right << 6) & 0x33003300)
  186. | (right & 0xcc33cc33)
  187. | ((right >> 6) & 0x00cc00cc);
  188. right = ((right << 12) & 0x0f0f0000)
  189. | (right & 0xf0f00f0f)
  190. | ((right >> 12) & 0x0000f0f0);
  191. for(j = 0; j < 3; j++){
  192. if((ende & 1) == DES3D) {
  193. key = &expanded_key[2-j][32-2];
  194. keystep = -2;
  195. } else {
  196. key = &expanded_key[j][0];
  197. keystep = 2;
  198. }
  199. ende >>= 1;
  200. for (i = 0; i < 8; i++) {
  201. v0 = key[0];
  202. v0 ^= (right >> 1) | (right << 31);
  203. left ^= fetch(0, v0, 24)
  204. ^ fetch(2, v0, 16)
  205. ^ fetch(4, v0, 8)
  206. ^ fetch(6, v0, 0);
  207. v1 = key[1];
  208. v1 ^= (right << 3) | (right >> 29);
  209. left ^= fetch(1, v1, 24)
  210. ^ fetch(3, v1, 16)
  211. ^ fetch(5, v1, 8)
  212. ^ fetch(7, v1, 0);
  213. key += keystep;
  214. v0 = key[0];
  215. v0 ^= (left >> 1) | (left << 31);
  216. right ^= fetch(0, v0, 24)
  217. ^ fetch(2, v0, 16)
  218. ^ fetch(4, v0, 8)
  219. ^ fetch(6, v0, 0);
  220. v1 = key[1];
  221. v1 ^= (left << 3) | (left >> 29);
  222. right ^= fetch(1, v1, 24)
  223. ^ fetch(3, v1, 16)
  224. ^ fetch(5, v1, 8)
  225. ^ fetch(7, v1, 0);
  226. key += keystep;
  227. }
  228. v0 = left;
  229. left = right;
  230. right = v0;
  231. }
  232. /*
  233. * final permutation, inverse initial permutation
  234. * left and right are swapped here
  235. */
  236. v0 = ((right << 1) & 0xaaaaaaaa) | (left & 0x55555555);
  237. v1 = (right & 0xaaaaaaaa) | ((left >> 1) & 0x55555555);
  238. v1 = ((v1 << 6) & 0x33003300)
  239. | (v1 & 0xcc33cc33)
  240. | ((v1 >> 6) & 0x00cc00cc);
  241. v1 = ((v1 << 12) & 0x0f0f0000)
  242. | (v1 & 0xf0f00f0f)
  243. | ((v1 >> 12) & 0x0000f0f0);
  244. v0 = ((v0 << 6) & 0x33003300)
  245. | (v0 & 0xcc33cc33)
  246. | ((v0 >> 6) & 0x00cc00cc);
  247. v0 = ((v0 << 12) & 0x0f0f0000)
  248. | (v0 & 0xf0f00f0f)
  249. | ((v0 >> 12) & 0x0000f0f0);
  250. text[0] = v0;
  251. text[2] = v0 >> 8;
  252. text[4] = v0 >> 16;
  253. text[6] = v0 >> 24;
  254. text[1] = v1;
  255. text[3] = v1 >> 8;
  256. text[5] = v1 >> 16;
  257. text[7] = v1 >> 24;
  258. }
  259. /*
  260. * key compression permutation, 4 bits at a time
  261. */
  262. static u32int comptab[] = {
  263. 0x000000,0x010000,0x000008,0x010008,0x000080,0x010080,0x000088,0x010088,
  264. 0x000000,0x010000,0x000008,0x010008,0x000080,0x010080,0x000088,0x010088,
  265. 0x000000,0x100000,0x000800,0x100800,0x000000,0x100000,0x000800,0x100800,
  266. 0x002000,0x102000,0x002800,0x102800,0x002000,0x102000,0x002800,0x102800,
  267. 0x000000,0x000004,0x000400,0x000404,0x000000,0x000004,0x000400,0x000404,
  268. 0x400000,0x400004,0x400400,0x400404,0x400000,0x400004,0x400400,0x400404,
  269. 0x000000,0x000020,0x008000,0x008020,0x800000,0x800020,0x808000,0x808020,
  270. 0x000002,0x000022,0x008002,0x008022,0x800002,0x800022,0x808002,0x808022,
  271. 0x000000,0x000200,0x200000,0x200200,0x001000,0x001200,0x201000,0x201200,
  272. 0x000000,0x000200,0x200000,0x200200,0x001000,0x001200,0x201000,0x201200,
  273. 0x000000,0x000040,0x000010,0x000050,0x004000,0x004040,0x004010,0x004050,
  274. 0x040000,0x040040,0x040010,0x040050,0x044000,0x044040,0x044010,0x044050,
  275. 0x000000,0x000100,0x020000,0x020100,0x000001,0x000101,0x020001,0x020101,
  276. 0x080000,0x080100,0x0a0000,0x0a0100,0x080001,0x080101,0x0a0001,0x0a0101,
  277. 0x000000,0x000100,0x040000,0x040100,0x000000,0x000100,0x040000,0x040100,
  278. 0x000040,0x000140,0x040040,0x040140,0x000040,0x000140,0x040040,0x040140,
  279. 0x000000,0x400000,0x008000,0x408000,0x000008,0x400008,0x008008,0x408008,
  280. 0x000400,0x400400,0x008400,0x408400,0x000408,0x400408,0x008408,0x408408,
  281. 0x000000,0x001000,0x080000,0x081000,0x000020,0x001020,0x080020,0x081020,
  282. 0x004000,0x005000,0x084000,0x085000,0x004020,0x005020,0x084020,0x085020,
  283. 0x000000,0x000800,0x000000,0x000800,0x000010,0x000810,0x000010,0x000810,
  284. 0x800000,0x800800,0x800000,0x800800,0x800010,0x800810,0x800010,0x800810,
  285. 0x000000,0x010000,0x000200,0x010200,0x000000,0x010000,0x000200,0x010200,
  286. 0x100000,0x110000,0x100200,0x110200,0x100000,0x110000,0x100200,0x110200,
  287. 0x000000,0x000004,0x000000,0x000004,0x000080,0x000084,0x000080,0x000084,
  288. 0x002000,0x002004,0x002000,0x002004,0x002080,0x002084,0x002080,0x002084,
  289. 0x000000,0x000001,0x200000,0x200001,0x020000,0x020001,0x220000,0x220001,
  290. 0x000002,0x000003,0x200002,0x200003,0x020002,0x020003,0x220002,0x220003,
  291. };
  292. static int keysh[] =
  293. {
  294. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
  295. };
  296. static void
  297. keycompperm(u32int left, u32int right, ulong *ek)
  298. {
  299. u32int v0, v1;
  300. int i;
  301. for(i = 0; i < 16; i++){
  302. left = (left << keysh[i]) | (left >> (28 - keysh[i]));
  303. left &= 0xfffffff0;
  304. right = (right << keysh[i]) | (right >> (28 - keysh[i]));
  305. right &= 0xfffffff0;
  306. v0 = comptab[6 * (1 << 4) + ((left >> (32-4)) & 0xf)]
  307. | comptab[5 * (1 << 4) + ((left >> (32-8)) & 0xf)]
  308. | comptab[4 * (1 << 4) + ((left >> (32-12)) & 0xf)]
  309. | comptab[3 * (1 << 4) + ((left >> (32-16)) & 0xf)]
  310. | comptab[2 * (1 << 4) + ((left >> (32-20)) & 0xf)]
  311. | comptab[1 * (1 << 4) + ((left >> (32-24)) & 0xf)]
  312. | comptab[0 * (1 << 4) + ((left >> (32-28)) & 0xf)];
  313. v1 = comptab[13 * (1 << 4) + ((right >> (32-4)) & 0xf)]
  314. | comptab[12 * (1 << 4) + ((right >> (32-8)) & 0xf)]
  315. | comptab[11 * (1 << 4) + ((right >> (32-12)) & 0xf)]
  316. | comptab[10 * (1 << 4) + ((right >> (32-16)) & 0xf)]
  317. | comptab[9 * (1 << 4) + ((right >> (32-20)) & 0xf)]
  318. | comptab[8 * (1 << 4) + ((right >> (32-24)) & 0xf)]
  319. | comptab[7 * (1 << 4) + ((right >> (32-28)) & 0xf)];
  320. ek[0] = (((v0 >> (24-6)) & 0x3f) << 26)
  321. | (((v0 >> (24-18)) & 0x3f) << 18)
  322. | (((v1 >> (24-6)) & 0x3f) << 10)
  323. | (((v1 >> (24-18)) & 0x3f) << 2);
  324. ek[1] = (((v0 >> (24-12)) & 0x3f) << 26)
  325. | (((v0 >> (24-24)) & 0x3f) << 18)
  326. | (((v1 >> (24-12)) & 0x3f) << 10)
  327. | (((v1 >> (24-24)) & 0x3f) << 2);
  328. ek += 2;
  329. }
  330. }
  331. void
  332. des_key_setup(uchar key[8], ulong *ek)
  333. {
  334. u32int left, right, v0, v1;
  335. v0 = key[0] | ((u32int)key[2] << 8) | ((u32int)key[4] << 16) | ((u32int)key[6] << 24);
  336. v1 = key[1] | ((u32int)key[3] << 8) | ((u32int)key[5] << 16) | ((u32int)key[7] << 24);
  337. left = ((v0 >> 1) & 0x40404040)
  338. | ((v0 >> 2) & 0x10101010)
  339. | ((v0 >> 3) & 0x04040404)
  340. | ((v0 >> 4) & 0x01010101)
  341. | ((v1 >> 0) & 0x80808080)
  342. | ((v1 >> 1) & 0x20202020)
  343. | ((v1 >> 2) & 0x08080808)
  344. | ((v1 >> 3) & 0x02020202);
  345. right = ((v0 >> 1) & 0x04040404)
  346. | ((v0 << 2) & 0x10101010)
  347. | ((v0 << 5) & 0x40404040)
  348. | ((v1 << 0) & 0x08080808)
  349. | ((v1 << 3) & 0x20202020)
  350. | ((v1 << 6) & 0x80808080);
  351. left = ((left << 6) & 0x33003300)
  352. | (left & 0xcc33cc33)
  353. | ((left >> 6) & 0x00cc00cc);
  354. v0 = ((left << 12) & 0x0f0f0000)
  355. | (left & 0xf0f00f0f)
  356. | ((left >> 12) & 0x0000f0f0);
  357. right = ((right << 6) & 0x33003300)
  358. | (right & 0xcc33cc33)
  359. | ((right >> 6) & 0x00cc00cc);
  360. v1 = ((right << 12) & 0x0f0f0000)
  361. | (right & 0xf0f00f0f)
  362. | ((right >> 12) & 0x0000f0f0);
  363. left = v0 & 0xfffffff0;
  364. right = (v1 & 0xffffff00) | ((v0 << 4) & 0xf0);
  365. keycompperm(left, right, ek);
  366. }
  367. static uchar parity[128] =
  368. {
  369. 0x01, 0x02, 0x04, 0x07, 0x08, 0x0b, 0x0d, 0x0e,
  370. 0x10, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c, 0x1f,
  371. 0x20, 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x2f,
  372. 0x31, 0x32, 0x34, 0x37, 0x38, 0x3b, 0x3d, 0x3e,
  373. 0x40, 0x43, 0x45, 0x46, 0x49, 0x4a, 0x4c, 0x4f,
  374. 0x51, 0x52, 0x54, 0x57, 0x58, 0x5b, 0x5d, 0x5e,
  375. 0x61, 0x62, 0x64, 0x67, 0x68, 0x6b, 0x6d, 0x6e,
  376. 0x70, 0x73, 0x75, 0x76, 0x79, 0x7a, 0x7c, 0x7f,
  377. 0x80, 0x83, 0x85, 0x86, 0x89, 0x8a, 0x8c, 0x8f,
  378. 0x91, 0x92, 0x94, 0x97, 0x98, 0x9b, 0x9d, 0x9e,
  379. 0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xab, 0xad, 0xae,
  380. 0xb0, 0xb3, 0xb5, 0xb6, 0xb9, 0xba, 0xbc, 0xbf,
  381. 0xc1, 0xc2, 0xc4, 0xc7, 0xc8, 0xcb, 0xcd, 0xce,
  382. 0xd0, 0xd3, 0xd5, 0xd6, 0xd9, 0xda, 0xdc, 0xdf,
  383. 0xe0, 0xe3, 0xe5, 0xe6, 0xe9, 0xea, 0xec, 0xef,
  384. 0xf1, 0xf2, 0xf4, 0xf7, 0xf8, 0xfb, 0xfd, 0xfe,
  385. };
  386. /*
  387. * convert a 7 byte key to an 8 byte one
  388. */
  389. void
  390. des56to64(uchar *k56, uchar *k64)
  391. {
  392. u32int hi, lo;
  393. hi = ((u32int)k56[0]<<24)|((u32int)k56[1]<<16)|((u32int)k56[2]<<8)|k56[3];
  394. lo = ((u32int)k56[4]<<24)|((u32int)k56[5]<<16)|((u32int)k56[6]<<8);
  395. k64[0] = parity[(hi>>25)&0x7f];
  396. k64[1] = parity[(hi>>18)&0x7f];
  397. k64[2] = parity[(hi>>11)&0x7f];
  398. k64[3] = parity[(hi>>4)&0x7f];
  399. k64[4] = parity[((hi<<3)|(lo>>29))&0x7f];
  400. k64[5] = parity[(lo>>22)&0x7f];
  401. k64[6] = parity[(lo>>15)&0x7f];
  402. k64[7] = parity[(lo>>8)&0x7f];
  403. }
  404. /*
  405. * convert an 8 byte key to a 7 byte one
  406. */
  407. void
  408. des64to56(uchar *k64, uchar *k56)
  409. {
  410. u32int hi, lo;
  411. hi = (((u32int)k64[0]&0xfe)<<24)|(((u32int)k64[1]&0xfe)<<17)|(((u32int)k64[2]&0xfe)<<10)
  412. |((k64[3]&0xfe)<<3)|(k64[4]>>4);
  413. lo = (((u32int)k64[4]&0xfe)<<28)|(((u32int)k64[5]&0xfe)<<21)|(((u32int)k64[6]&0xfe)<<14)
  414. |(((u32int)k64[7]&0xfe)<<7);
  415. k56[0] = hi>>24;
  416. k56[1] = hi>>16;
  417. k56[2] = hi>>8;
  418. k56[3] = hi>>0;
  419. k56[4] = lo>>24;
  420. k56[5] = lo>>16;
  421. k56[6] = lo>>8;
  422. }
  423. void
  424. key_setup(uchar key[7], ulong *ek)
  425. {
  426. uchar k64[8];
  427. des56to64(key, k64);
  428. des_key_setup(k64, ek);
  429. }