e_padlock-x86.pl 18 KB

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