keccak1600p8-ppc.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. #!/usr/bin/env perl
  2. # Copyright 2017-2018 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. # Keccak-1600 for PowerISA 2.07.
  17. #
  18. # June 2017.
  19. #
  20. # This is straightforward KECCAK_1X_ALT SIMD implementation, but with
  21. # disjoint Rho and Pi. The module is ABI-bitness- and endian-neutral.
  22. # POWER8 processor spends 9.8 cycles to process byte out of large
  23. # buffer for r=1088, which matches SHA3-256. This is 17% better than
  24. # scalar PPC64 code. It probably should be noted that if POWER8's
  25. # successor can achieve higher scalar instruction issue rate, then
  26. # this module will loose... And it does on POWER9 with 12.0 vs. 9.4.
  27. # $output is the last argument if it looks like a file (it has an extension)
  28. # $flavour is the first argument if it doesn't look like a file
  29. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  30. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  31. if ($flavour =~ /64/) {
  32. $SIZE_T =8;
  33. $LRSAVE =2*$SIZE_T;
  34. $UCMP ="cmpld";
  35. $STU ="stdu";
  36. $POP ="ld";
  37. $PUSH ="std";
  38. } elsif ($flavour =~ /32/) {
  39. $SIZE_T =4;
  40. $LRSAVE =$SIZE_T;
  41. $STU ="stwu";
  42. $POP ="lwz";
  43. $PUSH ="stw";
  44. $UCMP ="cmplw";
  45. } else { die "nonsense $flavour"; }
  46. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  47. ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
  48. ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
  49. die "can't locate ppc-xlate.pl";
  50. open STDOUT,"| $^X $xlate $flavour \"$output\""
  51. or die "can't call $xlate: $!";
  52. $FRAME=6*$SIZE_T+13*16; # 13*16 is for v20-v31 offload
  53. my $sp ="r1";
  54. my $iotas = "r12";
  55. ########################################################################
  56. # Register layout:
  57. #
  58. # v0 A[0][0] A[1][0]
  59. # v1 A[0][1] A[1][1]
  60. # v2 A[0][2] A[1][2]
  61. # v3 A[0][3] A[1][3]
  62. # v4 A[0][4] A[1][4]
  63. #
  64. # v5 A[2][0] A[3][0]
  65. # v6 A[2][1] A[3][1]
  66. # v7 A[2][2] A[3][2]
  67. # v8 A[2][3] A[3][3]
  68. # v9 A[2][4] A[3][4]
  69. #
  70. # v10 A[4][0] A[4][1]
  71. # v11 A[4][2] A[4][3]
  72. # v12 A[4][4] A[4][4]
  73. #
  74. # v13..25 rhotates[][]
  75. # v26..31 volatile
  76. #
  77. $code.=<<___;
  78. .machine "any"
  79. .text
  80. .type KeccakF1600_int,\@function
  81. .align 5
  82. KeccakF1600_int:
  83. li r0,24
  84. mtctr r0
  85. li r0,0
  86. b .Loop
  87. .align 4
  88. .Loop:
  89. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Theta
  90. vxor v26,v0, v5 ; A[0..1][0]^A[2..3][0]
  91. vxor v27,v1, v6 ; A[0..1][1]^A[2..3][1]
  92. vxor v28,v2, v7 ; A[0..1][2]^A[2..3][2]
  93. vxor v29,v3, v8 ; A[0..1][3]^A[2..3][3]
  94. vxor v30,v4, v9 ; A[0..1][4]^A[2..3][4]
  95. vpermdi v31,v26,v27,0b00 ; A[0][0..1]^A[2][0..1]
  96. vpermdi v26,v26,v27,0b11 ; A[1][0..1]^A[3][0..1]
  97. vpermdi v27,v28,v29,0b00 ; A[0][2..3]^A[2][2..3]
  98. vpermdi v28,v28,v29,0b11 ; A[1][2..3]^A[3][2..3]
  99. vpermdi v29,v30,v30,0b10 ; A[1..0][4]^A[3..2][4]
  100. vxor v26,v26,v31 ; C[0..1]
  101. vxor v27,v27,v28 ; C[2..3]
  102. vxor v28,v29,v30 ; C[4..4]
  103. vspltisb v31,1
  104. vxor v26,v26,v10 ; C[0..1] ^= A[4][0..1]
  105. vxor v27,v27,v11 ; C[2..3] ^= A[4][2..3]
  106. vxor v28,v28,v12 ; C[4..4] ^= A[4][4..4], low!
  107. vrld v29,v26,v31 ; ROL64(C[0..1],1)
  108. vrld v30,v27,v31 ; ROL64(C[2..3],1)
  109. vrld v31,v28,v31 ; ROL64(C[4..4],1)
  110. vpermdi v31,v31,v29,0b10
  111. vxor v26,v26,v30 ; C[0..1] ^= ROL64(C[2..3],1)
  112. vxor v27,v27,v31 ; C[2..3] ^= ROL64(C[4..0],1)
  113. vxor v28,v28,v29 ; C[4..4] ^= ROL64(C[0..1],1), low!
  114. vpermdi v29,v26,v26,0b00 ; C[0..0]
  115. vpermdi v30,v28,v26,0b10 ; C[4..0]
  116. vpermdi v31,v28,v28,0b11 ; C[4..4]
  117. vxor v1, v1, v29 ; A[0..1][1] ^= C[0..0]
  118. vxor v6, v6, v29 ; A[2..3][1] ^= C[0..0]
  119. vxor v10,v10,v30 ; A[4][0..1] ^= C[4..0]
  120. vxor v0, v0, v31 ; A[0..1][0] ^= C[4..4]
  121. vxor v5, v5, v31 ; A[2..3][0] ^= C[4..4]
  122. vpermdi v29,v27,v27,0b00 ; C[2..2]
  123. vpermdi v30,v26,v26,0b11 ; C[1..1]
  124. vpermdi v31,v26,v27,0b10 ; C[1..2]
  125. vxor v3, v3, v29 ; A[0..1][3] ^= C[2..2]
  126. vxor v8, v8, v29 ; A[2..3][3] ^= C[2..2]
  127. vxor v2, v2, v30 ; A[0..1][2] ^= C[1..1]
  128. vxor v7, v7, v30 ; A[2..3][2] ^= C[1..1]
  129. vxor v11,v11,v31 ; A[4][2..3] ^= C[1..2]
  130. vpermdi v29,v27,v27,0b11 ; C[3..3]
  131. vxor v4, v4, v29 ; A[0..1][4] ^= C[3..3]
  132. vxor v9, v9, v29 ; A[2..3][4] ^= C[3..3]
  133. vxor v12,v12,v29 ; A[4..4][4] ^= C[3..3]
  134. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Rho
  135. vrld v26,v0, v13 ; v0
  136. vrld v1, v1, v14
  137. vrld v27,v2, v15 ; v2
  138. vrld v28,v3, v16 ; v3
  139. vrld v4, v4, v17
  140. vrld v5, v5, v18
  141. vrld v6, v6, v19
  142. vrld v29,v7, v20 ; v7
  143. vrld v8, v8, v21
  144. vrld v9, v9, v22
  145. vrld v10,v10,v23
  146. vrld v30,v11,v24 ; v11
  147. vrld v12,v12,v25
  148. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Pi
  149. vpermdi v0, v26,v28,0b00 ; [0][0] [1][0] < [0][0] [0][3]
  150. vpermdi v2, v29,v5, 0b00 ; [0][2] [1][2] < [2][2] [2][0]
  151. vpermdi v11,v9, v5, 0b01 ; [4][2] [4][3] < [2][4] [3][0]
  152. vpermdi v5, v1, v4, 0b00 ; [2][0] [3][0] < [0][1] [0][4]
  153. vpermdi v1, v1, v4, 0b11 ; [0][1] [1][1] < [1][1] [1][4]
  154. vpermdi v3, v8, v6, 0b11 ; [0][3] [1][3] < [3][3] [3][1]
  155. vpermdi v4, v12,v30,0b10 ; [0][4] [1][4] < [4][4] [4][2]
  156. vpermdi v7, v8, v6, 0b00 ; [2][2] [3][2] < [2][3] [2][1]
  157. vpermdi v6, v27,v26,0b11 ; [2][1] [3][1] < [1][2] [1][0]
  158. vpermdi v8, v9, v29,0b11 ; [2][3] [3][3] < [3][4] [3][2]
  159. vpermdi v12,v10,v10,0b11 ; [4][4] [4][4] < [4][1] [4][1]
  160. vpermdi v9, v10,v30,0b01 ; [2][4] [3][4] < [4][0] [4][3]
  161. vpermdi v10,v27,v28,0b01 ; [4][0] [4][1] < [0][2] [1][3]
  162. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Chi + Iota
  163. lvx_u v31,$iotas,r0 ; iotas[index]
  164. addic r0,r0,16 ; index++
  165. vandc v26,v2, v1 ; (~A[0..1][1] & A[0..1][2])
  166. vandc v27,v3, v2 ; (~A[0..1][2] & A[0..1][3])
  167. vandc v28,v4, v3 ; (~A[0..1][3] & A[0..1][4])
  168. vandc v29,v0, v4 ; (~A[0..1][4] & A[0..1][0])
  169. vandc v30,v1, v0 ; (~A[0..1][0] & A[0..1][1])
  170. vxor v0, v0, v26 ; A[0..1][0] ^= (~A[0..1][1] & A[0..1][2])
  171. vxor v1, v1, v27 ; A[0..1][1] ^= (~A[0..1][2] & A[0..1][3])
  172. vxor v2, v2, v28 ; A[0..1][2] ^= (~A[0..1][3] & A[0..1][4])
  173. vxor v3, v3, v29 ; A[0..1][3] ^= (~A[0..1][4] & A[0..1][0])
  174. vxor v4, v4, v30 ; A[0..1][4] ^= (~A[0..1][0] & A[0..1][1])
  175. vandc v26,v7, v6 ; (~A[2..3][1] & A[2..3][2])
  176. vandc v27,v8, v7 ; (~A[2..3][2] & A[2..3][3])
  177. vandc v28,v9, v8 ; (~A[2..3][3] & A[2..3][4])
  178. vandc v29,v5, v9 ; (~A[2..3][4] & A[2..3][0])
  179. vandc v30,v6, v5 ; (~A[2..3][0] & A[2..3][1])
  180. vxor v5, v5, v26 ; A[2..3][0] ^= (~A[2..3][1] & A[2..3][2])
  181. vxor v6, v6, v27 ; A[2..3][1] ^= (~A[2..3][2] & A[2..3][3])
  182. vxor v7, v7, v28 ; A[2..3][2] ^= (~A[2..3][3] & A[2..3][4])
  183. vxor v8, v8, v29 ; A[2..3][3] ^= (~A[2..3][4] & A[2..3][0])
  184. vxor v9, v9, v30 ; A[2..3][4] ^= (~A[2..3][0] & A[2..3][1])
  185. vxor v0, v0, v31 ; A[0][0] ^= iotas[index++]
  186. vpermdi v26,v10,v11,0b10 ; A[4][1..2]
  187. vpermdi v27,v12,v10,0b00 ; A[4][4..0]
  188. vpermdi v28,v11,v12,0b10 ; A[4][3..4]
  189. vpermdi v29,v10,v10,0b10 ; A[4][1..0]
  190. vandc v26,v11,v26 ; (~A[4][1..2] & A[4][2..3])
  191. vandc v27,v27,v28 ; (~A[4][3..4] & A[4][4..0])
  192. vandc v28,v10,v29 ; (~A[4][1..0] & A[4][0..1])
  193. vxor v10,v10,v26 ; A[4][0..1] ^= (~A[4][1..2] & A[4][2..3])
  194. vxor v11,v11,v27 ; A[4][2..3] ^= (~A[4][3..4] & A[4][4..0])
  195. vxor v12,v12,v28 ; A[4][4..4] ^= (~A[4][0..1] & A[4][1..0])
  196. bdnz .Loop
  197. vpermdi v12,v12,v12,0b11 ; broadcast A[4][4]
  198. blr
  199. .long 0
  200. .byte 0,12,0x14,0,0,0,0,0
  201. .size KeccakF1600_int,.-KeccakF1600_int
  202. .type KeccakF1600,\@function
  203. .align 5
  204. KeccakF1600:
  205. $STU $sp,-$FRAME($sp)
  206. li r10,`15+6*$SIZE_T`
  207. li r11,`31+6*$SIZE_T`
  208. mflr r8
  209. mfspr r7, 256 ; save vrsave
  210. stvx v20,r10,$sp
  211. addi r10,r10,32
  212. stvx v21,r11,$sp
  213. addi r11,r11,32
  214. stvx v22,r10,$sp
  215. addi r10,r10,32
  216. stvx v23,r11,$sp
  217. addi r11,r11,32
  218. stvx v24,r10,$sp
  219. addi r10,r10,32
  220. stvx v25,r11,$sp
  221. addi r11,r11,32
  222. stvx v26,r10,$sp
  223. addi r10,r10,32
  224. stvx v27,r11,$sp
  225. addi r11,r11,32
  226. stvx v28,r10,$sp
  227. addi r10,r10,32
  228. stvx v29,r11,$sp
  229. addi r11,r11,32
  230. stvx v30,r10,$sp
  231. stvx v31,r11,$sp
  232. stw r7,`$FRAME-4`($sp) ; save vrsave
  233. li r0, -1
  234. $PUSH r8,`$FRAME+$LRSAVE`($sp)
  235. mtspr 256, r0 ; preserve all AltiVec registers
  236. li r11,16
  237. lvx_4w v0,0,r3 ; load A[5][5]
  238. li r10,32
  239. lvx_4w v1,r11,r3
  240. addi r11,r11,32
  241. lvx_4w v2,r10,r3
  242. addi r10,r10,32
  243. lvx_4w v3,r11,r3
  244. addi r11,r11,32
  245. lvx_4w v4,r10,r3
  246. addi r10,r10,32
  247. lvx_4w v5,r11,r3
  248. addi r11,r11,32
  249. lvx_4w v6,r10,r3
  250. addi r10,r10,32
  251. lvx_4w v7,r11,r3
  252. addi r11,r11,32
  253. lvx_4w v8,r10,r3
  254. addi r10,r10,32
  255. lvx_4w v9,r11,r3
  256. addi r11,r11,32
  257. lvx_4w v10,r10,r3
  258. addi r10,r10,32
  259. lvx_4w v11,r11,r3
  260. lvx_splt v12,r10,r3
  261. bl PICmeup
  262. li r11,16
  263. lvx_u v13,0,r12 ; load rhotates
  264. li r10,32
  265. lvx_u v14,r11,r12
  266. addi r11,r11,32
  267. lvx_u v15,r10,r12
  268. addi r10,r10,32
  269. lvx_u v16,r11,r12
  270. addi r11,r11,32
  271. lvx_u v17,r10,r12
  272. addi r10,r10,32
  273. lvx_u v18,r11,r12
  274. addi r11,r11,32
  275. lvx_u v19,r10,r12
  276. addi r10,r10,32
  277. lvx_u v20,r11,r12
  278. addi r11,r11,32
  279. lvx_u v21,r10,r12
  280. addi r10,r10,32
  281. lvx_u v22,r11,r12
  282. addi r11,r11,32
  283. lvx_u v23,r10,r12
  284. addi r10,r10,32
  285. lvx_u v24,r11,r12
  286. lvx_u v25,r10,r12
  287. addi r12,r12,`16*16` ; points at iotas
  288. bl KeccakF1600_int
  289. li r11,16
  290. stvx_4w v0,0,r3 ; return A[5][5]
  291. li r10,32
  292. stvx_4w v1,r11,r3
  293. addi r11,r11,32
  294. stvx_4w v2,r10,r3
  295. addi r10,r10,32
  296. stvx_4w v3,r11,r3
  297. addi r11,r11,32
  298. stvx_4w v4,r10,r3
  299. addi r10,r10,32
  300. stvx_4w v5,r11,r3
  301. addi r11,r11,32
  302. stvx_4w v6,r10,r3
  303. addi r10,r10,32
  304. stvx_4w v7,r11,r3
  305. addi r11,r11,32
  306. stvx_4w v8,r10,r3
  307. addi r10,r10,32
  308. stvx_4w v9,r11,r3
  309. addi r11,r11,32
  310. stvx_4w v10,r10,r3
  311. addi r10,r10,32
  312. stvx_4w v11,r11,r3
  313. stvdx_u v12,r10,r3
  314. li r10,`15+6*$SIZE_T`
  315. li r11,`31+6*$SIZE_T`
  316. mtlr r8
  317. mtspr 256, r7 ; restore vrsave
  318. lvx v20,r10,$sp
  319. addi r10,r10,32
  320. lvx v21,r11,$sp
  321. addi r11,r11,32
  322. lvx v22,r10,$sp
  323. addi r10,r10,32
  324. lvx v23,r11,$sp
  325. addi r11,r11,32
  326. lvx v24,r10,$sp
  327. addi r10,r10,32
  328. lvx v25,r11,$sp
  329. addi r11,r11,32
  330. lvx v26,r10,$sp
  331. addi r10,r10,32
  332. lvx v27,r11,$sp
  333. addi r11,r11,32
  334. lvx v28,r10,$sp
  335. addi r10,r10,32
  336. lvx v29,r11,$sp
  337. addi r11,r11,32
  338. lvx v30,r10,$sp
  339. lvx v31,r11,$sp
  340. addi $sp,$sp,$FRAME
  341. blr
  342. .long 0
  343. .byte 0,12,0x04,1,0x80,0,1,0
  344. .long 0
  345. .size KeccakF1600,.-KeccakF1600
  346. ___
  347. {
  348. my ($A_jagged,$inp,$len,$bsz) = map("r$_",(3..6));
  349. $code.=<<___;
  350. .globl SHA3_absorb
  351. .type SHA3_absorb,\@function
  352. .align 5
  353. SHA3_absorb:
  354. $STU $sp,-$FRAME($sp)
  355. li r10,`15+6*$SIZE_T`
  356. li r11,`31+6*$SIZE_T`
  357. mflr r8
  358. mfspr r7, 256 ; save vrsave
  359. stvx v20,r10,$sp
  360. addi r10,r10,32
  361. stvx v21,r11,$sp
  362. addi r11,r11,32
  363. stvx v22,r10,$sp
  364. addi r10,r10,32
  365. stvx v23,r11,$sp
  366. addi r11,r11,32
  367. stvx v24,r10,$sp
  368. addi r10,r10,32
  369. stvx v25,r11,$sp
  370. addi r11,r11,32
  371. stvx v26,r10,$sp
  372. addi r10,r10,32
  373. stvx v27,r11,$sp
  374. addi r11,r11,32
  375. stvx v28,r10,$sp
  376. addi r10,r10,32
  377. stvx v29,r11,$sp
  378. addi r11,r11,32
  379. stvx v30,r10,$sp
  380. stvx v31,r11,$sp
  381. stw r7,`$FRAME-4`($sp) ; save vrsave
  382. li r0, -1
  383. $PUSH r8,`$FRAME+$LRSAVE`($sp)
  384. mtspr 256, r0 ; preserve all AltiVec registers
  385. li r11,16
  386. lvx_4w v0,0,$A_jagged ; load A[5][5]
  387. li r10,32
  388. lvx_4w v1,r11,$A_jagged
  389. addi r11,r11,32
  390. lvx_4w v2,r10,$A_jagged
  391. addi r10,r10,32
  392. lvx_4w v3,r11,$A_jagged
  393. addi r11,r11,32
  394. lvx_4w v4,r10,$A_jagged
  395. addi r10,r10,32
  396. lvx_4w v5,r11,$A_jagged
  397. addi r11,r11,32
  398. lvx_4w v6,r10,$A_jagged
  399. addi r10,r10,32
  400. lvx_4w v7,r11,$A_jagged
  401. addi r11,r11,32
  402. lvx_4w v8,r10,$A_jagged
  403. addi r10,r10,32
  404. lvx_4w v9,r11,$A_jagged
  405. addi r11,r11,32
  406. lvx_4w v10,r10,$A_jagged
  407. addi r10,r10,32
  408. lvx_4w v11,r11,$A_jagged
  409. lvx_splt v12,r10,$A_jagged
  410. bl PICmeup
  411. li r11,16
  412. lvx_u v13,0,r12 ; load rhotates
  413. li r10,32
  414. lvx_u v14,r11,r12
  415. addi r11,r11,32
  416. lvx_u v15,r10,r12
  417. addi r10,r10,32
  418. lvx_u v16,r11,r12
  419. addi r11,r11,32
  420. lvx_u v17,r10,r12
  421. addi r10,r10,32
  422. lvx_u v18,r11,r12
  423. addi r11,r11,32
  424. lvx_u v19,r10,r12
  425. addi r10,r10,32
  426. lvx_u v20,r11,r12
  427. addi r11,r11,32
  428. lvx_u v21,r10,r12
  429. addi r10,r10,32
  430. lvx_u v22,r11,r12
  431. addi r11,r11,32
  432. lvx_u v23,r10,r12
  433. addi r10,r10,32
  434. lvx_u v24,r11,r12
  435. lvx_u v25,r10,r12
  436. li r10,-32
  437. li r11,-16
  438. addi r12,r12,`16*16` ; points at iotas
  439. b .Loop_absorb
  440. .align 4
  441. .Loop_absorb:
  442. $UCMP $len,$bsz ; len < bsz?
  443. blt .Labsorbed
  444. sub $len,$len,$bsz ; len -= bsz
  445. srwi r0,$bsz,3
  446. mtctr r0
  447. lvx_u v30,r10,r12 ; permutation masks
  448. lvx_u v31,r11,r12
  449. ?vspltisb v27,7 ; prepare masks for byte swap
  450. ?vxor v30,v30,v27 ; on big-endian
  451. ?vxor v31,v31,v27
  452. vxor v27,v27,v27 ; zero
  453. lvdx_u v26,0,$inp
  454. addi $inp,$inp,8
  455. vperm v26,v26,v27,v30
  456. vxor v0, v0, v26
  457. bdz .Lprocess_block
  458. lvdx_u v26,0,$inp
  459. addi $inp,$inp,8
  460. vperm v26,v26,v27,v30
  461. vxor v1, v1, v26
  462. bdz .Lprocess_block
  463. lvdx_u v26,0,$inp
  464. addi $inp,$inp,8
  465. vperm v26,v26,v27,v30
  466. vxor v2, v2, v26
  467. bdz .Lprocess_block
  468. lvdx_u v26,0,$inp
  469. addi $inp,$inp,8
  470. vperm v26,v26,v27,v30
  471. vxor v3, v3, v26
  472. bdz .Lprocess_block
  473. lvdx_u v26,0,$inp
  474. addi $inp,$inp,8
  475. vperm v26,v26,v27,v30
  476. vxor v4, v4, v26
  477. bdz .Lprocess_block
  478. lvdx_u v26,0,$inp
  479. addi $inp,$inp,8
  480. vperm v26,v26,v27,v31
  481. vxor v0, v0, v26
  482. bdz .Lprocess_block
  483. lvdx_u v26,0,$inp
  484. addi $inp,$inp,8
  485. vperm v26,v26,v27,v31
  486. vxor v1, v1, v26
  487. bdz .Lprocess_block
  488. lvdx_u v26,0,$inp
  489. addi $inp,$inp,8
  490. vperm v26,v26,v27,v31
  491. vxor v2, v2, v26
  492. bdz .Lprocess_block
  493. lvdx_u v26,0,$inp
  494. addi $inp,$inp,8
  495. vperm v26,v26,v27,v31
  496. vxor v3, v3, v26
  497. bdz .Lprocess_block
  498. lvdx_u v26,0,$inp
  499. addi $inp,$inp,8
  500. vperm v26,v26,v27,v31
  501. vxor v4, v4, v26
  502. bdz .Lprocess_block
  503. lvdx_u v26,0,$inp
  504. addi $inp,$inp,8
  505. vperm v26,v26,v27,v30
  506. vxor v5, v5, v26
  507. bdz .Lprocess_block
  508. lvdx_u v26,0,$inp
  509. addi $inp,$inp,8
  510. vperm v26,v26,v27,v30
  511. vxor v6, v6, v26
  512. bdz .Lprocess_block
  513. lvdx_u v26,0,$inp
  514. addi $inp,$inp,8
  515. vperm v26,v26,v27,v30
  516. vxor v7, v7, v26
  517. bdz .Lprocess_block
  518. lvdx_u v26,0,$inp
  519. addi $inp,$inp,8
  520. vperm v26,v26,v27,v30
  521. vxor v8, v8, v26
  522. bdz .Lprocess_block
  523. lvdx_u v26,0,$inp
  524. addi $inp,$inp,8
  525. vperm v26,v26,v27,v30
  526. vxor v9, v9, v26
  527. bdz .Lprocess_block
  528. lvdx_u v26,0,$inp
  529. addi $inp,$inp,8
  530. vperm v26,v26,v27,v31
  531. vxor v5, v5, v26
  532. bdz .Lprocess_block
  533. lvdx_u v26,0,$inp
  534. addi $inp,$inp,8
  535. vperm v26,v26,v27,v31
  536. vxor v6, v6, v26
  537. bdz .Lprocess_block
  538. lvdx_u v26,0,$inp
  539. addi $inp,$inp,8
  540. vperm v26,v26,v27,v31
  541. vxor v7, v7, v26
  542. bdz .Lprocess_block
  543. lvdx_u v26,0,$inp
  544. addi $inp,$inp,8
  545. vperm v26,v26,v27,v31
  546. vxor v8, v8, v26
  547. bdz .Lprocess_block
  548. lvdx_u v26,0,$inp
  549. addi $inp,$inp,8
  550. vperm v26,v26,v27,v31
  551. vxor v9, v9, v26
  552. bdz .Lprocess_block
  553. lvdx_u v26,0,$inp
  554. addi $inp,$inp,8
  555. vperm v26,v26,v27,v30
  556. vxor v10, v10, v26
  557. bdz .Lprocess_block
  558. lvdx_u v26,0,$inp
  559. addi $inp,$inp,8
  560. vperm v26,v26,v27,v31
  561. vxor v10, v10, v26
  562. bdz .Lprocess_block
  563. lvdx_u v26,0,$inp
  564. addi $inp,$inp,8
  565. vperm v26,v26,v27,v30
  566. vxor v11, v11, v26
  567. bdz .Lprocess_block
  568. lvdx_u v26,0,$inp
  569. addi $inp,$inp,8
  570. vperm v26,v26,v27,v31
  571. vxor v11, v11, v26
  572. bdz .Lprocess_block
  573. lvdx_u v26,0,$inp
  574. addi $inp,$inp,8
  575. vperm v26,v26,v27,v31
  576. vxor v12, v12, v26
  577. .Lprocess_block:
  578. bl KeccakF1600_int
  579. b .Loop_absorb
  580. .align 4
  581. .Labsorbed:
  582. li r11,16
  583. stvx_4w v0,0,$A_jagged ; return A[5][5]
  584. li r10,32
  585. stvx_4w v1,r11,$A_jagged
  586. addi r11,r11,32
  587. stvx_4w v2,r10,$A_jagged
  588. addi r10,r10,32
  589. stvx_4w v3,r11,$A_jagged
  590. addi r11,r11,32
  591. stvx_4w v4,r10,$A_jagged
  592. addi r10,r10,32
  593. stvx_4w v5,r11,$A_jagged
  594. addi r11,r11,32
  595. stvx_4w v6,r10,$A_jagged
  596. addi r10,r10,32
  597. stvx_4w v7,r11,$A_jagged
  598. addi r11,r11,32
  599. stvx_4w v8,r10,$A_jagged
  600. addi r10,r10,32
  601. stvx_4w v9,r11,$A_jagged
  602. addi r11,r11,32
  603. stvx_4w v10,r10,$A_jagged
  604. addi r10,r10,32
  605. stvx_4w v11,r11,$A_jagged
  606. stvdx_u v12,r10,$A_jagged
  607. mr r3,$len ; return value
  608. li r10,`15+6*$SIZE_T`
  609. li r11,`31+6*$SIZE_T`
  610. mtlr r8
  611. mtspr 256, r7 ; restore vrsave
  612. lvx v20,r10,$sp
  613. addi r10,r10,32
  614. lvx v21,r11,$sp
  615. addi r11,r11,32
  616. lvx v22,r10,$sp
  617. addi r10,r10,32
  618. lvx v23,r11,$sp
  619. addi r11,r11,32
  620. lvx v24,r10,$sp
  621. addi r10,r10,32
  622. lvx v25,r11,$sp
  623. addi r11,r11,32
  624. lvx v26,r10,$sp
  625. addi r10,r10,32
  626. lvx v27,r11,$sp
  627. addi r11,r11,32
  628. lvx v28,r10,$sp
  629. addi r10,r10,32
  630. lvx v29,r11,$sp
  631. addi r11,r11,32
  632. lvx v30,r10,$sp
  633. lvx v31,r11,$sp
  634. addi $sp,$sp,$FRAME
  635. blr
  636. .long 0
  637. .byte 0,12,0x04,1,0x80,0,4,0
  638. .long 0
  639. .size SHA3_absorb,.-SHA3_absorb
  640. ___
  641. }
  642. {
  643. my ($A_jagged,$out,$len,$bsz) = map("r$_",(3..6));
  644. $code.=<<___;
  645. .globl SHA3_squeeze
  646. .type SHA3_squeeze,\@function
  647. .align 5
  648. SHA3_squeeze:
  649. mflr r9 ; r9 is not touched by KeccakF1600
  650. subi $out,$out,1 ; prepare for stbu
  651. addi r8,$A_jagged,4 ; prepare volatiles
  652. mr r10,$bsz
  653. li r11,0
  654. b .Loop_squeeze
  655. .align 4
  656. .Loop_squeeze:
  657. lwzx r7,r11,r8 ; lo
  658. lwzx r0,r11,$A_jagged ; hi
  659. ${UCMP}i $len,8
  660. blt .Lsqueeze_tail
  661. stbu r7,1($out) ; write lo
  662. srwi r7,r7,8
  663. stbu r7,1($out)
  664. srwi r7,r7,8
  665. stbu r7,1($out)
  666. srwi r7,r7,8
  667. stbu r7,1($out)
  668. stbu r0,1($out) ; write hi
  669. srwi r0,r0,8
  670. stbu r0,1($out)
  671. srwi r0,r0,8
  672. stbu r0,1($out)
  673. srwi r0,r0,8
  674. stbu r0,1($out)
  675. subic. $len,$len,8
  676. beqlr ; return if done
  677. subic. r10,r10,8
  678. ble .Loutput_expand
  679. addi r11,r11,16 ; calculate jagged index
  680. cmplwi r11,`16*5`
  681. blt .Loop_squeeze
  682. subi r11,r11,72
  683. beq .Loop_squeeze
  684. addi r11,r11,72
  685. cmplwi r11,`16*5+8`
  686. subi r11,r11,8
  687. beq .Loop_squeeze
  688. addi r11,r11,8
  689. cmplwi r11,`16*10`
  690. subi r11,r11,72
  691. beq .Loop_squeeze
  692. addi r11,r11,72
  693. blt .Loop_squeeze
  694. subi r11,r11,8
  695. b .Loop_squeeze
  696. .align 4
  697. .Loutput_expand:
  698. bl KeccakF1600
  699. mtlr r9
  700. addi r8,$A_jagged,4 ; restore volatiles
  701. mr r10,$bsz
  702. li r11,0
  703. b .Loop_squeeze
  704. .align 4
  705. .Lsqueeze_tail:
  706. mtctr $len
  707. subic. $len,$len,4
  708. ble .Loop_tail_lo
  709. li r8,4
  710. mtctr r8
  711. .Loop_tail_lo:
  712. stbu r7,1($out)
  713. srdi r7,r7,8
  714. bdnz .Loop_tail_lo
  715. ble .Lsqueeze_done
  716. mtctr $len
  717. .Loop_tail_hi:
  718. stbu r0,1($out)
  719. srdi r0,r0,8
  720. bdnz .Loop_tail_hi
  721. .Lsqueeze_done:
  722. blr
  723. .long 0
  724. .byte 0,12,0x14,0,0,0,4,0
  725. .long 0
  726. .size SHA3_squeeze,.-SHA3_squeeze
  727. ___
  728. }
  729. $code.=<<___;
  730. .align 6
  731. PICmeup:
  732. mflr r0
  733. bcl 20,31,\$+4
  734. mflr r12 ; vvvvvv "distance" between . and 1st data entry
  735. addi r12,r12,`64-8`
  736. mtlr r0
  737. blr
  738. .long 0
  739. .byte 0,12,0x14,0,0,0,0,0
  740. .space `64-9*4`
  741. .type rhotates,\@object
  742. .align 6
  743. rhotates:
  744. .quad 0, 36
  745. .quad 1, 44
  746. .quad 62, 6
  747. .quad 28, 55
  748. .quad 27, 20
  749. .quad 3, 41
  750. .quad 10, 45
  751. .quad 43, 15
  752. .quad 25, 21
  753. .quad 39, 8
  754. .quad 18, 2
  755. .quad 61, 56
  756. .quad 14, 14
  757. .size rhotates,.-rhotates
  758. .quad 0,0
  759. .quad 0x0001020304050607,0x1011121314151617
  760. .quad 0x1011121314151617,0x0001020304050607
  761. .type iotas,\@object
  762. iotas:
  763. .quad 0x0000000000000001,0
  764. .quad 0x0000000000008082,0
  765. .quad 0x800000000000808a,0
  766. .quad 0x8000000080008000,0
  767. .quad 0x000000000000808b,0
  768. .quad 0x0000000080000001,0
  769. .quad 0x8000000080008081,0
  770. .quad 0x8000000000008009,0
  771. .quad 0x000000000000008a,0
  772. .quad 0x0000000000000088,0
  773. .quad 0x0000000080008009,0
  774. .quad 0x000000008000000a,0
  775. .quad 0x000000008000808b,0
  776. .quad 0x800000000000008b,0
  777. .quad 0x8000000000008089,0
  778. .quad 0x8000000000008003,0
  779. .quad 0x8000000000008002,0
  780. .quad 0x8000000000000080,0
  781. .quad 0x000000000000800a,0
  782. .quad 0x800000008000000a,0
  783. .quad 0x8000000080008081,0
  784. .quad 0x8000000000008080,0
  785. .quad 0x0000000080000001,0
  786. .quad 0x8000000080008008,0
  787. .size iotas,.-iotas
  788. .asciz "Keccak-1600 absorb and squeeze for PowerISA 2.07, CRYPTOGAMS by <appro\@openssl.org>"
  789. ___
  790. foreach (split("\n",$code)) {
  791. s/\`([^\`]*)\`/eval $1/ge;
  792. if ($flavour =~ /le$/) { # little-endian
  793. s/\?([a-z]+)/;$1/;
  794. } else { # big-endian
  795. s/\?([a-z]+)/$1/;
  796. }
  797. print $_,"\n";
  798. }
  799. close STDOUT;