des.c 37 KB

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