sshdes.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * sshdes.c: implementation of DES.
  3. */
  4. /*
  5. * Background
  6. * ----------
  7. *
  8. * The basic structure of DES is a Feistel network: the 64-bit cipher
  9. * block is divided into two 32-bit halves L and R, and in each round,
  10. * a mixing function is applied to one of them, the result is XORed
  11. * into the other, and then the halves are swapped so that the other
  12. * one will be the input to the mixing function next time. (This
  13. * structure guarantees reversibility no matter whether the mixing
  14. * function itself is bijective.)
  15. *
  16. * The mixing function for DES goes like this:
  17. * + Extract eight contiguous 6-bit strings from the 32-bit word.
  18. * They start at positions 4 bits apart, so each string overlaps
  19. * the next one by one bit. At least one has to wrap cyclically
  20. * round the end of the word.
  21. * + XOR each of those strings with 6 bits of data from the key
  22. * schedule (which consists of 8 x 6-bit strings per round).
  23. * + Use the resulting 6-bit numbers as the indices into eight
  24. * different lookup tables ('S-boxes'), each of which delivers a
  25. * 4-bit output.
  26. * + Concatenate those eight 4-bit values into a 32-bit word.
  27. * + Finally, apply a fixed permutation P to that word.
  28. *
  29. * DES adds one more wrinkle on top of this structure, which is to
  30. * conjugate it by a bitwise permutation of the cipher block. That is,
  31. * before starting the main cipher rounds, the input bits are permuted
  32. * according to a 64-bit permutation called IP, and after the rounds
  33. * are finished, the output bits are permuted back again by applying
  34. * the inverse of IP.
  35. *
  36. * This gives a lot of leeway to redefine the components of the cipher
  37. * without actually changing the input and output. You could permute
  38. * the bits in the output of any or all of the S-boxes, or reorder the
  39. * S-boxes among themselves, and adjust the following permutation P to
  40. * compensate. And you could adjust IP by post-composing a rotation of
  41. * each 32-bit half, and adjust the starting offsets of the 6-bit
  42. * S-box indices to compensate.
  43. *
  44. * test/desref.py demonstrates this by providing two equivalent forms
  45. * of the cipher, called DES and SGTDES, which give the same output.
  46. * DES is the form described in the original spec: if you make it
  47. * print diagnostic output during the cipher and check it against the
  48. * original, you should recognise the S-box outputs as matching the
  49. * ones you expect. But SGTDES, which I egotistically name after
  50. * myself, is much closer to the form implemented here: I've changed
  51. * the permutation P to suit my implementation strategy and
  52. * compensated by permuting the S-boxes, and also I've added a
  53. * rotation right by 1 bit to IP so that only one S-box index has to
  54. * wrap round the word and also so that the indices are nicely aligned
  55. * for the constant-time selection system I'm using.
  56. */
  57. #include <stdio.h>
  58. #include "ssh.h"
  59. #include "mpint_i.h" /* we reuse the BignumInt system */
  60. /*
  61. * General utility functions.
  62. */
  63. static inline uint32_t rol(uint32_t x, unsigned c)
  64. {
  65. return (x << (31 & c)) | (x >> (31 & -(int/*WINSCP*/)c));
  66. }
  67. static inline uint32_t ror(uint32_t x, unsigned c)
  68. {
  69. return rol(x, -(int/*WINSCP*/)c);
  70. }
  71. /*
  72. * The hard part of doing DES in constant time is the S-box lookup.
  73. *
  74. * My strategy is to iterate over the whole lookup table! That's slow,
  75. * but I don't see any way to avoid _something_ along those lines: in
  76. * every round, every entry in every S-box is potentially needed, and
  77. * if you can't change your memory access pattern based on the input
  78. * data, it follows that you have to read a quantity of information
  79. * equal to the size of all the S-boxes. (Unless they were to turn out
  80. * to be significantly compressible, but I for one couldn't show them
  81. * to be.)
  82. *
  83. * In more detail, I construct a sort of counter-based 'selection
  84. * gadget', which is 15 bits wide and starts off with the top bit
  85. * zero, the next eight bits all 1, and the bottom six set to the
  86. * input S-box index:
  87. *
  88. * 011111111xxxxxx
  89. *
  90. * Now if you add 1 in the lowest bit position, then either it carries
  91. * into the top section (resetting it to 100000000), or it doesn't do
  92. * that yet. If you do that 64 times, then it will _guarantee_ to have
  93. * ticked over into 100000000. In between those increments, the eight
  94. * bits that started off as 11111111 will have stayed that way for
  95. * some number of iterations and then become 00000000, and exactly how
  96. * many iterations depends on the input index.
  97. *
  98. * The purpose of the 0 bit at the top is to absorb the carry when the
  99. * switch happens, which means you can pack more than one gadget into
  100. * the same machine word and have them all work in parallel without
  101. * each one intefering with the next.
  102. *
  103. * The next step is to use each of those 8-bit segments as a bit mask:
  104. * each one is ANDed with a lookup table entry, and all the results
  105. * are XORed together. So you end up with the bitwise XOR of some
  106. * initial segment of the table entries. And the stored S-box tables
  107. * are transformed in such a way that the real S-box values are given
  108. * not by the individual entries, but by the cumulative XORs
  109. * constructed in this way.
  110. *
  111. * A refinement is that I increment each gadget by 2 rather than 1
  112. * each time, so I only iterate 32 times instead of 64. That's why
  113. * there are 8 selection bits instead of 4: each gadget selects enough
  114. * bits to reconstruct _two_ S-box entries, for a pair of indices
  115. * (2n,2n+1), and then finally I use the low bit of the index to do a
  116. * parallel selection between each of those pairs.
  117. *
  118. * The selection gadget is not quite 16 bits wide. So you can fit four
  119. * of them across a 64-bit word at 16-bit intervals, which is also
  120. * convenient because the place the S-box indices are coming from also
  121. * has pairs of them separated by 16-bit distances, so it's easy to
  122. * copy them into the gadgets in the first place.
  123. */
  124. /*
  125. * The S-box data. Each pair of nonzero columns here describes one of
  126. * the S-boxes, corresponding to the SGTDES tables in test/desref.py,
  127. * under the following transformation.
  128. *
  129. * Take S-box #3 as an example. Its values in successive rows of this
  130. * table are eb,e8,54,3d, ... So the cumulative XORs of initial
  131. * sequences of those values are eb,(eb^e8),(eb^e8^54), ... which
  132. * comes to eb,03,57,... Of _those_ values, the top nibble (e,0,5,...)
  133. * gives the even-numbered entries in the S-box, in _reverse_ order
  134. * (because a lower input index selects the XOR of a longer
  135. * subsequence). The odd-numbered entries are given by XORing the two
  136. * digits together: (e^b),(0^3),(5^7),... = 5,3,2,... And indeed, if
  137. * you check SGTDES.sboxes[3] you find it ends ... 52 03 e5.
  138. */
  139. #define SBOX_ITERATION(X) \
  140. /* 66 22 44 00 77 33 55 11 */ \
  141. X(0xf600970083008500, 0x0e00eb007b002e00) \
  142. X(0xda00e4009000e000, 0xad00e800a700b400) \
  143. X(0x1a009d003f003600, 0xf60054004300cd00) \
  144. X(0xaf00c500e900a900, 0x63003d00f2005900) \
  145. X(0xf300750079001400, 0x80005000a2008900) \
  146. X(0xa100d400d6007b00, 0xd3009000d300e100) \
  147. X(0x450087002600ac00, 0xae003c0031009c00) \
  148. X(0xd000b100b6003600, 0x3e006f0092005900) \
  149. X(0x4d008a0026001000, 0x89007a00b8004a00) \
  150. X(0xca00f5003f00ac00, 0x6f00f0003c009400) \
  151. X(0x92008d0090001000, 0x8c00c600ce004a00) \
  152. X(0xe2005900e9006d00, 0x790078007800fa00) \
  153. X(0x1300b10090008d00, 0xa300170027001800) \
  154. X(0xc70058005f006a00, 0x9c00c100e0006300) \
  155. X(0x9b002000f000f000, 0xf70057001600f900) \
  156. X(0xeb00b0009000af00, 0xa9006300b0005800) \
  157. X(0xa2001d00cf000000, 0x3800b00066000000) \
  158. X(0xf100da007900d000, 0xbc00790094007900) \
  159. X(0x570015001900ad00, 0x6f00ef005100cb00) \
  160. X(0xc3006100e9006d00, 0xc000b700f800f200) \
  161. X(0x1d005800b600d000, 0x67004d00cd002c00) \
  162. X(0xf400b800d600e000, 0x5e00a900b000e700) \
  163. X(0x5400d1003f009c00, 0xc90069002c005300) \
  164. X(0xe200e50060005900, 0x6a00b800c500f200) \
  165. X(0xdf0047007900d500, 0x7000ec004c00ea00) \
  166. X(0x7100d10060009c00, 0x3f00b10095005e00) \
  167. X(0x82008200f0002000, 0x87001d00cd008000) \
  168. X(0xd0007000af00c000, 0xe200be006100f200) \
  169. X(0x8000930060001000, 0x36006e0081001200) \
  170. X(0x6500a300d600ac00, 0xcf003d007d00c000) \
  171. X(0x9000700060009800, 0x62008100ad009200) \
  172. X(0xe000e4003f00f400, 0x5a00ed009000f200) \
  173. /* end of list */
  174. /*
  175. * The S-box mapping function. Expects two 32-bit input words: si6420
  176. * contains the table indices for S-boxes 0,2,4,6 with their low bits
  177. * starting at position 2 (for S-box 0) and going up in steps of 8.
  178. * si7531 has indices 1,3,5,7 in the same bit positions.
  179. */
  180. static inline uint32_t des_S(uint32_t si6420, uint32_t si7531)
  181. {
  182. debug("sindices: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  183. 0x3F & (si6420 >> 2), 0x3F & (si7531 >> 2),
  184. 0x3F & (si6420 >> 10), 0x3F & (si7531 >> 10),
  185. 0x3F & (si6420 >> 18), 0x3F & (si7531 >> 18),
  186. 0x3F & (si6420 >> 26), 0x3F & (si7531 >> 26));
  187. #ifdef SIXTY_FOUR_BIT
  188. /*
  189. * On 64-bit machines, we store the table in exactly the form
  190. * shown above, and make two 64-bit words containing four
  191. * selection gadgets each.
  192. */
  193. /* Set up the gadgets. The 'cNNNN' variables will be gradually
  194. * incremented, and the bits in positions FF00FF00FF00FF00 will
  195. * act as selectors for the words in the table.
  196. *
  197. * A side effect of moving the input indices further apart is that
  198. * they change order, because it's easier to keep a pair that were
  199. * originally 16 bits apart still 16 bits apart, which now makes
  200. * them adjacent instead of separated by one. So the fact that
  201. * si6420 turns into c6240 (with the 2,4 reversed) is not a typo!
  202. * This will all be undone when we rebuild the output word later.
  203. */
  204. uint64_t c6240 = ((si6420 | ((uint64_t)si6420 << 24))
  205. & 0x00FC00FC00FC00FC) | 0xFF00FF00FF00FF00;
  206. uint64_t c7351 = ((si7531 | ((uint64_t)si7531 << 24))
  207. & 0x00FC00FC00FC00FC) | 0xFF00FF00FF00FF00;
  208. debug("S in: c6240=%016"PRIx64" c7351=%016"PRIx64"\n", c6240, c7351);
  209. /* Iterate over the table. The 'sNNNN' variables accumulate the
  210. * XOR of all the table entries not masked out. */
  211. static const struct tbl { uint64_t t6240, t7351; } tbl[32] = {
  212. #define TABLE64(a, b) { a, b },
  213. SBOX_ITERATION(TABLE64)
  214. #undef TABLE64
  215. };
  216. uint64_t s6240 = 0, s7351 = 0;
  217. for (const struct tbl *t = tbl, *limit = tbl + 32; t < limit; t++) {
  218. s6240 ^= c6240 & t->t6240; c6240 += 0x0008000800080008;
  219. s7351 ^= c7351 & t->t7351; c7351 += 0x0008000800080008;
  220. }
  221. debug("S out: s6240=%016"PRIx64" s7351=%016"PRIx64"\n", s6240, s7351);
  222. /* Final selection between each even/odd pair: mask off the low
  223. * bits of all the input indices (which haven't changed throughout
  224. * the iteration), and multiply by a bit mask that will turn each
  225. * set bit into a mask covering the upper nibble of the selected
  226. * pair. Then use those masks to control which set of lower
  227. * nibbles is XORed into the upper nibbles. */
  228. s6240 ^= (s6240 << 4) & ((0xf000/0x004) * (c6240 & 0x0004000400040004));
  229. s7351 ^= (s7351 << 4) & ((0xf000/0x004) * (c7351 & 0x0004000400040004));
  230. /* Now the eight final S-box outputs are in the upper nibble of
  231. * each selection position. Mask away the rest of the clutter. */
  232. s6240 &= 0xf000f000f000f000;
  233. s7351 &= 0xf000f000f000f000;
  234. debug("s0=%x s1=%x s2=%x s3=%x s4=%x s5=%x s6=%x s7=%x\n",
  235. (unsigned)(0xF & (s6240 >> 12)),
  236. (unsigned)(0xF & (s7351 >> 12)),
  237. (unsigned)(0xF & (s6240 >> 44)),
  238. (unsigned)(0xF & (s7351 >> 44)),
  239. (unsigned)(0xF & (s6240 >> 28)),
  240. (unsigned)(0xF & (s7351 >> 28)),
  241. (unsigned)(0xF & (s6240 >> 60)),
  242. (unsigned)(0xF & (s7351 >> 60)));
  243. /* Combine them all into a single 32-bit output word, which will
  244. * come out in the order 76543210. */
  245. uint64_t combined = (s6240 >> 12) | (s7351 >> 8);
  246. return combined | (combined >> 24);
  247. #else /* SIXTY_FOUR_BIT */
  248. /*
  249. * For 32-bit platforms, we do the same thing but in four 32-bit
  250. * words instead of two 64-bit ones, so the CPU doesn't have to
  251. * waste time propagating carries or shifted bits between the two
  252. * halves of a uint64 that weren't needed anyway.
  253. */
  254. /* Set up the gadgets */
  255. { // WINSCP
  256. uint32_t c40 = ((si6420 ) & 0x00FC00FC) | 0xFF00FF00;
  257. uint32_t c62 = ((si6420 >> 8) & 0x00FC00FC) | 0xFF00FF00;
  258. uint32_t c51 = ((si7531 ) & 0x00FC00FC) | 0xFF00FF00;
  259. uint32_t c73 = ((si7531 >> 8) & 0x00FC00FC) | 0xFF00FF00;
  260. debug("S in: c40=%08"PRIx32" c62=%08"PRIx32
  261. " c51=%08"PRIx32" c73=%08"PRIx32"\n", c40, c62, c51, c73);
  262. /* Iterate over the table */
  263. { // WINSCP
  264. static const struct tbl { uint32_t t40, t62, t51, t73; } tbl[32] = {
  265. #define TABLE32(a, b) { ((uint32_t)a##LL), (a##LL>>32), ((uint32_t)b##LL), (b##LL>>32) }, // WINSCP
  266. SBOX_ITERATION(TABLE32)
  267. #undef TABLE32
  268. };
  269. uint32_t s40 = 0, s62 = 0, s51 = 0, s73 = 0;
  270. const struct tbl *t, *limit; // WINSCP
  271. for (t = tbl, limit = tbl + 32; t < limit; t++) {
  272. s40 ^= c40 & t->t40; c40 += 0x00080008;
  273. s62 ^= c62 & t->t62; c62 += 0x00080008;
  274. s51 ^= c51 & t->t51; c51 += 0x00080008;
  275. s73 ^= c73 & t->t73; c73 += 0x00080008;
  276. }
  277. debug("S out: s40=%08"PRIx32" s62=%08"PRIx32
  278. " s51=%08"PRIx32" s73=%08"PRIx32"\n", s40, s62, s51, s73);
  279. /* Final selection within each pair */
  280. s40 ^= (s40 << 4) & ((0xf000/0x004) * (c40 & 0x00040004));
  281. s62 ^= (s62 << 4) & ((0xf000/0x004) * (c62 & 0x00040004));
  282. s51 ^= (s51 << 4) & ((0xf000/0x004) * (c51 & 0x00040004));
  283. s73 ^= (s73 << 4) & ((0xf000/0x004) * (c73 & 0x00040004));
  284. /* Clean up the clutter */
  285. s40 &= 0xf000f000;
  286. s62 &= 0xf000f000;
  287. s51 &= 0xf000f000;
  288. s73 &= 0xf000f000;
  289. debug("s0=%x s1=%x s2=%x s3=%x s4=%x s5=%x s6=%x s7=%x\n",
  290. (unsigned)(0xF & (s40 >> 12)),
  291. (unsigned)(0xF & (s51 >> 12)),
  292. (unsigned)(0xF & (s62 >> 12)),
  293. (unsigned)(0xF & (s73 >> 12)),
  294. (unsigned)(0xF & (s40 >> 28)),
  295. (unsigned)(0xF & (s51 >> 28)),
  296. (unsigned)(0xF & (s62 >> 28)),
  297. (unsigned)(0xF & (s73 >> 28)));
  298. /* Recombine and return */
  299. return (s40 >> 12) | (s62 >> 4) | (s51 >> 8) | (s73);
  300. } // WINSCP
  301. } // WINSCP
  302. #endif /* SIXTY_FOUR_BIT */
  303. }
  304. /*
  305. * Now for the permutation P. The basic strategy here is to use a
  306. * Benes network: in each stage, the bit at position i is allowed to
  307. * either stay where it is or swap with i ^ D, where D is a power of 2
  308. * that varies with each phase. (So when D=1, pairs of the form
  309. * {2n,2n+1} can swap; when D=2, the pairs are {4n+j,4n+j+2} for
  310. * j={0,1}, and so on.)
  311. *
  312. * You can recursively construct a Benes network for an arbitrary
  313. * permutation, in which the values of D iterate across all the powers
  314. * of 2 less than the permutation size and then go back again. For
  315. * example, the typical presentation for 32 bits would have D iterate
  316. * over 16,8,4,2,1,2,4,8,16, and there's an easy algorithm that can
  317. * express any permutation in that form by deciding which pairs of
  318. * bits to swap in the outer pair of stages and then recursing to do
  319. * all the stages in between.
  320. *
  321. * Actually implementing the swaps is easy when they're all between
  322. * bits at the same separation: make the value x ^ (x >> D), mask out
  323. * just the bits in the low position of a pair that needs to swap, and
  324. * then use the resulting value y to make x ^ y ^ (y << D) which is
  325. * the swapped version.
  326. *
  327. * In this particular case, I processed the bit indices in the other
  328. * order (going 1,2,4,8,16,8,4,2,1), which makes no significant
  329. * difference to the construction algorithm (it's just a relabelling),
  330. * but it now means that the first two steps only permute entries
  331. * within the output of each S-box - and therefore we can leave them
  332. * completely out, in favour of just defining the S-boxes so that
  333. * those permutation steps are already applied. Furthermore, by
  334. * exhaustive search over the rest of the possible bit-orders for each
  335. * S-box, I was able to find a version of P which could be represented
  336. * in such a way that two further phases had all their control bits
  337. * zero and could be skipped. So the number of swap stages is reduced
  338. * to 5 from the 9 that might have been needed.
  339. */
  340. static inline uint32_t des_benes_step(uint32_t v, unsigned D, uint32_t mask)
  341. {
  342. uint32_t diff = (v ^ (v >> D)) & mask;
  343. return v ^ diff ^ (diff << D);
  344. }
  345. static inline uint32_t des_P(uint32_t v_orig)
  346. {
  347. uint32_t v = v_orig;
  348. /* initial stages with distance 1,2 are part of the S-box data table */
  349. v = des_benes_step(v, 4, 0x07030702);
  350. v = des_benes_step(v, 8, 0x004E009E);
  351. v = des_benes_step(v, 16, 0x0000D9D3);
  352. /* v = des_benes_step(v, 8, 0x00000000); no-op, so we can skip it */
  353. v = des_benes_step(v, 4, 0x05040004);
  354. /* v = des_benes_step(v, 2, 0x00000000); no-op, so we can skip it */
  355. v = des_benes_step(v, 1, 0x04045015);
  356. debug("P(%08"PRIx32") = %08"PRIx32"\n", v_orig, v);
  357. return v;
  358. }
  359. /*
  360. * Putting the S and P functions together, and adding in the round key
  361. * as well, gives us the full mixing function f.
  362. */
  363. static inline uint32_t des_f(uint32_t R, uint32_t K7531, uint32_t K6420)
  364. {
  365. uint32_t s7531 = R ^ K7531, s6420 = rol(R, 4) ^ K6420;
  366. return des_P(des_S(s6420, s7531));
  367. }
  368. /*
  369. * The key schedule, and the function to set it up.
  370. */
  371. typedef struct des_keysched des_keysched;
  372. struct des_keysched {
  373. uint32_t k7531[16], k6420[16];
  374. };
  375. /*
  376. * Simplistic function to select an arbitrary sequence of bits from
  377. * one value and glue them together into another value. bitnums[]
  378. * gives the sequence of bit indices of the input, from the highest
  379. * output bit downwards. An index of -1 means that output bit is left
  380. * at zero.
  381. *
  382. * This function is only used during key setup, so it doesn't need to
  383. * be highly optimised.
  384. */
  385. static inline uint64_t bitsel(
  386. uint64_t input, const int8_t *bitnums, size_t size)
  387. {
  388. uint64_t ret = 0;
  389. while (size-- > 0) {
  390. int bitpos = *bitnums++;
  391. ret <<= 1;
  392. if (bitpos >= 0)
  393. ret |= 1 & (input >> bitpos);
  394. }
  395. return ret;
  396. }
  397. void des_key_setup(uint64_t key, des_keysched *sched)
  398. {
  399. static const int8_t PC1[] = {
  400. 7, 15, 23, 31, 39, 47, 55, 63, 6, 14, 22, 30, 38, 46,
  401. 54, 62, 5, 13, 21, 29, 37, 45, 53, 61, 4, 12, 20, 28,
  402. -1, -1, -1, -1,
  403. 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34, 42,
  404. 50, 58, 3, 11, 19, 27, 35, 43, 51, 59, 36, 44, 52, 60,
  405. };
  406. static const int8_t PC2_7531[] = {
  407. 46, 43, 49, 36, 59, 55, -1, -1, /* index into S-box 7 */
  408. 37, 41, 48, 56, 34, 52, -1, -1, /* index into S-box 5 */
  409. 15, 4, 25, 19, 9, 1, -1, -1, /* index into S-box 3 */
  410. 12, 7, 17, 0, 22, 3, -1, -1, /* index into S-box 1 */
  411. };
  412. static const int8_t PC2_6420[] = {
  413. 57, 32, 45, 54, 39, 50, -1, -1, /* index into S-box 6 */
  414. 44, 53, 33, 40, 47, 58, -1, -1, /* index into S-box 4 */
  415. 26, 16, 5, 11, 23, 8, -1, -1, /* index into S-box 2 */
  416. 10, 14, 6, 20, 27, 24, -1, -1, /* index into S-box 0 */
  417. };
  418. static const int leftshifts[] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
  419. /* Select 56 bits from the 64-bit input key integer (the low bit
  420. * of each input byte is unused), into a word consisting of two
  421. * 28-bit integers starting at bits 0 and 32. */
  422. uint64_t CD = bitsel(key, PC1, lenof(PC1));
  423. { // WINSCP
  424. size_t i; // WINSCP
  425. for (i = 0; i < 16; i++) {
  426. /* Rotate each 28-bit half of CD left by 1 or 2 bits (varying
  427. * between rounds) */
  428. CD <<= leftshifts[i];
  429. CD = (CD & 0x0FFFFFFF0FFFFFFFLL) | ((CD & 0xF0000000F0000000LL) >> 28); // WINSCP
  430. /* Select key bits from the rotated word to use during the
  431. * actual cipher */
  432. sched->k7531[i] = bitsel(CD, PC2_7531, lenof(PC2_7531));
  433. sched->k6420[i] = bitsel(CD, PC2_6420, lenof(PC2_6420));
  434. }
  435. } // WINSCP
  436. }
  437. /*
  438. * Helper routines for dealing with 64-bit blocks in the form of an L
  439. * and R word.
  440. */
  441. typedef struct LR LR;
  442. struct LR { uint32_t L, R; };
  443. static inline LR des_load_lr(const void *vp)
  444. {
  445. const uint8_t *p = (const uint8_t *)vp;
  446. LR out;
  447. out.L = GET_32BIT_MSB_FIRST(p);
  448. out.R = GET_32BIT_MSB_FIRST(p+4);
  449. return out;
  450. }
  451. static inline void des_store_lr(void *vp, LR lr)
  452. {
  453. uint8_t *p = (uint8_t *)vp;
  454. PUT_32BIT_MSB_FIRST(p, lr.L);
  455. PUT_32BIT_MSB_FIRST(p+4, lr.R);
  456. }
  457. static inline LR des_xor_lr(LR a, LR b)
  458. {
  459. a.L ^= b.L;
  460. a.R ^= b.R;
  461. return a;
  462. }
  463. static inline LR des_swap_lr(LR in)
  464. {
  465. LR out;
  466. out.L = in.R;
  467. out.R = in.L;
  468. return out;
  469. }
  470. /*
  471. * The initial and final permutations of official DES are in a
  472. * restricted form, in which the 'before' and 'after' positions of a
  473. * given data bit are derived from each other by permuting the bits of
  474. * the _index_ and flipping some of them. This allows the permutation
  475. * to be performed effectively by a method that looks rather like
  476. * _half_ of a general Benes network, because the restricted form
  477. * means only half of it is actually needed.
  478. *
  479. * _Our_ initial and final permutations include a rotation by 1 bit,
  480. * but it's still easier to just suffix that to the standard IP/FP
  481. * than to regenerate everything using a more general method.
  482. *
  483. * Because we're permuting 64 bits in this case, between two 32-bit
  484. * words, there's a separate helper function for this code that
  485. * doesn't look quite like des_benes_step() above.
  486. */
  487. static inline void des_bitswap_IP_FP(uint32_t *L, uint32_t *R,
  488. unsigned D, uint32_t mask)
  489. {
  490. uint32_t diff = mask & ((*R >> D) ^ *L);
  491. *R ^= diff << D;
  492. *L ^= diff;
  493. }
  494. static inline LR des_IP(LR lr)
  495. {
  496. des_bitswap_IP_FP(&lr.R, &lr.L, 4, 0x0F0F0F0F);
  497. des_bitswap_IP_FP(&lr.R, &lr.L, 16, 0x0000FFFF);
  498. des_bitswap_IP_FP(&lr.L, &lr.R, 2, 0x33333333);
  499. des_bitswap_IP_FP(&lr.L, &lr.R, 8, 0x00FF00FF);
  500. des_bitswap_IP_FP(&lr.R, &lr.L, 1, 0x55555555);
  501. lr.L = ror(lr.L, 1);
  502. lr.R = ror(lr.R, 1);
  503. return lr;
  504. }
  505. static inline LR des_FP(LR lr)
  506. {
  507. lr.L = rol(lr.L, 1);
  508. lr.R = rol(lr.R, 1);
  509. des_bitswap_IP_FP(&lr.R, &lr.L, 1, 0x55555555);
  510. des_bitswap_IP_FP(&lr.L, &lr.R, 8, 0x00FF00FF);
  511. des_bitswap_IP_FP(&lr.L, &lr.R, 2, 0x33333333);
  512. des_bitswap_IP_FP(&lr.R, &lr.L, 16, 0x0000FFFF);
  513. des_bitswap_IP_FP(&lr.R, &lr.L, 4, 0x0F0F0F0F);
  514. return lr;
  515. }
  516. /*
  517. * The main cipher functions, which are identical except that they use
  518. * the key schedule in opposite orders.
  519. *
  520. * We provide a version without the initial and final permutations,
  521. * for use in triple-DES mode (no sense undoing and redoing it in
  522. * between the phases).
  523. */
  524. static inline LR des_round(LR in, const des_keysched *sched, size_t round)
  525. {
  526. LR out;
  527. out.L = in.R;
  528. out.R = in.L ^ des_f(in.R, sched->k7531[round], sched->k6420[round]);
  529. return out;
  530. }
  531. static inline LR des_inner_cipher(LR lr, const des_keysched *sched,
  532. size_t start, size_t step)
  533. {
  534. lr = des_round(lr, sched, start+0x0*step);
  535. lr = des_round(lr, sched, start+0x1*step);
  536. lr = des_round(lr, sched, start+0x2*step);
  537. lr = des_round(lr, sched, start+0x3*step);
  538. lr = des_round(lr, sched, start+0x4*step);
  539. lr = des_round(lr, sched, start+0x5*step);
  540. lr = des_round(lr, sched, start+0x6*step);
  541. lr = des_round(lr, sched, start+0x7*step);
  542. lr = des_round(lr, sched, start+0x8*step);
  543. lr = des_round(lr, sched, start+0x9*step);
  544. lr = des_round(lr, sched, start+0xa*step);
  545. lr = des_round(lr, sched, start+0xb*step);
  546. lr = des_round(lr, sched, start+0xc*step);
  547. lr = des_round(lr, sched, start+0xd*step);
  548. lr = des_round(lr, sched, start+0xe*step);
  549. lr = des_round(lr, sched, start+0xf*step);
  550. return des_swap_lr(lr);
  551. }
  552. static inline LR des_full_cipher(LR lr, const des_keysched *sched,
  553. size_t start, size_t step)
  554. {
  555. lr = des_IP(lr);
  556. lr = des_inner_cipher(lr, sched, start, step);
  557. lr = des_FP(lr);
  558. return lr;
  559. }
  560. /*
  561. * Parameter pairs for the start,step arguments to the cipher routines
  562. * above, causing them to use the same key schedule in opposite orders.
  563. */
  564. #define ENCIPHER 0, 1 /* for encryption */
  565. #define DECIPHER 15, -1 /* for decryption */
  566. /* ----------------------------------------------------------------------
  567. * Single-DES
  568. */
  569. struct des_cbc_ctx {
  570. des_keysched sched;
  571. LR iv;
  572. ssh_cipher ciph;
  573. };
  574. static ssh_cipher *des_cbc_new(const ssh_cipheralg *alg)
  575. {
  576. struct des_cbc_ctx *ctx = snew(struct des_cbc_ctx);
  577. ctx->ciph.vt = alg;
  578. return &ctx->ciph;
  579. }
  580. static void des_cbc_free(ssh_cipher *ciph)
  581. {
  582. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  583. smemclr(ctx, sizeof(*ctx));
  584. sfree(ctx);
  585. }
  586. static void des_cbc_setkey(ssh_cipher *ciph, const void *vkey)
  587. {
  588. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  589. const uint8_t *key = (const uint8_t *)vkey;
  590. des_key_setup(GET_64BIT_MSB_FIRST(key), &ctx->sched);
  591. }
  592. static void des_cbc_setiv(ssh_cipher *ciph, const void *iv)
  593. {
  594. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  595. ctx->iv = des_load_lr(iv);
  596. }
  597. static void des_cbc_encrypt(ssh_cipher *ciph, void *vdata, int len)
  598. {
  599. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  600. uint8_t *data = (uint8_t *)vdata;
  601. for (; len > 0; len -= 8, data += 8) {
  602. LR plaintext = des_load_lr(data);
  603. LR cipher_in = des_xor_lr(plaintext, ctx->iv);
  604. LR ciphertext = des_full_cipher(cipher_in, &ctx->sched, ENCIPHER);
  605. des_store_lr(data, ciphertext);
  606. ctx->iv = ciphertext;
  607. }
  608. }
  609. static void des_cbc_decrypt(ssh_cipher *ciph, void *vdata, int len)
  610. {
  611. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  612. uint8_t *data = (uint8_t *)vdata;
  613. for (; len > 0; len -= 8, data += 8) {
  614. LR ciphertext = des_load_lr(data);
  615. LR cipher_out = des_full_cipher(ciphertext, &ctx->sched, DECIPHER);
  616. LR plaintext = des_xor_lr(cipher_out, ctx->iv);
  617. des_store_lr(data, plaintext);
  618. ctx->iv = ciphertext;
  619. }
  620. }
  621. const ssh_cipheralg ssh_des = {
  622. des_cbc_new, des_cbc_free, des_cbc_setiv, des_cbc_setkey,
  623. des_cbc_encrypt, des_cbc_decrypt, NULL, NULL, "des-cbc",
  624. 8, 56, 8, SSH_CIPHER_IS_CBC, "single-DES CBC", NULL
  625. };
  626. const ssh_cipheralg ssh_des_sshcom_ssh2 = {
  627. /* Same as ssh_des_cbc, but with a different SSH-2 ID */
  628. des_cbc_new, des_cbc_free, des_cbc_setiv, des_cbc_setkey,
  629. des_cbc_encrypt, des_cbc_decrypt, NULL, NULL, "[email protected]",
  630. 8, 56, 8, SSH_CIPHER_IS_CBC, "single-DES CBC", NULL
  631. };
  632. static const ssh_cipheralg *const des_list[] = {
  633. &ssh_des,
  634. &ssh_des_sshcom_ssh2
  635. };
  636. const ssh2_ciphers ssh2_des = { lenof(des_list), des_list };
  637. /* ----------------------------------------------------------------------
  638. * Triple-DES CBC, SSH-2 style. The CBC mode treats the three
  639. * invocations of DES as a single unified cipher, and surrounds it
  640. * with just one layer of CBC, so only one IV is needed.
  641. */
  642. struct des3_cbc1_ctx {
  643. des_keysched sched[3];
  644. LR iv;
  645. ssh_cipher ciph;
  646. };
  647. static ssh_cipher *des3_cbc1_new(const ssh_cipheralg *alg)
  648. {
  649. struct des3_cbc1_ctx *ctx = snew(struct des3_cbc1_ctx);
  650. ctx->ciph.vt = alg;
  651. return &ctx->ciph;
  652. }
  653. static void des3_cbc1_free(ssh_cipher *ciph)
  654. {
  655. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  656. smemclr(ctx, sizeof(*ctx));
  657. sfree(ctx);
  658. }
  659. static void des3_cbc1_setkey(ssh_cipher *ciph, const void *vkey)
  660. {
  661. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  662. const uint8_t *key = (const uint8_t *)vkey;
  663. size_t i; // WINSCP
  664. for (i = 0; i < 3; i++)
  665. des_key_setup(GET_64BIT_MSB_FIRST(key + 8*i), &ctx->sched[i]);
  666. }
  667. static void des3_cbc1_setiv(ssh_cipher *ciph, const void *iv)
  668. {
  669. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  670. ctx->iv = des_load_lr(iv);
  671. }
  672. static void des3_cbc1_cbc_encrypt(ssh_cipher *ciph, void *vdata, int len)
  673. {
  674. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  675. uint8_t *data = (uint8_t *)vdata;
  676. for (; len > 0; len -= 8, data += 8) {
  677. LR plaintext = des_load_lr(data);
  678. LR cipher_in = des_xor_lr(plaintext, ctx->iv);
  679. /* Run three copies of the cipher, without undoing and redoing
  680. * IP/FP in between. */
  681. LR lr = des_IP(cipher_in);
  682. lr = des_inner_cipher(lr, &ctx->sched[0], ENCIPHER);
  683. lr = des_inner_cipher(lr, &ctx->sched[1], DECIPHER);
  684. lr = des_inner_cipher(lr, &ctx->sched[2], ENCIPHER);
  685. { // WINSCP
  686. LR ciphertext = des_FP(lr);
  687. des_store_lr(data, ciphertext);
  688. ctx->iv = ciphertext;
  689. } // WINSCP
  690. }
  691. }
  692. static void des3_cbc1_cbc_decrypt(ssh_cipher *ciph, void *vdata, int len)
  693. {
  694. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  695. uint8_t *data = (uint8_t *)vdata;
  696. for (; len > 0; len -= 8, data += 8) {
  697. LR ciphertext = des_load_lr(data);
  698. /* Similarly to encryption, but with the order reversed. */
  699. LR lr = des_IP(ciphertext);
  700. lr = des_inner_cipher(lr, &ctx->sched[2], DECIPHER);
  701. lr = des_inner_cipher(lr, &ctx->sched[1], ENCIPHER);
  702. lr = des_inner_cipher(lr, &ctx->sched[0], DECIPHER);
  703. { // WINSCP
  704. LR cipher_out = des_FP(lr);
  705. LR plaintext = des_xor_lr(cipher_out, ctx->iv);
  706. des_store_lr(data, plaintext);
  707. ctx->iv = ciphertext;
  708. } // WINSCP
  709. }
  710. }
  711. const ssh_cipheralg ssh_3des_ssh2 = {
  712. des3_cbc1_new, des3_cbc1_free, des3_cbc1_setiv, des3_cbc1_setkey,
  713. des3_cbc1_cbc_encrypt, des3_cbc1_cbc_decrypt, NULL, NULL, "3des-cbc",
  714. 8, 168, 24, SSH_CIPHER_IS_CBC, "triple-DES CBC", NULL
  715. };
  716. /* ----------------------------------------------------------------------
  717. * Triple-DES in SDCTR mode. Again, the three DES instances are
  718. * treated as one big cipher, with a single counter encrypted through
  719. * all three.
  720. */
  721. #define SDCTR_WORDS (8 / BIGNUM_INT_BYTES)
  722. struct des3_sdctr_ctx {
  723. des_keysched sched[3];
  724. BignumInt counter[SDCTR_WORDS];
  725. ssh_cipher ciph;
  726. };
  727. static ssh_cipher *des3_sdctr_new(const ssh_cipheralg *alg)
  728. {
  729. struct des3_sdctr_ctx *ctx = snew(struct des3_sdctr_ctx);
  730. ctx->ciph.vt = alg;
  731. return &ctx->ciph;
  732. }
  733. static void des3_sdctr_free(ssh_cipher *ciph)
  734. {
  735. struct des3_sdctr_ctx *ctx = container_of(
  736. ciph, struct des3_sdctr_ctx, ciph);
  737. smemclr(ctx, sizeof(*ctx));
  738. sfree(ctx);
  739. }
  740. static void des3_sdctr_setkey(ssh_cipher *ciph, const void *vkey)
  741. {
  742. struct des3_sdctr_ctx *ctx = container_of(
  743. ciph, struct des3_sdctr_ctx, ciph);
  744. const uint8_t *key = (const uint8_t *)vkey;
  745. size_t i; // WINSCP
  746. for (i = 0; i < 3; i++)
  747. des_key_setup(GET_64BIT_MSB_FIRST(key + 8*i), &ctx->sched[i]);
  748. }
  749. static void des3_sdctr_setiv(ssh_cipher *ciph, const void *viv)
  750. {
  751. struct des3_sdctr_ctx *ctx = container_of(
  752. ciph, struct des3_sdctr_ctx, ciph);
  753. const uint8_t *iv = (const uint8_t *)viv;
  754. /* Import the initial counter value into the internal representation */
  755. unsigned i = 0; // WINSCP
  756. for (i = 0; i < SDCTR_WORDS; i++)
  757. ctx->counter[i] = GET_BIGNUMINT_MSB_FIRST(
  758. iv + 8 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES);
  759. }
  760. static void des3_sdctr_encrypt_decrypt(ssh_cipher *ciph, void *vdata, int len)
  761. {
  762. struct des3_sdctr_ctx *ctx = container_of(
  763. ciph, struct des3_sdctr_ctx, ciph);
  764. uint8_t *data = (uint8_t *)vdata;
  765. uint8_t iv_buf[8];
  766. for (; len > 0; len -= 8, data += 8) {
  767. /* Format the counter value into the buffer. */
  768. unsigned i; // WINSCP
  769. for (i = 0; i < SDCTR_WORDS; i++)
  770. PUT_BIGNUMINT_MSB_FIRST(
  771. iv_buf + 8 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES,
  772. ctx->counter[i]);
  773. /* Increment the counter. */
  774. { // WINSCP
  775. BignumCarry carry = 1;
  776. for (i = 0; i < SDCTR_WORDS; i++) // WINSCP
  777. BignumADC(ctx->counter[i], carry, ctx->counter[i], 0, carry);
  778. /* Triple-encrypt the counter value from the IV. */
  779. { // WINSCP
  780. LR lr = des_IP(des_load_lr(iv_buf));
  781. lr = des_inner_cipher(lr, &ctx->sched[0], ENCIPHER);
  782. lr = des_inner_cipher(lr, &ctx->sched[1], DECIPHER);
  783. lr = des_inner_cipher(lr, &ctx->sched[2], ENCIPHER);
  784. { // WINSCP
  785. LR keystream = des_FP(lr);
  786. LR input = des_load_lr(data);
  787. LR output = des_xor_lr(input, keystream);
  788. des_store_lr(data, output);
  789. } // WINSCP
  790. } // WINSCP
  791. } // WINSCP
  792. }
  793. smemclr(iv_buf, sizeof(iv_buf));
  794. }
  795. const ssh_cipheralg ssh_3des_ssh2_ctr = {
  796. des3_sdctr_new, des3_sdctr_free, des3_sdctr_setiv, des3_sdctr_setkey,
  797. des3_sdctr_encrypt_decrypt, des3_sdctr_encrypt_decrypt,
  798. NULL, NULL, "3des-ctr", 8, 168, 24, 0, "triple-DES SDCTR", NULL
  799. };
  800. static const ssh_cipheralg *const des3_list[] = {
  801. &ssh_3des_ssh2_ctr,
  802. &ssh_3des_ssh2
  803. };
  804. const ssh2_ciphers ssh2_3des = { lenof(des3_list), des3_list };
  805. /* ----------------------------------------------------------------------
  806. * Triple-DES, SSH-1 style. SSH-1 replicated the whole CBC structure
  807. * three times, so there have to be three separate IVs, one in each
  808. * layer.
  809. */
  810. struct des3_cbc3_ctx {
  811. des_keysched sched[3];
  812. LR iv[3];
  813. ssh_cipher ciph;
  814. };
  815. static ssh_cipher *des3_cbc3_new(const ssh_cipheralg *alg)
  816. {
  817. struct des3_cbc3_ctx *ctx = snew(struct des3_cbc3_ctx);
  818. ctx->ciph.vt = alg;
  819. return &ctx->ciph;
  820. }
  821. static void des3_cbc3_free(ssh_cipher *ciph)
  822. {
  823. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  824. smemclr(ctx, sizeof(*ctx));
  825. sfree(ctx);
  826. }
  827. static void des3_cbc3_setkey(ssh_cipher *ciph, const void *vkey)
  828. {
  829. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  830. const uint8_t *key = (const uint8_t *)vkey;
  831. size_t i; // WINSCP
  832. for (i = 0; i < 3; i++)
  833. des_key_setup(GET_64BIT_MSB_FIRST(key + 8*i), &ctx->sched[i]);
  834. }
  835. static void des3_cbc3_setiv(ssh_cipher *ciph, const void *viv)
  836. {
  837. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  838. /*
  839. * In principle, we ought to provide an interface for the user to
  840. * input 24 instead of 8 bytes of IV. But that would make this an
  841. * ugly exception to the otherwise universal rule that IV size =
  842. * cipher block size, and there's really no need to violate that
  843. * rule given that this is a historical one-off oddity and SSH-1
  844. * always initialises all three IVs to zero anyway. So we fudge it
  845. * by just setting all the IVs to the same value.
  846. */
  847. LR iv = des_load_lr(viv);
  848. /* But we store the IVs in permuted form, so that we can handle
  849. * all three CBC layers without having to do IP/FP in between. */
  850. iv = des_IP(iv);
  851. { // WINSCP
  852. size_t i; // WINSCP
  853. for (i = 0; i < 3; i++)
  854. ctx->iv[i] = iv;
  855. } // WINSCP
  856. }
  857. static void des3_cbc3_cbc_encrypt(ssh_cipher *ciph, void *vdata, int len)
  858. {
  859. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  860. uint8_t *data = (uint8_t *)vdata;
  861. for (; len > 0; len -= 8, data += 8) {
  862. /* Load and IP the input. */
  863. LR plaintext = des_IP(des_load_lr(data));
  864. LR lr = plaintext;
  865. /* Do three passes of CBC, with the middle one inverted. */
  866. lr = des_xor_lr(lr, ctx->iv[0]);
  867. lr = des_inner_cipher(lr, &ctx->sched[0], ENCIPHER);
  868. ctx->iv[0] = lr;
  869. { // WINSCP
  870. LR ciphertext = lr;
  871. lr = des_inner_cipher(ciphertext, &ctx->sched[1], DECIPHER);
  872. lr = des_xor_lr(lr, ctx->iv[1]);
  873. ctx->iv[1] = ciphertext;
  874. lr = des_xor_lr(lr, ctx->iv[2]);
  875. lr = des_inner_cipher(lr, &ctx->sched[2], ENCIPHER);
  876. ctx->iv[2] = lr;
  877. des_store_lr(data, des_FP(lr));
  878. } // WINSCP
  879. }
  880. }
  881. static void des3_cbc3_cbc_decrypt(ssh_cipher *ciph, void *vdata, int len)
  882. {
  883. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  884. uint8_t *data = (uint8_t *)vdata;
  885. for (; len > 0; len -= 8, data += 8) {
  886. /* Load and IP the input */
  887. LR lr = des_IP(des_load_lr(data));
  888. LR ciphertext;
  889. /* Do three passes of CBC, with the middle one inverted. */
  890. ciphertext = lr;
  891. lr = des_inner_cipher(ciphertext, &ctx->sched[2], DECIPHER);
  892. lr = des_xor_lr(lr, ctx->iv[2]);
  893. ctx->iv[2] = ciphertext;
  894. lr = des_xor_lr(lr, ctx->iv[1]);
  895. lr = des_inner_cipher(lr, &ctx->sched[1], ENCIPHER);
  896. ctx->iv[1] = lr;
  897. ciphertext = lr;
  898. lr = des_inner_cipher(ciphertext, &ctx->sched[0], DECIPHER);
  899. lr = des_xor_lr(lr, ctx->iv[0]);
  900. ctx->iv[0] = ciphertext;
  901. des_store_lr(data, des_FP(lr));
  902. }
  903. }
  904. const ssh_cipheralg ssh_3des_ssh1 = {
  905. des3_cbc3_new, des3_cbc3_free, des3_cbc3_setiv, des3_cbc3_setkey,
  906. des3_cbc3_cbc_encrypt, des3_cbc3_cbc_decrypt, NULL, NULL, NULL,
  907. 8, 168, 24, SSH_CIPHER_IS_CBC, "triple-DES inner-CBC", NULL
  908. };