sha256-586.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 35 20 20
  18. # x86_64 asm(*) - - 21 15.8 16.5
  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,&DWP(4*(8+15+16-9),"esp")) if ($in_16_63); # T += X[-7]
  47. &ror ("ecx",6);
  48. &mov ("edi",$E);
  49. &ror ("edi",11);
  50. &mov ("esi",$Foff);
  51. &xor ("ecx","edi");
  52. &ror ("edi",25-11);
  53. &mov (&DWP(4*(8+15),"esp"),$T) if ($in_16_63); # save X[0]
  54. &xor ("ecx","edi"); # Sigma1(e)
  55. &mov ("edi",$Goff);
  56. &add ($T,"ecx"); # T += Sigma1(e)
  57. &mov ($Eoff,$E); # modulo-scheduled
  58. &xor ("esi","edi");
  59. &mov ("ecx",$A);
  60. &and ("esi",$E);
  61. &mov ($E,$Doff); # e becomes d, which is e in next iteration
  62. &xor ("esi","edi"); # Ch(e,f,g)
  63. &mov ("edi",$A);
  64. &add ($T,"esi"); # T += Ch(e,f,g)
  65. &ror ("ecx",2);
  66. &add ($T,$Hoff); # T += h
  67. &ror ("edi",13);
  68. &mov ("esi",$Boff);
  69. &xor ("ecx","edi");
  70. &ror ("edi",22-13);
  71. &add ($E,$T); # d += T
  72. &xor ("ecx","edi"); # Sigma0(a)
  73. &mov ("edi",$Coff);
  74. &add ($T,"ecx"); # T += Sigma0(a)
  75. &mov ($Aoff,$A); # modulo-scheduled
  76. &mov ("ecx",$A);
  77. &sub ("esp",4);
  78. &or ($A,"esi"); # a becomes h, which is a in next iteration
  79. &and ("ecx","esi");
  80. &and ($A,"edi");
  81. &mov ("esi",&DWP(0,$K256));
  82. &or ($A,"ecx"); # h=Maj(a,b,c)
  83. &add ($K256,4);
  84. &add ($A,$T); # h += T
  85. &mov ($T,&DWP(4*(8+15+16-1),"esp")) if ($in_16_63); # preload T
  86. &add ($E,"esi"); # d += K256[i]
  87. &add ($A,"esi"); # h += K256[i]
  88. }
  89. &function_begin("sha256_block_data_order",16);
  90. &mov ("esi",wparam(0)); # ctx
  91. &mov ("edi",wparam(1)); # inp
  92. &mov ("eax",wparam(2)); # num
  93. &mov ("ebx","esp"); # saved sp
  94. &call (&label("pic_point")); # make it PIC!
  95. &set_label("pic_point");
  96. &blindpop($K256);
  97. &lea ($K256,&DWP(&label("K256")."-".&label("pic_point"),$K256));
  98. &sub ("esp",16);
  99. &and ("esp",-64);
  100. &shl ("eax",6);
  101. &add ("eax","edi");
  102. &mov (&DWP(0,"esp"),"esi"); # ctx
  103. &mov (&DWP(4,"esp"),"edi"); # inp
  104. &mov (&DWP(8,"esp"),"eax"); # inp+num*128
  105. &mov (&DWP(12,"esp"),"ebx"); # saved sp
  106. &set_label("loop",16);
  107. # copy input block to stack reversing byte and dword order
  108. for($i=0;$i<4;$i++) {
  109. &mov ("eax",&DWP($i*16+0,"edi"));
  110. &mov ("ebx",&DWP($i*16+4,"edi"));
  111. &mov ("ecx",&DWP($i*16+8,"edi"));
  112. &mov ("edx",&DWP($i*16+12,"edi"));
  113. &bswap ("eax");
  114. &bswap ("ebx");
  115. &bswap ("ecx");
  116. &bswap ("edx");
  117. &push ("eax");
  118. &push ("ebx");
  119. &push ("ecx");
  120. &push ("edx");
  121. }
  122. &add ("edi",64);
  123. &sub ("esp",4*8); # place for A,B,C,D,E,F,G,H
  124. &mov (&DWP(4*(8+16)+4,"esp"),"edi");
  125. # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
  126. &mov ($A,&DWP(0,"esi"));
  127. &mov ("ebx",&DWP(4,"esi"));
  128. &mov ("ecx",&DWP(8,"esi"));
  129. &mov ("edi",&DWP(12,"esi"));
  130. # &mov ($Aoff,$A);
  131. &mov ($Boff,"ebx");
  132. &mov ($Coff,"ecx");
  133. &mov ($Doff,"edi");
  134. &mov ($E,&DWP(16,"esi"));
  135. &mov ("ebx",&DWP(20,"esi"));
  136. &mov ("ecx",&DWP(24,"esi"));
  137. &mov ("edi",&DWP(28,"esi"));
  138. # &mov ($Eoff,$E);
  139. &mov ($Foff,"ebx");
  140. &mov ($Goff,"ecx");
  141. &mov ($Hoff,"edi");
  142. &set_label("00_15",16);
  143. &mov ($T,&DWP(4*(8+15),"esp"));
  144. &BODY_00_15();
  145. &cmp ("esi",0xc19bf174);
  146. &jne (&label("00_15"));
  147. &mov ($T,&DWP(4*(8+15+16-1),"esp")); # preloaded in BODY_00_15(1)
  148. &set_label("16_63",16);
  149. &mov ("esi",$T);
  150. &mov ("ecx",&DWP(4*(8+15+16-14),"esp"));
  151. &shr ($T,3);
  152. &ror ("esi",7);
  153. &xor ($T,"esi");
  154. &ror ("esi",18-7);
  155. &mov ("edi","ecx");
  156. &xor ($T,"esi"); # T = sigma0(X[-15])
  157. &shr ("ecx",10);
  158. &mov ("esi",&DWP(4*(8+15+16),"esp"));
  159. &ror ("edi",17);
  160. &xor ("ecx","edi");
  161. &ror ("edi",19-17);
  162. &add ($T,"esi"); # T += X[-16]
  163. &xor ("edi","ecx") # sigma1(X[-2])
  164. &add ($T,"edi"); # T += sigma1(X[-2])
  165. # &add ($T,&DWP(4*(8+15+16-9),"esp")); # T += X[-7], moved to BODY_00_15(1)
  166. # &mov (&DWP(4*(8+15),"esp"),$T); # save X[0]
  167. &BODY_00_15(1);
  168. &cmp ("esi",0xc67178f2);
  169. &jne (&label("16_63"));
  170. &mov ("esi",&DWP(4*(8+16+64)+0,"esp"));#ctx
  171. # &mov ($A,$Aoff);
  172. &mov ("ebx",$Boff);
  173. &mov ("ecx",$Coff);
  174. &mov ("edi",$Doff);
  175. &add ($A,&DWP(0,"esi"));
  176. &add ("ebx",&DWP(4,"esi"));
  177. &add ("ecx",&DWP(8,"esi"));
  178. &add ("edi",&DWP(12,"esi"));
  179. &mov (&DWP(0,"esi"),$A);
  180. &mov (&DWP(4,"esi"),"ebx");
  181. &mov (&DWP(8,"esi"),"ecx");
  182. &mov (&DWP(12,"esi"),"edi");
  183. # &mov ($E,$Eoff);
  184. &mov ("eax",$Foff);
  185. &mov ("ebx",$Goff);
  186. &mov ("ecx",$Hoff);
  187. &mov ("edi",&DWP(4*(8+16+64)+4,"esp"));#inp
  188. &add ($E,&DWP(16,"esi"));
  189. &add ("eax",&DWP(20,"esi"));
  190. &add ("ebx",&DWP(24,"esi"));
  191. &add ("ecx",&DWP(28,"esi"));
  192. &mov (&DWP(16,"esi"),$E);
  193. &mov (&DWP(20,"esi"),"eax");
  194. &mov (&DWP(24,"esi"),"ebx");
  195. &mov (&DWP(28,"esi"),"ecx");
  196. &add ("esp",4*(8+16+64)); # destroy frame
  197. &sub ($K256,4*64); # rewind K
  198. &cmp ("edi",&DWP(8,"esp")); # are we done yet?
  199. &jb (&label("loop"));
  200. &mov ("esp",&DWP(12,"esp")); # restore sp
  201. &function_end_A();
  202. &set_label("K256",64); # Yes! I keep it in the code segment!
  203. &data_word(0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5);
  204. &data_word(0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5);
  205. &data_word(0xd807aa98,0x12835b01,0x243185be,0x550c7dc3);
  206. &data_word(0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174);
  207. &data_word(0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc);
  208. &data_word(0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da);
  209. &data_word(0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7);
  210. &data_word(0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967);
  211. &data_word(0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13);
  212. &data_word(0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85);
  213. &data_word(0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3);
  214. &data_word(0xd192e819,0xd6990624,0xf40e3585,0x106aa070);
  215. &data_word(0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5);
  216. &data_word(0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3);
  217. &data_word(0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208);
  218. &data_word(0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2);
  219. &function_end_B("sha256_block_data_order");
  220. &asciz("SHA256 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
  221. &asm_finish();