e_padlock-x86.pl 13 KB

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