sha256-586.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # SHA256 block transform for x86. September 2007.
  11. #
  12. # Performance in clock cycles per processed byte (less is better):
  13. #
  14. # Pentium PIII P4 AMD K8 Core2
  15. # gcc 46 36 41 27 26
  16. # icc 57 33 38 25 23
  17. # x86 asm 40 30 33 20 18
  18. # x86_64 asm(*) - - 21 16 16
  19. #
  20. # (*) x86_64 assembler performance is presented for reference
  21. # purposes.
  22. #
  23. # Performance improvement over compiler generated code varies from
  24. # 10% to 40% [see above]. Not very impressive on some µ-archs, but
  25. # it's 5 times smaller and optimizies amount of writes.
  26. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  27. push(@INC,"${dir}","${dir}../../perlasm");
  28. require "x86asm.pl";
  29. &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
  30. $A="eax";
  31. $E="edx";
  32. $T="ebx";
  33. $Aoff=&DWP(0,"esp");
  34. $Boff=&DWP(4,"esp");
  35. $Coff=&DWP(8,"esp");
  36. $Doff=&DWP(12,"esp");
  37. $Eoff=&DWP(16,"esp");
  38. $Foff=&DWP(20,"esp");
  39. $Goff=&DWP(24,"esp");
  40. $Hoff=&DWP(28,"esp");
  41. $Xoff=&DWP(32,"esp");
  42. $K256="ebp";
  43. sub BODY_00_15() {
  44. my $in_16_63=shift;
  45. &mov ("ecx",$E);
  46. &add ($T,"edi") if ($in_16_63); # T += sigma1(X[-2])
  47. &ror ("ecx",25-11);
  48. &mov ("esi",$Foff);
  49. &xor ("ecx",$E);
  50. &ror ("ecx",11-6);
  51. &mov (&DWP(4*(8+15),"esp"),$T) if ($in_16_63); # save X[0]
  52. &xor ("ecx",$E);
  53. &ror ("ecx",6); # Sigma1(e)
  54. &mov ("edi",$Goff);
  55. &add ($T,"ecx"); # T += Sigma1(e)
  56. &xor ("esi","edi");
  57. &mov ($Eoff,$E); # modulo-scheduled
  58. &mov ("ecx",$A);
  59. &and ("esi",$E);
  60. &mov ($E,$Doff); # e becomes d, which is e in next iteration
  61. &xor ("esi","edi"); # Ch(e,f,g)
  62. &mov ("edi",$A);
  63. &add ($T,"esi"); # T += Ch(e,f,g)
  64. &ror ("ecx",22-13);
  65. &add ($T,$Hoff); # T += h
  66. &xor ("ecx",$A);
  67. &ror ("ecx",13-2);
  68. &mov ("esi",$Boff);
  69. &xor ("ecx",$A);
  70. &ror ("ecx",2); # Sigma0(a)
  71. &add ($E,$T); # d += T
  72. &mov ("edi",$Coff);
  73. &add ($T,"ecx"); # T += Sigma0(a)
  74. &mov ($Aoff,$A); # modulo-scheduled
  75. &mov ("ecx",$A);
  76. &sub ("esp",4);
  77. &or ($A,"esi"); # a becomes h, which is a in next iteration
  78. &and ("ecx","esi");
  79. &and ($A,"edi");
  80. &mov ("esi",&DWP(0,$K256));
  81. &or ($A,"ecx"); # h=Maj(a,b,c)
  82. &add ($K256,4);
  83. &add ($A,$T); # h += T
  84. &mov ($T,&DWP(4*(8+15+16-1),"esp")) if ($in_16_63); # preload T
  85. &add ($E,"esi"); # d += K256[i]
  86. &add ($A,"esi"); # h += K256[i]
  87. }
  88. &function_begin("sha256_block_data_order");
  89. &mov ("esi",wparam(0)); # ctx
  90. &mov ("edi",wparam(1)); # inp
  91. &mov ("eax",wparam(2)); # num
  92. &mov ("ebx","esp"); # saved sp
  93. &call (&label("pic_point")); # make it PIC!
  94. &set_label("pic_point");
  95. &blindpop($K256);
  96. &lea ($K256,&DWP(&label("K256")."-".&label("pic_point"),$K256));
  97. &sub ("esp",16);
  98. &and ("esp",-64);
  99. &shl ("eax",6);
  100. &add ("eax","edi");
  101. &mov (&DWP(0,"esp"),"esi"); # ctx
  102. &mov (&DWP(4,"esp"),"edi"); # inp
  103. &mov (&DWP(8,"esp"),"eax"); # inp+num*128
  104. &mov (&DWP(12,"esp"),"ebx"); # saved sp
  105. &set_label("loop",16);
  106. # copy input block to stack reversing byte and dword order
  107. for($i=0;$i<4;$i++) {
  108. &mov ("eax",&DWP($i*16+0,"edi"));
  109. &mov ("ebx",&DWP($i*16+4,"edi"));
  110. &mov ("ecx",&DWP($i*16+8,"edi"));
  111. &mov ("edx",&DWP($i*16+12,"edi"));
  112. &bswap ("eax");
  113. &bswap ("ebx");
  114. &bswap ("ecx");
  115. &bswap ("edx");
  116. &push ("eax");
  117. &push ("ebx");
  118. &push ("ecx");
  119. &push ("edx");
  120. }
  121. &add ("edi",64);
  122. &sub ("esp",4*8); # place for A,B,C,D,E,F,G,H
  123. &mov (&DWP(4*(8+16)+4,"esp"),"edi");
  124. # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
  125. &mov ($A,&DWP(0,"esi"));
  126. &mov ("ebx",&DWP(4,"esi"));
  127. &mov ("ecx",&DWP(8,"esi"));
  128. &mov ("edi",&DWP(12,"esi"));
  129. # &mov ($Aoff,$A);
  130. &mov ($Boff,"ebx");
  131. &mov ($Coff,"ecx");
  132. &mov ($Doff,"edi");
  133. &mov ($E,&DWP(16,"esi"));
  134. &mov ("ebx",&DWP(20,"esi"));
  135. &mov ("ecx",&DWP(24,"esi"));
  136. &mov ("edi",&DWP(28,"esi"));
  137. # &mov ($Eoff,$E);
  138. &mov ($Foff,"ebx");
  139. &mov ($Goff,"ecx");
  140. &mov ($Hoff,"edi");
  141. &set_label("00_15",16);
  142. &mov ($T,&DWP(4*(8+15),"esp"));
  143. &BODY_00_15();
  144. &cmp ("esi",0xc19bf174);
  145. &jne (&label("00_15"));
  146. &mov ($T,&DWP(4*(8+15+16-1),"esp")); # preloaded in BODY_00_15(1)
  147. &set_label("16_63",16);
  148. &mov ("esi",$T);
  149. &mov ("ecx",&DWP(4*(8+15+16-14),"esp"));
  150. &ror ("esi",18-7);
  151. &mov ("edi","ecx");
  152. &xor ("esi",$T);
  153. &ror ("esi",7);
  154. &shr ($T,3);
  155. &ror ("edi",19-17);
  156. &xor ($T,"esi"); # T = sigma0(X[-15])
  157. &xor ("edi","ecx");
  158. &ror ("edi",17);
  159. &shr ("ecx",10);
  160. &add ($T,&DWP(4*(8+15+16),"esp")); # T += X[-16]
  161. &xor ("edi","ecx"); # sigma1(X[-2])
  162. &add ($T,&DWP(4*(8+15+16-9),"esp")); # T += X[-7]
  163. # &add ($T,"edi"); # T += sigma1(X[-2])
  164. # &mov (&DWP(4*(8+15),"esp"),$T); # save X[0]
  165. &BODY_00_15(1);
  166. &cmp ("esi",0xc67178f2);
  167. &jne (&label("16_63"));
  168. &mov ("esi",&DWP(4*(8+16+64)+0,"esp"));#ctx
  169. # &mov ($A,$Aoff);
  170. &mov ("ebx",$Boff);
  171. &mov ("ecx",$Coff);
  172. &mov ("edi",$Doff);
  173. &add ($A,&DWP(0,"esi"));
  174. &add ("ebx",&DWP(4,"esi"));
  175. &add ("ecx",&DWP(8,"esi"));
  176. &add ("edi",&DWP(12,"esi"));
  177. &mov (&DWP(0,"esi"),$A);
  178. &mov (&DWP(4,"esi"),"ebx");
  179. &mov (&DWP(8,"esi"),"ecx");
  180. &mov (&DWP(12,"esi"),"edi");
  181. # &mov ($E,$Eoff);
  182. &mov ("eax",$Foff);
  183. &mov ("ebx",$Goff);
  184. &mov ("ecx",$Hoff);
  185. &mov ("edi",&DWP(4*(8+16+64)+4,"esp"));#inp
  186. &add ($E,&DWP(16,"esi"));
  187. &add ("eax",&DWP(20,"esi"));
  188. &add ("ebx",&DWP(24,"esi"));
  189. &add ("ecx",&DWP(28,"esi"));
  190. &mov (&DWP(16,"esi"),$E);
  191. &mov (&DWP(20,"esi"),"eax");
  192. &mov (&DWP(24,"esi"),"ebx");
  193. &mov (&DWP(28,"esi"),"ecx");
  194. &add ("esp",4*(8+16+64)); # destroy frame
  195. &sub ($K256,4*64); # rewind K
  196. &cmp ("edi",&DWP(8,"esp")); # are we done yet?
  197. &jb (&label("loop"));
  198. &mov ("esp",&DWP(12,"esp")); # restore sp
  199. &function_end_A();
  200. &set_label("K256",64); # Yes! I keep it in the code segment!
  201. &data_word(0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5);
  202. &data_word(0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5);
  203. &data_word(0xd807aa98,0x12835b01,0x243185be,0x550c7dc3);
  204. &data_word(0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174);
  205. &data_word(0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc);
  206. &data_word(0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da);
  207. &data_word(0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7);
  208. &data_word(0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967);
  209. &data_word(0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13);
  210. &data_word(0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85);
  211. &data_word(0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3);
  212. &data_word(0xd192e819,0xd6990624,0xf40e3585,0x106aa070);
  213. &data_word(0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5);
  214. &data_word(0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3);
  215. &data_word(0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208);
  216. &data_word(0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2);
  217. &function_end_B("sha256_block_data_order");
  218. &asciz("SHA256 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
  219. &asm_finish();