e_padlock-x86.pl 18 KB

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