e_padlock-x86.pl 18 KB

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