sha1-armv8.pl 8.4 KB

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