e_padlock-x86.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2018 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. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # September 2011
  15. #
  16. # Assembler helpers for Padlock engine. Compared to original engine
  17. # version relying on inline assembler and compiled with gcc 3.4.6 it
  18. # was measured to provide ~100% improvement on misaligned data in ECB
  19. # mode and ~75% in CBC mode. For aligned data improvement can be
  20. # observed for short inputs only, e.g. 45% for 64-byte messages in
  21. # ECB mode, 20% in CBC. Difference in performance for aligned vs.
  22. # misaligned data depends on misalignment and is either ~1.8x or 2.9x.
  23. # These are approximately same factors as for hardware support, so
  24. # there is little reason to rely on the latter. On the contrary, it
  25. # might actually hurt performance in mixture of aligned and misaligned
  26. # buffers, because a) if you choose to flip 'align' flag in control
  27. # word on per-buffer basis, then you'd have to reload key context,
  28. # which incurs penalty; b) if you choose to set 'align' flag
  29. # permanently, it limits performance even for aligned data to ~1/2.
  30. # All above mentioned results were collected on 1.5GHz C7. Nano on the
  31. # other hand handles unaligned data more gracefully. Depending on
  32. # algorithm and how unaligned data is, hardware can be up to 70% more
  33. # efficient than below software alignment procedures, nor does 'align'
  34. # flag have affect on aligned performance [if has any meaning at all].
  35. # Therefore suggestion is to unconditionally set 'align' flag on Nano
  36. # for optimal performance.
  37. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  38. push(@INC,"${dir}","${dir}../../crypto/perlasm");
  39. require "x86asm.pl";
  40. $output=pop and open STDOUT,">$output";
  41. &asm_init($ARGV[0]);
  42. %PADLOCK_PREFETCH=(ecb=>128, cbc=>64); # prefetch errata
  43. $PADLOCK_CHUNK=512; # Must be a power of 2 larger than 16
  44. $ctx="edx";
  45. $out="edi";
  46. $inp="esi";
  47. $len="ecx";
  48. $chunk="ebx";
  49. &function_begin_B("padlock_capability");
  50. &push ("ebx");
  51. &pushf ();
  52. &pop ("eax");
  53. &mov ("ecx","eax");
  54. &xor ("eax",1<<21);
  55. &push ("eax");
  56. &popf ();
  57. &pushf ();
  58. &pop ("eax");
  59. &xor ("ecx","eax");
  60. &xor ("eax","eax");
  61. &bt ("ecx",21);
  62. &jnc (&label("noluck"));
  63. &cpuid ();
  64. &xor ("eax","eax");
  65. &cmp ("ebx","0x".unpack("H*",'tneC'));
  66. &jne (&label("zhaoxin"));
  67. &cmp ("edx","0x".unpack("H*",'Hrua'));
  68. &jne (&label("noluck"));
  69. &cmp ("ecx","0x".unpack("H*",'slua'));
  70. &jne (&label("noluck"));
  71. &jmp (&label("zhaoxinEnd"));
  72. &set_label("zhaoxin");
  73. &cmp ("ebx","0x".unpack("H*",'hS '));
  74. &jne (&label("noluck"));
  75. &cmp ("edx","0x".unpack("H*",'hgna'));
  76. &jne (&label("noluck"));
  77. &cmp ("ecx","0x".unpack("H*",' ia'));
  78. &jne (&label("noluck"));
  79. &set_label("zhaoxinEnd");
  80. &mov ("eax",0xC0000000);
  81. &cpuid ();
  82. &mov ("edx","eax");
  83. &xor ("eax","eax");
  84. &cmp ("edx",0xC0000001);
  85. &jb (&label("noluck"));
  86. &mov ("eax",1);
  87. &cpuid ();
  88. &or ("eax",0x0f);
  89. &xor ("ebx","ebx");
  90. &and ("eax",0x0fff);
  91. &cmp ("eax",0x06ff); # check for Nano
  92. &sete ("bl");
  93. &mov ("eax",0xC0000001);
  94. &push ("ebx");
  95. &cpuid ();
  96. &pop ("ebx");
  97. &mov ("eax","edx");
  98. &shl ("ebx",4); # bit#4 denotes Nano
  99. &and ("eax",0xffffffef);
  100. &or ("eax","ebx")
  101. &set_label("noluck");
  102. &pop ("ebx");
  103. &ret ();
  104. &function_end_B("padlock_capability")
  105. &function_begin_B("padlock_key_bswap");
  106. &mov ("edx",&wparam(0));
  107. &mov ("ecx",&DWP(240,"edx"));
  108. &set_label("bswap_loop");
  109. &mov ("eax",&DWP(0,"edx"));
  110. &bswap ("eax");
  111. &mov (&DWP(0,"edx"),"eax");
  112. &lea ("edx",&DWP(4,"edx"));
  113. &sub ("ecx",1);
  114. &jnz (&label("bswap_loop"));
  115. &ret ();
  116. &function_end_B("padlock_key_bswap");
  117. # This is heuristic key context tracing. At first one
  118. # believes that one should use atomic swap instructions,
  119. # but it's not actually necessary. Point is that if
  120. # padlock_saved_context was changed by another thread
  121. # after we've read it and before we compare it with ctx,
  122. # our key *shall* be reloaded upon thread context switch
  123. # and we are therefore set in either case...
  124. &static_label("padlock_saved_context");
  125. &function_begin_B("padlock_verify_context");
  126. &mov ($ctx,&wparam(0));
  127. &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
  128. &DWP(&label("padlock_saved_context")."-".&label("verify_pic_point")));
  129. &pushf ();
  130. &call ("_padlock_verify_ctx");
  131. &set_label("verify_pic_point");
  132. &lea ("esp",&DWP(4,"esp"));
  133. &ret ();
  134. &function_end_B("padlock_verify_context");
  135. &function_begin_B("_padlock_verify_ctx");
  136. &add ("eax",&DWP(0,"esp")) if(!($::win32 or $::coff));# &padlock_saved_context
  137. &bt (&DWP(4,"esp"),30); # eflags
  138. &jnc (&label("verified"));
  139. &cmp ($ctx,&DWP(0,"eax"));
  140. &je (&label("verified"));
  141. &pushf ();
  142. &popf ();
  143. &set_label("verified");
  144. &mov (&DWP(0,"eax"),$ctx);
  145. &ret ();
  146. &function_end_B("_padlock_verify_ctx");
  147. &function_begin_B("padlock_reload_key");
  148. &pushf ();
  149. &popf ();
  150. &ret ();
  151. &function_end_B("padlock_reload_key");
  152. &function_begin_B("padlock_aes_block");
  153. &push ("edi");
  154. &push ("esi");
  155. &push ("ebx");
  156. &mov ($out,&wparam(0)); # must be 16-byte aligned
  157. &mov ($inp,&wparam(1)); # must be 16-byte aligned
  158. &mov ($ctx,&wparam(2));
  159. &mov ($len,1);
  160. &lea ("ebx",&DWP(32,$ctx)); # key
  161. &lea ($ctx,&DWP(16,$ctx)); # control word
  162. &data_byte(0xf3,0x0f,0xa7,0xc8); # rep xcryptecb
  163. &pop ("ebx");
  164. &pop ("esi");
  165. &pop ("edi");
  166. &ret ();
  167. &function_end_B("padlock_aes_block");
  168. sub generate_mode {
  169. my ($mode,$opcode) = @_;
  170. # int padlock_$mode_encrypt(void *out, const void *inp,
  171. # struct padlock_cipher_data *ctx, size_t len);
  172. &function_begin("padlock_${mode}_encrypt");
  173. &mov ($out,&wparam(0));
  174. &mov ($inp,&wparam(1));
  175. &mov ($ctx,&wparam(2));
  176. &mov ($len,&wparam(3));
  177. &test ($ctx,15);
  178. &jnz (&label("${mode}_abort"));
  179. &test ($len,15);
  180. &jnz (&label("${mode}_abort"));
  181. &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
  182. &DWP(&label("padlock_saved_context")."-".&label("${mode}_pic_point")));
  183. &pushf ();
  184. &cld ();
  185. &call ("_padlock_verify_ctx");
  186. &set_label("${mode}_pic_point");
  187. &lea ($ctx,&DWP(16,$ctx)); # control word
  188. &xor ("eax","eax");
  189. if ($mode eq "ctr32") {
  190. &movq ("mm0",&QWP(-16,$ctx)); # load [upper part of] counter
  191. } else {
  192. &xor ("ebx","ebx");
  193. &test (&DWP(0,$ctx),1<<5); # align bit in control word
  194. &jnz (&label("${mode}_aligned"));
  195. &test ($out,0x0f);
  196. &setz ("al"); # !out_misaligned
  197. &test ($inp,0x0f);
  198. &setz ("bl"); # !inp_misaligned
  199. &test ("eax","ebx");
  200. &jnz (&label("${mode}_aligned"));
  201. &neg ("eax");
  202. }
  203. &mov ($chunk,$PADLOCK_CHUNK);
  204. &not ("eax"); # out_misaligned?-1:0
  205. &lea ("ebp",&DWP(-24,"esp"));
  206. &cmp ($len,$chunk);
  207. &cmovc ($chunk,$len); # chunk=len>PADLOCK_CHUNK?PADLOCK_CHUNK:len
  208. &and ("eax",$chunk); # out_misaligned?chunk:0
  209. &mov ($chunk,$len);
  210. &neg ("eax");
  211. &and ($chunk,$PADLOCK_CHUNK-1); # chunk=len%PADLOCK_CHUNK
  212. &lea ("esp",&DWP(0,"eax","ebp")); # alloca
  213. &mov ("eax",$PADLOCK_CHUNK);
  214. &cmovz ($chunk,"eax"); # chunk=chunk?:PADLOCK_CHUNK
  215. &mov ("eax","ebp");
  216. &and ("ebp",-16);
  217. &and ("esp",-16);
  218. &mov (&DWP(16,"ebp"),"eax");
  219. if ($PADLOCK_PREFETCH{$mode}) {
  220. &cmp ($len,$chunk);
  221. &ja (&label("${mode}_loop"));
  222. &mov ("eax",$inp); # check if prefetch crosses page
  223. &cmp ("ebp","esp");
  224. &cmove ("eax",$out);
  225. &add ("eax",$len);
  226. &neg ("eax");
  227. &and ("eax",0xfff); # distance to page boundary
  228. &cmp ("eax",$PADLOCK_PREFETCH{$mode});
  229. &mov ("eax",-$PADLOCK_PREFETCH{$mode});
  230. &cmovae ("eax",$chunk); # mask=distance<prefetch?-prefetch:-1
  231. &and ($chunk,"eax");
  232. &jz (&label("${mode}_unaligned_tail"));
  233. }
  234. &jmp (&label("${mode}_loop"));
  235. &set_label("${mode}_loop",16);
  236. &mov (&DWP(0,"ebp"),$out); # save parameters
  237. &mov (&DWP(4,"ebp"),$inp);
  238. &mov (&DWP(8,"ebp"),$len);
  239. &mov ($len,$chunk);
  240. &mov (&DWP(12,"ebp"),$chunk); # chunk
  241. if ($mode eq "ctr32") {
  242. &mov ("ecx",&DWP(-4,$ctx));
  243. &xor ($out,$out);
  244. &mov ("eax",&DWP(-8,$ctx)); # borrow $len
  245. &set_label("${mode}_prepare");
  246. &mov (&DWP(12,"esp",$out),"ecx");
  247. &bswap ("ecx");
  248. &movq (&QWP(0,"esp",$out),"mm0");
  249. &inc ("ecx");
  250. &mov (&DWP(8,"esp",$out),"eax");
  251. &bswap ("ecx");
  252. &lea ($out,&DWP(16,$out));
  253. &cmp ($out,$chunk);
  254. &jb (&label("${mode}_prepare"));
  255. &mov (&DWP(-4,$ctx),"ecx");
  256. &lea ($inp,&DWP(0,"esp"));
  257. &lea ($out,&DWP(0,"esp"));
  258. &mov ($len,$chunk);
  259. } else {
  260. &test ($out,0x0f); # out_misaligned
  261. &cmovnz ($out,"esp");
  262. &test ($inp,0x0f); # inp_misaligned
  263. &jz (&label("${mode}_inp_aligned"));
  264. &shr ($len,2);
  265. &data_byte(0xf3,0xa5); # rep movsl
  266. &sub ($out,$chunk);
  267. &mov ($len,$chunk);
  268. &mov ($inp,$out);
  269. &set_label("${mode}_inp_aligned");
  270. }
  271. &lea ("eax",&DWP(-16,$ctx)); # ivp
  272. &lea ("ebx",&DWP(16,$ctx)); # key
  273. &shr ($len,4); # len/=AES_BLOCK_SIZE
  274. &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
  275. if ($mode !~ /ecb|ctr/) {
  276. &movaps ("xmm0",&QWP(0,"eax"));
  277. &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
  278. }
  279. &mov ($out,&DWP(0,"ebp")); # restore parameters
  280. &mov ($chunk,&DWP(12,"ebp"));
  281. if ($mode eq "ctr32") {
  282. &mov ($inp,&DWP(4,"ebp"));
  283. &xor ($len,$len);
  284. &set_label("${mode}_xor");
  285. &movups ("xmm1",&QWP(0,$inp,$len));
  286. &lea ($len,&DWP(16,$len));
  287. &pxor ("xmm1",&QWP(-16,"esp",$len));
  288. &movups (&QWP(-16,$out,$len),"xmm1");
  289. &cmp ($len,$chunk);
  290. &jb (&label("${mode}_xor"));
  291. } else {
  292. &test ($out,0x0f);
  293. &jz (&label("${mode}_out_aligned"));
  294. &mov ($len,$chunk);
  295. &lea ($inp,&DWP(0,"esp"));
  296. &shr ($len,2);
  297. &data_byte(0xf3,0xa5); # rep movsl
  298. &sub ($out,$chunk);
  299. &set_label("${mode}_out_aligned");
  300. &mov ($inp,&DWP(4,"ebp"));
  301. }
  302. &mov ($len,&DWP(8,"ebp"));
  303. &add ($out,$chunk);
  304. &add ($inp,$chunk);
  305. &sub ($len,$chunk);
  306. &mov ($chunk,$PADLOCK_CHUNK);
  307. if (!$PADLOCK_PREFETCH{$mode}) {
  308. &jnz (&label("${mode}_loop"));
  309. } else {
  310. &jz (&label("${mode}_break"));
  311. &cmp ($len,$chunk);
  312. &jae (&label("${mode}_loop"));
  313. &set_label("${mode}_unaligned_tail");
  314. &xor ("eax","eax");
  315. &cmp ("esp","ebp");
  316. &cmove ("eax",$len);
  317. &sub ("esp","eax"); # alloca
  318. &mov ("eax", $out); # save parameters
  319. &mov ($chunk,$len);
  320. &shr ($len,2);
  321. &lea ($out,&DWP(0,"esp"));
  322. &data_byte(0xf3,0xa5); # rep movsl
  323. &mov ($inp,"esp");
  324. &mov ($out,"eax"); # restore parameters
  325. &mov ($len,$chunk);
  326. &jmp (&label("${mode}_loop"));
  327. &set_label("${mode}_break",16);
  328. }
  329. if ($mode ne "ctr32") {
  330. &cmp ("esp","ebp");
  331. &je (&label("${mode}_done"));
  332. }
  333. &pxor ("xmm0","xmm0");
  334. &lea ("eax",&DWP(0,"esp"));
  335. &set_label("${mode}_bzero");
  336. &movaps (&QWP(0,"eax"),"xmm0");
  337. &lea ("eax",&DWP(16,"eax"));
  338. &cmp ("ebp","eax");
  339. &ja (&label("${mode}_bzero"));
  340. &set_label("${mode}_done");
  341. &mov ("ebp",&DWP(16,"ebp"));
  342. &lea ("esp",&DWP(24,"ebp"));
  343. if ($mode ne "ctr32") {
  344. &jmp (&label("${mode}_exit"));
  345. &set_label("${mode}_aligned",16);
  346. if ($PADLOCK_PREFETCH{$mode}) {
  347. &lea ("ebp",&DWP(0,$inp,$len));
  348. &neg ("ebp");
  349. &and ("ebp",0xfff); # distance to page boundary
  350. &xor ("eax","eax");
  351. &cmp ("ebp",$PADLOCK_PREFETCH{$mode});
  352. &mov ("ebp",$PADLOCK_PREFETCH{$mode}-1);
  353. &cmovae ("ebp","eax");
  354. &and ("ebp",$len); # remainder
  355. &sub ($len,"ebp");
  356. &jz (&label("${mode}_aligned_tail"));
  357. }
  358. &lea ("eax",&DWP(-16,$ctx)); # ivp
  359. &lea ("ebx",&DWP(16,$ctx)); # key
  360. &shr ($len,4); # len/=AES_BLOCK_SIZE
  361. &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
  362. if ($mode ne "ecb") {
  363. &movaps ("xmm0",&QWP(0,"eax"));
  364. &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
  365. }
  366. if ($PADLOCK_PREFETCH{$mode}) {
  367. &test ("ebp","ebp");
  368. &jz (&label("${mode}_exit"));
  369. &set_label("${mode}_aligned_tail");
  370. &mov ($len,"ebp");
  371. &lea ("ebp",&DWP(-24,"esp"));
  372. &mov ("esp","ebp");
  373. &mov ("eax","ebp");
  374. &sub ("esp",$len);
  375. &and ("ebp",-16);
  376. &and ("esp",-16);
  377. &mov (&DWP(16,"ebp"),"eax");
  378. &mov ("eax", $out); # save parameters
  379. &mov ($chunk,$len);
  380. &shr ($len,2);
  381. &lea ($out,&DWP(0,"esp"));
  382. &data_byte(0xf3,0xa5); # rep movsl
  383. &mov ($inp,"esp");
  384. &mov ($out,"eax"); # restore parameters
  385. &mov ($len,$chunk);
  386. &jmp (&label("${mode}_loop"));
  387. }
  388. &set_label("${mode}_exit"); }
  389. &mov ("eax",1);
  390. &lea ("esp",&DWP(4,"esp")); # popf
  391. &emms () if ($mode eq "ctr32");
  392. &set_label("${mode}_abort");
  393. &function_end("padlock_${mode}_encrypt");
  394. }
  395. &generate_mode("ecb",0xc8);
  396. &generate_mode("cbc",0xd0);
  397. &generate_mode("cfb",0xe0);
  398. &generate_mode("ofb",0xe8);
  399. &generate_mode("ctr32",0xc8); # yes, it implements own CTR with ECB opcode,
  400. # because hardware CTR was introduced later
  401. # and even has errata on certain C7 stepping.
  402. # own implementation *always* works, though
  403. # ~15% slower than dedicated hardware...
  404. &function_begin_B("padlock_xstore");
  405. &push ("edi");
  406. &mov ("edi",&wparam(0));
  407. &mov ("edx",&wparam(1));
  408. &data_byte(0x0f,0xa7,0xc0); # xstore
  409. &pop ("edi");
  410. &ret ();
  411. &function_end_B("padlock_xstore");
  412. &function_begin_B("_win32_segv_handler");
  413. &mov ("eax",1); # ExceptionContinueSearch
  414. &mov ("edx",&wparam(0)); # *ExceptionRecord
  415. &mov ("ecx",&wparam(2)); # *ContextRecord
  416. &cmp (&DWP(0,"edx"),0xC0000005) # ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION
  417. &jne (&label("ret"));
  418. &add (&DWP(184,"ecx"),4); # skip over rep sha*
  419. &mov ("eax",0); # ExceptionContinueExecution
  420. &set_label("ret");
  421. &ret ();
  422. &function_end_B("_win32_segv_handler");
  423. &safeseh("_win32_segv_handler") if ($::win32);
  424. &function_begin_B("padlock_sha1_oneshot");
  425. &push ("edi");
  426. &push ("esi");
  427. &xor ("eax","eax");
  428. &mov ("edi",&wparam(0));
  429. &mov ("esi",&wparam(1));
  430. &mov ("ecx",&wparam(2));
  431. if ($::win32 or $::coff) {
  432. &push (&::islabel("_win32_segv_handler"));
  433. &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
  434. &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
  435. }
  436. &mov ("edx","esp"); # put aside %esp
  437. &add ("esp",-128); # 32 is enough but spec says 128
  438. &movups ("xmm0",&QWP(0,"edi")); # copy-in context
  439. &and ("esp",-16);
  440. &mov ("eax",&DWP(16,"edi"));
  441. &movaps (&QWP(0,"esp"),"xmm0");
  442. &mov ("edi","esp");
  443. &mov (&DWP(16,"esp"),"eax");
  444. &xor ("eax","eax");
  445. &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
  446. &movaps ("xmm0",&QWP(0,"esp"));
  447. &mov ("eax",&DWP(16,"esp"));
  448. &mov ("esp","edx"); # restore %esp
  449. if ($::win32 or $::coff) {
  450. &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
  451. &lea ("esp",&DWP(4,"esp"));
  452. }
  453. &mov ("edi",&wparam(0));
  454. &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
  455. &mov (&DWP(16,"edi"),"eax");
  456. &pop ("esi");
  457. &pop ("edi");
  458. &ret ();
  459. &function_end_B("padlock_sha1_oneshot");
  460. &function_begin_B("padlock_sha1_blocks");
  461. &push ("edi");
  462. &push ("esi");
  463. &mov ("edi",&wparam(0));
  464. &mov ("esi",&wparam(1));
  465. &mov ("edx","esp"); # put aside %esp
  466. &mov ("ecx",&wparam(2));
  467. &add ("esp",-128);
  468. &movups ("xmm0",&QWP(0,"edi")); # copy-in context
  469. &and ("esp",-16);
  470. &mov ("eax",&DWP(16,"edi"));
  471. &movaps (&QWP(0,"esp"),"xmm0");
  472. &mov ("edi","esp");
  473. &mov (&DWP(16,"esp"),"eax");
  474. &mov ("eax",-1);
  475. &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
  476. &movaps ("xmm0",&QWP(0,"esp"));
  477. &mov ("eax",&DWP(16,"esp"));
  478. &mov ("esp","edx"); # restore %esp
  479. &mov ("edi",&wparam(0));
  480. &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
  481. &mov (&DWP(16,"edi"),"eax");
  482. &pop ("esi");
  483. &pop ("edi");
  484. &ret ();
  485. &function_end_B("padlock_sha1_blocks");
  486. &function_begin_B("padlock_sha256_oneshot");
  487. &push ("edi");
  488. &push ("esi");
  489. &xor ("eax","eax");
  490. &mov ("edi",&wparam(0));
  491. &mov ("esi",&wparam(1));
  492. &mov ("ecx",&wparam(2));
  493. if ($::win32 or $::coff) {
  494. &push (&::islabel("_win32_segv_handler"));
  495. &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
  496. &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
  497. }
  498. &mov ("edx","esp"); # put aside %esp
  499. &add ("esp",-128);
  500. &movups ("xmm0",&QWP(0,"edi")); # copy-in context
  501. &and ("esp",-16);
  502. &movups ("xmm1",&QWP(16,"edi"));
  503. &movaps (&QWP(0,"esp"),"xmm0");
  504. &mov ("edi","esp");
  505. &movaps (&QWP(16,"esp"),"xmm1");
  506. &xor ("eax","eax");
  507. &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
  508. &movaps ("xmm0",&QWP(0,"esp"));
  509. &movaps ("xmm1",&QWP(16,"esp"));
  510. &mov ("esp","edx"); # restore %esp
  511. if ($::win32 or $::coff) {
  512. &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
  513. &lea ("esp",&DWP(4,"esp"));
  514. }
  515. &mov ("edi",&wparam(0));
  516. &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
  517. &movups (&QWP(16,"edi"),"xmm1");
  518. &pop ("esi");
  519. &pop ("edi");
  520. &ret ();
  521. &function_end_B("padlock_sha256_oneshot");
  522. &function_begin_B("padlock_sha256_blocks");
  523. &push ("edi");
  524. &push ("esi");
  525. &mov ("edi",&wparam(0));
  526. &mov ("esi",&wparam(1));
  527. &mov ("ecx",&wparam(2));
  528. &mov ("edx","esp"); # put aside %esp
  529. &add ("esp",-128);
  530. &movups ("xmm0",&QWP(0,"edi")); # copy-in context
  531. &and ("esp",-16);
  532. &movups ("xmm1",&QWP(16,"edi"));
  533. &movaps (&QWP(0,"esp"),"xmm0");
  534. &mov ("edi","esp");
  535. &movaps (&QWP(16,"esp"),"xmm1");
  536. &mov ("eax",-1);
  537. &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
  538. &movaps ("xmm0",&QWP(0,"esp"));
  539. &movaps ("xmm1",&QWP(16,"esp"));
  540. &mov ("esp","edx"); # restore %esp
  541. &mov ("edi",&wparam(0));
  542. &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
  543. &movups (&QWP(16,"edi"),"xmm1");
  544. &pop ("esi");
  545. &pop ("edi");
  546. &ret ();
  547. &function_end_B("padlock_sha256_blocks");
  548. &function_begin_B("padlock_sha512_blocks");
  549. &push ("edi");
  550. &push ("esi");
  551. &mov ("edi",&wparam(0));
  552. &mov ("esi",&wparam(1));
  553. &mov ("ecx",&wparam(2));
  554. &mov ("edx","esp"); # put aside %esp
  555. &add ("esp",-128);
  556. &movups ("xmm0",&QWP(0,"edi")); # copy-in context
  557. &and ("esp",-16);
  558. &movups ("xmm1",&QWP(16,"edi"));
  559. &movups ("xmm2",&QWP(32,"edi"));
  560. &movups ("xmm3",&QWP(48,"edi"));
  561. &movaps (&QWP(0,"esp"),"xmm0");
  562. &mov ("edi","esp");
  563. &movaps (&QWP(16,"esp"),"xmm1");
  564. &movaps (&QWP(32,"esp"),"xmm2");
  565. &movaps (&QWP(48,"esp"),"xmm3");
  566. &data_byte(0xf3,0x0f,0xa6,0xe0); # rep xsha512
  567. &movaps ("xmm0",&QWP(0,"esp"));
  568. &movaps ("xmm1",&QWP(16,"esp"));
  569. &movaps ("xmm2",&QWP(32,"esp"));
  570. &movaps ("xmm3",&QWP(48,"esp"));
  571. &mov ("esp","edx"); # restore %esp
  572. &mov ("edi",&wparam(0));
  573. &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
  574. &movups (&QWP(16,"edi"),"xmm1");
  575. &movups (&QWP(32,"edi"),"xmm2");
  576. &movups (&QWP(48,"edi"),"xmm3");
  577. &pop ("esi");
  578. &pop ("edi");
  579. &ret ();
  580. &function_end_B("padlock_sha512_blocks");
  581. &asciz ("VIA Padlock x86 module, CRYPTOGAMS by <appro\@openssl.org>");
  582. &align (16);
  583. &dataseg();
  584. # Essentially this variable belongs in thread local storage.
  585. # Having this variable global on the other hand can only cause
  586. # few bogus key reloads [if any at all on signle-CPU system],
  587. # so we accept the penalty...
  588. &set_label("padlock_saved_context",4);
  589. &data_word(0);
  590. &asm_finish();
  591. close STDOUT;