aes-sw.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * Software implementation of AES.
  3. *
  4. * This implementation uses a bit-sliced representation. Instead of
  5. * the obvious approach of storing the cipher state so that each byte
  6. * (or field element, or entry in the cipher matrix) occupies 8
  7. * contiguous bits in a machine integer somewhere, we organise the
  8. * cipher state as an array of 8 integers, in such a way that each
  9. * logical byte of the cipher state occupies one bit in each integer,
  10. * all at the same position. This allows us to do parallel logic on
  11. * all bytes of the state by doing bitwise operations between the 8
  12. * integers; in particular, the S-box (SubBytes) lookup is done this
  13. * way, which takes about 110 operations - but for those 110 bitwise
  14. * ops you get 64 S-box lookups, not just one.
  15. */
  16. #include "ssh.h"
  17. #include "aes.h"
  18. #include "mpint_i.h" /* we reuse the BignumInt system */
  19. static bool aes_sw_available(void)
  20. {
  21. /* Software AES is always available */
  22. return true;
  23. }
  24. #define SLICE_PARALLELISM (BIGNUM_INT_BYTES / 2)
  25. #ifdef WINSCP_VS
  26. #ifdef BITSLICED_DEBUG
  27. /* Dump function that undoes the bitslicing transform, so you can see
  28. * the logical data represented by a set of slice words. */
  29. static inline void dumpslices_uint16_t(
  30. const char *prefix, const uint16_t slices[8])
  31. {
  32. printf("%-30s", prefix);
  33. for (unsigned byte = 0; byte < 16; byte++) {
  34. unsigned byteval = 0;
  35. for (unsigned bit = 0; bit < 8; bit++)
  36. byteval |= (1 & (slices[bit] >> byte)) << bit;
  37. printf("%02x", byteval);
  38. }
  39. printf("\n");
  40. }
  41. static inline void dumpslices_BignumInt(
  42. const char *prefix, const BignumInt slices[8])
  43. {
  44. printf("%-30s", prefix);
  45. for (unsigned iter = 0; iter < SLICE_PARALLELISM; iter++) {
  46. for (unsigned byte = 0; byte < 16; byte++) {
  47. unsigned byteval = 0;
  48. for (unsigned bit = 0; bit < 8; bit++)
  49. byteval |= (1 & (slices[bit] >> (iter*16+byte))) << bit;
  50. printf("%02x", byteval);
  51. }
  52. if (iter+1 < SLICE_PARALLELISM)
  53. printf(" ");
  54. }
  55. printf("\n");
  56. }
  57. #else
  58. #define dumpslices_uintN_t(prefix, slices) ((void)0)
  59. #define dumpslices_BignumInt(prefix, slices) ((void)0)
  60. #endif
  61. /* -----
  62. * Bit-slicing transformation: convert between an array of 16 uint8_t
  63. * and an array of 8 uint16_t, so as to interchange the bit index
  64. * within each element and the element index within the array. (That
  65. * is, bit j of input[i] == bit i of output[j].
  66. */
  67. #define SWAPWORDS(shift) do \
  68. { \
  69. uint64_t mask = ~(uint64_t)0 / ((1ULL << shift) + 1); \
  70. uint64_t diff = ((i0 >> shift) ^ i1) & mask; \
  71. i0 ^= diff << shift; \
  72. i1 ^= diff; \
  73. } while (0)
  74. #define SWAPINWORD(i, bigshift, smallshift) do \
  75. { \
  76. uint64_t mask = ~(uint64_t)0; \
  77. mask /= ((1ULL << bigshift) + 1); \
  78. mask /= ((1ULL << smallshift) + 1); \
  79. mask <<= smallshift; \
  80. unsigned shift = bigshift - smallshift; \
  81. uint64_t diff = ((i >> shift) ^ i) & mask; \
  82. i ^= diff ^ (diff << shift); \
  83. } while (0)
  84. #define TO_BITSLICES(slices, bytes, uintN_t, assign_op, shift) do \
  85. { \
  86. uint64_t i0 = GET_64BIT_LSB_FIRST(bytes); \
  87. uint64_t i1 = GET_64BIT_LSB_FIRST(bytes + 8); \
  88. SWAPINWORD(i0, 8, 1); \
  89. SWAPINWORD(i1, 8, 1); \
  90. SWAPINWORD(i0, 16, 2); \
  91. SWAPINWORD(i1, 16, 2); \
  92. SWAPINWORD(i0, 32, 4); \
  93. SWAPINWORD(i1, 32, 4); \
  94. SWAPWORDS(8); \
  95. slices[0] assign_op (uintN_t)((i0 >> 0) & 0xFFFF) << (shift); \
  96. slices[2] assign_op (uintN_t)((i0 >> 16) & 0xFFFF) << (shift); \
  97. slices[4] assign_op (uintN_t)((i0 >> 32) & 0xFFFF) << (shift); \
  98. slices[6] assign_op (uintN_t)((i0 >> 48) & 0xFFFF) << (shift); \
  99. slices[1] assign_op (uintN_t)((i1 >> 0) & 0xFFFF) << (shift); \
  100. slices[3] assign_op (uintN_t)((i1 >> 16) & 0xFFFF) << (shift); \
  101. slices[5] assign_op (uintN_t)((i1 >> 32) & 0xFFFF) << (shift); \
  102. slices[7] assign_op (uintN_t)((i1 >> 48) & 0xFFFF) << (shift); \
  103. } while (0)
  104. #define FROM_BITSLICES(bytes, slices, shift) do \
  105. { \
  106. uint64_t i1 = ((slices[7] >> (shift)) & 0xFFFF); \
  107. i1 = (i1 << 16) | ((slices[5] >> (shift)) & 0xFFFF); \
  108. i1 = (i1 << 16) | ((slices[3] >> (shift)) & 0xFFFF); \
  109. i1 = (i1 << 16) | ((slices[1] >> (shift)) & 0xFFFF); \
  110. uint64_t i0 = ((slices[6] >> (shift)) & 0xFFFF); \
  111. i0 = (i0 << 16) | ((slices[4] >> (shift)) & 0xFFFF); \
  112. i0 = (i0 << 16) | ((slices[2] >> (shift)) & 0xFFFF); \
  113. i0 = (i0 << 16) | ((slices[0] >> (shift)) & 0xFFFF); \
  114. SWAPWORDS(8); \
  115. SWAPINWORD(i0, 32, 4); \
  116. SWAPINWORD(i1, 32, 4); \
  117. SWAPINWORD(i0, 16, 2); \
  118. SWAPINWORD(i1, 16, 2); \
  119. SWAPINWORD(i0, 8, 1); \
  120. SWAPINWORD(i1, 8, 1); \
  121. PUT_64BIT_LSB_FIRST(bytes, i0); \
  122. PUT_64BIT_LSB_FIRST((bytes) + 8, i1); \
  123. } while (0)
  124. /* -----
  125. * Some macros that will be useful repeatedly.
  126. */
  127. /* Iterate a unary transformation over all 8 slices. */
  128. #define ITERATE(MACRO, output, input, uintN_t) do \
  129. { \
  130. MACRO(output[0], input[0], uintN_t); \
  131. MACRO(output[1], input[1], uintN_t); \
  132. MACRO(output[2], input[2], uintN_t); \
  133. MACRO(output[3], input[3], uintN_t); \
  134. MACRO(output[4], input[4], uintN_t); \
  135. MACRO(output[5], input[5], uintN_t); \
  136. MACRO(output[6], input[6], uintN_t); \
  137. MACRO(output[7], input[7], uintN_t); \
  138. } while (0)
  139. /* Simply add (i.e. XOR) two whole sets of slices together. */
  140. #define BITSLICED_ADD(output, lhs, rhs) do \
  141. { \
  142. output[0] = lhs[0] ^ rhs[0]; \
  143. output[1] = lhs[1] ^ rhs[1]; \
  144. output[2] = lhs[2] ^ rhs[2]; \
  145. output[3] = lhs[3] ^ rhs[3]; \
  146. output[4] = lhs[4] ^ rhs[4]; \
  147. output[5] = lhs[5] ^ rhs[5]; \
  148. output[6] = lhs[6] ^ rhs[6]; \
  149. output[7] = lhs[7] ^ rhs[7]; \
  150. } while (0)
  151. /* -----
  152. * The AES S-box, in pure bitwise logic so that it can be run in
  153. * parallel on whole words full of bit-sliced field elements.
  154. *
  155. * Source: 'A new combinational logic minimization technique with
  156. * applications to cryptology', https://eprint.iacr.org/2009/191
  157. *
  158. * As a minor speed optimisation, I use a modified version of the
  159. * S-box which omits the additive constant 0x63, i.e. this S-box
  160. * consists of only the field inversion and linear map components.
  161. * Instead, the addition of the constant is deferred until after the
  162. * subsequent ShiftRows and MixColumns stages, so that it happens at
  163. * the same time as adding the next round key - and then we just make
  164. * it _part_ of the round key, so it doesn't cost any extra
  165. * instructions to add.
  166. *
  167. * (Obviously adding a constant to each byte commutes with ShiftRows,
  168. * which only permutes the bytes. It also commutes with MixColumns:
  169. * that's not quite so obvious, but since the effect of MixColumns is
  170. * to multiply a constant polynomial M into each column, it is obvious
  171. * that adding some polynomial K and then multiplying by M is
  172. * equivalent to multiplying by M and then adding the product KM. And
  173. * in fact, since the coefficients of M happen to sum to 1, it turns
  174. * out that KM = K, so we don't even have to change the constant when
  175. * we move it to the far side of MixColumns.)
  176. *
  177. * Of course, one knock-on effect of this is that the use of the S-box
  178. * *during* key setup has to be corrected by manually adding on the
  179. * constant afterwards!
  180. */
  181. /* Initial linear transformation for the forward S-box, from Fig 2 of
  182. * the paper. */
  183. #define SBOX_FORWARD_TOP_TRANSFORM(input, uintN_t) \
  184. uintN_t y14 = input[4] ^ input[2]; \
  185. uintN_t y13 = input[7] ^ input[1]; \
  186. uintN_t y9 = input[7] ^ input[4]; \
  187. uintN_t y8 = input[7] ^ input[2]; \
  188. uintN_t t0 = input[6] ^ input[5]; \
  189. uintN_t y1 = t0 ^ input[0]; \
  190. uintN_t y4 = y1 ^ input[4]; \
  191. uintN_t y12 = y13 ^ y14; \
  192. uintN_t y2 = y1 ^ input[7]; \
  193. uintN_t y5 = y1 ^ input[1]; \
  194. uintN_t y3 = y5 ^ y8; \
  195. uintN_t t1 = input[3] ^ y12; \
  196. uintN_t y15 = t1 ^ input[2]; \
  197. uintN_t y20 = t1 ^ input[6]; \
  198. uintN_t y6 = y15 ^ input[0]; \
  199. uintN_t y10 = y15 ^ t0; \
  200. uintN_t y11 = y20 ^ y9; \
  201. uintN_t y7 = input[0] ^ y11; \
  202. uintN_t y17 = y10 ^ y11; \
  203. uintN_t y19 = y10 ^ y8; \
  204. uintN_t y16 = t0 ^ y11; \
  205. uintN_t y21 = y13 ^ y16; \
  206. uintN_t y18 = input[7] ^ y16; \
  207. /* Make a copy of input[0] under a new name, because the core
  208. * will refer to it, and in the inverse version of the S-box
  209. * the corresponding value will be one of the calculated ones
  210. * and not in input[0] itself. */ \
  211. uintN_t i0 = input[0]; \
  212. /* end */
  213. /* Core nonlinear component, from Fig 3 of the paper. */
  214. #define SBOX_CORE(uintN_t) \
  215. uintN_t t2 = y12 & y15; \
  216. uintN_t t3 = y3 & y6; \
  217. uintN_t t4 = t3 ^ t2; \
  218. uintN_t t5 = y4 & i0; \
  219. uintN_t t6 = t5 ^ t2; \
  220. uintN_t t7 = y13 & y16; \
  221. uintN_t t8 = y5 & y1; \
  222. uintN_t t9 = t8 ^ t7; \
  223. uintN_t t10 = y2 & y7; \
  224. uintN_t t11 = t10 ^ t7; \
  225. uintN_t t12 = y9 & y11; \
  226. uintN_t t13 = y14 & y17; \
  227. uintN_t t14 = t13 ^ t12; \
  228. uintN_t t15 = y8 & y10; \
  229. uintN_t t16 = t15 ^ t12; \
  230. uintN_t t17 = t4 ^ t14; \
  231. uintN_t t18 = t6 ^ t16; \
  232. uintN_t t19 = t9 ^ t14; \
  233. uintN_t t20 = t11 ^ t16; \
  234. uintN_t t21 = t17 ^ y20; \
  235. uintN_t t22 = t18 ^ y19; \
  236. uintN_t t23 = t19 ^ y21; \
  237. uintN_t t24 = t20 ^ y18; \
  238. uintN_t t25 = t21 ^ t22; \
  239. uintN_t t26 = t21 & t23; \
  240. uintN_t t27 = t24 ^ t26; \
  241. uintN_t t28 = t25 & t27; \
  242. uintN_t t29 = t28 ^ t22; \
  243. uintN_t t30 = t23 ^ t24; \
  244. uintN_t t31 = t22 ^ t26; \
  245. uintN_t t32 = t31 & t30; \
  246. uintN_t t33 = t32 ^ t24; \
  247. uintN_t t34 = t23 ^ t33; \
  248. uintN_t t35 = t27 ^ t33; \
  249. uintN_t t36 = t24 & t35; \
  250. uintN_t t37 = t36 ^ t34; \
  251. uintN_t t38 = t27 ^ t36; \
  252. uintN_t t39 = t29 & t38; \
  253. uintN_t t40 = t25 ^ t39; \
  254. uintN_t t41 = t40 ^ t37; \
  255. uintN_t t42 = t29 ^ t33; \
  256. uintN_t t43 = t29 ^ t40; \
  257. uintN_t t44 = t33 ^ t37; \
  258. uintN_t t45 = t42 ^ t41; \
  259. uintN_t z0 = t44 & y15; \
  260. uintN_t z1 = t37 & y6; \
  261. uintN_t z2 = t33 & i0; \
  262. uintN_t z3 = t43 & y16; \
  263. uintN_t z4 = t40 & y1; \
  264. uintN_t z5 = t29 & y7; \
  265. uintN_t z6 = t42 & y11; \
  266. uintN_t z7 = t45 & y17; \
  267. uintN_t z8 = t41 & y10; \
  268. uintN_t z9 = t44 & y12; \
  269. uintN_t z10 = t37 & y3; \
  270. uintN_t z11 = t33 & y4; \
  271. uintN_t z12 = t43 & y13; \
  272. uintN_t z13 = t40 & y5; \
  273. uintN_t z14 = t29 & y2; \
  274. uintN_t z15 = t42 & y9; \
  275. uintN_t z16 = t45 & y14; \
  276. uintN_t z17 = t41 & y8; \
  277. /* end */
  278. /* Final linear transformation for the forward S-box, from Fig 4 of
  279. * the paper. */
  280. #define SBOX_FORWARD_BOTTOM_TRANSFORM(output, uintN_t) \
  281. uintN_t t46 = z15 ^ z16; \
  282. uintN_t t47 = z10 ^ z11; \
  283. uintN_t t48 = z5 ^ z13; \
  284. uintN_t t49 = z9 ^ z10; \
  285. uintN_t t50 = z2 ^ z12; \
  286. uintN_t t51 = z2 ^ z5; \
  287. uintN_t t52 = z7 ^ z8; \
  288. uintN_t t53 = z0 ^ z3; \
  289. uintN_t t54 = z6 ^ z7; \
  290. uintN_t t55 = z16 ^ z17; \
  291. uintN_t t56 = z12 ^ t48; \
  292. uintN_t t57 = t50 ^ t53; \
  293. uintN_t t58 = z4 ^ t46; \
  294. uintN_t t59 = z3 ^ t54; \
  295. uintN_t t60 = t46 ^ t57; \
  296. uintN_t t61 = z14 ^ t57; \
  297. uintN_t t62 = t52 ^ t58; \
  298. uintN_t t63 = t49 ^ t58; \
  299. uintN_t t64 = z4 ^ t59; \
  300. uintN_t t65 = t61 ^ t62; \
  301. uintN_t t66 = z1 ^ t63; \
  302. output[7] = t59 ^ t63; \
  303. output[1] = t56 ^ t62; \
  304. output[0] = t48 ^ t60; \
  305. uintN_t t67 = t64 ^ t65; \
  306. output[4] = t53 ^ t66; \
  307. output[3] = t51 ^ t66; \
  308. output[2] = t47 ^ t65; \
  309. output[6] = t64 ^ output[4]; \
  310. output[5] = t55 ^ t67; \
  311. /* end */
  312. #define BITSLICED_SUBBYTES(output, input, uintN_t) do { \
  313. SBOX_FORWARD_TOP_TRANSFORM(input, uintN_t); \
  314. SBOX_CORE(uintN_t); \
  315. SBOX_FORWARD_BOTTOM_TRANSFORM(output, uintN_t); \
  316. } while (0)
  317. /*
  318. * Initial and final linear transformations for the backward S-box. I
  319. * generated these myself, by implementing the linear-transform
  320. * optimisation algorithm in the paper, and applying it to the
  321. * matrices calculated by _their_ top and bottom transformations, pre-
  322. * and post-multiplied as appropriate by the linear map in the inverse
  323. * S_box.
  324. */
  325. #define SBOX_BACKWARD_TOP_TRANSFORM(input, uintN_t) \
  326. uintN_t y5 = input[4] ^ input[6]; \
  327. uintN_t y19 = input[3] ^ input[0]; \
  328. uintN_t itmp8 = y5 ^ input[0]; \
  329. uintN_t y4 = itmp8 ^ input[1]; \
  330. uintN_t y9 = input[4] ^ input[3]; \
  331. uintN_t y2 = y9 ^ y4; \
  332. uintN_t itmp9 = y2 ^ input[7]; \
  333. uintN_t y1 = y9 ^ input[0]; \
  334. uintN_t y6 = y5 ^ input[7]; \
  335. uintN_t y18 = y9 ^ input[5]; \
  336. uintN_t y7 = y18 ^ y2; \
  337. uintN_t y16 = y7 ^ y1; \
  338. uintN_t y21 = y7 ^ input[1]; \
  339. uintN_t y3 = input[4] ^ input[7]; \
  340. uintN_t y13 = y16 ^ y21; \
  341. uintN_t y8 = input[4] ^ y6; \
  342. uintN_t y10 = y8 ^ y19; \
  343. uintN_t y14 = y8 ^ y9; \
  344. uintN_t y20 = itmp9 ^ input[2]; \
  345. uintN_t y11 = y9 ^ y20; \
  346. uintN_t i0 = y11 ^ y7; \
  347. uintN_t y15 = i0 ^ y6; \
  348. uintN_t y17 = y16 ^ y15; \
  349. uintN_t y12 = itmp9 ^ input[3]; \
  350. /* end */
  351. #define SBOX_BACKWARD_BOTTOM_TRANSFORM(output, uintN_t) \
  352. uintN_t otmp18 = z15 ^ z6; \
  353. uintN_t otmp19 = z13 ^ otmp18; \
  354. uintN_t otmp20 = z12 ^ otmp19; \
  355. uintN_t otmp21 = z16 ^ otmp20; \
  356. uintN_t otmp22 = z8 ^ otmp21; \
  357. uintN_t otmp23 = z0 ^ otmp22; \
  358. uintN_t otmp24 = otmp22 ^ z3; \
  359. uintN_t otmp25 = otmp24 ^ z4; \
  360. uintN_t otmp26 = otmp25 ^ z2; \
  361. uintN_t otmp27 = z1 ^ otmp26; \
  362. uintN_t otmp28 = z14 ^ otmp27; \
  363. uintN_t otmp29 = otmp28 ^ z10; \
  364. output[4] = z2 ^ otmp23; \
  365. output[7] = z5 ^ otmp24; \
  366. uintN_t otmp30 = z11 ^ otmp29; \
  367. output[5] = z13 ^ otmp30; \
  368. uintN_t otmp31 = otmp25 ^ z8; \
  369. output[1] = z7 ^ otmp31; \
  370. uintN_t otmp32 = z11 ^ z9; \
  371. uintN_t otmp33 = z17 ^ otmp32; \
  372. uintN_t otmp34 = otmp30 ^ otmp33; \
  373. output[0] = z15 ^ otmp33; \
  374. uintN_t otmp35 = z12 ^ otmp34; \
  375. output[6] = otmp35 ^ z16; \
  376. uintN_t otmp36 = z1 ^ otmp23; \
  377. uintN_t otmp37 = z5 ^ otmp36; \
  378. output[2] = z4 ^ otmp37; \
  379. uintN_t otmp38 = z11 ^ output[1]; \
  380. uintN_t otmp39 = z2 ^ otmp38; \
  381. uintN_t otmp40 = z17 ^ otmp39; \
  382. uintN_t otmp41 = z0 ^ otmp40; \
  383. uintN_t otmp42 = z5 ^ otmp41; \
  384. uintN_t otmp43 = otmp42 ^ z10; \
  385. uintN_t otmp44 = otmp43 ^ z3; \
  386. output[3] = otmp44 ^ z16; \
  387. /* end */
  388. #define BITSLICED_INVSUBBYTES(output, input, uintN_t) do { \
  389. SBOX_BACKWARD_TOP_TRANSFORM(input, uintN_t); \
  390. SBOX_CORE(uintN_t); \
  391. SBOX_BACKWARD_BOTTOM_TRANSFORM(output, uintN_t); \
  392. } while (0)
  393. /* -----
  394. * The ShiftRows transformation. This operates independently on each
  395. * bit slice.
  396. */
  397. #define SINGLE_BITSLICE_SHIFTROWS(output, input, uintN_t) do \
  398. { \
  399. uintN_t mask, mask2, mask3, diff, x = (input); \
  400. /* Rotate rows 2 and 3 by 16 bits */ \
  401. mask = 0x00CC * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  402. diff = ((x >> 8) ^ x) & mask; \
  403. x ^= diff ^ (diff << 8); \
  404. /* Rotate rows 1 and 3 by 8 bits */ \
  405. mask = 0x0AAA * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  406. mask2 = 0xA000 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  407. mask3 = 0x5555 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  408. x = ((x >> 4) & mask) | ((x << 12) & mask2) | (x & mask3); \
  409. /* Write output */ \
  410. (output) = x; \
  411. } while (0)
  412. #define SINGLE_BITSLICE_INVSHIFTROWS(output, input, uintN_t) do \
  413. { \
  414. uintN_t mask, mask2, mask3, diff, x = (input); \
  415. /* Rotate rows 2 and 3 by 16 bits */ \
  416. mask = 0x00CC * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  417. diff = ((x >> 8) ^ x) & mask; \
  418. x ^= diff ^ (diff << 8); \
  419. /* Rotate rows 1 and 3 by 8 bits, the opposite way to ShiftRows */ \
  420. mask = 0x000A * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  421. mask2 = 0xAAA0 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  422. mask3 = 0x5555 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  423. x = ((x >> 12) & mask) | ((x << 4) & mask2) | (x & mask3); \
  424. /* Write output */ \
  425. (output) = x; \
  426. } while (0)
  427. #define BITSLICED_SHIFTROWS(output, input, uintN_t) do \
  428. { \
  429. ITERATE(SINGLE_BITSLICE_SHIFTROWS, output, input, uintN_t); \
  430. } while (0)
  431. #define BITSLICED_INVSHIFTROWS(output, input, uintN_t) do \
  432. { \
  433. ITERATE(SINGLE_BITSLICE_INVSHIFTROWS, output, input, uintN_t); \
  434. } while (0)
  435. /* -----
  436. * The MixColumns transformation. This has to operate on all eight bit
  437. * slices at once, and also passes data back and forth between the
  438. * bits in an adjacent group of 4 within each slice.
  439. *
  440. * Notation: let F = GF(2)[X]/<X^8+X^4+X^3+X+1> be the finite field
  441. * used in AES, and let R = F[Y]/<Y^4+1> be the ring whose elements
  442. * represent the possible contents of a column of the matrix. I use X
  443. * and Y below in those senses, i.e. X is the value in F that
  444. * represents the byte 0x02, and Y is the value in R that cycles the
  445. * four bytes around by one if you multiply by it.
  446. */
  447. /* Multiply every column by Y^3, i.e. cycle it round one place to the
  448. * right. Operates on one bit slice at a time; you have to wrap it in
  449. * ITERATE to affect all the data at once. */
  450. #define BITSLICED_MUL_BY_Y3(output, input, uintN_t) do \
  451. { \
  452. uintN_t mask, mask2, x; \
  453. mask = 0x8 * (((uintN_t)~(uintN_t)0) / 0xF); \
  454. mask2 = 0x7 * (((uintN_t)~(uintN_t)0) / 0xF); \
  455. x = input; \
  456. output = ((x << 3) & mask) ^ ((x >> 1) & mask2); \
  457. } while (0)
  458. /* Multiply every column by Y^2. */
  459. #define BITSLICED_MUL_BY_Y2(output, input, uintN_t) do \
  460. { \
  461. uintN_t mask, mask2, x; \
  462. mask = 0xC * (((uintN_t)~(uintN_t)0) / 0xF); \
  463. mask2 = 0x3 * (((uintN_t)~(uintN_t)0) / 0xF); \
  464. x = input; \
  465. output = ((x << 2) & mask) ^ ((x >> 2) & mask2); \
  466. } while (0)
  467. #define BITSLICED_MUL_BY_1_Y3(output, input, uintN_t) do \
  468. { \
  469. uintN_t tmp = input; \
  470. BITSLICED_MUL_BY_Y3(tmp, input, uintN_t); \
  471. output = input ^ tmp; \
  472. } while (0)
  473. /* Multiply every column by 1+Y^2. */
  474. #define BITSLICED_MUL_BY_1_Y2(output, input, uintN_t) do \
  475. { \
  476. uintN_t tmp = input; \
  477. BITSLICED_MUL_BY_Y2(tmp, input, uintN_t); \
  478. output = input ^ tmp; \
  479. } while (0)
  480. /* Multiply every field element by X. This has to feed data between
  481. * slices, so it does the whole job in one go without needing ITERATE. */
  482. #define BITSLICED_MUL_BY_X(output, input, uintN_t) do \
  483. { \
  484. uintN_t bit7 = input[7]; \
  485. output[7] = input[6]; \
  486. output[6] = input[5]; \
  487. output[5] = input[4]; \
  488. output[4] = input[3] ^ bit7; \
  489. output[3] = input[2] ^ bit7; \
  490. output[2] = input[1]; \
  491. output[1] = input[0] ^ bit7; \
  492. output[0] = bit7; \
  493. } while (0)
  494. /*
  495. * The MixColumns constant is
  496. * M = X + Y + Y^2 + (X+1)Y^3
  497. * which we construct by rearranging it into
  498. * M = 1 + (1+Y^3) [ X + (1+Y^2) ]
  499. */
  500. #define BITSLICED_MIXCOLUMNS(output, input, uintN_t) do \
  501. { \
  502. uintN_t a[8], aX[8], b[8]; \
  503. /* a = input * (1+Y^3) */ \
  504. ITERATE(BITSLICED_MUL_BY_1_Y3, a, input, uintN_t); \
  505. /* aX = a * X */ \
  506. BITSLICED_MUL_BY_X(aX, a, uintN_t); \
  507. /* b = a * (1+Y^2) = input * (1+Y+Y^2+Y^3) */ \
  508. ITERATE(BITSLICED_MUL_BY_1_Y2, b, a, uintN_t); \
  509. /* output = input + aX + b (reusing a as a temp */ \
  510. BITSLICED_ADD(a, aX, b); \
  511. BITSLICED_ADD(output, input, a); \
  512. } while (0)
  513. /*
  514. * The InvMixColumns constant, written out longhand, is
  515. * I = (X^3+X^2+X) + (X^3+1)Y + (X^3+X^2+1)Y^2 + (X^3+X+1)Y^3
  516. * We represent this as
  517. * I = (X^3+X^2+X+1)(Y^3+Y^2+Y+1) + 1 + X(Y+Y^2) + X^2(Y+Y^3)
  518. */
  519. #define BITSLICED_INVMIXCOLUMNS(output, input, uintN_t) do \
  520. { \
  521. /* We need input * X^i for i=1,...,3 */ \
  522. uintN_t X[8], X2[8], X3[8]; \
  523. BITSLICED_MUL_BY_X(X, input, uintN_t); \
  524. BITSLICED_MUL_BY_X(X2, X, uintN_t); \
  525. BITSLICED_MUL_BY_X(X3, X2, uintN_t); \
  526. /* Sum them all and multiply by 1+Y+Y^2+Y^3. */ \
  527. uintN_t S[8]; \
  528. BITSLICED_ADD(S, input, X); \
  529. BITSLICED_ADD(S, S, X2); \
  530. BITSLICED_ADD(S, S, X3); \
  531. ITERATE(BITSLICED_MUL_BY_1_Y3, S, S, uintN_t); \
  532. ITERATE(BITSLICED_MUL_BY_1_Y2, S, S, uintN_t); \
  533. /* Compute the X(Y+Y^2) term. */ \
  534. uintN_t A[8]; \
  535. ITERATE(BITSLICED_MUL_BY_1_Y3, A, X, uintN_t); \
  536. ITERATE(BITSLICED_MUL_BY_Y2, A, A, uintN_t); \
  537. /* Compute the X^2(Y+Y^3) term. */ \
  538. uintN_t B[8]; \
  539. ITERATE(BITSLICED_MUL_BY_1_Y2, B, X2, uintN_t); \
  540. ITERATE(BITSLICED_MUL_BY_Y3, B, B, uintN_t); \
  541. /* And add all the pieces together. */ \
  542. BITSLICED_ADD(S, S, input); \
  543. BITSLICED_ADD(S, S, A); \
  544. BITSLICED_ADD(output, S, B); \
  545. } while (0)
  546. /* -----
  547. * Put it all together into a cipher round.
  548. */
  549. /* Dummy macro to get rid of the MixColumns in the final round. */
  550. #define NO_MIXCOLUMNS(out, in, uintN_t) do {} while (0)
  551. #define ENCRYPT_ROUND_FN(suffix, uintN_t, mixcol_macro) \
  552. static void aes_sliced_round_e_##suffix( \
  553. uintN_t output[8], const uintN_t input[8], const uintN_t roundkey[8]) \
  554. { \
  555. BITSLICED_SUBBYTES(output, input, uintN_t); \
  556. BITSLICED_SHIFTROWS(output, output, uintN_t); \
  557. mixcol_macro(output, output, uintN_t); \
  558. BITSLICED_ADD(output, output, roundkey); \
  559. }
  560. ENCRYPT_ROUND_FN(serial, uint16_t, BITSLICED_MIXCOLUMNS)
  561. ENCRYPT_ROUND_FN(serial_last, uint16_t, NO_MIXCOLUMNS)
  562. ENCRYPT_ROUND_FN(parallel, BignumInt, BITSLICED_MIXCOLUMNS)
  563. ENCRYPT_ROUND_FN(parallel_last, BignumInt, NO_MIXCOLUMNS)
  564. #define DECRYPT_ROUND_FN(suffix, uintN_t, mixcol_macro) \
  565. static void aes_sliced_round_d_##suffix( \
  566. uintN_t output[8], const uintN_t input[8], const uintN_t roundkey[8]) \
  567. { \
  568. BITSLICED_ADD(output, input, roundkey); \
  569. mixcol_macro(output, output, uintN_t); \
  570. BITSLICED_INVSUBBYTES(output, output, uintN_t); \
  571. BITSLICED_INVSHIFTROWS(output, output, uintN_t); \
  572. }
  573. #if 0 /* no cipher mode we support requires serial decryption */
  574. DECRYPT_ROUND_FN(serial, uint16_t, BITSLICED_INVMIXCOLUMNS)
  575. DECRYPT_ROUND_FN(serial_first, uint16_t, NO_MIXCOLUMNS)
  576. #endif
  577. DECRYPT_ROUND_FN(parallel, BignumInt, BITSLICED_INVMIXCOLUMNS)
  578. DECRYPT_ROUND_FN(parallel_first, BignumInt, NO_MIXCOLUMNS)
  579. #endif // WINSCP_VS
  580. /* -----
  581. * Key setup function.
  582. */
  583. typedef struct aes_sliced_key aes_sliced_key;
  584. struct aes_sliced_key {
  585. BignumInt roundkeys_parallel[MAXROUNDKEYS * 8];
  586. uint16_t roundkeys_serial[MAXROUNDKEYS * 8];
  587. unsigned rounds;
  588. };
  589. /*WINSCP static*/ void aes_sliced_key_setup(
  590. aes_sliced_key *sk, const void *vkey, size_t keybits)
  591. #ifndef WINSCP_VS
  592. ;
  593. #else
  594. {
  595. const unsigned char *key = (const unsigned char *)vkey;
  596. size_t key_words = keybits / 32;
  597. sk->rounds = key_words + 6;
  598. size_t sched_words = (sk->rounds + 1) * 4;
  599. unsigned rconpos = 0;
  600. uint16_t *outslices = sk->roundkeys_serial;
  601. unsigned outshift = 0;
  602. memset(sk->roundkeys_serial, 0, sizeof(sk->roundkeys_serial));
  603. uint8_t inblk[16];
  604. memset(inblk, 0, 16);
  605. uint16_t slices[8];
  606. for (size_t i = 0; i < sched_words; i++) {
  607. /*
  608. * Prepare a word of round key in the low 4 bits of each
  609. * integer in slices[].
  610. */
  611. if (i < key_words) {
  612. memcpy(inblk, key + 4*i, 4);
  613. TO_BITSLICES(slices, inblk, uint16_t, =, 0);
  614. } else {
  615. unsigned wordindex, bitshift;
  616. uint16_t *prevslices;
  617. /* Fetch the (i-1)th key word */
  618. wordindex = i-1;
  619. bitshift = 4 * (wordindex & 3);
  620. prevslices = sk->roundkeys_serial + 8 * (wordindex >> 2);
  621. for (size_t i = 0; i < 8; i++)
  622. slices[i] = prevslices[i] >> bitshift;
  623. /* Decide what we're doing in this expansion stage */
  624. bool rotate_and_round_constant = (i % key_words == 0);
  625. bool sub = rotate_and_round_constant ||
  626. (key_words == 8 && i % 8 == 4);
  627. if (rotate_and_round_constant) {
  628. for (size_t i = 0; i < 8; i++)
  629. slices[i] = ((slices[i] << 3) | (slices[i] >> 1)) & 0xF;
  630. }
  631. if (sub) {
  632. /* Apply the SubBytes transform to the key word. But
  633. * here we need to apply the _full_ SubBytes from the
  634. * spec, including the constant which our S-box leaves
  635. * out. */
  636. BITSLICED_SUBBYTES(slices, slices, uint16_t);
  637. slices[0] ^= 0xFFFF;
  638. slices[1] ^= 0xFFFF;
  639. slices[5] ^= 0xFFFF;
  640. slices[6] ^= 0xFFFF;
  641. }
  642. if (rotate_and_round_constant) {
  643. assert(rconpos < lenof(aes_key_setup_round_constants));
  644. uint8_t rcon = aes_key_setup_round_constants[rconpos++];
  645. for (size_t i = 0; i < 8; i++)
  646. slices[i] ^= 1 & (rcon >> i);
  647. }
  648. /* Combine with the (i-Nk)th key word */
  649. wordindex = i - key_words;
  650. bitshift = 4 * (wordindex & 3);
  651. prevslices = sk->roundkeys_serial + 8 * (wordindex >> 2);
  652. for (size_t i = 0; i < 8; i++)
  653. slices[i] ^= prevslices[i] >> bitshift;
  654. }
  655. /*
  656. * Now copy it into sk.
  657. */
  658. for (unsigned b = 0; b < 8; b++)
  659. outslices[b] |= (slices[b] & 0xF) << outshift;
  660. outshift += 4;
  661. if (outshift == 16) {
  662. outshift = 0;
  663. outslices += 8;
  664. }
  665. }
  666. smemclr(inblk, sizeof(inblk));
  667. smemclr(slices, sizeof(slices));
  668. /*
  669. * Add the S-box constant to every round key after the first one,
  670. * compensating for it being left out in the main cipher.
  671. */
  672. for (size_t i = 8; i < 8 * (sched_words/4); i += 8) {
  673. sk->roundkeys_serial[i+0] ^= 0xFFFF;
  674. sk->roundkeys_serial[i+1] ^= 0xFFFF;
  675. sk->roundkeys_serial[i+5] ^= 0xFFFF;
  676. sk->roundkeys_serial[i+6] ^= 0xFFFF;
  677. }
  678. /*
  679. * Replicate that set of round keys into larger integers for the
  680. * parallel versions of the cipher.
  681. */
  682. for (size_t i = 0; i < 8 * (sched_words / 4); i++) {
  683. sk->roundkeys_parallel[i] = sk->roundkeys_serial[i] *
  684. ((BignumInt)~(BignumInt)0 / 0xFFFF);
  685. }
  686. }
  687. #endif
  688. #ifdef WINSCP_VS
  689. /* -----
  690. * The full cipher primitive, including transforming the input and
  691. * output to/from bit-sliced form.
  692. */
  693. #define ENCRYPT_FN(suffix, uintN_t, nblocks) \
  694. static void aes_sliced_e_##suffix( \
  695. uint8_t *output, const uint8_t *input, const aes_sliced_key *sk) \
  696. { \
  697. uintN_t state[8]; \
  698. TO_BITSLICES(state, input, uintN_t, =, 0); \
  699. for (unsigned i = 1; i < nblocks; i++) { \
  700. input += 16; \
  701. TO_BITSLICES(state, input, uintN_t, |=, i*16); \
  702. } \
  703. const uintN_t *keys = sk->roundkeys_##suffix; \
  704. BITSLICED_ADD(state, state, keys); \
  705. keys += 8; \
  706. for (unsigned i = 0; i < sk->rounds-1; i++) { \
  707. aes_sliced_round_e_##suffix(state, state, keys); \
  708. keys += 8; \
  709. } \
  710. aes_sliced_round_e_##suffix##_last(state, state, keys); \
  711. for (unsigned i = 0; i < nblocks; i++) { \
  712. FROM_BITSLICES(output, state, i*16); \
  713. output += 16; \
  714. } \
  715. }
  716. #define DECRYPT_FN(suffix, uintN_t, nblocks) \
  717. static void aes_sliced_d_##suffix( \
  718. uint8_t *output, const uint8_t *input, const aes_sliced_key *sk) \
  719. { \
  720. uintN_t state[8]; \
  721. TO_BITSLICES(state, input, uintN_t, =, 0); \
  722. for (unsigned i = 1; i < nblocks; i++) { \
  723. input += 16; \
  724. TO_BITSLICES(state, input, uintN_t, |=, i*16); \
  725. } \
  726. const uintN_t *keys = sk->roundkeys_##suffix + 8*sk->rounds; \
  727. aes_sliced_round_d_##suffix##_first(state, state, keys); \
  728. keys -= 8; \
  729. for (unsigned i = 0; i < sk->rounds-1; i++) { \
  730. aes_sliced_round_d_##suffix(state, state, keys); \
  731. keys -= 8; \
  732. } \
  733. BITSLICED_ADD(state, state, keys); \
  734. for (unsigned i = 0; i < nblocks; i++) { \
  735. FROM_BITSLICES(output, state, i*16); \
  736. output += 16; \
  737. } \
  738. }
  739. ENCRYPT_FN(serial, uint16_t, 1)
  740. #if 0 /* no cipher mode we support requires serial decryption */
  741. DECRYPT_FN(serial, uint16_t, 1)
  742. #endif
  743. ENCRYPT_FN(parallel, BignumInt, SLICE_PARALLELISM)
  744. DECRYPT_FN(parallel, BignumInt, SLICE_PARALLELISM)
  745. #endif // WINSCP_VS
  746. /* -----
  747. * The SSH interface and the cipher modes.
  748. */
  749. #define SDCTR_WORDS (16 / BIGNUM_INT_BYTES)
  750. typedef struct aes_sw_context aes_sw_context;
  751. struct aes_sw_context {
  752. aes_sliced_key sk;
  753. union {
  754. struct {
  755. /* In CBC mode, the IV is just a copy of the last seen
  756. * cipher block. */
  757. uint8_t prevblk[16];
  758. } cbc;
  759. struct {
  760. /* In SDCTR mode, we keep the counter itself in a form
  761. * that's easy to increment. We also use the parallel
  762. * version of the core AES function, so we'll encrypt
  763. * multiple counter values in one go. That won't align
  764. * nicely with the sizes of data we're asked to encrypt,
  765. * so we must also store a cache of the last set of
  766. * keystream blocks we generated, and our current position
  767. * within that cache. */
  768. BignumInt counter[SDCTR_WORDS];
  769. uint8_t keystream[SLICE_PARALLELISM * 16];
  770. uint8_t *keystream_pos;
  771. } sdctr;
  772. struct {
  773. /* In GCM mode, the cipher preimage consists of three
  774. * sections: one fixed, one that increments per message
  775. * sent and MACed, and one that increments per cipher
  776. * block. */
  777. uint64_t msg_counter;
  778. uint32_t fixed_iv, block_counter;
  779. /* But we keep the precomputed keystream chunks just like
  780. * SDCTR mode. */
  781. uint8_t keystream[SLICE_PARALLELISM * 16];
  782. uint8_t *keystream_pos;
  783. } gcm;
  784. } iv;
  785. ssh_cipher ciph;
  786. };
  787. #ifndef WINSCP_VS
  788. static ssh_cipher *aes_sw_new(const ssh_cipheralg *alg)
  789. {
  790. aes_sw_context *ctx = snew(aes_sw_context);
  791. ctx->ciph.vt = alg;
  792. return &ctx->ciph;
  793. }
  794. static void aes_sw_free(ssh_cipher *ciph)
  795. {
  796. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  797. smemclr(ctx, sizeof(*ctx));
  798. sfree(ctx);
  799. }
  800. static void aes_sw_setkey(ssh_cipher *ciph, const void *vkey)
  801. {
  802. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  803. aes_sliced_key_setup(&ctx->sk, vkey, ctx->ciph.vt->real_keybits);
  804. }
  805. static void aes_sw_setiv_cbc(ssh_cipher *ciph, const void *iv)
  806. {
  807. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  808. memcpy(ctx->iv.cbc.prevblk, iv, 16);
  809. }
  810. static void aes_sw_setiv_sdctr(ssh_cipher *ciph, const void *viv)
  811. {
  812. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  813. const uint8_t *iv = (const uint8_t *)viv;
  814. /* Import the initial counter value into the internal representation */
  815. unsigned i; // WINSCP
  816. for (i = 0; i < SDCTR_WORDS; i++)
  817. ctx->iv.sdctr.counter[i] =
  818. GET_BIGNUMINT_MSB_FIRST(
  819. iv + 16 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES);
  820. /* Set keystream_pos to indicate that the keystream cache is
  821. * currently empty */
  822. ctx->iv.sdctr.keystream_pos =
  823. ctx->iv.sdctr.keystream + sizeof(ctx->iv.sdctr.keystream);
  824. }
  825. static void aes_sw_setiv_gcm(ssh_cipher *ciph, const void *viv)
  826. {
  827. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  828. const uint8_t *iv = (const uint8_t *)viv;
  829. ctx->iv.gcm.fixed_iv = GET_32BIT_MSB_FIRST(iv);
  830. ctx->iv.gcm.msg_counter = GET_64BIT_MSB_FIRST(iv + 4);
  831. ctx->iv.gcm.block_counter = 1;
  832. /* Set keystream_pos to indicate that the keystream cache is
  833. * currently empty */
  834. ctx->iv.gcm.keystream_pos =
  835. ctx->iv.gcm.keystream + sizeof(ctx->iv.gcm.keystream);
  836. }
  837. static void aes_sw_next_message_gcm(ssh_cipher *ciph)
  838. {
  839. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  840. ctx->iv.gcm.msg_counter++;
  841. ctx->iv.gcm.block_counter = 1;
  842. ctx->iv.gcm.keystream_pos =
  843. ctx->iv.gcm.keystream + sizeof(ctx->iv.gcm.keystream);
  844. }
  845. #endif
  846. typedef void (*aes_sw_fn)(uint32_t v[4], const uint32_t *keysched);
  847. #ifdef WINSCP_VS
  848. static inline void memxor16(void *vout, const void *vlhs, const void *vrhs)
  849. {
  850. uint8_t *out = (uint8_t *)vout;
  851. const uint8_t *lhs = (const uint8_t *)vlhs, *rhs = (const uint8_t *)vrhs;
  852. uint64_t w;
  853. w = GET_64BIT_LSB_FIRST(lhs);
  854. w ^= GET_64BIT_LSB_FIRST(rhs);
  855. PUT_64BIT_LSB_FIRST(out, w);
  856. w = GET_64BIT_LSB_FIRST(lhs + 8);
  857. w ^= GET_64BIT_LSB_FIRST(rhs + 8);
  858. PUT_64BIT_LSB_FIRST(out + 8, w);
  859. }
  860. static inline void aes_cbc_sw_encrypt(
  861. ssh_cipher *ciph, void *vblk, int blklen)
  862. {
  863. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  864. /*
  865. * CBC encryption has to be done serially, because the input to
  866. * each run of the cipher includes the output from the previous
  867. * run.
  868. */
  869. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  870. blk < finish; blk += 16) {
  871. /*
  872. * We use the IV array itself as the location for the
  873. * encryption, because there's no reason not to.
  874. */
  875. /* XOR the new plaintext block into the previous cipher block */
  876. memxor16(ctx->iv.cbc.prevblk, ctx->iv.cbc.prevblk, blk);
  877. /* Run the cipher over the result, which leaves it
  878. * conveniently already stored in ctx->iv */
  879. aes_sliced_e_serial(
  880. ctx->iv.cbc.prevblk, ctx->iv.cbc.prevblk, &ctx->sk);
  881. /* Copy it to the output location */
  882. memcpy(blk, ctx->iv.cbc.prevblk, 16);
  883. }
  884. }
  885. static inline void aes_cbc_sw_decrypt(
  886. ssh_cipher *ciph, void *vblk, int blklen)
  887. {
  888. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  889. uint8_t *blk = (uint8_t *)vblk;
  890. /*
  891. * CBC decryption can run in parallel, because all the
  892. * _ciphertext_ blocks are already available.
  893. */
  894. size_t blocks_remaining = blklen / 16;
  895. uint8_t data[SLICE_PARALLELISM * 16];
  896. /* Zeroing the data array is probably overcautious, but it avoids
  897. * technically undefined behaviour from leaving it uninitialised
  898. * if our very first iteration doesn't include enough cipher
  899. * blocks to populate it fully */
  900. memset(data, 0, sizeof(data));
  901. while (blocks_remaining > 0) {
  902. /* Number of blocks we'll handle in this iteration. If we're
  903. * dealing with fewer than the maximum, it doesn't matter -
  904. * it's harmless to run the full parallel cipher function
  905. * anyway. */
  906. size_t blocks = (blocks_remaining < SLICE_PARALLELISM ?
  907. blocks_remaining : SLICE_PARALLELISM);
  908. /* Parallel-decrypt the input, in a separate array so we still
  909. * have the cipher stream available for XORing. */
  910. memcpy(data, blk, 16 * blocks);
  911. aes_sliced_d_parallel(data, data, &ctx->sk);
  912. /* Write the output and update the IV */
  913. for (size_t i = 0; i < blocks; i++) {
  914. uint8_t *decrypted = data + 16*i;
  915. uint8_t *output = blk + 16*i;
  916. memxor16(decrypted, decrypted, ctx->iv.cbc.prevblk);
  917. memcpy(ctx->iv.cbc.prevblk, output, 16);
  918. memcpy(output, decrypted, 16);
  919. }
  920. /* Advance the input pointer. */
  921. blk += 16 * blocks;
  922. blocks_remaining -= blocks;
  923. }
  924. smemclr(data, sizeof(data));
  925. }
  926. static inline void aes_sdctr_sw(
  927. ssh_cipher *ciph, void *vblk, int blklen)
  928. {
  929. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  930. /*
  931. * SDCTR encrypt/decrypt loops round one block at a time XORing
  932. * the keystream into the user's data, and periodically has to run
  933. * a parallel encryption operation to get more keystream.
  934. */
  935. uint8_t *keystream_end =
  936. ctx->iv.sdctr.keystream + sizeof(ctx->iv.sdctr.keystream);
  937. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  938. blk < finish; blk += 16) {
  939. if (ctx->iv.sdctr.keystream_pos == keystream_end) {
  940. /*
  941. * Generate some keystream.
  942. */
  943. for (uint8_t *block = ctx->iv.sdctr.keystream;
  944. block < keystream_end; block += 16) {
  945. /* Format the counter value into the buffer. */
  946. for (unsigned i = 0; i < SDCTR_WORDS; i++)
  947. PUT_BIGNUMINT_MSB_FIRST(
  948. block + 16 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES,
  949. ctx->iv.sdctr.counter[i]);
  950. /* Increment the counter. */
  951. BignumCarry carry = 1;
  952. for (unsigned i = 0; i < SDCTR_WORDS; i++)
  953. BignumADC(ctx->iv.sdctr.counter[i], carry,
  954. ctx->iv.sdctr.counter[i], 0, carry);
  955. }
  956. /* Encrypt all those counter blocks. */
  957. aes_sliced_e_parallel(ctx->iv.sdctr.keystream,
  958. ctx->iv.sdctr.keystream, &ctx->sk);
  959. /* Reset keystream_pos to the start of the buffer. */
  960. ctx->iv.sdctr.keystream_pos = ctx->iv.sdctr.keystream;
  961. }
  962. memxor16(blk, blk, ctx->iv.sdctr.keystream_pos);
  963. ctx->iv.sdctr.keystream_pos += 16;
  964. }
  965. }
  966. static inline void aes_encrypt_ecb_block_sw(ssh_cipher *ciph, void *blk)
  967. {
  968. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  969. aes_sliced_e_serial(blk, blk, &ctx->sk);
  970. }
  971. static inline void aes_gcm_sw(
  972. ssh_cipher *ciph, void *vblk, int blklen)
  973. {
  974. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  975. /*
  976. * GCM encrypt/decrypt looks just like SDCTR, except that the
  977. * method of generating more keystream varies slightly.
  978. */
  979. uint8_t *keystream_end =
  980. ctx->iv.gcm.keystream + sizeof(ctx->iv.gcm.keystream);
  981. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  982. blk < finish; blk += 16) {
  983. if (ctx->iv.gcm.keystream_pos == keystream_end) {
  984. /*
  985. * Generate some keystream.
  986. */
  987. for (uint8_t *block = ctx->iv.gcm.keystream;
  988. block < keystream_end; block += 16) {
  989. /* Format the counter value into the buffer. */
  990. PUT_32BIT_MSB_FIRST(block, ctx->iv.gcm.fixed_iv);
  991. PUT_64BIT_MSB_FIRST(block + 4, ctx->iv.gcm.msg_counter);
  992. PUT_32BIT_MSB_FIRST(block + 12, ctx->iv.gcm.block_counter);
  993. /* Increment the counter. */
  994. ctx->iv.gcm.block_counter++;
  995. }
  996. /* Encrypt all those counter blocks. */
  997. aes_sliced_e_parallel(ctx->iv.gcm.keystream,
  998. ctx->iv.gcm.keystream, &ctx->sk);
  999. /* Reset keystream_pos to the start of the buffer. */
  1000. ctx->iv.gcm.keystream_pos = ctx->iv.gcm.keystream;
  1001. }
  1002. memxor16(blk, blk, ctx->iv.gcm.keystream_pos);
  1003. ctx->iv.gcm.keystream_pos += 16;
  1004. }
  1005. }
  1006. #define SW_ENC_DEC(len) \
  1007. /*WINSCP static*/ void aes##len##_sw_cbc_encrypt( \
  1008. ssh_cipher *ciph, void *vblk, int blklen) \
  1009. { aes_cbc_sw_encrypt(ciph, vblk, blklen); } \
  1010. /*WINSCP static*/ void aes##len##_sw_cbc_decrypt( \
  1011. ssh_cipher *ciph, void *vblk, int blklen) \
  1012. { aes_cbc_sw_decrypt(ciph, vblk, blklen); } \
  1013. /*WINSCP static*/ void aes##len##_sw_sdctr( \
  1014. ssh_cipher *ciph, void *vblk, int blklen) \
  1015. { aes_sdctr_sw(ciph, vblk, blklen); } \
  1016. /*WINSCP static*/ void aes##len##_sw_gcm( \
  1017. ssh_cipher *ciph, void *vblk, int blklen) \
  1018. { aes_gcm_sw(ciph, vblk, blklen); } \
  1019. /*WINSCP static*/ void aes##len##_sw_encrypt_ecb_block( \
  1020. ssh_cipher *ciph, void *vblk) \
  1021. { aes_encrypt_ecb_block_sw(ciph, vblk); }
  1022. #else // WINSCP_VS
  1023. #define SW_ENC_DEC(len) \
  1024. void aes##len##_sw_cbc_encrypt( \
  1025. ssh_cipher *ciph, void *vblk, int blklen); \
  1026. void aes##len##_sw_cbc_decrypt( \
  1027. ssh_cipher *ciph, void *vblk, int blklen); \
  1028. void aes##len##_sw_sdctr( \
  1029. ssh_cipher *ciph, void *vblk, int blklen); \
  1030. void aes##len##_sw_gcm( \
  1031. ssh_cipher *ciph, void *vblk, int blklen); \
  1032. void aes##len##_sw_encrypt_ecb_block( \
  1033. ssh_cipher *ciph, void *vblk); \
  1034. #endif // WINSCP_VS
  1035. SW_ENC_DEC(128)
  1036. SW_ENC_DEC(192)
  1037. SW_ENC_DEC(256)
  1038. #ifndef WINSCP_VS
  1039. AES_EXTRA(_sw);
  1040. AES_ALL_VTABLES(_sw, "unaccelerated");
  1041. #ifdef MPEXT
  1042. #include "puttyexp.h"
  1043. AESContext * aes_make_context()
  1044. {
  1045. ssh_cipher * cipher = ssh_cipher_new(&ssh_aes256_sdctr);
  1046. return cipher;
  1047. }
  1048. void aes_free_context(AESContext * ctx)
  1049. {
  1050. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1051. ssh_cipher_free(cipher);
  1052. }
  1053. void aes_iv(AESContext * ctx, const void * iv)
  1054. {
  1055. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1056. ssh_cipher_setiv(cipher, iv);
  1057. }
  1058. void call_aes_setup(AESContext * ctx, unsigned char * key, int keylen)
  1059. {
  1060. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1061. assert(keylen == 32);
  1062. ssh_cipher_setkey(cipher, key);
  1063. }
  1064. void call_aes_sdctr(unsigned char *blk, int len, void *ctx)
  1065. {
  1066. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1067. ssh_cipher_encrypt(cipher, blk, len);
  1068. }
  1069. #endif
  1070. #endif // WINSCP_VS