sshaes.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. /*
  2. * sshaes.c - implementation of AES
  3. */
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include "ssh.h"
  7. #include "mpint_i.h" /* we reuse the BignumInt system */
  8. /*
  9. * Start by deciding whether we can support hardware AES at all.
  10. */
  11. #define HW_AES_NONE 0
  12. #define HW_AES_NI 1
  13. #define HW_AES_NEON 2
  14. #ifdef _FORCE_AES_NI
  15. # define HW_AES HW_AES_NI
  16. #elif defined(__clang__)
  17. # if __has_attribute(target) && __has_include(<wmmintrin.h>) && \
  18. (defined(__x86_64__) || defined(__i386))
  19. # define HW_AES HW_AES_NI
  20. # endif
  21. #elif defined(__GNUC__)
  22. # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) && \
  23. (defined(__x86_64__) || defined(__i386))
  24. # define HW_AES HW_AES_NI
  25. # endif
  26. #elif defined (_MSC_VER)
  27. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_FULL_VER >= 150030729
  28. # define HW_AES HW_AES_NI
  29. # endif
  30. #endif
  31. #define HW_AES HW_AES_NI // WINSCP
  32. #ifdef _FORCE_AES_NEON
  33. # define HW_AES HW_AES_NEON
  34. #elif defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  35. /* Arm can potentially support both endiannesses, but this code
  36. * hasn't been tested on anything but little. If anyone wants to
  37. * run big-endian, they'll need to fix it first. */
  38. #elif defined __ARM_FEATURE_CRYPTO
  39. /* If the Arm crypto extension is available already, we can
  40. * support NEON AES without having to enable anything by hand */
  41. # define HW_AES HW_AES_NEON
  42. #elif defined(__clang__)
  43. # if __has_attribute(target) && __has_include(<arm_neon.h>) && \
  44. (defined(__aarch64__))
  45. /* clang can enable the crypto extension in AArch64 using
  46. * __attribute__((target)) */
  47. # define HW_AES HW_AES_NEON
  48. # define USE_CLANG_ATTR_TARGET_AARCH64
  49. # endif
  50. #elif defined _MSC_VER
  51. # if defined _M_ARM64
  52. # define HW_AES HW_AES_NEON
  53. /* 64-bit Visual Studio uses the header <arm64_neon.h> in place
  54. * of the standard <arm_neon.h> */
  55. # define USE_ARM64_NEON_H
  56. # elif defined _M_ARM
  57. # define HW_AES HW_AES_NEON
  58. /* 32-bit Visual Studio uses the right header name, but requires
  59. * this #define to enable a set of intrinsic definitions that
  60. * do not omit one of the parameters for vaes[ed]q_u8 */
  61. # define _ARM_USE_NEW_NEON_INTRINSICS
  62. # endif
  63. #endif
  64. #if defined _FORCE_SOFTWARE_AES || !defined HW_AES
  65. # undef HW_AES
  66. # define HW_AES HW_AES_NONE
  67. #endif
  68. #if HW_AES == HW_AES_NI
  69. #define HW_NAME_SUFFIX " (AES-NI accelerated)"
  70. #elif HW_AES == HW_AES_NEON
  71. #define HW_NAME_SUFFIX " (NEON accelerated)"
  72. #else
  73. #define HW_NAME_SUFFIX " (!NONEXISTENT ACCELERATED VERSION!)"
  74. #endif
  75. /*
  76. * Vtable collection for AES. For each SSH-level cipher id (i.e.
  77. * combination of key length and cipher mode), we provide three
  78. * vtables: one for the pure software implementation, one using
  79. * hardware acceleration (if available), and a top-level one which is
  80. * never actually instantiated, and only contains a new() method whose
  81. * job is to decide which of the other two to return an actual
  82. * instance of.
  83. */
  84. static ssh_cipher *aes_select(const ssh_cipheralg *alg);
  85. static ssh_cipher *aes_sw_new(const ssh_cipheralg *alg);
  86. static void aes_sw_free(ssh_cipher *);
  87. static void aes_sw_setiv_cbc(ssh_cipher *, const void *iv);
  88. static void aes_sw_setiv_sdctr(ssh_cipher *, const void *iv);
  89. static void aes_sw_setkey(ssh_cipher *, const void *key);
  90. /*WINSCP static*/ ssh_cipher *aes_hw_new(const ssh_cipheralg *alg);
  91. /*WINSCP static*/ void aes_hw_free(ssh_cipher *);
  92. /*WINSCP static*/ void aes_hw_setiv_cbc(ssh_cipher *, const void *iv);
  93. /*WINSCP static*/ void aes_hw_setiv_sdctr(ssh_cipher *, const void *iv);
  94. /*WINSCP static*/ void aes_hw_setkey(ssh_cipher *, const void *key);
  95. #ifndef WINSCP_VS
  96. struct aes_extra {
  97. const ssh_cipheralg *sw, *hw;
  98. };
  99. #define VTABLES_INNER(cid, pid, bits, name, encsuffix, \
  100. decsuffix, setivsuffix, flagsval) \
  101. /*WINSCP static*/ void cid##_sw##encsuffix(ssh_cipher *, void *blk, int len); \
  102. /*WINSCP static*/ void cid##_sw##decsuffix(ssh_cipher *, void *blk, int len); \
  103. const ssh_cipheralg ssh_##cid##_sw = { \
  104. /*WINSCP*/ \
  105. /*.new =*/ aes_sw_new, \
  106. /*.free =*/ aes_sw_free, \
  107. /*.setiv =*/ aes_sw_##setivsuffix, \
  108. /*.setkey =*/ aes_sw_setkey, \
  109. /*.encrypt =*/ cid##_sw##encsuffix, \
  110. /*.decrypt =*/ cid##_sw##decsuffix, \
  111. NULL, NULL, /*WINSCP*/ \
  112. /*.ssh2_id =*/ pid, \
  113. /*.blksize =*/ 16, \
  114. /*.real_keybits =*/ bits, \
  115. /*.padded_keybytes =*/ bits/8, \
  116. /*.flags =*/ flagsval, \
  117. /*.text_name =*/ name " (unaccelerated)", \
  118. NULL, NULL, /*WINSCP*/ \
  119. }; \
  120. \
  121. /*WINSCP static*/ void cid##_hw##encsuffix(ssh_cipher *, void *blk, int len); \
  122. /*WINSCP static*/ void cid##_hw##decsuffix(ssh_cipher *, void *blk, int len); \
  123. const ssh_cipheralg ssh_##cid##_hw = { \
  124. /*WINSCP*/ \
  125. /*.new =*/ aes_hw_new, \
  126. /*.free =*/ aes_hw_free, \
  127. /*.setiv =*/ aes_hw_##setivsuffix, \
  128. /*.setkey =*/ aes_hw_setkey, \
  129. /*.encrypt =*/ cid##_hw##encsuffix, \
  130. /*.decrypt =*/ cid##_hw##decsuffix, \
  131. NULL, NULL, /*WINSCP*/ \
  132. /*.ssh2_id =*/ pid, \
  133. /*.blksize =*/ 16, \
  134. /*.real_keybits =*/ bits, \
  135. /*.padded_keybytes =*/ bits/8, \
  136. /*.flags =*/ flagsval, \
  137. /*.text_name =*/ name HW_NAME_SUFFIX, \
  138. NULL, NULL, /*WINSCP*/ \
  139. }; \
  140. \
  141. static const struct aes_extra extra_##cid = { \
  142. &ssh_##cid##_sw, &ssh_##cid##_hw }; \
  143. \
  144. const ssh_cipheralg ssh_##cid = { \
  145. /*WINSCP*/ \
  146. /*.new =*/ aes_select, \
  147. NULL, NULL, NULL, NULL, NULL, NULL, NULL, \
  148. /*.ssh2_id =*/ pid, \
  149. /*.blksize =*/ 16, \
  150. /*.real_keybits =*/ bits, \
  151. /*.padded_keybytes =*/ bits/8, \
  152. /*.flags =*/ flagsval, \
  153. /*.text_name =*/ name " (dummy selector vtable)", \
  154. NULL, \
  155. /*.extra =*/ &extra_##cid \
  156. }; \
  157. #define VTABLES(keylen) \
  158. VTABLES_INNER(aes ## keylen ## _cbc, "aes" #keylen "-cbc", \
  159. keylen, "AES-" #keylen " CBC", _encrypt, _decrypt, \
  160. setiv_cbc, SSH_CIPHER_IS_CBC) \
  161. VTABLES_INNER(aes ## keylen ## _sdctr, "aes" #keylen "-ctr", \
  162. keylen, "AES-" #keylen " SDCTR",,, setiv_sdctr, 0)
  163. VTABLES(128)
  164. VTABLES(192)
  165. VTABLES(256)
  166. static const ssh_cipheralg ssh_rijndael_lysator = {
  167. /* Same as aes256_cbc, but with a different protocol ID */
  168. // WINSCP
  169. /*.new =*/ aes_select,
  170. NULL, NULL, NULL, NULL, NULL, NULL, NULL, // WINSCP
  171. /*.ssh2_id =*/ "[email protected]",
  172. /*.blksize =*/ 16,
  173. /*.real_keybits =*/ 256,
  174. /*.padded_keybytes =*/ 256/8,
  175. /*.flags =*/ 0,
  176. /*.text_name =*/ "AES-256 CBC (dummy selector vtable)",
  177. NULL, // WINSCP
  178. /*.extra =*/ &extra_aes256_cbc,
  179. };
  180. static const ssh_cipheralg *const aes_list[] = {
  181. &ssh_aes256_sdctr,
  182. &ssh_aes256_cbc,
  183. &ssh_rijndael_lysator,
  184. &ssh_aes192_sdctr,
  185. &ssh_aes192_cbc,
  186. &ssh_aes128_sdctr,
  187. &ssh_aes128_cbc,
  188. };
  189. const ssh2_ciphers ssh2_aes = { lenof(aes_list), aes_list };
  190. #endif
  191. /*
  192. * The actual query function that asks if hardware acceleration is
  193. * available.
  194. */
  195. /*WINSCP static*/ bool aes_hw_available(void);
  196. /*
  197. * The top-level selection function, caching the results of
  198. * aes_hw_available() so it only has to run once.
  199. */
  200. static bool aes_hw_available_cached(void)
  201. {
  202. static bool initialised = false;
  203. static bool hw_available;
  204. if (!initialised) {
  205. hw_available = aes_hw_available();
  206. initialised = true;
  207. }
  208. return hw_available;
  209. }
  210. #ifndef WINSCP_VS
  211. static ssh_cipher *aes_select(const ssh_cipheralg *alg)
  212. {
  213. const struct aes_extra *extra = (const struct aes_extra *)alg->extra;
  214. const ssh_cipheralg *real_alg =
  215. aes_hw_available_cached() ? extra->hw : extra->sw;
  216. return ssh_cipher_new(real_alg);
  217. }
  218. #endif
  219. /* ----------------------------------------------------------------------
  220. * Definitions likely to be helpful to multiple implementations.
  221. */
  222. #define REP2(x) x x
  223. #define REP4(x) REP2(REP2(x))
  224. #define REP8(x) REP2(REP4(x))
  225. #define REP9(x) REP8(x) x
  226. #define REP11(x) REP8(x) REP2(x) x
  227. #define REP13(x) REP8(x) REP4(x) x
  228. static const uint8_t key_setup_round_constants[] = {
  229. /* The first few powers of X in GF(2^8), used during key setup.
  230. * This can safely be a lookup table without side channel risks,
  231. * because key setup iterates through it once in a standard way
  232. * regardless of the key. */
  233. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
  234. };
  235. #define MAXROUNDKEYS 15
  236. /* ----------------------------------------------------------------------
  237. * Software implementation of AES.
  238. *
  239. * This implementation uses a bit-sliced representation. Instead of
  240. * the obvious approach of storing the cipher state so that each byte
  241. * (or field element, or entry in the cipher matrix) occupies 8
  242. * contiguous bits in a machine integer somewhere, we organise the
  243. * cipher state as an array of 8 integers, in such a way that each
  244. * logical byte of the cipher state occupies one bit in each integer,
  245. * all at the same position. This allows us to do parallel logic on
  246. * all bytes of the state by doing bitwise operations between the 8
  247. * integers; in particular, the S-box (SubBytes) lookup is done this
  248. * way, which takes about 110 operations - but for those 110 bitwise
  249. * ops you get 64 S-box lookups, not just one.
  250. */
  251. #define SLICE_PARALLELISM (BIGNUM_INT_BYTES / 2)
  252. #ifdef WINSCP_VS
  253. #ifdef BITSLICED_DEBUG
  254. /* Dump function that undoes the bitslicing transform, so you can see
  255. * the logical data represented by a set of slice words. */
  256. static inline void dumpslices_uint16_t(
  257. const char *prefix, const uint16_t slices[8])
  258. {
  259. printf("%-30s", prefix);
  260. for (unsigned byte = 0; byte < 16; byte++) {
  261. unsigned byteval = 0;
  262. for (unsigned bit = 0; bit < 8; bit++)
  263. byteval |= (1 & (slices[bit] >> byte)) << bit;
  264. printf("%02x", byteval);
  265. }
  266. printf("\n");
  267. }
  268. static inline void dumpslices_BignumInt(
  269. const char *prefix, const BignumInt slices[8])
  270. {
  271. printf("%-30s", prefix);
  272. for (unsigned iter = 0; iter < SLICE_PARALLELISM; iter++) {
  273. for (unsigned byte = 0; byte < 16; byte++) {
  274. unsigned byteval = 0;
  275. for (unsigned bit = 0; bit < 8; bit++)
  276. byteval |= (1 & (slices[bit] >> (iter*16+byte))) << bit;
  277. printf("%02x", byteval);
  278. }
  279. if (iter+1 < SLICE_PARALLELISM)
  280. printf(" ");
  281. }
  282. printf("\n");
  283. }
  284. #else
  285. #define dumpslices_uintN_t(prefix, slices) ((void)0)
  286. #define dumpslices_BignumInt(prefix, slices) ((void)0)
  287. #endif
  288. /* -----
  289. * Bit-slicing transformation: convert between an array of 16 uint8_t
  290. * and an array of 8 uint16_t, so as to interchange the bit index
  291. * within each element and the element index within the array. (That
  292. * is, bit j of input[i] == bit i of output[j].
  293. */
  294. #define SWAPWORDS(shift) do \
  295. { \
  296. uint64_t mask = ~(uint64_t)0 / ((1ULL << shift) + 1); \
  297. uint64_t diff = ((i0 >> shift) ^ i1) & mask; \
  298. i0 ^= diff << shift; \
  299. i1 ^= diff; \
  300. } while (0)
  301. #define SWAPINWORD(i, bigshift, smallshift) do \
  302. { \
  303. uint64_t mask = ~(uint64_t)0; \
  304. mask /= ((1ULL << bigshift) + 1); \
  305. mask /= ((1ULL << smallshift) + 1); \
  306. mask <<= smallshift; \
  307. unsigned shift = bigshift - smallshift; \
  308. uint64_t diff = ((i >> shift) ^ i) & mask; \
  309. i ^= diff ^ (diff << shift); \
  310. } while (0)
  311. #define TO_BITSLICES(slices, bytes, uintN_t, assign_op, shift) do \
  312. { \
  313. uint64_t i0 = GET_64BIT_LSB_FIRST(bytes); \
  314. uint64_t i1 = GET_64BIT_LSB_FIRST(bytes + 8); \
  315. SWAPINWORD(i0, 8, 1); \
  316. SWAPINWORD(i1, 8, 1); \
  317. SWAPINWORD(i0, 16, 2); \
  318. SWAPINWORD(i1, 16, 2); \
  319. SWAPINWORD(i0, 32, 4); \
  320. SWAPINWORD(i1, 32, 4); \
  321. SWAPWORDS(8); \
  322. slices[0] assign_op (uintN_t)((i0 >> 0) & 0xFFFF) << (shift); \
  323. slices[2] assign_op (uintN_t)((i0 >> 16) & 0xFFFF) << (shift); \
  324. slices[4] assign_op (uintN_t)((i0 >> 32) & 0xFFFF) << (shift); \
  325. slices[6] assign_op (uintN_t)((i0 >> 48) & 0xFFFF) << (shift); \
  326. slices[1] assign_op (uintN_t)((i1 >> 0) & 0xFFFF) << (shift); \
  327. slices[3] assign_op (uintN_t)((i1 >> 16) & 0xFFFF) << (shift); \
  328. slices[5] assign_op (uintN_t)((i1 >> 32) & 0xFFFF) << (shift); \
  329. slices[7] assign_op (uintN_t)((i1 >> 48) & 0xFFFF) << (shift); \
  330. } while (0)
  331. #define FROM_BITSLICES(bytes, slices, shift) do \
  332. { \
  333. uint64_t i1 = ((slices[7] >> (shift)) & 0xFFFF); \
  334. i1 = (i1 << 16) | ((slices[5] >> (shift)) & 0xFFFF); \
  335. i1 = (i1 << 16) | ((slices[3] >> (shift)) & 0xFFFF); \
  336. i1 = (i1 << 16) | ((slices[1] >> (shift)) & 0xFFFF); \
  337. uint64_t i0 = ((slices[6] >> (shift)) & 0xFFFF); \
  338. i0 = (i0 << 16) | ((slices[4] >> (shift)) & 0xFFFF); \
  339. i0 = (i0 << 16) | ((slices[2] >> (shift)) & 0xFFFF); \
  340. i0 = (i0 << 16) | ((slices[0] >> (shift)) & 0xFFFF); \
  341. SWAPWORDS(8); \
  342. SWAPINWORD(i0, 32, 4); \
  343. SWAPINWORD(i1, 32, 4); \
  344. SWAPINWORD(i0, 16, 2); \
  345. SWAPINWORD(i1, 16, 2); \
  346. SWAPINWORD(i0, 8, 1); \
  347. SWAPINWORD(i1, 8, 1); \
  348. PUT_64BIT_LSB_FIRST(bytes, i0); \
  349. PUT_64BIT_LSB_FIRST((bytes) + 8, i1); \
  350. } while (0)
  351. /* -----
  352. * Some macros that will be useful repeatedly.
  353. */
  354. /* Iterate a unary transformation over all 8 slices. */
  355. #define ITERATE(MACRO, output, input, uintN_t) do \
  356. { \
  357. MACRO(output[0], input[0], uintN_t); \
  358. MACRO(output[1], input[1], uintN_t); \
  359. MACRO(output[2], input[2], uintN_t); \
  360. MACRO(output[3], input[3], uintN_t); \
  361. MACRO(output[4], input[4], uintN_t); \
  362. MACRO(output[5], input[5], uintN_t); \
  363. MACRO(output[6], input[6], uintN_t); \
  364. MACRO(output[7], input[7], uintN_t); \
  365. } while (0)
  366. /* Simply add (i.e. XOR) two whole sets of slices together. */
  367. #define BITSLICED_ADD(output, lhs, rhs) do \
  368. { \
  369. output[0] = lhs[0] ^ rhs[0]; \
  370. output[1] = lhs[1] ^ rhs[1]; \
  371. output[2] = lhs[2] ^ rhs[2]; \
  372. output[3] = lhs[3] ^ rhs[3]; \
  373. output[4] = lhs[4] ^ rhs[4]; \
  374. output[5] = lhs[5] ^ rhs[5]; \
  375. output[6] = lhs[6] ^ rhs[6]; \
  376. output[7] = lhs[7] ^ rhs[7]; \
  377. } while (0)
  378. /* -----
  379. * The AES S-box, in pure bitwise logic so that it can be run in
  380. * parallel on whole words full of bit-sliced field elements.
  381. *
  382. * Source: 'A new combinational logic minimization technique with
  383. * applications to cryptology', https://eprint.iacr.org/2009/191
  384. *
  385. * As a minor speed optimisation, I use a modified version of the
  386. * S-box which omits the additive constant 0x63, i.e. this S-box
  387. * consists of only the field inversion and linear map components.
  388. * Instead, the addition of the constant is deferred until after the
  389. * subsequent ShiftRows and MixColumns stages, so that it happens at
  390. * the same time as adding the next round key - and then we just make
  391. * it _part_ of the round key, so it doesn't cost any extra
  392. * instructions to add.
  393. *
  394. * (Obviously adding a constant to each byte commutes with ShiftRows,
  395. * which only permutes the bytes. It also commutes with MixColumns:
  396. * that's not quite so obvious, but since the effect of MixColumns is
  397. * to multiply a constant polynomial M into each column, it is obvious
  398. * that adding some polynomial K and then multiplying by M is
  399. * equivalent to multiplying by M and then adding the product KM. And
  400. * in fact, since the coefficients of M happen to sum to 1, it turns
  401. * out that KM = K, so we don't even have to change the constant when
  402. * we move it to the far side of MixColumns.)
  403. *
  404. * Of course, one knock-on effect of this is that the use of the S-box
  405. * *during* key setup has to be corrected by manually adding on the
  406. * constant afterwards!
  407. */
  408. /* Initial linear transformation for the forward S-box, from Fig 2 of
  409. * the paper. */
  410. #define SBOX_FORWARD_TOP_TRANSFORM(input, uintN_t) \
  411. uintN_t y14 = input[4] ^ input[2]; \
  412. uintN_t y13 = input[7] ^ input[1]; \
  413. uintN_t y9 = input[7] ^ input[4]; \
  414. uintN_t y8 = input[7] ^ input[2]; \
  415. uintN_t t0 = input[6] ^ input[5]; \
  416. uintN_t y1 = t0 ^ input[0]; \
  417. uintN_t y4 = y1 ^ input[4]; \
  418. uintN_t y12 = y13 ^ y14; \
  419. uintN_t y2 = y1 ^ input[7]; \
  420. uintN_t y5 = y1 ^ input[1]; \
  421. uintN_t y3 = y5 ^ y8; \
  422. uintN_t t1 = input[3] ^ y12; \
  423. uintN_t y15 = t1 ^ input[2]; \
  424. uintN_t y20 = t1 ^ input[6]; \
  425. uintN_t y6 = y15 ^ input[0]; \
  426. uintN_t y10 = y15 ^ t0; \
  427. uintN_t y11 = y20 ^ y9; \
  428. uintN_t y7 = input[0] ^ y11; \
  429. uintN_t y17 = y10 ^ y11; \
  430. uintN_t y19 = y10 ^ y8; \
  431. uintN_t y16 = t0 ^ y11; \
  432. uintN_t y21 = y13 ^ y16; \
  433. uintN_t y18 = input[7] ^ y16; \
  434. /* Make a copy of input[0] under a new name, because the core
  435. * will refer to it, and in the inverse version of the S-box
  436. * the corresponding value will be one of the calculated ones
  437. * and not in input[0] itself. */ \
  438. uintN_t i0 = input[0]; \
  439. /* end */
  440. /* Core nonlinear component, from Fig 3 of the paper. */
  441. #define SBOX_CORE(uintN_t) \
  442. uintN_t t2 = y12 & y15; \
  443. uintN_t t3 = y3 & y6; \
  444. uintN_t t4 = t3 ^ t2; \
  445. uintN_t t5 = y4 & i0; \
  446. uintN_t t6 = t5 ^ t2; \
  447. uintN_t t7 = y13 & y16; \
  448. uintN_t t8 = y5 & y1; \
  449. uintN_t t9 = t8 ^ t7; \
  450. uintN_t t10 = y2 & y7; \
  451. uintN_t t11 = t10 ^ t7; \
  452. uintN_t t12 = y9 & y11; \
  453. uintN_t t13 = y14 & y17; \
  454. uintN_t t14 = t13 ^ t12; \
  455. uintN_t t15 = y8 & y10; \
  456. uintN_t t16 = t15 ^ t12; \
  457. uintN_t t17 = t4 ^ t14; \
  458. uintN_t t18 = t6 ^ t16; \
  459. uintN_t t19 = t9 ^ t14; \
  460. uintN_t t20 = t11 ^ t16; \
  461. uintN_t t21 = t17 ^ y20; \
  462. uintN_t t22 = t18 ^ y19; \
  463. uintN_t t23 = t19 ^ y21; \
  464. uintN_t t24 = t20 ^ y18; \
  465. uintN_t t25 = t21 ^ t22; \
  466. uintN_t t26 = t21 & t23; \
  467. uintN_t t27 = t24 ^ t26; \
  468. uintN_t t28 = t25 & t27; \
  469. uintN_t t29 = t28 ^ t22; \
  470. uintN_t t30 = t23 ^ t24; \
  471. uintN_t t31 = t22 ^ t26; \
  472. uintN_t t32 = t31 & t30; \
  473. uintN_t t33 = t32 ^ t24; \
  474. uintN_t t34 = t23 ^ t33; \
  475. uintN_t t35 = t27 ^ t33; \
  476. uintN_t t36 = t24 & t35; \
  477. uintN_t t37 = t36 ^ t34; \
  478. uintN_t t38 = t27 ^ t36; \
  479. uintN_t t39 = t29 & t38; \
  480. uintN_t t40 = t25 ^ t39; \
  481. uintN_t t41 = t40 ^ t37; \
  482. uintN_t t42 = t29 ^ t33; \
  483. uintN_t t43 = t29 ^ t40; \
  484. uintN_t t44 = t33 ^ t37; \
  485. uintN_t t45 = t42 ^ t41; \
  486. uintN_t z0 = t44 & y15; \
  487. uintN_t z1 = t37 & y6; \
  488. uintN_t z2 = t33 & i0; \
  489. uintN_t z3 = t43 & y16; \
  490. uintN_t z4 = t40 & y1; \
  491. uintN_t z5 = t29 & y7; \
  492. uintN_t z6 = t42 & y11; \
  493. uintN_t z7 = t45 & y17; \
  494. uintN_t z8 = t41 & y10; \
  495. uintN_t z9 = t44 & y12; \
  496. uintN_t z10 = t37 & y3; \
  497. uintN_t z11 = t33 & y4; \
  498. uintN_t z12 = t43 & y13; \
  499. uintN_t z13 = t40 & y5; \
  500. uintN_t z14 = t29 & y2; \
  501. uintN_t z15 = t42 & y9; \
  502. uintN_t z16 = t45 & y14; \
  503. uintN_t z17 = t41 & y8; \
  504. /* end */
  505. /* Final linear transformation for the forward S-box, from Fig 4 of
  506. * the paper. */
  507. #define SBOX_FORWARD_BOTTOM_TRANSFORM(output, uintN_t) \
  508. uintN_t t46 = z15 ^ z16; \
  509. uintN_t t47 = z10 ^ z11; \
  510. uintN_t t48 = z5 ^ z13; \
  511. uintN_t t49 = z9 ^ z10; \
  512. uintN_t t50 = z2 ^ z12; \
  513. uintN_t t51 = z2 ^ z5; \
  514. uintN_t t52 = z7 ^ z8; \
  515. uintN_t t53 = z0 ^ z3; \
  516. uintN_t t54 = z6 ^ z7; \
  517. uintN_t t55 = z16 ^ z17; \
  518. uintN_t t56 = z12 ^ t48; \
  519. uintN_t t57 = t50 ^ t53; \
  520. uintN_t t58 = z4 ^ t46; \
  521. uintN_t t59 = z3 ^ t54; \
  522. uintN_t t60 = t46 ^ t57; \
  523. uintN_t t61 = z14 ^ t57; \
  524. uintN_t t62 = t52 ^ t58; \
  525. uintN_t t63 = t49 ^ t58; \
  526. uintN_t t64 = z4 ^ t59; \
  527. uintN_t t65 = t61 ^ t62; \
  528. uintN_t t66 = z1 ^ t63; \
  529. output[7] = t59 ^ t63; \
  530. output[1] = t56 ^ t62; \
  531. output[0] = t48 ^ t60; \
  532. uintN_t t67 = t64 ^ t65; \
  533. output[4] = t53 ^ t66; \
  534. output[3] = t51 ^ t66; \
  535. output[2] = t47 ^ t65; \
  536. output[6] = t64 ^ output[4]; \
  537. output[5] = t55 ^ t67; \
  538. /* end */
  539. #define BITSLICED_SUBBYTES(output, input, uintN_t) do { \
  540. SBOX_FORWARD_TOP_TRANSFORM(input, uintN_t); \
  541. SBOX_CORE(uintN_t); \
  542. SBOX_FORWARD_BOTTOM_TRANSFORM(output, uintN_t); \
  543. } while (0)
  544. /*
  545. * Initial and final linear transformations for the backward S-box. I
  546. * generated these myself, by implementing the linear-transform
  547. * optimisation algorithm in the paper, and applying it to the
  548. * matrices calculated by _their_ top and bottom transformations, pre-
  549. * and post-multiplied as appropriate by the linear map in the inverse
  550. * S_box.
  551. */
  552. #define SBOX_BACKWARD_TOP_TRANSFORM(input, uintN_t) \
  553. uintN_t y5 = input[4] ^ input[6]; \
  554. uintN_t y19 = input[3] ^ input[0]; \
  555. uintN_t itmp8 = y5 ^ input[0]; \
  556. uintN_t y4 = itmp8 ^ input[1]; \
  557. uintN_t y9 = input[4] ^ input[3]; \
  558. uintN_t y2 = y9 ^ y4; \
  559. uintN_t itmp9 = y2 ^ input[7]; \
  560. uintN_t y1 = y9 ^ input[0]; \
  561. uintN_t y6 = y5 ^ input[7]; \
  562. uintN_t y18 = y9 ^ input[5]; \
  563. uintN_t y7 = y18 ^ y2; \
  564. uintN_t y16 = y7 ^ y1; \
  565. uintN_t y21 = y7 ^ input[1]; \
  566. uintN_t y3 = input[4] ^ input[7]; \
  567. uintN_t y13 = y16 ^ y21; \
  568. uintN_t y8 = input[4] ^ y6; \
  569. uintN_t y10 = y8 ^ y19; \
  570. uintN_t y14 = y8 ^ y9; \
  571. uintN_t y20 = itmp9 ^ input[2]; \
  572. uintN_t y11 = y9 ^ y20; \
  573. uintN_t i0 = y11 ^ y7; \
  574. uintN_t y15 = i0 ^ y6; \
  575. uintN_t y17 = y16 ^ y15; \
  576. uintN_t y12 = itmp9 ^ input[3]; \
  577. /* end */
  578. #define SBOX_BACKWARD_BOTTOM_TRANSFORM(output, uintN_t) \
  579. uintN_t otmp18 = z15 ^ z6; \
  580. uintN_t otmp19 = z13 ^ otmp18; \
  581. uintN_t otmp20 = z12 ^ otmp19; \
  582. uintN_t otmp21 = z16 ^ otmp20; \
  583. uintN_t otmp22 = z8 ^ otmp21; \
  584. uintN_t otmp23 = z0 ^ otmp22; \
  585. uintN_t otmp24 = otmp22 ^ z3; \
  586. uintN_t otmp25 = otmp24 ^ z4; \
  587. uintN_t otmp26 = otmp25 ^ z2; \
  588. uintN_t otmp27 = z1 ^ otmp26; \
  589. uintN_t otmp28 = z14 ^ otmp27; \
  590. uintN_t otmp29 = otmp28 ^ z10; \
  591. output[4] = z2 ^ otmp23; \
  592. output[7] = z5 ^ otmp24; \
  593. uintN_t otmp30 = z11 ^ otmp29; \
  594. output[5] = z13 ^ otmp30; \
  595. uintN_t otmp31 = otmp25 ^ z8; \
  596. output[1] = z7 ^ otmp31; \
  597. uintN_t otmp32 = z11 ^ z9; \
  598. uintN_t otmp33 = z17 ^ otmp32; \
  599. uintN_t otmp34 = otmp30 ^ otmp33; \
  600. output[0] = z15 ^ otmp33; \
  601. uintN_t otmp35 = z12 ^ otmp34; \
  602. output[6] = otmp35 ^ z16; \
  603. uintN_t otmp36 = z1 ^ otmp23; \
  604. uintN_t otmp37 = z5 ^ otmp36; \
  605. output[2] = z4 ^ otmp37; \
  606. uintN_t otmp38 = z11 ^ output[1]; \
  607. uintN_t otmp39 = z2 ^ otmp38; \
  608. uintN_t otmp40 = z17 ^ otmp39; \
  609. uintN_t otmp41 = z0 ^ otmp40; \
  610. uintN_t otmp42 = z5 ^ otmp41; \
  611. uintN_t otmp43 = otmp42 ^ z10; \
  612. uintN_t otmp44 = otmp43 ^ z3; \
  613. output[3] = otmp44 ^ z16; \
  614. /* end */
  615. #define BITSLICED_INVSUBBYTES(output, input, uintN_t) do { \
  616. SBOX_BACKWARD_TOP_TRANSFORM(input, uintN_t); \
  617. SBOX_CORE(uintN_t); \
  618. SBOX_BACKWARD_BOTTOM_TRANSFORM(output, uintN_t); \
  619. } while (0)
  620. /* -----
  621. * The ShiftRows transformation. This operates independently on each
  622. * bit slice.
  623. */
  624. #define SINGLE_BITSLICE_SHIFTROWS(output, input, uintN_t) do \
  625. { \
  626. uintN_t mask, mask2, mask3, diff, x = (input); \
  627. /* Rotate rows 2 and 3 by 16 bits */ \
  628. mask = 0x00CC * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  629. diff = ((x >> 8) ^ x) & mask; \
  630. x ^= diff ^ (diff << 8); \
  631. /* Rotate rows 1 and 3 by 8 bits */ \
  632. mask = 0x0AAA * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  633. mask2 = 0xA000 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  634. mask3 = 0x5555 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  635. x = ((x >> 4) & mask) | ((x << 12) & mask2) | (x & mask3); \
  636. /* Write output */ \
  637. (output) = x; \
  638. } while (0)
  639. #define SINGLE_BITSLICE_INVSHIFTROWS(output, input, uintN_t) do \
  640. { \
  641. uintN_t mask, mask2, mask3, diff, x = (input); \
  642. /* Rotate rows 2 and 3 by 16 bits */ \
  643. mask = 0x00CC * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  644. diff = ((x >> 8) ^ x) & mask; \
  645. x ^= diff ^ (diff << 8); \
  646. /* Rotate rows 1 and 3 by 8 bits, the opposite way to ShiftRows */ \
  647. mask = 0x000A * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  648. mask2 = 0xAAA0 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  649. mask3 = 0x5555 * (((uintN_t)~(uintN_t)0) / 0xFFFF); \
  650. x = ((x >> 12) & mask) | ((x << 4) & mask2) | (x & mask3); \
  651. /* Write output */ \
  652. (output) = x; \
  653. } while (0)
  654. #define BITSLICED_SHIFTROWS(output, input, uintN_t) do \
  655. { \
  656. ITERATE(SINGLE_BITSLICE_SHIFTROWS, output, input, uintN_t); \
  657. } while (0)
  658. #define BITSLICED_INVSHIFTROWS(output, input, uintN_t) do \
  659. { \
  660. ITERATE(SINGLE_BITSLICE_INVSHIFTROWS, output, input, uintN_t); \
  661. } while (0)
  662. /* -----
  663. * The MixColumns transformation. This has to operate on all eight bit
  664. * slices at once, and also passes data back and forth between the
  665. * bits in an adjacent group of 4 within each slice.
  666. *
  667. * Notation: let F = GF(2)[X]/<X^8+X^4+X^3+X+1> be the finite field
  668. * used in AES, and let R = F[Y]/<Y^4+1> be the ring whose elements
  669. * represent the possible contents of a column of the matrix. I use X
  670. * and Y below in those senses, i.e. X is the value in F that
  671. * represents the byte 0x02, and Y is the value in R that cycles the
  672. * four bytes around by one if you multiply by it.
  673. */
  674. /* Multiply every column by Y^3, i.e. cycle it round one place to the
  675. * right. Operates on one bit slice at a time; you have to wrap it in
  676. * ITERATE to affect all the data at once. */
  677. #define BITSLICED_MUL_BY_Y3(output, input, uintN_t) do \
  678. { \
  679. uintN_t mask, mask2, x; \
  680. mask = 0x8 * (((uintN_t)~(uintN_t)0) / 0xF); \
  681. mask2 = 0x7 * (((uintN_t)~(uintN_t)0) / 0xF); \
  682. x = input; \
  683. output = ((x << 3) & mask) ^ ((x >> 1) & mask2); \
  684. } while (0)
  685. /* Multiply every column by Y^2. */
  686. #define BITSLICED_MUL_BY_Y2(output, input, uintN_t) do \
  687. { \
  688. uintN_t mask, mask2, x; \
  689. mask = 0xC * (((uintN_t)~(uintN_t)0) / 0xF); \
  690. mask2 = 0x3 * (((uintN_t)~(uintN_t)0) / 0xF); \
  691. x = input; \
  692. output = ((x << 2) & mask) ^ ((x >> 2) & mask2); \
  693. } while (0)
  694. #define BITSLICED_MUL_BY_1_Y3(output, input, uintN_t) do \
  695. { \
  696. uintN_t tmp = input; \
  697. BITSLICED_MUL_BY_Y3(tmp, input, uintN_t); \
  698. output = input ^ tmp; \
  699. } while (0)
  700. /* Multiply every column by 1+Y^2. */
  701. #define BITSLICED_MUL_BY_1_Y2(output, input, uintN_t) do \
  702. { \
  703. uintN_t tmp = input; \
  704. BITSLICED_MUL_BY_Y2(tmp, input, uintN_t); \
  705. output = input ^ tmp; \
  706. } while (0)
  707. /* Multiply every field element by X. This has to feed data between
  708. * slices, so it does the whole job in one go without needing ITERATE. */
  709. #define BITSLICED_MUL_BY_X(output, input, uintN_t) do \
  710. { \
  711. uintN_t bit7 = input[7]; \
  712. output[7] = input[6]; \
  713. output[6] = input[5]; \
  714. output[5] = input[4]; \
  715. output[4] = input[3] ^ bit7; \
  716. output[3] = input[2] ^ bit7; \
  717. output[2] = input[1]; \
  718. output[1] = input[0] ^ bit7; \
  719. output[0] = bit7; \
  720. } while (0)
  721. /*
  722. * The MixColumns constant is
  723. * M = X + Y + Y^2 + (X+1)Y^3
  724. * which we construct by rearranging it into
  725. * M = 1 + (1+Y^3) [ X + (1+Y^2) ]
  726. */
  727. #define BITSLICED_MIXCOLUMNS(output, input, uintN_t) do \
  728. { \
  729. uintN_t a[8], aX[8], b[8]; \
  730. /* a = input * (1+Y^3) */ \
  731. ITERATE(BITSLICED_MUL_BY_1_Y3, a, input, uintN_t); \
  732. /* aX = a * X */ \
  733. BITSLICED_MUL_BY_X(aX, a, uintN_t); \
  734. /* b = a * (1+Y^2) = input * (1+Y+Y^2+Y^3) */ \
  735. ITERATE(BITSLICED_MUL_BY_1_Y2, b, a, uintN_t); \
  736. /* output = input + aX + b (reusing a as a temp */ \
  737. BITSLICED_ADD(a, aX, b); \
  738. BITSLICED_ADD(output, input, a); \
  739. } while (0)
  740. /*
  741. * The InvMixColumns constant, written out longhand, is
  742. * I = (X^3+X^2+X) + (X^3+1)Y + (X^3+X^2+1)Y^2 + (X^3+X+1)Y^3
  743. * We represent this as
  744. * I = (X^3+X^2+X+1)(Y^3+Y^2+Y+1) + 1 + X(Y+Y^2) + X^2(Y+Y^3)
  745. */
  746. #define BITSLICED_INVMIXCOLUMNS(output, input, uintN_t) do \
  747. { \
  748. /* We need input * X^i for i=1,...,3 */ \
  749. uintN_t X[8], X2[8], X3[8]; \
  750. BITSLICED_MUL_BY_X(X, input, uintN_t); \
  751. BITSLICED_MUL_BY_X(X2, X, uintN_t); \
  752. BITSLICED_MUL_BY_X(X3, X2, uintN_t); \
  753. /* Sum them all and multiply by 1+Y+Y^2+Y^3. */ \
  754. uintN_t S[8]; \
  755. BITSLICED_ADD(S, input, X); \
  756. BITSLICED_ADD(S, S, X2); \
  757. BITSLICED_ADD(S, S, X3); \
  758. ITERATE(BITSLICED_MUL_BY_1_Y3, S, S, uintN_t); \
  759. ITERATE(BITSLICED_MUL_BY_1_Y2, S, S, uintN_t); \
  760. /* Compute the X(Y+Y^2) term. */ \
  761. uintN_t A[8]; \
  762. ITERATE(BITSLICED_MUL_BY_1_Y3, A, X, uintN_t); \
  763. ITERATE(BITSLICED_MUL_BY_Y2, A, A, uintN_t); \
  764. /* Compute the X^2(Y+Y^3) term. */ \
  765. uintN_t B[8]; \
  766. ITERATE(BITSLICED_MUL_BY_1_Y2, B, X2, uintN_t); \
  767. ITERATE(BITSLICED_MUL_BY_Y3, B, B, uintN_t); \
  768. /* And add all the pieces together. */ \
  769. BITSLICED_ADD(S, S, input); \
  770. BITSLICED_ADD(S, S, A); \
  771. BITSLICED_ADD(output, S, B); \
  772. } while (0)
  773. /* -----
  774. * Put it all together into a cipher round.
  775. */
  776. /* Dummy macro to get rid of the MixColumns in the final round. */
  777. #define NO_MIXCOLUMNS(out, in, uintN_t) do {} while (0)
  778. #define ENCRYPT_ROUND_FN(suffix, uintN_t, mixcol_macro) \
  779. static void aes_sliced_round_e_##suffix( \
  780. uintN_t output[8], const uintN_t input[8], const uintN_t roundkey[8]) \
  781. { \
  782. BITSLICED_SUBBYTES(output, input, uintN_t); \
  783. BITSLICED_SHIFTROWS(output, output, uintN_t); \
  784. mixcol_macro(output, output, uintN_t); \
  785. BITSLICED_ADD(output, output, roundkey); \
  786. }
  787. ENCRYPT_ROUND_FN(serial, uint16_t, BITSLICED_MIXCOLUMNS)
  788. ENCRYPT_ROUND_FN(serial_last, uint16_t, NO_MIXCOLUMNS)
  789. ENCRYPT_ROUND_FN(parallel, BignumInt, BITSLICED_MIXCOLUMNS)
  790. ENCRYPT_ROUND_FN(parallel_last, BignumInt, NO_MIXCOLUMNS)
  791. #define DECRYPT_ROUND_FN(suffix, uintN_t, mixcol_macro) \
  792. static void aes_sliced_round_d_##suffix( \
  793. uintN_t output[8], const uintN_t input[8], const uintN_t roundkey[8]) \
  794. { \
  795. BITSLICED_ADD(output, input, roundkey); \
  796. mixcol_macro(output, output, uintN_t); \
  797. BITSLICED_INVSUBBYTES(output, output, uintN_t); \
  798. BITSLICED_INVSHIFTROWS(output, output, uintN_t); \
  799. }
  800. #if 0 /* no cipher mode we support requires serial decryption */
  801. DECRYPT_ROUND_FN(serial, uint16_t, BITSLICED_INVMIXCOLUMNS)
  802. DECRYPT_ROUND_FN(serial_first, uint16_t, NO_MIXCOLUMNS)
  803. #endif
  804. DECRYPT_ROUND_FN(parallel, BignumInt, BITSLICED_INVMIXCOLUMNS)
  805. DECRYPT_ROUND_FN(parallel_first, BignumInt, NO_MIXCOLUMNS)
  806. #endif // WINSCP_VS
  807. /* -----
  808. * Key setup function.
  809. */
  810. typedef struct aes_sliced_key aes_sliced_key;
  811. struct aes_sliced_key {
  812. BignumInt roundkeys_parallel[MAXROUNDKEYS * 8];
  813. uint16_t roundkeys_serial[MAXROUNDKEYS * 8];
  814. unsigned rounds;
  815. };
  816. /*WINSCP static*/ void aes_sliced_key_setup(
  817. aes_sliced_key *sk, const void *vkey, size_t keybits)
  818. #ifndef WINSCP_VS
  819. ;
  820. #else
  821. {
  822. const unsigned char *key = (const unsigned char *)vkey;
  823. size_t key_words = keybits / 32;
  824. sk->rounds = key_words + 6;
  825. size_t sched_words = (sk->rounds + 1) * 4;
  826. unsigned rconpos = 0;
  827. uint16_t *outslices = sk->roundkeys_serial;
  828. unsigned outshift = 0;
  829. memset(sk->roundkeys_serial, 0, sizeof(sk->roundkeys_serial));
  830. uint8_t inblk[16];
  831. memset(inblk, 0, 16);
  832. uint16_t slices[8];
  833. for (size_t i = 0; i < sched_words; i++) {
  834. /*
  835. * Prepare a word of round key in the low 4 bits of each
  836. * integer in slices[].
  837. */
  838. if (i < key_words) {
  839. memcpy(inblk, key + 4*i, 4);
  840. TO_BITSLICES(slices, inblk, uint16_t, =, 0);
  841. } else {
  842. unsigned wordindex, bitshift;
  843. uint16_t *prevslices;
  844. /* Fetch the (i-1)th key word */
  845. wordindex = i-1;
  846. bitshift = 4 * (wordindex & 3);
  847. prevslices = sk->roundkeys_serial + 8 * (wordindex >> 2);
  848. for (size_t i = 0; i < 8; i++)
  849. slices[i] = prevslices[i] >> bitshift;
  850. /* Decide what we're doing in this expansion stage */
  851. bool rotate_and_round_constant = (i % key_words == 0);
  852. bool sub = rotate_and_round_constant ||
  853. (key_words == 8 && i % 8 == 4);
  854. if (rotate_and_round_constant) {
  855. for (size_t i = 0; i < 8; i++)
  856. slices[i] = ((slices[i] << 3) | (slices[i] >> 1)) & 0xF;
  857. }
  858. if (sub) {
  859. /* Apply the SubBytes transform to the key word. But
  860. * here we need to apply the _full_ SubBytes from the
  861. * spec, including the constant which our S-box leaves
  862. * out. */
  863. BITSLICED_SUBBYTES(slices, slices, uint16_t);
  864. slices[0] ^= 0xFFFF;
  865. slices[1] ^= 0xFFFF;
  866. slices[5] ^= 0xFFFF;
  867. slices[6] ^= 0xFFFF;
  868. }
  869. if (rotate_and_round_constant) {
  870. assert(rconpos < lenof(key_setup_round_constants));
  871. uint8_t rcon = key_setup_round_constants[rconpos++];
  872. for (size_t i = 0; i < 8; i++)
  873. slices[i] ^= 1 & (rcon >> i);
  874. }
  875. /* Combine with the (i-Nk)th key word */
  876. wordindex = i - key_words;
  877. bitshift = 4 * (wordindex & 3);
  878. prevslices = sk->roundkeys_serial + 8 * (wordindex >> 2);
  879. for (size_t i = 0; i < 8; i++)
  880. slices[i] ^= prevslices[i] >> bitshift;
  881. }
  882. /*
  883. * Now copy it into sk.
  884. */
  885. for (unsigned b = 0; b < 8; b++)
  886. outslices[b] |= (slices[b] & 0xF) << outshift;
  887. outshift += 4;
  888. if (outshift == 16) {
  889. outshift = 0;
  890. outslices += 8;
  891. }
  892. }
  893. smemclr(inblk, sizeof(inblk));
  894. smemclr(slices, sizeof(slices));
  895. /*
  896. * Add the S-box constant to every round key after the first one,
  897. * compensating for it being left out in the main cipher.
  898. */
  899. for (size_t i = 8; i < 8 * (sched_words/4); i += 8) {
  900. sk->roundkeys_serial[i+0] ^= 0xFFFF;
  901. sk->roundkeys_serial[i+1] ^= 0xFFFF;
  902. sk->roundkeys_serial[i+5] ^= 0xFFFF;
  903. sk->roundkeys_serial[i+6] ^= 0xFFFF;
  904. }
  905. /*
  906. * Replicate that set of round keys into larger integers for the
  907. * parallel versions of the cipher.
  908. */
  909. for (size_t i = 0; i < 8 * (sched_words / 4); i++) {
  910. sk->roundkeys_parallel[i] = sk->roundkeys_serial[i] *
  911. ((BignumInt)~(BignumInt)0 / 0xFFFF);
  912. }
  913. }
  914. #endif
  915. #ifdef WINSCP_VS
  916. /* -----
  917. * The full cipher primitive, including transforming the input and
  918. * output to/from bit-sliced form.
  919. */
  920. #define ENCRYPT_FN(suffix, uintN_t, nblocks) \
  921. static void aes_sliced_e_##suffix( \
  922. uint8_t *output, const uint8_t *input, const aes_sliced_key *sk) \
  923. { \
  924. uintN_t state[8]; \
  925. TO_BITSLICES(state, input, uintN_t, =, 0); \
  926. for (unsigned i = 1; i < nblocks; i++) { \
  927. input += 16; \
  928. TO_BITSLICES(state, input, uintN_t, |=, i*16); \
  929. } \
  930. const uintN_t *keys = sk->roundkeys_##suffix; \
  931. BITSLICED_ADD(state, state, keys); \
  932. keys += 8; \
  933. for (unsigned i = 0; i < sk->rounds-1; i++) { \
  934. aes_sliced_round_e_##suffix(state, state, keys); \
  935. keys += 8; \
  936. } \
  937. aes_sliced_round_e_##suffix##_last(state, state, keys); \
  938. for (unsigned i = 0; i < nblocks; i++) { \
  939. FROM_BITSLICES(output, state, i*16); \
  940. output += 16; \
  941. } \
  942. }
  943. #define DECRYPT_FN(suffix, uintN_t, nblocks) \
  944. static void aes_sliced_d_##suffix( \
  945. uint8_t *output, const uint8_t *input, const aes_sliced_key *sk) \
  946. { \
  947. uintN_t state[8]; \
  948. TO_BITSLICES(state, input, uintN_t, =, 0); \
  949. for (unsigned i = 1; i < nblocks; i++) { \
  950. input += 16; \
  951. TO_BITSLICES(state, input, uintN_t, |=, i*16); \
  952. } \
  953. const uintN_t *keys = sk->roundkeys_##suffix + 8*sk->rounds; \
  954. aes_sliced_round_d_##suffix##_first(state, state, keys); \
  955. keys -= 8; \
  956. for (unsigned i = 0; i < sk->rounds-1; i++) { \
  957. aes_sliced_round_d_##suffix(state, state, keys); \
  958. keys -= 8; \
  959. } \
  960. BITSLICED_ADD(state, state, keys); \
  961. for (unsigned i = 0; i < nblocks; i++) { \
  962. FROM_BITSLICES(output, state, i*16); \
  963. output += 16; \
  964. } \
  965. }
  966. ENCRYPT_FN(serial, uint16_t, 1)
  967. #if 0 /* no cipher mode we support requires serial decryption */
  968. DECRYPT_FN(serial, uint16_t, 1)
  969. #endif
  970. ENCRYPT_FN(parallel, BignumInt, SLICE_PARALLELISM)
  971. DECRYPT_FN(parallel, BignumInt, SLICE_PARALLELISM)
  972. #endif // WINSCP_VS
  973. /* -----
  974. * The SSH interface and the cipher modes.
  975. */
  976. #define SDCTR_WORDS (16 / BIGNUM_INT_BYTES)
  977. typedef struct aes_sw_context aes_sw_context;
  978. struct aes_sw_context {
  979. aes_sliced_key sk;
  980. union {
  981. struct {
  982. /* In CBC mode, the IV is just a copy of the last seen
  983. * cipher block. */
  984. uint8_t prevblk[16];
  985. } cbc;
  986. struct {
  987. /* In SDCTR mode, we keep the counter itself in a form
  988. * that's easy to increment. We also use the parallel
  989. * version of the core AES function, so we'll encrypt
  990. * multiple counter values in one go. That won't align
  991. * nicely with the sizes of data we're asked to encrypt,
  992. * so we must also store a cache of the last set of
  993. * keystream blocks we generated, and our current position
  994. * within that cache. */
  995. BignumInt counter[SDCTR_WORDS];
  996. uint8_t keystream[SLICE_PARALLELISM * 16];
  997. uint8_t *keystream_pos;
  998. } sdctr;
  999. } iv;
  1000. ssh_cipher ciph;
  1001. };
  1002. #ifndef WINSCP_VS
  1003. static ssh_cipher *aes_sw_new(const ssh_cipheralg *alg)
  1004. {
  1005. aes_sw_context *ctx = snew(aes_sw_context);
  1006. ctx->ciph.vt = alg;
  1007. return &ctx->ciph;
  1008. }
  1009. static void aes_sw_free(ssh_cipher *ciph)
  1010. {
  1011. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1012. smemclr(ctx, sizeof(*ctx));
  1013. sfree(ctx);
  1014. }
  1015. static void aes_sw_setkey(ssh_cipher *ciph, const void *vkey)
  1016. {
  1017. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1018. aes_sliced_key_setup(&ctx->sk, vkey, ctx->ciph.vt->real_keybits);
  1019. }
  1020. static void aes_sw_setiv_cbc(ssh_cipher *ciph, const void *iv)
  1021. {
  1022. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1023. memcpy(ctx->iv.cbc.prevblk, iv, 16);
  1024. }
  1025. static void aes_sw_setiv_sdctr(ssh_cipher *ciph, const void *viv)
  1026. {
  1027. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1028. const uint8_t *iv = (const uint8_t *)viv;
  1029. /* Import the initial counter value into the internal representation */
  1030. unsigned i; // WINSCP
  1031. for (i = 0; i < SDCTR_WORDS; i++)
  1032. ctx->iv.sdctr.counter[i] =
  1033. GET_BIGNUMINT_MSB_FIRST(
  1034. iv + 16 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES);
  1035. /* Set keystream_pos to indicate that the keystream cache is
  1036. * currently empty */
  1037. ctx->iv.sdctr.keystream_pos =
  1038. ctx->iv.sdctr.keystream + sizeof(ctx->iv.sdctr.keystream);
  1039. }
  1040. #endif
  1041. typedef void (*aes_sw_fn)(uint32_t v[4], const uint32_t *keysched);
  1042. #ifdef WINSCP_VS
  1043. static inline void memxor16(void *vout, const void *vlhs, const void *vrhs)
  1044. {
  1045. uint8_t *out = (uint8_t *)vout;
  1046. const uint8_t *lhs = (const uint8_t *)vlhs, *rhs = (const uint8_t *)vrhs;
  1047. uint64_t w;
  1048. w = GET_64BIT_LSB_FIRST(lhs);
  1049. w ^= GET_64BIT_LSB_FIRST(rhs);
  1050. PUT_64BIT_LSB_FIRST(out, w);
  1051. w = GET_64BIT_LSB_FIRST(lhs + 8);
  1052. w ^= GET_64BIT_LSB_FIRST(rhs + 8);
  1053. PUT_64BIT_LSB_FIRST(out + 8, w);
  1054. }
  1055. static inline void aes_cbc_sw_encrypt(
  1056. ssh_cipher *ciph, void *vblk, int blklen)
  1057. {
  1058. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1059. /*
  1060. * CBC encryption has to be done serially, because the input to
  1061. * each run of the cipher includes the output from the previous
  1062. * run.
  1063. */
  1064. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1065. blk < finish; blk += 16) {
  1066. /*
  1067. * We use the IV array itself as the location for the
  1068. * encryption, because there's no reason not to.
  1069. */
  1070. /* XOR the new plaintext block into the previous cipher block */
  1071. memxor16(ctx->iv.cbc.prevblk, ctx->iv.cbc.prevblk, blk);
  1072. /* Run the cipher over the result, which leaves it
  1073. * conveniently already stored in ctx->iv */
  1074. aes_sliced_e_serial(
  1075. ctx->iv.cbc.prevblk, ctx->iv.cbc.prevblk, &ctx->sk);
  1076. /* Copy it to the output location */
  1077. memcpy(blk, ctx->iv.cbc.prevblk, 16);
  1078. }
  1079. }
  1080. static inline void aes_cbc_sw_decrypt(
  1081. ssh_cipher *ciph, void *vblk, int blklen)
  1082. {
  1083. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1084. uint8_t *blk = (uint8_t *)vblk;
  1085. /*
  1086. * CBC decryption can run in parallel, because all the
  1087. * _ciphertext_ blocks are already available.
  1088. */
  1089. size_t blocks_remaining = blklen / 16;
  1090. uint8_t data[SLICE_PARALLELISM * 16];
  1091. /* Zeroing the data array is probably overcautious, but it avoids
  1092. * technically undefined behaviour from leaving it uninitialised
  1093. * if our very first iteration doesn't include enough cipher
  1094. * blocks to populate it fully */
  1095. memset(data, 0, sizeof(data));
  1096. while (blocks_remaining > 0) {
  1097. /* Number of blocks we'll handle in this iteration. If we're
  1098. * dealing with fewer than the maximum, it doesn't matter -
  1099. * it's harmless to run the full parallel cipher function
  1100. * anyway. */
  1101. size_t blocks = (blocks_remaining < SLICE_PARALLELISM ?
  1102. blocks_remaining : SLICE_PARALLELISM);
  1103. /* Parallel-decrypt the input, in a separate array so we still
  1104. * have the cipher stream available for XORing. */
  1105. memcpy(data, blk, 16 * blocks);
  1106. aes_sliced_d_parallel(data, data, &ctx->sk);
  1107. /* Write the output and update the IV */
  1108. for (size_t i = 0; i < blocks; i++) {
  1109. uint8_t *decrypted = data + 16*i;
  1110. uint8_t *output = blk + 16*i;
  1111. memxor16(decrypted, decrypted, ctx->iv.cbc.prevblk);
  1112. memcpy(ctx->iv.cbc.prevblk, output, 16);
  1113. memcpy(output, decrypted, 16);
  1114. }
  1115. /* Advance the input pointer. */
  1116. blk += 16 * blocks;
  1117. blocks_remaining -= blocks;
  1118. }
  1119. smemclr(data, sizeof(data));
  1120. }
  1121. static inline void aes_sdctr_sw(
  1122. ssh_cipher *ciph, void *vblk, int blklen)
  1123. {
  1124. aes_sw_context *ctx = container_of(ciph, aes_sw_context, ciph);
  1125. /*
  1126. * SDCTR encrypt/decrypt loops round one block at a time XORing
  1127. * the keystream into the user's data, and periodically has to run
  1128. * a parallel encryption operation to get more keystream.
  1129. */
  1130. uint8_t *keystream_end =
  1131. ctx->iv.sdctr.keystream + sizeof(ctx->iv.sdctr.keystream);
  1132. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1133. blk < finish; blk += 16) {
  1134. if (ctx->iv.sdctr.keystream_pos == keystream_end) {
  1135. /*
  1136. * Generate some keystream.
  1137. */
  1138. for (uint8_t *block = ctx->iv.sdctr.keystream;
  1139. block < keystream_end; block += 16) {
  1140. /* Format the counter value into the buffer. */
  1141. for (unsigned i = 0; i < SDCTR_WORDS; i++)
  1142. PUT_BIGNUMINT_MSB_FIRST(
  1143. block + 16 - BIGNUM_INT_BYTES - i*BIGNUM_INT_BYTES,
  1144. ctx->iv.sdctr.counter[i]);
  1145. /* Increment the counter. */
  1146. BignumCarry carry = 1;
  1147. for (unsigned i = 0; i < SDCTR_WORDS; i++)
  1148. BignumADC(ctx->iv.sdctr.counter[i], carry,
  1149. ctx->iv.sdctr.counter[i], 0, carry);
  1150. }
  1151. /* Encrypt all those counter blocks. */
  1152. aes_sliced_e_parallel(ctx->iv.sdctr.keystream,
  1153. ctx->iv.sdctr.keystream, &ctx->sk);
  1154. /* Reset keystream_pos to the start of the buffer. */
  1155. ctx->iv.sdctr.keystream_pos = ctx->iv.sdctr.keystream;
  1156. }
  1157. memxor16(blk, blk, ctx->iv.sdctr.keystream_pos);
  1158. ctx->iv.sdctr.keystream_pos += 16;
  1159. }
  1160. }
  1161. #define SW_ENC_DEC(len) \
  1162. /*WINSCP static*/ void aes##len##_cbc_sw_encrypt( \
  1163. ssh_cipher *ciph, void *vblk, int blklen) \
  1164. { aes_cbc_sw_encrypt(ciph, vblk, blklen); } \
  1165. /*WINSCP static*/ void aes##len##_cbc_sw_decrypt( \
  1166. ssh_cipher *ciph, void *vblk, int blklen) \
  1167. { aes_cbc_sw_decrypt(ciph, vblk, blklen); } \
  1168. /*WINSCP static*/ void aes##len##_sdctr_sw( \
  1169. ssh_cipher *ciph, void *vblk, int blklen) \
  1170. { aes_sdctr_sw(ciph, vblk, blklen); }
  1171. SW_ENC_DEC(128)
  1172. SW_ENC_DEC(192)
  1173. SW_ENC_DEC(256)
  1174. #endif
  1175. /* ----------------------------------------------------------------------
  1176. * Hardware-accelerated implementation of AES using x86 AES-NI.
  1177. */
  1178. #if HW_AES == HW_AES_NI
  1179. #ifdef WINSCP_VS
  1180. /*
  1181. * Set target architecture for Clang and GCC
  1182. */
  1183. #if !defined(__clang__) && defined(__GNUC__)
  1184. # pragma GCC target("aes")
  1185. # pragma GCC target("sse4.1")
  1186. #endif
  1187. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
  1188. # define FUNC_ISA __attribute__ ((target("sse4.1,aes")))
  1189. #else
  1190. # define FUNC_ISA
  1191. #endif
  1192. #include <wmmintrin.h>
  1193. #include <smmintrin.h>
  1194. #if defined(__clang__) || defined(__GNUC__)
  1195. #include <cpuid.h>
  1196. #define GET_CPU_ID(out) __cpuid(1, (out)[0], (out)[1], (out)[2], (out)[3])
  1197. #else
  1198. #define GET_CPU_ID(out) __cpuid(out, 1)
  1199. #endif
  1200. bool aes_hw_available(void)
  1201. {
  1202. /*
  1203. * Determine if AES is available on this CPU, by checking that
  1204. * both AES itself and SSE4.1 are supported.
  1205. */
  1206. unsigned int CPUInfo[4];
  1207. GET_CPU_ID(CPUInfo);
  1208. return (CPUInfo[2] & (1 << 25)) && (CPUInfo[2] & (1 << 19));
  1209. }
  1210. /*
  1211. * Core AES-NI encrypt/decrypt functions, one per length and direction.
  1212. */
  1213. #define NI_CIPHER(len, dir, dirlong, repmacro) \
  1214. static FUNC_ISA inline __m128i aes_ni_##len##_##dir( \
  1215. __m128i v, const __m128i *keysched) \
  1216. { \
  1217. v = _mm_xor_si128(v, *keysched++); \
  1218. repmacro(v = _mm_aes##dirlong##_si128(v, *keysched++);); \
  1219. return _mm_aes##dirlong##last_si128(v, *keysched); \
  1220. }
  1221. NI_CIPHER(128, e, enc, REP9)
  1222. NI_CIPHER(128, d, dec, REP9)
  1223. NI_CIPHER(192, e, enc, REP11)
  1224. NI_CIPHER(192, d, dec, REP11)
  1225. NI_CIPHER(256, e, enc, REP13)
  1226. NI_CIPHER(256, d, dec, REP13)
  1227. /*
  1228. * The main key expansion.
  1229. */
  1230. static FUNC_ISA void aes_ni_key_expand(
  1231. const unsigned char *key, size_t key_words,
  1232. __m128i *keysched_e, __m128i *keysched_d)
  1233. {
  1234. size_t rounds = key_words + 6;
  1235. size_t sched_words = (rounds + 1) * 4;
  1236. /*
  1237. * Store the key schedule as 32-bit integers during expansion, so
  1238. * that it's easy to refer back to individual previous words. We
  1239. * collect them into the final __m128i form at the end.
  1240. */
  1241. uint32_t sched[MAXROUNDKEYS * 4];
  1242. unsigned rconpos = 0;
  1243. for (size_t i = 0; i < sched_words; i++) {
  1244. if (i < key_words) {
  1245. sched[i] = GET_32BIT_LSB_FIRST(key + 4 * i);
  1246. } else {
  1247. uint32_t temp = sched[i - 1];
  1248. bool rotate_and_round_constant = (i % key_words == 0);
  1249. bool only_sub = (key_words == 8 && i % 8 == 4);
  1250. if (rotate_and_round_constant) {
  1251. __m128i v = _mm_setr_epi32(0,temp,0,0);
  1252. v = _mm_aeskeygenassist_si128(v, 0);
  1253. temp = _mm_extract_epi32(v, 1);
  1254. assert(rconpos < lenof(key_setup_round_constants));
  1255. temp ^= key_setup_round_constants[rconpos++];
  1256. } else if (only_sub) {
  1257. __m128i v = _mm_setr_epi32(0,temp,0,0);
  1258. v = _mm_aeskeygenassist_si128(v, 0);
  1259. temp = _mm_extract_epi32(v, 0);
  1260. }
  1261. sched[i] = sched[i - key_words] ^ temp;
  1262. }
  1263. }
  1264. /*
  1265. * Combine the key schedule words into __m128i vectors and store
  1266. * them in the output context.
  1267. */
  1268. for (size_t round = 0; round <= rounds; round++)
  1269. keysched_e[round] = _mm_setr_epi32(
  1270. sched[4*round ], sched[4*round+1],
  1271. sched[4*round+2], sched[4*round+3]);
  1272. smemclr(sched, sizeof(sched));
  1273. /*
  1274. * Now prepare the modified keys for the inverse cipher.
  1275. */
  1276. for (size_t eround = 0; eround <= rounds; eround++) {
  1277. size_t dround = rounds - eround;
  1278. __m128i rkey = keysched_e[eround];
  1279. if (eround && dround) /* neither first nor last */
  1280. rkey = _mm_aesimc_si128(rkey);
  1281. keysched_d[dround] = rkey;
  1282. }
  1283. }
  1284. // WINSCP
  1285. // WORKAROUND
  1286. // Cannot use _mm_setr_epi* - it results in the constant being stored in .rdata segment.
  1287. // objconv reports:
  1288. // Warning 1060: Different alignments specified for same segment, %s. Using highest alignment.rdata
  1289. // Despite that the code crashes.
  1290. // This macro is based on:
  1291. // Based on https://stackoverflow.com/q/35268036/850848
  1292. #define _MM_SETR_EPI8(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af) \
  1293. { (char)a0, (char)a1, (char)a2, (char)a3, (char)a4, (char)a5, (char)a6, (char)a7, \
  1294. (char)a8, (char)a9, (char)aa, (char)ab, (char)ac, (char)ad, (char)ae, (char)af }
  1295. /*
  1296. * Auxiliary routine to increment the 128-bit counter used in SDCTR
  1297. * mode.
  1298. */
  1299. static FUNC_ISA inline __m128i aes_ni_sdctr_increment(__m128i v)
  1300. {
  1301. const __m128i ONE = _MM_SETR_EPI8(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); // WINSCP
  1302. const __m128i ZERO = _mm_setzero_si128();
  1303. /* Increment the low-order 64 bits of v */
  1304. v = _mm_add_epi64(v, ONE);
  1305. /* Check if they've become zero */
  1306. __m128i cmp = _mm_cmpeq_epi64(v, ZERO);
  1307. /* If so, the low half of cmp is all 1s. Pack that into the high
  1308. * half of addend with zero in the low half. */
  1309. __m128i addend = _mm_unpacklo_epi64(ZERO, cmp);
  1310. /* And subtract that from v, which increments the high 64 bits iff
  1311. * the low 64 wrapped round. */
  1312. v = _mm_sub_epi64(v, addend);
  1313. return v;
  1314. }
  1315. /*
  1316. * Auxiliary routine to reverse the byte order of a vector, so that
  1317. * the SDCTR IV can be made big-endian for feeding to the cipher.
  1318. */
  1319. static FUNC_ISA inline __m128i aes_ni_sdctr_reverse(__m128i v)
  1320. {
  1321. const __m128i R = _MM_SETR_EPI8(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0); // WINSCP
  1322. v = _mm_shuffle_epi8(
  1323. v, R); // WINSCP
  1324. return v;
  1325. }
  1326. /*
  1327. * The SSH interface and the cipher modes.
  1328. */
  1329. typedef struct aes_ni_context aes_ni_context;
  1330. struct aes_ni_context {
  1331. __m128i keysched_e[MAXROUNDKEYS], keysched_d[MAXROUNDKEYS], iv;
  1332. void *pointer_to_free;
  1333. ssh_cipher ciph;
  1334. };
  1335. /*static WINSCP*/ ssh_cipher *aes_hw_new(const ssh_cipheralg *alg)
  1336. {
  1337. if (!aes_hw_available_cached())
  1338. return NULL;
  1339. /*
  1340. * The __m128i variables in the context structure need to be
  1341. * 16-byte aligned, but not all malloc implementations that this
  1342. * code has to work with will guarantee to return a 16-byte
  1343. * aligned pointer. So we over-allocate, manually realign the
  1344. * pointer ourselves, and store the original one inside the
  1345. * context so we know how to free it later.
  1346. */
  1347. void *allocation = smalloc(sizeof(aes_ni_context) + 15);
  1348. uintptr_t alloc_address = (uintptr_t)allocation;
  1349. uintptr_t aligned_address = (alloc_address + 15) & ~15;
  1350. aes_ni_context *ctx = (aes_ni_context *)aligned_address;
  1351. ctx->ciph.vt = alg;
  1352. ctx->pointer_to_free = allocation;
  1353. return &ctx->ciph;
  1354. }
  1355. /*static WINSCP*/ void aes_hw_free(ssh_cipher *ciph)
  1356. {
  1357. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1358. void *allocation = ctx->pointer_to_free;
  1359. smemclr(ctx, sizeof(*ctx));
  1360. sfree(allocation);
  1361. }
  1362. /*static WINSCP*/ void aes_hw_setkey(ssh_cipher *ciph, const void *vkey)
  1363. {
  1364. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1365. const unsigned char *key = (const unsigned char *)vkey;
  1366. aes_ni_key_expand(key, ctx->ciph.vt->real_keybits / 32,
  1367. ctx->keysched_e, ctx->keysched_d);
  1368. }
  1369. /*static WINSCP*/ FUNC_ISA void aes_hw_setiv_cbc(ssh_cipher *ciph, const void *iv)
  1370. {
  1371. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1372. ctx->iv = _mm_loadu_si128(iv);
  1373. }
  1374. /*static WINSCP*/ FUNC_ISA void aes_hw_setiv_sdctr(ssh_cipher *ciph, const void *iv)
  1375. {
  1376. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1377. __m128i counter = _mm_loadu_si128(iv);
  1378. ctx->iv = aes_ni_sdctr_reverse(counter);
  1379. }
  1380. typedef __m128i (*aes_ni_fn)(__m128i v, const __m128i *keysched);
  1381. static FUNC_ISA inline void aes_cbc_ni_encrypt(
  1382. ssh_cipher *ciph, void *vblk, int blklen, aes_ni_fn encrypt)
  1383. {
  1384. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1385. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1386. blk < finish; blk += 16) {
  1387. __m128i plaintext = _mm_loadu_si128((const __m128i *)blk);
  1388. __m128i cipher_input = _mm_xor_si128(plaintext, ctx->iv);
  1389. __m128i ciphertext = encrypt(cipher_input, ctx->keysched_e);
  1390. _mm_storeu_si128((__m128i *)blk, ciphertext);
  1391. ctx->iv = ciphertext;
  1392. }
  1393. }
  1394. static FUNC_ISA inline void aes_cbc_ni_decrypt(
  1395. ssh_cipher *ciph, void *vblk, int blklen, aes_ni_fn decrypt)
  1396. {
  1397. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1398. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1399. blk < finish; blk += 16) {
  1400. __m128i ciphertext = _mm_loadu_si128((const __m128i *)blk);
  1401. __m128i decrypted = decrypt(ciphertext, ctx->keysched_d);
  1402. __m128i plaintext = _mm_xor_si128(decrypted, ctx->iv);
  1403. _mm_storeu_si128((__m128i *)blk, plaintext);
  1404. ctx->iv = ciphertext;
  1405. }
  1406. }
  1407. static FUNC_ISA inline void aes_sdctr_ni(
  1408. ssh_cipher *ciph, void *vblk, int blklen, aes_ni_fn encrypt)
  1409. {
  1410. aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
  1411. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1412. blk < finish; blk += 16) {
  1413. __m128i counter = aes_ni_sdctr_reverse(ctx->iv);
  1414. __m128i keystream = encrypt(counter, ctx->keysched_e);
  1415. __m128i input = _mm_loadu_si128((const __m128i *)blk);
  1416. __m128i output = _mm_xor_si128(input, keystream);
  1417. _mm_storeu_si128((__m128i *)blk, output);
  1418. ctx->iv = aes_ni_sdctr_increment(ctx->iv);
  1419. }
  1420. }
  1421. #define NI_ENC_DEC(len) \
  1422. /*static WINSCP*/ FUNC_ISA void aes##len##_cbc_hw_encrypt( \
  1423. ssh_cipher *ciph, void *vblk, int blklen) \
  1424. { aes_cbc_ni_encrypt(ciph, vblk, blklen, aes_ni_##len##_e); } \
  1425. /*static WINSCP*/ FUNC_ISA void aes##len##_cbc_hw_decrypt( \
  1426. ssh_cipher *ciph, void *vblk, int blklen) \
  1427. { aes_cbc_ni_decrypt(ciph, vblk, blklen, aes_ni_##len##_d); } \
  1428. /*static WINSCP*/ FUNC_ISA void aes##len##_sdctr_hw( \
  1429. ssh_cipher *ciph, void *vblk, int blklen) \
  1430. { aes_sdctr_ni(ciph, vblk, blklen, aes_ni_##len##_e); } \
  1431. NI_ENC_DEC(128)
  1432. NI_ENC_DEC(192)
  1433. NI_ENC_DEC(256)
  1434. #endif // WINSCP_VS
  1435. /* ----------------------------------------------------------------------
  1436. * Hardware-accelerated implementation of AES using Arm NEON.
  1437. */
  1438. #elif HW_AES == HW_AES_NEON
  1439. /*
  1440. * Manually set the target architecture, if we decided above that we
  1441. * need to.
  1442. */
  1443. #ifdef USE_CLANG_ATTR_TARGET_AARCH64
  1444. /*
  1445. * A spot of cheating: redefine some ACLE feature macros before
  1446. * including arm_neon.h. Otherwise we won't get the AES intrinsics
  1447. * defined by that header, because it will be looking at the settings
  1448. * for the whole translation unit rather than the ones we're going to
  1449. * put on some particular functions using __attribute__((target)).
  1450. */
  1451. #define __ARM_NEON 1
  1452. #define __ARM_FEATURE_CRYPTO 1
  1453. #define __ARM_FEATURE_AES 1
  1454. #define FUNC_ISA __attribute__ ((target("neon,crypto")))
  1455. #endif /* USE_CLANG_ATTR_TARGET_AARCH64 */
  1456. #ifndef FUNC_ISA
  1457. #define FUNC_ISA
  1458. #endif
  1459. #ifdef USE_ARM64_NEON_H
  1460. #include <arm64_neon.h>
  1461. #else
  1462. #include <arm_neon.h>
  1463. #endif
  1464. static bool aes_hw_available(void)
  1465. {
  1466. /*
  1467. * For Arm, we delegate to a per-platform AES detection function,
  1468. * because it has to be implemented by asking the operating system
  1469. * rather than directly querying the CPU.
  1470. *
  1471. * That's because Arm systems commonly have multiple cores that
  1472. * are not all alike, so any method of querying whether NEON
  1473. * crypto instructions work on the _current_ CPU - even one as
  1474. * crude as just trying one and catching the SIGILL - wouldn't
  1475. * give an answer that you could still rely on the first time the
  1476. * OS migrated your process to another CPU.
  1477. */
  1478. return platform_aes_hw_available();
  1479. }
  1480. /*
  1481. * Core NEON encrypt/decrypt functions, one per length and direction.
  1482. */
  1483. #define NEON_CIPHER(len, repmacro) \
  1484. static FUNC_ISA inline uint8x16_t aes_neon_##len##_e( \
  1485. uint8x16_t v, const uint8x16_t *keysched) \
  1486. { \
  1487. repmacro(v = vaesmcq_u8(vaeseq_u8(v, *keysched++));); \
  1488. v = vaeseq_u8(v, *keysched++); \
  1489. return veorq_u8(v, *keysched); \
  1490. } \
  1491. static FUNC_ISA inline uint8x16_t aes_neon_##len##_d( \
  1492. uint8x16_t v, const uint8x16_t *keysched) \
  1493. { \
  1494. repmacro(v = vaesimcq_u8(vaesdq_u8(v, *keysched++));); \
  1495. v = vaesdq_u8(v, *keysched++); \
  1496. return veorq_u8(v, *keysched); \
  1497. }
  1498. NEON_CIPHER(128, REP9)
  1499. NEON_CIPHER(192, REP11)
  1500. NEON_CIPHER(256, REP13)
  1501. /*
  1502. * The main key expansion.
  1503. */
  1504. static FUNC_ISA void aes_neon_key_expand(
  1505. const unsigned char *key, size_t key_words,
  1506. uint8x16_t *keysched_e, uint8x16_t *keysched_d)
  1507. {
  1508. size_t rounds = key_words + 6;
  1509. size_t sched_words = (rounds + 1) * 4;
  1510. /*
  1511. * Store the key schedule as 32-bit integers during expansion, so
  1512. * that it's easy to refer back to individual previous words. We
  1513. * collect them into the final uint8x16_t form at the end.
  1514. */
  1515. uint32_t sched[MAXROUNDKEYS * 4];
  1516. unsigned rconpos = 0;
  1517. for (size_t i = 0; i < sched_words; i++) {
  1518. if (i < key_words) {
  1519. sched[i] = GET_32BIT_LSB_FIRST(key + 4 * i);
  1520. } else {
  1521. uint32_t temp = sched[i - 1];
  1522. bool rotate_and_round_constant = (i % key_words == 0);
  1523. bool sub = rotate_and_round_constant ||
  1524. (key_words == 8 && i % 8 == 4);
  1525. if (rotate_and_round_constant)
  1526. temp = (temp << 24) | (temp >> 8);
  1527. if (sub) {
  1528. uint32x4_t v32 = vdupq_n_u32(temp);
  1529. uint8x16_t v8 = vreinterpretq_u8_u32(v32);
  1530. v8 = vaeseq_u8(v8, vdupq_n_u8(0));
  1531. v32 = vreinterpretq_u32_u8(v8);
  1532. temp = vget_lane_u32(vget_low_u32(v32), 0);
  1533. }
  1534. if (rotate_and_round_constant) {
  1535. assert(rconpos < lenof(key_setup_round_constants));
  1536. temp ^= key_setup_round_constants[rconpos++];
  1537. }
  1538. sched[i] = sched[i - key_words] ^ temp;
  1539. }
  1540. }
  1541. /*
  1542. * Combine the key schedule words into uint8x16_t vectors and
  1543. * store them in the output context.
  1544. */
  1545. for (size_t round = 0; round <= rounds; round++)
  1546. keysched_e[round] = vreinterpretq_u8_u32(vld1q_u32(sched + 4*round));
  1547. smemclr(sched, sizeof(sched));
  1548. /*
  1549. * Now prepare the modified keys for the inverse cipher.
  1550. */
  1551. for (size_t eround = 0; eround <= rounds; eround++) {
  1552. size_t dround = rounds - eround;
  1553. uint8x16_t rkey = keysched_e[eround];
  1554. if (eround && dround) /* neither first nor last */
  1555. rkey = vaesimcq_u8(rkey);
  1556. keysched_d[dround] = rkey;
  1557. }
  1558. }
  1559. /*
  1560. * Auxiliary routine to reverse the byte order of a vector, so that
  1561. * the SDCTR IV can be made big-endian for feeding to the cipher.
  1562. *
  1563. * In fact we don't need to reverse the vector _all_ the way; we leave
  1564. * the two lanes in MSW,LSW order, because that makes no difference to
  1565. * the efficiency of the increment. That way we only have to reverse
  1566. * bytes within each lane in this function.
  1567. */
  1568. static FUNC_ISA inline uint8x16_t aes_neon_sdctr_reverse(uint8x16_t v)
  1569. {
  1570. return vrev64q_u8(v);
  1571. }
  1572. /*
  1573. * Auxiliary routine to increment the 128-bit counter used in SDCTR
  1574. * mode. There's no instruction to treat a 128-bit vector as a single
  1575. * long integer, so instead we have to increment the bottom half
  1576. * unconditionally, and the top half if the bottom half started off as
  1577. * all 1s (in which case there was about to be a carry).
  1578. */
  1579. static FUNC_ISA inline uint8x16_t aes_neon_sdctr_increment(uint8x16_t in)
  1580. {
  1581. #ifdef __aarch64__
  1582. /* There will be a carry if the low 64 bits are all 1s. */
  1583. uint64x1_t all1 = vcreate_u64(0xFFFFFFFFFFFFFFFF);
  1584. uint64x1_t carry = vceq_u64(vget_high_u64(vreinterpretq_u64_u8(in)), all1);
  1585. /* Make a word whose bottom half is unconditionally all 1s, and
  1586. * the top half is 'carry', i.e. all 0s most of the time but all
  1587. * 1s if we need to increment the top half. Then that word is what
  1588. * we need to _subtract_ from the input counter. */
  1589. uint64x2_t subtrahend = vcombine_u64(carry, all1);
  1590. #else
  1591. /* AArch32 doesn't have comparisons that operate on a 64-bit lane,
  1592. * so we start by comparing each 32-bit half of the low 64 bits
  1593. * _separately_ to all-1s. */
  1594. uint32x2_t all1 = vdup_n_u32(0xFFFFFFFF);
  1595. uint32x2_t carry = vceq_u32(
  1596. vget_high_u32(vreinterpretq_u32_u8(in)), all1);
  1597. /* Swap the 32-bit words of the compare output, and AND with the
  1598. * unswapped version. Now carry is all 1s iff the bottom half of
  1599. * the input counter was all 1s, and all 0s otherwise. */
  1600. carry = vand_u32(carry, vrev64_u32(carry));
  1601. /* Now make the vector to subtract in the same way as above. */
  1602. uint64x2_t subtrahend = vreinterpretq_u64_u32(vcombine_u32(carry, all1));
  1603. #endif
  1604. return vreinterpretq_u8_u64(
  1605. vsubq_u64(vreinterpretq_u64_u8(in), subtrahend));
  1606. }
  1607. /*
  1608. * The SSH interface and the cipher modes.
  1609. */
  1610. typedef struct aes_neon_context aes_neon_context;
  1611. struct aes_neon_context {
  1612. uint8x16_t keysched_e[MAXROUNDKEYS], keysched_d[MAXROUNDKEYS], iv;
  1613. ssh_cipher ciph;
  1614. };
  1615. static ssh_cipher *aes_hw_new(const ssh_cipheralg *alg)
  1616. {
  1617. if (!aes_hw_available_cached())
  1618. return NULL;
  1619. aes_neon_context *ctx = snew(aes_neon_context);
  1620. ctx->ciph.vt = alg;
  1621. return &ctx->ciph;
  1622. }
  1623. static void aes_hw_free(ssh_cipher *ciph)
  1624. {
  1625. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1626. smemclr(ctx, sizeof(*ctx));
  1627. sfree(ctx);
  1628. }
  1629. static void aes_hw_setkey(ssh_cipher *ciph, const void *vkey)
  1630. {
  1631. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1632. const unsigned char *key = (const unsigned char *)vkey;
  1633. aes_neon_key_expand(key, ctx->ciph.vt->real_keybits / 32,
  1634. ctx->keysched_e, ctx->keysched_d);
  1635. }
  1636. static FUNC_ISA void aes_hw_setiv_cbc(ssh_cipher *ciph, const void *iv)
  1637. {
  1638. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1639. ctx->iv = vld1q_u8(iv);
  1640. }
  1641. static FUNC_ISA void aes_hw_setiv_sdctr(ssh_cipher *ciph, const void *iv)
  1642. {
  1643. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1644. uint8x16_t counter = vld1q_u8(iv);
  1645. ctx->iv = aes_neon_sdctr_reverse(counter);
  1646. }
  1647. typedef uint8x16_t (*aes_neon_fn)(uint8x16_t v, const uint8x16_t *keysched);
  1648. static FUNC_ISA inline void aes_cbc_neon_encrypt(
  1649. ssh_cipher *ciph, void *vblk, int blklen, aes_neon_fn encrypt)
  1650. {
  1651. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1652. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1653. blk < finish; blk += 16) {
  1654. uint8x16_t plaintext = vld1q_u8(blk);
  1655. uint8x16_t cipher_input = veorq_u8(plaintext, ctx->iv);
  1656. uint8x16_t ciphertext = encrypt(cipher_input, ctx->keysched_e);
  1657. vst1q_u8(blk, ciphertext);
  1658. ctx->iv = ciphertext;
  1659. }
  1660. }
  1661. static FUNC_ISA inline void aes_cbc_neon_decrypt(
  1662. ssh_cipher *ciph, void *vblk, int blklen, aes_neon_fn decrypt)
  1663. {
  1664. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1665. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1666. blk < finish; blk += 16) {
  1667. uint8x16_t ciphertext = vld1q_u8(blk);
  1668. uint8x16_t decrypted = decrypt(ciphertext, ctx->keysched_d);
  1669. uint8x16_t plaintext = veorq_u8(decrypted, ctx->iv);
  1670. vst1q_u8(blk, plaintext);
  1671. ctx->iv = ciphertext;
  1672. }
  1673. }
  1674. static FUNC_ISA inline void aes_sdctr_neon(
  1675. ssh_cipher *ciph, void *vblk, int blklen, aes_neon_fn encrypt)
  1676. {
  1677. aes_neon_context *ctx = container_of(ciph, aes_neon_context, ciph);
  1678. for (uint8_t *blk = (uint8_t *)vblk, *finish = blk + blklen;
  1679. blk < finish; blk += 16) {
  1680. uint8x16_t counter = aes_neon_sdctr_reverse(ctx->iv);
  1681. uint8x16_t keystream = encrypt(counter, ctx->keysched_e);
  1682. uint8x16_t input = vld1q_u8(blk);
  1683. uint8x16_t output = veorq_u8(input, keystream);
  1684. vst1q_u8(blk, output);
  1685. ctx->iv = aes_neon_sdctr_increment(ctx->iv);
  1686. }
  1687. }
  1688. #define NEON_ENC_DEC(len) \
  1689. static FUNC_ISA void aes##len##_cbc_hw_encrypt( \
  1690. ssh_cipher *ciph, void *vblk, int blklen) \
  1691. { aes_cbc_neon_encrypt(ciph, vblk, blklen, aes_neon_##len##_e); } \
  1692. static FUNC_ISA void aes##len##_cbc_hw_decrypt( \
  1693. ssh_cipher *ciph, void *vblk, int blklen) \
  1694. { aes_cbc_neon_decrypt(ciph, vblk, blklen, aes_neon_##len##_d); } \
  1695. static FUNC_ISA void aes##len##_sdctr_hw( \
  1696. ssh_cipher *ciph, void *vblk, int blklen) \
  1697. { aes_sdctr_neon(ciph, vblk, blklen, aes_neon_##len##_e); } \
  1698. NEON_ENC_DEC(128)
  1699. NEON_ENC_DEC(192)
  1700. NEON_ENC_DEC(256)
  1701. /* ----------------------------------------------------------------------
  1702. * Stub functions if we have no hardware-accelerated AES. In this
  1703. * case, aes_hw_new returns NULL (though it should also never be
  1704. * selected by aes_select, so the only thing that should even be
  1705. * _able_ to call it is testcrypt). As a result, the remaining vtable
  1706. * functions should never be called at all.
  1707. */
  1708. #elif HW_AES == HW_AES_NONE
  1709. bool aes_hw_available(void)
  1710. {
  1711. return false;
  1712. }
  1713. static ssh_cipher *aes_hw_new(const ssh_cipheralg *alg)
  1714. {
  1715. return NULL;
  1716. }
  1717. #define STUB_BODY { unreachable("Should never be called"); }
  1718. static void aes_hw_free(ssh_cipher *ciph) STUB_BODY
  1719. static void aes_hw_setkey(ssh_cipher *ciph, const void *key) STUB_BODY
  1720. static void aes_hw_setiv_cbc(ssh_cipher *ciph, const void *iv) STUB_BODY
  1721. static void aes_hw_setiv_sdctr(ssh_cipher *ciph, const void *iv) STUB_BODY
  1722. #define STUB_ENC_DEC(len) \
  1723. static void aes##len##_cbc_hw_encrypt( \
  1724. ssh_cipher *ciph, void *vblk, int blklen) STUB_BODY \
  1725. static void aes##len##_cbc_hw_decrypt( \
  1726. ssh_cipher *ciph, void *vblk, int blklen) STUB_BODY \
  1727. static void aes##len##_sdctr_hw( \
  1728. ssh_cipher *ciph, void *vblk, int blklen) STUB_BODY
  1729. STUB_ENC_DEC(128)
  1730. STUB_ENC_DEC(192)
  1731. STUB_ENC_DEC(256)
  1732. #endif /* HW_AES */
  1733. #ifndef WINSCP_VS
  1734. #ifdef MPEXT
  1735. #include "puttyexp.h"
  1736. AESContext * aes_make_context()
  1737. {
  1738. ssh_cipher * cipher = ssh_cipher_new(&ssh_aes256_sdctr);
  1739. return cipher;
  1740. }
  1741. void aes_free_context(AESContext * ctx)
  1742. {
  1743. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1744. ssh_cipher_free(cipher);
  1745. }
  1746. void aes_iv(AESContext * ctx, const void * iv)
  1747. {
  1748. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1749. ssh_cipher_setiv(cipher, iv);
  1750. }
  1751. void call_aes_setup(AESContext * ctx, unsigned char * key, int keylen)
  1752. {
  1753. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1754. assert(keylen == 32);
  1755. ssh_cipher_setkey(cipher, key);
  1756. }
  1757. void call_aes_sdctr(unsigned char *blk, int len, void *ctx)
  1758. {
  1759. ssh_cipher * cipher = (ssh_cipher *)ctx;
  1760. ssh_cipher_encrypt(cipher, blk, len);
  1761. }
  1762. #endif
  1763. #endif // WINSCP_VS