You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2532 lines
74 KiB

  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  4. # project. The module is, however, dual licensed under OpenSSL and
  5. # CRYPTOGAMS licenses depending on where you obtain it. For further
  6. # details see http://www.openssl.org/~appro/cryptogams/.
  7. # ====================================================================
  8. #
  9. # This module implements support for Intel AES-NI extension. In
  10. # OpenSSL context it's used with Intel engine, but can also be used as
  11. # drop-in replacement for crypto/aes/asm/aes-586.pl [see below for
  12. # details].
  13. #
  14. # Performance.
  15. #
  16. # To start with see corresponding paragraph in aesni-x86_64.pl...
  17. # Instead of filling table similar to one found there I've chosen to
  18. # summarize *comparison* results for raw ECB, CTR and CBC benchmarks.
  19. # The simplified table below represents 32-bit performance relative
  20. # to 64-bit one in every given point. Ratios vary for different
  21. # encryption modes, therefore interval values.
  22. #
  23. # 16-byte 64-byte 256-byte 1-KB 8-KB
  24. # 53-67% 67-84% 91-94% 95-98% 97-99.5%
  25. #
  26. # Lower ratios for smaller block sizes are perfectly understandable,
  27. # because function call overhead is higher in 32-bit mode. Largest
  28. # 8-KB block performance is virtually same: 32-bit code is less than
  29. # 1% slower for ECB, CBC and CCM, and ~3% slower otherwise.
  30. # January 2011
  31. #
  32. # See aesni-x86_64.pl for details. Unlike x86_64 version this module
  33. # interleaves at most 6 aes[enc|dec] instructions, because there are
  34. # not enough registers for 8x interleave [which should be optimal for
  35. # Sandy Bridge]. Actually, performance results for 6x interleave
  36. # factor presented in aesni-x86_64.pl (except for CTR) are for this
  37. # module.
  38. # April 2011
  39. #
  40. # Add aesni_xts_[en|de]crypt. Westmere spends 1.50 cycles processing
  41. # one byte out of 8KB with 128-bit key, Sandy Bridge - 1.09.
  42. ######################################################################
  43. # Current large-block performance in cycles per byte processed with
  44. # 128-bit key (less is better).
  45. #
  46. # CBC en-/decrypt CTR XTS ECB
  47. # Westmere 3.77/1.37 1.37 1.52 1.27
  48. # * Bridge 5.07/0.98 0.99 1.09 0.91
  49. # Haswell 4.44/0.80 0.97 1.03 0.72
  50. # Silvermont 5.77/3.56 3.67 4.03 3.46
  51. # Bulldozer 5.80/0.98 1.05 1.24 0.93
  52. $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
  53. # generates drop-in replacement for
  54. # crypto/aes/asm/aes-586.pl:-)
  55. $inline=1; # inline _aesni_[en|de]crypt
  56. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  57. push(@INC,"${dir}","${dir}../../perlasm");
  58. require "x86asm.pl";
  59. $output = pop;
  60. open OUT,">$output";
  61. *STDOUT=*OUT;
  62. &asm_init($ARGV[0],$0);
  63. &external_label("OPENSSL_ia32cap_P");
  64. &static_label("key_const");
  65. if ($PREFIX eq "aesni") { $movekey=\&movups; }
  66. else { $movekey=\&movups; }
  67. $len="eax";
  68. $rounds="ecx";
  69. $key="edx";
  70. $inp="esi";
  71. $out="edi";
  72. $rounds_="ebx"; # backup copy for $rounds
  73. $key_="ebp"; # backup copy for $key
  74. $rndkey0="xmm0";
  75. $rndkey1="xmm1";
  76. $inout0="xmm2";
  77. $inout1="xmm3";
  78. $inout2="xmm4";
  79. $inout3="xmm5"; $in1="xmm5";
  80. $inout4="xmm6"; $in0="xmm6";
  81. $inout5="xmm7"; $ivec="xmm7";
  82. # AESNI extension
  83. sub aeskeygenassist
  84. { my($dst,$src,$imm)=@_;
  85. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  86. { &data_byte(0x66,0x0f,0x3a,0xdf,0xc0|($1<<3)|$2,$imm); }
  87. }
  88. sub aescommon
  89. { my($opcodelet,$dst,$src)=@_;
  90. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  91. { &data_byte(0x66,0x0f,0x38,$opcodelet,0xc0|($1<<3)|$2);}
  92. }
  93. sub aesimc { aescommon(0xdb,@_); }
  94. sub aesenc { aescommon(0xdc,@_); }
  95. sub aesenclast { aescommon(0xdd,@_); }
  96. sub aesdec { aescommon(0xde,@_); }
  97. sub aesdeclast { aescommon(0xdf,@_); }
  98. # Inline version of internal aesni_[en|de]crypt1
  99. { my $sn;
  100. sub aesni_inline_generate1
  101. { my ($p,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
  102. $sn++;
  103. &$movekey ($rndkey0,&QWP(0,$key));
  104. &$movekey ($rndkey1,&QWP(16,$key));
  105. &xorps ($ivec,$rndkey0) if (defined($ivec));
  106. &lea ($key,&DWP(32,$key));
  107. &xorps ($inout,$ivec) if (defined($ivec));
  108. &xorps ($inout,$rndkey0) if (!defined($ivec));
  109. &set_label("${p}1_loop_$sn");
  110. eval"&aes${p} ($inout,$rndkey1)";
  111. &dec ($rounds);
  112. &$movekey ($rndkey1,&QWP(0,$key));
  113. &lea ($key,&DWP(16,$key));
  114. &jnz (&label("${p}1_loop_$sn"));
  115. eval"&aes${p}last ($inout,$rndkey1)";
  116. }}
  117. sub aesni_generate1 # fully unrolled loop
  118. { my ($p,$inout)=@_; $inout=$inout0 if (!defined($inout));
  119. &function_begin_B("_aesni_${p}rypt1");
  120. &movups ($rndkey0,&QWP(0,$key));
  121. &$movekey ($rndkey1,&QWP(0x10,$key));
  122. &xorps ($inout,$rndkey0);
  123. &$movekey ($rndkey0,&QWP(0x20,$key));
  124. &lea ($key,&DWP(0x30,$key));
  125. &cmp ($rounds,11);
  126. &jb (&label("${p}128"));
  127. &lea ($key,&DWP(0x20,$key));
  128. &je (&label("${p}192"));
  129. &lea ($key,&DWP(0x20,$key));
  130. eval"&aes${p} ($inout,$rndkey1)";
  131. &$movekey ($rndkey1,&QWP(-0x40,$key));
  132. eval"&aes${p} ($inout,$rndkey0)";
  133. &$movekey ($rndkey0,&QWP(-0x30,$key));
  134. &set_label("${p}192");
  135. eval"&aes${p} ($inout,$rndkey1)";
  136. &$movekey ($rndkey1,&QWP(-0x20,$key));
  137. eval"&aes${p} ($inout,$rndkey0)";
  138. &$movekey ($rndkey0,&QWP(-0x10,$key));
  139. &set_label("${p}128");
  140. eval"&aes${p} ($inout,$rndkey1)";
  141. &$movekey ($rndkey1,&QWP(0,$key));
  142. eval"&aes${p} ($inout,$rndkey0)";
  143. &$movekey ($rndkey0,&QWP(0x10,$key));
  144. eval"&aes${p} ($inout,$rndkey1)";
  145. &$movekey ($rndkey1,&QWP(0x20,$key));
  146. eval"&aes${p} ($inout,$rndkey0)";
  147. &$movekey ($rndkey0,&QWP(0x30,$key));
  148. eval"&aes${p} ($inout,$rndkey1)";
  149. &$movekey ($rndkey1,&QWP(0x40,$key));
  150. eval"&aes${p} ($inout,$rndkey0)";
  151. &$movekey ($rndkey0,&QWP(0x50,$key));
  152. eval"&aes${p} ($inout,$rndkey1)";
  153. &$movekey ($rndkey1,&QWP(0x60,$key));
  154. eval"&aes${p} ($inout,$rndkey0)";
  155. &$movekey ($rndkey0,&QWP(0x70,$key));
  156. eval"&aes${p} ($inout,$rndkey1)";
  157. eval"&aes${p}last ($inout,$rndkey0)";
  158. &ret();
  159. &function_end_B("_aesni_${p}rypt1");
  160. }
  161. # void $PREFIX_encrypt (const void *inp,void *out,const AES_KEY *key);
  162. &aesni_generate1("enc") if (!$inline);
  163. &function_begin_B("${PREFIX}_encrypt");
  164. &mov ("eax",&wparam(0));
  165. &mov ($key,&wparam(2));
  166. &movups ($inout0,&QWP(0,"eax"));
  167. &mov ($rounds,&DWP(240,$key));
  168. &mov ("eax",&wparam(1));
  169. if ($inline)
  170. { &aesni_inline_generate1("enc"); }
  171. else
  172. { &call ("_aesni_encrypt1"); }
  173. &pxor ($rndkey0,$rndkey0); # clear register bank
  174. &pxor ($rndkey1,$rndkey1);
  175. &movups (&QWP(0,"eax"),$inout0);
  176. &pxor ($inout0,$inout0);
  177. &ret ();
  178. &function_end_B("${PREFIX}_encrypt");
  179. # void $PREFIX_decrypt (const void *inp,void *out,const AES_KEY *key);
  180. &aesni_generate1("dec") if(!$inline);
  181. &function_begin_B("${PREFIX}_decrypt");
  182. &mov ("eax",&wparam(0));
  183. &mov ($key,&wparam(2));
  184. &movups ($inout0,&QWP(0,"eax"));
  185. &mov ($rounds,&DWP(240,$key));
  186. &mov ("eax",&wparam(1));
  187. if ($inline)
  188. { &aesni_inline_generate1("dec"); }
  189. else
  190. { &call ("_aesni_decrypt1"); }
  191. &pxor ($rndkey0,$rndkey0); # clear register bank
  192. &pxor ($rndkey1,$rndkey1);
  193. &movups (&QWP(0,"eax"),$inout0);
  194. &pxor ($inout0,$inout0);
  195. &ret ();
  196. &function_end_B("${PREFIX}_decrypt");
  197. # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
  198. # factor. Why 3x subroutine were originally used in loops? Even though
  199. # aes[enc|dec] latency was originally 6, it could be scheduled only
  200. # every *2nd* cycle. Thus 3x interleave was the one providing optimal
  201. # utilization, i.e. when subroutine's throughput is virtually same as
  202. # of non-interleaved subroutine [for number of input blocks up to 3].
  203. # This is why it originally made no sense to implement 2x subroutine.
  204. # But times change and it became appropriate to spend extra 192 bytes
  205. # on 2x subroutine on Atom Silvermont account. For processors that
  206. # can schedule aes[enc|dec] every cycle optimal interleave factor
  207. # equals to corresponding instructions latency. 8x is optimal for
  208. # * Bridge, but it's unfeasible to accommodate such implementation
  209. # in XMM registers addreassable in 32-bit mode and therefore maximum
  210. # of 6x is used instead...
  211. sub aesni_generate2
  212. { my $p=shift;
  213. &function_begin_B("_aesni_${p}rypt2");
  214. &$movekey ($rndkey0,&QWP(0,$key));
  215. &shl ($rounds,4);
  216. &$movekey ($rndkey1,&QWP(16,$key));
  217. &xorps ($inout0,$rndkey0);
  218. &pxor ($inout1,$rndkey0);
  219. &$movekey ($rndkey0,&QWP(32,$key));
  220. &lea ($key,&DWP(32,$key,$rounds));
  221. &neg ($rounds);
  222. &add ($rounds,16);
  223. &set_label("${p}2_loop");
  224. eval"&aes${p} ($inout0,$rndkey1)";
  225. eval"&aes${p} ($inout1,$rndkey1)";
  226. &$movekey ($rndkey1,&QWP(0,$key,$rounds));
  227. &add ($rounds,32);
  228. eval"&aes${p} ($inout0,$rndkey0)";
  229. eval"&aes${p} ($inout1,$rndkey0)";
  230. &$movekey ($rndkey0,&QWP(-16,$key,$rounds));
  231. &jnz (&label("${p}2_loop"));
  232. eval"&aes${p} ($inout0,$rndkey1)";
  233. eval"&aes${p} ($inout1,$rndkey1)";
  234. eval"&aes${p}last ($inout0,$rndkey0)";
  235. eval"&aes${p}last ($inout1,$rndkey0)";
  236. &ret();
  237. &function_end_B("_aesni_${p}rypt2");
  238. }
  239. sub aesni_generate3
  240. { my $p=shift;
  241. &function_begin_B("_aesni_${p}rypt3");
  242. &$movekey ($rndkey0,&QWP(0,$key));
  243. &shl ($rounds,4);
  244. &$movekey ($rndkey1,&QWP(16,$key));
  245. &xorps ($inout0,$rndkey0);
  246. &pxor ($inout1,$rndkey0);
  247. &pxor ($inout2,$rndkey0);
  248. &$movekey ($rndkey0,&QWP(32,$key));
  249. &lea ($key,&DWP(32,$key,$rounds));
  250. &neg ($rounds);
  251. &add ($rounds,16);
  252. &set_label("${p}3_loop");
  253. eval"&aes${p} ($inout0,$rndkey1)";
  254. eval"&aes${p} ($inout1,$rndkey1)";
  255. eval"&aes${p} ($inout2,$rndkey1)";
  256. &$movekey ($rndkey1,&QWP(0,$key,$rounds));
  257. &add ($rounds,32);
  258. eval"&aes${p} ($inout0,$rndkey0)";
  259. eval"&aes${p} ($inout1,$rndkey0)";
  260. eval"&aes${p} ($inout2,$rndkey0)";
  261. &$movekey ($rndkey0,&QWP(-16,$key,$rounds));
  262. &jnz (&label("${p}3_loop"));
  263. eval"&aes${p} ($inout0,$rndkey1)";
  264. eval"&aes${p} ($inout1,$rndkey1)";
  265. eval"&aes${p} ($inout2,$rndkey1)";
  266. eval"&aes${p}last ($inout0,$rndkey0)";
  267. eval"&aes${p}last ($inout1,$rndkey0)";
  268. eval"&aes${p}last ($inout2,$rndkey0)";
  269. &ret();
  270. &function_end_B("_aesni_${p}rypt3");
  271. }
  272. # 4x interleave is implemented to improve small block performance,
  273. # most notably [and naturally] 4 block by ~30%. One can argue that one
  274. # should have implemented 5x as well, but improvement would be <20%,
  275. # so it's not worth it...
  276. sub aesni_generate4
  277. { my $p=shift;
  278. &function_begin_B("_aesni_${p}rypt4");
  279. &$movekey ($rndkey0,&QWP(0,$key));
  280. &$movekey ($rndkey1,&QWP(16,$key));
  281. &shl ($rounds,4);
  282. &xorps ($inout0,$rndkey0);
  283. &pxor ($inout1,$rndkey0);
  284. &pxor ($inout2,$rndkey0);
  285. &pxor ($inout3,$rndkey0);
  286. &$movekey ($rndkey0,&QWP(32,$key));
  287. &lea ($key,&DWP(32,$key,$rounds));
  288. &neg ($rounds);
  289. &data_byte (0x0f,0x1f,0x40,0x00);
  290. &add ($rounds,16);
  291. &set_label("${p}4_loop");
  292. eval"&aes${p} ($inout0,$rndkey1)";
  293. eval"&aes${p} ($inout1,$rndkey1)";
  294. eval"&aes${p} ($inout2,$rndkey1)";
  295. eval"&aes${p} ($inout3,$rndkey1)";
  296. &$movekey ($rndkey1,&QWP(0,$key,$rounds));
  297. &add ($rounds,32);
  298. eval"&aes${p} ($inout0,$rndkey0)";
  299. eval"&aes${p} ($inout1,$rndkey0)";
  300. eval"&aes${p} ($inout2,$rndkey0)";
  301. eval"&aes${p} ($inout3,$rndkey0)";
  302. &$movekey ($rndkey0,&QWP(-16,$key,$rounds));
  303. &jnz (&label("${p}4_loop"));
  304. eval"&aes${p} ($inout0,$rndkey1)";
  305. eval"&aes${p} ($inout1,$rndkey1)";
  306. eval"&aes${p} ($inout2,$rndkey1)";
  307. eval"&aes${p} ($inout3,$rndkey1)";
  308. eval"&aes${p}last ($inout0,$rndkey0)";
  309. eval"&aes${p}last ($inout1,$rndkey0)";
  310. eval"&aes${p}last ($inout2,$rndkey0)";
  311. eval"&aes${p}last ($inout3,$rndkey0)";
  312. &ret();
  313. &function_end_B("_aesni_${p}rypt4");
  314. }
  315. sub aesni_generate6
  316. { my $p=shift;
  317. &function_begin_B("_aesni_${p}rypt6");
  318. &static_label("_aesni_${p}rypt6_enter");
  319. &$movekey ($rndkey0,&QWP(0,$key));
  320. &shl ($rounds,4);
  321. &$movekey ($rndkey1,&QWP(16,$key));
  322. &xorps ($inout0,$rndkey0);
  323. &pxor ($inout1,$rndkey0); # pxor does better here
  324. &pxor ($inout2,$rndkey0);
  325. eval"&aes${p} ($inout0,$rndkey1)";
  326. &pxor ($inout3,$rndkey0);
  327. &pxor ($inout4,$rndkey0);
  328. eval"&aes${p} ($inout1,$rndkey1)";
  329. &lea ($key,&DWP(32,$key,$rounds));
  330. &neg ($rounds);
  331. eval"&aes${p} ($inout2,$rndkey1)";
  332. &pxor ($inout5,$rndkey0);
  333. &$movekey ($rndkey0,&QWP(0,$key,$rounds));
  334. &add ($rounds,16);
  335. &jmp (&label("_aesni_${p}rypt6_inner"));
  336. &set_label("${p}6_loop",16);
  337. eval"&aes${p} ($inout0,$rndkey1)";
  338. eval"&aes${p} ($inout1,$rndkey1)";
  339. eval"&aes${p} ($inout2,$rndkey1)";
  340. &set_label("_aesni_${p}rypt6_inner");
  341. eval"&aes${p} ($inout3,$rndkey1)";
  342. eval"&aes${p} ($inout4,$rndkey1)";
  343. eval"&aes${p} ($inout5,$rndkey1)";
  344. &set_label("_aesni_${p}rypt6_enter");
  345. &$movekey ($rndkey1,&QWP(0,$key,$rounds));
  346. &add ($rounds,32);
  347. eval"&aes${p} ($inout0,$rndkey0)";
  348. eval"&aes${p} ($inout1,$rndkey0)";
  349. eval"&aes${p} ($inout2,$rndkey0)";
  350. eval"&aes${p} ($inout3,$rndkey0)";
  351. eval"&aes${p} ($inout4,$rndkey0)";
  352. eval"&aes${p} ($inout5,$rndkey0)";
  353. &$movekey ($rndkey0,&QWP(-16,$key,$rounds));
  354. &jnz (&label("${p}6_loop"));
  355. eval"&aes${p} ($inout0,$rndkey1)";
  356. eval"&aes${p} ($inout1,$rndkey1)";
  357. eval"&aes${p} ($inout2,$rndkey1)";
  358. eval"&aes${p} ($inout3,$rndkey1)";
  359. eval"&aes${p} ($inout4,$rndkey1)";
  360. eval"&aes${p} ($inout5,$rndkey1)";
  361. eval"&aes${p}last ($inout0,$rndkey0)";
  362. eval"&aes${p}last ($inout1,$rndkey0)";
  363. eval"&aes${p}last ($inout2,$rndkey0)";
  364. eval"&aes${p}last ($inout3,$rndkey0)";
  365. eval"&aes${p}last ($inout4,$rndkey0)";
  366. eval"&aes${p}last ($inout5,$rndkey0)";
  367. &ret();
  368. &function_end_B("_aesni_${p}rypt6");
  369. }
  370. &aesni_generate2("enc") if ($PREFIX eq "aesni");
  371. &aesni_generate2("dec");
  372. &aesni_generate3("enc") if ($PREFIX eq "aesni");
  373. &aesni_generate3("dec");
  374. &aesni_generate4("enc") if ($PREFIX eq "aesni");
  375. &aesni_generate4("dec");
  376. &aesni_generate6("enc") if ($PREFIX eq "aesni");
  377. &aesni_generate6("dec");
  378. if ($PREFIX eq "aesni") {
  379. ######################################################################
  380. # void aesni_ecb_encrypt (const void *in, void *out,
  381. # size_t length, const AES_KEY *key,
  382. # int enc);
  383. &function_begin("aesni_ecb_encrypt");
  384. &mov ($inp,&wparam(0));
  385. &mov ($out,&wparam(1));
  386. &mov ($len,&wparam(2));
  387. &mov ($key,&wparam(3));
  388. &mov ($rounds_,&wparam(4));
  389. &and ($len,-16);
  390. &jz (&label("ecb_ret"));
  391. &mov ($rounds,&DWP(240,$key));
  392. &test ($rounds_,$rounds_);
  393. &jz (&label("ecb_decrypt"));
  394. &mov ($key_,$key); # backup $key
  395. &mov ($rounds_,$rounds); # backup $rounds
  396. &cmp ($len,0x60);
  397. &jb (&label("ecb_enc_tail"));
  398. &movdqu ($inout0,&QWP(0,$inp));
  399. &movdqu ($inout1,&QWP(0x10,$inp));
  400. &movdqu ($inout2,&QWP(0x20,$inp));
  401. &movdqu ($inout3,&QWP(0x30,$inp));
  402. &movdqu ($inout4,&QWP(0x40,$inp));
  403. &movdqu ($inout5,&QWP(0x50,$inp));
  404. &lea ($inp,&DWP(0x60,$inp));
  405. &sub ($len,0x60);
  406. &jmp (&label("ecb_enc_loop6_enter"));
  407. &set_label("ecb_enc_loop6",16);
  408. &movups (&QWP(0,$out),$inout0);
  409. &movdqu ($inout0,&QWP(0,$inp));
  410. &movups (&QWP(0x10,$out),$inout1);
  411. &movdqu ($inout1,&QWP(0x10,$inp));
  412. &movups (&QWP(0x20,$out),$inout2);
  413. &movdqu ($inout2,&QWP(0x20,$inp));
  414. &movups (&QWP(0x30,$out),$inout3);
  415. &movdqu ($inout3,&QWP(0x30,$inp));
  416. &movups (&QWP(0x40,$out),$inout4);
  417. &movdqu ($inout4,&QWP(0x40,$inp));
  418. &movups (&QWP(0x50,$out),$inout5);
  419. &lea ($out,&DWP(0x60,$out));
  420. &movdqu ($inout5,&QWP(0x50,$inp));
  421. &lea ($inp,&DWP(0x60,$inp));
  422. &set_label("ecb_enc_loop6_enter");
  423. &call ("_aesni_encrypt6");
  424. &mov ($key,$key_); # restore $key
  425. &mov ($rounds,$rounds_); # restore $rounds
  426. &sub ($len,0x60);
  427. &jnc (&label("ecb_enc_loop6"));
  428. &movups (&QWP(0,$out),$inout0);
  429. &movups (&QWP(0x10,$out),$inout1);
  430. &movups (&QWP(0x20,$out),$inout2);
  431. &movups (&QWP(0x30,$out),$inout3);
  432. &movups (&QWP(0x40,$out),$inout4);
  433. &movups (&QWP(0x50,$out),$inout5);
  434. &lea ($out,&DWP(0x60,$out));
  435. &add ($len,0x60);
  436. &jz (&label("ecb_ret"));
  437. &set_label("ecb_enc_tail");
  438. &movups ($inout0,&QWP(0,$inp));
  439. &cmp ($len,0x20);
  440. &jb (&label("ecb_enc_one"));
  441. &movups ($inout1,&QWP(0x10,$inp));
  442. &je (&label("ecb_enc_two"));
  443. &movups ($inout2,&QWP(0x20,$inp));
  444. &cmp ($len,0x40);
  445. &jb (&label("ecb_enc_three"));
  446. &movups ($inout3,&QWP(0x30,$inp));
  447. &je (&label("ecb_enc_four"));
  448. &movups ($inout4,&QWP(0x40,$inp));
  449. &xorps ($inout5,$inout5);
  450. &call ("_aesni_encrypt6");
  451. &movups (&QWP(0,$out),$inout0);
  452. &movups (&QWP(0x10,$out),$inout1);
  453. &movups (&QWP(0x20,$out),$inout2);
  454. &movups (&QWP(0x30,$out),$inout3);
  455. &movups (&QWP(0x40,$out),$inout4);
  456. jmp (&label("ecb_ret"));
  457. &set_label("ecb_enc_one",16);
  458. if ($inline)
  459. { &aesni_inline_generate1("enc"); }
  460. else
  461. { &call ("_aesni_encrypt1"); }
  462. &movups (&QWP(0,$out),$inout0);
  463. &jmp (&label("ecb_ret"));
  464. &set_label("ecb_enc_two",16);
  465. &call ("_aesni_encrypt2");
  466. &movups (&QWP(0,$out),$inout0);
  467. &movups (&QWP(0x10,$out),$inout1);
  468. &jmp (&label("ecb_ret"));
  469. &set_label("ecb_enc_three",16);
  470. &call ("_aesni_encrypt3");
  471. &movups (&QWP(0,$out),$inout0);
  472. &movups (&QWP(0x10,$out),$inout1);
  473. &movups (&QWP(0x20,$out),$inout2);
  474. &jmp (&label("ecb_ret"));
  475. &set_label("ecb_enc_four",16);
  476. &call ("_aesni_encrypt4");
  477. &movups (&QWP(0,$out),$inout0);
  478. &movups (&QWP(0x10,$out),$inout1);
  479. &movups (&QWP(0x20,$out),$inout2);
  480. &movups (&QWP(0x30,$out),$inout3);
  481. &jmp (&label("ecb_ret"));
  482. ######################################################################
  483. &set_label("ecb_decrypt",16);
  484. &mov ($key_,$key); # backup $key
  485. &mov ($rounds_,$rounds); # backup $rounds
  486. &cmp ($len,0x60);
  487. &jb (&label("ecb_dec_tail"));
  488. &movdqu ($inout0,&QWP(0,$inp));
  489. &movdqu ($inout1,&QWP(0x10,$inp));
  490. &movdqu ($inout2,&QWP(0x20,$inp));
  491. &movdqu ($inout3,&QWP(0x30,$inp));
  492. &movdqu ($inout4,&QWP(0x40,$inp));
  493. &movdqu ($inout5,&QWP(0x50,$inp));
  494. &lea ($inp,&DWP(0x60,$inp));
  495. &sub ($len,0x60);
  496. &jmp (&label("ecb_dec_loop6_enter"));
  497. &set_label("ecb_dec_loop6",16);
  498. &movups (&QWP(0,$out),$inout0);
  499. &movdqu ($inout0,&QWP(0,$inp));
  500. &movups (&QWP(0x10,$out),$inout1);
  501. &movdqu ($inout1,&QWP(0x10,$inp));
  502. &movups (&QWP(0x20,$out),$inout2);
  503. &movdqu ($inout2,&QWP(0x20,$inp));
  504. &movups (&QWP(0x30,$out),$inout3);
  505. &movdqu ($inout3,&QWP(0x30,$inp));
  506. &movups (&QWP(0x40,$out),$inout4);
  507. &movdqu ($inout4,&QWP(0x40,$inp));
  508. &movups (&QWP(0x50,$out),$inout5);
  509. &lea ($out,&DWP(0x60,$out));
  510. &movdqu ($inout5,&QWP(0x50,$inp));
  511. &lea ($inp,&DWP(0x60,$inp));
  512. &set_label("ecb_dec_loop6_enter");
  513. &call ("_aesni_decrypt6");
  514. &mov ($key,$key_); # restore $key
  515. &mov ($rounds,$rounds_); # restore $rounds
  516. &sub ($len,0x60);
  517. &jnc (&label("ecb_dec_loop6"));
  518. &movups (&QWP(0,$out),$inout0);
  519. &movups (&QWP(0x10,$out),$inout1);
  520. &movups (&QWP(0x20,$out),$inout2);
  521. &movups (&QWP(0x30,$out),$inout3);
  522. &movups (&QWP(0x40,$out),$inout4);
  523. &movups (&QWP(0x50,$out),$inout5);
  524. &lea ($out,&DWP(0x60,$out));
  525. &add ($len,0x60);
  526. &jz (&label("ecb_ret"));
  527. &set_label("ecb_dec_tail");
  528. &movups ($inout0,&QWP(0,$inp));
  529. &cmp ($len,0x20);
  530. &jb (&label("ecb_dec_one"));
  531. &movups ($inout1,&QWP(0x10,$inp));
  532. &je (&label("ecb_dec_two"));
  533. &movups ($inout2,&QWP(0x20,$inp));
  534. &cmp ($len,0x40);
  535. &jb (&label("ecb_dec_three"));
  536. &movups ($inout3,&QWP(0x30,$inp));
  537. &je (&label("ecb_dec_four"));
  538. &movups ($inout4,&QWP(0x40,$inp));
  539. &xorps ($inout5,$inout5);
  540. &call ("_aesni_decrypt6");
  541. &movups (&QWP(0,$out),$inout0);
  542. &movups (&QWP(0x10,$out),$inout1);
  543. &movups (&QWP(0x20,$out),$inout2);
  544. &movups (&QWP(0x30,$out),$inout3);
  545. &movups (&QWP(0x40,$out),$inout4);
  546. &jmp (&label("ecb_ret"));
  547. &set_label("ecb_dec_one",16);
  548. if ($inline)
  549. { &aesni_inline_generate1("dec"); }
  550. else
  551. { &call ("_aesni_decrypt1"); }
  552. &movups (&QWP(0,$out),$inout0);
  553. &jmp (&label("ecb_ret"));
  554. &set_label("ecb_dec_two",16);
  555. &call ("_aesni_decrypt2");
  556. &movups (&QWP(0,$out),$inout0);
  557. &movups (&QWP(0x10,$out),$inout1);
  558. &jmp (&label("ecb_ret"));
  559. &set_label("ecb_dec_three",16);
  560. &call ("_aesni_decrypt3");
  561. &movups (&QWP(0,$out),$inout0);
  562. &movups (&QWP(0x10,$out),$inout1);
  563. &movups (&QWP(0x20,$out),$inout2);
  564. &jmp (&label("ecb_ret"));
  565. &set_label("ecb_dec_four",16);
  566. &call ("_aesni_decrypt4");
  567. &movups (&QWP(0,$out),$inout0);
  568. &movups (&QWP(0x10,$out),$inout1);
  569. &movups (&QWP(0x20,$out),$inout2);
  570. &movups (&QWP(0x30,$out),$inout3);
  571. &set_label("ecb_ret");
  572. &pxor ("xmm0","xmm0"); # clear register bank
  573. &pxor ("xmm1","xmm1");
  574. &pxor ("xmm2","xmm2");
  575. &pxor ("xmm3","xmm3");
  576. &pxor ("xmm4","xmm4");
  577. &pxor ("xmm5","xmm5");
  578. &pxor ("xmm6","xmm6");
  579. &pxor ("xmm7","xmm7");
  580. &function_end("aesni_ecb_encrypt");
  581. ######################################################################
  582. # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
  583. # size_t blocks, const AES_KEY *key,
  584. # const char *ivec,char *cmac);
  585. #
  586. # Handles only complete blocks, operates on 64-bit counter and
  587. # does not update *ivec! Nor does it finalize CMAC value
  588. # (see engine/eng_aesni.c for details)
  589. #
  590. { my $cmac=$inout1;
  591. &function_begin("aesni_ccm64_encrypt_blocks");
  592. &mov ($inp,&wparam(0));
  593. &mov ($out,&wparam(1));
  594. &mov ($len,&wparam(2));
  595. &mov ($key,&wparam(3));
  596. &mov ($rounds_,&wparam(4));
  597. &mov ($rounds,&wparam(5));
  598. &mov ($key_,"esp");
  599. &sub ("esp",60);
  600. &and ("esp",-16); # align stack
  601. &mov (&DWP(48,"esp"),$key_);
  602. &movdqu ($ivec,&QWP(0,$rounds_)); # load ivec
  603. &movdqu ($cmac,&QWP(0,$rounds)); # load cmac
  604. &mov ($rounds,&DWP(240,$key));
  605. # compose byte-swap control mask for pshufb on stack
  606. &mov (&DWP(0,"esp"),0x0c0d0e0f);
  607. &mov (&DWP(4,"esp"),0x08090a0b);
  608. &mov (&DWP(8,"esp"),0x04050607);
  609. &mov (&DWP(12,"esp"),0x00010203);
  610. # compose counter increment vector on stack
  611. &mov ($rounds_,1);
  612. &xor ($key_,$key_);
  613. &mov (&DWP(16,"esp"),$rounds_);
  614. &mov (&DWP(20,"esp"),$key_);
  615. &mov (&DWP(24,"esp"),$key_);
  616. &mov (&DWP(28,"esp"),$key_);
  617. &shl ($rounds,4);
  618. &mov ($rounds_,16);
  619. &lea ($key_,&DWP(0,$key));
  620. &movdqa ($inout3,&QWP(0,"esp"));
  621. &movdqa ($inout0,$ivec);
  622. &lea ($key,&DWP(32,$key,$rounds));
  623. &sub ($rounds_,$rounds);
  624. &pshufb ($ivec,$inout3);
  625. &set_label("ccm64_enc_outer");
  626. &$movekey ($rndkey0,&QWP(0,$key_));
  627. &mov ($rounds,$rounds_);
  628. &movups ($in0,&QWP(0,$inp));
  629. &xorps ($inout0,$rndkey0);
  630. &$movekey ($rndkey1,&QWP(16,$key_));
  631. &xorps ($rndkey0,$in0);
  632. &xorps ($cmac,$rndkey0); # cmac^=inp
  633. &$movekey ($rndkey0,&QWP(32,$key_));
  634. &set_label("ccm64_enc2_loop");
  635. &aesenc ($inout0,$rndkey1);
  636. &aesenc ($cmac,$rndkey1);
  637. &$movekey ($rndkey1,&QWP(0,$key,$rounds));
  638. &add ($rounds,32);
  639. &aesenc ($inout0,$rndkey0);
  640. &aesenc ($cmac,$rndkey0);
  641. &$movekey ($rndkey0,&QWP(-16,$key,$rounds));
  642. &jnz (&label("ccm64_enc2_loop"));
  643. &aesenc ($inout0,$rndkey1);
  644. &aesenc ($cmac,$rndkey1);
  645. &paddq ($ivec,&QWP(16,"esp"));
  646. &dec ($len);
  647. &aesenclast ($inout0,$rndkey0);
  648. &aesenclast ($cmac,$rndkey0);
  649. &lea ($inp,&DWP(16,$inp));
  650. &xorps ($in0,$inout0); # inp^=E(ivec)
  651. &movdqa ($inout0,$ivec);
  652. &movups (&QWP(0,$out),$in0); # save output
  653. &pshufb ($inout0,$inout3);
  654. &lea ($out,&DWP(16,$out));
  655. &jnz (&label("ccm64_enc_outer"));
  656. &mov ("esp",&DWP(48,"esp"));
  657. &mov ($out,&wparam(5));
  658. &movups (&QWP(0,$out),$cmac);
  659. &pxor ("xmm0","xmm0"); # clear register bank
  660. &pxor ("xmm1","xmm1");
  661. &pxor ("xmm2","xmm2");
  662. &pxor ("xmm3","xmm3");
  663. &pxor ("xmm4","xmm4");
  664. &pxor ("xmm5","xmm5");
  665. &pxor ("xmm6","xmm6");
  666. &pxor ("xmm7","xmm7");
  667. &function_end("aesni_ccm64_encrypt_blocks");
  668. &function_begin("aesni_ccm64_decrypt_blocks");
  669. &mov ($inp,&wparam(0));
  670. &mov ($out,&wparam(1));
  671. &mov ($len,&wparam(2));
  672. &mov ($key,&wparam(3));
  673. &mov ($rounds_,&wparam(4));
  674. &mov ($rounds,&wparam(5));
  675. &mov ($key_,"esp");
  676. &sub ("esp",60);
  677. &and ("esp",-16); # align stack
  678. &mov (&DWP(48,"esp"),$key_);
  679. &movdqu ($ivec,&QWP(0,$rounds_)); # load ivec
  680. &movdqu ($cmac,&QWP(0,$rounds)); # load cmac
  681. &mov ($rounds,&DWP(240,$key));
  682. # compose byte-swap control mask for pshufb on stack
  683. &mov (&DWP(0,"esp"),0x0c0d0e0f);
  684. &mov (&DWP(4,"esp"),0x08090a0b);
  685. &mov (&DWP(8,"esp"),0x04050607);
  686. &mov (&DWP(12,"esp"),0x00010203);
  687. # compose counter increment vector on stack
  688. &mov ($rounds_,1);
  689. &xor ($key_,$key_);
  690. &mov (&DWP(16,"esp"),$rounds_);
  691. &mov (&DWP(20,"esp"),$key_);
  692. &mov (&DWP(24,"esp"),$key_);
  693. &mov (&DWP(28,"esp"),$key_);
  694. &movdqa ($inout3,&QWP(0,"esp")); # bswap mask
  695. &movdqa ($inout0,$ivec);
  696. &mov ($key_,$key);
  697. &mov ($rounds_,$rounds);
  698. &pshufb ($ivec,$inout3);
  699. if ($inline)
  700. { &aesni_inline_generate1("enc"); }
  701. else
  702. { &call ("_aesni_encrypt1"); }
  703. &shl ($rounds_,4);
  704. &mov ($rounds,16);
  705. &movups ($in0,&QWP(0,$inp)); # load inp
  706. &paddq ($ivec,&QWP(16,"esp"));
  707. &lea ($inp,&QWP(16,$inp));
  708. &sub ($rounds,$rounds_);
  709. &lea ($key,&DWP(32,$key_,$rounds_));
  710. &mov ($rounds_,$rounds);
  711. &jmp (&label("ccm64_dec_outer"));
  712. &set_label("ccm64_dec_outer",16);
  713. &xorps ($in0,$inout0); # inp ^= E(ivec)
  714. &movdqa ($inout0,$ivec);
  715. &movups (&QWP(0,$out),$in0); # save output
  716. &lea ($out,&DWP(16,$out));
  717. &pshufb ($inout0,$inout3);
  718. &sub ($len,1);
  719. &jz (&label("ccm64_dec_break"));
  720. &$movekey ($rndkey0,&QWP(0,$key_));
  721. &mov ($rounds,$rounds_);
  722. &$movekey ($rndkey1,&QWP(16,$key_));
  723. &xorps ($in0,$rndkey0);
  724. &xorps ($inout0,$rndkey0);
  725. &xorps ($cmac,$in0); # cmac^=out
  726. &$movekey ($rndkey0,&QWP(32,$key_));
  727. &set_label("ccm64_dec2_loop");
  728. &aesenc ($inout0,$rndkey1);
  729. &aesenc ($cmac,$rndkey1);
  730. &$movekey ($rndkey1,&QWP(0,$key,$rounds));
  731. &add ($rounds,32);
  732. &aesenc ($inout0,$rndkey0);
  733. &aesenc ($cmac,$rndkey0);
  734. &$movekey ($rndkey0,&QWP(-16,$key,$rounds));
  735. &jnz (&label("ccm64_dec2_loop"));
  736. &movups ($in0,&QWP(0,$inp)); # load inp
  737. &paddq ($ivec,&QWP(16,"esp"));
  738. &aesenc ($inout0,$rndkey1);
  739. &aesenc ($cmac,$rndkey1);
  740. &aesenclast ($inout0,$rndkey0);
  741. &aesenclast ($cmac,$rndkey0);
  742. &lea ($inp,&QWP(16,$inp));
  743. &jmp (&label("ccm64_dec_outer"));
  744. &set_label("ccm64_dec_break",16);
  745. &mov ($rounds,&DWP(240,$key_));
  746. &mov ($key,$key_);
  747. if ($inline)
  748. { &aesni_inline_generate1("enc",$cmac,$in0); }
  749. else
  750. { &call ("_aesni_encrypt1",$cmac); }
  751. &mov ("esp",&DWP(48,"esp"));
  752. &mov ($out,&wparam(5));
  753. &movups (&QWP(0,$out),$cmac);
  754. &pxor ("xmm0","xmm0"); # clear register bank
  755. &pxor ("xmm1","xmm1");
  756. &pxor ("xmm2","xmm2");
  757. &pxor ("xmm3","xmm3");
  758. &pxor ("xmm4","xmm4");
  759. &pxor ("xmm5","xmm5");
  760. &pxor ("xmm6","xmm6");
  761. &pxor ("xmm7","xmm7");
  762. &function_end("aesni_ccm64_decrypt_blocks");
  763. }
  764. ######################################################################
  765. # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
  766. # size_t blocks, const AES_KEY *key,
  767. # const char *ivec);
  768. #
  769. # Handles only complete blocks, operates on 32-bit counter and
  770. # does not update *ivec! (see crypto/modes/ctr128.c for details)
  771. #
  772. # stack layout:
  773. # 0 pshufb mask
  774. # 16 vector addend: 0,6,6,6
  775. # 32 counter-less ivec
  776. # 48 1st triplet of counter vector
  777. # 64 2nd triplet of counter vector
  778. # 80 saved %esp
  779. &function_begin("aesni_ctr32_encrypt_blocks");
  780. &mov ($inp,&wparam(0));
  781. &mov ($out,&wparam(1));
  782. &mov ($len,&wparam(2));
  783. &mov ($key,&wparam(3));
  784. &mov ($rounds_,&wparam(4));
  785. &mov ($key_,"esp");
  786. &sub ("esp",88);
  787. &and ("esp",-16); # align stack
  788. &mov (&DWP(80,"esp"),$key_);
  789. &cmp ($len,1);
  790. &je (&label("ctr32_one_shortcut"));
  791. &movdqu ($inout5,&QWP(0,$rounds_)); # load ivec
  792. # compose byte-swap control mask for pshufb on stack
  793. &mov (&DWP(0,"esp"),0x0c0d0e0f);
  794. &mov (&DWP(4,"esp"),0x08090a0b);
  795. &mov (&DWP(8,"esp"),0x04050607);
  796. &mov (&DWP(12,"esp"),0x00010203);
  797. # compose counter increment vector on stack
  798. &mov ($rounds,6);
  799. &xor ($key_,$key_);
  800. &mov (&DWP(16,"esp"),$rounds);
  801. &mov (&DWP(20,"esp"),$rounds);
  802. &mov (&DWP(24,"esp"),$rounds);
  803. &mov (&DWP(28,"esp"),$key_);
  804. &pextrd ($rounds_,$inout5,3); # pull 32-bit counter
  805. &pinsrd ($inout5,$key_,3); # wipe 32-bit counter
  806. &mov ($rounds,&DWP(240,$key)); # key->rounds
  807. # compose 2 vectors of 3x32-bit counters
  808. &bswap ($rounds_);
  809. &pxor ($rndkey0,$rndkey0);
  810. &pxor ($rndkey1,$rndkey1);
  811. &movdqa ($inout0,&QWP(0,"esp")); # load byte-swap mask
  812. &pinsrd ($rndkey0,$rounds_,0);
  813. &lea ($key_,&DWP(3,$rounds_));
  814. &pinsrd ($rndkey1,$key_,0);
  815. &inc ($rounds_);
  816. &pinsrd ($rndkey0,$rounds_,1);
  817. &inc ($key_);
  818. &pinsrd ($rndkey1,$key_,1);
  819. &inc ($rounds_);
  820. &pinsrd ($rndkey0,$rounds_,2);
  821. &inc ($key_);
  822. &pinsrd ($rndkey1,$key_,2);
  823. &movdqa (&QWP(48,"esp"),$rndkey0); # save 1st triplet
  824. &pshufb ($rndkey0,$inout0); # byte swap
  825. &movdqu ($inout4,&QWP(0,$key)); # key[0]
  826. &movdqa (&QWP(64,"esp"),$rndkey1); # save 2nd triplet
  827. &pshufb ($rndkey1,$inout0); # byte swap
  828. &pshufd ($inout0,$rndkey0,3<<6); # place counter to upper dword
  829. &pshufd ($inout1,$rndkey0,2<<6);
  830. &cmp ($len,6);
  831. &jb (&label("ctr32_tail"));
  832. &pxor ($inout5,$inout4); # counter-less ivec^key[0]
  833. &shl ($rounds,4);
  834. &mov ($rounds_,16);
  835. &movdqa (&QWP(32,"esp"),$inout5); # save counter-less ivec^key[0]
  836. &mov ($key_,$key); # backup $key
  837. &sub ($rounds_,$rounds); # backup twisted $rounds
  838. &lea ($key,&DWP(32,$key,$rounds));
  839. &sub ($len,6);
  840. &jmp (&label("ctr32_loop6"));
  841. &set_label("ctr32_loop6",16);
  842. # inlining _aesni_encrypt6's prologue gives ~6% improvement...
  843. &pshufd ($inout2,$rndkey0,1<<6);
  844. &movdqa ($rndkey0,&QWP(32,"esp")); # pull counter-less ivec
  845. &pshufd ($inout3,$rndkey1,3<<6);
  846. &pxor ($inout0,$rndkey0); # merge counter-less ivec
  847. &pshufd ($inout4,$rndkey1,2<<6);
  848. &pxor ($inout1,$rndkey0);
  849. &pshufd ($inout5,$rndkey1,1<<6);
  850. &$movekey ($rndkey1,&QWP(16,$key_));
  851. &pxor ($inout2,$rndkey0);
  852. &pxor ($inout3,$rndkey0);
  853. &aesenc ($inout0,$rndkey1);
  854. &pxor ($inout4,$rndkey0);
  855. &pxor ($inout5,$rndkey0);
  856. &aesenc ($inout1,$rndkey1);
  857. &$movekey ($rndkey0,&QWP(32,$key_));
  858. &mov ($rounds,$rounds_);
  859. &aesenc ($inout2,$rndkey1);
  860. &aesenc ($inout3,$rndkey1);
  861. &aesenc ($inout4,$rndkey1);
  862. &aesenc ($inout5,$rndkey1);
  863. &call (&label("_aesni_encrypt6_enter"));
  864. &movups ($rndkey1,&QWP(0,$inp));
  865. &movups ($rndkey0,&QWP(0x10,$inp));
  866. &xorps ($inout0,$rndkey1);
  867. &movups ($rndkey1,&QWP(0x20,$inp));
  868. &xorps ($inout1,$rndkey0);
  869. &movups (&QWP(0,$out),$inout0);
  870. &movdqa ($rndkey0,&QWP(16,"esp")); # load increment
  871. &xorps ($inout2,$rndkey1);
  872. &movdqa ($rndkey1,&QWP(64,"esp")); # load 2nd triplet
  873. &movups (&QWP(0x10,$out),$inout1);
  874. &movups (&QWP(0x20,$out),$inout2);
  875. &paddd ($rndkey1,$rndkey0); # 2nd triplet increment
  876. &paddd ($rndkey0,&QWP(48,"esp")); # 1st triplet increment
  877. &movdqa ($inout0,&QWP(0,"esp")); # load byte swap mask
  878. &movups ($inout1,&QWP(0x30,$inp));
  879. &movups ($inout2,&QWP(0x40,$inp));
  880. &xorps ($inout3,$inout1);
  881. &movups ($inout1,&QWP(0x50,$inp));
  882. &lea ($inp,&DWP(0x60,$inp));
  883. &movdqa (&QWP(48,"esp"),$rndkey0); # save 1st triplet
  884. &pshufb ($rndkey0,$inout0); # byte swap
  885. &xorps ($inout4,$inout2);
  886. &movups (&QWP(0x30,$out),$inout3);
  887. &xorps ($inout5,$inout1);
  888. &movdqa (&QWP(64,"esp"),$rndkey1); # save 2nd triplet
  889. &pshufb ($rndkey1,$inout0); # byte swap
  890. &movups (&QWP(0x40,$out),$inout4);
  891. &pshufd ($inout0,$rndkey0,3<<6);
  892. &movups (&QWP(0x50,$out),$inout5);
  893. &lea ($out,&DWP(0x60,$out));
  894. &pshufd ($inout1,$rndkey0,2<<6);
  895. &sub ($len,6);
  896. &jnc (&label("ctr32_loop6"));
  897. &add ($len,6);
  898. &jz (&label("ctr32_ret"));
  899. &movdqu ($inout5,&QWP(0,$key_));
  900. &mov ($key,$key_);
  901. &pxor ($inout5,&QWP(32,"esp")); # restore count-less ivec
  902. &mov ($rounds,&DWP(240,$key_)); # restore $rounds
  903. &set_label("ctr32_tail");
  904. &por ($inout0,$inout5);
  905. &cmp ($len,2);
  906. &jb (&label("ctr32_one"));
  907. &pshufd ($inout2,$rndkey0,1<<6);
  908. &por ($inout1,$inout5);
  909. &je (&label("ctr32_two"));
  910. &pshufd ($inout3,$rndkey1,3<<6);
  911. &por ($inout2,$inout5);
  912. &cmp ($len,4);
  913. &jb (&label("ctr32_three"));
  914. &pshufd ($inout4,$rndkey1,2<<6);
  915. &por ($inout3,$inout5);
  916. &je (&label("ctr32_four"));
  917. &por ($inout4,$inout5);
  918. &call ("_aesni_encrypt6");
  919. &movups ($rndkey1,&QWP(0,$inp));
  920. &movups ($rndkey0,&QWP(0x10,$inp));
  921. &xorps ($inout0,$rndkey1);
  922. &movups ($rndkey1,&QWP(0x20,$inp));
  923. &xorps ($inout1,$rndkey0);
  924. &movups ($rndkey0,&QWP(0x30,$inp));
  925. &xorps ($inout2,$rndkey1);
  926. &movups ($rndkey1,&QWP(0x40,$inp));
  927. &xorps ($inout3,$rndkey0);
  928. &movups (&QWP(0,$out),$inout0);
  929. &xorps ($inout4,$rndkey1);
  930. &movups (&QWP(0x10,$out),$inout1);
  931. &movups (&QWP(0x20,$out),$inout2);
  932. &movups (&QWP(0x30,$out),$inout3);
  933. &movups (&QWP(0x40,$out),$inout4);
  934. &jmp (&label("ctr32_ret"));
  935. &set_label("ctr32_one_shortcut",16);
  936. &movups ($inout0,&QWP(0,$rounds_)); # load ivec
  937. &mov ($rounds,&DWP(240,$key));
  938. &set_label("ctr32_one");
  939. if ($inline)
  940. { &aesni_inline_generate1("enc"); }
  941. else
  942. { &call ("_aesni_encrypt1"); }
  943. &movups ($in0,&QWP(0,$inp));
  944. &xorps ($in0,$inout0);
  945. &movups (&QWP(0,$out),$in0);
  946. &jmp (&label("ctr32_ret"));
  947. &set_label("ctr32_two",16);
  948. &call ("_aesni_encrypt2");
  949. &movups ($inout3,&QWP(0,$inp));
  950. &movups ($inout4,&QWP(0x10,$inp));
  951. &xorps ($inout0,$inout3);
  952. &xorps ($inout1,$inout4);
  953. &movups (&QWP(0,$out),$inout0);
  954. &movups (&QWP(0x10,$out),$inout1);
  955. &jmp (&label("ctr32_ret"));
  956. &set_label("ctr32_three",16);
  957. &call ("_aesni_encrypt3");
  958. &movups ($inout3,&QWP(0,$inp));
  959. &movups ($inout4,&QWP(0x10,$inp));
  960. &xorps ($inout0,$inout3);
  961. &movups ($inout5,&QWP(0x20,$inp));
  962. &xorps ($inout1,$inout4);
  963. &movups (&QWP(0,$out),$inout0);
  964. &xorps ($inout2,$inout5);
  965. &movups (&QWP(0x10,$out),$inout1);
  966. &movups (&QWP(0x20,$out),$inout2);
  967. &jmp (&label("ctr32_ret"));
  968. &set_label("ctr32_four",16);
  969. &call ("_aesni_encrypt4");
  970. &movups ($inout4,&QWP(0,$inp));
  971. &movups ($inout5,&QWP(0x10,$inp));
  972. &movups ($rndkey1,&QWP(0x20,$inp));
  973. &xorps ($inout0,$inout4);
  974. &movups ($rndkey0,&QWP(0x30,$inp));
  975. &xorps ($inout1,$inout5);
  976. &movups (&QWP(0,$out),$inout0);
  977. &xorps ($inout2,$rndkey1);
  978. &movups (&QWP(0x10,$out),$inout1);
  979. &xorps ($inout3,$rndkey0);
  980. &movups (&QWP(0x20,$out),$inout2);
  981. &movups (&QWP(0x30,$out),$inout3);
  982. &set_label("ctr32_ret");
  983. &pxor ("xmm0","xmm0"); # clear register bank
  984. &pxor ("xmm1","xmm1");
  985. &pxor ("xmm2","xmm2");
  986. &pxor ("xmm3","xmm3");
  987. &pxor ("xmm4","xmm4");
  988. &movdqa (&QWP(32,"esp"),"xmm0"); # clear stack
  989. &pxor ("xmm5","xmm5");
  990. &movdqa (&QWP(48,"esp"),"xmm0");
  991. &pxor ("xmm6","xmm6");
  992. &movdqa (&QWP(64,"esp"),"xmm0");
  993. &pxor ("xmm7","xmm7");
  994. &mov ("esp",&DWP(80,"esp"));
  995. &function_end("aesni_ctr32_encrypt_blocks");
  996. ######################################################################
  997. # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  998. # const AES_KEY *key1, const AES_KEY *key2
  999. # const unsigned char iv[16]);
  1000. #
  1001. { my ($tweak,$twtmp,$twres,$twmask)=($rndkey1,$rndkey0,$inout0,$inout1);
  1002. &function_begin("aesni_xts_encrypt");
  1003. &mov ($key,&wparam(4)); # key2
  1004. &mov ($inp,&wparam(5)); # clear-text tweak
  1005. &mov ($rounds,&DWP(240,$key)); # key2->rounds
  1006. &movups ($inout0,&QWP(0,$inp));
  1007. if ($inline)
  1008. { &aesni_inline_generate1("enc"); }
  1009. else
  1010. { &call ("_aesni_encrypt1"); }
  1011. &mov ($inp,&wparam(0));
  1012. &mov ($out,&wparam(1));
  1013. &mov ($len,&wparam(2));
  1014. &mov ($key,&wparam(3)); # key1
  1015. &mov ($key_,"esp");
  1016. &sub ("esp",16*7+8);
  1017. &mov ($rounds,&DWP(240,$key)); # key1->rounds
  1018. &and ("esp",-16); # align stack
  1019. &mov (&DWP(16*6+0,"esp"),0x87); # compose the magic constant
  1020. &mov (&DWP(16*6+4,"esp"),0);
  1021. &mov (&DWP(16*6+8,"esp"),1);
  1022. &mov (&DWP(16*6+12,"esp"),0);
  1023. &mov (&DWP(16*7+0,"esp"),$len); # save original $len
  1024. &mov (&DWP(16*7+4,"esp"),$key_); # save original %esp
  1025. &movdqa ($tweak,$inout0);
  1026. &pxor ($twtmp,$twtmp);
  1027. &movdqa ($twmask,&QWP(6*16,"esp")); # 0x0...010...87
  1028. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1029. &and ($len,-16);
  1030. &mov ($key_,$key); # backup $key
  1031. &mov ($rounds_,$rounds); # backup $rounds
  1032. &sub ($len,16*6);
  1033. &jc (&label("xts_enc_short"));
  1034. &shl ($rounds,4);
  1035. &mov ($rounds_,16);
  1036. &sub ($rounds_,$rounds);
  1037. &lea ($key,&DWP(32,$key,$rounds));
  1038. &jmp (&label("xts_enc_loop6"));
  1039. &set_label("xts_enc_loop6",16);
  1040. for ($i=0;$i<4;$i++) {
  1041. &pshufd ($twres,$twtmp,0x13);
  1042. &pxor ($twtmp,$twtmp);
  1043. &movdqa (&QWP(16*$i,"esp"),$tweak);
  1044. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1045. &pand ($twres,$twmask); # isolate carry and residue
  1046. &pcmpgtd ($twtmp,$tweak); # broadcast upper bits
  1047. &pxor ($tweak,$twres);
  1048. }
  1049. &pshufd ($inout5,$twtmp,0x13);
  1050. &movdqa (&QWP(16*$i++,"esp"),$tweak);
  1051. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1052. &$movekey ($rndkey0,&QWP(0,$key_));
  1053. &pand ($inout5,$twmask); # isolate carry and residue
  1054. &movups ($inout0,&QWP(0,$inp)); # load input
  1055. &pxor ($inout5,$tweak);
  1056. # inline _aesni_encrypt6 prologue and flip xor with tweak and key[0]
  1057. &mov ($rounds,$rounds_); # restore $rounds
  1058. &movdqu ($inout1,&QWP(16*1,$inp));
  1059. &xorps ($inout0,$rndkey0); # input^=rndkey[0]
  1060. &movdqu ($inout2,&QWP(16*2,$inp));
  1061. &pxor ($inout1,$rndkey0);
  1062. &movdqu ($inout3,&QWP(16*3,$inp));
  1063. &pxor ($inout2,$rndkey0);
  1064. &movdqu ($inout4,&QWP(16*4,$inp));
  1065. &pxor ($inout3,$rndkey0);
  1066. &movdqu ($rndkey1,&QWP(16*5,$inp));
  1067. &pxor ($inout4,$rndkey0);
  1068. &lea ($inp,&DWP(16*6,$inp));
  1069. &pxor ($inout0,&QWP(16*0,"esp")); # input^=tweak
  1070. &movdqa (&QWP(16*$i,"esp"),$inout5); # save last tweak
  1071. &pxor ($inout5,$rndkey1);
  1072. &$movekey ($rndkey1,&QWP(16,$key_));
  1073. &pxor ($inout1,&QWP(16*1,"esp"));
  1074. &pxor ($inout2,&QWP(16*2,"esp"));
  1075. &aesenc ($inout0,$rndkey1);
  1076. &pxor ($inout3,&QWP(16*3,"esp"));
  1077. &pxor ($inout4,&QWP(16*4,"esp"));
  1078. &aesenc ($inout1,$rndkey1);
  1079. &pxor ($inout5,$rndkey0);
  1080. &$movekey ($rndkey0,&QWP(32,$key_));
  1081. &aesenc ($inout2,$rndkey1);
  1082. &aesenc ($inout3,$rndkey1);
  1083. &aesenc ($inout4,$rndkey1);
  1084. &aesenc ($inout5,$rndkey1);
  1085. &call (&label("_aesni_encrypt6_enter"));
  1086. &movdqa ($tweak,&QWP(16*5,"esp")); # last tweak
  1087. &pxor ($twtmp,$twtmp);
  1088. &xorps ($inout0,&QWP(16*0,"esp")); # output^=tweak
  1089. &pcmpgtd ($twtmp,$tweak); # broadcast upper bits
  1090. &xorps ($inout1,&QWP(16*1,"esp"));
  1091. &movups (&QWP(16*0,$out),$inout0); # write output
  1092. &xorps ($inout2,&QWP(16*2,"esp"));
  1093. &movups (&QWP(16*1,$out),$inout1);
  1094. &xorps ($inout3,&QWP(16*3,"esp"));
  1095. &movups (&QWP(16*2,$out),$inout2);
  1096. &xorps ($inout4,&QWP(16*4,"esp"));
  1097. &movups (&QWP(16*3,$out),$inout3);
  1098. &xorps ($inout5,$tweak);
  1099. &movups (&QWP(16*4,$out),$inout4);
  1100. &pshufd ($twres,$twtmp,0x13);
  1101. &movups (&QWP(16*5,$out),$inout5);
  1102. &lea ($out,&DWP(16*6,$out));
  1103. &movdqa ($twmask,&QWP(16*6,"esp")); # 0x0...010...87
  1104. &pxor ($twtmp,$twtmp);
  1105. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1106. &pand ($twres,$twmask); # isolate carry and residue
  1107. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1108. &pxor ($tweak,$twres);
  1109. &sub ($len,16*6);
  1110. &jnc (&label("xts_enc_loop6"));
  1111. &mov ($rounds,&DWP(240,$key_)); # restore $rounds
  1112. &mov ($key,$key_); # restore $key
  1113. &mov ($rounds_,$rounds);
  1114. &set_label("xts_enc_short");
  1115. &add ($len,16*6);
  1116. &jz (&label("xts_enc_done6x"));
  1117. &movdqa ($inout3,$tweak); # put aside previous tweak
  1118. &cmp ($len,0x20);
  1119. &jb (&label("xts_enc_one"));
  1120. &pshufd ($twres,$twtmp,0x13);
  1121. &pxor ($twtmp,$twtmp);
  1122. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1123. &pand ($twres,$twmask); # isolate carry and residue
  1124. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1125. &pxor ($tweak,$twres);
  1126. &je (&label("xts_enc_two"));
  1127. &pshufd ($twres,$twtmp,0x13);
  1128. &pxor ($twtmp,$twtmp);
  1129. &movdqa ($inout4,$tweak); # put aside previous tweak
  1130. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1131. &pand ($twres,$twmask); # isolate carry and residue
  1132. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1133. &pxor ($tweak,$twres);
  1134. &cmp ($len,0x40);
  1135. &jb (&label("xts_enc_three"));
  1136. &pshufd ($twres,$twtmp,0x13);
  1137. &pxor ($twtmp,$twtmp);
  1138. &movdqa ($inout5,$tweak); # put aside previous tweak
  1139. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1140. &pand ($twres,$twmask); # isolate carry and residue
  1141. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1142. &pxor ($tweak,$twres);
  1143. &movdqa (&QWP(16*0,"esp"),$inout3);
  1144. &movdqa (&QWP(16*1,"esp"),$inout4);
  1145. &je (&label("xts_enc_four"));
  1146. &movdqa (&QWP(16*2,"esp"),$inout5);
  1147. &pshufd ($inout5,$twtmp,0x13);
  1148. &movdqa (&QWP(16*3,"esp"),$tweak);
  1149. &paddq ($tweak,$tweak); # &psllq($inout0,1);
  1150. &pand ($inout5,$twmask); # isolate carry and residue
  1151. &pxor ($inout5,$tweak);
  1152. &movdqu ($inout0,&QWP(16*0,$inp)); # load input
  1153. &movdqu ($inout1,&QWP(16*1,$inp));
  1154. &movdqu ($inout2,&QWP(16*2,$inp));
  1155. &pxor ($inout0,&QWP(16*0,"esp")); # input^=tweak
  1156. &movdqu ($inout3,&QWP(16*3,$inp));
  1157. &pxor ($inout1,&QWP(16*1,"esp"));
  1158. &movdqu ($inout4,&QWP(16*4,$inp));
  1159. &pxor ($inout2,&QWP(16*2,"esp"));
  1160. &lea ($inp,&DWP(16*5,$inp));
  1161. &pxor ($inout3,&QWP(16*3,"esp"));
  1162. &movdqa (&QWP(16*4,"esp"),$inout5); # save last tweak
  1163. &pxor ($inout4,$inout5);
  1164. &call ("_aesni_encrypt6");
  1165. &movaps ($tweak,&QWP(16*4,"esp")); # last tweak
  1166. &xorps ($inout0,&QWP(16*0,"esp")); # output^=tweak
  1167. &xorps ($inout1,&QWP(16*1,"esp"));
  1168. &xorps ($inout2,&QWP(16*2,"esp"));
  1169. &movups (&QWP(16*0,$out),$inout0); # write output
  1170. &xorps ($inout3,&QWP(16*3,"esp"));
  1171. &movups (&QWP(16*1,$out),$inout1);
  1172. &xorps ($inout4,$tweak);
  1173. &movups (&QWP(16*2,$out),$inout2);
  1174. &movups (&QWP(16*3,$out),$inout3);
  1175. &movups (&QWP(16*4,$out),$inout4);
  1176. &lea ($out,&DWP(16*5,$out));
  1177. &jmp (&label("xts_enc_done"));
  1178. &set_label("xts_enc_one",16);
  1179. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1180. &lea ($inp,&DWP(16*1,$inp));
  1181. &xorps ($inout0,$inout3); # input^=tweak
  1182. if ($inline)
  1183. { &aesni_inline_generate1("enc"); }
  1184. else
  1185. { &call ("_aesni_encrypt1"); }
  1186. &xorps ($inout0,$inout3); # output^=tweak
  1187. &movups (&QWP(16*0,$out),$inout0); # write output
  1188. &lea ($out,&DWP(16*1,$out));
  1189. &movdqa ($tweak,$inout3); # last tweak
  1190. &jmp (&label("xts_enc_done"));
  1191. &set_label("xts_enc_two",16);
  1192. &movaps ($inout4,$tweak); # put aside last tweak
  1193. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1194. &movups ($inout1,&QWP(16*1,$inp));
  1195. &lea ($inp,&DWP(16*2,$inp));
  1196. &xorps ($inout0,$inout3); # input^=tweak
  1197. &xorps ($inout1,$inout4);
  1198. &call ("_aesni_encrypt2");
  1199. &xorps ($inout0,$inout3); # output^=tweak
  1200. &xorps ($inout1,$inout4);
  1201. &movups (&QWP(16*0,$out),$inout0); # write output
  1202. &movups (&QWP(16*1,$out),$inout1);
  1203. &lea ($out,&DWP(16*2,$out));
  1204. &movdqa ($tweak,$inout4); # last tweak
  1205. &jmp (&label("xts_enc_done"));
  1206. &set_label("xts_enc_three",16);
  1207. &movaps ($inout5,$tweak); # put aside last tweak
  1208. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1209. &movups ($inout1,&QWP(16*1,$inp));
  1210. &movups ($inout2,&QWP(16*2,$inp));
  1211. &lea ($inp,&DWP(16*3,$inp));
  1212. &xorps ($inout0,$inout3); # input^=tweak
  1213. &xorps ($inout1,$inout4);
  1214. &xorps ($inout2,$inout5);
  1215. &call ("_aesni_encrypt3");
  1216. &xorps ($inout0,$inout3); # output^=tweak
  1217. &xorps ($inout1,$inout4);
  1218. &xorps ($inout2,$inout5);
  1219. &movups (&QWP(16*0,$out),$inout0); # write output
  1220. &movups (&QWP(16*1,$out),$inout1);
  1221. &movups (&QWP(16*2,$out),$inout2);
  1222. &lea ($out,&DWP(16*3,$out));
  1223. &movdqa ($tweak,$inout5); # last tweak
  1224. &jmp (&label("xts_enc_done"));
  1225. &set_label("xts_enc_four",16);
  1226. &movaps ($inout4,$tweak); # put aside last tweak
  1227. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1228. &movups ($inout1,&QWP(16*1,$inp));
  1229. &movups ($inout2,&QWP(16*2,$inp));
  1230. &xorps ($inout0,&QWP(16*0,"esp")); # input^=tweak
  1231. &movups ($inout3,&QWP(16*3,$inp));
  1232. &lea ($inp,&DWP(16*4,$inp));
  1233. &xorps ($inout1,&QWP(16*1,"esp"));
  1234. &xorps ($inout2,$inout5);
  1235. &xorps ($inout3,$inout4);
  1236. &call ("_aesni_encrypt4");
  1237. &xorps ($inout0,&QWP(16*0,"esp")); # output^=tweak
  1238. &xorps ($inout1,&QWP(16*1,"esp"));
  1239. &xorps ($inout2,$inout5);
  1240. &movups (&QWP(16*0,$out),$inout0); # write output
  1241. &xorps ($inout3,$inout4);
  1242. &movups (&QWP(16*1,$out),$inout1);
  1243. &movups (&QWP(16*2,$out),$inout2);
  1244. &movups (&QWP(16*3,$out),$inout3);
  1245. &lea ($out,&DWP(16*4,$out));
  1246. &movdqa ($tweak,$inout4); # last tweak
  1247. &jmp (&label("xts_enc_done"));
  1248. &set_label("xts_enc_done6x",16); # $tweak is pre-calculated
  1249. &mov ($len,&DWP(16*7+0,"esp")); # restore original $len
  1250. &and ($len,15);
  1251. &jz (&label("xts_enc_ret"));
  1252. &movdqa ($inout3,$tweak);
  1253. &mov (&DWP(16*7+0,"esp"),$len); # save $len%16
  1254. &jmp (&label("xts_enc_steal"));
  1255. &set_label("xts_enc_done",16);
  1256. &mov ($len,&DWP(16*7+0,"esp")); # restore original $len
  1257. &pxor ($twtmp,$twtmp);
  1258. &and ($len,15);
  1259. &jz (&label("xts_enc_ret"));
  1260. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1261. &mov (&DWP(16*7+0,"esp"),$len); # save $len%16
  1262. &pshufd ($inout3,$twtmp,0x13);
  1263. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1264. &pand ($inout3,&QWP(16*6,"esp")); # isolate carry and residue
  1265. &pxor ($inout3,$tweak);
  1266. &set_label("xts_enc_steal");
  1267. &movz ($rounds,&BP(0,$inp));
  1268. &movz ($key,&BP(-16,$out));
  1269. &lea ($inp,&DWP(1,$inp));
  1270. &mov (&BP(-16,$out),&LB($rounds));
  1271. &mov (&BP(0,$out),&LB($key));
  1272. &lea ($out,&DWP(1,$out));
  1273. &sub ($len,1);
  1274. &jnz (&label("xts_enc_steal"));
  1275. &sub ($out,&DWP(16*7+0,"esp")); # rewind $out
  1276. &mov ($key,$key_); # restore $key
  1277. &mov ($rounds,$rounds_); # restore $rounds
  1278. &movups ($inout0,&QWP(-16,$out)); # load input
  1279. &xorps ($inout0,$inout3); # input^=tweak
  1280. if ($inline)
  1281. { &aesni_inline_generate1("enc"); }
  1282. else
  1283. { &call ("_aesni_encrypt1"); }
  1284. &xorps ($inout0,$inout3); # output^=tweak
  1285. &movups (&QWP(-16,$out),$inout0); # write output
  1286. &set_label("xts_enc_ret");
  1287. &pxor ("xmm0","xmm0"); # clear register bank
  1288. &pxor ("xmm1","xmm1");
  1289. &pxor ("xmm2","xmm2");
  1290. &movdqa (&QWP(16*0,"esp"),"xmm0"); # clear stack
  1291. &pxor ("xmm3","xmm3");
  1292. &movdqa (&QWP(16*1,"esp"),"xmm0");
  1293. &pxor ("xmm4","xmm4");
  1294. &movdqa (&QWP(16*2,"esp"),"xmm0");
  1295. &pxor ("xmm5","xmm5");
  1296. &movdqa (&QWP(16*3,"esp"),"xmm0");
  1297. &pxor ("xmm6","xmm6");
  1298. &movdqa (&QWP(16*4,"esp"),"xmm0");
  1299. &pxor ("xmm7","xmm7");
  1300. &movdqa (&QWP(16*5,"esp"),"xmm0");
  1301. &mov ("esp",&DWP(16*7+4,"esp")); # restore %esp
  1302. &function_end("aesni_xts_encrypt");
  1303. &function_begin("aesni_xts_decrypt");
  1304. &mov ($key,&wparam(4)); # key2
  1305. &mov ($inp,&wparam(5)); # clear-text tweak
  1306. &mov ($rounds,&DWP(240,$key)); # key2->rounds
  1307. &movups ($inout0,&QWP(0,$inp));
  1308. if ($inline)
  1309. { &aesni_inline_generate1("enc"); }
  1310. else
  1311. { &call ("_aesni_encrypt1"); }
  1312. &mov ($inp,&wparam(0));
  1313. &mov ($out,&wparam(1));
  1314. &mov ($len,&wparam(2));
  1315. &mov ($key,&wparam(3)); # key1
  1316. &mov ($key_,"esp");
  1317. &sub ("esp",16*7+8);
  1318. &and ("esp",-16); # align stack
  1319. &xor ($rounds_,$rounds_); # if(len%16) len-=16;
  1320. &test ($len,15);
  1321. &setnz (&LB($rounds_));
  1322. &shl ($rounds_,4);
  1323. &sub ($len,$rounds_);
  1324. &mov (&DWP(16*6+0,"esp"),0x87); # compose the magic constant
  1325. &mov (&DWP(16*6+4,"esp"),0);
  1326. &mov (&DWP(16*6+8,"esp"),1);
  1327. &mov (&DWP(16*6+12,"esp"),0);
  1328. &mov (&DWP(16*7+0,"esp"),$len); # save original $len
  1329. &mov (&DWP(16*7+4,"esp"),$key_); # save original %esp
  1330. &mov ($rounds,&DWP(240,$key)); # key1->rounds
  1331. &mov ($key_,$key); # backup $key
  1332. &mov ($rounds_,$rounds); # backup $rounds
  1333. &movdqa ($tweak,$inout0);
  1334. &pxor ($twtmp,$twtmp);
  1335. &movdqa ($twmask,&QWP(6*16,"esp")); # 0x0...010...87
  1336. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1337. &and ($len,-16);
  1338. &sub ($len,16*6);
  1339. &jc (&label("xts_dec_short"));
  1340. &shl ($rounds,4);
  1341. &mov ($rounds_,16);
  1342. &sub ($rounds_,$rounds);
  1343. &lea ($key,&DWP(32,$key,$rounds));
  1344. &jmp (&label("xts_dec_loop6"));
  1345. &set_label("xts_dec_loop6",16);
  1346. for ($i=0;$i<4;$i++) {
  1347. &pshufd ($twres,$twtmp,0x13);
  1348. &pxor ($twtmp,$twtmp);
  1349. &movdqa (&QWP(16*$i,"esp"),$tweak);
  1350. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1351. &pand ($twres,$twmask); # isolate carry and residue
  1352. &pcmpgtd ($twtmp,$tweak); # broadcast upper bits
  1353. &pxor ($tweak,$twres);
  1354. }
  1355. &pshufd ($inout5,$twtmp,0x13);
  1356. &movdqa (&QWP(16*$i++,"esp"),$tweak);
  1357. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1358. &$movekey ($rndkey0,&QWP(0,$key_));
  1359. &pand ($inout5,$twmask); # isolate carry and residue
  1360. &movups ($inout0,&QWP(0,$inp)); # load input
  1361. &pxor ($inout5,$tweak);
  1362. # inline _aesni_encrypt6 prologue and flip xor with tweak and key[0]
  1363. &mov ($rounds,$rounds_);
  1364. &movdqu ($inout1,&QWP(16*1,$inp));
  1365. &xorps ($inout0,$rndkey0); # input^=rndkey[0]
  1366. &movdqu ($inout2,&QWP(16*2,$inp));
  1367. &pxor ($inout1,$rndkey0);
  1368. &movdqu ($inout3,&QWP(16*3,$inp));
  1369. &pxor ($inout2,$rndkey0);
  1370. &movdqu ($inout4,&QWP(16*4,$inp));
  1371. &pxor ($inout3,$rndkey0);
  1372. &movdqu ($rndkey1,&QWP(16*5,$inp));
  1373. &pxor ($inout4,$rndkey0);
  1374. &lea ($inp,&DWP(16*6,$inp));
  1375. &pxor ($inout0,&QWP(16*0,"esp")); # input^=tweak
  1376. &movdqa (&QWP(16*$i,"esp"),$inout5); # save last tweak
  1377. &pxor ($inout5,$rndkey1);
  1378. &$movekey ($rndkey1,&QWP(16,$key_));
  1379. &pxor ($inout1,&QWP(16*1,"esp"));
  1380. &pxor ($inout2,&QWP(16*2,"esp"));
  1381. &aesdec ($inout0,$rndkey1);
  1382. &pxor ($inout3,&QWP(16*3,"esp"));
  1383. &pxor ($inout4,&QWP(16*4,"esp"));
  1384. &aesdec ($inout1,$rndkey1);
  1385. &pxor ($inout5,$rndkey0);
  1386. &$movekey ($rndkey0,&QWP(32,$key_));
  1387. &aesdec ($inout2,$rndkey1);
  1388. &aesdec ($inout3,$rndkey1);
  1389. &aesdec ($inout4,$rndkey1);
  1390. &aesdec ($inout5,$rndkey1);
  1391. &call (&label("_aesni_decrypt6_enter"));
  1392. &movdqa ($tweak,&QWP(16*5,"esp")); # last tweak
  1393. &pxor ($twtmp,$twtmp);
  1394. &xorps ($inout0,&QWP(16*0,"esp")); # output^=tweak
  1395. &pcmpgtd ($twtmp,$tweak); # broadcast upper bits
  1396. &xorps ($inout1,&QWP(16*1,"esp"));
  1397. &movups (&QWP(16*0,$out),$inout0); # write output
  1398. &xorps ($inout2,&QWP(16*2,"esp"));
  1399. &movups (&QWP(16*1,$out),$inout1);
  1400. &xorps ($inout3,&QWP(16*3,"esp"));
  1401. &movups (&QWP(16*2,$out),$inout2);
  1402. &xorps ($inout4,&QWP(16*4,"esp"));
  1403. &movups (&QWP(16*3,$out),$inout3);
  1404. &xorps ($inout5,$tweak);
  1405. &movups (&QWP(16*4,$out),$inout4);
  1406. &pshufd ($twres,$twtmp,0x13);
  1407. &movups (&QWP(16*5,$out),$inout5);
  1408. &lea ($out,&DWP(16*6,$out));
  1409. &movdqa ($twmask,&QWP(16*6,"esp")); # 0x0...010...87
  1410. &pxor ($twtmp,$twtmp);
  1411. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1412. &pand ($twres,$twmask); # isolate carry and residue
  1413. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1414. &pxor ($tweak,$twres);
  1415. &sub ($len,16*6);
  1416. &jnc (&label("xts_dec_loop6"));
  1417. &mov ($rounds,&DWP(240,$key_)); # restore $rounds
  1418. &mov ($key,$key_); # restore $key
  1419. &mov ($rounds_,$rounds);
  1420. &set_label("xts_dec_short");
  1421. &add ($len,16*6);
  1422. &jz (&label("xts_dec_done6x"));
  1423. &movdqa ($inout3,$tweak); # put aside previous tweak
  1424. &cmp ($len,0x20);
  1425. &jb (&label("xts_dec_one"));
  1426. &pshufd ($twres,$twtmp,0x13);
  1427. &pxor ($twtmp,$twtmp);
  1428. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1429. &pand ($twres,$twmask); # isolate carry and residue
  1430. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1431. &pxor ($tweak,$twres);
  1432. &je (&label("xts_dec_two"));
  1433. &pshufd ($twres,$twtmp,0x13);
  1434. &pxor ($twtmp,$twtmp);
  1435. &movdqa ($inout4,$tweak); # put aside previous tweak
  1436. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1437. &pand ($twres,$twmask); # isolate carry and residue
  1438. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1439. &pxor ($tweak,$twres);
  1440. &cmp ($len,0x40);
  1441. &jb (&label("xts_dec_three"));
  1442. &pshufd ($twres,$twtmp,0x13);
  1443. &pxor ($twtmp,$twtmp);
  1444. &movdqa ($inout5,$tweak); # put aside previous tweak
  1445. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1446. &pand ($twres,$twmask); # isolate carry and residue
  1447. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1448. &pxor ($tweak,$twres);
  1449. &movdqa (&QWP(16*0,"esp"),$inout3);
  1450. &movdqa (&QWP(16*1,"esp"),$inout4);
  1451. &je (&label("xts_dec_four"));
  1452. &movdqa (&QWP(16*2,"esp"),$inout5);
  1453. &pshufd ($inout5,$twtmp,0x13);
  1454. &movdqa (&QWP(16*3,"esp"),$tweak);
  1455. &paddq ($tweak,$tweak); # &psllq($inout0,1);
  1456. &pand ($inout5,$twmask); # isolate carry and residue
  1457. &pxor ($inout5,$tweak);
  1458. &movdqu ($inout0,&QWP(16*0,$inp)); # load input
  1459. &movdqu ($inout1,&QWP(16*1,$inp));
  1460. &movdqu ($inout2,&QWP(16*2,$inp));
  1461. &pxor ($inout0,&QWP(16*0,"esp")); # input^=tweak
  1462. &movdqu ($inout3,&QWP(16*3,$inp));
  1463. &pxor ($inout1,&QWP(16*1,"esp"));
  1464. &movdqu ($inout4,&QWP(16*4,$inp));
  1465. &pxor ($inout2,&QWP(16*2,"esp"));
  1466. &lea ($inp,&DWP(16*5,$inp));
  1467. &pxor ($inout3,&QWP(16*3,"esp"));
  1468. &movdqa (&QWP(16*4,"esp"),$inout5); # save last tweak
  1469. &pxor ($inout4,$inout5);
  1470. &call ("_aesni_decrypt6");
  1471. &movaps ($tweak,&QWP(16*4,"esp")); # last tweak
  1472. &xorps ($inout0,&QWP(16*0,"esp")); # output^=tweak
  1473. &xorps ($inout1,&QWP(16*1,"esp"));
  1474. &xorps ($inout2,&QWP(16*2,"esp"));
  1475. &movups (&QWP(16*0,$out),$inout0); # write output
  1476. &xorps ($inout3,&QWP(16*3,"esp"));
  1477. &movups (&QWP(16*1,$out),$inout1);
  1478. &xorps ($inout4,$tweak);
  1479. &movups (&QWP(16*2,$out),$inout2);
  1480. &movups (&QWP(16*3,$out),$inout3);
  1481. &movups (&QWP(16*4,$out),$inout4);
  1482. &lea ($out,&DWP(16*5,$out));
  1483. &jmp (&label("xts_dec_done"));
  1484. &set_label("xts_dec_one",16);
  1485. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1486. &lea ($inp,&DWP(16*1,$inp));
  1487. &xorps ($inout0,$inout3); # input^=tweak
  1488. if ($inline)
  1489. { &aesni_inline_generate1("dec"); }
  1490. else
  1491. { &call ("_aesni_decrypt1"); }
  1492. &xorps ($inout0,$inout3); # output^=tweak
  1493. &movups (&QWP(16*0,$out),$inout0); # write output
  1494. &lea ($out,&DWP(16*1,$out));
  1495. &movdqa ($tweak,$inout3); # last tweak
  1496. &jmp (&label("xts_dec_done"));
  1497. &set_label("xts_dec_two",16);
  1498. &movaps ($inout4,$tweak); # put aside last tweak
  1499. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1500. &movups ($inout1,&QWP(16*1,$inp));
  1501. &lea ($inp,&DWP(16*2,$inp));
  1502. &xorps ($inout0,$inout3); # input^=tweak
  1503. &xorps ($inout1,$inout4);
  1504. &call ("_aesni_decrypt2");
  1505. &xorps ($inout0,$inout3); # output^=tweak
  1506. &xorps ($inout1,$inout4);
  1507. &movups (&QWP(16*0,$out),$inout0); # write output
  1508. &movups (&QWP(16*1,$out),$inout1);
  1509. &lea ($out,&DWP(16*2,$out));
  1510. &movdqa ($tweak,$inout4); # last tweak
  1511. &jmp (&label("xts_dec_done"));
  1512. &set_label("xts_dec_three",16);
  1513. &movaps ($inout5,$tweak); # put aside last tweak
  1514. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1515. &movups ($inout1,&QWP(16*1,$inp));
  1516. &movups ($inout2,&QWP(16*2,$inp));
  1517. &lea ($inp,&DWP(16*3,$inp));
  1518. &xorps ($inout0,$inout3); # input^=tweak
  1519. &xorps ($inout1,$inout4);
  1520. &xorps ($inout2,$inout5);
  1521. &call ("_aesni_decrypt3");
  1522. &xorps ($inout0,$inout3); # output^=tweak
  1523. &xorps ($inout1,$inout4);
  1524. &xorps ($inout2,$inout5);
  1525. &movups (&QWP(16*0,$out),$inout0); # write output
  1526. &movups (&QWP(16*1,$out),$inout1);
  1527. &movups (&QWP(16*2,$out),$inout2);
  1528. &lea ($out,&DWP(16*3,$out));
  1529. &movdqa ($tweak,$inout5); # last tweak
  1530. &jmp (&label("xts_dec_done"));
  1531. &set_label("xts_dec_four",16);
  1532. &movaps ($inout4,$tweak); # put aside last tweak
  1533. &movups ($inout0,&QWP(16*0,$inp)); # load input
  1534. &movups ($inout1,&QWP(16*1,$inp));
  1535. &movups ($inout2,&QWP(16*2,$inp));
  1536. &xorps ($inout0,&QWP(16*0,"esp")); # input^=tweak
  1537. &movups ($inout3,&QWP(16*3,$inp));
  1538. &lea ($inp,&DWP(16*4,$inp));
  1539. &xorps ($inout1,&QWP(16*1,"esp"));
  1540. &xorps ($inout2,$inout5);
  1541. &xorps ($inout3,$inout4);
  1542. &call ("_aesni_decrypt4");
  1543. &xorps ($inout0,&QWP(16*0,"esp")); # output^=tweak
  1544. &xorps ($inout1,&QWP(16*1,"esp"));
  1545. &xorps ($inout2,$inout5);
  1546. &movups (&QWP(16*0,$out),$inout0); # write output
  1547. &xorps ($inout3,$inout4);
  1548. &movups (&QWP(16*1,$out),$inout1);
  1549. &movups (&QWP(16*2,$out),$inout2);
  1550. &movups (&QWP(16*3,$out),$inout3);
  1551. &lea ($out,&DWP(16*4,$out));
  1552. &movdqa ($tweak,$inout4); # last tweak
  1553. &jmp (&label("xts_dec_done"));
  1554. &set_label("xts_dec_done6x",16); # $tweak is pre-calculated
  1555. &mov ($len,&DWP(16*7+0,"esp")); # restore original $len
  1556. &and ($len,15);
  1557. &jz (&label("xts_dec_ret"));
  1558. &mov (&DWP(16*7+0,"esp"),$len); # save $len%16
  1559. &jmp (&label("xts_dec_only_one_more"));
  1560. &set_label("xts_dec_done",16);
  1561. &mov ($len,&DWP(16*7+0,"esp")); # restore original $len
  1562. &pxor ($twtmp,$twtmp);
  1563. &and ($len,15);
  1564. &jz (&label("xts_dec_ret"));
  1565. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1566. &mov (&DWP(16*7+0,"esp"),$len); # save $len%16
  1567. &pshufd ($twres,$twtmp,0x13);
  1568. &pxor ($twtmp,$twtmp);
  1569. &movdqa ($twmask,&QWP(16*6,"esp"));
  1570. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1571. &pand ($twres,$twmask); # isolate carry and residue
  1572. &pcmpgtd($twtmp,$tweak); # broadcast upper bits
  1573. &pxor ($tweak,$twres);
  1574. &set_label("xts_dec_only_one_more");
  1575. &pshufd ($inout3,$twtmp,0x13);
  1576. &movdqa ($inout4,$tweak); # put aside previous tweak
  1577. &paddq ($tweak,$tweak); # &psllq($tweak,1);
  1578. &pand ($inout3,$twmask); # isolate carry and residue
  1579. &pxor ($inout3,$tweak);
  1580. &mov ($key,$key_); # restore $key
  1581. &mov ($rounds,$rounds_); # restore $rounds
  1582. &movups ($inout0,&QWP(0,$inp)); # load input
  1583. &xorps ($inout0,$inout3); # input^=tweak
  1584. if ($inline)
  1585. { &aesni_inline_generate1("dec"); }
  1586. else
  1587. { &call ("_aesni_decrypt1"); }
  1588. &xorps ($inout0,$inout3); # output^=tweak
  1589. &movups (&QWP(0,$out),$inout0); # write output
  1590. &set_label("xts_dec_steal");
  1591. &movz ($rounds,&BP(16,$inp));
  1592. &movz ($key,&BP(0,$out));
  1593. &lea ($inp,&DWP(1,$inp));
  1594. &mov (&BP(0,$out),&LB($rounds));
  1595. &mov (&BP(16,$out),&LB($key));
  1596. &lea ($out,&DWP(1,$out));
  1597. &sub ($len,1);
  1598. &jnz (&label("xts_dec_steal"));
  1599. &sub ($out,&DWP(16*7+0,"esp")); # rewind $out
  1600. &mov ($key,$key_); # restore $key
  1601. &mov ($rounds,$rounds_); # restore $rounds
  1602. &movups ($inout0,&QWP(0,$out)); # load input
  1603. &xorps ($inout0,$inout4); # input^=tweak
  1604. if ($inline)
  1605. { &aesni_inline_generate1("dec"); }
  1606. else
  1607. { &call ("_aesni_decrypt1"); }
  1608. &xorps ($inout0,$inout4); # output^=tweak
  1609. &movups (&QWP(0,$out),$inout0); # write output
  1610. &set_label("xts_dec_ret");
  1611. &pxor ("xmm0","xmm0"); # clear register bank
  1612. &pxor ("xmm1","xmm1");
  1613. &pxor ("xmm2","xmm2");
  1614. &movdqa (&QWP(16*0,"esp"),"xmm0"); # clear stack
  1615. &pxor ("xmm3","xmm3");
  1616. &movdqa (&QWP(16*1,"esp"),"xmm0");
  1617. &pxor ("xmm4","xmm4");
  1618. &movdqa (&QWP(16*2,"esp"),"xmm0");
  1619. &pxor ("xmm5","xmm5");
  1620. &movdqa (&QWP(16*3,"esp"),"xmm0");
  1621. &pxor ("xmm6","xmm6");
  1622. &movdqa (&QWP(16*4,"esp"),"xmm0");
  1623. &pxor ("xmm7","xmm7");
  1624. &movdqa (&QWP(16*5,"esp"),"xmm0");
  1625. &mov ("esp",&DWP(16*7+4,"esp")); # restore %esp
  1626. &function_end("aesni_xts_decrypt");
  1627. }
  1628. }
  1629. ######################################################################
  1630. # void $PREFIX_cbc_encrypt (const void *inp, void *out,
  1631. # size_t length, const AES_KEY *key,
  1632. # unsigned char *ivp,const int enc);
  1633. &function_begin("${PREFIX}_cbc_encrypt");
  1634. &mov ($inp,&wparam(0));
  1635. &mov ($rounds_,"esp");
  1636. &mov ($out,&wparam(1));
  1637. &sub ($rounds_,24);
  1638. &mov ($len,&wparam(2));
  1639. &and ($rounds_,-16);
  1640. &mov ($key,&wparam(3));
  1641. &mov ($key_,&wparam(4));
  1642. &test ($len,$len);
  1643. &jz (&label("cbc_abort"));
  1644. &cmp (&wparam(5),0);
  1645. &xchg ($rounds_,"esp"); # alloca
  1646. &movups ($ivec,&QWP(0,$key_)); # load IV
  1647. &mov ($rounds,&DWP(240,$key));
  1648. &mov ($key_,$key); # backup $key
  1649. &mov (&DWP(16,"esp"),$rounds_); # save original %esp
  1650. &mov ($rounds_,$rounds); # backup $rounds
  1651. &je (&label("cbc_decrypt"));
  1652. &movaps ($inout0,$ivec);
  1653. &cmp ($len,16);
  1654. &jb (&label("cbc_enc_tail"));
  1655. &sub ($len,16);
  1656. &jmp (&label("cbc_enc_loop"));
  1657. &set_label("cbc_enc_loop",16);
  1658. &movups ($ivec,&QWP(0,$inp)); # input actually
  1659. &lea ($inp,&DWP(16,$inp));
  1660. if ($inline)
  1661. { &aesni_inline_generate1("enc",$inout0,$ivec); }
  1662. else
  1663. { &xorps($inout0,$ivec); &call("_aesni_encrypt1"); }
  1664. &mov ($rounds,$rounds_); # restore $rounds
  1665. &mov ($key,$key_); # restore $key
  1666. &movups (&QWP(0,$out),$inout0); # store output
  1667. &lea ($out,&DWP(16,$out));
  1668. &sub ($len,16);
  1669. &jnc (&label("cbc_enc_loop"));
  1670. &add ($len,16);
  1671. &jnz (&label("cbc_enc_tail"));
  1672. &movaps ($ivec,$inout0);
  1673. &pxor ($inout0,$inout0);
  1674. &jmp (&label("cbc_ret"));
  1675. &set_label("cbc_enc_tail");
  1676. &mov ("ecx",$len); # zaps $rounds
  1677. &data_word(0xA4F3F689); # rep movsb
  1678. &mov ("ecx",16); # zero tail
  1679. &sub ("ecx",$len);
  1680. &xor ("eax","eax"); # zaps $len
  1681. &data_word(0xAAF3F689); # rep stosb
  1682. &lea ($out,&DWP(-16,$out)); # rewind $out by 1 block
  1683. &mov ($rounds,$rounds_); # restore $rounds
  1684. &mov ($inp,$out); # $inp and $out are the same
  1685. &mov ($key,$key_); # restore $key
  1686. &jmp (&label("cbc_enc_loop"));
  1687. ######################################################################
  1688. &set_label("cbc_decrypt",16);
  1689. &cmp ($len,0x50);
  1690. &jbe (&label("cbc_dec_tail"));
  1691. &movaps (&QWP(0,"esp"),$ivec); # save IV
  1692. &sub ($len,0x50);
  1693. &jmp (&label("cbc_dec_loop6_enter"));
  1694. &set_label("cbc_dec_loop6",16);
  1695. &movaps (&QWP(0,"esp"),$rndkey0); # save IV
  1696. &movups (&QWP(0,$out),$inout5);
  1697. &lea ($out,&DWP(0x10,$out));
  1698. &set_label("cbc_dec_loop6_enter");
  1699. &movdqu ($inout0,&QWP(0,$inp));
  1700. &movdqu ($inout1,&QWP(0x10,$inp));
  1701. &movdqu ($inout2,&QWP(0x20,$inp));
  1702. &movdqu ($inout3,&QWP(0x30,$inp));
  1703. &movdqu ($inout4,&QWP(0x40,$inp));
  1704. &movdqu ($inout5,&QWP(0x50,$inp));
  1705. &call ("_aesni_decrypt6");
  1706. &movups ($rndkey1,&QWP(0,$inp));
  1707. &movups ($rndkey0,&QWP(0x10,$inp));
  1708. &xorps ($inout0,&QWP(0,"esp")); # ^=IV
  1709. &xorps ($inout1,$rndkey1);
  1710. &movups ($rndkey1,&QWP(0x20,$inp));
  1711. &xorps ($inout2,$rndkey0);
  1712. &movups ($rndkey0,&QWP(0x30,$inp));
  1713. &xorps ($inout3,$rndkey1);
  1714. &movups ($rndkey1,&QWP(0x40,$inp));
  1715. &xorps ($inout4,$rndkey0);
  1716. &movups ($rndkey0,&QWP(0x50,$inp)); # IV
  1717. &xorps ($inout5,$rndkey1);
  1718. &movups (&QWP(0,$out),$inout0);
  1719. &movups (&QWP(0x10,$out),$inout1);
  1720. &lea ($inp,&DWP(0x60,$inp));
  1721. &movups (&QWP(0x20,$out),$inout2);
  1722. &mov ($rounds,$rounds_); # restore $rounds
  1723. &movups (&QWP(0x30,$out),$inout3);
  1724. &mov ($key,$key_); # restore $key
  1725. &movups (&QWP(0x40,$out),$inout4);
  1726. &lea ($out,&DWP(0x50,$out));
  1727. &sub ($len,0x60);
  1728. &ja (&label("cbc_dec_loop6"));
  1729. &movaps ($inout0,$inout5);
  1730. &movaps ($ivec,$rndkey0);
  1731. &add ($len,0x50);
  1732. &jle (&label("cbc_dec_clear_tail_collected"));
  1733. &movups (&QWP(0,$out),$inout0);
  1734. &lea ($out,&DWP(0x10,$out));
  1735. &set_label("cbc_dec_tail");
  1736. &movups ($inout0,&QWP(0,$inp));
  1737. &movaps ($in0,$inout0);
  1738. &cmp ($len,0x10);
  1739. &jbe (&label("cbc_dec_one"));
  1740. &movups ($inout1,&QWP(0x10,$inp));
  1741. &movaps ($in1,$inout1);
  1742. &cmp ($len,0x20);
  1743. &jbe (&label("cbc_dec_two"));
  1744. &movups ($inout2,&QWP(0x20,$inp));
  1745. &cmp ($len,0x30);
  1746. &jbe (&label("cbc_dec_three"));
  1747. &movups ($inout3,&QWP(0x30,$inp));
  1748. &cmp ($len,0x40);
  1749. &jbe (&label("cbc_dec_four"));
  1750. &movups ($inout4,&QWP(0x40,$inp));
  1751. &movaps (&QWP(0,"esp"),$ivec); # save IV
  1752. &movups ($inout0,&QWP(0,$inp));
  1753. &xorps ($inout5,$inout5);
  1754. &call ("_aesni_decrypt6");
  1755. &movups ($rndkey1,&QWP(0,$inp));
  1756. &movups ($rndkey0,&QWP(0x10,$inp));
  1757. &xorps ($inout0,&QWP(0,"esp")); # ^= IV
  1758. &xorps ($inout1,$rndkey1);
  1759. &movups ($rndkey1,&QWP(0x20,$inp));
  1760. &xorps ($inout2,$rndkey0);
  1761. &movups ($rndkey0,&QWP(0x30,$inp));
  1762. &xorps ($inout3,$rndkey1);
  1763. &movups ($ivec,&QWP(0x40,$inp)); # IV
  1764. &xorps ($inout4,$rndkey0);
  1765. &movups (&QWP(0,$out),$inout0);
  1766. &movups (&QWP(0x10,$out),$inout1);
  1767. &pxor ($inout1,$inout1);
  1768. &movups (&QWP(0x20,$out),$inout2);
  1769. &pxor ($inout2,$inout2);
  1770. &movups (&QWP(0x30,$out),$inout3);
  1771. &pxor ($inout3,$inout3);
  1772. &lea ($out,&DWP(0x40,$out));
  1773. &movaps ($inout0,$inout4);
  1774. &pxor ($inout4,$inout4);
  1775. &sub ($len,0x50);
  1776. &jmp (&label("cbc_dec_tail_collected"));
  1777. &set_label("cbc_dec_one",16);
  1778. if ($inline)
  1779. { &aesni_inline_generate1("dec"); }
  1780. else
  1781. { &call ("_aesni_decrypt1"); }
  1782. &xorps ($inout0,$ivec);
  1783. &movaps ($ivec,$in0);
  1784. &sub ($len,0x10);
  1785. &jmp (&label("cbc_dec_tail_collected"));
  1786. &set_label("cbc_dec_two",16);
  1787. &call ("_aesni_decrypt2");
  1788. &xorps ($inout0,$ivec);
  1789. &xorps ($inout1,$in0);
  1790. &movups (&QWP(0,$out),$inout0);
  1791. &movaps ($inout0,$inout1);
  1792. &pxor ($inout1,$inout1);
  1793. &lea ($out,&DWP(0x10,$out));
  1794. &movaps ($ivec,$in1);
  1795. &sub ($len,0x20);
  1796. &jmp (&label("cbc_dec_tail_collected"));
  1797. &set_label("cbc_dec_three",16);
  1798. &call ("_aesni_decrypt3");
  1799. &xorps ($inout0,$ivec);
  1800. &xorps ($inout1,$in0);
  1801. &xorps ($inout2,$in1);
  1802. &movups (&QWP(0,$out),$inout0);
  1803. &movaps ($inout0,$inout2);
  1804. &pxor ($inout2,$inout2);
  1805. &movups (&QWP(0x10,$out),$inout1);
  1806. &pxor ($inout1,$inout1);
  1807. &lea ($out,&DWP(0x20,$out));
  1808. &movups ($ivec,&QWP(0x20,$inp));
  1809. &sub ($len,0x30);
  1810. &jmp (&label("cbc_dec_tail_collected"));
  1811. &set_label("cbc_dec_four",16);
  1812. &call ("_aesni_decrypt4");
  1813. &movups ($rndkey1,&QWP(0x10,$inp));
  1814. &movups ($rndkey0,&QWP(0x20,$inp));
  1815. &xorps ($inout0,$ivec);
  1816. &movups ($ivec,&QWP(0x30,$inp));
  1817. &xorps ($inout1,$in0);
  1818. &movups (&QWP(0,$out),$inout0);
  1819. &xorps ($inout2,$rndkey1);
  1820. &movups (&QWP(0x10,$out),$inout1);
  1821. &pxor ($inout1,$inout1);
  1822. &xorps ($inout3,$rndkey0);
  1823. &movups (&QWP(0x20,$out),$inout2);
  1824. &pxor ($inout2,$inout2);
  1825. &lea ($out,&DWP(0x30,$out));
  1826. &movaps ($inout0,$inout3);
  1827. &pxor ($inout3,$inout3);
  1828. &sub ($len,0x40);
  1829. &jmp (&label("cbc_dec_tail_collected"));
  1830. &set_label("cbc_dec_clear_tail_collected",16);
  1831. &pxor ($inout1,$inout1);
  1832. &pxor ($inout2,$inout2);
  1833. &pxor ($inout3,$inout3);
  1834. &pxor ($inout4,$inout4);
  1835. &set_label("cbc_dec_tail_collected");
  1836. &and ($len,15);
  1837. &jnz (&label("cbc_dec_tail_partial"));
  1838. &movups (&QWP(0,$out),$inout0);
  1839. &pxor ($rndkey0,$rndkey0);
  1840. &jmp (&label("cbc_ret"));
  1841. &set_label("cbc_dec_tail_partial",16);
  1842. &movaps (&QWP(0,"esp"),$inout0);
  1843. &pxor ($rndkey0,$rndkey0);
  1844. &mov ("ecx",16);
  1845. &mov ($inp,"esp");
  1846. &sub ("ecx",$len);
  1847. &data_word(0xA4F3F689); # rep movsb
  1848. &movdqa (&QWP(0,"esp"),$inout0);
  1849. &set_label("cbc_ret");
  1850. &mov ("esp",&DWP(16,"esp")); # pull original %esp
  1851. &mov ($key_,&wparam(4));
  1852. &pxor ($inout0,$inout0);
  1853. &pxor ($rndkey1,$rndkey1);
  1854. &movups (&QWP(0,$key_),$ivec); # output IV
  1855. &pxor ($ivec,$ivec);
  1856. &set_label("cbc_abort");
  1857. &function_end("${PREFIX}_cbc_encrypt");
  1858. ######################################################################
  1859. # Mechanical port from aesni-x86_64.pl.
  1860. #
  1861. # _aesni_set_encrypt_key is private interface,
  1862. # input:
  1863. # "eax" const unsigned char *userKey
  1864. # $rounds int bits
  1865. # $key AES_KEY *key
  1866. # output:
  1867. # "eax" return code
  1868. # $round rounds
  1869. &function_begin_B("_aesni_set_encrypt_key");
  1870. &push ("ebp");
  1871. &push ("ebx");
  1872. &test ("eax","eax");
  1873. &jz (&label("bad_pointer"));
  1874. &test ($key,$key);
  1875. &jz (&label("bad_pointer"));
  1876. &call (&label("pic"));
  1877. &set_label("pic");
  1878. &blindpop("ebx");
  1879. &lea ("ebx",&DWP(&label("key_const")."-".&label("pic"),"ebx"));
  1880. &picmeup("ebp","OPENSSL_ia32cap_P","ebx",&label("key_const"));
  1881. &movups ("xmm0",&QWP(0,"eax")); # pull first 128 bits of *userKey
  1882. &xorps ("xmm4","xmm4"); # low dword of xmm4 is assumed 0
  1883. &mov ("ebp",&DWP(4,"ebp"));
  1884. &lea ($key,&DWP(16,$key));
  1885. &and ("ebp",1<<28|1<<11); # AVX and XOP bits
  1886. &cmp ($rounds,256);
  1887. &je (&label("14rounds"));
  1888. &cmp ($rounds,192);
  1889. &je (&label("12rounds"));
  1890. &cmp ($rounds,128);
  1891. &jne (&label("bad_keybits"));
  1892. &set_label("10rounds",16);
  1893. &cmp ("ebp",1<<28);
  1894. &je (&label("10rounds_alt"));
  1895. &mov ($rounds,9);
  1896. &$movekey (&QWP(-16,$key),"xmm0"); # round 0
  1897. &aeskeygenassist("xmm1","xmm0",0x01); # round 1
  1898. &call (&label("key_128_cold"));
  1899. &aeskeygenassist("xmm1","xmm0",0x2); # round 2
  1900. &call (&label("key_128"));
  1901. &aeskeygenassist("xmm1","xmm0",0x04); # round 3
  1902. &call (&label("key_128"));
  1903. &aeskeygenassist("xmm1","xmm0",0x08); # round 4
  1904. &call (&label("key_128"));
  1905. &aeskeygenassist("xmm1","xmm0",0x10); # round 5
  1906. &call (&label("key_128"));
  1907. &aeskeygenassist("xmm1","xmm0",0x20); # round 6
  1908. &call (&label("key_128"));
  1909. &aeskeygenassist("xmm1","xmm0",0x40); # round 7
  1910. &call (&label("key_128"));
  1911. &aeskeygenassist("xmm1","xmm0",0x80); # round 8
  1912. &call (&label("key_128"));
  1913. &aeskeygenassist("xmm1","xmm0",0x1b); # round 9
  1914. &call (&label("key_128"));
  1915. &aeskeygenassist("xmm1","xmm0",0x36); # round 10
  1916. &call (&label("key_128"));
  1917. &$movekey (&QWP(0,$key),"xmm0");
  1918. &mov (&DWP(80,$key),$rounds);
  1919. &jmp (&label("good_key"));
  1920. &set_label("key_128",16);
  1921. &$movekey (&QWP(0,$key),"xmm0");
  1922. &lea ($key,&DWP(16,$key));
  1923. &set_label("key_128_cold");
  1924. &shufps ("xmm4","xmm0",0b00010000);
  1925. &xorps ("xmm0","xmm4");
  1926. &shufps ("xmm4","xmm0",0b10001100);
  1927. &xorps ("xmm0","xmm4");
  1928. &shufps ("xmm1","xmm1",0b11111111); # critical path
  1929. &xorps ("xmm0","xmm1");
  1930. &ret();
  1931. &set_label("10rounds_alt",16);
  1932. &movdqa ("xmm5",&QWP(0x00,"ebx"));
  1933. &mov ($rounds,8);
  1934. &movdqa ("xmm4",&QWP(0x20,"ebx"));
  1935. &movdqa ("xmm2","xmm0");
  1936. &movdqu (&QWP(-16,$key),"xmm0");
  1937. &set_label("loop_key128");
  1938. &pshufb ("xmm0","xmm5");
  1939. &aesenclast ("xmm0","xmm4");
  1940. &pslld ("xmm4",1);
  1941. &lea ($key,&DWP(16,$key));
  1942. &movdqa ("xmm3","xmm2");
  1943. &pslldq ("xmm2",4);
  1944. &pxor ("xmm3","xmm2");
  1945. &pslldq ("xmm2",4);
  1946. &pxor ("xmm3","xmm2");
  1947. &pslldq ("xmm2",4);
  1948. &pxor ("xmm2","xmm3");
  1949. &pxor ("xmm0","xmm2");
  1950. &movdqu (&QWP(-16,$key),"xmm0");
  1951. &movdqa ("xmm2","xmm0");
  1952. &dec ($rounds);
  1953. &jnz (&label("loop_key128"));
  1954. &movdqa ("xmm4",&QWP(0x30,"ebx"));
  1955. &pshufb ("xmm0","xmm5");
  1956. &aesenclast ("xmm0","xmm4");
  1957. &pslld ("xmm4",1);
  1958. &movdqa ("xmm3","xmm2");
  1959. &pslldq ("xmm2",4);
  1960. &pxor ("xmm3","xmm2");
  1961. &pslldq ("xmm2",4);
  1962. &pxor ("xmm3","xmm2");
  1963. &pslldq ("xmm2",4);
  1964. &pxor ("xmm2","xmm3");
  1965. &pxor ("xmm0","xmm2");
  1966. &movdqu (&QWP(0,$key),"xmm0");
  1967. &movdqa ("xmm2","xmm0");
  1968. &pshufb ("xmm0","xmm5");
  1969. &aesenclast ("xmm0","xmm4");
  1970. &movdqa ("xmm3","xmm2");
  1971. &pslldq ("xmm2",4);
  1972. &pxor ("xmm3","xmm2");
  1973. &pslldq ("xmm2",4);
  1974. &pxor ("xmm3","xmm2");
  1975. &pslldq ("xmm2",4);
  1976. &pxor ("xmm2","xmm3");
  1977. &pxor ("xmm0","xmm2");
  1978. &movdqu (&QWP(16,$key),"xmm0");
  1979. &mov ($rounds,9);
  1980. &mov (&DWP(96,$key),$rounds);
  1981. &jmp (&label("good_key"));
  1982. &set_label("12rounds",16);
  1983. &movq ("xmm2",&QWP(16,"eax")); # remaining 1/3 of *userKey
  1984. &cmp ("ebp",1<<28);
  1985. &je (&label("12rounds_alt"));
  1986. &mov ($rounds,11);
  1987. &$movekey (&QWP(-16,$key),"xmm0"); # round 0
  1988. &aeskeygenassist("xmm1","xmm2",0x01); # round 1,2
  1989. &call (&label("key_192a_cold"));
  1990. &aeskeygenassist("xmm1","xmm2",0x02); # round 2,3
  1991. &call (&label("key_192b"));
  1992. &aeskeygenassist("xmm1","xmm2",0x04); # round 4,5
  1993. &call (&label("key_192a"));
  1994. &aeskeygenassist("xmm1","xmm2",0x08); # round 5,6
  1995. &call (&label("key_192b"));
  1996. &aeskeygenassist("xmm1","xmm2",0x10); # round 7,8
  1997. &call (&label("key_192a"));
  1998. &aeskeygenassist("xmm1","xmm2",0x20); # round 8,9
  1999. &call (&label("key_192b"));
  2000. &aeskeygenassist("xmm1","xmm2",0x40); # round 10,11
  2001. &call (&label("key_192a"));
  2002. &aeskeygenassist("xmm1","xmm2",0x80); # round 11,12
  2003. &call (&label("key_192b"));
  2004. &$movekey (&QWP(0,$key),"xmm0");
  2005. &mov (&DWP(48,$key),$rounds);
  2006. &jmp (&label("good_key"));
  2007. &set_label("key_192a",16);
  2008. &$movekey (&QWP(0,$key),"xmm0");
  2009. &lea ($key,&DWP(16,$key));
  2010. &set_label("key_192a_cold",16);
  2011. &movaps ("xmm5","xmm2");
  2012. &set_label("key_192b_warm");
  2013. &shufps ("xmm4","xmm0",0b00010000);
  2014. &movdqa ("xmm3","xmm2");
  2015. &xorps ("xmm0","xmm4");
  2016. &shufps ("xmm4","xmm0",0b10001100);
  2017. &pslldq ("xmm3",4);
  2018. &xorps ("xmm0","xmm4");
  2019. &pshufd ("xmm1","xmm1",0b01010101); # critical path
  2020. &pxor ("xmm2","xmm3");
  2021. &pxor ("xmm0","xmm1");
  2022. &pshufd ("xmm3","xmm0",0b11111111);
  2023. &pxor ("xmm2","xmm3");
  2024. &ret();
  2025. &set_label("key_192b",16);
  2026. &movaps ("xmm3","xmm0");
  2027. &shufps ("xmm5","xmm0",0b01000100);
  2028. &$movekey (&QWP(0,$key),"xmm5");
  2029. &shufps ("xmm3","xmm2",0b01001110);
  2030. &$movekey (&QWP(16,$key),"xmm3");
  2031. &lea ($key,&DWP(32,$key));
  2032. &jmp (&label("key_192b_warm"));
  2033. &set_label("12rounds_alt",16);
  2034. &movdqa ("xmm5",&QWP(0x10,"ebx"));
  2035. &movdqa ("xmm4",&QWP(0x20,"ebx"));
  2036. &mov ($rounds,8);
  2037. &movdqu (&QWP(-16,$key),"xmm0");
  2038. &set_label("loop_key192");
  2039. &movq (&QWP(0,$key),"xmm2");
  2040. &movdqa ("xmm1","xmm2");
  2041. &pshufb ("xmm2","xmm5");
  2042. &aesenclast ("xmm2","xmm4");
  2043. &pslld ("xmm4",1);
  2044. &lea ($key,&DWP(24,$key));
  2045. &movdqa ("xmm3","xmm0");
  2046. &pslldq ("xmm0",4);
  2047. &pxor ("xmm3","xmm0");
  2048. &pslldq ("xmm0",4);
  2049. &pxor ("xmm3","xmm0");
  2050. &pslldq ("xmm0",4);
  2051. &pxor ("xmm0","xmm3");
  2052. &pshufd ("xmm3","xmm0",0xff);
  2053. &pxor ("xmm3","xmm1");
  2054. &pslldq ("xmm1",4);
  2055. &pxor ("xmm3","xmm1");
  2056. &pxor ("xmm0","xmm2");
  2057. &pxor ("xmm2","xmm3");
  2058. &movdqu (&QWP(-16,$key),"xmm0");
  2059. &dec ($rounds);
  2060. &jnz (&label("loop_key192"));
  2061. &mov ($rounds,11);
  2062. &mov (&DWP(32,$key),$rounds);
  2063. &jmp (&label("good_key"));
  2064. &set_label("14rounds",16);
  2065. &movups ("xmm2",&QWP(16,"eax")); # remaining half of *userKey
  2066. &lea ($key,&DWP(16,$key));
  2067. &cmp ("ebp",1<<28);
  2068. &je (&label("14rounds_alt"));
  2069. &mov ($rounds,13);
  2070. &$movekey (&QWP(-32,$key),"xmm0"); # round 0
  2071. &$movekey (&QWP(-16,$key),"xmm2"); # round 1
  2072. &aeskeygenassist("xmm1","xmm2",0x01); # round 2
  2073. &call (&label("key_256a_cold"));
  2074. &aeskeygenassist("xmm1","xmm0",0x01); # round 3
  2075. &call (&label("key_256b"));
  2076. &aeskeygenassist("xmm1","xmm2",0x02); # round 4
  2077. &call (&label("key_256a"));
  2078. &aeskeygenassist("xmm1","xmm0",0x02); # round 5
  2079. &call (&label("key_256b"));
  2080. &aeskeygenassist("xmm1","xmm2",0x04); # round 6
  2081. &call (&label("key_256a"));
  2082. &aeskeygenassist("xmm1","xmm0",0x04); # round 7
  2083. &call (&label("key_256b"));
  2084. &aeskeygenassist("xmm1","xmm2",0x08); # round 8
  2085. &call (&label("key_256a"));
  2086. &aeskeygenassist("xmm1","xmm0",0x08); # round 9
  2087. &call (&label("key_256b"));
  2088. &aeskeygenassist("xmm1","xmm2",0x10); # round 10
  2089. &call (&label("key_256a"));
  2090. &aeskeygenassist("xmm1","xmm0",0x10); # round 11
  2091. &call (&label("key_256b"));
  2092. &aeskeygenassist("xmm1","xmm2",0x20); # round 12
  2093. &call (&label("key_256a"));
  2094. &aeskeygenassist("xmm1","xmm0",0x20); # round 13
  2095. &call (&label("key_256b"));
  2096. &aeskeygenassist("xmm1","xmm2",0x40); # round 14
  2097. &call (&label("key_256a"));
  2098. &$movekey (&QWP(0,$key),"xmm0");
  2099. &mov (&DWP(16,$key),$rounds);
  2100. &xor ("eax","eax");
  2101. &jmp (&label("good_key"));
  2102. &set_label("key_256a",16);
  2103. &$movekey (&QWP(0,$key),"xmm2");
  2104. &lea ($key,&DWP(16,$key));
  2105. &set_label("key_256a_cold");
  2106. &shufps ("xmm4","xmm0",0b00010000);
  2107. &xorps ("xmm0","xmm4");
  2108. &shufps ("xmm4","xmm0",0b10001100);
  2109. &xorps ("xmm0","xmm4");
  2110. &shufps ("xmm1","xmm1",0b11111111); # critical path
  2111. &xorps ("xmm0","xmm1");
  2112. &ret();
  2113. &set_label("key_256b",16);
  2114. &$movekey (&QWP(0,$key),"xmm0");
  2115. &lea ($key,&DWP(16,$key));
  2116. &shufps ("xmm4","xmm2",0b00010000);
  2117. &xorps ("xmm2","xmm4");
  2118. &shufps ("xmm4","xmm2",0b10001100);
  2119. &xorps ("xmm2","xmm4");
  2120. &shufps ("xmm1","xmm1",0b10101010); # critical path
  2121. &xorps ("xmm2","xmm1");
  2122. &ret();
  2123. &set_label("14rounds_alt",16);
  2124. &movdqa ("xmm5",&QWP(0x00,"ebx"));
  2125. &movdqa ("xmm4",&QWP(0x20,"ebx"));
  2126. &mov ($rounds,7);
  2127. &movdqu (&QWP(-32,$key),"xmm0");
  2128. &movdqa ("xmm1","xmm2");
  2129. &movdqu (&QWP(-16,$key),"xmm2");
  2130. &set_label("loop_key256");
  2131. &pshufb ("xmm2","xmm5");
  2132. &aesenclast ("xmm2","xmm4");
  2133. &movdqa ("xmm3","xmm0");
  2134. &pslldq ("xmm0",4);
  2135. &pxor ("xmm3","xmm0");
  2136. &pslldq ("xmm0",4);
  2137. &pxor ("xmm3","xmm0");
  2138. &pslldq ("xmm0",4);
  2139. &pxor ("xmm0","xmm3");
  2140. &pslld ("xmm4",1);
  2141. &pxor ("xmm0","xmm2");
  2142. &movdqu (&QWP(0,$key),"xmm0");
  2143. &dec ($rounds);
  2144. &jz (&label("done_key256"));
  2145. &pshufd ("xmm2","xmm0",0xff);
  2146. &pxor ("xmm3","xmm3");
  2147. &aesenclast ("xmm2","xmm3");
  2148. &movdqa ("xmm3","xmm1")
  2149. &pslldq ("xmm1",4);
  2150. &pxor ("xmm3","xmm1");
  2151. &pslldq ("xmm1",4);
  2152. &pxor ("xmm3","xmm1");
  2153. &pslldq ("xmm1",4);
  2154. &pxor ("xmm1","xmm3");
  2155. &pxor ("xmm2","xmm1");
  2156. &movdqu (&QWP(16,$key),"xmm2");
  2157. &lea ($key,&DWP(32,$key));
  2158. &movdqa ("xmm1","xmm2");
  2159. &jmp (&label("loop_key256"));
  2160. &set_label("done_key256");
  2161. &mov ($rounds,13);
  2162. &mov (&DWP(16,$key),$rounds);
  2163. &set_label("good_key");
  2164. &pxor ("xmm0","xmm0");
  2165. &pxor ("xmm1","xmm1");
  2166. &pxor ("xmm2","xmm2");
  2167. &pxor ("xmm3","xmm3");
  2168. &pxor ("xmm4","xmm4");
  2169. &pxor ("xmm5","xmm5");
  2170. &xor ("eax","eax");
  2171. &pop ("ebx");
  2172. &pop ("ebp");
  2173. &ret ();
  2174. &set_label("bad_pointer",4);
  2175. &mov ("eax",-1);
  2176. &pop ("ebx");
  2177. &pop ("ebp");
  2178. &ret ();
  2179. &set_label("bad_keybits",4);
  2180. &pxor ("xmm0","xmm0");
  2181. &mov ("eax",-2);
  2182. &pop ("ebx");
  2183. &pop ("ebp");
  2184. &ret ();
  2185. &function_end_B("_aesni_set_encrypt_key");
  2186. # int $PREFIX_set_encrypt_key (const unsigned char *userKey, int bits,
  2187. # AES_KEY *key)
  2188. &function_begin_B("${PREFIX}_set_encrypt_key");
  2189. &mov ("eax",&wparam(0));
  2190. &mov ($rounds,&wparam(1));
  2191. &mov ($key,&wparam(2));
  2192. &call ("_aesni_set_encrypt_key");
  2193. &ret ();
  2194. &function_end_B("${PREFIX}_set_encrypt_key");
  2195. # int $PREFIX_set_decrypt_key (const unsigned char *userKey, int bits,
  2196. # AES_KEY *key)
  2197. &function_begin_B("${PREFIX}_set_decrypt_key");
  2198. &mov ("eax",&wparam(0));
  2199. &mov ($rounds,&wparam(1));
  2200. &mov ($key,&wparam(2));
  2201. &call ("_aesni_set_encrypt_key");
  2202. &mov ($key,&wparam(2));
  2203. &shl ($rounds,4); # rounds-1 after _aesni_set_encrypt_key
  2204. &test ("eax","eax");
  2205. &jnz (&label("dec_key_ret"));
  2206. &lea ("eax",&DWP(16,$key,$rounds)); # end of key schedule
  2207. &$movekey ("xmm0",&QWP(0,$key)); # just swap
  2208. &$movekey ("xmm1",&QWP(0,"eax"));
  2209. &$movekey (&QWP(0,"eax"),"xmm0");
  2210. &$movekey (&QWP(0,$key),"xmm1");
  2211. &lea ($key,&DWP(16,$key));
  2212. &lea ("eax",&DWP(-16,"eax"));
  2213. &set_label("dec_key_inverse");
  2214. &$movekey ("xmm0",&QWP(0,$key)); # swap and inverse
  2215. &$movekey ("xmm1",&QWP(0,"eax"));
  2216. &aesimc ("xmm0","xmm0");
  2217. &aesimc ("xmm1","xmm1");
  2218. &lea ($key,&DWP(16,$key));
  2219. &lea ("eax",&DWP(-16,"eax"));
  2220. &$movekey (&QWP(16,"eax"),"xmm0");
  2221. &$movekey (&QWP(-16,$key),"xmm1");
  2222. &cmp ("eax",$key);
  2223. &ja (&label("dec_key_inverse"));
  2224. &$movekey ("xmm0",&QWP(0,$key)); # inverse middle
  2225. &aesimc ("xmm0","xmm0");
  2226. &$movekey (&QWP(0,$key),"xmm0");
  2227. &pxor ("xmm0","xmm0");
  2228. &pxor ("xmm1","xmm1");
  2229. &xor ("eax","eax"); # return success
  2230. &set_label("dec_key_ret");
  2231. &ret ();
  2232. &function_end_B("${PREFIX}_set_decrypt_key");
  2233. &set_label("key_const",64);
  2234. &data_word(0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d);
  2235. &data_word(0x04070605,0x04070605,0x04070605,0x04070605);
  2236. &data_word(1,1,1,1);
  2237. &data_word(0x1b,0x1b,0x1b,0x1b);
  2238. &asciz("AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>");
  2239. &asm_finish();
  2240. close STDOUT;