aes-sw.c 50 KB

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