2
0

sha512p8-ppc.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  4. # project. The module is, however, dual licensed under OpenSSL and
  5. # CRYPTOGAMS licenses depending on where you obtain it. For further
  6. # details see http://www.openssl.org/~appro/cryptogams/.
  7. # ====================================================================
  8. # SHA256/512 for PowerISA v2.07.
  9. #
  10. # Accurate performance measurements are problematic, because it's
  11. # always virtualized setup with possibly throttled processor.
  12. # Relative comparison is therefore more informative. This module is
  13. # ~60% faster than integer-only sha512-ppc.pl. To anchor to something
  14. # else, SHA256 is 24% slower than sha1-ppc.pl and 2.5x slower than
  15. # hardware-assisted aes-128-cbc encrypt. SHA512 is 20% faster than
  16. # sha1-ppc.pl and 1.6x slower than aes-128-cbc. Another interesting
  17. # result is degree of computational resources' utilization. POWER8 is
  18. # "massively multi-threaded chip" and difference between single- and
  19. # maximum multi-process benchmark results tells that utlization is
  20. # whooping 94%. For sha512-ppc.pl we get [not unimpressive] 84% and
  21. # for sha1-ppc.pl - 73%. 100% means that multi-process result equals
  22. # to single-process one, given that all threads end up on the same
  23. # physical core.
  24. #
  25. #######################################################################
  26. #
  27. # SHA256/pre-2.07(*) SHA512/pre-2.07(*) SHA1(*)
  28. # POWER8 9.3 /14.8 5.8 /9.5 7.1
  29. #
  30. # (*) presented for reference/comparison purposes;
  31. $flavour=shift;
  32. $output =shift;
  33. if ($flavour =~ /64/) {
  34. $SIZE_T=8;
  35. $LRSAVE=2*$SIZE_T;
  36. $STU="stdu";
  37. $POP="ld";
  38. $PUSH="std";
  39. } elsif ($flavour =~ /32/) {
  40. $SIZE_T=4;
  41. $LRSAVE=$SIZE_T;
  42. $STU="stwu";
  43. $POP="lwz";
  44. $PUSH="stw";
  45. } else { die "nonsense $flavour"; }
  46. $LENDIAN=($flavour=~/le/);
  47. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  48. ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
  49. ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
  50. die "can't locate ppc-xlate.pl";
  51. open STDOUT,"| $^X $xlate $flavour $output" || die "can't call $xlate: $!";
  52. if ($output =~ /512/) {
  53. $bits=512;
  54. $SZ=8;
  55. $sz="d";
  56. $rounds=80;
  57. } else {
  58. $bits=256;
  59. $SZ=4;
  60. $sz="w";
  61. $rounds=64;
  62. }
  63. $func="sha${bits}_block_p8";
  64. $FRAME=8*$SIZE_T;
  65. $sp ="r1";
  66. $toc="r2";
  67. $ctx="r3";
  68. $inp="r4";
  69. $num="r5";
  70. $Tbl="r6";
  71. $idx="r7";
  72. $lrsave="r8";
  73. $offload="r11";
  74. $vrsave="r12";
  75. ($x00,$x10,$x20,$x30,$x40,$x50,$x60,$x70)=map("r$_",(0,10,26..31));
  76. $x00=0 if ($flavour =~ /osx/);
  77. @V=($A,$B,$C,$D,$E,$F,$G,$H)=map("v$_",(0..7));
  78. @X=map("v$_",(8..23));
  79. ($Ki,$Func,$S0,$S1,$s0,$s1,$lemask)=map("v$_",(24..31));
  80. sub ROUND {
  81. my ($i,$a,$b,$c,$d,$e,$f,$g,$h)=@_;
  82. my $j=($i+1)%16;
  83. $code.=<<___ if ($i<15 && ($i%(16/$SZ))==(16/$SZ-1));
  84. lvx_u @X[$i+1],0,$inp ; load X[i] in advance
  85. addi $inp,$inp,16
  86. ___
  87. $code.=<<___ if ($i<16 && ($i%(16/$SZ)));
  88. vsldoi @X[$i],@X[$i-1],@X[$i-1],$SZ
  89. ___
  90. $code.=<<___ if ($LENDIAN && $i<16 && ($i%(16/$SZ))==0);
  91. vperm @X[$i],@X[$i],@X[$i],$lemask
  92. ___
  93. $code.=<<___;
  94. `"vshasigma${sz} $s0,@X[($j+1)%16],0,0" if ($i>=15)`
  95. vsel $Func,$g,$f,$e ; Ch(e,f,g)
  96. vshasigma${sz} $S1,$e,1,15 ; Sigma1(e)
  97. vaddu${sz}m $h,$h,@X[$i%16] ; h+=X[i]
  98. vshasigma${sz} $S0,$a,1,0 ; Sigma0(a)
  99. `"vshasigma${sz} $s1,@X[($j+14)%16],0,15" if ($i>=15)`
  100. vaddu${sz}m $h,$h,$Func ; h+=Ch(e,f,g)
  101. vxor $Func,$a,$b
  102. `"vaddu${sz}m @X[$j],@X[$j],@X[($j+9)%16]" if ($i>=15)`
  103. vaddu${sz}m $h,$h,$S1 ; h+=Sigma1(e)
  104. vsel $Func,$b,$c,$Func ; Maj(a,b,c)
  105. vaddu${sz}m $g,$g,$Ki ; future h+=K[i]
  106. vaddu${sz}m $d,$d,$h ; d+=h
  107. vaddu${sz}m $S0,$S0,$Func ; Sigma0(a)+Maj(a,b,c)
  108. `"vaddu${sz}m @X[$j],@X[$j],$s0" if ($i>=15)`
  109. lvx $Ki,$idx,$Tbl ; load next K[i]
  110. addi $idx,$idx,16
  111. vaddu${sz}m $h,$h,$S0 ; h+=Sigma0(a)+Maj(a,b,c)
  112. `"vaddu${sz}m @X[$j],@X[$j],$s1" if ($i>=15)`
  113. ___
  114. }
  115. $code=<<___;
  116. .machine "any"
  117. .text
  118. .globl $func
  119. .align 6
  120. $func:
  121. $STU $sp,-`($FRAME+21*16+6*$SIZE_T)`($sp)
  122. mflr $lrsave
  123. li r10,`$FRAME+8*16+15`
  124. li r11,`$FRAME+8*16+31`
  125. stvx v20,r10,$sp # ABI says so
  126. addi r10,r10,32
  127. mfspr $vrsave,256
  128. stvx v21,r11,$sp
  129. addi r11,r11,32
  130. stvx v22,r10,$sp
  131. addi r10,r10,32
  132. stvx v23,r11,$sp
  133. addi r11,r11,32
  134. stvx v24,r10,$sp
  135. addi r10,r10,32
  136. stvx v25,r11,$sp
  137. addi r11,r11,32
  138. stvx v26,r10,$sp
  139. addi r10,r10,32
  140. stvx v27,r11,$sp
  141. addi r11,r11,32
  142. stvx v28,r10,$sp
  143. addi r10,r10,32
  144. stvx v29,r11,$sp
  145. addi r11,r11,32
  146. stvx v30,r10,$sp
  147. stvx v31,r11,$sp
  148. li r11,-1
  149. stw $vrsave,`$FRAME+21*16-4`($sp) # save vrsave
  150. li $x10,0x10
  151. $PUSH r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  152. li $x20,0x20
  153. $PUSH r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  154. li $x30,0x30
  155. $PUSH r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  156. li $x40,0x40
  157. $PUSH r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  158. li $x50,0x50
  159. $PUSH r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  160. li $x60,0x60
  161. $PUSH r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  162. li $x70,0x70
  163. $PUSH $lrsave,`$FRAME+21*16+6*$SIZE_T+$LRSAVE`($sp)
  164. mtspr 256,r11
  165. bl LPICmeup
  166. addi $offload,$sp,$FRAME+15
  167. ___
  168. $code.=<<___ if ($LENDIAN);
  169. li $idx,8
  170. lvsl $lemask,0,$idx
  171. vspltisb $Ki,0x0f
  172. vxor $lemask,$lemask,$Ki
  173. ___
  174. $code.=<<___ if ($SZ==4);
  175. lvx_4w $A,$x00,$ctx
  176. lvx_4w $E,$x10,$ctx
  177. vsldoi $B,$A,$A,4 # unpack
  178. vsldoi $C,$A,$A,8
  179. vsldoi $D,$A,$A,12
  180. vsldoi $F,$E,$E,4
  181. vsldoi $G,$E,$E,8
  182. vsldoi $H,$E,$E,12
  183. ___
  184. $code.=<<___ if ($SZ==8);
  185. lvx_u $A,$x00,$ctx
  186. lvx_u $C,$x10,$ctx
  187. lvx_u $E,$x20,$ctx
  188. vsldoi $B,$A,$A,8 # unpack
  189. lvx_u $G,$x30,$ctx
  190. vsldoi $D,$C,$C,8
  191. vsldoi $F,$E,$E,8
  192. vsldoi $H,$G,$G,8
  193. ___
  194. $code.=<<___;
  195. li r0,`($rounds-16)/16` # inner loop counter
  196. b Loop
  197. .align 5
  198. Loop:
  199. lvx $Ki,$x00,$Tbl
  200. li $idx,16
  201. lvx_u @X[0],0,$inp
  202. addi $inp,$inp,16
  203. stvx $A,$x00,$offload # offload $A-$H
  204. stvx $B,$x10,$offload
  205. stvx $C,$x20,$offload
  206. stvx $D,$x30,$offload
  207. stvx $E,$x40,$offload
  208. stvx $F,$x50,$offload
  209. stvx $G,$x60,$offload
  210. stvx $H,$x70,$offload
  211. vaddu${sz}m $H,$H,$Ki # h+K[i]
  212. lvx $Ki,$idx,$Tbl
  213. addi $idx,$idx,16
  214. ___
  215. for ($i=0;$i<16;$i++) { &ROUND($i,@V); unshift(@V,pop(@V)); }
  216. $code.=<<___;
  217. mtctr r0
  218. b L16_xx
  219. .align 5
  220. L16_xx:
  221. ___
  222. for (;$i<32;$i++) { &ROUND($i,@V); unshift(@V,pop(@V)); }
  223. $code.=<<___;
  224. bdnz L16_xx
  225. lvx @X[2],$x00,$offload
  226. subic. $num,$num,1
  227. lvx @X[3],$x10,$offload
  228. vaddu${sz}m $A,$A,@X[2]
  229. lvx @X[4],$x20,$offload
  230. vaddu${sz}m $B,$B,@X[3]
  231. lvx @X[5],$x30,$offload
  232. vaddu${sz}m $C,$C,@X[4]
  233. lvx @X[6],$x40,$offload
  234. vaddu${sz}m $D,$D,@X[5]
  235. lvx @X[7],$x50,$offload
  236. vaddu${sz}m $E,$E,@X[6]
  237. lvx @X[8],$x60,$offload
  238. vaddu${sz}m $F,$F,@X[7]
  239. lvx @X[9],$x70,$offload
  240. vaddu${sz}m $G,$G,@X[8]
  241. vaddu${sz}m $H,$H,@X[9]
  242. bne Loop
  243. ___
  244. $code.=<<___ if ($SZ==4);
  245. lvx @X[0],$idx,$Tbl
  246. addi $idx,$idx,16
  247. vperm $A,$A,$B,$Ki # pack the answer
  248. lvx @X[1],$idx,$Tbl
  249. vperm $E,$E,$F,$Ki
  250. vperm $A,$A,$C,@X[0]
  251. vperm $E,$E,$G,@X[0]
  252. vperm $A,$A,$D,@X[1]
  253. vperm $E,$E,$H,@X[1]
  254. stvx_4w $A,$x00,$ctx
  255. stvx_4w $E,$x10,$ctx
  256. ___
  257. $code.=<<___ if ($SZ==8);
  258. vperm $A,$A,$B,$Ki # pack the answer
  259. vperm $C,$C,$D,$Ki
  260. vperm $E,$E,$F,$Ki
  261. vperm $G,$G,$H,$Ki
  262. stvx_u $A,$x00,$ctx
  263. stvx_u $C,$x10,$ctx
  264. stvx_u $E,$x20,$ctx
  265. stvx_u $G,$x30,$ctx
  266. ___
  267. $code.=<<___;
  268. li r10,`$FRAME+8*16+15`
  269. mtlr $lrsave
  270. li r11,`$FRAME+8*16+31`
  271. mtspr 256,$vrsave
  272. lvx v20,r10,$sp # ABI says so
  273. addi r10,r10,32
  274. lvx v21,r11,$sp
  275. addi r11,r11,32
  276. lvx v22,r10,$sp
  277. addi r10,r10,32
  278. lvx v23,r11,$sp
  279. addi r11,r11,32
  280. lvx v24,r10,$sp
  281. addi r10,r10,32
  282. lvx v25,r11,$sp
  283. addi r11,r11,32
  284. lvx v26,r10,$sp
  285. addi r10,r10,32
  286. lvx v27,r11,$sp
  287. addi r11,r11,32
  288. lvx v28,r10,$sp
  289. addi r10,r10,32
  290. lvx v29,r11,$sp
  291. addi r11,r11,32
  292. lvx v30,r10,$sp
  293. lvx v31,r11,$sp
  294. $POP r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  295. $POP r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  296. $POP r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  297. $POP r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  298. $POP r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  299. $POP r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  300. addi $sp,$sp,`$FRAME+21*16+6*$SIZE_T`
  301. blr
  302. .long 0
  303. .byte 0,12,4,1,0x80,6,3,0
  304. .long 0
  305. .size $func,.-$func
  306. ___
  307. # Ugly hack here, because PPC assembler syntax seem to vary too
  308. # much from platforms to platform...
  309. $code.=<<___;
  310. .align 6
  311. LPICmeup:
  312. mflr r0
  313. bcl 20,31,\$+4
  314. mflr $Tbl ; vvvvvv "distance" between . and 1st data entry
  315. addi $Tbl,$Tbl,`64-8`
  316. mtlr r0
  317. blr
  318. .long 0
  319. .byte 0,12,0x14,0,0,0,0,0
  320. .space `64-9*4`
  321. ___
  322. if ($SZ==8) {
  323. local *table = sub {
  324. foreach(@_) { $code.=".quad $_,$_\n"; }
  325. };
  326. table(
  327. "0x428a2f98d728ae22","0x7137449123ef65cd",
  328. "0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc",
  329. "0x3956c25bf348b538","0x59f111f1b605d019",
  330. "0x923f82a4af194f9b","0xab1c5ed5da6d8118",
  331. "0xd807aa98a3030242","0x12835b0145706fbe",
  332. "0x243185be4ee4b28c","0x550c7dc3d5ffb4e2",
  333. "0x72be5d74f27b896f","0x80deb1fe3b1696b1",
  334. "0x9bdc06a725c71235","0xc19bf174cf692694",
  335. "0xe49b69c19ef14ad2","0xefbe4786384f25e3",
  336. "0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65",
  337. "0x2de92c6f592b0275","0x4a7484aa6ea6e483",
  338. "0x5cb0a9dcbd41fbd4","0x76f988da831153b5",
  339. "0x983e5152ee66dfab","0xa831c66d2db43210",
  340. "0xb00327c898fb213f","0xbf597fc7beef0ee4",
  341. "0xc6e00bf33da88fc2","0xd5a79147930aa725",
  342. "0x06ca6351e003826f","0x142929670a0e6e70",
  343. "0x27b70a8546d22ffc","0x2e1b21385c26c926",
  344. "0x4d2c6dfc5ac42aed","0x53380d139d95b3df",
  345. "0x650a73548baf63de","0x766a0abb3c77b2a8",
  346. "0x81c2c92e47edaee6","0x92722c851482353b",
  347. "0xa2bfe8a14cf10364","0xa81a664bbc423001",
  348. "0xc24b8b70d0f89791","0xc76c51a30654be30",
  349. "0xd192e819d6ef5218","0xd69906245565a910",
  350. "0xf40e35855771202a","0x106aa07032bbd1b8",
  351. "0x19a4c116b8d2d0c8","0x1e376c085141ab53",
  352. "0x2748774cdf8eeb99","0x34b0bcb5e19b48a8",
  353. "0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb",
  354. "0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3",
  355. "0x748f82ee5defb2fc","0x78a5636f43172f60",
  356. "0x84c87814a1f0ab72","0x8cc702081a6439ec",
  357. "0x90befffa23631e28","0xa4506cebde82bde9",
  358. "0xbef9a3f7b2c67915","0xc67178f2e372532b",
  359. "0xca273eceea26619c","0xd186b8c721c0c207",
  360. "0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178",
  361. "0x06f067aa72176fba","0x0a637dc5a2c898a6",
  362. "0x113f9804bef90dae","0x1b710b35131c471b",
  363. "0x28db77f523047d84","0x32caab7b40c72493",
  364. "0x3c9ebe0a15c9bebc","0x431d67c49c100d4c",
  365. "0x4cc5d4becb3e42b6","0x597f299cfc657e2a",
  366. "0x5fcb6fab3ad6faec","0x6c44198c4a475817","0");
  367. $code.=<<___ if (!$LENDIAN);
  368. .quad 0x0001020304050607,0x1011121314151617
  369. ___
  370. $code.=<<___ if ($LENDIAN); # quad-swapped
  371. .quad 0x1011121314151617,0x0001020304050607
  372. ___
  373. } else {
  374. local *table = sub {
  375. foreach(@_) { $code.=".long $_,$_,$_,$_\n"; }
  376. };
  377. table(
  378. "0x428a2f98","0x71374491","0xb5c0fbcf","0xe9b5dba5",
  379. "0x3956c25b","0x59f111f1","0x923f82a4","0xab1c5ed5",
  380. "0xd807aa98","0x12835b01","0x243185be","0x550c7dc3",
  381. "0x72be5d74","0x80deb1fe","0x9bdc06a7","0xc19bf174",
  382. "0xe49b69c1","0xefbe4786","0x0fc19dc6","0x240ca1cc",
  383. "0x2de92c6f","0x4a7484aa","0x5cb0a9dc","0x76f988da",
  384. "0x983e5152","0xa831c66d","0xb00327c8","0xbf597fc7",
  385. "0xc6e00bf3","0xd5a79147","0x06ca6351","0x14292967",
  386. "0x27b70a85","0x2e1b2138","0x4d2c6dfc","0x53380d13",
  387. "0x650a7354","0x766a0abb","0x81c2c92e","0x92722c85",
  388. "0xa2bfe8a1","0xa81a664b","0xc24b8b70","0xc76c51a3",
  389. "0xd192e819","0xd6990624","0xf40e3585","0x106aa070",
  390. "0x19a4c116","0x1e376c08","0x2748774c","0x34b0bcb5",
  391. "0x391c0cb3","0x4ed8aa4a","0x5b9cca4f","0x682e6ff3",
  392. "0x748f82ee","0x78a5636f","0x84c87814","0x8cc70208",
  393. "0x90befffa","0xa4506ceb","0xbef9a3f7","0xc67178f2","0");
  394. $code.=<<___ if (!$LENDIAN);
  395. .long 0x00010203,0x10111213,0x10111213,0x10111213
  396. .long 0x00010203,0x04050607,0x10111213,0x10111213
  397. .long 0x00010203,0x04050607,0x08090a0b,0x10111213
  398. ___
  399. $code.=<<___ if ($LENDIAN); # word-swapped
  400. .long 0x10111213,0x10111213,0x10111213,0x00010203
  401. .long 0x10111213,0x10111213,0x04050607,0x00010203
  402. .long 0x10111213,0x08090a0b,0x04050607,0x00010203
  403. ___
  404. }
  405. $code.=<<___;
  406. .asciz "SHA${bits} for PowerISA 2.07, CRYPTOGAMS by <appro\@openssl.org>"
  407. .align 2
  408. ___
  409. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  410. print $code;
  411. close STDOUT;