sha1-armv8.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #! /usr/bin/env perl
  2. # Copyright 2014-2016 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. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # SHA1 for ARMv8.
  17. #
  18. # Performance in cycles per processed byte and improvement coefficient
  19. # over code generated with "default" compiler:
  20. #
  21. # hardware-assisted software(*)
  22. # Apple A7 2.31 4.13 (+14%)
  23. # Cortex-A53 2.24 8.03 (+97%)
  24. # Cortex-A57 2.35 7.88 (+74%)
  25. # Denver 2.13 3.97 (+0%)(**)
  26. # X-Gene 8.80 (+200%)
  27. # Mongoose 2.05 6.50 (+160%)
  28. # Kryo 1.88 8.00 (+90%)
  29. #
  30. # (*) Software results are presented mostly for reference purposes.
  31. # (**) Keep in mind that Denver relies on binary translation, which
  32. # optimizes compiler output at run-time.
  33. $flavour = shift;
  34. $output = shift;
  35. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  36. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  37. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  38. die "can't locate arm-xlate.pl";
  39. open OUT,"| \"$^X\" $xlate $flavour $output";
  40. *STDOUT=*OUT;
  41. ($ctx,$inp,$num)=("x0","x1","x2");
  42. @Xw=map("w$_",(3..17,19));
  43. @Xx=map("x$_",(3..17,19));
  44. @V=($A,$B,$C,$D,$E)=map("w$_",(20..24));
  45. ($t0,$t1,$t2,$K)=map("w$_",(25..28));
  46. sub BODY_00_19 {
  47. my ($i,$a,$b,$c,$d,$e)=@_;
  48. my $j=($i+2)&15;
  49. $code.=<<___ if ($i<15 && !($i&1));
  50. lsr @Xx[$i+1],@Xx[$i],#32
  51. ___
  52. $code.=<<___ if ($i<14 && !($i&1));
  53. ldr @Xx[$i+2],[$inp,#`($i+2)*4-64`]
  54. ___
  55. $code.=<<___ if ($i<14 && ($i&1));
  56. #ifdef __ARMEB__
  57. ror @Xx[$i+1],@Xx[$i+1],#32
  58. #else
  59. rev32 @Xx[$i+1],@Xx[$i+1]
  60. #endif
  61. ___
  62. $code.=<<___ if ($i<14);
  63. bic $t0,$d,$b
  64. and $t1,$c,$b
  65. ror $t2,$a,#27
  66. add $d,$d,$K // future e+=K
  67. orr $t0,$t0,$t1
  68. add $e,$e,$t2 // e+=rot(a,5)
  69. ror $b,$b,#2
  70. add $d,$d,@Xw[($i+1)&15] // future e+=X[i]
  71. add $e,$e,$t0 // e+=F(b,c,d)
  72. ___
  73. $code.=<<___ if ($i==19);
  74. movz $K,#0xeba1
  75. movk $K,#0x6ed9,lsl#16
  76. ___
  77. $code.=<<___ if ($i>=14);
  78. eor @Xw[$j],@Xw[$j],@Xw[($j+2)&15]
  79. bic $t0,$d,$b
  80. and $t1,$c,$b
  81. ror $t2,$a,#27
  82. eor @Xw[$j],@Xw[$j],@Xw[($j+8)&15]
  83. add $d,$d,$K // future e+=K
  84. orr $t0,$t0,$t1
  85. add $e,$e,$t2 // e+=rot(a,5)
  86. eor @Xw[$j],@Xw[$j],@Xw[($j+13)&15]
  87. ror $b,$b,#2
  88. add $d,$d,@Xw[($i+1)&15] // future e+=X[i]
  89. add $e,$e,$t0 // e+=F(b,c,d)
  90. ror @Xw[$j],@Xw[$j],#31
  91. ___
  92. }
  93. sub BODY_40_59 {
  94. my ($i,$a,$b,$c,$d,$e)=@_;
  95. my $j=($i+2)&15;
  96. $code.=<<___ if ($i==59);
  97. movz $K,#0xc1d6
  98. movk $K,#0xca62,lsl#16
  99. ___
  100. $code.=<<___;
  101. orr $t0,$b,$c
  102. and $t1,$b,$c
  103. eor @Xw[$j],@Xw[$j],@Xw[($j+2)&15]
  104. ror $t2,$a,#27
  105. and $t0,$t0,$d
  106. add $d,$d,$K // future e+=K
  107. eor @Xw[$j],@Xw[$j],@Xw[($j+8)&15]
  108. add $e,$e,$t2 // e+=rot(a,5)
  109. orr $t0,$t0,$t1
  110. ror $b,$b,#2
  111. eor @Xw[$j],@Xw[$j],@Xw[($j+13)&15]
  112. add $d,$d,@Xw[($i+1)&15] // future e+=X[i]
  113. add $e,$e,$t0 // e+=F(b,c,d)
  114. ror @Xw[$j],@Xw[$j],#31
  115. ___
  116. }
  117. sub BODY_20_39 {
  118. my ($i,$a,$b,$c,$d,$e)=@_;
  119. my $j=($i+2)&15;
  120. $code.=<<___ if ($i==39);
  121. movz $K,#0xbcdc
  122. movk $K,#0x8f1b,lsl#16
  123. ___
  124. $code.=<<___ if ($i<78);
  125. eor @Xw[$j],@Xw[$j],@Xw[($j+2)&15]
  126. eor $t0,$d,$b
  127. ror $t2,$a,#27
  128. add $d,$d,$K // future e+=K
  129. eor @Xw[$j],@Xw[$j],@Xw[($j+8)&15]
  130. eor $t0,$t0,$c
  131. add $e,$e,$t2 // e+=rot(a,5)
  132. ror $b,$b,#2
  133. eor @Xw[$j],@Xw[$j],@Xw[($j+13)&15]
  134. add $d,$d,@Xw[($i+1)&15] // future e+=X[i]
  135. add $e,$e,$t0 // e+=F(b,c,d)
  136. ror @Xw[$j],@Xw[$j],#31
  137. ___
  138. $code.=<<___ if ($i==78);
  139. ldp @Xw[1],@Xw[2],[$ctx]
  140. eor $t0,$d,$b
  141. ror $t2,$a,#27
  142. add $d,$d,$K // future e+=K
  143. eor $t0,$t0,$c
  144. add $e,$e,$t2 // e+=rot(a,5)
  145. ror $b,$b,#2
  146. add $d,$d,@Xw[($i+1)&15] // future e+=X[i]
  147. add $e,$e,$t0 // e+=F(b,c,d)
  148. ___
  149. $code.=<<___ if ($i==79);
  150. ldp @Xw[3],@Xw[4],[$ctx,#8]
  151. eor $t0,$d,$b
  152. ror $t2,$a,#27
  153. eor $t0,$t0,$c
  154. add $e,$e,$t2 // e+=rot(a,5)
  155. ror $b,$b,#2
  156. ldr @Xw[5],[$ctx,#16]
  157. add $e,$e,$t0 // e+=F(b,c,d)
  158. ___
  159. }
  160. $code.=<<___;
  161. #include "arm_arch.h"
  162. .text
  163. .extern OPENSSL_armcap_P
  164. .globl sha1_block_data_order
  165. .type sha1_block_data_order,%function
  166. .align 6
  167. sha1_block_data_order:
  168. #ifdef __ILP32__
  169. ldrsw x16,.LOPENSSL_armcap_P
  170. #else
  171. ldr x16,.LOPENSSL_armcap_P
  172. #endif
  173. adr x17,.LOPENSSL_armcap_P
  174. add x16,x16,x17
  175. ldr w16,[x16]
  176. tst w16,#ARMV8_SHA1
  177. b.ne .Lv8_entry
  178. stp x29,x30,[sp,#-96]!
  179. add x29,sp,#0
  180. stp x19,x20,[sp,#16]
  181. stp x21,x22,[sp,#32]
  182. stp x23,x24,[sp,#48]
  183. stp x25,x26,[sp,#64]
  184. stp x27,x28,[sp,#80]
  185. ldp $A,$B,[$ctx]
  186. ldp $C,$D,[$ctx,#8]
  187. ldr $E,[$ctx,#16]
  188. .Loop:
  189. ldr @Xx[0],[$inp],#64
  190. movz $K,#0x7999
  191. sub $num,$num,#1
  192. movk $K,#0x5a82,lsl#16
  193. #ifdef __ARMEB__
  194. ror $Xx[0],@Xx[0],#32
  195. #else
  196. rev32 @Xx[0],@Xx[0]
  197. #endif
  198. add $E,$E,$K // warm it up
  199. add $E,$E,@Xw[0]
  200. ___
  201. for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
  202. for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  203. for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  204. for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  205. $code.=<<___;
  206. add $B,$B,@Xw[2]
  207. add $C,$C,@Xw[3]
  208. add $A,$A,@Xw[1]
  209. add $D,$D,@Xw[4]
  210. add $E,$E,@Xw[5]
  211. stp $A,$B,[$ctx]
  212. stp $C,$D,[$ctx,#8]
  213. str $E,[$ctx,#16]
  214. cbnz $num,.Loop
  215. ldp x19,x20,[sp,#16]
  216. ldp x21,x22,[sp,#32]
  217. ldp x23,x24,[sp,#48]
  218. ldp x25,x26,[sp,#64]
  219. ldp x27,x28,[sp,#80]
  220. ldr x29,[sp],#96
  221. ret
  222. .size sha1_block_data_order,.-sha1_block_data_order
  223. ___
  224. {{{
  225. my ($ABCD,$E,$E0,$E1)=map("v$_.16b",(0..3));
  226. my @MSG=map("v$_.16b",(4..7));
  227. my @Kxx=map("v$_.4s",(16..19));
  228. my ($W0,$W1)=("v20.4s","v21.4s");
  229. my $ABCD_SAVE="v22.16b";
  230. $code.=<<___;
  231. .type sha1_block_armv8,%function
  232. .align 6
  233. sha1_block_armv8:
  234. .Lv8_entry:
  235. stp x29,x30,[sp,#-16]!
  236. add x29,sp,#0
  237. adr x4,.Lconst
  238. eor $E,$E,$E
  239. ld1.32 {$ABCD},[$ctx],#16
  240. ld1.32 {$E}[0],[$ctx]
  241. sub $ctx,$ctx,#16
  242. ld1.32 {@Kxx[0]-@Kxx[3]},[x4]
  243. .Loop_hw:
  244. ld1 {@MSG[0]-@MSG[3]},[$inp],#64
  245. sub $num,$num,#1
  246. rev32 @MSG[0],@MSG[0]
  247. rev32 @MSG[1],@MSG[1]
  248. add.i32 $W0,@Kxx[0],@MSG[0]
  249. rev32 @MSG[2],@MSG[2]
  250. orr $ABCD_SAVE,$ABCD,$ABCD // offload
  251. add.i32 $W1,@Kxx[0],@MSG[1]
  252. rev32 @MSG[3],@MSG[3]
  253. sha1h $E1,$ABCD
  254. sha1c $ABCD,$E,$W0 // 0
  255. add.i32 $W0,@Kxx[$j],@MSG[2]
  256. sha1su0 @MSG[0],@MSG[1],@MSG[2]
  257. ___
  258. for ($j=0,$i=1;$i<20-3;$i++) {
  259. my $f=("c","p","m","p")[$i/5];
  260. $code.=<<___;
  261. sha1h $E0,$ABCD // $i
  262. sha1$f $ABCD,$E1,$W1
  263. add.i32 $W1,@Kxx[$j],@MSG[3]
  264. sha1su1 @MSG[0],@MSG[3]
  265. ___
  266. $code.=<<___ if ($i<20-4);
  267. sha1su0 @MSG[1],@MSG[2],@MSG[3]
  268. ___
  269. ($E0,$E1)=($E1,$E0); ($W0,$W1)=($W1,$W0);
  270. push(@MSG,shift(@MSG)); $j++ if ((($i+3)%5)==0);
  271. }
  272. $code.=<<___;
  273. sha1h $E0,$ABCD // $i
  274. sha1p $ABCD,$E1,$W1
  275. add.i32 $W1,@Kxx[$j],@MSG[3]
  276. sha1h $E1,$ABCD // 18
  277. sha1p $ABCD,$E0,$W0
  278. sha1h $E0,$ABCD // 19
  279. sha1p $ABCD,$E1,$W1
  280. add.i32 $E,$E,$E0
  281. add.i32 $ABCD,$ABCD,$ABCD_SAVE
  282. cbnz $num,.Loop_hw
  283. st1.32 {$ABCD},[$ctx],#16
  284. st1.32 {$E}[0],[$ctx]
  285. ldr x29,[sp],#16
  286. ret
  287. .size sha1_block_armv8,.-sha1_block_armv8
  288. .align 6
  289. .Lconst:
  290. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 //K_00_19
  291. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 //K_20_39
  292. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc //K_40_59
  293. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 //K_60_79
  294. .LOPENSSL_armcap_P:
  295. #ifdef __ILP32__
  296. .long OPENSSL_armcap_P-.
  297. #else
  298. .quad OPENSSL_armcap_P-.
  299. #endif
  300. .asciz "SHA1 block transform for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  301. .align 2
  302. .comm OPENSSL_armcap_P,4,4
  303. ___
  304. }}}
  305. { my %opcode = (
  306. "sha1c" => 0x5e000000, "sha1p" => 0x5e001000,
  307. "sha1m" => 0x5e002000, "sha1su0" => 0x5e003000,
  308. "sha1h" => 0x5e280800, "sha1su1" => 0x5e281800 );
  309. sub unsha1 {
  310. my ($mnemonic,$arg)=@_;
  311. $arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv]([0-9]+))?/o
  312. &&
  313. sprintf ".inst\t0x%08x\t//%s %s",
  314. $opcode{$mnemonic}|$1|($2<<5)|($3<<16),
  315. $mnemonic,$arg;
  316. }
  317. }
  318. foreach(split("\n",$code)) {
  319. s/\`([^\`]*)\`/eval($1)/geo;
  320. s/\b(sha1\w+)\s+([qv].*)/unsha1($1,$2)/geo;
  321. s/\.\w?32\b//o and s/\.16b/\.4s/go;
  322. m/(ld|st)1[^\[]+\[0\]/o and s/\.4s/\.s/go;
  323. print $_,"\n";
  324. }
  325. close STDOUT;