vpaes-x86.pl 27 KB

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