sha256-armv4.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #! /usr/bin/env perl
  2. # Copyright 2007-2018 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. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. #
  14. # Permission to use under GPL terms is granted.
  15. # ====================================================================
  16. # SHA256 block procedure for ARMv4. May 2007.
  17. # Performance is ~2x better than gcc 3.4 generated code and in "abso-
  18. # lute" terms is ~2250 cycles per 64-byte block or ~35 cycles per
  19. # byte [on single-issue Xscale PXA250 core].
  20. # July 2010.
  21. #
  22. # Rescheduling for dual-issue pipeline resulted in 22% improvement on
  23. # Cortex A8 core and ~20 cycles per processed byte.
  24. # February 2011.
  25. #
  26. # Profiler-assisted and platform-specific optimization resulted in 16%
  27. # improvement on Cortex A8 core and ~15.4 cycles per processed byte.
  28. # September 2013.
  29. #
  30. # Add NEON implementation. On Cortex A8 it was measured to process one
  31. # byte in 12.5 cycles or 23% faster than integer-only code. Snapdragon
  32. # S4 does it in 12.5 cycles too, but it's 50% faster than integer-only
  33. # code (meaning that latter performs sub-optimally, nothing was done
  34. # about it).
  35. # May 2014.
  36. #
  37. # Add ARMv8 code path performing at 2.0 cpb on Apple A7.
  38. $flavour = shift;
  39. if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
  40. else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
  41. if ($flavour && $flavour ne "void") {
  42. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  43. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  44. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  45. die "can't locate arm-xlate.pl";
  46. open STDOUT,"| \"$^X\" $xlate $flavour $output";
  47. } else {
  48. open STDOUT,">$output";
  49. }
  50. $ctx="r0"; $t0="r0";
  51. $inp="r1"; $t4="r1";
  52. $len="r2"; $t1="r2";
  53. $T1="r3"; $t3="r3";
  54. $A="r4";
  55. $B="r5";
  56. $C="r6";
  57. $D="r7";
  58. $E="r8";
  59. $F="r9";
  60. $G="r10";
  61. $H="r11";
  62. @V=($A,$B,$C,$D,$E,$F,$G,$H);
  63. $t2="r12";
  64. $Ktbl="r14";
  65. @Sigma0=( 2,13,22);
  66. @Sigma1=( 6,11,25);
  67. @sigma0=( 7,18, 3);
  68. @sigma1=(17,19,10);
  69. sub BODY_00_15 {
  70. my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
  71. $code.=<<___ if ($i<16);
  72. #if __ARM_ARCH__>=7
  73. @ ldr $t1,[$inp],#4 @ $i
  74. # if $i==15
  75. str $inp,[sp,#17*4] @ make room for $t4
  76. # endif
  77. eor $t0,$e,$e,ror#`$Sigma1[1]-$Sigma1[0]`
  78. add $a,$a,$t2 @ h+=Maj(a,b,c) from the past
  79. eor $t0,$t0,$e,ror#`$Sigma1[2]-$Sigma1[0]` @ Sigma1(e)
  80. # ifndef __ARMEB__
  81. rev $t1,$t1
  82. # endif
  83. #else
  84. @ ldrb $t1,[$inp,#3] @ $i
  85. add $a,$a,$t2 @ h+=Maj(a,b,c) from the past
  86. ldrb $t2,[$inp,#2]
  87. ldrb $t0,[$inp,#1]
  88. orr $t1,$t1,$t2,lsl#8
  89. ldrb $t2,[$inp],#4
  90. orr $t1,$t1,$t0,lsl#16
  91. # if $i==15
  92. str $inp,[sp,#17*4] @ make room for $t4
  93. # endif
  94. eor $t0,$e,$e,ror#`$Sigma1[1]-$Sigma1[0]`
  95. orr $t1,$t1,$t2,lsl#24
  96. eor $t0,$t0,$e,ror#`$Sigma1[2]-$Sigma1[0]` @ Sigma1(e)
  97. #endif
  98. ___
  99. $code.=<<___;
  100. ldr $t2,[$Ktbl],#4 @ *K256++
  101. add $h,$h,$t1 @ h+=X[i]
  102. str $t1,[sp,#`$i%16`*4]
  103. eor $t1,$f,$g
  104. add $h,$h,$t0,ror#$Sigma1[0] @ h+=Sigma1(e)
  105. and $t1,$t1,$e
  106. add $h,$h,$t2 @ h+=K256[i]
  107. eor $t1,$t1,$g @ Ch(e,f,g)
  108. eor $t0,$a,$a,ror#`$Sigma0[1]-$Sigma0[0]`
  109. add $h,$h,$t1 @ h+=Ch(e,f,g)
  110. #if $i==31
  111. and $t2,$t2,#0xff
  112. cmp $t2,#0xf2 @ done?
  113. #endif
  114. #if $i<15
  115. # if __ARM_ARCH__>=7
  116. ldr $t1,[$inp],#4 @ prefetch
  117. # else
  118. ldrb $t1,[$inp,#3]
  119. # endif
  120. eor $t2,$a,$b @ a^b, b^c in next round
  121. #else
  122. ldr $t1,[sp,#`($i+2)%16`*4] @ from future BODY_16_xx
  123. eor $t2,$a,$b @ a^b, b^c in next round
  124. ldr $t4,[sp,#`($i+15)%16`*4] @ from future BODY_16_xx
  125. #endif
  126. eor $t0,$t0,$a,ror#`$Sigma0[2]-$Sigma0[0]` @ Sigma0(a)
  127. and $t3,$t3,$t2 @ (b^c)&=(a^b)
  128. add $d,$d,$h @ d+=h
  129. eor $t3,$t3,$b @ Maj(a,b,c)
  130. add $h,$h,$t0,ror#$Sigma0[0] @ h+=Sigma0(a)
  131. @ add $h,$h,$t3 @ h+=Maj(a,b,c)
  132. ___
  133. ($t2,$t3)=($t3,$t2);
  134. }
  135. sub BODY_16_XX {
  136. my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
  137. $code.=<<___;
  138. @ ldr $t1,[sp,#`($i+1)%16`*4] @ $i
  139. @ ldr $t4,[sp,#`($i+14)%16`*4]
  140. mov $t0,$t1,ror#$sigma0[0]
  141. add $a,$a,$t2 @ h+=Maj(a,b,c) from the past
  142. mov $t2,$t4,ror#$sigma1[0]
  143. eor $t0,$t0,$t1,ror#$sigma0[1]
  144. eor $t2,$t2,$t4,ror#$sigma1[1]
  145. eor $t0,$t0,$t1,lsr#$sigma0[2] @ sigma0(X[i+1])
  146. ldr $t1,[sp,#`($i+0)%16`*4]
  147. eor $t2,$t2,$t4,lsr#$sigma1[2] @ sigma1(X[i+14])
  148. ldr $t4,[sp,#`($i+9)%16`*4]
  149. add $t2,$t2,$t0
  150. eor $t0,$e,$e,ror#`$Sigma1[1]-$Sigma1[0]` @ from BODY_00_15
  151. add $t1,$t1,$t2
  152. eor $t0,$t0,$e,ror#`$Sigma1[2]-$Sigma1[0]` @ Sigma1(e)
  153. add $t1,$t1,$t4 @ X[i]
  154. ___
  155. &BODY_00_15(@_);
  156. }
  157. $code=<<___;
  158. #ifndef __KERNEL__
  159. # include "arm_arch.h"
  160. #else
  161. # define __ARM_ARCH__ __LINUX_ARM_ARCH__
  162. # define __ARM_MAX_ARCH__ 7
  163. #endif
  164. .text
  165. #if defined(__thumb2__)
  166. .syntax unified
  167. .thumb
  168. #else
  169. .code 32
  170. #endif
  171. .type K256,%object
  172. .align 5
  173. K256:
  174. .word 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
  175. .word 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
  176. .word 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
  177. .word 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
  178. .word 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
  179. .word 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
  180. .word 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
  181. .word 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
  182. .word 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
  183. .word 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
  184. .word 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
  185. .word 0xd192e819,0xd6990624,0xf40e3585,0x106aa070
  186. .word 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
  187. .word 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
  188. .word 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
  189. .word 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  190. .size K256,.-K256
  191. .word 0 @ terminator
  192. #if __ARM_MAX_ARCH__>=7 && !defined(__KERNEL__)
  193. .LOPENSSL_armcap:
  194. .word OPENSSL_armcap_P-.Lsha256_block_data_order
  195. #endif
  196. .align 5
  197. .global sha256_block_data_order
  198. .type sha256_block_data_order,%function
  199. sha256_block_data_order:
  200. .Lsha256_block_data_order:
  201. #if __ARM_ARCH__<7 && !defined(__thumb2__)
  202. sub r3,pc,#8 @ sha256_block_data_order
  203. #else
  204. adr r3,.Lsha256_block_data_order
  205. #endif
  206. #if __ARM_MAX_ARCH__>=7 && !defined(__KERNEL__)
  207. ldr r12,.LOPENSSL_armcap
  208. ldr r12,[r3,r12] @ OPENSSL_armcap_P
  209. #ifdef __APPLE__
  210. ldr r12,[r12]
  211. #endif
  212. tst r12,#ARMV8_SHA256
  213. bne .LARMv8
  214. tst r12,#ARMV7_NEON
  215. bne .LNEON
  216. #endif
  217. add $len,$inp,$len,lsl#6 @ len to point at the end of inp
  218. stmdb sp!,{$ctx,$inp,$len,r4-r11,lr}
  219. ldmia $ctx,{$A,$B,$C,$D,$E,$F,$G,$H}
  220. sub $Ktbl,r3,#256+32 @ K256
  221. sub sp,sp,#16*4 @ alloca(X[16])
  222. .Loop:
  223. # if __ARM_ARCH__>=7
  224. ldr $t1,[$inp],#4
  225. # else
  226. ldrb $t1,[$inp,#3]
  227. # endif
  228. eor $t3,$B,$C @ magic
  229. eor $t2,$t2,$t2
  230. ___
  231. for($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
  232. $code.=".Lrounds_16_xx:\n";
  233. for (;$i<32;$i++) { &BODY_16_XX($i,@V); unshift(@V,pop(@V)); }
  234. $code.=<<___;
  235. #ifdef __thumb2__
  236. ite eq @ Thumb2 thing, sanity check in ARM
  237. #endif
  238. ldreq $t3,[sp,#16*4] @ pull ctx
  239. bne .Lrounds_16_xx
  240. add $A,$A,$t2 @ h+=Maj(a,b,c) from the past
  241. ldr $t0,[$t3,#0]
  242. ldr $t1,[$t3,#4]
  243. ldr $t2,[$t3,#8]
  244. add $A,$A,$t0
  245. ldr $t0,[$t3,#12]
  246. add $B,$B,$t1
  247. ldr $t1,[$t3,#16]
  248. add $C,$C,$t2
  249. ldr $t2,[$t3,#20]
  250. add $D,$D,$t0
  251. ldr $t0,[$t3,#24]
  252. add $E,$E,$t1
  253. ldr $t1,[$t3,#28]
  254. add $F,$F,$t2
  255. ldr $inp,[sp,#17*4] @ pull inp
  256. ldr $t2,[sp,#18*4] @ pull inp+len
  257. add $G,$G,$t0
  258. add $H,$H,$t1
  259. stmia $t3,{$A,$B,$C,$D,$E,$F,$G,$H}
  260. cmp $inp,$t2
  261. sub $Ktbl,$Ktbl,#256 @ rewind Ktbl
  262. bne .Loop
  263. add sp,sp,#`16+3`*4 @ destroy frame
  264. #if __ARM_ARCH__>=5
  265. ldmia sp!,{r4-r11,pc}
  266. #else
  267. ldmia sp!,{r4-r11,lr}
  268. tst lr,#1
  269. moveq pc,lr @ be binary compatible with V4, yet
  270. bx lr @ interoperable with Thumb ISA:-)
  271. #endif
  272. .size sha256_block_data_order,.-sha256_block_data_order
  273. ___
  274. ######################################################################
  275. # NEON stuff
  276. #
  277. {{{
  278. my @X=map("q$_",(0..3));
  279. my ($T0,$T1,$T2,$T3,$T4,$T5)=("q8","q9","q10","q11","d24","d25");
  280. my $Xfer=$t4;
  281. my $j=0;
  282. sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
  283. sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
  284. sub AUTOLOAD() # thunk [simplified] x86-style perlasm
  285. { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./;
  286. my $arg = pop;
  287. $arg = "#$arg" if ($arg*1 eq $arg);
  288. $code .= "\t$opcode\t".join(',',@_,$arg)."\n";
  289. }
  290. sub Xupdate()
  291. { use integer;
  292. my $body = shift;
  293. my @insns = (&$body,&$body,&$body,&$body);
  294. my ($a,$b,$c,$d,$e,$f,$g,$h);
  295. &vext_8 ($T0,@X[0],@X[1],4); # X[1..4]
  296. eval(shift(@insns));
  297. eval(shift(@insns));
  298. eval(shift(@insns));
  299. &vext_8 ($T1,@X[2],@X[3],4); # X[9..12]
  300. eval(shift(@insns));
  301. eval(shift(@insns));
  302. eval(shift(@insns));
  303. &vshr_u32 ($T2,$T0,$sigma0[0]);
  304. eval(shift(@insns));
  305. eval(shift(@insns));
  306. &vadd_i32 (@X[0],@X[0],$T1); # X[0..3] += X[9..12]
  307. eval(shift(@insns));
  308. eval(shift(@insns));
  309. &vshr_u32 ($T1,$T0,$sigma0[2]);
  310. eval(shift(@insns));
  311. eval(shift(@insns));
  312. &vsli_32 ($T2,$T0,32-$sigma0[0]);
  313. eval(shift(@insns));
  314. eval(shift(@insns));
  315. &vshr_u32 ($T3,$T0,$sigma0[1]);
  316. eval(shift(@insns));
  317. eval(shift(@insns));
  318. &veor ($T1,$T1,$T2);
  319. eval(shift(@insns));
  320. eval(shift(@insns));
  321. &vsli_32 ($T3,$T0,32-$sigma0[1]);
  322. eval(shift(@insns));
  323. eval(shift(@insns));
  324. &vshr_u32 ($T4,&Dhi(@X[3]),$sigma1[0]);
  325. eval(shift(@insns));
  326. eval(shift(@insns));
  327. &veor ($T1,$T1,$T3); # sigma0(X[1..4])
  328. eval(shift(@insns));
  329. eval(shift(@insns));
  330. &vsli_32 ($T4,&Dhi(@X[3]),32-$sigma1[0]);
  331. eval(shift(@insns));
  332. eval(shift(@insns));
  333. &vshr_u32 ($T5,&Dhi(@X[3]),$sigma1[2]);
  334. eval(shift(@insns));
  335. eval(shift(@insns));
  336. &vadd_i32 (@X[0],@X[0],$T1); # X[0..3] += sigma0(X[1..4])
  337. eval(shift(@insns));
  338. eval(shift(@insns));
  339. &veor ($T5,$T5,$T4);
  340. eval(shift(@insns));
  341. eval(shift(@insns));
  342. &vshr_u32 ($T4,&Dhi(@X[3]),$sigma1[1]);
  343. eval(shift(@insns));
  344. eval(shift(@insns));
  345. &vsli_32 ($T4,&Dhi(@X[3]),32-$sigma1[1]);
  346. eval(shift(@insns));
  347. eval(shift(@insns));
  348. &veor ($T5,$T5,$T4); # sigma1(X[14..15])
  349. eval(shift(@insns));
  350. eval(shift(@insns));
  351. &vadd_i32 (&Dlo(@X[0]),&Dlo(@X[0]),$T5);# X[0..1] += sigma1(X[14..15])
  352. eval(shift(@insns));
  353. eval(shift(@insns));
  354. &vshr_u32 ($T4,&Dlo(@X[0]),$sigma1[0]);
  355. eval(shift(@insns));
  356. eval(shift(@insns));
  357. &vsli_32 ($T4,&Dlo(@X[0]),32-$sigma1[0]);
  358. eval(shift(@insns));
  359. eval(shift(@insns));
  360. &vshr_u32 ($T5,&Dlo(@X[0]),$sigma1[2]);
  361. eval(shift(@insns));
  362. eval(shift(@insns));
  363. &veor ($T5,$T5,$T4);
  364. eval(shift(@insns));
  365. eval(shift(@insns));
  366. &vshr_u32 ($T4,&Dlo(@X[0]),$sigma1[1]);
  367. eval(shift(@insns));
  368. eval(shift(@insns));
  369. &vld1_32 ("{$T0}","[$Ktbl,:128]!");
  370. eval(shift(@insns));
  371. eval(shift(@insns));
  372. &vsli_32 ($T4,&Dlo(@X[0]),32-$sigma1[1]);
  373. eval(shift(@insns));
  374. eval(shift(@insns));
  375. &veor ($T5,$T5,$T4); # sigma1(X[16..17])
  376. eval(shift(@insns));
  377. eval(shift(@insns));
  378. &vadd_i32 (&Dhi(@X[0]),&Dhi(@X[0]),$T5);# X[2..3] += sigma1(X[16..17])
  379. eval(shift(@insns));
  380. eval(shift(@insns));
  381. &vadd_i32 ($T0,$T0,@X[0]);
  382. while($#insns>=2) { eval(shift(@insns)); }
  383. &vst1_32 ("{$T0}","[$Xfer,:128]!");
  384. eval(shift(@insns));
  385. eval(shift(@insns));
  386. push(@X,shift(@X)); # "rotate" X[]
  387. }
  388. sub Xpreload()
  389. { use integer;
  390. my $body = shift;
  391. my @insns = (&$body,&$body,&$body,&$body);
  392. my ($a,$b,$c,$d,$e,$f,$g,$h);
  393. eval(shift(@insns));
  394. eval(shift(@insns));
  395. eval(shift(@insns));
  396. eval(shift(@insns));
  397. &vld1_32 ("{$T0}","[$Ktbl,:128]!");
  398. eval(shift(@insns));
  399. eval(shift(@insns));
  400. eval(shift(@insns));
  401. eval(shift(@insns));
  402. &vrev32_8 (@X[0],@X[0]);
  403. eval(shift(@insns));
  404. eval(shift(@insns));
  405. eval(shift(@insns));
  406. eval(shift(@insns));
  407. &vadd_i32 ($T0,$T0,@X[0]);
  408. foreach (@insns) { eval; } # remaining instructions
  409. &vst1_32 ("{$T0}","[$Xfer,:128]!");
  410. push(@X,shift(@X)); # "rotate" X[]
  411. }
  412. sub body_00_15 () {
  413. (
  414. '($a,$b,$c,$d,$e,$f,$g,$h)=@V;'.
  415. '&add ($h,$h,$t1)', # h+=X[i]+K[i]
  416. '&eor ($t1,$f,$g)',
  417. '&eor ($t0,$e,$e,"ror#".($Sigma1[1]-$Sigma1[0]))',
  418. '&add ($a,$a,$t2)', # h+=Maj(a,b,c) from the past
  419. '&and ($t1,$t1,$e)',
  420. '&eor ($t2,$t0,$e,"ror#".($Sigma1[2]-$Sigma1[0]))', # Sigma1(e)
  421. '&eor ($t0,$a,$a,"ror#".($Sigma0[1]-$Sigma0[0]))',
  422. '&eor ($t1,$t1,$g)', # Ch(e,f,g)
  423. '&add ($h,$h,$t2,"ror#$Sigma1[0]")', # h+=Sigma1(e)
  424. '&eor ($t2,$a,$b)', # a^b, b^c in next round
  425. '&eor ($t0,$t0,$a,"ror#".($Sigma0[2]-$Sigma0[0]))', # Sigma0(a)
  426. '&add ($h,$h,$t1)', # h+=Ch(e,f,g)
  427. '&ldr ($t1,sprintf "[sp,#%d]",4*(($j+1)&15)) if (($j&15)!=15);'.
  428. '&ldr ($t1,"[$Ktbl]") if ($j==15);'.
  429. '&ldr ($t1,"[sp,#64]") if ($j==31)',
  430. '&and ($t3,$t3,$t2)', # (b^c)&=(a^b)
  431. '&add ($d,$d,$h)', # d+=h
  432. '&add ($h,$h,$t0,"ror#$Sigma0[0]");'. # h+=Sigma0(a)
  433. '&eor ($t3,$t3,$b)', # Maj(a,b,c)
  434. '$j++; unshift(@V,pop(@V)); ($t2,$t3)=($t3,$t2);'
  435. )
  436. }
  437. $code.=<<___;
  438. #if __ARM_MAX_ARCH__>=7
  439. .arch armv7-a
  440. .fpu neon
  441. .global sha256_block_data_order_neon
  442. .type sha256_block_data_order_neon,%function
  443. .align 5
  444. .skip 16
  445. sha256_block_data_order_neon:
  446. .LNEON:
  447. stmdb sp!,{r4-r12,lr}
  448. sub $H,sp,#16*4+16
  449. adr $Ktbl,K256
  450. bic $H,$H,#15 @ align for 128-bit stores
  451. mov $t2,sp
  452. mov sp,$H @ alloca
  453. add $len,$inp,$len,lsl#6 @ len to point at the end of inp
  454. vld1.8 {@X[0]},[$inp]!
  455. vld1.8 {@X[1]},[$inp]!
  456. vld1.8 {@X[2]},[$inp]!
  457. vld1.8 {@X[3]},[$inp]!
  458. vld1.32 {$T0},[$Ktbl,:128]!
  459. vld1.32 {$T1},[$Ktbl,:128]!
  460. vld1.32 {$T2},[$Ktbl,:128]!
  461. vld1.32 {$T3},[$Ktbl,:128]!
  462. vrev32.8 @X[0],@X[0] @ yes, even on
  463. str $ctx,[sp,#64]
  464. vrev32.8 @X[1],@X[1] @ big-endian
  465. str $inp,[sp,#68]
  466. mov $Xfer,sp
  467. vrev32.8 @X[2],@X[2]
  468. str $len,[sp,#72]
  469. vrev32.8 @X[3],@X[3]
  470. str $t2,[sp,#76] @ save original sp
  471. vadd.i32 $T0,$T0,@X[0]
  472. vadd.i32 $T1,$T1,@X[1]
  473. vst1.32 {$T0},[$Xfer,:128]!
  474. vadd.i32 $T2,$T2,@X[2]
  475. vst1.32 {$T1},[$Xfer,:128]!
  476. vadd.i32 $T3,$T3,@X[3]
  477. vst1.32 {$T2},[$Xfer,:128]!
  478. vst1.32 {$T3},[$Xfer,:128]!
  479. ldmia $ctx,{$A-$H}
  480. sub $Xfer,$Xfer,#64
  481. ldr $t1,[sp,#0]
  482. eor $t2,$t2,$t2
  483. eor $t3,$B,$C
  484. b .L_00_48
  485. .align 4
  486. .L_00_48:
  487. ___
  488. &Xupdate(\&body_00_15);
  489. &Xupdate(\&body_00_15);
  490. &Xupdate(\&body_00_15);
  491. &Xupdate(\&body_00_15);
  492. $code.=<<___;
  493. teq $t1,#0 @ check for K256 terminator
  494. ldr $t1,[sp,#0]
  495. sub $Xfer,$Xfer,#64
  496. bne .L_00_48
  497. ldr $inp,[sp,#68]
  498. ldr $t0,[sp,#72]
  499. sub $Ktbl,$Ktbl,#256 @ rewind $Ktbl
  500. teq $inp,$t0
  501. it eq
  502. subeq $inp,$inp,#64 @ avoid SEGV
  503. vld1.8 {@X[0]},[$inp]! @ load next input block
  504. vld1.8 {@X[1]},[$inp]!
  505. vld1.8 {@X[2]},[$inp]!
  506. vld1.8 {@X[3]},[$inp]!
  507. it ne
  508. strne $inp,[sp,#68]
  509. mov $Xfer,sp
  510. ___
  511. &Xpreload(\&body_00_15);
  512. &Xpreload(\&body_00_15);
  513. &Xpreload(\&body_00_15);
  514. &Xpreload(\&body_00_15);
  515. $code.=<<___;
  516. ldr $t0,[$t1,#0]
  517. add $A,$A,$t2 @ h+=Maj(a,b,c) from the past
  518. ldr $t2,[$t1,#4]
  519. ldr $t3,[$t1,#8]
  520. ldr $t4,[$t1,#12]
  521. add $A,$A,$t0 @ accumulate
  522. ldr $t0,[$t1,#16]
  523. add $B,$B,$t2
  524. ldr $t2,[$t1,#20]
  525. add $C,$C,$t3
  526. ldr $t3,[$t1,#24]
  527. add $D,$D,$t4
  528. ldr $t4,[$t1,#28]
  529. add $E,$E,$t0
  530. str $A,[$t1],#4
  531. add $F,$F,$t2
  532. str $B,[$t1],#4
  533. add $G,$G,$t3
  534. str $C,[$t1],#4
  535. add $H,$H,$t4
  536. str $D,[$t1],#4
  537. stmia $t1,{$E-$H}
  538. ittte ne
  539. movne $Xfer,sp
  540. ldrne $t1,[sp,#0]
  541. eorne $t2,$t2,$t2
  542. ldreq sp,[sp,#76] @ restore original sp
  543. itt ne
  544. eorne $t3,$B,$C
  545. bne .L_00_48
  546. ldmia sp!,{r4-r12,pc}
  547. .size sha256_block_data_order_neon,.-sha256_block_data_order_neon
  548. #endif
  549. ___
  550. }}}
  551. ######################################################################
  552. # ARMv8 stuff
  553. #
  554. {{{
  555. my ($ABCD,$EFGH,$abcd)=map("q$_",(0..2));
  556. my @MSG=map("q$_",(8..11));
  557. my ($W0,$W1,$ABCD_SAVE,$EFGH_SAVE)=map("q$_",(12..15));
  558. my $Ktbl="r3";
  559. $code.=<<___;
  560. #if __ARM_MAX_ARCH__>=7 && !defined(__KERNEL__)
  561. # if defined(__thumb2__)
  562. # define INST(a,b,c,d) .byte c,d|0xc,a,b
  563. # else
  564. # define INST(a,b,c,d) .byte a,b,c,d
  565. # endif
  566. .type sha256_block_data_order_armv8,%function
  567. .align 5
  568. sha256_block_data_order_armv8:
  569. .LARMv8:
  570. vld1.32 {$ABCD,$EFGH},[$ctx]
  571. sub $Ktbl,$Ktbl,#256+32
  572. add $len,$inp,$len,lsl#6 @ len to point at the end of inp
  573. b .Loop_v8
  574. .align 4
  575. .Loop_v8:
  576. vld1.8 {@MSG[0]-@MSG[1]},[$inp]!
  577. vld1.8 {@MSG[2]-@MSG[3]},[$inp]!
  578. vld1.32 {$W0},[$Ktbl]!
  579. vrev32.8 @MSG[0],@MSG[0]
  580. vrev32.8 @MSG[1],@MSG[1]
  581. vrev32.8 @MSG[2],@MSG[2]
  582. vrev32.8 @MSG[3],@MSG[3]
  583. vmov $ABCD_SAVE,$ABCD @ offload
  584. vmov $EFGH_SAVE,$EFGH
  585. teq $inp,$len
  586. ___
  587. for($i=0;$i<12;$i++) {
  588. $code.=<<___;
  589. vld1.32 {$W1},[$Ktbl]!
  590. vadd.i32 $W0,$W0,@MSG[0]
  591. sha256su0 @MSG[0],@MSG[1]
  592. vmov $abcd,$ABCD
  593. sha256h $ABCD,$EFGH,$W0
  594. sha256h2 $EFGH,$abcd,$W0
  595. sha256su1 @MSG[0],@MSG[2],@MSG[3]
  596. ___
  597. ($W0,$W1)=($W1,$W0); push(@MSG,shift(@MSG));
  598. }
  599. $code.=<<___;
  600. vld1.32 {$W1},[$Ktbl]!
  601. vadd.i32 $W0,$W0,@MSG[0]
  602. vmov $abcd,$ABCD
  603. sha256h $ABCD,$EFGH,$W0
  604. sha256h2 $EFGH,$abcd,$W0
  605. vld1.32 {$W0},[$Ktbl]!
  606. vadd.i32 $W1,$W1,@MSG[1]
  607. vmov $abcd,$ABCD
  608. sha256h $ABCD,$EFGH,$W1
  609. sha256h2 $EFGH,$abcd,$W1
  610. vld1.32 {$W1},[$Ktbl]
  611. vadd.i32 $W0,$W0,@MSG[2]
  612. sub $Ktbl,$Ktbl,#256-16 @ rewind
  613. vmov $abcd,$ABCD
  614. sha256h $ABCD,$EFGH,$W0
  615. sha256h2 $EFGH,$abcd,$W0
  616. vadd.i32 $W1,$W1,@MSG[3]
  617. vmov $abcd,$ABCD
  618. sha256h $ABCD,$EFGH,$W1
  619. sha256h2 $EFGH,$abcd,$W1
  620. vadd.i32 $ABCD,$ABCD,$ABCD_SAVE
  621. vadd.i32 $EFGH,$EFGH,$EFGH_SAVE
  622. it ne
  623. bne .Loop_v8
  624. vst1.32 {$ABCD,$EFGH},[$ctx]
  625. ret @ bx lr
  626. .size sha256_block_data_order_armv8,.-sha256_block_data_order_armv8
  627. #endif
  628. ___
  629. }}}
  630. $code.=<<___;
  631. .asciz "SHA256 block transform for ARMv4/NEON/ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  632. .align 2
  633. #if __ARM_MAX_ARCH__>=7 && !defined(__KERNEL__)
  634. .comm OPENSSL_armcap_P,4,4
  635. #endif
  636. ___
  637. open SELF,$0;
  638. while(<SELF>) {
  639. next if (/^#!/);
  640. last if (!s/^#/@/ and !/^$/);
  641. print;
  642. }
  643. close SELF;
  644. { my %opcode = (
  645. "sha256h" => 0xf3000c40, "sha256h2" => 0xf3100c40,
  646. "sha256su0" => 0xf3ba03c0, "sha256su1" => 0xf3200c40 );
  647. sub unsha256 {
  648. my ($mnemonic,$arg)=@_;
  649. if ($arg =~ m/q([0-9]+)(?:,\s*q([0-9]+))?,\s*q([0-9]+)/o) {
  650. my $word = $opcode{$mnemonic}|(($1&7)<<13)|(($1&8)<<19)
  651. |(($2&7)<<17)|(($2&8)<<4)
  652. |(($3&7)<<1) |(($3&8)<<2);
  653. # since ARMv7 instructions are always encoded little-endian.
  654. # correct solution is to use .inst directive, but older
  655. # assemblers don't implement it:-(
  656. sprintf "INST(0x%02x,0x%02x,0x%02x,0x%02x)\t@ %s %s",
  657. $word&0xff,($word>>8)&0xff,
  658. ($word>>16)&0xff,($word>>24)&0xff,
  659. $mnemonic,$arg;
  660. }
  661. }
  662. }
  663. foreach (split($/,$code)) {
  664. s/\`([^\`]*)\`/eval $1/geo;
  665. s/\b(sha256\w+)\s+(q.*)/unsha256($1,$2)/geo;
  666. s/\bret\b/bx lr/go or
  667. s/\bbx\s+lr\b/.word\t0xe12fff1e/go; # make it possible to compile with -march=armv4
  668. print $_,"\n";
  669. }
  670. close STDOUT; # enforce flush