sha512-ppc.pl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. #! /usr/bin/env perl
  2. # Copyright 2006-2016 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. # 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. # I let hardware handle unaligned input, except on page boundaries
  15. # (see below for details). Otherwise straightforward implementation
  16. # with X vector in register bank.
  17. # sha256 | sha512
  18. # -m64 -m32 | -m64 -m32
  19. # --------------------------------------+-----------------------
  20. # PPC970,gcc-4.0.0 +50% +38% | +40% +410%(*)
  21. # Power6,xlc-7 +150% +90% | +100% +430%(*)
  22. #
  23. # (*) 64-bit code in 32-bit application context, which actually is
  24. # on TODO list. It should be noted that for safe deployment in
  25. # 32-bit *multi-threaded* context asynchronous signals should be
  26. # blocked upon entry to SHA512 block routine. This is because
  27. # 32-bit signaling procedure invalidates upper halves of GPRs.
  28. # Context switch procedure preserves them, but not signaling:-(
  29. # Second version is true multi-thread safe. Trouble with the original
  30. # version was that it was using thread local storage pointer register.
  31. # Well, it scrupulously preserved it, but the problem would arise the
  32. # moment asynchronous signal was delivered and signal handler would
  33. # dereference the TLS pointer. While it's never the case in openssl
  34. # application or test suite, we have to respect this scenario and not
  35. # use TLS pointer register. Alternative would be to require caller to
  36. # block signals prior calling this routine. For the record, in 32-bit
  37. # context R2 serves as TLS pointer, while in 64-bit context - R13.
  38. $flavour=shift;
  39. $output =shift;
  40. if ($flavour =~ /64/) {
  41. $SIZE_T=8;
  42. $LRSAVE=2*$SIZE_T;
  43. $STU="stdu";
  44. $UCMP="cmpld";
  45. $SHL="sldi";
  46. $POP="ld";
  47. $PUSH="std";
  48. } elsif ($flavour =~ /32/) {
  49. $SIZE_T=4;
  50. $LRSAVE=$SIZE_T;
  51. $STU="stwu";
  52. $UCMP="cmplw";
  53. $SHL="slwi";
  54. $POP="lwz";
  55. $PUSH="stw";
  56. } else { die "nonsense $flavour"; }
  57. $LITTLE_ENDIAN = ($flavour=~/le$/) ? $SIZE_T : 0;
  58. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  59. ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
  60. ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
  61. die "can't locate ppc-xlate.pl";
  62. open STDOUT,"| $^X $xlate $flavour $output" || die "can't call $xlate: $!";
  63. if ($output =~ /512/) {
  64. $func="sha512_block_ppc";
  65. $SZ=8;
  66. @Sigma0=(28,34,39);
  67. @Sigma1=(14,18,41);
  68. @sigma0=(1, 8, 7);
  69. @sigma1=(19,61, 6);
  70. $rounds=80;
  71. $LD="ld";
  72. $ST="std";
  73. $ROR="rotrdi";
  74. $SHR="srdi";
  75. } else {
  76. $func="sha256_block_ppc";
  77. $SZ=4;
  78. @Sigma0=( 2,13,22);
  79. @Sigma1=( 6,11,25);
  80. @sigma0=( 7,18, 3);
  81. @sigma1=(17,19,10);
  82. $rounds=64;
  83. $LD="lwz";
  84. $ST="stw";
  85. $ROR="rotrwi";
  86. $SHR="srwi";
  87. }
  88. $FRAME=32*$SIZE_T+16*$SZ;
  89. $LOCALS=6*$SIZE_T;
  90. $sp ="r1";
  91. $toc="r2";
  92. $ctx="r3"; # zapped by $a0
  93. $inp="r4"; # zapped by $a1
  94. $num="r5"; # zapped by $t0
  95. $T ="r0";
  96. $a0 ="r3";
  97. $a1 ="r4";
  98. $t0 ="r5";
  99. $t1 ="r6";
  100. $Tbl="r7";
  101. $A ="r8";
  102. $B ="r9";
  103. $C ="r10";
  104. $D ="r11";
  105. $E ="r12";
  106. $F =$t1; $t1 = "r0"; # stay away from "r13";
  107. $G ="r14";
  108. $H ="r15";
  109. @V=($A,$B,$C,$D,$E,$F,$G,$H);
  110. @X=("r16","r17","r18","r19","r20","r21","r22","r23",
  111. "r24","r25","r26","r27","r28","r29","r30","r31");
  112. $inp="r31" if($SZ==4 || $SIZE_T==8); # reassigned $inp! aliases with @X[15]
  113. sub ROUND_00_15 {
  114. my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_;
  115. $code.=<<___;
  116. $ROR $a0,$e,$Sigma1[0]
  117. $ROR $a1,$e,$Sigma1[1]
  118. and $t0,$f,$e
  119. xor $a0,$a0,$a1
  120. add $h,$h,$t1
  121. andc $t1,$g,$e
  122. $ROR $a1,$a1,`$Sigma1[2]-$Sigma1[1]`
  123. or $t0,$t0,$t1 ; Ch(e,f,g)
  124. add $h,$h,@X[$i%16]
  125. xor $a0,$a0,$a1 ; Sigma1(e)
  126. add $h,$h,$t0
  127. add $h,$h,$a0
  128. $ROR $a0,$a,$Sigma0[0]
  129. $ROR $a1,$a,$Sigma0[1]
  130. and $t0,$a,$b
  131. and $t1,$a,$c
  132. xor $a0,$a0,$a1
  133. $ROR $a1,$a1,`$Sigma0[2]-$Sigma0[1]`
  134. xor $t0,$t0,$t1
  135. and $t1,$b,$c
  136. xor $a0,$a0,$a1 ; Sigma0(a)
  137. add $d,$d,$h
  138. xor $t0,$t0,$t1 ; Maj(a,b,c)
  139. ___
  140. $code.=<<___ if ($i<15);
  141. $LD $t1,`($i+1)*$SZ`($Tbl)
  142. ___
  143. $code.=<<___;
  144. add $h,$h,$a0
  145. add $h,$h,$t0
  146. ___
  147. }
  148. sub ROUND_16_xx {
  149. my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_;
  150. $i-=16;
  151. $code.=<<___;
  152. $ROR $a0,@X[($i+1)%16],$sigma0[0]
  153. $ROR $a1,@X[($i+1)%16],$sigma0[1]
  154. $ROR $t0,@X[($i+14)%16],$sigma1[0]
  155. $ROR $t1,@X[($i+14)%16],$sigma1[1]
  156. xor $a0,$a0,$a1
  157. $SHR $a1,@X[($i+1)%16],$sigma0[2]
  158. xor $t0,$t0,$t1
  159. $SHR $t1,@X[($i+14)%16],$sigma1[2]
  160. add @X[$i],@X[$i],@X[($i+9)%16]
  161. xor $a0,$a0,$a1 ; sigma0(X[(i+1)&0x0f])
  162. xor $t0,$t0,$t1 ; sigma1(X[(i+14)&0x0f])
  163. $LD $t1,`$i*$SZ`($Tbl)
  164. add @X[$i],@X[$i],$a0
  165. add @X[$i],@X[$i],$t0
  166. ___
  167. &ROUND_00_15($i+16,$a,$b,$c,$d,$e,$f,$g,$h);
  168. }
  169. $code=<<___;
  170. .machine "any"
  171. .text
  172. .globl $func
  173. .align 6
  174. $func:
  175. $STU $sp,-$FRAME($sp)
  176. mflr r0
  177. $SHL $num,$num,`log(16*$SZ)/log(2)`
  178. $PUSH $ctx,`$FRAME-$SIZE_T*22`($sp)
  179. $PUSH r14,`$FRAME-$SIZE_T*18`($sp)
  180. $PUSH r15,`$FRAME-$SIZE_T*17`($sp)
  181. $PUSH r16,`$FRAME-$SIZE_T*16`($sp)
  182. $PUSH r17,`$FRAME-$SIZE_T*15`($sp)
  183. $PUSH r18,`$FRAME-$SIZE_T*14`($sp)
  184. $PUSH r19,`$FRAME-$SIZE_T*13`($sp)
  185. $PUSH r20,`$FRAME-$SIZE_T*12`($sp)
  186. $PUSH r21,`$FRAME-$SIZE_T*11`($sp)
  187. $PUSH r22,`$FRAME-$SIZE_T*10`($sp)
  188. $PUSH r23,`$FRAME-$SIZE_T*9`($sp)
  189. $PUSH r24,`$FRAME-$SIZE_T*8`($sp)
  190. $PUSH r25,`$FRAME-$SIZE_T*7`($sp)
  191. $PUSH r26,`$FRAME-$SIZE_T*6`($sp)
  192. $PUSH r27,`$FRAME-$SIZE_T*5`($sp)
  193. $PUSH r28,`$FRAME-$SIZE_T*4`($sp)
  194. $PUSH r29,`$FRAME-$SIZE_T*3`($sp)
  195. $PUSH r30,`$FRAME-$SIZE_T*2`($sp)
  196. $PUSH r31,`$FRAME-$SIZE_T*1`($sp)
  197. $PUSH r0,`$FRAME+$LRSAVE`($sp)
  198. ___
  199. if ($SZ==4 || $SIZE_T==8) {
  200. $code.=<<___;
  201. $LD $A,`0*$SZ`($ctx)
  202. mr $inp,r4 ; incarnate $inp
  203. $LD $B,`1*$SZ`($ctx)
  204. $LD $C,`2*$SZ`($ctx)
  205. $LD $D,`3*$SZ`($ctx)
  206. $LD $E,`4*$SZ`($ctx)
  207. $LD $F,`5*$SZ`($ctx)
  208. $LD $G,`6*$SZ`($ctx)
  209. $LD $H,`7*$SZ`($ctx)
  210. ___
  211. } else {
  212. for ($i=16;$i<32;$i++) {
  213. $code.=<<___;
  214. lwz r$i,`$LITTLE_ENDIAN^(4*($i-16))`($ctx)
  215. ___
  216. }
  217. }
  218. $code.=<<___;
  219. bl LPICmeup
  220. LPICedup:
  221. andi. r0,$inp,3
  222. bne Lunaligned
  223. Laligned:
  224. add $num,$inp,$num
  225. $PUSH $num,`$FRAME-$SIZE_T*24`($sp) ; end pointer
  226. $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer
  227. bl Lsha2_block_private
  228. b Ldone
  229. ; PowerPC specification allows an implementation to be ill-behaved
  230. ; upon unaligned access which crosses page boundary. "Better safe
  231. ; than sorry" principle makes me treat it specially. But I don't
  232. ; look for particular offending word, but rather for the input
  233. ; block which crosses the boundary. Once found that block is aligned
  234. ; and hashed separately...
  235. .align 4
  236. Lunaligned:
  237. subfic $t1,$inp,4096
  238. andi. $t1,$t1,`4096-16*$SZ` ; distance to closest page boundary
  239. beq Lcross_page
  240. $UCMP $num,$t1
  241. ble Laligned ; didn't cross the page boundary
  242. subfc $num,$t1,$num
  243. add $t1,$inp,$t1
  244. $PUSH $num,`$FRAME-$SIZE_T*25`($sp) ; save real remaining num
  245. $PUSH $t1,`$FRAME-$SIZE_T*24`($sp) ; intermediate end pointer
  246. $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer
  247. bl Lsha2_block_private
  248. ; $inp equals to the intermediate end pointer here
  249. $POP $num,`$FRAME-$SIZE_T*25`($sp) ; restore real remaining num
  250. Lcross_page:
  251. li $t1,`16*$SZ/4`
  252. mtctr $t1
  253. ___
  254. if ($SZ==4 || $SIZE_T==8) {
  255. $code.=<<___;
  256. addi r20,$sp,$LOCALS ; aligned spot below the frame
  257. Lmemcpy:
  258. lbz r16,0($inp)
  259. lbz r17,1($inp)
  260. lbz r18,2($inp)
  261. lbz r19,3($inp)
  262. addi $inp,$inp,4
  263. stb r16,0(r20)
  264. stb r17,1(r20)
  265. stb r18,2(r20)
  266. stb r19,3(r20)
  267. addi r20,r20,4
  268. bdnz Lmemcpy
  269. ___
  270. } else {
  271. $code.=<<___;
  272. addi r12,$sp,$LOCALS ; aligned spot below the frame
  273. Lmemcpy:
  274. lbz r8,0($inp)
  275. lbz r9,1($inp)
  276. lbz r10,2($inp)
  277. lbz r11,3($inp)
  278. addi $inp,$inp,4
  279. stb r8,0(r12)
  280. stb r9,1(r12)
  281. stb r10,2(r12)
  282. stb r11,3(r12)
  283. addi r12,r12,4
  284. bdnz Lmemcpy
  285. ___
  286. }
  287. $code.=<<___;
  288. $PUSH $inp,`$FRAME-$SIZE_T*26`($sp) ; save real inp
  289. addi $t1,$sp,`$LOCALS+16*$SZ` ; fictitious end pointer
  290. addi $inp,$sp,$LOCALS ; fictitious inp pointer
  291. $PUSH $num,`$FRAME-$SIZE_T*25`($sp) ; save real num
  292. $PUSH $t1,`$FRAME-$SIZE_T*24`($sp) ; end pointer
  293. $PUSH $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer
  294. bl Lsha2_block_private
  295. $POP $inp,`$FRAME-$SIZE_T*26`($sp) ; restore real inp
  296. $POP $num,`$FRAME-$SIZE_T*25`($sp) ; restore real num
  297. addic. $num,$num,`-16*$SZ` ; num--
  298. bne Lunaligned
  299. Ldone:
  300. $POP r0,`$FRAME+$LRSAVE`($sp)
  301. $POP r14,`$FRAME-$SIZE_T*18`($sp)
  302. $POP r15,`$FRAME-$SIZE_T*17`($sp)
  303. $POP r16,`$FRAME-$SIZE_T*16`($sp)
  304. $POP r17,`$FRAME-$SIZE_T*15`($sp)
  305. $POP r18,`$FRAME-$SIZE_T*14`($sp)
  306. $POP r19,`$FRAME-$SIZE_T*13`($sp)
  307. $POP r20,`$FRAME-$SIZE_T*12`($sp)
  308. $POP r21,`$FRAME-$SIZE_T*11`($sp)
  309. $POP r22,`$FRAME-$SIZE_T*10`($sp)
  310. $POP r23,`$FRAME-$SIZE_T*9`($sp)
  311. $POP r24,`$FRAME-$SIZE_T*8`($sp)
  312. $POP r25,`$FRAME-$SIZE_T*7`($sp)
  313. $POP r26,`$FRAME-$SIZE_T*6`($sp)
  314. $POP r27,`$FRAME-$SIZE_T*5`($sp)
  315. $POP r28,`$FRAME-$SIZE_T*4`($sp)
  316. $POP r29,`$FRAME-$SIZE_T*3`($sp)
  317. $POP r30,`$FRAME-$SIZE_T*2`($sp)
  318. $POP r31,`$FRAME-$SIZE_T*1`($sp)
  319. mtlr r0
  320. addi $sp,$sp,$FRAME
  321. blr
  322. .long 0
  323. .byte 0,12,4,1,0x80,18,3,0
  324. .long 0
  325. ___
  326. if ($SZ==4 || $SIZE_T==8) {
  327. $code.=<<___;
  328. .align 4
  329. Lsha2_block_private:
  330. $LD $t1,0($Tbl)
  331. ___
  332. for($i=0;$i<16;$i++) {
  333. $code.=<<___ if ($SZ==4 && !$LITTLE_ENDIAN);
  334. lwz @X[$i],`$i*$SZ`($inp)
  335. ___
  336. $code.=<<___ if ($SZ==4 && $LITTLE_ENDIAN);
  337. lwz $a0,`$i*$SZ`($inp)
  338. rotlwi @X[$i],$a0,8
  339. rlwimi @X[$i],$a0,24,0,7
  340. rlwimi @X[$i],$a0,24,16,23
  341. ___
  342. # 64-bit loads are split to 2x32-bit ones, as CPU can't handle
  343. # unaligned 64-bit loads, only 32-bit ones...
  344. $code.=<<___ if ($SZ==8 && !$LITTLE_ENDIAN);
  345. lwz $t0,`$i*$SZ`($inp)
  346. lwz @X[$i],`$i*$SZ+4`($inp)
  347. insrdi @X[$i],$t0,32,0
  348. ___
  349. $code.=<<___ if ($SZ==8 && $LITTLE_ENDIAN);
  350. lwz $a0,`$i*$SZ`($inp)
  351. lwz $a1,`$i*$SZ+4`($inp)
  352. rotlwi $t0,$a0,8
  353. rotlwi @X[$i],$a1,8
  354. rlwimi $t0,$a0,24,0,7
  355. rlwimi @X[$i],$a1,24,0,7
  356. rlwimi $t0,$a0,24,16,23
  357. rlwimi @X[$i],$a1,24,16,23
  358. insrdi @X[$i],$t0,32,0
  359. ___
  360. &ROUND_00_15($i,@V);
  361. unshift(@V,pop(@V));
  362. }
  363. $code.=<<___;
  364. li $t0,`$rounds/16-1`
  365. mtctr $t0
  366. .align 4
  367. Lrounds:
  368. addi $Tbl,$Tbl,`16*$SZ`
  369. ___
  370. for(;$i<32;$i++) {
  371. &ROUND_16_xx($i,@V);
  372. unshift(@V,pop(@V));
  373. }
  374. $code.=<<___;
  375. bdnz Lrounds
  376. $POP $ctx,`$FRAME-$SIZE_T*22`($sp)
  377. $POP $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer
  378. $POP $num,`$FRAME-$SIZE_T*24`($sp) ; end pointer
  379. subi $Tbl,$Tbl,`($rounds-16)*$SZ` ; rewind Tbl
  380. $LD r16,`0*$SZ`($ctx)
  381. $LD r17,`1*$SZ`($ctx)
  382. $LD r18,`2*$SZ`($ctx)
  383. $LD r19,`3*$SZ`($ctx)
  384. $LD r20,`4*$SZ`($ctx)
  385. $LD r21,`5*$SZ`($ctx)
  386. $LD r22,`6*$SZ`($ctx)
  387. addi $inp,$inp,`16*$SZ` ; advance inp
  388. $LD r23,`7*$SZ`($ctx)
  389. add $A,$A,r16
  390. add $B,$B,r17
  391. $PUSH $inp,`$FRAME-$SIZE_T*23`($sp)
  392. add $C,$C,r18
  393. $ST $A,`0*$SZ`($ctx)
  394. add $D,$D,r19
  395. $ST $B,`1*$SZ`($ctx)
  396. add $E,$E,r20
  397. $ST $C,`2*$SZ`($ctx)
  398. add $F,$F,r21
  399. $ST $D,`3*$SZ`($ctx)
  400. add $G,$G,r22
  401. $ST $E,`4*$SZ`($ctx)
  402. add $H,$H,r23
  403. $ST $F,`5*$SZ`($ctx)
  404. $ST $G,`6*$SZ`($ctx)
  405. $UCMP $inp,$num
  406. $ST $H,`7*$SZ`($ctx)
  407. bne Lsha2_block_private
  408. blr
  409. .long 0
  410. .byte 0,12,0x14,0,0,0,0,0
  411. .size $func,.-$func
  412. ___
  413. } else {
  414. ########################################################################
  415. # SHA512 for PPC32, X vector is off-loaded to stack...
  416. #
  417. # | sha512
  418. # | -m32
  419. # ----------------------+-----------------------
  420. # PPC74x0,gcc-4.0.1 | +48%
  421. # POWER6,gcc-4.4.6 | +124%(*)
  422. # POWER7,gcc-4.4.6 | +79%(*)
  423. # e300,gcc-4.1.0 | +167%
  424. #
  425. # (*) ~1/3 of -m64 result [and ~20% better than -m32 code generated
  426. # by xlc-12.1]
  427. my $XOFF=$LOCALS;
  428. my @V=map("r$_",(16..31)); # A..H
  429. my ($s0,$s1,$t0,$t1,$t2,$t3,$a0,$a1,$a2,$a3)=map("r$_",(0,5,6,8..12,14,15));
  430. my ($x0,$x1)=("r3","r4"); # zaps $ctx and $inp
  431. sub ROUND_00_15_ppc32 {
  432. my ($i, $ahi,$alo,$bhi,$blo,$chi,$clo,$dhi,$dlo,
  433. $ehi,$elo,$fhi,$flo,$ghi,$glo,$hhi,$hlo)=@_;
  434. $code.=<<___;
  435. lwz $t2,`$SZ*($i%16)+($LITTLE_ENDIAN^4)`($Tbl)
  436. xor $a0,$flo,$glo
  437. lwz $t3,`$SZ*($i%16)+($LITTLE_ENDIAN^0)`($Tbl)
  438. xor $a1,$fhi,$ghi
  439. addc $hlo,$hlo,$t0 ; h+=x[i]
  440. stw $t0,`$XOFF+0+$SZ*($i%16)`($sp) ; save x[i]
  441. srwi $s0,$elo,$Sigma1[0]
  442. srwi $s1,$ehi,$Sigma1[0]
  443. and $a0,$a0,$elo
  444. adde $hhi,$hhi,$t1
  445. and $a1,$a1,$ehi
  446. stw $t1,`$XOFF+4+$SZ*($i%16)`($sp)
  447. srwi $t0,$elo,$Sigma1[1]
  448. srwi $t1,$ehi,$Sigma1[1]
  449. addc $hlo,$hlo,$t2 ; h+=K512[i]
  450. insrwi $s0,$ehi,$Sigma1[0],0
  451. insrwi $s1,$elo,$Sigma1[0],0
  452. xor $a0,$a0,$glo ; Ch(e,f,g)
  453. adde $hhi,$hhi,$t3
  454. xor $a1,$a1,$ghi
  455. insrwi $t0,$ehi,$Sigma1[1],0
  456. insrwi $t1,$elo,$Sigma1[1],0
  457. addc $hlo,$hlo,$a0 ; h+=Ch(e,f,g)
  458. srwi $t2,$ehi,$Sigma1[2]-32
  459. srwi $t3,$elo,$Sigma1[2]-32
  460. xor $s0,$s0,$t0
  461. xor $s1,$s1,$t1
  462. insrwi $t2,$elo,$Sigma1[2]-32,0
  463. insrwi $t3,$ehi,$Sigma1[2]-32,0
  464. xor $a0,$alo,$blo ; a^b, b^c in next round
  465. adde $hhi,$hhi,$a1
  466. xor $a1,$ahi,$bhi
  467. xor $s0,$s0,$t2 ; Sigma1(e)
  468. xor $s1,$s1,$t3
  469. srwi $t0,$alo,$Sigma0[0]
  470. and $a2,$a2,$a0
  471. addc $hlo,$hlo,$s0 ; h+=Sigma1(e)
  472. and $a3,$a3,$a1
  473. srwi $t1,$ahi,$Sigma0[0]
  474. srwi $s0,$ahi,$Sigma0[1]-32
  475. adde $hhi,$hhi,$s1
  476. srwi $s1,$alo,$Sigma0[1]-32
  477. insrwi $t0,$ahi,$Sigma0[0],0
  478. insrwi $t1,$alo,$Sigma0[0],0
  479. xor $a2,$a2,$blo ; Maj(a,b,c)
  480. addc $dlo,$dlo,$hlo ; d+=h
  481. xor $a3,$a3,$bhi
  482. insrwi $s0,$alo,$Sigma0[1]-32,0
  483. insrwi $s1,$ahi,$Sigma0[1]-32,0
  484. adde $dhi,$dhi,$hhi
  485. srwi $t2,$ahi,$Sigma0[2]-32
  486. srwi $t3,$alo,$Sigma0[2]-32
  487. xor $s0,$s0,$t0
  488. addc $hlo,$hlo,$a2 ; h+=Maj(a,b,c)
  489. xor $s1,$s1,$t1
  490. insrwi $t2,$alo,$Sigma0[2]-32,0
  491. insrwi $t3,$ahi,$Sigma0[2]-32,0
  492. adde $hhi,$hhi,$a3
  493. ___
  494. $code.=<<___ if ($i>=15);
  495. lwz $t0,`$XOFF+0+$SZ*(($i+2)%16)`($sp)
  496. lwz $t1,`$XOFF+4+$SZ*(($i+2)%16)`($sp)
  497. ___
  498. $code.=<<___ if ($i<15 && !$LITTLE_ENDIAN);
  499. lwz $t1,`$SZ*($i+1)+0`($inp)
  500. lwz $t0,`$SZ*($i+1)+4`($inp)
  501. ___
  502. $code.=<<___ if ($i<15 && $LITTLE_ENDIAN);
  503. lwz $a2,`$SZ*($i+1)+0`($inp)
  504. lwz $a3,`$SZ*($i+1)+4`($inp)
  505. rotlwi $t1,$a2,8
  506. rotlwi $t0,$a3,8
  507. rlwimi $t1,$a2,24,0,7
  508. rlwimi $t0,$a3,24,0,7
  509. rlwimi $t1,$a2,24,16,23
  510. rlwimi $t0,$a3,24,16,23
  511. ___
  512. $code.=<<___;
  513. xor $s0,$s0,$t2 ; Sigma0(a)
  514. xor $s1,$s1,$t3
  515. addc $hlo,$hlo,$s0 ; h+=Sigma0(a)
  516. adde $hhi,$hhi,$s1
  517. ___
  518. $code.=<<___ if ($i==15);
  519. lwz $x0,`$XOFF+0+$SZ*(($i+1)%16)`($sp)
  520. lwz $x1,`$XOFF+4+$SZ*(($i+1)%16)`($sp)
  521. ___
  522. }
  523. sub ROUND_16_xx_ppc32 {
  524. my ($i, $ahi,$alo,$bhi,$blo,$chi,$clo,$dhi,$dlo,
  525. $ehi,$elo,$fhi,$flo,$ghi,$glo,$hhi,$hlo)=@_;
  526. $code.=<<___;
  527. srwi $s0,$t0,$sigma0[0]
  528. srwi $s1,$t1,$sigma0[0]
  529. srwi $t2,$t0,$sigma0[1]
  530. srwi $t3,$t1,$sigma0[1]
  531. insrwi $s0,$t1,$sigma0[0],0
  532. insrwi $s1,$t0,$sigma0[0],0
  533. srwi $a0,$t0,$sigma0[2]
  534. insrwi $t2,$t1,$sigma0[1],0
  535. insrwi $t3,$t0,$sigma0[1],0
  536. insrwi $a0,$t1,$sigma0[2],0
  537. xor $s0,$s0,$t2
  538. lwz $t2,`$XOFF+0+$SZ*(($i+14)%16)`($sp)
  539. srwi $a1,$t1,$sigma0[2]
  540. xor $s1,$s1,$t3
  541. lwz $t3,`$XOFF+4+$SZ*(($i+14)%16)`($sp)
  542. xor $a0,$a0,$s0
  543. srwi $s0,$t2,$sigma1[0]
  544. xor $a1,$a1,$s1
  545. srwi $s1,$t3,$sigma1[0]
  546. addc $x0,$x0,$a0 ; x[i]+=sigma0(x[i+1])
  547. srwi $a0,$t3,$sigma1[1]-32
  548. insrwi $s0,$t3,$sigma1[0],0
  549. insrwi $s1,$t2,$sigma1[0],0
  550. adde $x1,$x1,$a1
  551. srwi $a1,$t2,$sigma1[1]-32
  552. insrwi $a0,$t2,$sigma1[1]-32,0
  553. srwi $t2,$t2,$sigma1[2]
  554. insrwi $a1,$t3,$sigma1[1]-32,0
  555. insrwi $t2,$t3,$sigma1[2],0
  556. xor $s0,$s0,$a0
  557. lwz $a0,`$XOFF+0+$SZ*(($i+9)%16)`($sp)
  558. srwi $t3,$t3,$sigma1[2]
  559. xor $s1,$s1,$a1
  560. lwz $a1,`$XOFF+4+$SZ*(($i+9)%16)`($sp)
  561. xor $s0,$s0,$t2
  562. addc $x0,$x0,$a0 ; x[i]+=x[i+9]
  563. xor $s1,$s1,$t3
  564. adde $x1,$x1,$a1
  565. addc $x0,$x0,$s0 ; x[i]+=sigma1(x[i+14])
  566. adde $x1,$x1,$s1
  567. ___
  568. ($t0,$t1,$x0,$x1) = ($x0,$x1,$t0,$t1);
  569. &ROUND_00_15_ppc32(@_);
  570. }
  571. $code.=<<___;
  572. .align 4
  573. Lsha2_block_private:
  574. ___
  575. $code.=<<___ if (!$LITTLE_ENDIAN);
  576. lwz $t1,0($inp)
  577. xor $a2,@V[3],@V[5] ; B^C, magic seed
  578. lwz $t0,4($inp)
  579. xor $a3,@V[2],@V[4]
  580. ___
  581. $code.=<<___ if ($LITTLE_ENDIAN);
  582. lwz $a1,0($inp)
  583. xor $a2,@V[3],@V[5] ; B^C, magic seed
  584. lwz $a0,4($inp)
  585. xor $a3,@V[2],@V[4]
  586. rotlwi $t1,$a1,8
  587. rotlwi $t0,$a0,8
  588. rlwimi $t1,$a1,24,0,7
  589. rlwimi $t0,$a0,24,0,7
  590. rlwimi $t1,$a1,24,16,23
  591. rlwimi $t0,$a0,24,16,23
  592. ___
  593. for($i=0;$i<16;$i++) {
  594. &ROUND_00_15_ppc32($i,@V);
  595. unshift(@V,pop(@V)); unshift(@V,pop(@V));
  596. ($a0,$a1,$a2,$a3) = ($a2,$a3,$a0,$a1);
  597. }
  598. $code.=<<___;
  599. li $a0,`$rounds/16-1`
  600. mtctr $a0
  601. .align 4
  602. Lrounds:
  603. addi $Tbl,$Tbl,`16*$SZ`
  604. ___
  605. for(;$i<32;$i++) {
  606. &ROUND_16_xx_ppc32($i,@V);
  607. unshift(@V,pop(@V)); unshift(@V,pop(@V));
  608. ($a0,$a1,$a2,$a3) = ($a2,$a3,$a0,$a1);
  609. }
  610. $code.=<<___;
  611. bdnz Lrounds
  612. $POP $ctx,`$FRAME-$SIZE_T*22`($sp)
  613. $POP $inp,`$FRAME-$SIZE_T*23`($sp) ; inp pointer
  614. $POP $num,`$FRAME-$SIZE_T*24`($sp) ; end pointer
  615. subi $Tbl,$Tbl,`($rounds-16)*$SZ` ; rewind Tbl
  616. lwz $t0,`$LITTLE_ENDIAN^0`($ctx)
  617. lwz $t1,`$LITTLE_ENDIAN^4`($ctx)
  618. lwz $t2,`$LITTLE_ENDIAN^8`($ctx)
  619. lwz $t3,`$LITTLE_ENDIAN^12`($ctx)
  620. lwz $a0,`$LITTLE_ENDIAN^16`($ctx)
  621. lwz $a1,`$LITTLE_ENDIAN^20`($ctx)
  622. lwz $a2,`$LITTLE_ENDIAN^24`($ctx)
  623. addc @V[1],@V[1],$t1
  624. lwz $a3,`$LITTLE_ENDIAN^28`($ctx)
  625. adde @V[0],@V[0],$t0
  626. lwz $t0,`$LITTLE_ENDIAN^32`($ctx)
  627. addc @V[3],@V[3],$t3
  628. lwz $t1,`$LITTLE_ENDIAN^36`($ctx)
  629. adde @V[2],@V[2],$t2
  630. lwz $t2,`$LITTLE_ENDIAN^40`($ctx)
  631. addc @V[5],@V[5],$a1
  632. lwz $t3,`$LITTLE_ENDIAN^44`($ctx)
  633. adde @V[4],@V[4],$a0
  634. lwz $a0,`$LITTLE_ENDIAN^48`($ctx)
  635. addc @V[7],@V[7],$a3
  636. lwz $a1,`$LITTLE_ENDIAN^52`($ctx)
  637. adde @V[6],@V[6],$a2
  638. lwz $a2,`$LITTLE_ENDIAN^56`($ctx)
  639. addc @V[9],@V[9],$t1
  640. lwz $a3,`$LITTLE_ENDIAN^60`($ctx)
  641. adde @V[8],@V[8],$t0
  642. stw @V[0],`$LITTLE_ENDIAN^0`($ctx)
  643. stw @V[1],`$LITTLE_ENDIAN^4`($ctx)
  644. addc @V[11],@V[11],$t3
  645. stw @V[2],`$LITTLE_ENDIAN^8`($ctx)
  646. stw @V[3],`$LITTLE_ENDIAN^12`($ctx)
  647. adde @V[10],@V[10],$t2
  648. stw @V[4],`$LITTLE_ENDIAN^16`($ctx)
  649. stw @V[5],`$LITTLE_ENDIAN^20`($ctx)
  650. addc @V[13],@V[13],$a1
  651. stw @V[6],`$LITTLE_ENDIAN^24`($ctx)
  652. stw @V[7],`$LITTLE_ENDIAN^28`($ctx)
  653. adde @V[12],@V[12],$a0
  654. stw @V[8],`$LITTLE_ENDIAN^32`($ctx)
  655. stw @V[9],`$LITTLE_ENDIAN^36`($ctx)
  656. addc @V[15],@V[15],$a3
  657. stw @V[10],`$LITTLE_ENDIAN^40`($ctx)
  658. stw @V[11],`$LITTLE_ENDIAN^44`($ctx)
  659. adde @V[14],@V[14],$a2
  660. stw @V[12],`$LITTLE_ENDIAN^48`($ctx)
  661. stw @V[13],`$LITTLE_ENDIAN^52`($ctx)
  662. stw @V[14],`$LITTLE_ENDIAN^56`($ctx)
  663. stw @V[15],`$LITTLE_ENDIAN^60`($ctx)
  664. addi $inp,$inp,`16*$SZ` ; advance inp
  665. $PUSH $inp,`$FRAME-$SIZE_T*23`($sp)
  666. $UCMP $inp,$num
  667. bne Lsha2_block_private
  668. blr
  669. .long 0
  670. .byte 0,12,0x14,0,0,0,0,0
  671. .size $func,.-$func
  672. ___
  673. }
  674. # Ugly hack here, because PPC assembler syntax seem to vary too
  675. # much from platforms to platform...
  676. $code.=<<___;
  677. .align 6
  678. LPICmeup:
  679. mflr r0
  680. bcl 20,31,\$+4
  681. mflr $Tbl ; vvvvvv "distance" between . and 1st data entry
  682. addi $Tbl,$Tbl,`64-8`
  683. mtlr r0
  684. blr
  685. .long 0
  686. .byte 0,12,0x14,0,0,0,0,0
  687. .space `64-9*4`
  688. ___
  689. $code.=<<___ if ($SZ==8);
  690. .quad 0x428a2f98d728ae22,0x7137449123ef65cd
  691. .quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
  692. .quad 0x3956c25bf348b538,0x59f111f1b605d019
  693. .quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118
  694. .quad 0xd807aa98a3030242,0x12835b0145706fbe
  695. .quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
  696. .quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1
  697. .quad 0x9bdc06a725c71235,0xc19bf174cf692694
  698. .quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3
  699. .quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
  700. .quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483
  701. .quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
  702. .quad 0x983e5152ee66dfab,0xa831c66d2db43210
  703. .quad 0xb00327c898fb213f,0xbf597fc7beef0ee4
  704. .quad 0xc6e00bf33da88fc2,0xd5a79147930aa725
  705. .quad 0x06ca6351e003826f,0x142929670a0e6e70
  706. .quad 0x27b70a8546d22ffc,0x2e1b21385c26c926
  707. .quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
  708. .quad 0x650a73548baf63de,0x766a0abb3c77b2a8
  709. .quad 0x81c2c92e47edaee6,0x92722c851482353b
  710. .quad 0xa2bfe8a14cf10364,0xa81a664bbc423001
  711. .quad 0xc24b8b70d0f89791,0xc76c51a30654be30
  712. .quad 0xd192e819d6ef5218,0xd69906245565a910
  713. .quad 0xf40e35855771202a,0x106aa07032bbd1b8
  714. .quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53
  715. .quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
  716. .quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
  717. .quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
  718. .quad 0x748f82ee5defb2fc,0x78a5636f43172f60
  719. .quad 0x84c87814a1f0ab72,0x8cc702081a6439ec
  720. .quad 0x90befffa23631e28,0xa4506cebde82bde9
  721. .quad 0xbef9a3f7b2c67915,0xc67178f2e372532b
  722. .quad 0xca273eceea26619c,0xd186b8c721c0c207
  723. .quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
  724. .quad 0x06f067aa72176fba,0x0a637dc5a2c898a6
  725. .quad 0x113f9804bef90dae,0x1b710b35131c471b
  726. .quad 0x28db77f523047d84,0x32caab7b40c72493
  727. .quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
  728. .quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
  729. .quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
  730. ___
  731. $code.=<<___ if ($SZ==4);
  732. .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
  733. .long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
  734. .long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
  735. .long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
  736. .long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
  737. .long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
  738. .long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
  739. .long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
  740. .long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
  741. .long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
  742. .long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
  743. .long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070
  744. .long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
  745. .long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
  746. .long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
  747. .long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  748. ___
  749. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  750. print $code;
  751. close STDOUT;