sshaes.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. * sshaes.c - implementation of AES
  3. */
  4. #include <assert.h>
  5. #ifndef WINSCP_VS
  6. #include <stdlib.h>
  7. #endif
  8. #include "ssh.h"
  9. #include "mpint_i.h" /* we reuse the BignumInt system */
  10. /*
  11. * Start by deciding whether we can support hardware AES at all.
  12. */
  13. #define HW_AES_NONE 0
  14. #define HW_AES_NI 1
  15. #ifdef _FORCE_AES_NI
  16. # define HW_AES HW_AES_NI
  17. #elif defined(__clang__)
  18. # if __has_attribute(target) && __has_include(<wmmintrin.h>) && \
  19. (defined(__x86_64__) || defined(__i386))
  20. # define HW_AES HW_AES_NI
  21. # endif
  22. #elif defined(__GNUC__)
  23. # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) && \
  24. (defined(__x86_64__) || defined(__i386))
  25. # define HW_AES HW_AES_NI
  26. # endif
  27. #elif defined (_MSC_VER)
  28. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_FULL_VER >= 150030729
  29. # define HW_AES HW_AES_NI
  30. # endif
  31. #endif
  32. #if defined _FORCE_SOFTWARE_AES || !defined HW_AES
  33. # undef HW_AES
  34. # define HW_AES HW_AES_NONE
  35. #endif
  36. #if HW_AES == HW_AES_NI
  37. #define HW_NAME_SUFFIX " (AES-NI accelerated)"
  38. #else
  39. #define HW_NAME_SUFFIX " (!NONEXISTENT ACCELERATED VERSION!)"
  40. #endif
  41. /*
  42. * Vtable collection for AES. For each SSH-level cipher id (i.e.
  43. * combination of key length and cipher mode), we provide three
  44. * vtables: one for the pure software implementation, one using
  45. * hardware acceleration (if available), and a top-level one which is
  46. * never actually instantiated, and only contains a new() method whose
  47. * job is to decide whihc of the other two to return an actual
  48. * instance of.
  49. */
  50. static ssh2_cipher *aes_select(const ssh2_cipheralg *alg);
  51. static ssh2_cipher *aes_sw_new(const ssh2_cipheralg *alg);
  52. static void aes_sw_free(ssh2_cipher *);
  53. static void aes_sw_setiv_cbc(ssh2_cipher *, const void *iv);
  54. static void aes_sw_setiv_sdctr(ssh2_cipher *, const void *iv);
  55. static void aes_sw_setkey(ssh2_cipher *, const void *key);
  56. static ssh2_cipher *aes_hw_new(const ssh2_cipheralg *alg);
  57. static void aes_hw_free(ssh2_cipher *);
  58. static void aes_hw_setiv_cbc(ssh2_cipher *, const void *iv);
  59. static void aes_hw_setiv_sdctr(ssh2_cipher *, const void *iv);
  60. static void aes_hw_setkey(ssh2_cipher *, const void *key);
  61. struct aes_extra {
  62. const ssh2_cipheralg *sw, *hw;
  63. };
  64. #define VTABLES(cid, pid, bits, name, encsuffix, decsuffix, setiv) \
  65. static void cid##_sw##encsuffix(ssh2_cipher *, void *blk, int len); \
  66. static void cid##_sw##decsuffix(ssh2_cipher *, void *blk, int len); \
  67. const ssh2_cipheralg ssh_##cid##_sw = { \
  68. aes_sw_new, aes_sw_free, aes_sw_##setiv, aes_sw_setkey, \
  69. cid##_sw##encsuffix, cid##_sw##decsuffix, NULL, NULL, \
  70. pid, 16, bits, bits/8, 0, name /*WINSCP " (unaccelerated)" */, \
  71. NULL, NULL }; \
  72. \
  73. static void cid##_hw##encsuffix(ssh2_cipher *, void *blk, int len); \
  74. static void cid##_hw##decsuffix(ssh2_cipher *, void *blk, int len); \
  75. const ssh2_cipheralg ssh_##cid##_hw = { \
  76. aes_hw_new, aes_hw_free, aes_hw_##setiv, aes_hw_setkey, \
  77. cid##_hw##encsuffix, cid##_hw##decsuffix, NULL, NULL, \
  78. pid, 16, bits, bits/8, 0, name HW_NAME_SUFFIX, \
  79. NULL, NULL }; \
  80. \
  81. const struct aes_extra extra_##cid = { \
  82. &ssh_##cid##_sw, &ssh_##cid##_hw }; \
  83. \
  84. const ssh2_cipheralg ssh_##cid = { \
  85. aes_select, NULL, NULL, NULL, NULL, NULL, NULL, NULL, \
  86. pid, 16, bits, bits/8, 0, name " (dummy selector vtable)", \
  87. NULL, &extra_##cid }; \
  88. VTABLES(aes128_cbc, "aes128", 128, "AES-128 CBC", _encrypt,_decrypt,setiv_cbc)
  89. VTABLES(aes192_cbc, "aes192", 192, "AES-192 CBC", _encrypt,_decrypt,setiv_cbc)
  90. VTABLES(aes256_cbc, "aes256", 256, "AES-256 CBC", _encrypt,_decrypt,setiv_cbc)
  91. VTABLES(aes128_sdctr, "aes128-ctr", 128, "AES-128 SDCTR",,, setiv_sdctr)
  92. VTABLES(aes192_sdctr, "aes192-ctr", 192, "AES-192 SDCTR",,, setiv_sdctr)
  93. VTABLES(aes256_sdctr, "aes256-ctr", 256, "AES-256 SDCTR",,, setiv_sdctr)
  94. static const ssh2_cipheralg ssh_rijndael_lysator = {
  95. /* Same as aes256_cbc, but with a different protocol ID */
  96. aes_select, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  97. "[email protected]", 16, 256, 256/8, 0,
  98. "AES-256 CBC (dummy selector vtable)", NULL, &extra_aes256_cbc
  99. };
  100. static const ssh2_cipheralg *const aes_list[] = {
  101. &ssh_aes256_sdctr,
  102. &ssh_aes256_cbc,
  103. &ssh_rijndael_lysator,
  104. &ssh_aes192_sdctr,
  105. &ssh_aes192_cbc,
  106. &ssh_aes128_sdctr,
  107. &ssh_aes128_cbc,
  108. };
  109. const ssh2_ciphers ssh2_aes = { lenof(aes_list), aes_list };
  110. /*
  111. * The actual query function that asks if hardware acceleration is
  112. * available.
  113. */
  114. static bool aes_hw_available(void);
  115. /*
  116. * The top-level selection function, caching the results of
  117. * aes_hw_available() so it only has to run once.
  118. */
  119. static bool aes_hw_available_cached(void)
  120. {
  121. static bool initialised = false;
  122. static bool hw_available;
  123. if (!initialised)
  124. hw_available = aes_hw_available();
  125. return hw_available;
  126. }
  127. static ssh2_cipher *aes_select(const ssh2_cipheralg *alg)
  128. {
  129. const struct aes_extra *extra = (const struct aes_extra *)alg->extra;
  130. const ssh2_cipheralg *real_alg =
  131. aes_hw_available_cached() ? extra->hw : extra->sw;
  132. return ssh2_cipher_new(real_alg);
  133. }
  134. /* ----------------------------------------------------------------------
  135. * Definitions likely to be helpful to multiple implementations.
  136. */
  137. #define REP2(x) x x
  138. #define REP4(x) REP2(REP2(x))
  139. #define REP8(x) REP2(REP4(x))
  140. #define REP9(x) REP8(x) x
  141. #define REP11(x) REP8(x) REP2(x) x
  142. #define REP13(x) REP8(x) REP4(x) x
  143. static const uint8_t key_setup_round_constants[] = {
  144. /* The first few powers of X in GF(2^8), used during key setup.
  145. * This can safely be a lookup table without side channel risks,
  146. * because key setup iterates through it once in a standard way
  147. * regardless of the key. */
  148. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
  149. };
  150. #define MAXROUNDKEYS 15
  151. /* ----------------------------------------------------------------------
  152. * Software implementation of AES.
  153. *
  154. * This implementation uses a bit-sliced representation. Instead of
  155. * the obvious approach of storing the cipher state so that each byte
  156. * (or field element, or entry in the cipher matrix) occupies 8
  157. * contiguous bits in a machine integer somewhere, we organise the
  158. * cipher state as an array of 8 integers, in such a way that each
  159. * logical byte of the cipher state occupies one bit in each integer,
  160. * all at the same position. This allows us to do parallel logic on
  161. * all bytes of the state by doing bitwise operations between the 8
  162. * integers; in particular, the S-box (SubBytes) lookup is done this
  163. * way, which takes about 110 operations - but for those 110 bitwise
  164. * ops you get 64 S-box lookups, not just one.
  165. */
  166. #define SLICE_PARALLELISM (BIGNUM_INT_BYTES / 2)
  167. #ifdef BITSLICED_DEBUG
  168. /* Dump function that undoes the bitslicing transform, so you can see
  169. * the logical data represented by a set of slice words. */
  170. static inline void dumpslices_uint16_t(
  171. const char *prefix, const uint16_t slices[8])
  172. {
  173. printf("%-30s", prefix);
  174. for (unsigned byte = 0; byte < 16; byte++) {
  175. unsigned byteval = 0;
  176. for (unsigned bit = 0; bit < 8; bit++)
  177. byteval |= (1 & (slices[bit] >> byte)) << bit;
  178. printf("%02x", byteval);
  179. }
  180. printf("\n");
  181. }
  182. static inline void dumpslices_BignumInt(
  183. const char *prefix, const BignumInt slices[8])
  184. {
  185. printf("%-30s", prefix);
  186. for (unsigned iter = 0; iter < SLICE_PARALLELISM; iter++) {
  187. for (unsigned byte = 0; byte < 16; byte++) {
  188. unsigned byteval = 0;
  189. for (unsigned bit = 0; bit < 8; bit++)
  190. byteval |= (1 & (slices[bit] >> (iter*16+byte))) << bit;
  191. printf("%02x", byteval);
  192. }
  193. if (iter+1 < SLICE_PARALLELISM)
  194. printf(" ");
  195. }
  196. printf("\n");
  197. }
  198. #else
  199. #define dumpslices_uintN_t(prefix, slices) ((void)0)
  200. #define dumpslices_BignumInt(prefix, slices) ((void)0)
  201. #endif
  202. /* -----
  203. * Bit-slicing transformation: convert between an array of 16 uint8_t
  204. * and an array of 8 uint16_t, so as to interchange the bit index
  205. * within each element and the element index within the array. (That
  206. * is, bit j of input[i] == bit i of output[j].
  207. */
  208. #define SWAPWORDS(shift) do \
  209. { \
  210. uint64_t mask = ~(uint64_t)0 / ((1ULL << shift) + 1); \
  211. uint64_t diff = ((i0 >> shift) ^ i1) & mask; \
  212. i0 ^= diff << shift; \
  213. i1 ^= diff; \
  214. } while (0)
  215. #define SWAPINWORD(i, bigshift, smallshift) do \
  216. { \
  217. uint64_t mask = ~(uint64_t)0; \
  218. mask /= ((1ULL << bigshift) + 1); \
  219. mask /= ((1ULL << smallshift) + 1); \
  220. mask <<= smallshift; \
  221. { /*WINSCP*/ \
  222. unsigned shift = bigshift - smallshift; \
  223. uint64_t diff = ((i >> shift) ^ i) & mask; \
  224. i ^= diff ^ (diff << shift); \
  225. } /*WINSCP*/ \
  226. } while (0)
  227. #define TO_BITSLICES(slices, bytes, uintN_t, assign_op, shift) do \
  228. { \
  229. uint64_t i0 = GET_64BIT_LSB_FIRST(bytes); \
  230. uint64_t i1 = GET_64BIT_LSB_FIRST(bytes + 8); \
  231. SWAPINWORD(i0, 8, 1); \
  232. SWAPINWORD(i1, 8, 1); \
  233. SWAPINWORD(i0, 16, 2); \
  234. SWAPINWORD(i1, 16, 2); \
  235. SWAPINWORD(i0, 32, 4); \
  236. SWAPINWORD(i1, 32, 4); \
  237. SWAPWORDS(8); \
  238. slices[0] assign_op (uintN_t)((i0 >> 0) & 0xFFFF) << (shift); \
  239. slices[2] assign_op (uintN_t)((i0 >> 16) & 0xFFFF) << (shift); \
  240. slices[4] assign_op (uintN_t)((i0 >> 32) & 0xFFFF) << (shift); \
  241. slices[6] assign_op (uintN_t)((i0 >> 48) & 0xFFFF) << (shift); \
  242. slices[1] assign_op (uintN_t)((i1 >> 0) & 0xFFFF) << (shift); \
  243. slices[3] assign_op (uintN_t)((i1 >> 16) & 0xFFFF) << (shift); \
  244. slices[5] assign_op (uintN_t)((i1 >> 32) & 0xFFFF) << (shift); \
  245. slices[7] assign_op (uintN_t)((i1 >> 48) & 0xFFFF) << (shift); \
  246. } while (0)
  247. #define FROM_BITSLICES(bytes, slices, shift) do \
  248. { \
  249. uint64_t i1 = ((slices[7] >> (shift)) & 0xFFFF); \
  250. i1 = (i1 << 16) | ((slices[5] >> (shift)) & 0xFFFF); \
  251. i1 = (i1 << 16) | ((slices[3] >> (shift)) & 0xFFFF); \
  252. i1 = (i1 << 16) | ((slices[1] >> (shift)) & 0xFFFF); \
  253. { /*WINSCP*/ \
  254. uint64_t i0 = ((slices[6] >> (shift)) & 0xFFFF); \
  255. i0 = (i0 << 16) | ((slices[4] >> (shift)) & 0xFFFF); \
  256. i0 = (i0 << 16) | ((slices[2] >> (shift)) & 0xFFFF); \
  257. i0 = (i0 << 16) | ((slices[0] >> (shift)) & 0xFFFF); \
  258. SWAPWORDS(8); \
  259. SWAPINWORD(i0, 32, 4); \
  260. SWAPINWORD(i1, 32, 4); \
  261. SWAPINWORD(i0, 16, 2); \
  262. SWAPINWORD(i1, 16, 2); \
  263. SWAPINWORD(i0, 8, 1); \
  264. SWAPINWORD(i1, 8, 1); \
  265. PUT_64BIT_LSB_FIRST(bytes, i0); \
  266. PUT_64BIT_LSB_FIRST((bytes) + 8, i1); \
  267. } /*WINSCP*/ \
  268. } while (0)
  269. /* -----
  270. * Some macros that will be useful repeatedly.
  271. */
  272. /* Iterate a unary transformation over all 8 slices. */
  273. #define ITERATE(MACRO, output, input, uintN_t) do \
  274. { \
  275. MACRO(output[0], input[0], uintN_t); \
  276. MACRO(output[1], input[1], uintN_t); \
  277. MACRO(output[2], input[2], uintN_t); \
  278. MACRO(output[3], input[3], uintN_t); \
  279. MACRO(output[4], input[4], uintN_t); \
  280. MACRO(output[5], input[5], uintN_t); \
  281. MACRO(output[6], input[6], uintN_t); \
  282. MACRO(output[7], input[7], uintN_t); \
  283. } while (0)
  284. /* Simply add (i.e. XOR) two whole sets of slices together. */
  285. #define BITSLICED_ADD(output, lhs, rhs) do \
  286. { \
  287. output[0] = lhs[0] ^ rhs[0]; \
  288. output[1] = lhs[1] ^ rhs[1]; \
  289. output[2] = lhs[2] ^ rhs[2]; \
  290. output[3] = lhs[3] ^ rhs[3]; \
  291. output[4] = lhs[4] ^ rhs[4]; \
  292. output[5] = lhs[5] ^ rhs[5]; \
  293. output[6] = lhs[6] ^ rhs[6]; \
  294. output[7] = lhs[7] ^ rhs[7]; \
  295. } while (0)
  296. /* -----
  297. * The AES S-box, in pure bitwise logic so that it can be run in
  298. * parallel on whole words full of bit-sliced field elements.
  299. *
  300. * Source: 'A new combinational logic minimization technique with
  301. * applications to cryptology', https://eprint.iacr.org/2009/191
  302. */
  303. /* Initial linear transformation for the forward S-box, from Fig 2 of
  304. * the paper. */
  305. #define SBOX_FORWARD_TOP_TRANSFORM(input, uintN_t) \
  306. uintN_t y14 = input[4] ^ input[2]; \
  307. uintN_t y13 = input[7] ^ input[1]; \
  308. uintN_t y9 = input[7] ^ input[4]; \
  309. uintN_t y8 = input[7] ^ input[2]; \
  310. uintN_t t0 = input[6] ^ input[5]; \
  311. uintN_t y1 = t0 ^ input[0]; \
  312. uintN_t y4 = y1 ^ input[4]; \
  313. uintN_t y12 = y13 ^ y14; \
  314. uintN_t y2 = y1 ^ input[7]; \
  315. uintN_t y5 = y1 ^ input[1]; \
  316. uintN_t y3 = y5 ^ y8; \
  317. uintN_t t1 = input[3] ^ y12; \
  318. uintN_t y15 = t1 ^ input[2]; \
  319. uintN_t y20 = t1 ^ input[6]; \
  320. uintN_t y6 = y15 ^ input[0]; \
  321. uintN_t y10 = y15 ^ t0; \
  322. uintN_t y11 = y20 ^ y9; \
  323. uintN_t y7 = input[0] ^ y11; \
  324. uintN_t y17 = y10 ^ y11; \
  325. uintN_t y19 = y10 ^ y8; \
  326. uintN_t y16 = t0 ^ y11; \
  327. uintN_t y21 = y13 ^ y16; \
  328. uintN_t y18 = input[7] ^ y16; \
  329. /* Make a copy of input[0] under a new name, because the core
  330. * will refer to it, and in the inverse version of the S-box
  331. * the corresponding value will be one of the calculated ones
  332. * and not in input[0] itself. */ \
  333. uintN_t i0 = input[0]; \
  334. /* end */
  335. /* Core nonlinear component, from Fig 3 of the paper. */
  336. #define SBOX_CORE(uintN_t) \
  337. uintN_t t2 = y12 & y15; \
  338. uintN_t t3 = y3 & y6; \
  339. uintN_t t4 = t3 ^ t2; \
  340. uintN_t t5 = y4 & i0; \
  341. uintN_t t6 = t5 ^ t2; \
  342. uintN_t t7 = y13 & y16; \
  343. uintN_t t8 = y5 & y1; \
  344. uintN_t t9 = t8 ^ t7; \
  345. uintN_t t10 = y2 & y7; \
  346. uintN_t t11 = t10 ^ t7; \
  347. uintN_t t12 = y9 & y11; \
  348. uintN_t t13 = y14 & y17; \
  349. uintN_t t14 = t13 ^ t12; \
  350. uintN_t t15 = y8 & y10; \
  351. uintN_t t16 = t15 ^ t12; \
  352. uintN_t t17 = t4 ^ t14; \
  353. uintN_t t18 = t6 ^ t16; \
  354. uintN_t t19 = t9 ^ t14; \
  355. uintN_t t20 = t11 ^ t16; \
  356. uintN_t t21 = t17 ^ y20; \
  357. uintN_t t22 = t18 ^ y19; \
  358. uintN_t t23 = t19 ^ y21; \
  359. uintN_t t24 = t20 ^ y18; \
  360. uintN_t t25 = t21 ^ t22; \
  361. uintN_t t26 = t21 & t23; \
  362. uintN_t t27 = t24 ^ t26; \
  363. uintN_t t28 = t25 & t27; \
  364. uintN_t t29 = t28 ^ t22; \
  365. uintN_t t30 = t23 ^ t24; \
  366. uintN_t t31 = t22 ^ t26; \
  367. uintN_t t32 = t31 & t30; \
  368. uintN_t t33 = t32 ^ t24; \
  369. uintN_t t34 = t23 ^ t33; \
  370. uintN_t t35 = t27 ^ t33; \
  371. uintN_t t36 = t24 & t35; \
  372. uintN_t t37 = t36 ^ t34; \
  373. uintN_t t38 = t27 ^ t36; \
  374. uintN_t t39 = t29 & t38; \
  375. uintN_t t40 = t25 ^ t39; \
  376. uintN_t t41 = t40 ^ t37; \
  377. uintN_t t42 = t29 ^ t33; \
  378. uintN_t t43 = t29 ^ t40; \
  379. uintN_t t44 = t33 ^ t37; \
  380. uintN_t t45 = t42 ^ t41; \
  381. uintN_t z0 = t44 & y15; \
  382. uintN_t z1 = t37 & y6; \
  383. uintN_t z2 = t33 & i0; \
  384. uintN_t z3 = t43 & y16; \
  385. uintN_t z4 = t40 & y1; \
  386. uintN_t z5 = t29 & y7; \
  387. uintN_t z6 = t42 & y11; \
  388. uintN_t z7 = t45 & y17; \
  389. uintN_t z8 = t41 & y10; \
  390. uintN_t z9 = t44 & y12; \
  391. uintN_t z10 = t37 & y3; \
  392. uintN_t z11 = t33 & y4; \
  393. uintN_t z12 = t43 & y13; \
  394. uintN_t z13 = t40 & y5; \
  395. uintN_t z14 = t29 & y2; \
  396. uintN_t z15 = t42 & y9; \
  397. uintN_t z16 = t45 & y14; \
  398. uintN_t z17 = t41 & y8; \
  399. /* end */
  400. /* Final linear transformation for the forward S-box, from Fig 4 of
  401. * the paper. */
  402. #define SBOX_FORWARD_BOTTOM_TRANSFORM(output, uintN_t) \
  403. uintN_t t46 = z15 ^ z16; \
  404. uintN_t t47 = z10 ^ z11; \
  405. uintN_t t48 = z5 ^ z13; \
  406. uintN_t t49 = z9 ^ z10; \
  407. uintN_t t50 = z2 ^ z12; \
  408. uintN_t t51 = z2 ^ z5; \
  409. uintN_t t52 = z7 ^ z8; \
  410. uintN_t t53 = z0 ^ z3; \
  411. uintN_t t54 = z6 ^ z7; \
  412. uintN_t t55 = z16 ^ z17; \
  413. uintN_t t56 = z12 ^ t48; \
  414. uintN_t t57 = t50 ^ t53; \
  415. uintN_t t58 = z4 ^ t46; \
  416. uintN_t t59 = z3 ^ t54; \
  417. uintN_t t60 = t46 ^ t57; \
  418. uintN_t t61 = z14 ^ t57; \
  419. uintN_t t62 = t52 ^ t58; \
  420. uintN_t t63 = t49 ^ t58; \
  421. uintN_t t64 = z4 ^ t59; \
  422. uintN_t t65 = t61 ^ t62; \
  423. uintN_t t66 = z1 ^ t63; \
  424. output[7] = t59 ^ t63; \
  425. output[1] = ~(t56 ^ t62); \
  426. output[0] = ~(t48 ^ t60); \
  427. { /*WINSCP*/ \
  428. uintN_t t67 = t64 ^ t65; \
  429. output[4] = t53 ^ t66; \
  430. output[3] = t51 ^ t66; \
  431. output[2] = t47 ^ t65; \
  432. output[6] = ~(t64 ^ output[4]); \
  433. output[5] = ~(t55 ^ t67); \
  434. } /*WINSCP*/ \
  435. /* end */
  436. #define BITSLICED_SUBBYTES(output, input, uintN_t) do { \
  437. SBOX_FORWARD_TOP_TRANSFORM(input, uintN_t); \
  438. { /*WINSCP*/ \
  439. SBOX_CORE(uintN_t); \
  440. { /*WINSCP*/ \
  441. SBOX_FORWARD_BOTTOM_TRANSFORM(output, uintN_t); \
  442. } /*WINSCP*/ \
  443. } /*WINSCP*/ \
  444. } while (0)
  445. /*
  446. * Initial and final linear transformations for the backward S-box. I
  447. * generated these myself, by implementing the linear-transform
  448. * optimisation algorithm in the paper, and applying it to the
  449. * matrices calculated by _their_ top and bottom transformations, pre-
  450. * and post-multiplied as appropriate by the linear map in the inverse
  451. * S_box.
  452. */
  453. #define SBOX_BACKWARD_TOP_TRANSFORM(input, uintN_t) \
  454. /* Initial subtraction of the constant */ \
  455. uintN_t iv0 = ~input[0], iv1 = ~input[1]; \
  456. uintN_t iv5 = ~input[5], iv6 = ~input[6]; \
  457. \
  458. uintN_t y5 = input[4] ^ iv6; \
  459. uintN_t y19 = input[3] ^ iv0; \
  460. uintN_t itmp8 = y5 ^ iv0; \
  461. uintN_t y4 = itmp8 ^ iv1; \
  462. uintN_t y9 = input[4] ^ input[3]; \
  463. uintN_t y2 = y9 ^ y4; \
  464. uintN_t itmp9 = y2 ^ input[7]; \
  465. uintN_t y1 = y9 ^ iv0; \
  466. uintN_t y6 = y5 ^ input[7]; \
  467. uintN_t y18 = y9 ^ iv5; \
  468. uintN_t y7 = y18 ^ y2; \
  469. uintN_t y16 = y7 ^ y1; \
  470. uintN_t y21 = y7 ^ iv1; \
  471. uintN_t y3 = input[4] ^ input[7]; \
  472. uintN_t y13 = y16 ^ y21; \
  473. uintN_t y8 = input[4] ^ y6; \
  474. uintN_t y10 = y8 ^ y19; \
  475. uintN_t y14 = y8 ^ y9; \
  476. uintN_t y20 = itmp9 ^ input[2]; \
  477. uintN_t y11 = y9 ^ y20; \
  478. uintN_t i0 = y11 ^ y7; \
  479. uintN_t y15 = i0 ^ y6; \
  480. uintN_t y17 = y16 ^ y15; \
  481. uintN_t y12 = itmp9 ^ input[3]; \
  482. /* end */
  483. #define SBOX_BACKWARD_BOTTOM_TRANSFORM(output, uintN_t) \
  484. uintN_t otmp18 = z15 ^ z6; \
  485. uintN_t otmp19 = z13 ^ otmp18; \
  486. uintN_t otmp20 = z12 ^ otmp19; \
  487. uintN_t otmp21 = z16 ^ otmp20; \
  488. uintN_t otmp22 = z8 ^ otmp21; \
  489. uintN_t otmp23 = z0 ^ otmp22; \
  490. uintN_t otmp24 = otmp22 ^ z3; \
  491. uintN_t otmp25 = otmp24 ^ z4; \
  492. uintN_t otmp26 = otmp25 ^ z2; \
  493. uintN_t otmp27 = z1 ^ otmp26; \
  494. uintN_t otmp28 = z14 ^ otmp27; \
  495. uintN_t otmp29 = otmp28 ^ z10; \
  496. output[4] = z2 ^ otmp23; \
  497. output[7] = z5 ^ otmp24; \
  498. { /*WINSCP*/ \
  499. uintN_t otmp30 = z11 ^ otmp29; \
  500. output[5] = z13 ^ otmp30; \
  501. { /*WINSCP*/ \
  502. uintN_t otmp31 = otmp25 ^ z8; \
  503. output[1] = z7 ^ otmp31; \
  504. { /*WINSCP*/ \
  505. uintN_t otmp32 = z11 ^ z9; \
  506. uintN_t otmp33 = z17 ^ otmp32; \
  507. uintN_t otmp34 = otmp30 ^ otmp33; \
  508. output[0] = z15 ^ otmp33; \
  509. { /*WINSCP*/ \
  510. uintN_t otmp35 = z12 ^ otmp34; \
  511. output[6] = otmp35 ^ z16; \
  512. { /*WINSCP*/ \
  513. uintN_t otmp36 = z1 ^ otmp23; \
  514. uintN_t otmp37 = z5 ^ otmp36; \
  515. output[2] = z4 ^ otmp37; \
  516. { /*WINSCP*/ \
  517. uintN_t otmp38 = z11 ^ output[1]; \
  518. uintN_t otmp39 = z2 ^ otmp38; \
  519. uintN_t otmp40 = z17 ^ otmp39; \
  520. uintN_t otmp41 = z0 ^ otmp40; \
  521. uintN_t otmp42 = z5 ^ otmp41; \
  522. uintN_t otmp43 = otmp42 ^ z10; \
  523. uintN_t otmp44 = otmp43 ^ z3; \
  524. output[3] = otmp44 ^ z16; \
  525. } /*WINSCP*/ \
  526. } /*WINSCP*/ \
  527. } /*WINSCP*/ \
  528. } /*WINSCP*/ \
  529. } /*WINSCP*/ \
  530. } /*WINSCP*/ \
  531. /* end */
  532. #define BITSLICED_INVSUBBYTES(output, input, uintN_t) do { \
  533. SBOX_BACKWARD_TOP_TRANSFORM(input, uintN_t); \
  534. { /*WINSCP*/ \
  535. SBOX_CORE(uintN_t); \
  536. { /*WINSCP*/ \
  537. SBOX_BACKWARD_BOTTOM_TRANSFORM(output, uintN_t); \
  538. } /*WINSCP*/ \
  539. } /*WINSCP*/ \
  540. } while (0)
  541. /* -----
  542. * The ShiftRows transformation. This operates independently on each
  543. * bit slice.
  544. */
  545. #define SINGLE_BITSLICE_SHIFTROWS(output, input, uintN_t) do \
  546. { \
  547. uintN_t mask, mask2, mask3, diff, x = (input); \
  548. /* Rotate rows 2 and 3 by 16 bits */ \
  549. mask = 0x00CC * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  550. diff = ((x >> 8) ^ x) & mask; \
  551. x ^= diff ^ (diff << 8); \
  552. /* Rotate rows 1 and 3 by 8 bits */ \
  553. mask = 0x0AAA * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  554. mask2 = 0xA000 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  555. mask3 = 0x5555 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  556. x = ((x >> 4) & mask) | ((x << 12) & mask2) | (x & mask3); \
  557. /* Write output */ \
  558. (output) = x; \
  559. } while (0)
  560. #define SINGLE_BITSLICE_INVSHIFTROWS(output, input, uintN_t) do \
  561. { \
  562. uintN_t mask, mask2, mask3, diff, x = (input); \
  563. /* Rotate rows 2 and 3 by 16 bits */ \
  564. mask = 0x00CC * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  565. diff = ((x >> 8) ^ x) & mask; \
  566. x ^= diff ^ (diff << 8); \
  567. /* Rotate rows 1 and 3 by 8 bits, the opposite way to ShiftRows */ \
  568. mask = 0x000A * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  569. mask2 = 0xAAA0 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  570. mask3 = 0x5555 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  571. x = ((x >> 12) & mask) | ((x << 4) & mask2) | (x & mask3); \
  572. /* Write output */ \
  573. (output) = x; \
  574. } while (0)
  575. #define BITSLICED_SHIFTROWS(output, input, uintN_t) do \
  576. { \
  577. ITERATE(SINGLE_BITSLICE_SHIFTROWS, output, input, uintN_t); \
  578. } while (0)
  579. #define BITSLICED_INVSHIFTROWS(output, input, uintN_t) do \
  580. { \
  581. ITERATE(SINGLE_BITSLICE_INVSHIFTROWS, output, input, uintN_t); \
  582. } while (0)
  583. /* -----
  584. * The MixColumns transformation. This has to operate on all eight bit
  585. * slices at once, and also passes data back and forth between the
  586. * bits in an adjacent group of 4 within each slice.
  587. *
  588. * Notation: let F = GF(2)[X]/<X^8+X^4+X^3+X+1> be the finite field
  589. * used in AES, and let R = F[Y]/<Y^4+1> be the ring whose elements
  590. * represent the possible contents of a column of the matrix. I use X
  591. * and Y below in those senses, i.e. X is the value in F that
  592. * represents the byte 0x02, and Y is the value in R that cycles the
  593. * four bytes around by one if you multiply by it.
  594. */
  595. /* Multiply every column by Y^3, i.e. cycle it round one place to the
  596. * right. Operates on one bit slice at a time; you have to wrap it in
  597. * ITERATE to affect all the data at once. */
  598. #define BITSLICED_MUL_BY_Y3(output, input, uintN_t) do \
  599. { \
  600. uintN_t mask, mask2, x; \
  601. mask = 0x8 * (((uintN_t)~(uintN_t)0) / 0xF); \
  602. mask2 = 0x7 * (((uintN_t)~(uintN_t)0) / 0xF); \
  603. x = input; \
  604. output = ((x << 3) & mask) ^ ((x >> 1) & mask2); \
  605. } while (0)
  606. /* Multiply every column by Y^2. */
  607. #define BITSLICED_MUL_BY_Y2(output, input, uintN_t) do \
  608. { \
  609. uintN_t mask, mask2, x; \
  610. mask = 0xC * (((uintN_t)~(uintN_t)0) / 0xF); \
  611. mask2 = 0x3 * (((uintN_t)~(uintN_t)0) / 0xF); \
  612. x = input; \
  613. output = ((x << 2) & mask) ^ ((x >> 2) & mask2); \
  614. } while (0)
  615. #define BITSLICED_MUL_BY_1_Y3(output, input, uintN_t) do \
  616. { \
  617. uintN_t tmp = input; \
  618. BITSLICED_MUL_BY_Y3(tmp, input, uintN_t); \
  619. output = input ^ tmp; \
  620. } while (0)
  621. /* Multiply every column by 1+Y^2. */
  622. #define BITSLICED_MUL_BY_1_Y2(output, input, uintN_t) do \
  623. { \
  624. uintN_t tmp = input; \
  625. BITSLICED_MUL_BY_Y2(tmp, input, uintN_t); \
  626. output = input ^ tmp; \
  627. } while (0)
  628. /* Multiply every field element by X. This has to feed data between
  629. * slices, so it does the whole job in one go without needing ITERATE. */
  630. #define BITSLICED_MUL_BY_X(output, input, uintN_t) do \
  631. { \
  632. uintN_t bit7 = input[7]; \
  633. output[7] = input[6]; \
  634. output[6] = input[5]; \
  635. output[5] = input[4]; \
  636. output[4] = input[3] ^ bit7; \
  637. output[3] = input[2] ^ bit7; \
  638. output[2] = input[1]; \
  639. output[1] = input[0] ^ bit7; \
  640. output[0] = bit7; \
  641. } while (0)
  642. /*
  643. * The MixColumns constant is
  644. * M = X + Y + Y^2 + (X+1)Y^3
  645. * which we construct by rearranging it into
  646. * M = 1 + (1+Y^3) [ X + (1+Y^2) ]
  647. */
  648. #define BITSLICED_MIXCOLUMNS(output, input, uintN_t) do \
  649. { \
  650. uintN_t a[8], aX[8], b[8]; \
  651. /* a = input * (1+Y^3) */ \
  652. ITERATE(BITSLICED_MUL_BY_1_Y3, a, input, uintN_t); \
  653. /* aX = a * X */ \
  654. BITSLICED_MUL_BY_X(aX, a, uintN_t); \
  655. /* b = a * (1+Y^2) = input * (1+Y+Y^2+Y^3) */ \
  656. ITERATE(BITSLICED_MUL_BY_1_Y2, b, a, uintN_t); \
  657. /* output = input + aX + b (reusing a as a temp */ \
  658. BITSLICED_ADD(a, aX, b); \
  659. BITSLICED_ADD(output, input, a); \
  660. } while (0)
  661. /*
  662. * The InvMixColumns constant, written out longhand, is
  663. * I = (X^3+X^2+X) + (X^3+1)Y + (X^3+X^2+1)Y^2 + (X^3+X+1)Y^3
  664. * We represent this as
  665. * I = (X^3+X^2+X+1)(Y^3+Y^2+Y+1) + 1 + X(Y+Y^2) + X^2(Y+Y^3)
  666. */
  667. #define BITSLICED_INVMIXCOLUMNS(output, input, uintN_t) do \
  668. { \
  669. /* We need input * X^i for i=1,...,3 */ \
  670. uintN_t X[8], X2[8], X3[8]; \
  671. BITSLICED_MUL_BY_X(X, input, uintN_t); \
  672. BITSLICED_MUL_BY_X(X2, X, uintN_t); \
  673. BITSLICED_MUL_BY_X(X3, X2, uintN_t); \
  674. /* Sum them all and multiply by 1+Y+Y^2+Y^3. */ \
  675. { /*WINSCP*/ \
  676. uintN_t S[8]; \
  677. BITSLICED_ADD(S, input, X); \
  678. BITSLICED_ADD(S, S, X2); \
  679. BITSLICED_ADD(S, S, X3); \
  680. ITERATE(BITSLICED_MUL_BY_1_Y3, S, S, uintN_t); \
  681. ITERATE(BITSLICED_MUL_BY_1_Y2, S, S, uintN_t); \
  682. /* Compute the X(Y+Y^2) term. */ \
  683. { /*WINSCP*/ \
  684. uintN_t A[8]; \
  685. ITERATE(BITSLICED_MUL_BY_1_Y3, A, X, uintN_t); \
  686. ITERATE(BITSLICED_MUL_BY_Y2, A, A, uintN_t); \
  687. /* Compute the X^2(Y+Y^3) term. */ \
  688. { /*WINSCP*/ \
  689. uintN_t B[8]; \
  690. ITERATE(BITSLICED_MUL_BY_1_Y2, B, X2, uintN_t); \
  691. ITERATE(BITSLICED_MUL_BY_Y3, B, B, uintN_t); \
  692. /* And add all the pieces together. */ \
  693. BITSLICED_ADD(S, S, input); \
  694. BITSLICED_ADD(S, S, A); \
  695. BITSLICED_ADD(output, S, B); \
  696. } /*WINSCP*/ \
  697. } /*WINSCP*/ \
  698. } /*WINSCP*/ \
  699. } while (0)
  700. /* -----
  701. * Put it all together into a cipher round.
  702. */
  703. /* Dummy macro to get rid of the MixColumns in the final round. */
  704. #define NO_MIXCOLUMNS(out, in, uintN_t) do {} while (0)
  705. #define ENCRYPT_ROUND_FN(suffix, uintN_t, mixcol_macro) \
  706. static void aes_sliced_round_e_##suffix( \
  707. uintN_t output[8], const uintN_t input[8], const uintN_t roundkey[8]) \
  708. { \
  709. BITSLICED_SUBBYTES(output, input, uintN_t); \
  710. BITSLICED_SHIFTROWS(output, output, uintN_t); \
  711. mixcol_macro(output, output, uintN_t); \
  712. BITSLICED_ADD(output, output, roundkey); \
  713. }
  714. ENCRYPT_ROUND_FN(serial, uint16_t, BITSLICED_MIXCOLUMNS)
  715. ENCRYPT_ROUND_FN(serial_last, uint16_t, NO_MIXCOLUMNS)
  716. ENCRYPT_ROUND_FN(parallel, BignumInt, BITSLICED_MIXCOLUMNS)
  717. ENCRYPT_ROUND_FN(parallel_last, BignumInt, NO_MIXCOLUMNS)
  718. #define DECRYPT_ROUND_FN(suffix, uintN_t, mixcol_macro) \
  719. static void aes_sliced_round_d_##suffix( \
  720. uintN_t output[8], const uintN_t input[8], const uintN_t roundkey[8]) \
  721. { \
  722. BITSLICED_ADD(output, input, roundkey); \
  723. mixcol_macro(output, output, uintN_t); \
  724. BITSLICED_INVSUBBYTES(output, output, uintN_t); \
  725. BITSLICED_INVSHIFTROWS(output, output, uintN_t); \
  726. }
  727. #if 0 /* no cipher mode we support requires serial decryption */
  728. DECRYPT_ROUND_FN(serial, uint16_t, BITSLICED_INVMIXCOLUMNS)
  729. DECRYPT_ROUND_FN(serial_first, uint16_t, NO_MIXCOLUMNS)
  730. #endif
  731. DECRYPT_ROUND_FN(parallel, BignumInt, BITSLICED_INVMIXCOLUMNS)
  732. DECRYPT_ROUND_FN(parallel_first, BignumInt, NO_MIXCOLUMNS)
  733. /* -----
  734. * Key setup function.
  735. */
  736. typedef struct aes_sliced_key aes_sliced_key;
  737. struct aes_sliced_key {
  738. BignumInt roundkeys_parallel[MAXROUNDKEYS * 8];
  739. uint16_t roundkeys_serial[MAXROUNDKEYS * 8];
  740. unsigned rounds;
  741. };
  742. static void aes_sliced_key_setup(
  743. aes_sliced_key *sk, const void *vkey, size_t keybits)
  744. {
  745. const unsigned char *key = (const unsigned char *)vkey;
  746. size_t key_words = keybits / 32;
  747. sk->rounds = key_words + 6;
  748. { // WINSCP
  749. size_t sched_words = (sk->rounds + 1) * 4;
  750. unsigned rconpos = 0;
  751. uint16_t *outslices = sk->roundkeys_serial;
  752. unsigned outshift = 0;
  753. memset(sk->roundkeys_serial, 0, sizeof(sk->roundkeys_serial));
  754. { // WINSCP
  755. uint8_t inblk[16];
  756. memset(inblk, 0, 16);
  757. { // WINSCP
  758. uint16_t slices[8];
  759. size_t i; // WINSCP
  760. for (i = 0; i < sched_words; i++) {
  761. /*
  762. * Prepare a word of round key in the low 4 bits of each
  763. * integer in slices[].
  764. */
  765. if (i < key_words) {
  766. memcpy(inblk, key + 4*i, 4);
  767. TO_BITSLICES(slices, inblk, uint16_t, =, 0);
  768. } else {
  769. unsigned wordindex, bitshift;
  770. uint16_t *prevslices;
  771. size_t i2; // WINSCP
  772. /* Fetch the (i-1)th key word */
  773. wordindex = i-1;
  774. bitshift = 4 * (wordindex & 3);
  775. prevslices = sk->roundkeys_serial + 8 * (wordindex >> 2);
  776. for (i2 = 0; i2 < 8; i2++)
  777. slices[i2] = prevslices[i2] >> bitshift;
  778. /* Decide what we're doing in this expansion stage */
  779. { // WINSCP
  780. bool rotate_and_round_constant = (i % key_words == 0);
  781. bool sub = rotate_and_round_constant ||
  782. (key_words == 8 && i % 8 == 4);
  783. if (rotate_and_round_constant) {
  784. for (i2 = 0; i2 < 8; i2++)
  785. slices[i2] = ((slices[i2] << 3) | (slices[i2] >> 1)) & 0xF;
  786. }
  787. if (sub) {
  788. BITSLICED_SUBBYTES(slices, slices, uint16_t);
  789. }
  790. if (rotate_and_round_constant) {
  791. pinitassert(rconpos < lenof(key_setup_round_constants));
  792. uint8_t rcon = key_setup_round_constants[rconpos++];
  793. for (i2 = 0; i2 < 8; i2++)
  794. slices[i2] ^= 1 & (rcon >> i2);
  795. }
  796. /* Combine with the (i-Nk)th key word */
  797. wordindex = i - key_words;
  798. bitshift = 4 * (wordindex & 3);
  799. prevslices = sk->roundkeys_serial + 8 * (wordindex >> 2);
  800. { // WINSCP
  801. for (i2 = 0; i2 < 8; i2++)
  802. slices[i2] ^= prevslices[i2] >> bitshift;
  803. } // WINSCP
  804. } // WINSCP
  805. }
  806. /*
  807. * Now copy it into sk.
  808. */
  809. { // WINSCP
  810. unsigned b;
  811. for (b = 0; b < 8; b++)
  812. outslices[b] |= (slices[b] & 0xF) << outshift;
  813. } // WINSCP
  814. outshift += 4;
  815. if (outshift == 16) {
  816. outshift = 0;
  817. outslices += 8;
  818. }
  819. }
  820. smemclr(inblk, sizeof(inblk));
  821. smemclr(slices, sizeof(slices));
  822. /*
  823. * Replicate that set of round keys into larger integers for the
  824. * parallel versions of the cipher.
  825. */
  826. for (i = 0; i < 8 * (sched_words / 4); i++) {
  827. sk->roundkeys_parallel[i] = sk->roundkeys_serial[i] *
  828. ((BignumInt)~(BignumInt)0 / 0xFFFF);
  829. }
  830. } // WINSCP
  831. } // WINSCP
  832. } // WINSCP
  833. }
  834. /* -----
  835. * The full cipher primitive, including transforming the input and
  836. * output to/from bit-sliced form.
  837. */
  838. #define ENCRYPT_FN(suffix, uintN_t, nblocks) \
  839. static void aes_sliced_e_##suffix( \
  840. uint8_t *output, const uint8_t *input, const aes_sliced_key *sk) \
  841. { \
  842. uintN_t state[8]; \
  843. unsigned i; /*WINSCP*/ \
  844. TO_BITSLICES(state, input, uintN_t, =, 0); \
  845. for (i = 1; i < nblocks; i++) { \
  846. input += 16; \
  847. TO_BITSLICES(state, input, uintN_t, |=, i*16); \
  848. } \
  849. { /*WINSCP*/ \
  850. const uintN_t *keys = sk->roundkeys_##suffix; \
  851. BITSLICED_ADD(state, state, keys); \
  852. keys += 8; \
  853. for (i = 0; i < sk->rounds-1; i++) { \
  854. aes_sliced_round_e_##suffix(state, state, keys); \
  855. keys += 8; \
  856. } \
  857. aes_sliced_round_e_##suffix##_last(state, state, keys); \
  858. for (i = 0; i < nblocks; i++) { \
  859. FROM_BITSLICES(output, state, i*16); \
  860. output += 16; \
  861. } \
  862. } /*WINSCP*/ \
  863. }
  864. #define DECRYPT_FN(suffix, uintN_t, nblocks) \
  865. static void aes_sliced_d_##suffix( \
  866. uint8_t *output, const uint8_t *input, const aes_sliced_key *sk) \
  867. { \
  868. uintN_t state[8]; \
  869. unsigned i; /*WINSCP*/ \
  870. TO_BITSLICES(state, input, uintN_t, =, 0); \
  871. for (i = 1; i < nblocks; i++) { \
  872. input += 16; \
  873. TO_BITSLICES(state, input, uintN_t, |=, i*16); \
  874. } \
  875. { /*WINSCP*/ \
  876. const uintN_t *keys = sk->roundkeys_##suffix + 8*sk->rounds; \
  877. aes_sliced_round_d_##suffix##_first(state, state, keys); \
  878. keys -= 8; \
  879. for (i = 0; i < sk->rounds-1; i++) { \
  880. aes_sliced_round_d_##suffix(state, state, keys); \
  881. keys -= 8; \
  882. } \
  883. BITSLICED_ADD(state, state, keys); \
  884. for (i = 0; i < nblocks; i++) { \
  885. FROM_BITSLICES(output, state, i*16); \
  886. output += 16; \
  887. } \
  888. } /*WINSCP*/ \
  889. }
  890. ENCRYPT_FN(serial, uint16_t, 1)
  891. #if 0 /* no cipher mode we support requires serial decryption */
  892. DECRYPT_FN(serial, uint16_t, 1)
  893. #endif
  894. ENCRYPT_FN(parallel, BignumInt, SLICE_PARALLELISM)
  895. DECRYPT_FN(parallel, BignumInt, SLICE_PARALLELISM)
  896. /* -----
  897. * The SSH interface and the cipher modes.
  898. */
  899. #define SDCTR_WORDS (16 / BIGNUM_INT_BYTES)
  900. typedef struct aes_sw_context aes_sw_context;
  901. struct aes_sw_context {
  902. aes_sliced_key sk;
  903. union {
  904. struct {
  905. /* In CBC mode, the IV is just a copy of the last seen
  906. * cipher block. */
  907. uint8_t prevblk[16];
  908. } cbc;
  909. struct {
  910. /* In SDCTR mode, we keep the counter itself in a form
  911. * that's easy to increment. We also use the parallel
  912. * version of the core AES function, so we'll encrypt
  913. * multiple counter values in one go. That won't align
  914. * nicely with the sizes of data we're asked to encrypt,
  915. * so we must also store a cache of the last set of
  916. * keystream blocks we generated, and our current position
  917. * within that cache. */
  918. BignumInt counter[SDCTR_WORDS];
  919. uint8_t keystream[SLICE_PARALLELISM * 16];
  920. uint8_t *keystream_pos;
  921. } sdctr;
  922. } iv;
  923. ssh2_cipher ciph;
  924. };
  925. static ssh2_cipher *aes_sw_new(const ssh2_cipheralg *alg)
  926. {
  927. aes_sw_context *ctx = snew(aes_sw_context);
  928. ctx->ciph.vt = alg;
  929. return &ctx->ciph;
  930. }
  931. static void aes_sw_free(ssh2_cipher *ciph)
  932. {
  933. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  934. smemclr(ctx, sizeof(*ctx));
  935. sfree(ctx);
  936. }
  937. static void aes_sw_setkey(ssh2_cipher *ciph, const void *vkey)
  938. {
  939. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  940. aes_sliced_key_setup(&ctx->sk, vkey, ctx->ciph.vt->real_keybits);
  941. }
  942. static void aes_sw_setiv_cbc(ssh2_cipher *ciph, const void *iv)
  943. {
  944. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  945. memcpy(ctx->iv.cbc.prevblk, iv, 16);
  946. }
  947. static void aes_sw_setiv_sdctr(ssh2_cipher *ciph, const void *viv)
  948. {
  949. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  950. const uint8_t *iv = (const uint8_t *)viv;
  951. /* Import the initial counter value into the internal representation */
  952. unsigned i; // WINSCP
  953. for (i = 0; i < SDCTR_WORDS; i++)
  954. ctx->iv.sdctr.counter[i] =
  955. GET_BIGNUMINT_MSB_FIRST(
  956. iv + 16 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES);
  957. /* Set keystream_pos to indicate that the keystream cache is
  958. * currently empty */
  959. ctx->iv.sdctr.keystream_pos =
  960. ctx->iv.sdctr.keystream + sizeof(ctx->iv.sdctr.keystream);
  961. }
  962. typedef void (*aes_sw_fn)(uint32_t v[4], const uint32_t *keysched);
  963. static inline void memxor16(void *vout, const void *vlhs, const void *vrhs)
  964. {
  965. uint8_t *out = (uint8_t *)vout;
  966. const uint8_t *lhs = (const uint8_t *)vlhs, *rhs = (const uint8_t *)vrhs;
  967. uint64_t w;
  968. w = GET_64BIT_LSB_FIRST(lhs);
  969. w ^= GET_64BIT_LSB_FIRST(rhs);
  970. PUT_64BIT_LSB_FIRST(out, w);
  971. w = GET_64BIT_LSB_FIRST(lhs + 8);
  972. w ^= GET_64BIT_LSB_FIRST(rhs + 8);
  973. PUT_64BIT_LSB_FIRST(out + 8, w);
  974. }
  975. static inline void aes_cbc_sw_encrypt(
  976. ssh2_cipher *ciph, void *vblk, int blklen)
  977. {
  978. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  979. /*
  980. * CBC encryption has to be done serially, because the input to
  981. * each run of the cipher includes the output from the previous
  982. * run.
  983. */
  984. uint8_t *blk, *finish; // WINSCP
  985. for (blk = (uint8_t *)vblk, finish = blk + blklen;
  986. blk < finish; blk += 16) {
  987. /*
  988. * We use the IV array itself as the location for the
  989. * encryption, because there's no reason not to.
  990. */
  991. /* XOR the new plaintext block into the previous cipher block */
  992. memxor16(ctx->iv.cbc.prevblk, ctx->iv.cbc.prevblk, blk);
  993. /* Run the cipher over the result, which leaves it
  994. * conveniently already stored in ctx->iv */
  995. aes_sliced_e_serial(
  996. ctx->iv.cbc.prevblk, ctx->iv.cbc.prevblk, &ctx->sk);
  997. /* Copy it to the output location */
  998. memcpy(blk, ctx->iv.cbc.prevblk, 16);
  999. }
  1000. }
  1001. static inline void aes_cbc_sw_decrypt(
  1002. ssh2_cipher *ciph, void *vblk, int blklen)
  1003. {
  1004. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1005. uint8_t *blk = (uint8_t *)vblk;
  1006. /*
  1007. * CBC decryption can run in parallel, because all the
  1008. * _ciphertext_ blocks are already available.
  1009. */
  1010. size_t blocks_remaining = blklen / 16;
  1011. uint8_t data[SLICE_PARALLELISM * 16];
  1012. /* Zeroing the data array is probably overcautious, but it avoids
  1013. * technically undefined behaviour from leaving it uninitialised
  1014. * if our very first iteration doesn't include enough cipher
  1015. * blocks to populate it fully */
  1016. memset(data, 0, sizeof(data));
  1017. while (blocks_remaining > 0) {
  1018. /* Number of blocks we'll handle in this iteration. If we're
  1019. * dealing with fewer than the maximum, it doesn't matter -
  1020. * it's harmless to run the full parallel cipher function
  1021. * anyway. */
  1022. size_t blocks = (blocks_remaining < SLICE_PARALLELISM ?
  1023. blocks_remaining : SLICE_PARALLELISM);
  1024. /* Parallel-decrypt the input, in a separate array so we still
  1025. * have the cipher stream available for XORing. */
  1026. memcpy(data, blk, 16 * blocks);
  1027. aes_sliced_d_parallel(data, data, &ctx->sk);
  1028. /* Write the output and update the IV */
  1029. { // WINSCP
  1030. size_t i; // WINSCP
  1031. for (i = 0; i < blocks; i++) {
  1032. uint8_t *decrypted = data + 16*i;
  1033. uint8_t *output = blk + 16*i;
  1034. memxor16(decrypted, decrypted, ctx->iv.cbc.prevblk);
  1035. memcpy(ctx->iv.cbc.prevblk, output, 16);
  1036. memcpy(output, decrypted, 16);
  1037. }
  1038. /* Advance the input pointer. */
  1039. blk += 16 * blocks;
  1040. blocks_remaining -= blocks;
  1041. } // WINSCP
  1042. }
  1043. smemclr(data, sizeof(data));
  1044. }
  1045. static inline void aes_sdctr_sw(
  1046. ssh2_cipher *ciph, void *vblk, int blklen)
  1047. {
  1048. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1049. /*
  1050. * SDCTR encrypt/decrypt loops round one block at a time XORing
  1051. * the keystream into the user's data, and periodically has to run
  1052. * a parallel encryption operation to get more keystream.
  1053. */
  1054. uint8_t *keystream_end =
  1055. ctx->iv.sdctr.keystream + sizeof(ctx->iv.sdctr.keystream);
  1056. uint8_t *blk, *finish; // WINSCP
  1057. for (blk = (uint8_t *)vblk, finish = blk + blklen;
  1058. blk < finish; blk += 16) {
  1059. if (ctx->iv.sdctr.keystream_pos == keystream_end) {
  1060. /*
  1061. * Generate some keystream.
  1062. */
  1063. uint8_t *block; // WINSCP
  1064. for (block = ctx->iv.sdctr.keystream;
  1065. block < keystream_end; block += 16) {
  1066. /* Format the counter value into the buffer. */
  1067. unsigned i; // WINSCP
  1068. for (i = 0; i < SDCTR_WORDS; i++)
  1069. PUT_BIGNUMINT_MSB_FIRST(
  1070. block + 16 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES,
  1071. ctx->iv.sdctr.counter[i]);
  1072. /* Increment the counter. */
  1073. { // WINSCP
  1074. BignumCarry carry = 1;
  1075. for (i = 0; i < SDCTR_WORDS; i++)
  1076. BignumADC(ctx->iv.sdctr.counter[i], carry,
  1077. ctx->iv.sdctr.counter[i], 0, carry);
  1078. } // WINSCP
  1079. }
  1080. /* Encrypt all those counter blocks. */
  1081. aes_sliced_e_parallel(ctx->iv.sdctr.keystream,
  1082. ctx->iv.sdctr.keystream, &ctx->sk);
  1083. /* Reset keystream_pos to the start of the buffer. */
  1084. ctx->iv.sdctr.keystream_pos = ctx->iv.sdctr.keystream;
  1085. }
  1086. memxor16(blk, blk, ctx->iv.sdctr.keystream_pos);
  1087. ctx->iv.sdctr.keystream_pos += 16;
  1088. }
  1089. }
  1090. #define SW_ENC_DEC(len) \
  1091. static void aes##len##_cbc_sw_encrypt( \
  1092. ssh2_cipher *ciph, void *vblk, int blklen) \
  1093. { aes_cbc_sw_encrypt(ciph, vblk, blklen); } \
  1094. static void aes##len##_cbc_sw_decrypt( \
  1095. ssh2_cipher *ciph, void *vblk, int blklen) \
  1096. { aes_cbc_sw_decrypt(ciph, vblk, blklen); } \
  1097. static void aes##len##_sdctr_sw( \
  1098. ssh2_cipher *ciph, void *vblk, int blklen) \
  1099. { aes_sdctr_sw(ciph, vblk, blklen); }
  1100. SW_ENC_DEC(128)
  1101. SW_ENC_DEC(192)
  1102. SW_ENC_DEC(256)
  1103. /* ----------------------------------------------------------------------
  1104. * Hardware-accelerated implementation of AES using x86 AES-NI.
  1105. */
  1106. #if HW_AES == HW_AES_NI
  1107. /*
  1108. * Set target architecture for Clang and GCC
  1109. */
  1110. #if !defined(__clang__) && defined(__GNUC__)
  1111. # pragma GCC target("aes")
  1112. # pragma GCC target("sse4.1")
  1113. #endif
  1114. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
  1115. # define FUNC_ISA __attribute__ ((target("sse4.1,aes")))
  1116. #else
  1117. # define FUNC_ISA
  1118. #endif
  1119. #include <wmmintrin.h>
  1120. #include <smmintrin.h>
  1121. #if defined(__clang__) || defined(__GNUC__)
  1122. #include <cpuid.h>
  1123. #define GET_CPU_ID(out) __cpuid(1, (out)[0], (out)[1], (out)[2], (out)[3])
  1124. #else
  1125. #define GET_CPU_ID(out) __cpuid(out, 1)
  1126. #endif
  1127. bool aes_hw_available(void)
  1128. {
  1129. /*
  1130. * Determine if AES is available on this CPU, by checking that
  1131. * both AES itself and SSE4.1 are supported.
  1132. */
  1133. unsigned int CPUInfo[4];
  1134. GET_CPU_ID(CPUInfo);
  1135. return (CPUInfo[2] & (1 << 25)) && (CPUInfo[2] & (1 << 19));
  1136. }
  1137. /*
  1138. * Core AES-NI encrypt/decrypt functions, one per length and direction.
  1139. */
  1140. #define NI_CIPHER(len, dir, dirlong, repmacro) \
  1141. static FUNC_ISA inline __m128i aes_ni_##len##_##dir( \
  1142. __m128i v, const __m128i *keysched) \
  1143. { \
  1144. v = _mm_xor_si128(v, *keysched++); \
  1145. repmacro(v = _mm_aes##dirlong##_si128(v, *keysched++);); \
  1146. return _mm_aes##dirlong##last_si128(v, *keysched); \
  1147. }
  1148. NI_CIPHER(128, e, enc, REP9)
  1149. NI_CIPHER(128, d, dec, REP9)
  1150. NI_CIPHER(192, e, enc, REP11)
  1151. NI_CIPHER(192, d, dec, REP11)
  1152. NI_CIPHER(256, e, enc, REP13)
  1153. NI_CIPHER(256, d, dec, REP13)
  1154. /*
  1155. * The main key expansion.
  1156. */
  1157. static FUNC_ISA void aes_ni_key_expand(
  1158. const unsigned char *key, size_t key_words,
  1159. __m128i *keysched_e, __m128i *keysched_d)
  1160. {
  1161. size_t rounds = key_words + 6;
  1162. size_t sched_words = (rounds + 1) * 4;
  1163. /*
  1164. * Store the key schedule as 32-bit integers during expansion, so
  1165. * that it's easy to refer back to individual previous words. We
  1166. * collect them into the final __m128i form at the end.
  1167. */
  1168. uint32_t sched[MAXROUNDKEYS * 4];
  1169. unsigned rconpos = 0;
  1170. for (size_t i = 0; i < sched_words; i++) {
  1171. if (i < key_words) {
  1172. sched[i] = GET_32BIT_LSB_FIRST(key + 4 * i);
  1173. } else {
  1174. uint32_t temp = sched[i - 1];
  1175. bool rotate_and_round_constant = (i % key_words == 0);
  1176. bool only_sub = (key_words == 8 && i % 8 == 4);
  1177. if (rotate_and_round_constant) {
  1178. __m128i v = _mm_setr_epi32(0,temp,0,0);
  1179. v = _mm_aeskeygenassist_si128(v, 0);
  1180. temp = _mm_extract_epi32(v, 1);
  1181. assert(rconpos < lenof(key_setup_round_constants));
  1182. temp ^= key_setup_round_constants[rconpos++];
  1183. } else if (only_sub) {
  1184. __m128i v = _mm_setr_epi32(0,temp,0,0);
  1185. v = _mm_aeskeygenassist_si128(v, 0);
  1186. temp = _mm_extract_epi32(v, 0);
  1187. }
  1188. sched[i] = sched[i - key_words] ^ temp;
  1189. }
  1190. }
  1191. /*
  1192. * Combine the key schedule words into __m128i vectors and store
  1193. * them in the output context.
  1194. */
  1195. for (size_t round = 0; round <= rounds; round++)
  1196. keysched_e[round] = _mm_setr_epi32(
  1197. sched[4*round ], sched[4*round+1],
  1198. sched[4*round+2], sched[4*round+3]);
  1199. smemclr(sched, sizeof(sched));
  1200. /*
  1201. * Now prepare the modified keys for the inverse cipher.
  1202. */
  1203. for (size_t eround = 0; eround <= rounds; eround++) {
  1204. size_t dround = rounds - eround;
  1205. __m128i rkey = keysched_e[eround];
  1206. if (eround && dround) /* neither first nor last */
  1207. rkey = _mm_aesimc_si128(rkey);
  1208. keysched_d[dround] = rkey;
  1209. }
  1210. }
  1211. /*
  1212. * Auxiliary routine to increment the 128-bit counter used in SDCTR
  1213. * mode.
  1214. */
  1215. static FUNC_ISA inline __m128i aes_ni_sdctr_increment(__m128i v)
  1216. {
  1217. const __m128i ONE = _mm_setr_epi32(1,0,0,0);
  1218. const __m128i ZERO = _mm_setzero_si128();
  1219. /* Increment the low-order 64 bits of v */
  1220. v = _mm_add_epi64(v, ONE);
  1221. /* Check if they've become zero */
  1222. __m128i cmp = _mm_cmpeq_epi64(v, ZERO);
  1223. /* If so, the low half of cmp is all 1s. Pack that into the high
  1224. * half of addend with zero in the low half. */
  1225. __m128i addend = _mm_unpacklo_epi64(ZERO, cmp);
  1226. /* And subtract that from v, which increments the high 64 bits iff
  1227. * the low 64 wrapped round. */
  1228. v = _mm_sub_epi64(v, addend);
  1229. return v;
  1230. }
  1231. /*
  1232. * Auxiliary routine to reverse the byte order of a vector, so that
  1233. * the SDCTR IV can be made big-endian for feeding to the cipher.
  1234. */
  1235. static FUNC_ISA inline __m128i aes_ni_sdctr_reverse(__m128i v)
  1236. {
  1237. v = _mm_shuffle_epi8(
  1238. v, _mm_setr_epi8(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0));
  1239. return v;
  1240. }
  1241. /*
  1242. * The SSH interface and the cipher modes.
  1243. */
  1244. typedef struct aes_ni_context aes_ni_context;
  1245. struct aes_ni_context {
  1246. __m128i keysched_e[MAXROUNDKEYS], keysched_d[MAXROUNDKEYS], iv;
  1247. ssh2_cipher ciph;
  1248. };
  1249. static ssh2_cipher *aes_hw_new(const ssh2_cipheralg *alg)
  1250. {
  1251. if (!aes_hw_available_cached())
  1252. return NULL;
  1253. aes_ni_context *ctx = snew(aes_ni_context);
  1254. ctx->ciph.vt = alg;
  1255. return &ctx->ciph;
  1256. }
  1257. static void aes_hw_free(ssh2_cipher *ciph)
  1258. {
  1259. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1260. smemclr(ctx, sizeof(*ctx));
  1261. sfree(ctx);
  1262. }
  1263. static void aes_hw_setkey(ssh2_cipher *ciph, const void *vkey)
  1264. {
  1265. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1266. const unsigned char *key = (const unsigned char *)vkey;
  1267. aes_ni_key_expand(key, ctx->ciph.vt->real_keybits / 32,
  1268. ctx->keysched_e, ctx->keysched_d);
  1269. }
  1270. static FUNC_ISA void aes_hw_setiv_cbc(ssh2_cipher *ciph, const void *iv)
  1271. {
  1272. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1273. ctx->iv = _mm_loadu_si128(iv);
  1274. }
  1275. static FUNC_ISA void aes_hw_setiv_sdctr(ssh2_cipher *ciph, const void *iv)
  1276. {
  1277. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1278. __m128i counter = _mm_loadu_si128(iv);
  1279. ctx->iv = aes_ni_sdctr_reverse(counter);
  1280. }
  1281. typedef __m128i (*aes_ni_fn)(__m128i v, const __m128i *keysched);
  1282. static FUNC_ISA inline void aes_cbc_ni_encrypt(
  1283. ssh2_cipher *ciph, void *vblk, int blklen, aes_ni_fn encrypt)
  1284. {
  1285. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1286. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1287. blk < finish; blk += 16) {
  1288. __m128i plaintext = _mm_loadu_si128((const __m128i *)blk);
  1289. __m128i cipher_input = _mm_xor_si128(plaintext, ctx->iv);
  1290. __m128i ciphertext = encrypt(cipher_input, ctx->keysched_e);
  1291. _mm_storeu_si128((__m128i *)blk, ciphertext);
  1292. ctx->iv = ciphertext;
  1293. }
  1294. }
  1295. static FUNC_ISA inline void aes_cbc_ni_decrypt(
  1296. ssh2_cipher *ciph, void *vblk, int blklen, aes_ni_fn decrypt)
  1297. {
  1298. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1299. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1300. blk < finish; blk += 16) {
  1301. __m128i ciphertext = _mm_loadu_si128((const __m128i *)blk);
  1302. __m128i decrypted = decrypt(ciphertext, ctx->keysched_d);
  1303. __m128i plaintext = _mm_xor_si128(decrypted, ctx->iv);
  1304. _mm_storeu_si128((__m128i *)blk, plaintext);
  1305. ctx->iv = ciphertext;
  1306. }
  1307. }
  1308. static FUNC_ISA inline void aes_sdctr_ni(
  1309. ssh2_cipher *ciph, void *vblk, int blklen, aes_ni_fn encrypt)
  1310. {
  1311. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1312. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1313. blk < finish; blk += 16) {
  1314. __m128i counter = aes_ni_sdctr_reverse(ctx->iv);
  1315. __m128i keystream = encrypt(counter, ctx->keysched_e);
  1316. __m128i input = _mm_loadu_si128((const __m128i *)blk);
  1317. __m128i output = _mm_xor_si128(input, keystream);
  1318. _mm_storeu_si128((__m128i *)blk, output);
  1319. ctx->iv = aes_ni_sdctr_increment(ctx->iv);
  1320. }
  1321. }
  1322. #define NI_ENC_DEC(len) \
  1323. static FUNC_ISA void aes##len##_cbc_hw_encrypt( \
  1324. ssh2_cipher *ciph, void *vblk, int blklen) \
  1325. { aes_cbc_ni_encrypt(ciph, vblk, blklen, aes_ni_##len##_e); } \
  1326. static FUNC_ISA void aes##len##_cbc_hw_decrypt( \
  1327. ssh2_cipher *ciph, void *vblk, int blklen) \
  1328. { aes_cbc_ni_decrypt(ciph, vblk, blklen, aes_ni_##len##_d); } \
  1329. static FUNC_ISA void aes##len##_sdctr_hw( \
  1330. ssh2_cipher *ciph, void *vblk, int blklen) \
  1331. { aes_sdctr_ni(ciph, vblk, blklen, aes_ni_##len##_e); } \
  1332. NI_ENC_DEC(128)
  1333. NI_ENC_DEC(192)
  1334. NI_ENC_DEC(256)
  1335. /* ----------------------------------------------------------------------
  1336. * Stub functions if we have no hardware-accelerated AES. In this
  1337. * case, aes_hw_new returns NULL (though it should also never be
  1338. * selected by aes_select, so the only thing that should even be
  1339. * _able_ to call it is testcrypt). As a result, the remaining vtable
  1340. * functions should never be called at all.
  1341. */
  1342. #elif HW_AES == HW_AES_NONE
  1343. bool aes_hw_available(void)
  1344. {
  1345. return false;
  1346. }
  1347. static ssh2_cipher *aes_hw_new(const ssh2_cipheralg *alg)
  1348. {
  1349. return NULL;
  1350. }
  1351. #define STUB_BODY { unreachable("Should never be called"); }
  1352. static void aes_hw_free(ssh2_cipher *ciph) STUB_BODY
  1353. static void aes_hw_setkey(ssh2_cipher *ciph, const void *key) STUB_BODY
  1354. static void aes_hw_setiv_cbc(ssh2_cipher *ciph, const void *iv) STUB_BODY
  1355. static void aes_hw_setiv_sdctr(ssh2_cipher *ciph, const void *iv) STUB_BODY
  1356. #define STUB_ENC_DEC(len) \
  1357. static void aes##len##_cbc_hw_encrypt( \
  1358. ssh2_cipher *ciph, void *vblk, int blklen) STUB_BODY \
  1359. static void aes##len##_cbc_hw_decrypt( \
  1360. ssh2_cipher *ciph, void *vblk, int blklen) STUB_BODY \
  1361. static void aes##len##_sdctr_hw( \
  1362. ssh2_cipher *ciph, void *vblk, int blklen) STUB_BODY
  1363. STUB_ENC_DEC(128)
  1364. STUB_ENC_DEC(192)
  1365. STUB_ENC_DEC(256)
  1366. #endif /* HW_AES */
  1367. /* ----------------------------------------------------------------------
  1368. * Auxiliary routines for use of AES in other contexts than the main
  1369. * SSH packet protocol.
  1370. */
  1371. void aes256_encrypt_pubkey(const void *key, void *blk, int len)
  1372. {
  1373. char iv[16];
  1374. memset(iv, 0, 16);
  1375. { // WINSCP
  1376. ssh2_cipher *cipher = ssh2_cipher_new(&ssh_aes256_cbc);
  1377. ssh2_cipher_setkey(cipher, key);
  1378. ssh2_cipher_setiv(cipher, iv);
  1379. ssh2_cipher_encrypt(cipher, blk, len);
  1380. ssh2_cipher_free(cipher);
  1381. } // WINSCP
  1382. }
  1383. void aes256_decrypt_pubkey(const void *key, void *blk, int len)
  1384. {
  1385. char iv[16];
  1386. memset(iv, 0, 16);
  1387. { // WINSCP
  1388. ssh2_cipher *cipher = ssh2_cipher_new(&ssh_aes256_cbc);
  1389. ssh2_cipher_setkey(cipher, key);
  1390. ssh2_cipher_setiv(cipher, iv);
  1391. ssh2_cipher_decrypt(cipher, blk, len);
  1392. ssh2_cipher_free(cipher);
  1393. } // WINSCP
  1394. }
  1395. #ifdef MPEXT
  1396. #include "puttyexp.h"
  1397. AESContext * aes_make_context()
  1398. {
  1399. ssh2_cipher * cipher = ssh2_cipher_new(&ssh_aes256_sdctr);
  1400. return cipher;
  1401. }
  1402. void aes_free_context(AESContext * ctx)
  1403. {
  1404. ssh2_cipher * cipher = (ssh2_cipher *)ctx;
  1405. ssh2_cipher_free(cipher);
  1406. }
  1407. void aes_iv(AESContext * ctx, const void * iv)
  1408. {
  1409. ssh2_cipher * cipher = (ssh2_cipher *)ctx;
  1410. ssh2_cipher_setiv(cipher, iv);
  1411. }
  1412. void call_aes_setup(AESContext * ctx, unsigned char * key, int keylen)
  1413. {
  1414. ssh2_cipher * cipher = (ssh2_cipher *)ctx;
  1415. assert(keylen == 32);
  1416. ssh2_cipher_setkey(cipher, key);
  1417. }
  1418. void call_aes_sdctr(unsigned char *blk, int len, void *ctx)
  1419. {
  1420. ssh2_cipher * cipher = (ssh2_cipher *)ctx;
  1421. ssh2_cipher_encrypt(cipher, blk, len);
  1422. }
  1423. #endif