des.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 & -(int/*WINSCP*/)c));
  73. }
  74. static inline uint32_t ror(uint32_t x, unsigned c)
  75. {
  76. return rol(x, -(int/*WINSCP*/)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. { // WINSCP
  263. uint32_t c40 = ((si6420 ) & 0x00FC00FC) | 0xFF00FF00;
  264. uint32_t c62 = ((si6420 >> 8) & 0x00FC00FC) | 0xFF00FF00;
  265. uint32_t c51 = ((si7531 ) & 0x00FC00FC) | 0xFF00FF00;
  266. uint32_t c73 = ((si7531 >> 8) & 0x00FC00FC) | 0xFF00FF00;
  267. debug("S in: c40=%08"PRIx32" c62=%08"PRIx32
  268. " c51=%08"PRIx32" c73=%08"PRIx32"\n", c40, c62, c51, c73);
  269. /* Iterate over the table */
  270. { // WINSCP
  271. static const struct tbl { uint32_t t40, t62, t51, t73; } tbl[32] = {
  272. #define TABLE32(a, b) { ((uint32_t)a##LL), (a##LL>>32), ((uint32_t)b##LL), (b##LL>>32) }, // WINSCP
  273. SBOX_ITERATION(TABLE32)
  274. #undef TABLE32
  275. };
  276. uint32_t s40 = 0, s62 = 0, s51 = 0, s73 = 0;
  277. const struct tbl *t, *limit; // WINSCP
  278. for (t = tbl, limit = tbl + 32; t < limit; t++) {
  279. s40 ^= c40 & t->t40; c40 += 0x00080008;
  280. s62 ^= c62 & t->t62; c62 += 0x00080008;
  281. s51 ^= c51 & t->t51; c51 += 0x00080008;
  282. s73 ^= c73 & t->t73; c73 += 0x00080008;
  283. }
  284. debug("S out: s40=%08"PRIx32" s62=%08"PRIx32
  285. " s51=%08"PRIx32" s73=%08"PRIx32"\n", s40, s62, s51, s73);
  286. /* Final selection within each pair */
  287. s40 ^= (s40 << 4) & ((0xf000/0x004) * (c40 & 0x00040004));
  288. s62 ^= (s62 << 4) & ((0xf000/0x004) * (c62 & 0x00040004));
  289. s51 ^= (s51 << 4) & ((0xf000/0x004) * (c51 & 0x00040004));
  290. s73 ^= (s73 << 4) & ((0xf000/0x004) * (c73 & 0x00040004));
  291. /* Clean up the clutter */
  292. s40 &= 0xf000f000;
  293. s62 &= 0xf000f000;
  294. s51 &= 0xf000f000;
  295. s73 &= 0xf000f000;
  296. debug("s0=%x s1=%x s2=%x s3=%x s4=%x s5=%x s6=%x s7=%x\n",
  297. (unsigned)(0xF & (s40 >> 12)),
  298. (unsigned)(0xF & (s51 >> 12)),
  299. (unsigned)(0xF & (s62 >> 12)),
  300. (unsigned)(0xF & (s73 >> 12)),
  301. (unsigned)(0xF & (s40 >> 28)),
  302. (unsigned)(0xF & (s51 >> 28)),
  303. (unsigned)(0xF & (s62 >> 28)),
  304. (unsigned)(0xF & (s73 >> 28)));
  305. /* Recombine and return */
  306. return (s40 >> 12) | (s62 >> 4) | (s51 >> 8) | (s73);
  307. } // WINSCP
  308. } // WINSCP
  309. #endif /* SIXTY_FOUR_BIT */
  310. }
  311. /*
  312. * Now for the permutation P. The basic strategy here is to use a
  313. * Benes network: in each stage, the bit at position i is allowed to
  314. * either stay where it is or swap with i ^ D, where D is a power of 2
  315. * that varies with each phase. (So when D=1, pairs of the form
  316. * {2n,2n+1} can swap; when D=2, the pairs are {4n+j,4n+j+2} for
  317. * j={0,1}, and so on.)
  318. *
  319. * You can recursively construct a Benes network for an arbitrary
  320. * permutation, in which the values of D iterate across all the powers
  321. * of 2 less than the permutation size and then go back again. For
  322. * example, the typical presentation for 32 bits would have D iterate
  323. * over 16,8,4,2,1,2,4,8,16, and there's an easy algorithm that can
  324. * express any permutation in that form by deciding which pairs of
  325. * bits to swap in the outer pair of stages and then recursing to do
  326. * all the stages in between.
  327. *
  328. * Actually implementing the swaps is easy when they're all between
  329. * bits at the same separation: make the value x ^ (x >> D), mask out
  330. * just the bits in the low position of a pair that needs to swap, and
  331. * then use the resulting value y to make x ^ y ^ (y << D) which is
  332. * the swapped version.
  333. *
  334. * In this particular case, I processed the bit indices in the other
  335. * order (going 1,2,4,8,16,8,4,2,1), which makes no significant
  336. * difference to the construction algorithm (it's just a relabelling),
  337. * but it now means that the first two steps only permute entries
  338. * within the output of each S-box - and therefore we can leave them
  339. * completely out, in favour of just defining the S-boxes so that
  340. * those permutation steps are already applied. Furthermore, by
  341. * exhaustive search over the rest of the possible bit-orders for each
  342. * S-box, I was able to find a version of P which could be represented
  343. * in such a way that two further phases had all their control bits
  344. * zero and could be skipped. So the number of swap stages is reduced
  345. * to 5 from the 9 that might have been needed.
  346. */
  347. static inline uint32_t des_benes_step(uint32_t v, unsigned D, uint32_t mask)
  348. {
  349. uint32_t diff = (v ^ (v >> D)) & mask;
  350. return v ^ diff ^ (diff << D);
  351. }
  352. static inline uint32_t des_P(uint32_t v_orig)
  353. {
  354. uint32_t v = v_orig;
  355. /* initial stages with distance 1,2 are part of the S-box data table */
  356. v = des_benes_step(v, 4, 0x07030702);
  357. v = des_benes_step(v, 8, 0x004E009E);
  358. v = des_benes_step(v, 16, 0x0000D9D3);
  359. /* v = des_benes_step(v, 8, 0x00000000); no-op, so we can skip it */
  360. v = des_benes_step(v, 4, 0x05040004);
  361. /* v = des_benes_step(v, 2, 0x00000000); no-op, so we can skip it */
  362. v = des_benes_step(v, 1, 0x04045015);
  363. debug("P(%08"PRIx32") = %08"PRIx32"\n", v_orig, v);
  364. return v;
  365. }
  366. /*
  367. * Putting the S and P functions together, and adding in the round key
  368. * as well, gives us the full mixing function f.
  369. */
  370. static inline uint32_t des_f(uint32_t R, uint32_t K7531, uint32_t K6420)
  371. {
  372. uint32_t s7531 = R ^ K7531, s6420 = rol(R, 4) ^ K6420;
  373. return des_P(des_S(s6420, s7531));
  374. }
  375. /*
  376. * The key schedule, and the function to set it up.
  377. */
  378. typedef struct des_keysched des_keysched;
  379. struct des_keysched {
  380. uint32_t k7531[16], k6420[16];
  381. };
  382. /*
  383. * Simplistic function to select an arbitrary sequence of bits from
  384. * one value and glue them together into another value. bitnums[]
  385. * gives the sequence of bit indices of the input, from the highest
  386. * output bit downwards. An index of -1 means that output bit is left
  387. * at zero.
  388. *
  389. * This function is only used during key setup, so it doesn't need to
  390. * be highly optimised.
  391. */
  392. static inline uint64_t bitsel(
  393. uint64_t input, const int8_t *bitnums, size_t size)
  394. {
  395. uint64_t ret = 0;
  396. while (size-- > 0) {
  397. int bitpos = *bitnums++;
  398. ret <<= 1;
  399. if (bitpos >= 0)
  400. ret |= 1 & (input >> bitpos);
  401. }
  402. return ret;
  403. }
  404. static void des_key_setup(uint64_t key, des_keysched *sched)
  405. {
  406. static const int8_t PC1[] = {
  407. 7, 15, 23, 31, 39, 47, 55, 63, 6, 14, 22, 30, 38, 46,
  408. 54, 62, 5, 13, 21, 29, 37, 45, 53, 61, 4, 12, 20, 28,
  409. -1, -1, -1, -1,
  410. 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34, 42,
  411. 50, 58, 3, 11, 19, 27, 35, 43, 51, 59, 36, 44, 52, 60,
  412. };
  413. static const int8_t PC2_7531[] = {
  414. 46, 43, 49, 36, 59, 55, -1, -1, /* index into S-box 7 */
  415. 37, 41, 48, 56, 34, 52, -1, -1, /* index into S-box 5 */
  416. 15, 4, 25, 19, 9, 1, -1, -1, /* index into S-box 3 */
  417. 12, 7, 17, 0, 22, 3, -1, -1, /* index into S-box 1 */
  418. };
  419. static const int8_t PC2_6420[] = {
  420. 57, 32, 45, 54, 39, 50, -1, -1, /* index into S-box 6 */
  421. 44, 53, 33, 40, 47, 58, -1, -1, /* index into S-box 4 */
  422. 26, 16, 5, 11, 23, 8, -1, -1, /* index into S-box 2 */
  423. 10, 14, 6, 20, 27, 24, -1, -1, /* index into S-box 0 */
  424. };
  425. static const int leftshifts[] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
  426. /* Select 56 bits from the 64-bit input key integer (the low bit
  427. * of each input byte is unused), into a word consisting of two
  428. * 28-bit integers starting at bits 0 and 32. */
  429. uint64_t CD = bitsel(key, PC1, lenof(PC1));
  430. { // WINSCP
  431. size_t i; // WINSCP
  432. for (i = 0; i < 16; i++) {
  433. /* Rotate each 28-bit half of CD left by 1 or 2 bits (varying
  434. * between rounds) */
  435. CD <<= leftshifts[i];
  436. CD = (CD & 0x0FFFFFFF0FFFFFFFLL) | ((CD & 0xF0000000F0000000LL) >> 28); // WINSCP
  437. /* Select key bits from the rotated word to use during the
  438. * actual cipher */
  439. sched->k7531[i] = bitsel(CD, PC2_7531, lenof(PC2_7531));
  440. sched->k6420[i] = bitsel(CD, PC2_6420, lenof(PC2_6420));
  441. }
  442. } // WINSCP
  443. }
  444. /*
  445. * Helper routines for dealing with 64-bit blocks in the form of an L
  446. * and R word.
  447. */
  448. typedef struct LR LR;
  449. struct LR { uint32_t L, R; };
  450. static inline LR des_load_lr(const void *vp)
  451. {
  452. const uint8_t *p = (const uint8_t *)vp;
  453. LR out;
  454. out.L = GET_32BIT_MSB_FIRST(p);
  455. out.R = GET_32BIT_MSB_FIRST(p+4);
  456. return out;
  457. }
  458. static inline void des_store_lr(void *vp, LR lr)
  459. {
  460. uint8_t *p = (uint8_t *)vp;
  461. PUT_32BIT_MSB_FIRST(p, lr.L);
  462. PUT_32BIT_MSB_FIRST(p+4, lr.R);
  463. }
  464. static inline LR des_xor_lr(LR a, LR b)
  465. {
  466. a.L ^= b.L;
  467. a.R ^= b.R;
  468. return a;
  469. }
  470. static inline LR des_swap_lr(LR in)
  471. {
  472. LR out;
  473. out.L = in.R;
  474. out.R = in.L;
  475. return out;
  476. }
  477. /*
  478. * The initial and final permutations of official DES are in a
  479. * restricted form, in which the 'before' and 'after' positions of a
  480. * given data bit are derived from each other by permuting the bits of
  481. * the _index_ and flipping some of them. This allows the permutation
  482. * to be performed effectively by a method that looks rather like
  483. * _half_ of a general Benes network, because the restricted form
  484. * means only half of it is actually needed.
  485. *
  486. * _Our_ initial and final permutations include a rotation by 1 bit,
  487. * but it's still easier to just suffix that to the standard IP/FP
  488. * than to regenerate everything using a more general method.
  489. *
  490. * Because we're permuting 64 bits in this case, between two 32-bit
  491. * words, there's a separate helper function for this code that
  492. * doesn't look quite like des_benes_step() above.
  493. */
  494. static inline void des_bitswap_IP_FP(uint32_t *L, uint32_t *R,
  495. unsigned D, uint32_t mask)
  496. {
  497. uint32_t diff = mask & ((*R >> D) ^ *L);
  498. *R ^= diff << D;
  499. *L ^= diff;
  500. }
  501. static inline LR des_IP(LR lr)
  502. {
  503. des_bitswap_IP_FP(&lr.R, &lr.L, 4, 0x0F0F0F0F);
  504. des_bitswap_IP_FP(&lr.R, &lr.L, 16, 0x0000FFFF);
  505. des_bitswap_IP_FP(&lr.L, &lr.R, 2, 0x33333333);
  506. des_bitswap_IP_FP(&lr.L, &lr.R, 8, 0x00FF00FF);
  507. des_bitswap_IP_FP(&lr.R, &lr.L, 1, 0x55555555);
  508. lr.L = ror(lr.L, 1);
  509. lr.R = ror(lr.R, 1);
  510. return lr;
  511. }
  512. static inline LR des_FP(LR lr)
  513. {
  514. lr.L = rol(lr.L, 1);
  515. lr.R = rol(lr.R, 1);
  516. des_bitswap_IP_FP(&lr.R, &lr.L, 1, 0x55555555);
  517. des_bitswap_IP_FP(&lr.L, &lr.R, 8, 0x00FF00FF);
  518. des_bitswap_IP_FP(&lr.L, &lr.R, 2, 0x33333333);
  519. des_bitswap_IP_FP(&lr.R, &lr.L, 16, 0x0000FFFF);
  520. des_bitswap_IP_FP(&lr.R, &lr.L, 4, 0x0F0F0F0F);
  521. return lr;
  522. }
  523. /*
  524. * The main cipher functions, which are identical except that they use
  525. * the key schedule in opposite orders.
  526. *
  527. * We provide a version without the initial and final permutations,
  528. * for use in triple-DES mode (no sense undoing and redoing it in
  529. * between the phases).
  530. */
  531. static inline LR des_round(LR in, const des_keysched *sched, size_t round)
  532. {
  533. LR out;
  534. out.L = in.R;
  535. out.R = in.L ^ des_f(in.R, sched->k7531[round], sched->k6420[round]);
  536. return out;
  537. }
  538. static inline LR des_inner_cipher(LR lr, const des_keysched *sched,
  539. size_t start, size_t step)
  540. {
  541. lr = des_round(lr, sched, start+0x0*step);
  542. lr = des_round(lr, sched, start+0x1*step);
  543. lr = des_round(lr, sched, start+0x2*step);
  544. lr = des_round(lr, sched, start+0x3*step);
  545. lr = des_round(lr, sched, start+0x4*step);
  546. lr = des_round(lr, sched, start+0x5*step);
  547. lr = des_round(lr, sched, start+0x6*step);
  548. lr = des_round(lr, sched, start+0x7*step);
  549. lr = des_round(lr, sched, start+0x8*step);
  550. lr = des_round(lr, sched, start+0x9*step);
  551. lr = des_round(lr, sched, start+0xa*step);
  552. lr = des_round(lr, sched, start+0xb*step);
  553. lr = des_round(lr, sched, start+0xc*step);
  554. lr = des_round(lr, sched, start+0xd*step);
  555. lr = des_round(lr, sched, start+0xe*step);
  556. lr = des_round(lr, sched, start+0xf*step);
  557. return des_swap_lr(lr);
  558. }
  559. static inline LR des_full_cipher(LR lr, const des_keysched *sched,
  560. size_t start, size_t step)
  561. {
  562. lr = des_IP(lr);
  563. lr = des_inner_cipher(lr, sched, start, step);
  564. lr = des_FP(lr);
  565. return lr;
  566. }
  567. /*
  568. * Parameter pairs for the start,step arguments to the cipher routines
  569. * above, causing them to use the same key schedule in opposite orders.
  570. */
  571. #define ENCIPHER 0, 1 /* for encryption */
  572. #define DECIPHER 15, -1 /* for decryption */
  573. /* ----------------------------------------------------------------------
  574. * Single-DES
  575. */
  576. struct des_cbc_ctx {
  577. des_keysched sched;
  578. LR iv;
  579. ssh_cipher ciph;
  580. };
  581. static ssh_cipher *des_cbc_new(const ssh_cipheralg *alg)
  582. {
  583. struct des_cbc_ctx *ctx = snew(struct des_cbc_ctx);
  584. ctx->ciph.vt = alg;
  585. return &ctx->ciph;
  586. }
  587. static void des_cbc_free(ssh_cipher *ciph)
  588. {
  589. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  590. smemclr(ctx, sizeof(*ctx));
  591. sfree(ctx);
  592. }
  593. static void des_cbc_setkey(ssh_cipher *ciph, const void *vkey)
  594. {
  595. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  596. const uint8_t *key = (const uint8_t *)vkey;
  597. des_key_setup(GET_64BIT_MSB_FIRST(key), &ctx->sched);
  598. }
  599. static void des_cbc_setiv(ssh_cipher *ciph, const void *iv)
  600. {
  601. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  602. ctx->iv = des_load_lr(iv);
  603. }
  604. static void des_cbc_encrypt(ssh_cipher *ciph, void *vdata, int len)
  605. {
  606. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  607. uint8_t *data = (uint8_t *)vdata;
  608. for (; len > 0; len -= 8, data += 8) {
  609. LR plaintext = des_load_lr(data);
  610. LR cipher_in = des_xor_lr(plaintext, ctx->iv);
  611. LR ciphertext = des_full_cipher(cipher_in, &ctx->sched, ENCIPHER);
  612. des_store_lr(data, ciphertext);
  613. ctx->iv = ciphertext;
  614. }
  615. }
  616. static void des_cbc_decrypt(ssh_cipher *ciph, void *vdata, int len)
  617. {
  618. struct des_cbc_ctx *ctx = container_of(ciph, struct des_cbc_ctx, ciph);
  619. uint8_t *data = (uint8_t *)vdata;
  620. for (; len > 0; len -= 8, data += 8) {
  621. LR ciphertext = des_load_lr(data);
  622. LR cipher_out = des_full_cipher(ciphertext, &ctx->sched, DECIPHER);
  623. LR plaintext = des_xor_lr(cipher_out, ctx->iv);
  624. des_store_lr(data, plaintext);
  625. ctx->iv = ciphertext;
  626. }
  627. }
  628. const ssh_cipheralg ssh_des = {
  629. // WINSCP
  630. /*.new =*/ des_cbc_new,
  631. /*.free =*/ des_cbc_free,
  632. /*.setiv =*/ des_cbc_setiv,
  633. /*.setkey =*/ des_cbc_setkey,
  634. /*.encrypt =*/ des_cbc_encrypt,
  635. /*.decrypt =*/ des_cbc_decrypt,
  636. NULL, NULL, // WINSCP
  637. /*.next_message =*/ nullcipher_next_message,
  638. /*.ssh2_id =*/ "des-cbc",
  639. /*.blksize =*/ 8,
  640. /*.real_keybits =*/ 56,
  641. /*.padded_keybytes =*/ 8,
  642. /*.flags =*/ SSH_CIPHER_IS_CBC,
  643. /*.text_name =*/ "single-DES CBC",
  644. NULL, NULL, // WINSCP
  645. };
  646. const ssh_cipheralg ssh_des_sshcom_ssh2 = {
  647. /* Same as ssh_des_cbc, but with a different SSH-2 ID */
  648. // WINSCP
  649. /*.new =*/ des_cbc_new,
  650. /*.free =*/ des_cbc_free,
  651. /*.setiv =*/ des_cbc_setiv,
  652. /*.setkey =*/ des_cbc_setkey,
  653. /*.encrypt =*/ des_cbc_encrypt,
  654. /*.decrypt =*/ des_cbc_decrypt,
  655. NULL, NULL, // WINSCP
  656. /*.next_message =*/ nullcipher_next_message,
  657. /*.ssh2_id =*/ "[email protected]",
  658. /*.blksize =*/ 8,
  659. /*.real_keybits =*/ 56,
  660. /*.padded_keybytes =*/ 8,
  661. /*.flags =*/ SSH_CIPHER_IS_CBC,
  662. /*.text_name =*/ "single-DES CBC",
  663. NULL, NULL, // WINSCP
  664. };
  665. static const ssh_cipheralg *const des_list[] = {
  666. &ssh_des,
  667. &ssh_des_sshcom_ssh2
  668. };
  669. const ssh2_ciphers ssh2_des = { lenof(des_list), des_list };
  670. /* ----------------------------------------------------------------------
  671. * Triple-DES CBC, SSH-2 style. The CBC mode treats the three
  672. * invocations of DES as a single unified cipher, and surrounds it
  673. * with just one layer of CBC, so only one IV is needed.
  674. */
  675. struct des3_cbc1_ctx {
  676. des_keysched sched[3];
  677. LR iv;
  678. ssh_cipher ciph;
  679. };
  680. static ssh_cipher *des3_cbc1_new(const ssh_cipheralg *alg)
  681. {
  682. struct des3_cbc1_ctx *ctx = snew(struct des3_cbc1_ctx);
  683. ctx->ciph.vt = alg;
  684. return &ctx->ciph;
  685. }
  686. static void des3_cbc1_free(ssh_cipher *ciph)
  687. {
  688. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  689. smemclr(ctx, sizeof(*ctx));
  690. sfree(ctx);
  691. }
  692. static void des3_cbc1_setkey(ssh_cipher *ciph, const void *vkey)
  693. {
  694. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  695. const uint8_t *key = (const uint8_t *)vkey;
  696. size_t i; // WINSCP
  697. for (i = 0; i < 3; i++)
  698. des_key_setup(GET_64BIT_MSB_FIRST(key + 8*i), &ctx->sched[i]);
  699. }
  700. static void des3_cbc1_setiv(ssh_cipher *ciph, const void *iv)
  701. {
  702. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  703. ctx->iv = des_load_lr(iv);
  704. }
  705. static void des3_cbc1_cbc_encrypt(ssh_cipher *ciph, void *vdata, int len)
  706. {
  707. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  708. uint8_t *data = (uint8_t *)vdata;
  709. for (; len > 0; len -= 8, data += 8) {
  710. LR plaintext = des_load_lr(data);
  711. LR cipher_in = des_xor_lr(plaintext, ctx->iv);
  712. /* Run three copies of the cipher, without undoing and redoing
  713. * IP/FP in between. */
  714. LR lr = des_IP(cipher_in);
  715. lr = des_inner_cipher(lr, &ctx->sched[0], ENCIPHER);
  716. lr = des_inner_cipher(lr, &ctx->sched[1], DECIPHER);
  717. lr = des_inner_cipher(lr, &ctx->sched[2], ENCIPHER);
  718. { // WINSCP
  719. LR ciphertext = des_FP(lr);
  720. des_store_lr(data, ciphertext);
  721. ctx->iv = ciphertext;
  722. } // WINSCP
  723. }
  724. }
  725. static void des3_cbc1_cbc_decrypt(ssh_cipher *ciph, void *vdata, int len)
  726. {
  727. struct des3_cbc1_ctx *ctx = container_of(ciph, struct des3_cbc1_ctx, ciph);
  728. uint8_t *data = (uint8_t *)vdata;
  729. for (; len > 0; len -= 8, data += 8) {
  730. LR ciphertext = des_load_lr(data);
  731. /* Similarly to encryption, but with the order reversed. */
  732. LR lr = des_IP(ciphertext);
  733. lr = des_inner_cipher(lr, &ctx->sched[2], DECIPHER);
  734. lr = des_inner_cipher(lr, &ctx->sched[1], ENCIPHER);
  735. lr = des_inner_cipher(lr, &ctx->sched[0], DECIPHER);
  736. { // WINSCP
  737. LR cipher_out = des_FP(lr);
  738. LR plaintext = des_xor_lr(cipher_out, ctx->iv);
  739. des_store_lr(data, plaintext);
  740. ctx->iv = ciphertext;
  741. } // WINSCP
  742. }
  743. }
  744. const ssh_cipheralg ssh_3des_ssh2 = {
  745. // WINSCP
  746. /*.new =*/ des3_cbc1_new,
  747. /*.free =*/ des3_cbc1_free,
  748. /*.setiv =*/ des3_cbc1_setiv,
  749. /*.setkey =*/ des3_cbc1_setkey,
  750. /*.encrypt =*/ des3_cbc1_cbc_encrypt,
  751. /*.decrypt =*/ des3_cbc1_cbc_decrypt,
  752. NULL, NULL, // WINSCP
  753. /*.next_message =*/ nullcipher_next_message,
  754. /*.ssh2_id =*/ "3des-cbc",
  755. /*.blksize =*/ 8,
  756. /*.real_keybits =*/ 168,
  757. /*.padded_keybytes =*/ 24,
  758. /*.flags =*/ SSH_CIPHER_IS_CBC,
  759. /*.text_name =*/ "triple-DES CBC",
  760. NULL, NULL, // WINSCP
  761. };
  762. /* ----------------------------------------------------------------------
  763. * Triple-DES in SDCTR mode. Again, the three DES instances are
  764. * treated as one big cipher, with a single counter encrypted through
  765. * all three.
  766. */
  767. #define SDCTR_WORDS (8 / BIGNUM_INT_BYTES)
  768. struct des3_sdctr_ctx {
  769. des_keysched sched[3];
  770. BignumInt counter[SDCTR_WORDS];
  771. ssh_cipher ciph;
  772. };
  773. static ssh_cipher *des3_sdctr_new(const ssh_cipheralg *alg)
  774. {
  775. struct des3_sdctr_ctx *ctx = snew(struct des3_sdctr_ctx);
  776. ctx->ciph.vt = alg;
  777. return &ctx->ciph;
  778. }
  779. static void des3_sdctr_free(ssh_cipher *ciph)
  780. {
  781. struct des3_sdctr_ctx *ctx = container_of(
  782. ciph, struct des3_sdctr_ctx, ciph);
  783. smemclr(ctx, sizeof(*ctx));
  784. sfree(ctx);
  785. }
  786. static void des3_sdctr_setkey(ssh_cipher *ciph, const void *vkey)
  787. {
  788. struct des3_sdctr_ctx *ctx = container_of(
  789. ciph, struct des3_sdctr_ctx, ciph);
  790. const uint8_t *key = (const uint8_t *)vkey;
  791. size_t i; // WINSCP
  792. for (i = 0; i < 3; i++)
  793. des_key_setup(GET_64BIT_MSB_FIRST(key + 8*i), &ctx->sched[i]);
  794. }
  795. static void des3_sdctr_setiv(ssh_cipher *ciph, const void *viv)
  796. {
  797. struct des3_sdctr_ctx *ctx = container_of(
  798. ciph, struct des3_sdctr_ctx, ciph);
  799. const uint8_t *iv = (const uint8_t *)viv;
  800. /* Import the initial counter value into the internal representation */
  801. unsigned i = 0; // WINSCP
  802. for (i = 0; i < SDCTR_WORDS; i++)
  803. ctx->counter[i] = GET_BIGNUMINT_MSB_FIRST(
  804. iv + 8 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES);
  805. }
  806. static void des3_sdctr_encrypt_decrypt(ssh_cipher *ciph, void *vdata, int len)
  807. {
  808. struct des3_sdctr_ctx *ctx = container_of(
  809. ciph, struct des3_sdctr_ctx, ciph);
  810. uint8_t *data = (uint8_t *)vdata;
  811. uint8_t iv_buf[8];
  812. for (; len > 0; len -= 8, data += 8) {
  813. /* Format the counter value into the buffer. */
  814. unsigned i; // WINSCP
  815. for (i = 0; i < SDCTR_WORDS; i++)
  816. PUT_BIGNUMINT_MSB_FIRST(
  817. iv_buf + 8 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES,
  818. ctx->counter[i]);
  819. /* Increment the counter. */
  820. { // WINSCP
  821. BignumCarry carry = 1;
  822. for (i = 0; i < SDCTR_WORDS; i++) // WINSCP
  823. BignumADC(ctx->counter[i], carry, ctx->counter[i], 0, carry);
  824. /* Triple-encrypt the counter value from the IV. */
  825. { // WINSCP
  826. LR lr = des_IP(des_load_lr(iv_buf));
  827. lr = des_inner_cipher(lr, &ctx->sched[0], ENCIPHER);
  828. lr = des_inner_cipher(lr, &ctx->sched[1], DECIPHER);
  829. lr = des_inner_cipher(lr, &ctx->sched[2], ENCIPHER);
  830. { // WINSCP
  831. LR keystream = des_FP(lr);
  832. LR input = des_load_lr(data);
  833. LR output = des_xor_lr(input, keystream);
  834. des_store_lr(data, output);
  835. } // WINSCP
  836. } // WINSCP
  837. } // WINSCP
  838. }
  839. smemclr(iv_buf, sizeof(iv_buf));
  840. }
  841. const ssh_cipheralg ssh_3des_ssh2_ctr = {
  842. // WINSCP
  843. /*.new =*/ des3_sdctr_new,
  844. /*.free =*/ des3_sdctr_free,
  845. /*.setiv =*/ des3_sdctr_setiv,
  846. /*.setkey =*/ des3_sdctr_setkey,
  847. /*.encrypt =*/ des3_sdctr_encrypt_decrypt,
  848. /*.decrypt =*/ des3_sdctr_encrypt_decrypt,
  849. NULL, NULL, // WINSCP
  850. /*.next_message =*/ nullcipher_next_message,
  851. /*.ssh2_id =*/ "3des-ctr",
  852. /*.blksize =*/ 8,
  853. /*.real_keybits =*/ 168,
  854. /*.padded_keybytes =*/ 24,
  855. /*.flags =*/ 0,
  856. /*.text_name =*/ "triple-DES SDCTR",
  857. NULL, NULL, // WINSCP
  858. };
  859. static const ssh_cipheralg *const des3_list[] = {
  860. &ssh_3des_ssh2_ctr,
  861. &ssh_3des_ssh2
  862. };
  863. const ssh2_ciphers ssh2_3des = { lenof(des3_list), des3_list };
  864. /* ----------------------------------------------------------------------
  865. * Triple-DES, SSH-1 style. SSH-1 replicated the whole CBC structure
  866. * three times, so there have to be three separate IVs, one in each
  867. * layer.
  868. */
  869. struct des3_cbc3_ctx {
  870. des_keysched sched[3];
  871. LR iv[3];
  872. ssh_cipher ciph;
  873. };
  874. static ssh_cipher *des3_cbc3_new(const ssh_cipheralg *alg)
  875. {
  876. struct des3_cbc3_ctx *ctx = snew(struct des3_cbc3_ctx);
  877. ctx->ciph.vt = alg;
  878. return &ctx->ciph;
  879. }
  880. static void des3_cbc3_free(ssh_cipher *ciph)
  881. {
  882. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  883. smemclr(ctx, sizeof(*ctx));
  884. sfree(ctx);
  885. }
  886. static void des3_cbc3_setkey(ssh_cipher *ciph, const void *vkey)
  887. {
  888. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  889. const uint8_t *key = (const uint8_t *)vkey;
  890. size_t i; // WINSCP
  891. for (i = 0; i < 3; i++)
  892. des_key_setup(GET_64BIT_MSB_FIRST(key + 8*i), &ctx->sched[i]);
  893. }
  894. static void des3_cbc3_setiv(ssh_cipher *ciph, const void *viv)
  895. {
  896. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  897. /*
  898. * In principle, we ought to provide an interface for the user to
  899. * input 24 instead of 8 bytes of IV. But that would make this an
  900. * ugly exception to the otherwise universal rule that IV size =
  901. * cipher block size, and there's really no need to violate that
  902. * rule given that this is a historical one-off oddity and SSH-1
  903. * always initialises all three IVs to zero anyway. So we fudge it
  904. * by just setting all the IVs to the same value.
  905. */
  906. LR iv = des_load_lr(viv);
  907. /* But we store the IVs in permuted form, so that we can handle
  908. * all three CBC layers without having to do IP/FP in between. */
  909. iv = des_IP(iv);
  910. { // WINSCP
  911. size_t i; // WINSCP
  912. for (i = 0; i < 3; i++)
  913. ctx->iv[i] = iv;
  914. } // WINSCP
  915. }
  916. static void des3_cbc3_cbc_encrypt(ssh_cipher *ciph, void *vdata, int len)
  917. {
  918. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  919. uint8_t *data = (uint8_t *)vdata;
  920. for (; len > 0; len -= 8, data += 8) {
  921. /* Load and IP the input. */
  922. LR plaintext = des_IP(des_load_lr(data));
  923. LR lr = plaintext;
  924. /* Do three passes of CBC, with the middle one inverted. */
  925. lr = des_xor_lr(lr, ctx->iv[0]);
  926. lr = des_inner_cipher(lr, &ctx->sched[0], ENCIPHER);
  927. ctx->iv[0] = lr;
  928. { // WINSCP
  929. LR ciphertext = lr;
  930. lr = des_inner_cipher(ciphertext, &ctx->sched[1], DECIPHER);
  931. lr = des_xor_lr(lr, ctx->iv[1]);
  932. ctx->iv[1] = ciphertext;
  933. lr = des_xor_lr(lr, ctx->iv[2]);
  934. lr = des_inner_cipher(lr, &ctx->sched[2], ENCIPHER);
  935. ctx->iv[2] = lr;
  936. des_store_lr(data, des_FP(lr));
  937. } // WINSCP
  938. }
  939. }
  940. static void des3_cbc3_cbc_decrypt(ssh_cipher *ciph, void *vdata, int len)
  941. {
  942. struct des3_cbc3_ctx *ctx = container_of(ciph, struct des3_cbc3_ctx, ciph);
  943. uint8_t *data = (uint8_t *)vdata;
  944. for (; len > 0; len -= 8, data += 8) {
  945. /* Load and IP the input */
  946. LR lr = des_IP(des_load_lr(data));
  947. LR ciphertext;
  948. /* Do three passes of CBC, with the middle one inverted. */
  949. ciphertext = lr;
  950. lr = des_inner_cipher(ciphertext, &ctx->sched[2], DECIPHER);
  951. lr = des_xor_lr(lr, ctx->iv[2]);
  952. ctx->iv[2] = ciphertext;
  953. lr = des_xor_lr(lr, ctx->iv[1]);
  954. lr = des_inner_cipher(lr, &ctx->sched[1], ENCIPHER);
  955. ctx->iv[1] = lr;
  956. ciphertext = lr;
  957. lr = des_inner_cipher(ciphertext, &ctx->sched[0], DECIPHER);
  958. lr = des_xor_lr(lr, ctx->iv[0]);
  959. ctx->iv[0] = ciphertext;
  960. des_store_lr(data, des_FP(lr));
  961. }
  962. }
  963. const ssh_cipheralg ssh_3des_ssh1 = {
  964. // WINSCP
  965. /*.new =*/ des3_cbc3_new,
  966. /*.free =*/ des3_cbc3_free,
  967. /*.setiv =*/ des3_cbc3_setiv,
  968. /*.setkey =*/ des3_cbc3_setkey,
  969. /*.encrypt =*/ des3_cbc3_cbc_encrypt,
  970. /*.decrypt =*/ des3_cbc3_cbc_decrypt,
  971. NULL, NULL, // WINSCP
  972. /*.next_message =*/ nullcipher_next_message,
  973. NULL, // WINSCP
  974. /*.blksize =*/ 8,
  975. /*.real_keybits =*/ 168,
  976. /*.padded_keybytes =*/ 24,
  977. /*.flags =*/ SSH_CIPHER_IS_CBC,
  978. /*.text_name =*/ "triple-DES inner-CBC",
  979. NULL, NULL, // WINSCP
  980. };