vpaes-x86.pl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. #!/usr/bin/env perl
  2. ######################################################################
  3. ## Constant-time SSSE3 AES core implementation.
  4. ## version 0.1
  5. ##
  6. ## By Mike Hamburg (Stanford University), 2009
  7. ## Public domain.
  8. ##
  9. ## For details see http://shiftleft.org/papers/vector_aes/ and
  10. ## http://crypto.stanford.edu/vpaes/.
  11. ######################################################################
  12. # September 2011.
  13. #
  14. # Port vpaes-x86_64.pl as 32-bit "almost" drop-in replacement for
  15. # aes-586.pl. "Almost" refers to the fact that AES_cbc_encrypt
  16. # doesn't handle partial vectors (doesn't have to if called from
  17. # EVP only). "Drop-in" implies that this module doesn't share key
  18. # schedule structure with the original nor does it make assumption
  19. # about its alignment...
  20. #
  21. # Performance summary. aes-586.pl column lists large-block CBC
  22. # encrypt/decrypt/with-hyper-threading-off(*) results in cycles per
  23. # byte processed with 128-bit key, and vpaes-x86.pl column -
  24. # encrypt/decrypt.
  25. #
  26. # aes-586.pl vpaes-x86.pl
  27. #
  28. # Core 2(**) 29.1/42.3/18.3 22.0/25.6(***)
  29. # Nehalem 27.9/40.4/18.1 10.3/12.0
  30. # Atom 102./119./60.1 64.5/85.3(***)
  31. #
  32. # (*) "Hyper-threading" in the context refers rather to cache shared
  33. # among multiple cores, than to specifically Intel HTT. As vast
  34. # majority of contemporary cores share cache, slower code path
  35. # is common place. In other words "with-hyper-threading-off"
  36. # results are presented mostly for reference purposes.
  37. #
  38. # (**) "Core 2" refers to initial 65nm design, a.k.a. Conroe.
  39. #
  40. # (***) Less impressive improvement on Core 2 and Atom is due to slow
  41. # pshufb, yet it's respectable +32%/65% improvement on Core 2
  42. # and +58%/40% on Atom (as implied, over "hyper-threading-safe"
  43. # code path).
  44. #
  45. # <appro@openssl.org>
  46. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  47. push(@INC,"${dir}","${dir}../../perlasm");
  48. require "x86asm.pl";
  49. &asm_init($ARGV[0],"vpaes-x86.pl",$x86only = $ARGV[$#ARGV] eq "386");
  50. $PREFIX="vpaes";
  51. my ($round, $base, $magic, $key, $const, $inp, $out)=
  52. ("eax", "ebx", "ecx", "edx","ebp", "esi","edi");
  53. &static_label("_vpaes_consts");
  54. &static_label("_vpaes_schedule_low_round");
  55. &set_label("_vpaes_consts",64);
  56. $k_inv=-0x30; # inv, inva
  57. &data_word(0x0D080180,0x0E05060F,0x0A0B0C02,0x04070309);
  58. &data_word(0x0F0B0780,0x01040A06,0x02050809,0x030D0E0C);
  59. $k_s0F=-0x10; # s0F
  60. &data_word(0x0F0F0F0F,0x0F0F0F0F,0x0F0F0F0F,0x0F0F0F0F);
  61. $k_ipt=0x00; # input transform (lo, hi)
  62. &data_word(0x5A2A7000,0xC2B2E898,0x52227808,0xCABAE090);
  63. &data_word(0x317C4D00,0x4C01307D,0xB0FDCC81,0xCD80B1FC);
  64. $k_sb1=0x20; # sb1u, sb1t
  65. &data_word(0xCB503E00,0xB19BE18F,0x142AF544,0xA5DF7A6E);
  66. &data_word(0xFAE22300,0x3618D415,0x0D2ED9EF,0x3BF7CCC1);
  67. $k_sb2=0x40; # sb2u, sb2t
  68. &data_word(0x0B712400,0xE27A93C6,0xBC982FCD,0x5EB7E955);
  69. &data_word(0x0AE12900,0x69EB8840,0xAB82234A,0xC2A163C8);
  70. $k_sbo=0x60; # sbou, sbot
  71. &data_word(0x6FBDC700,0xD0D26D17,0xC502A878,0x15AABF7A);
  72. &data_word(0x5FBB6A00,0xCFE474A5,0x412B35FA,0x8E1E90D1);
  73. $k_mc_forward=0x80; # mc_forward
  74. &data_word(0x00030201,0x04070605,0x080B0A09,0x0C0F0E0D);
  75. &data_word(0x04070605,0x080B0A09,0x0C0F0E0D,0x00030201);
  76. &data_word(0x080B0A09,0x0C0F0E0D,0x00030201,0x04070605);
  77. &data_word(0x0C0F0E0D,0x00030201,0x04070605,0x080B0A09);
  78. $k_mc_backward=0xc0; # mc_backward
  79. &data_word(0x02010003,0x06050407,0x0A09080B,0x0E0D0C0F);
  80. &data_word(0x0E0D0C0F,0x02010003,0x06050407,0x0A09080B);
  81. &data_word(0x0A09080B,0x0E0D0C0F,0x02010003,0x06050407);
  82. &data_word(0x06050407,0x0A09080B,0x0E0D0C0F,0x02010003);
  83. $k_sr=0x100; # sr
  84. &data_word(0x03020100,0x07060504,0x0B0A0908,0x0F0E0D0C);
  85. &data_word(0x0F0A0500,0x030E0904,0x07020D08,0x0B06010C);
  86. &data_word(0x0B020900,0x0F060D04,0x030A0108,0x070E050C);
  87. &data_word(0x070A0D00,0x0B0E0104,0x0F020508,0x0306090C);
  88. $k_rcon=0x140; # rcon
  89. &data_word(0xAF9DEEB6,0x1F8391B9,0x4D7C7D81,0x702A9808);
  90. $k_s63=0x150; # s63: all equal to 0x63 transformed
  91. &data_word(0x5B5B5B5B,0x5B5B5B5B,0x5B5B5B5B,0x5B5B5B5B);
  92. $k_opt=0x160; # output transform
  93. &data_word(0xD6B66000,0xFF9F4929,0xDEBE6808,0xF7974121);
  94. &data_word(0x50BCEC00,0x01EDBD51,0xB05C0CE0,0xE10D5DB1);
  95. $k_deskew=0x180; # deskew tables: inverts the sbox's "skew"
  96. &data_word(0x47A4E300,0x07E4A340,0x5DBEF91A,0x1DFEB95A);
  97. &data_word(0x83EA6900,0x5F36B5DC,0xF49D1E77,0x2841C2AB);
  98. ##
  99. ## Decryption stuff
  100. ## Key schedule constants
  101. ##
  102. $k_dksd=0x1a0; # decryption key schedule: invskew x*D
  103. &data_word(0xA3E44700,0xFEB91A5D,0x5A1DBEF9,0x0740E3A4);
  104. &data_word(0xB5368300,0x41C277F4,0xAB289D1E,0x5FDC69EA);
  105. $k_dksb=0x1c0; # decryption key schedule: invskew x*B
  106. &data_word(0x8550D500,0x9A4FCA1F,0x1CC94C99,0x03D65386);
  107. &data_word(0xB6FC4A00,0x115BEDA7,0x7E3482C8,0xD993256F);
  108. $k_dkse=0x1e0; # decryption key schedule: invskew x*E + 0x63
  109. &data_word(0x1FC9D600,0xD5031CCA,0x994F5086,0x53859A4C);
  110. &data_word(0x4FDC7BE8,0xA2319605,0x20B31487,0xCD5EF96A);
  111. $k_dks9=0x200; # decryption key schedule: invskew x*9
  112. &data_word(0x7ED9A700,0xB6116FC8,0x82255BFC,0x4AED9334);
  113. &data_word(0x27143300,0x45765162,0xE9DAFDCE,0x8BB89FAC);
  114. ##
  115. ## Decryption stuff
  116. ## Round function constants
  117. ##
  118. $k_dipt=0x220; # decryption input transform
  119. &data_word(0x0B545F00,0x0F505B04,0x114E451A,0x154A411E);
  120. &data_word(0x60056500,0x86E383E6,0xF491F194,0x12771772);
  121. $k_dsb9=0x240; # decryption sbox output *9*u, *9*t
  122. &data_word(0x9A86D600,0x851C0353,0x4F994CC9,0xCAD51F50);
  123. &data_word(0xECD74900,0xC03B1789,0xB2FBA565,0x725E2C9E);
  124. $k_dsbd=0x260; # decryption sbox output *D*u, *D*t
  125. &data_word(0xE6B1A200,0x7D57CCDF,0x882A4439,0xF56E9B13);
  126. &data_word(0x24C6CB00,0x3CE2FAF7,0x15DEEFD3,0x2931180D);
  127. $k_dsbb=0x280; # decryption sbox output *B*u, *B*t
  128. &data_word(0x96B44200,0xD0226492,0xB0F2D404,0x602646F6);
  129. &data_word(0xCD596700,0xC19498A6,0x3255AA6B,0xF3FF0C3E);
  130. $k_dsbe=0x2a0; # decryption sbox output *E*u, *E*t
  131. &data_word(0x26D4D000,0x46F29296,0x64B4F6B0,0x22426004);
  132. &data_word(0xFFAAC100,0x0C55A6CD,0x98593E32,0x9467F36B);
  133. $k_dsbo=0x2c0; # decryption sbox final output
  134. &data_word(0x7EF94000,0x1387EA53,0xD4943E2D,0xC7AA6DB9);
  135. &data_word(0x93441D00,0x12D7560F,0xD8C58E9C,0xCA4B8159);
  136. &asciz ("Vector Permutation AES for x86, Mike Hamburg (Stanford University)");
  137. &align (64);
  138. &function_begin_B("_vpaes_preheat");
  139. &add ($const,&DWP(0,"esp"));
  140. &movdqa ("xmm7",&QWP($k_inv,$const));
  141. &movdqa ("xmm6",&QWP($k_s0F,$const));
  142. &ret ();
  143. &function_end_B("_vpaes_preheat");
  144. ##
  145. ## _aes_encrypt_core
  146. ##
  147. ## AES-encrypt %xmm0.
  148. ##
  149. ## Inputs:
  150. ## %xmm0 = input
  151. ## %xmm6-%xmm7 as in _vpaes_preheat
  152. ## (%edx) = scheduled keys
  153. ##
  154. ## Output in %xmm0
  155. ## Clobbers %xmm1-%xmm5, %eax, %ebx, %ecx, %edx
  156. ##
  157. ##
  158. &function_begin_B("_vpaes_encrypt_core");
  159. &mov ($magic,16);
  160. &mov ($round,&DWP(240,$key));
  161. &movdqa ("xmm1","xmm6")
  162. &movdqa ("xmm2",&QWP($k_ipt,$const));
  163. &pandn ("xmm1","xmm0");
  164. &movdqu ("xmm5",&QWP(0,$key));
  165. &psrld ("xmm1",4);
  166. &pand ("xmm0","xmm6");
  167. &pshufb ("xmm2","xmm0");
  168. &movdqa ("xmm0",&QWP($k_ipt+16,$const));
  169. &pshufb ("xmm0","xmm1");
  170. &pxor ("xmm2","xmm5");
  171. &pxor ("xmm0","xmm2");
  172. &add ($key,16);
  173. &lea ($base,&DWP($k_mc_backward,$const));
  174. &jmp (&label("enc_entry"));
  175. &set_label("enc_loop",16);
  176. # middle of middle round
  177. &movdqa ("xmm4",&QWP($k_sb1,$const)); # 4 : sb1u
  178. &pshufb ("xmm4","xmm2"); # 4 = sb1u
  179. &pxor ("xmm4","xmm5"); # 4 = sb1u + k
  180. &movdqa ("xmm0",&QWP($k_sb1+16,$const));# 0 : sb1t
  181. &pshufb ("xmm0","xmm3"); # 0 = sb1t
  182. &pxor ("xmm0","xmm4"); # 0 = A
  183. &movdqa ("xmm5",&QWP($k_sb2,$const)); # 4 : sb2u
  184. &pshufb ("xmm5","xmm2"); # 4 = sb2u
  185. &movdqa ("xmm1",&QWP(-0x40,$base,$magic));# .Lk_mc_forward[]
  186. &movdqa ("xmm2",&QWP($k_sb2+16,$const));# 2 : sb2t
  187. &pshufb ("xmm2","xmm3"); # 2 = sb2t
  188. &pxor ("xmm2","xmm5"); # 2 = 2A
  189. &movdqa ("xmm4",&QWP(0,$base,$magic)); # .Lk_mc_backward[]
  190. &movdqa ("xmm3","xmm0"); # 3 = A
  191. &pshufb ("xmm0","xmm1"); # 0 = B
  192. &add ($key,16); # next key
  193. &pxor ("xmm0","xmm2"); # 0 = 2A+B
  194. &pshufb ("xmm3","xmm4"); # 3 = D
  195. &add ($magic,16); # next mc
  196. &pxor ("xmm3","xmm0"); # 3 = 2A+B+D
  197. &pshufb ("xmm0","xmm1"); # 0 = 2B+C
  198. &and ($magic,0x30); # ... mod 4
  199. &pxor ("xmm0","xmm3"); # 0 = 2A+3B+C+D
  200. &sub ($round,1); # nr--
  201. &set_label("enc_entry");
  202. # top of round
  203. &movdqa ("xmm1","xmm6"); # 1 : i
  204. &pandn ("xmm1","xmm0"); # 1 = i<<4
  205. &psrld ("xmm1",4); # 1 = i
  206. &pand ("xmm0","xmm6"); # 0 = k
  207. &movdqa ("xmm5",&QWP($k_inv+16,$const));# 2 : a/k
  208. &pshufb ("xmm5","xmm0"); # 2 = a/k
  209. &pxor ("xmm0","xmm1"); # 0 = j
  210. &movdqa ("xmm3","xmm7"); # 3 : 1/i
  211. &pshufb ("xmm3","xmm1"); # 3 = 1/i
  212. &pxor ("xmm3","xmm5"); # 3 = iak = 1/i + a/k
  213. &movdqa ("xmm4","xmm7"); # 4 : 1/j
  214. &pshufb ("xmm4","xmm0"); # 4 = 1/j
  215. &pxor ("xmm4","xmm5"); # 4 = jak = 1/j + a/k
  216. &movdqa ("xmm2","xmm7"); # 2 : 1/iak
  217. &pshufb ("xmm2","xmm3"); # 2 = 1/iak
  218. &pxor ("xmm2","xmm0"); # 2 = io
  219. &movdqa ("xmm3","xmm7"); # 3 : 1/jak
  220. &movdqu ("xmm5",&QWP(0,$key));
  221. &pshufb ("xmm3","xmm4"); # 3 = 1/jak
  222. &pxor ("xmm3","xmm1"); # 3 = jo
  223. &jnz (&label("enc_loop"));
  224. # middle of last round
  225. &movdqa ("xmm4",&QWP($k_sbo,$const)); # 3 : sbou .Lk_sbo
  226. &movdqa ("xmm0",&QWP($k_sbo+16,$const));# 3 : sbot .Lk_sbo+16
  227. &pshufb ("xmm4","xmm2"); # 4 = sbou
  228. &pxor ("xmm4","xmm5"); # 4 = sb1u + k
  229. &pshufb ("xmm0","xmm3"); # 0 = sb1t
  230. &movdqa ("xmm1",&QWP(0x40,$base,$magic));# .Lk_sr[]
  231. &pxor ("xmm0","xmm4"); # 0 = A
  232. &pshufb ("xmm0","xmm1");
  233. &ret ();
  234. &function_end_B("_vpaes_encrypt_core");
  235. ##
  236. ## Decryption core
  237. ##
  238. ## Same API as encryption core.
  239. ##
  240. &function_begin_B("_vpaes_decrypt_core");
  241. &mov ($round,&DWP(240,$key));
  242. &lea ($base,&DWP($k_dsbd,$const));
  243. &movdqa ("xmm1","xmm6");
  244. &movdqa ("xmm2",&QWP($k_dipt-$k_dsbd,$base));
  245. &pandn ("xmm1","xmm0");
  246. &mov ($magic,$round);
  247. &psrld ("xmm1",4)
  248. &movdqu ("xmm5",&QWP(0,$key));
  249. &shl ($magic,4);
  250. &pand ("xmm0","xmm6");
  251. &pshufb ("xmm2","xmm0");
  252. &movdqa ("xmm0",&QWP($k_dipt-$k_dsbd+16,$base));
  253. &xor ($magic,0x30);
  254. &pshufb ("xmm0","xmm1");
  255. &and ($magic,0x30);
  256. &pxor ("xmm2","xmm5");
  257. &movdqa ("xmm5",&QWP($k_mc_forward+48,$const));
  258. &pxor ("xmm0","xmm2");
  259. &add ($key,16);
  260. &lea ($magic,&DWP($k_sr-$k_dsbd,$base,$magic));
  261. &jmp (&label("dec_entry"));
  262. &set_label("dec_loop",16);
  263. ##
  264. ## Inverse mix columns
  265. ##
  266. &movdqa ("xmm4",&QWP(-0x20,$base)); # 4 : sb9u
  267. &pshufb ("xmm4","xmm2"); # 4 = sb9u
  268. &pxor ("xmm4","xmm0");
  269. &movdqa ("xmm0",&QWP(-0x10,$base)); # 0 : sb9t
  270. &pshufb ("xmm0","xmm3"); # 0 = sb9t
  271. &pxor ("xmm0","xmm4"); # 0 = ch
  272. &add ($key,16); # next round key
  273. &pshufb ("xmm0","xmm5"); # MC ch
  274. &movdqa ("xmm4",&QWP(0,$base)); # 4 : sbdu
  275. &pshufb ("xmm4","xmm2"); # 4 = sbdu
  276. &pxor ("xmm4","xmm0"); # 4 = ch
  277. &movdqa ("xmm0",&QWP(0x10,$base)); # 0 : sbdt
  278. &pshufb ("xmm0","xmm3"); # 0 = sbdt
  279. &pxor ("xmm0","xmm4"); # 0 = ch
  280. &sub ($round,1); # nr--
  281. &pshufb ("xmm0","xmm5"); # MC ch
  282. &movdqa ("xmm4",&QWP(0x20,$base)); # 4 : sbbu
  283. &pshufb ("xmm4","xmm2"); # 4 = sbbu
  284. &pxor ("xmm4","xmm0"); # 4 = ch
  285. &movdqa ("xmm0",&QWP(0x30,$base)); # 0 : sbbt
  286. &pshufb ("xmm0","xmm3"); # 0 = sbbt
  287. &pxor ("xmm0","xmm4"); # 0 = ch
  288. &pshufb ("xmm0","xmm5"); # MC ch
  289. &movdqa ("xmm4",&QWP(0x40,$base)); # 4 : sbeu
  290. &pshufb ("xmm4","xmm2"); # 4 = sbeu
  291. &pxor ("xmm4","xmm0"); # 4 = ch
  292. &movdqa ("xmm0",&QWP(0x50,$base)); # 0 : sbet
  293. &pshufb ("xmm0","xmm3"); # 0 = sbet
  294. &pxor ("xmm0","xmm4"); # 0 = ch
  295. &palignr("xmm5","xmm5",12);
  296. &set_label("dec_entry");
  297. # top of round
  298. &movdqa ("xmm1","xmm6"); # 1 : i
  299. &pandn ("xmm1","xmm0"); # 1 = i<<4
  300. &psrld ("xmm1",4); # 1 = i
  301. &pand ("xmm0","xmm6"); # 0 = k
  302. &movdqa ("xmm2",&QWP($k_inv+16,$const));# 2 : a/k
  303. &pshufb ("xmm2","xmm0"); # 2 = a/k
  304. &pxor ("xmm0","xmm1"); # 0 = j
  305. &movdqa ("xmm3","xmm7"); # 3 : 1/i
  306. &pshufb ("xmm3","xmm1"); # 3 = 1/i
  307. &pxor ("xmm3","xmm2"); # 3 = iak = 1/i + a/k
  308. &movdqa ("xmm4","xmm7"); # 4 : 1/j
  309. &pshufb ("xmm4","xmm0"); # 4 = 1/j
  310. &pxor ("xmm4","xmm2"); # 4 = jak = 1/j + a/k
  311. &movdqa ("xmm2","xmm7"); # 2 : 1/iak
  312. &pshufb ("xmm2","xmm3"); # 2 = 1/iak
  313. &pxor ("xmm2","xmm0"); # 2 = io
  314. &movdqa ("xmm3","xmm7"); # 3 : 1/jak
  315. &pshufb ("xmm3","xmm4"); # 3 = 1/jak
  316. &pxor ("xmm3","xmm1"); # 3 = jo
  317. &movdqu ("xmm0",&QWP(0,$key));
  318. &jnz (&label("dec_loop"));
  319. # middle of last round
  320. &movdqa ("xmm4",&QWP(0x60,$base)); # 3 : sbou
  321. &pshufb ("xmm4","xmm2"); # 4 = sbou
  322. &pxor ("xmm4","xmm0"); # 4 = sb1u + k
  323. &movdqa ("xmm0",&QWP(0x70,$base)); # 0 : sbot
  324. &movdqa ("xmm2",&QWP(0,$magic));
  325. &pshufb ("xmm0","xmm3"); # 0 = sb1t
  326. &pxor ("xmm0","xmm4"); # 0 = A
  327. &pshufb ("xmm0","xmm2");
  328. &ret ();
  329. &function_end_B("_vpaes_decrypt_core");
  330. ########################################################
  331. ## ##
  332. ## AES key schedule ##
  333. ## ##
  334. ########################################################
  335. &function_begin_B("_vpaes_schedule_core");
  336. &add ($const,&DWP(0,"esp"));
  337. &movdqu ("xmm0",&QWP(0,$inp)); # load key (unaligned)
  338. &movdqa ("xmm2",&QWP($k_rcon,$const)); # load rcon
  339. # input transform
  340. &movdqa ("xmm3","xmm0");
  341. &lea ($base,&DWP($k_ipt,$const));
  342. &movdqa (&QWP(4,"esp"),"xmm2"); # xmm8
  343. &call ("_vpaes_schedule_transform");
  344. &movdqa ("xmm7","xmm0");
  345. &test ($out,$out);
  346. &jnz (&label("schedule_am_decrypting"));
  347. # encrypting, output zeroth round key after transform
  348. &movdqu (&QWP(0,$key),"xmm0");
  349. &jmp (&label("schedule_go"));
  350. &set_label("schedule_am_decrypting");
  351. # decrypting, output zeroth round key after shiftrows
  352. &movdqa ("xmm1",&QWP($k_sr,$const,$magic));
  353. &pshufb ("xmm3","xmm1");
  354. &movdqu (&QWP(0,$key),"xmm3");
  355. &xor ($magic,0x30);
  356. &set_label("schedule_go");
  357. &cmp ($round,192);
  358. &ja (&label("schedule_256"));
  359. &je (&label("schedule_192"));
  360. # 128: fall though
  361. ##
  362. ## .schedule_128
  363. ##
  364. ## 128-bit specific part of key schedule.
  365. ##
  366. ## This schedule is really simple, because all its parts
  367. ## are accomplished by the subroutines.
  368. ##
  369. &set_label("schedule_128");
  370. &mov ($round,10);
  371. &set_label("loop_schedule_128");
  372. &call ("_vpaes_schedule_round");
  373. &dec ($round);
  374. &jz (&label("schedule_mangle_last"));
  375. &call ("_vpaes_schedule_mangle"); # write output
  376. &jmp (&label("loop_schedule_128"));
  377. ##
  378. ## .aes_schedule_192
  379. ##
  380. ## 192-bit specific part of key schedule.
  381. ##
  382. ## The main body of this schedule is the same as the 128-bit
  383. ## schedule, but with more smearing. The long, high side is
  384. ## stored in %xmm7 as before, and the short, low side is in
  385. ## the high bits of %xmm6.
  386. ##
  387. ## This schedule is somewhat nastier, however, because each
  388. ## round produces 192 bits of key material, or 1.5 round keys.
  389. ## Therefore, on each cycle we do 2 rounds and produce 3 round
  390. ## keys.
  391. ##
  392. &set_label("schedule_192",16);
  393. &movdqu ("xmm0",&QWP(8,$inp)); # load key part 2 (very unaligned)
  394. &call ("_vpaes_schedule_transform"); # input transform
  395. &movdqa ("xmm6","xmm0"); # save short part
  396. &pxor ("xmm4","xmm4"); # clear 4
  397. &movhlps("xmm6","xmm4"); # clobber low side with zeros
  398. &mov ($round,4);
  399. &set_label("loop_schedule_192");
  400. &call ("_vpaes_schedule_round");
  401. &palignr("xmm0","xmm6",8);
  402. &call ("_vpaes_schedule_mangle"); # save key n
  403. &call ("_vpaes_schedule_192_smear");
  404. &call ("_vpaes_schedule_mangle"); # save key n+1
  405. &call ("_vpaes_schedule_round");
  406. &dec ($round);
  407. &jz (&label("schedule_mangle_last"));
  408. &call ("_vpaes_schedule_mangle"); # save key n+2
  409. &call ("_vpaes_schedule_192_smear");
  410. &jmp (&label("loop_schedule_192"));
  411. ##
  412. ## .aes_schedule_256
  413. ##
  414. ## 256-bit specific part of key schedule.
  415. ##
  416. ## The structure here is very similar to the 128-bit
  417. ## schedule, but with an additional "low side" in
  418. ## %xmm6. The low side's rounds are the same as the
  419. ## high side's, except no rcon and no rotation.
  420. ##
  421. &set_label("schedule_256",16);
  422. &movdqu ("xmm0",&QWP(16,$inp)); # load key part 2 (unaligned)
  423. &call ("_vpaes_schedule_transform"); # input transform
  424. &mov ($round,7);
  425. &set_label("loop_schedule_256");
  426. &call ("_vpaes_schedule_mangle"); # output low result
  427. &movdqa ("xmm6","xmm0"); # save cur_lo in xmm6
  428. # high round
  429. &call ("_vpaes_schedule_round");
  430. &dec ($round);
  431. &jz (&label("schedule_mangle_last"));
  432. &call ("_vpaes_schedule_mangle");
  433. # low round. swap xmm7 and xmm6
  434. &pshufd ("xmm0","xmm0",0xFF);
  435. &movdqa (&QWP(20,"esp"),"xmm7");
  436. &movdqa ("xmm7","xmm6");
  437. &call ("_vpaes_schedule_low_round");
  438. &movdqa ("xmm7",&QWP(20,"esp"));
  439. &jmp (&label("loop_schedule_256"));
  440. ##
  441. ## .aes_schedule_mangle_last
  442. ##
  443. ## Mangler for last round of key schedule
  444. ## Mangles %xmm0
  445. ## when encrypting, outputs out(%xmm0) ^ 63
  446. ## when decrypting, outputs unskew(%xmm0)
  447. ##
  448. ## Always called right before return... jumps to cleanup and exits
  449. ##
  450. &set_label("schedule_mangle_last",16);
  451. # schedule last round key from xmm0
  452. &lea ($base,&DWP($k_deskew,$const));
  453. &test ($out,$out);
  454. &jnz (&label("schedule_mangle_last_dec"));
  455. # encrypting
  456. &movdqa ("xmm1",&QWP($k_sr,$const,$magic));
  457. &pshufb ("xmm0","xmm1"); # output permute
  458. &lea ($base,&DWP($k_opt,$const)); # prepare to output transform
  459. &add ($key,32);
  460. &set_label("schedule_mangle_last_dec");
  461. &add ($key,-16);
  462. &pxor ("xmm0",&QWP($k_s63,$const));
  463. &call ("_vpaes_schedule_transform"); # output transform
  464. &movdqu (&QWP(0,$key),"xmm0"); # save last key
  465. # cleanup
  466. &pxor ("xmm0","xmm0");
  467. &pxor ("xmm1","xmm1");
  468. &pxor ("xmm2","xmm2");
  469. &pxor ("xmm3","xmm3");
  470. &pxor ("xmm4","xmm4");
  471. &pxor ("xmm5","xmm5");
  472. &pxor ("xmm6","xmm6");
  473. &pxor ("xmm7","xmm7");
  474. &ret ();
  475. &function_end_B("_vpaes_schedule_core");
  476. ##
  477. ## .aes_schedule_192_smear
  478. ##
  479. ## Smear the short, low side in the 192-bit key schedule.
  480. ##
  481. ## Inputs:
  482. ## %xmm7: high side, b a x y
  483. ## %xmm6: low side, d c 0 0
  484. ## %xmm13: 0
  485. ##
  486. ## Outputs:
  487. ## %xmm6: b+c+d b+c 0 0
  488. ## %xmm0: b+c+d b+c b a
  489. ##
  490. &function_begin_B("_vpaes_schedule_192_smear");
  491. &pshufd ("xmm0","xmm6",0x80); # d c 0 0 -> c 0 0 0
  492. &pxor ("xmm6","xmm0"); # -> c+d c 0 0
  493. &pshufd ("xmm0","xmm7",0xFE); # b a _ _ -> b b b a
  494. &pxor ("xmm6","xmm0"); # -> b+c+d b+c b a
  495. &movdqa ("xmm0","xmm6");
  496. &pxor ("xmm1","xmm1");
  497. &movhlps("xmm6","xmm1"); # clobber low side with zeros
  498. &ret ();
  499. &function_end_B("_vpaes_schedule_192_smear");
  500. ##
  501. ## .aes_schedule_round
  502. ##
  503. ## Runs one main round of the key schedule on %xmm0, %xmm7
  504. ##
  505. ## Specifically, runs subbytes on the high dword of %xmm0
  506. ## then rotates it by one byte and xors into the low dword of
  507. ## %xmm7.
  508. ##
  509. ## Adds rcon from low byte of %xmm8, then rotates %xmm8 for
  510. ## next rcon.
  511. ##
  512. ## Smears the dwords of %xmm7 by xoring the low into the
  513. ## second low, result into third, result into highest.
  514. ##
  515. ## Returns results in %xmm7 = %xmm0.
  516. ## Clobbers %xmm1-%xmm5.
  517. ##
  518. &function_begin_B("_vpaes_schedule_round");
  519. # extract rcon from xmm8
  520. &movdqa ("xmm2",&QWP(8,"esp")); # xmm8
  521. &pxor ("xmm1","xmm1");
  522. &palignr("xmm1","xmm2",15);
  523. &palignr("xmm2","xmm2",15);
  524. &pxor ("xmm7","xmm1");
  525. # rotate
  526. &pshufd ("xmm0","xmm0",0xFF);
  527. &palignr("xmm0","xmm0",1);
  528. # fall through...
  529. &movdqa (&QWP(8,"esp"),"xmm2"); # xmm8
  530. # low round: same as high round, but no rotation and no rcon.
  531. &set_label("_vpaes_schedule_low_round");
  532. # smear xmm7
  533. &movdqa ("xmm1","xmm7");
  534. &pslldq ("xmm7",4);
  535. &pxor ("xmm7","xmm1");
  536. &movdqa ("xmm1","xmm7");
  537. &pslldq ("xmm7",8);
  538. &pxor ("xmm7","xmm1");
  539. &pxor ("xmm7",&QWP($k_s63,$const));
  540. # subbyte
  541. &movdqa ("xmm4",&QWP($k_s0F,$const));
  542. &movdqa ("xmm5",&QWP($k_inv,$const)); # 4 : 1/j
  543. &movdqa ("xmm1","xmm4");
  544. &pandn ("xmm1","xmm0");
  545. &psrld ("xmm1",4); # 1 = i
  546. &pand ("xmm0","xmm4"); # 0 = k
  547. &movdqa ("xmm2",&QWP($k_inv+16,$const));# 2 : a/k
  548. &pshufb ("xmm2","xmm0"); # 2 = a/k
  549. &pxor ("xmm0","xmm1"); # 0 = j
  550. &movdqa ("xmm3","xmm5"); # 3 : 1/i
  551. &pshufb ("xmm3","xmm1"); # 3 = 1/i
  552. &pxor ("xmm3","xmm2"); # 3 = iak = 1/i + a/k
  553. &movdqa ("xmm4","xmm5"); # 4 : 1/j
  554. &pshufb ("xmm4","xmm0"); # 4 = 1/j
  555. &pxor ("xmm4","xmm2"); # 4 = jak = 1/j + a/k
  556. &movdqa ("xmm2","xmm5"); # 2 : 1/iak
  557. &pshufb ("xmm2","xmm3"); # 2 = 1/iak
  558. &pxor ("xmm2","xmm0"); # 2 = io
  559. &movdqa ("xmm3","xmm5"); # 3 : 1/jak
  560. &pshufb ("xmm3","xmm4"); # 3 = 1/jak
  561. &pxor ("xmm3","xmm1"); # 3 = jo
  562. &movdqa ("xmm4",&QWP($k_sb1,$const)); # 4 : sbou
  563. &pshufb ("xmm4","xmm2"); # 4 = sbou
  564. &movdqa ("xmm0",&QWP($k_sb1+16,$const));# 0 : sbot
  565. &pshufb ("xmm0","xmm3"); # 0 = sb1t
  566. &pxor ("xmm0","xmm4"); # 0 = sbox output
  567. # add in smeared stuff
  568. &pxor ("xmm0","xmm7");
  569. &movdqa ("xmm7","xmm0");
  570. &ret ();
  571. &function_end_B("_vpaes_schedule_round");
  572. ##
  573. ## .aes_schedule_transform
  574. ##
  575. ## Linear-transform %xmm0 according to tables at (%ebx)
  576. ##
  577. ## Output in %xmm0
  578. ## Clobbers %xmm1, %xmm2
  579. ##
  580. &function_begin_B("_vpaes_schedule_transform");
  581. &movdqa ("xmm2",&QWP($k_s0F,$const));
  582. &movdqa ("xmm1","xmm2");
  583. &pandn ("xmm1","xmm0");
  584. &psrld ("xmm1",4);
  585. &pand ("xmm0","xmm2");
  586. &movdqa ("xmm2",&QWP(0,$base));
  587. &pshufb ("xmm2","xmm0");
  588. &movdqa ("xmm0",&QWP(16,$base));
  589. &pshufb ("xmm0","xmm1");
  590. &pxor ("xmm0","xmm2");
  591. &ret ();
  592. &function_end_B("_vpaes_schedule_transform");
  593. ##
  594. ## .aes_schedule_mangle
  595. ##
  596. ## Mangle xmm0 from (basis-transformed) standard version
  597. ## to our version.
  598. ##
  599. ## On encrypt,
  600. ## xor with 0x63
  601. ## multiply by circulant 0,1,1,1
  602. ## apply shiftrows transform
  603. ##
  604. ## On decrypt,
  605. ## xor with 0x63
  606. ## multiply by "inverse mixcolumns" circulant E,B,D,9
  607. ## deskew
  608. ## apply shiftrows transform
  609. ##
  610. ##
  611. ## Writes out to (%edx), and increments or decrements it
  612. ## Keeps track of round number mod 4 in %ecx
  613. ## Preserves xmm0
  614. ## Clobbers xmm1-xmm5
  615. ##
  616. &function_begin_B("_vpaes_schedule_mangle");
  617. &movdqa ("xmm4","xmm0"); # save xmm0 for later
  618. &movdqa ("xmm5",&QWP($k_mc_forward,$const));
  619. &test ($out,$out);
  620. &jnz (&label("schedule_mangle_dec"));
  621. # encrypting
  622. &add ($key,16);
  623. &pxor ("xmm4",&QWP($k_s63,$const));
  624. &pshufb ("xmm4","xmm5");
  625. &movdqa ("xmm3","xmm4");
  626. &pshufb ("xmm4","xmm5");
  627. &pxor ("xmm3","xmm4");
  628. &pshufb ("xmm4","xmm5");
  629. &pxor ("xmm3","xmm4");
  630. &jmp (&label("schedule_mangle_both"));
  631. &set_label("schedule_mangle_dec",16);
  632. # inverse mix columns
  633. &movdqa ("xmm2",&QWP($k_s0F,$const));
  634. &lea ($inp,&DWP($k_dksd,$const));
  635. &movdqa ("xmm1","xmm2");
  636. &pandn ("xmm1","xmm4");
  637. &psrld ("xmm1",4); # 1 = hi
  638. &pand ("xmm4","xmm2"); # 4 = lo
  639. &movdqa ("xmm2",&QWP(0,$inp));
  640. &pshufb ("xmm2","xmm4");
  641. &movdqa ("xmm3",&QWP(0x10,$inp));
  642. &pshufb ("xmm3","xmm1");
  643. &pxor ("xmm3","xmm2");
  644. &pshufb ("xmm3","xmm5");
  645. &movdqa ("xmm2",&QWP(0x20,$inp));
  646. &pshufb ("xmm2","xmm4");
  647. &pxor ("xmm2","xmm3");
  648. &movdqa ("xmm3",&QWP(0x30,$inp));
  649. &pshufb ("xmm3","xmm1");
  650. &pxor ("xmm3","xmm2");
  651. &pshufb ("xmm3","xmm5");
  652. &movdqa ("xmm2",&QWP(0x40,$inp));
  653. &pshufb ("xmm2","xmm4");
  654. &pxor ("xmm2","xmm3");
  655. &movdqa ("xmm3",&QWP(0x50,$inp));
  656. &pshufb ("xmm3","xmm1");
  657. &pxor ("xmm3","xmm2");
  658. &pshufb ("xmm3","xmm5");
  659. &movdqa ("xmm2",&QWP(0x60,$inp));
  660. &pshufb ("xmm2","xmm4");
  661. &pxor ("xmm2","xmm3");
  662. &movdqa ("xmm3",&QWP(0x70,$inp));
  663. &pshufb ("xmm3","xmm1");
  664. &pxor ("xmm3","xmm2");
  665. &add ($key,-16);
  666. &set_label("schedule_mangle_both");
  667. &movdqa ("xmm1",&QWP($k_sr,$const,$magic));
  668. &pshufb ("xmm3","xmm1");
  669. &add ($magic,-16);
  670. &and ($magic,0x30);
  671. &movdqu (&QWP(0,$key),"xmm3");
  672. &ret ();
  673. &function_end_B("_vpaes_schedule_mangle");
  674. #
  675. # Interface to OpenSSL
  676. #
  677. &function_begin("${PREFIX}_set_encrypt_key");
  678. &mov ($inp,&wparam(0)); # inp
  679. &lea ($base,&DWP(-56,"esp"));
  680. &mov ($round,&wparam(1)); # bits
  681. &and ($base,-16);
  682. &mov ($key,&wparam(2)); # key
  683. &xchg ($base,"esp"); # alloca
  684. &mov (&DWP(48,"esp"),$base);
  685. &mov ($base,$round);
  686. &shr ($base,5);
  687. &add ($base,5);
  688. &mov (&DWP(240,$key),$base); # AES_KEY->rounds = nbits/32+5;
  689. &mov ($magic,0x30);
  690. &mov ($out,0);
  691. &lea ($const,&DWP(&label("_vpaes_consts")."+0x30-".&label("pic_point")));
  692. &call ("_vpaes_schedule_core");
  693. &set_label("pic_point");
  694. &mov ("esp",&DWP(48,"esp"));
  695. &xor ("eax","eax");
  696. &function_end("${PREFIX}_set_encrypt_key");
  697. &function_begin("${PREFIX}_set_decrypt_key");
  698. &mov ($inp,&wparam(0)); # inp
  699. &lea ($base,&DWP(-56,"esp"));
  700. &mov ($round,&wparam(1)); # bits
  701. &and ($base,-16);
  702. &mov ($key,&wparam(2)); # key
  703. &xchg ($base,"esp"); # alloca
  704. &mov (&DWP(48,"esp"),$base);
  705. &mov ($base,$round);
  706. &shr ($base,5);
  707. &add ($base,5);
  708. &mov (&DWP(240,$key),$base); # AES_KEY->rounds = nbits/32+5;
  709. &shl ($base,4);
  710. &lea ($key,&DWP(16,$key,$base));
  711. &mov ($out,1);
  712. &mov ($magic,$round);
  713. &shr ($magic,1);
  714. &and ($magic,32);
  715. &xor ($magic,32); # nbist==192?0:32;
  716. &lea ($const,&DWP(&label("_vpaes_consts")."+0x30-".&label("pic_point")));
  717. &call ("_vpaes_schedule_core");
  718. &set_label("pic_point");
  719. &mov ("esp",&DWP(48,"esp"));
  720. &xor ("eax","eax");
  721. &function_end("${PREFIX}_set_decrypt_key");
  722. &function_begin("${PREFIX}_encrypt");
  723. &lea ($const,&DWP(&label("_vpaes_consts")."+0x30-".&label("pic_point")));
  724. &call ("_vpaes_preheat");
  725. &set_label("pic_point");
  726. &mov ($inp,&wparam(0)); # inp
  727. &lea ($base,&DWP(-56,"esp"));
  728. &mov ($out,&wparam(1)); # out
  729. &and ($base,-16);
  730. &mov ($key,&wparam(2)); # key
  731. &xchg ($base,"esp"); # alloca
  732. &mov (&DWP(48,"esp"),$base);
  733. &movdqu ("xmm0",&QWP(0,$inp));
  734. &call ("_vpaes_encrypt_core");
  735. &movdqu (&QWP(0,$out),"xmm0");
  736. &mov ("esp",&DWP(48,"esp"));
  737. &function_end("${PREFIX}_encrypt");
  738. &function_begin("${PREFIX}_decrypt");
  739. &lea ($const,&DWP(&label("_vpaes_consts")."+0x30-".&label("pic_point")));
  740. &call ("_vpaes_preheat");
  741. &set_label("pic_point");
  742. &mov ($inp,&wparam(0)); # inp
  743. &lea ($base,&DWP(-56,"esp"));
  744. &mov ($out,&wparam(1)); # out
  745. &and ($base,-16);
  746. &mov ($key,&wparam(2)); # key
  747. &xchg ($base,"esp"); # alloca
  748. &mov (&DWP(48,"esp"),$base);
  749. &movdqu ("xmm0",&QWP(0,$inp));
  750. &call ("_vpaes_decrypt_core");
  751. &movdqu (&QWP(0,$out),"xmm0");
  752. &mov ("esp",&DWP(48,"esp"));
  753. &function_end("${PREFIX}_decrypt");
  754. &function_begin("${PREFIX}_cbc_encrypt");
  755. &mov ($inp,&wparam(0)); # inp
  756. &mov ($out,&wparam(1)); # out
  757. &mov ($round,&wparam(2)); # len
  758. &mov ($key,&wparam(3)); # key
  759. &lea ($base,&DWP(-56,"esp"));
  760. &mov ($const,&wparam(4)); # ivp
  761. &and ($base,-16);
  762. &mov ($magic,&wparam(5)); # enc
  763. &xchg ($base,"esp"); # alloca
  764. &movdqu ("xmm1",&QWP(0,$const)); # load IV
  765. &sub ($out,$inp);
  766. &mov (&DWP(48,"esp"),$base);
  767. &mov (&DWP(0,"esp"),$out); # save out
  768. &sub ($round,16);
  769. &mov (&DWP(4,"esp"),$key) # save key
  770. &mov (&DWP(8,"esp"),$const); # save ivp
  771. &mov ($out,$round); # $out works as $len
  772. &lea ($const,&DWP(&label("_vpaes_consts")."+0x30-".&label("pic_point")));
  773. &call ("_vpaes_preheat");
  774. &set_label("pic_point");
  775. &cmp ($magic,0);
  776. &je (&label("cbc_dec_loop"));
  777. &jmp (&label("cbc_enc_loop"));
  778. &set_label("cbc_enc_loop",16);
  779. &movdqu ("xmm0",&QWP(0,$inp)); # load input
  780. &pxor ("xmm0","xmm1"); # inp^=iv
  781. &call ("_vpaes_encrypt_core");
  782. &mov ($base,&DWP(0,"esp")); # restore out
  783. &mov ($key,&DWP(4,"esp")); # restore key
  784. &movdqa ("xmm1","xmm0");
  785. &movdqu (&QWP(0,$base,$inp),"xmm0"); # write output
  786. &lea ($inp,&DWP(16,$inp));
  787. &sub ($out,16);
  788. &jnc (&label("cbc_enc_loop"));
  789. &jmp (&label("cbc_done"));
  790. &set_label("cbc_dec_loop",16);
  791. &movdqu ("xmm0",&QWP(0,$inp)); # load input
  792. &movdqa (&QWP(16,"esp"),"xmm1"); # save IV
  793. &movdqa (&QWP(32,"esp"),"xmm0"); # save future IV
  794. &call ("_vpaes_decrypt_core");
  795. &mov ($base,&DWP(0,"esp")); # restore out
  796. &mov ($key,&DWP(4,"esp")); # restore key
  797. &pxor ("xmm0",&QWP(16,"esp")); # out^=iv
  798. &movdqa ("xmm1",&QWP(32,"esp")); # load next IV
  799. &movdqu (&QWP(0,$base,$inp),"xmm0"); # write output
  800. &lea ($inp,&DWP(16,$inp));
  801. &sub ($out,16);
  802. &jnc (&label("cbc_dec_loop"));
  803. &set_label("cbc_done");
  804. &mov ($base,&DWP(8,"esp")); # restore ivp
  805. &mov ("esp",&DWP(48,"esp"));
  806. &movdqu (&QWP(0,$base),"xmm1"); # write IV
  807. &function_end("${PREFIX}_cbc_encrypt");
  808. &asm_finish();