2
0

keccak1600-armv8.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. #!/usr/bin/env perl
  2. # Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # Keccak-1600 for ARMv8.
  17. #
  18. # June 2017.
  19. #
  20. # This is straightforward KECCAK_1X_ALT implementation. It makes no
  21. # sense to attempt SIMD/NEON implementation for following reason.
  22. # 64-bit lanes of vector registers can't be addressed as easily as in
  23. # 32-bit mode. This means that 64-bit NEON is bound to be slower than
  24. # 32-bit NEON, and this implementation is faster than 32-bit NEON on
  25. # same processor. Even though it takes more scalar xor's and andn's,
  26. # it gets compensated by availability of rotate. Not to forget that
  27. # most processors achieve higher issue rate with scalar instructions.
  28. #
  29. # February 2018.
  30. #
  31. # Add hardware-assisted ARMv8.2 implementation. It's KECCAK_1X_ALT
  32. # variant with register permutation/rotation twist that allows to
  33. # eliminate copies to temporary registers. If you look closely you'll
  34. # notice that it uses only one lane of vector registers. The new
  35. # instructions effectively facilitate parallel hashing, which we don't
  36. # support [yet?]. But lowest-level core procedure is prepared for it.
  37. # The inner round is 67 [vector] instructions, so it's not actually
  38. # obvious that it will provide performance improvement [in serial
  39. # hash] as long as vector instructions issue rate is limited to 1 per
  40. # cycle...
  41. #
  42. ######################################################################
  43. # Numbers are cycles per processed byte.
  44. #
  45. # r=1088(*)
  46. #
  47. # Cortex-A53 13
  48. # Cortex-A57 12
  49. # X-Gene 14
  50. # Mongoose 10
  51. # Kryo 12
  52. # Denver 7.8
  53. # Apple A7 7.2
  54. # ThunderX2 9.7
  55. #
  56. # (*) Corresponds to SHA3-256. No improvement coefficients are listed
  57. # because they vary too much from compiler to compiler. Newer
  58. # compiler does much better and improvement varies from 5% on
  59. # Cortex-A57 to 25% on Cortex-A53. While in comparison to older
  60. # compiler this code is at least 2x faster...
  61. # $output is the last argument if it looks like a file (it has an extension)
  62. # $flavour is the first argument if it doesn't look like a file
  63. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  64. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  65. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  66. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  67. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  68. die "can't locate arm-xlate.pl";
  69. open OUT,"| \"$^X\" $xlate $flavour \"$output\""
  70. or die "can't call $xlate: $!";
  71. *STDOUT=*OUT;
  72. my @rhotates = ([ 0, 1, 62, 28, 27 ],
  73. [ 36, 44, 6, 55, 20 ],
  74. [ 3, 10, 43, 25, 39 ],
  75. [ 41, 45, 15, 21, 8 ],
  76. [ 18, 2, 61, 56, 14 ]);
  77. $code.=<<___;
  78. .text
  79. .align 8 // strategic alignment and padding that allows to use
  80. // address value as loop termination condition...
  81. .quad 0,0,0,0,0,0,0,0
  82. .type iotas,%object
  83. iotas:
  84. .quad 0x0000000000000001
  85. .quad 0x0000000000008082
  86. .quad 0x800000000000808a
  87. .quad 0x8000000080008000
  88. .quad 0x000000000000808b
  89. .quad 0x0000000080000001
  90. .quad 0x8000000080008081
  91. .quad 0x8000000000008009
  92. .quad 0x000000000000008a
  93. .quad 0x0000000000000088
  94. .quad 0x0000000080008009
  95. .quad 0x000000008000000a
  96. .quad 0x000000008000808b
  97. .quad 0x800000000000008b
  98. .quad 0x8000000000008089
  99. .quad 0x8000000000008003
  100. .quad 0x8000000000008002
  101. .quad 0x8000000000000080
  102. .quad 0x000000000000800a
  103. .quad 0x800000008000000a
  104. .quad 0x8000000080008081
  105. .quad 0x8000000000008080
  106. .quad 0x0000000080000001
  107. .quad 0x8000000080008008
  108. .size iotas,.-iotas
  109. ___
  110. {{{
  111. my @A = map([ "x$_", "x".($_+1), "x".($_+2), "x".($_+3), "x".($_+4) ],
  112. (0, 5, 10, 15, 20));
  113. $A[3][3] = "x25"; # x18 is reserved
  114. my @C = map("x$_", (26,27,28,30));
  115. $code.=<<___;
  116. .type KeccakF1600_int,%function
  117. .align 5
  118. KeccakF1600_int:
  119. adr $C[2],iotas
  120. .inst 0xd503233f // paciasp
  121. stp $C[2],x30,[sp,#16] // 32 bytes on top are mine
  122. b .Loop
  123. .align 4
  124. .Loop:
  125. ////////////////////////////////////////// Theta
  126. eor $C[0],$A[0][0],$A[1][0]
  127. stp $A[0][4],$A[1][4],[sp,#0] // offload pair...
  128. eor $C[1],$A[0][1],$A[1][1]
  129. eor $C[2],$A[0][2],$A[1][2]
  130. eor $C[3],$A[0][3],$A[1][3]
  131. ___
  132. $C[4]=$A[0][4];
  133. $C[5]=$A[1][4];
  134. $code.=<<___;
  135. eor $C[4],$A[0][4],$A[1][4]
  136. eor $C[0],$C[0],$A[2][0]
  137. eor $C[1],$C[1],$A[2][1]
  138. eor $C[2],$C[2],$A[2][2]
  139. eor $C[3],$C[3],$A[2][3]
  140. eor $C[4],$C[4],$A[2][4]
  141. eor $C[0],$C[0],$A[3][0]
  142. eor $C[1],$C[1],$A[3][1]
  143. eor $C[2],$C[2],$A[3][2]
  144. eor $C[3],$C[3],$A[3][3]
  145. eor $C[4],$C[4],$A[3][4]
  146. eor $C[0],$C[0],$A[4][0]
  147. eor $C[2],$C[2],$A[4][2]
  148. eor $C[1],$C[1],$A[4][1]
  149. eor $C[3],$C[3],$A[4][3]
  150. eor $C[4],$C[4],$A[4][4]
  151. eor $C[5],$C[0],$C[2],ror#63
  152. eor $A[0][1],$A[0][1],$C[5]
  153. eor $A[1][1],$A[1][1],$C[5]
  154. eor $A[2][1],$A[2][1],$C[5]
  155. eor $A[3][1],$A[3][1],$C[5]
  156. eor $A[4][1],$A[4][1],$C[5]
  157. eor $C[5],$C[1],$C[3],ror#63
  158. eor $C[2],$C[2],$C[4],ror#63
  159. eor $C[3],$C[3],$C[0],ror#63
  160. eor $C[4],$C[4],$C[1],ror#63
  161. eor $C[1], $A[0][2],$C[5] // mov $C[1],$A[0][2]
  162. eor $A[1][2],$A[1][2],$C[5]
  163. eor $A[2][2],$A[2][2],$C[5]
  164. eor $A[3][2],$A[3][2],$C[5]
  165. eor $A[4][2],$A[4][2],$C[5]
  166. eor $A[0][0],$A[0][0],$C[4]
  167. eor $A[1][0],$A[1][0],$C[4]
  168. eor $A[2][0],$A[2][0],$C[4]
  169. eor $A[3][0],$A[3][0],$C[4]
  170. eor $A[4][0],$A[4][0],$C[4]
  171. ___
  172. $C[4]=undef;
  173. $C[5]=undef;
  174. $code.=<<___;
  175. ldp $A[0][4],$A[1][4],[sp,#0] // re-load offloaded data
  176. eor $C[0], $A[0][3],$C[2] // mov $C[0],$A[0][3]
  177. eor $A[1][3],$A[1][3],$C[2]
  178. eor $A[2][3],$A[2][3],$C[2]
  179. eor $A[3][3],$A[3][3],$C[2]
  180. eor $A[4][3],$A[4][3],$C[2]
  181. eor $C[2], $A[0][4],$C[3] // mov $C[2],$A[0][4]
  182. eor $A[1][4],$A[1][4],$C[3]
  183. eor $A[2][4],$A[2][4],$C[3]
  184. eor $A[3][4],$A[3][4],$C[3]
  185. eor $A[4][4],$A[4][4],$C[3]
  186. ////////////////////////////////////////// Rho+Pi
  187. mov $C[3],$A[0][1]
  188. ror $A[0][1],$A[1][1],#64-$rhotates[1][1]
  189. //mov $C[1],$A[0][2]
  190. ror $A[0][2],$A[2][2],#64-$rhotates[2][2]
  191. //mov $C[0],$A[0][3]
  192. ror $A[0][3],$A[3][3],#64-$rhotates[3][3]
  193. //mov $C[2],$A[0][4]
  194. ror $A[0][4],$A[4][4],#64-$rhotates[4][4]
  195. ror $A[1][1],$A[1][4],#64-$rhotates[1][4]
  196. ror $A[2][2],$A[2][3],#64-$rhotates[2][3]
  197. ror $A[3][3],$A[3][2],#64-$rhotates[3][2]
  198. ror $A[4][4],$A[4][1],#64-$rhotates[4][1]
  199. ror $A[1][4],$A[4][2],#64-$rhotates[4][2]
  200. ror $A[2][3],$A[3][4],#64-$rhotates[3][4]
  201. ror $A[3][2],$A[2][1],#64-$rhotates[2][1]
  202. ror $A[4][1],$A[1][3],#64-$rhotates[1][3]
  203. ror $A[4][2],$A[2][4],#64-$rhotates[2][4]
  204. ror $A[3][4],$A[4][3],#64-$rhotates[4][3]
  205. ror $A[2][1],$A[1][2],#64-$rhotates[1][2]
  206. ror $A[1][3],$A[3][1],#64-$rhotates[3][1]
  207. ror $A[2][4],$A[4][0],#64-$rhotates[4][0]
  208. ror $A[4][3],$A[3][0],#64-$rhotates[3][0]
  209. ror $A[1][2],$A[2][0],#64-$rhotates[2][0]
  210. ror $A[3][1],$A[1][0],#64-$rhotates[1][0]
  211. ror $A[1][0],$C[0],#64-$rhotates[0][3]
  212. ror $A[2][0],$C[3],#64-$rhotates[0][1]
  213. ror $A[3][0],$C[2],#64-$rhotates[0][4]
  214. ror $A[4][0],$C[1],#64-$rhotates[0][2]
  215. ////////////////////////////////////////// Chi+Iota
  216. bic $C[0],$A[0][2],$A[0][1]
  217. bic $C[1],$A[0][3],$A[0][2]
  218. bic $C[2],$A[0][0],$A[0][4]
  219. bic $C[3],$A[0][1],$A[0][0]
  220. eor $A[0][0],$A[0][0],$C[0]
  221. bic $C[0],$A[0][4],$A[0][3]
  222. eor $A[0][1],$A[0][1],$C[1]
  223. ldr $C[1],[sp,#16]
  224. eor $A[0][3],$A[0][3],$C[2]
  225. eor $A[0][4],$A[0][4],$C[3]
  226. eor $A[0][2],$A[0][2],$C[0]
  227. ldr $C[3],[$C[1]],#8 // Iota[i++]
  228. bic $C[0],$A[1][2],$A[1][1]
  229. tst $C[1],#255 // are we done?
  230. str $C[1],[sp,#16]
  231. bic $C[1],$A[1][3],$A[1][2]
  232. bic $C[2],$A[1][0],$A[1][4]
  233. eor $A[0][0],$A[0][0],$C[3] // A[0][0] ^= Iota
  234. bic $C[3],$A[1][1],$A[1][0]
  235. eor $A[1][0],$A[1][0],$C[0]
  236. bic $C[0],$A[1][4],$A[1][3]
  237. eor $A[1][1],$A[1][1],$C[1]
  238. eor $A[1][3],$A[1][3],$C[2]
  239. eor $A[1][4],$A[1][4],$C[3]
  240. eor $A[1][2],$A[1][2],$C[0]
  241. bic $C[0],$A[2][2],$A[2][1]
  242. bic $C[1],$A[2][3],$A[2][2]
  243. bic $C[2],$A[2][0],$A[2][4]
  244. bic $C[3],$A[2][1],$A[2][0]
  245. eor $A[2][0],$A[2][0],$C[0]
  246. bic $C[0],$A[2][4],$A[2][3]
  247. eor $A[2][1],$A[2][1],$C[1]
  248. eor $A[2][3],$A[2][3],$C[2]
  249. eor $A[2][4],$A[2][4],$C[3]
  250. eor $A[2][2],$A[2][2],$C[0]
  251. bic $C[0],$A[3][2],$A[3][1]
  252. bic $C[1],$A[3][3],$A[3][2]
  253. bic $C[2],$A[3][0],$A[3][4]
  254. bic $C[3],$A[3][1],$A[3][0]
  255. eor $A[3][0],$A[3][0],$C[0]
  256. bic $C[0],$A[3][4],$A[3][3]
  257. eor $A[3][1],$A[3][1],$C[1]
  258. eor $A[3][3],$A[3][3],$C[2]
  259. eor $A[3][4],$A[3][4],$C[3]
  260. eor $A[3][2],$A[3][2],$C[0]
  261. bic $C[0],$A[4][2],$A[4][1]
  262. bic $C[1],$A[4][3],$A[4][2]
  263. bic $C[2],$A[4][0],$A[4][4]
  264. bic $C[3],$A[4][1],$A[4][0]
  265. eor $A[4][0],$A[4][0],$C[0]
  266. bic $C[0],$A[4][4],$A[4][3]
  267. eor $A[4][1],$A[4][1],$C[1]
  268. eor $A[4][3],$A[4][3],$C[2]
  269. eor $A[4][4],$A[4][4],$C[3]
  270. eor $A[4][2],$A[4][2],$C[0]
  271. bne .Loop
  272. ldr x30,[sp,#24]
  273. .inst 0xd50323bf // autiasp
  274. ret
  275. .size KeccakF1600_int,.-KeccakF1600_int
  276. .type KeccakF1600,%function
  277. .align 5
  278. KeccakF1600:
  279. .inst 0xd503233f // paciasp
  280. stp x29,x30,[sp,#-128]!
  281. add x29,sp,#0
  282. stp x19,x20,[sp,#16]
  283. stp x21,x22,[sp,#32]
  284. stp x23,x24,[sp,#48]
  285. stp x25,x26,[sp,#64]
  286. stp x27,x28,[sp,#80]
  287. sub sp,sp,#48
  288. str x0,[sp,#32] // offload argument
  289. mov $C[0],x0
  290. ldp $A[0][0],$A[0][1],[x0,#16*0]
  291. ldp $A[0][2],$A[0][3],[$C[0],#16*1]
  292. ldp $A[0][4],$A[1][0],[$C[0],#16*2]
  293. ldp $A[1][1],$A[1][2],[$C[0],#16*3]
  294. ldp $A[1][3],$A[1][4],[$C[0],#16*4]
  295. ldp $A[2][0],$A[2][1],[$C[0],#16*5]
  296. ldp $A[2][2],$A[2][3],[$C[0],#16*6]
  297. ldp $A[2][4],$A[3][0],[$C[0],#16*7]
  298. ldp $A[3][1],$A[3][2],[$C[0],#16*8]
  299. ldp $A[3][3],$A[3][4],[$C[0],#16*9]
  300. ldp $A[4][0],$A[4][1],[$C[0],#16*10]
  301. ldp $A[4][2],$A[4][3],[$C[0],#16*11]
  302. ldr $A[4][4],[$C[0],#16*12]
  303. bl KeccakF1600_int
  304. ldr $C[0],[sp,#32]
  305. stp $A[0][0],$A[0][1],[$C[0],#16*0]
  306. stp $A[0][2],$A[0][3],[$C[0],#16*1]
  307. stp $A[0][4],$A[1][0],[$C[0],#16*2]
  308. stp $A[1][1],$A[1][2],[$C[0],#16*3]
  309. stp $A[1][3],$A[1][4],[$C[0],#16*4]
  310. stp $A[2][0],$A[2][1],[$C[0],#16*5]
  311. stp $A[2][2],$A[2][3],[$C[0],#16*6]
  312. stp $A[2][4],$A[3][0],[$C[0],#16*7]
  313. stp $A[3][1],$A[3][2],[$C[0],#16*8]
  314. stp $A[3][3],$A[3][4],[$C[0],#16*9]
  315. stp $A[4][0],$A[4][1],[$C[0],#16*10]
  316. stp $A[4][2],$A[4][3],[$C[0],#16*11]
  317. str $A[4][4],[$C[0],#16*12]
  318. ldp x19,x20,[x29,#16]
  319. add sp,sp,#48
  320. ldp x21,x22,[x29,#32]
  321. ldp x23,x24,[x29,#48]
  322. ldp x25,x26,[x29,#64]
  323. ldp x27,x28,[x29,#80]
  324. ldp x29,x30,[sp],#128
  325. .inst 0xd50323bf // autiasp
  326. ret
  327. .size KeccakF1600,.-KeccakF1600
  328. .globl SHA3_absorb
  329. .type SHA3_absorb,%function
  330. .align 5
  331. SHA3_absorb:
  332. .inst 0xd503233f // paciasp
  333. stp x29,x30,[sp,#-128]!
  334. add x29,sp,#0
  335. stp x19,x20,[sp,#16]
  336. stp x21,x22,[sp,#32]
  337. stp x23,x24,[sp,#48]
  338. stp x25,x26,[sp,#64]
  339. stp x27,x28,[sp,#80]
  340. sub sp,sp,#64
  341. stp x0,x1,[sp,#32] // offload arguments
  342. stp x2,x3,[sp,#48]
  343. mov $C[0],x0 // uint64_t A[5][5]
  344. mov $C[1],x1 // const void *inp
  345. mov $C[2],x2 // size_t len
  346. mov $C[3],x3 // size_t bsz
  347. ldp $A[0][0],$A[0][1],[$C[0],#16*0]
  348. ldp $A[0][2],$A[0][3],[$C[0],#16*1]
  349. ldp $A[0][4],$A[1][0],[$C[0],#16*2]
  350. ldp $A[1][1],$A[1][2],[$C[0],#16*3]
  351. ldp $A[1][3],$A[1][4],[$C[0],#16*4]
  352. ldp $A[2][0],$A[2][1],[$C[0],#16*5]
  353. ldp $A[2][2],$A[2][3],[$C[0],#16*6]
  354. ldp $A[2][4],$A[3][0],[$C[0],#16*7]
  355. ldp $A[3][1],$A[3][2],[$C[0],#16*8]
  356. ldp $A[3][3],$A[3][4],[$C[0],#16*9]
  357. ldp $A[4][0],$A[4][1],[$C[0],#16*10]
  358. ldp $A[4][2],$A[4][3],[$C[0],#16*11]
  359. ldr $A[4][4],[$C[0],#16*12]
  360. b .Loop_absorb
  361. .align 4
  362. .Loop_absorb:
  363. subs $C[0],$C[2],$C[3] // len - bsz
  364. blo .Labsorbed
  365. str $C[0],[sp,#48] // save len - bsz
  366. ___
  367. for (my $i=0; $i<24; $i+=2) {
  368. my $j = $i+1;
  369. $code.=<<___;
  370. ldr $C[0],[$C[1]],#8 // *inp++
  371. #ifdef __AARCH64EB__
  372. rev $C[0],$C[0]
  373. #endif
  374. eor $A[$i/5][$i%5],$A[$i/5][$i%5],$C[0]
  375. cmp $C[3],#8*($i+2)
  376. blo .Lprocess_block
  377. ldr $C[0],[$C[1]],#8 // *inp++
  378. #ifdef __AARCH64EB__
  379. rev $C[0],$C[0]
  380. #endif
  381. eor $A[$j/5][$j%5],$A[$j/5][$j%5],$C[0]
  382. beq .Lprocess_block
  383. ___
  384. }
  385. $code.=<<___;
  386. ldr $C[0],[$C[1]],#8 // *inp++
  387. #ifdef __AARCH64EB__
  388. rev $C[0],$C[0]
  389. #endif
  390. eor $A[4][4],$A[4][4],$C[0]
  391. .Lprocess_block:
  392. str $C[1],[sp,#40] // save inp
  393. bl KeccakF1600_int
  394. ldr $C[1],[sp,#40] // restore arguments
  395. ldp $C[2],$C[3],[sp,#48]
  396. b .Loop_absorb
  397. .align 4
  398. .Labsorbed:
  399. ldr $C[1],[sp,#32]
  400. stp $A[0][0],$A[0][1],[$C[1],#16*0]
  401. stp $A[0][2],$A[0][3],[$C[1],#16*1]
  402. stp $A[0][4],$A[1][0],[$C[1],#16*2]
  403. stp $A[1][1],$A[1][2],[$C[1],#16*3]
  404. stp $A[1][3],$A[1][4],[$C[1],#16*4]
  405. stp $A[2][0],$A[2][1],[$C[1],#16*5]
  406. stp $A[2][2],$A[2][3],[$C[1],#16*6]
  407. stp $A[2][4],$A[3][0],[$C[1],#16*7]
  408. stp $A[3][1],$A[3][2],[$C[1],#16*8]
  409. stp $A[3][3],$A[3][4],[$C[1],#16*9]
  410. stp $A[4][0],$A[4][1],[$C[1],#16*10]
  411. stp $A[4][2],$A[4][3],[$C[1],#16*11]
  412. str $A[4][4],[$C[1],#16*12]
  413. mov x0,$C[2] // return value
  414. ldp x19,x20,[x29,#16]
  415. add sp,sp,#64
  416. ldp x21,x22,[x29,#32]
  417. ldp x23,x24,[x29,#48]
  418. ldp x25,x26,[x29,#64]
  419. ldp x27,x28,[x29,#80]
  420. ldp x29,x30,[sp],#128
  421. .inst 0xd50323bf // autiasp
  422. ret
  423. .size SHA3_absorb,.-SHA3_absorb
  424. ___
  425. {
  426. my ($A_flat,$out,$len,$bsz) = map("x$_",(19..22));
  427. $code.=<<___;
  428. .globl SHA3_squeeze
  429. .type SHA3_squeeze,%function
  430. .align 5
  431. SHA3_squeeze:
  432. .inst 0xd503233f // paciasp
  433. stp x29,x30,[sp,#-48]!
  434. add x29,sp,#0
  435. stp x19,x20,[sp,#16]
  436. stp x21,x22,[sp,#32]
  437. mov $A_flat,x0 // put aside arguments
  438. mov $out,x1
  439. mov $len,x2
  440. mov $bsz,x3
  441. .Loop_squeeze:
  442. ldr x4,[x0],#8
  443. cmp $len,#8
  444. blo .Lsqueeze_tail
  445. #ifdef __AARCH64EB__
  446. rev x4,x4
  447. #endif
  448. str x4,[$out],#8
  449. subs $len,$len,#8
  450. beq .Lsqueeze_done
  451. subs x3,x3,#8
  452. bhi .Loop_squeeze
  453. mov x0,$A_flat
  454. bl KeccakF1600
  455. mov x0,$A_flat
  456. mov x3,$bsz
  457. b .Loop_squeeze
  458. .align 4
  459. .Lsqueeze_tail:
  460. strb w4,[$out],#1
  461. lsr x4,x4,#8
  462. subs $len,$len,#1
  463. beq .Lsqueeze_done
  464. strb w4,[$out],#1
  465. lsr x4,x4,#8
  466. subs $len,$len,#1
  467. beq .Lsqueeze_done
  468. strb w4,[$out],#1
  469. lsr x4,x4,#8
  470. subs $len,$len,#1
  471. beq .Lsqueeze_done
  472. strb w4,[$out],#1
  473. lsr x4,x4,#8
  474. subs $len,$len,#1
  475. beq .Lsqueeze_done
  476. strb w4,[$out],#1
  477. lsr x4,x4,#8
  478. subs $len,$len,#1
  479. beq .Lsqueeze_done
  480. strb w4,[$out],#1
  481. lsr x4,x4,#8
  482. subs $len,$len,#1
  483. beq .Lsqueeze_done
  484. strb w4,[$out],#1
  485. .Lsqueeze_done:
  486. ldp x19,x20,[sp,#16]
  487. ldp x21,x22,[sp,#32]
  488. ldp x29,x30,[sp],#48
  489. .inst 0xd50323bf // autiasp
  490. ret
  491. .size SHA3_squeeze,.-SHA3_squeeze
  492. ___
  493. } }}}
  494. {{{
  495. my @A = map([ "v".$_.".16b", "v".($_+1).".16b", "v".($_+2).".16b",
  496. "v".($_+3).".16b", "v".($_+4).".16b" ],
  497. (0, 5, 10, 15, 20));
  498. my @C = map("v$_.16b", (25..31));
  499. my @D = @C[4,5,6,2,3];
  500. $code.=<<___;
  501. .type KeccakF1600_ce,%function
  502. .align 5
  503. KeccakF1600_ce:
  504. mov x9,#24
  505. adr x10,iotas
  506. b .Loop_ce
  507. .align 4
  508. .Loop_ce:
  509. ////////////////////////////////////////////////// Theta
  510. eor3 $C[0],$A[4][0],$A[3][0],$A[2][0]
  511. eor3 $C[1],$A[4][1],$A[3][1],$A[2][1]
  512. eor3 $C[2],$A[4][2],$A[3][2],$A[2][2]
  513. eor3 $C[3],$A[4][3],$A[3][3],$A[2][3]
  514. eor3 $C[4],$A[4][4],$A[3][4],$A[2][4]
  515. eor3 $C[0],$C[0], $A[1][0],$A[0][0]
  516. eor3 $C[1],$C[1], $A[1][1],$A[0][1]
  517. eor3 $C[2],$C[2], $A[1][2],$A[0][2]
  518. eor3 $C[3],$C[3], $A[1][3],$A[0][3]
  519. eor3 $C[4],$C[4], $A[1][4],$A[0][4]
  520. rax1 $C[5],$C[0],$C[2] // D[1]
  521. rax1 $C[6],$C[1],$C[3] // D[2]
  522. rax1 $C[2],$C[2],$C[4] // D[3]
  523. rax1 $C[3],$C[3],$C[0] // D[4]
  524. rax1 $C[4],$C[4],$C[1] // D[0]
  525. ////////////////////////////////////////////////// Theta+Rho+Pi
  526. xar $C[0], $A[0][1],$D[1],#64-$rhotates[0][1] // C[0]=A[2][0]
  527. xar $A[0][1],$A[1][1],$D[1],#64-$rhotates[1][1]
  528. xar $A[1][1],$A[1][4],$D[4],#64-$rhotates[1][4]
  529. xar $A[1][4],$A[4][2],$D[2],#64-$rhotates[4][2]
  530. xar $A[4][2],$A[2][4],$D[4],#64-$rhotates[2][4]
  531. xar $A[2][4],$A[4][0],$D[0],#64-$rhotates[4][0]
  532. xar $C[1], $A[0][2],$D[2],#64-$rhotates[0][2] // C[1]=A[4][0]
  533. xar $A[0][2],$A[2][2],$D[2],#64-$rhotates[2][2]
  534. xar $A[2][2],$A[2][3],$D[3],#64-$rhotates[2][3]
  535. xar $A[2][3],$A[3][4],$D[4],#64-$rhotates[3][4]
  536. xar $A[3][4],$A[4][3],$D[3],#64-$rhotates[4][3]
  537. xar $A[4][3],$A[3][0],$D[0],#64-$rhotates[3][0]
  538. xar $A[3][0],$A[0][4],$D[4],#64-$rhotates[0][4]
  539. xar $D[4], $A[4][4],$D[4],#64-$rhotates[4][4] // D[4]=A[0][4]
  540. xar $A[4][4],$A[4][1],$D[1],#64-$rhotates[4][1]
  541. xar $A[1][3],$A[1][3],$D[3],#64-$rhotates[1][3] // A[1][3]=A[4][1]
  542. xar $A[0][4],$A[3][1],$D[1],#64-$rhotates[3][1] // A[0][4]=A[1][3]
  543. xar $A[3][1],$A[1][0],$D[0],#64-$rhotates[1][0]
  544. xar $A[1][0],$A[0][3],$D[3],#64-$rhotates[0][3]
  545. eor $A[0][0],$A[0][0],$D[0]
  546. xar $D[3], $A[3][3],$D[3],#64-$rhotates[3][3] // D[3]=A[0][3]
  547. xar $A[0][3],$A[3][2],$D[2],#64-$rhotates[3][2] // A[0][3]=A[3][3]
  548. xar $D[1], $A[2][1],$D[1],#64-$rhotates[2][1] // D[1]=A[3][2]
  549. xar $D[2], $A[1][2],$D[2],#64-$rhotates[1][2] // D[2]=A[2][1]
  550. xar $D[0], $A[2][0],$D[0],#64-$rhotates[2][0] // D[0]=A[1][2]
  551. ////////////////////////////////////////////////// Chi+Iota
  552. bcax $A[4][0],$C[1], $A[4][2],$A[1][3] // A[1][3]=A[4][1]
  553. bcax $A[4][1],$A[1][3],$A[4][3],$A[4][2] // A[1][3]=A[4][1]
  554. bcax $A[4][2],$A[4][2],$A[4][4],$A[4][3]
  555. bcax $A[4][3],$A[4][3],$C[1], $A[4][4]
  556. bcax $A[4][4],$A[4][4],$A[1][3],$C[1] // A[1][3]=A[4][1]
  557. ld1r {$C[1]},[x10],#8
  558. bcax $A[3][2],$D[1], $A[3][4],$A[0][3] // A[0][3]=A[3][3]
  559. bcax $A[3][3],$A[0][3],$A[3][0],$A[3][4] // A[0][3]=A[3][3]
  560. bcax $A[3][4],$A[3][4],$A[3][1],$A[3][0]
  561. bcax $A[3][0],$A[3][0],$D[1], $A[3][1]
  562. bcax $A[3][1],$A[3][1],$A[0][3],$D[1] // A[0][3]=A[3][3]
  563. bcax $A[2][0],$C[0], $A[2][2],$D[2]
  564. bcax $A[2][1],$D[2], $A[2][3],$A[2][2]
  565. bcax $A[2][2],$A[2][2],$A[2][4],$A[2][3]
  566. bcax $A[2][3],$A[2][3],$C[0], $A[2][4]
  567. bcax $A[2][4],$A[2][4],$D[2], $C[0]
  568. bcax $A[1][2],$D[0], $A[1][4],$A[0][4] // A[0][4]=A[1][3]
  569. bcax $A[1][3],$A[0][4],$A[1][0],$A[1][4] // A[0][4]=A[1][3]
  570. bcax $A[1][4],$A[1][4],$A[1][1],$A[1][0]
  571. bcax $A[1][0],$A[1][0],$D[0], $A[1][1]
  572. bcax $A[1][1],$A[1][1],$A[0][4],$D[0] // A[0][4]=A[1][3]
  573. bcax $A[0][3],$D[3], $A[0][0],$D[4]
  574. bcax $A[0][4],$D[4], $A[0][1],$A[0][0]
  575. bcax $A[0][0],$A[0][0],$A[0][2],$A[0][1]
  576. bcax $A[0][1],$A[0][1],$D[3], $A[0][2]
  577. bcax $A[0][2],$A[0][2],$D[4], $D[3]
  578. eor $A[0][0],$A[0][0],$C[1]
  579. subs x9,x9,#1
  580. bne .Loop_ce
  581. ret
  582. .size KeccakF1600_ce,.-KeccakF1600_ce
  583. .type KeccakF1600_cext,%function
  584. .align 5
  585. KeccakF1600_cext:
  586. .inst 0xd503233f // paciasp
  587. stp x29,x30,[sp,#-80]!
  588. add x29,sp,#0
  589. stp d8,d9,[sp,#16] // per ABI requirement
  590. stp d10,d11,[sp,#32]
  591. stp d12,d13,[sp,#48]
  592. stp d14,d15,[sp,#64]
  593. ___
  594. for($i=0; $i<24; $i+=2) { # load A[5][5]
  595. my $j=$i+1;
  596. $code.=<<___;
  597. ldp d$i,d$j,[x0,#8*$i]
  598. ___
  599. }
  600. $code.=<<___;
  601. ldr d24,[x0,#8*$i]
  602. bl KeccakF1600_ce
  603. ldr x30,[sp,#8]
  604. ___
  605. for($i=0; $i<24; $i+=2) { # store A[5][5]
  606. my $j=$i+1;
  607. $code.=<<___;
  608. stp d$i,d$j,[x0,#8*$i]
  609. ___
  610. }
  611. $code.=<<___;
  612. str d24,[x0,#8*$i]
  613. ldp d8,d9,[sp,#16]
  614. ldp d10,d11,[sp,#32]
  615. ldp d12,d13,[sp,#48]
  616. ldp d14,d15,[sp,#64]
  617. ldr x29,[sp],#80
  618. .inst 0xd50323bf // autiasp
  619. ret
  620. .size KeccakF1600_cext,.-KeccakF1600_cext
  621. ___
  622. {
  623. my ($ctx,$inp,$len,$bsz) = map("x$_",(0..3));
  624. $code.=<<___;
  625. .globl SHA3_absorb_cext
  626. .type SHA3_absorb_cext,%function
  627. .align 5
  628. SHA3_absorb_cext:
  629. .inst 0xd503233f // paciasp
  630. stp x29,x30,[sp,#-80]!
  631. add x29,sp,#0
  632. stp d8,d9,[sp,#16] // per ABI requirement
  633. stp d10,d11,[sp,#32]
  634. stp d12,d13,[sp,#48]
  635. stp d14,d15,[sp,#64]
  636. ___
  637. for($i=0; $i<24; $i+=2) { # load A[5][5]
  638. my $j=$i+1;
  639. $code.=<<___;
  640. ldp d$i,d$j,[x0,#8*$i]
  641. ___
  642. }
  643. $code.=<<___;
  644. ldr d24,[x0,#8*$i]
  645. b .Loop_absorb_ce
  646. .align 4
  647. .Loop_absorb_ce:
  648. subs $len,$len,$bsz // len - bsz
  649. blo .Labsorbed_ce
  650. ___
  651. for (my $i=0; $i<24; $i+=2) {
  652. my $j = $i+1;
  653. $code.=<<___;
  654. ldr d31,[$inp],#8 // *inp++
  655. #ifdef __AARCH64EB__
  656. rev64 v31.16b,v31.16b
  657. #endif
  658. eor $A[$i/5][$i%5],$A[$i/5][$i%5],v31.16b
  659. cmp $bsz,#8*($i+2)
  660. blo .Lprocess_block_ce
  661. ldr d31,[$inp],#8 // *inp++
  662. #ifdef __AARCH64EB__
  663. rev64 v31.16b,v31.16b
  664. #endif
  665. eor $A[$j/5][$j%5],$A[$j/5][$j%5],v31.16b
  666. beq .Lprocess_block_ce
  667. ___
  668. }
  669. $code.=<<___;
  670. ldr d31,[$inp],#8 // *inp++
  671. #ifdef __AARCH64EB__
  672. rev64 v31.16b,v31.16b
  673. #endif
  674. eor $A[4][4],$A[4][4],v31.16b
  675. .Lprocess_block_ce:
  676. bl KeccakF1600_ce
  677. b .Loop_absorb_ce
  678. .align 4
  679. .Labsorbed_ce:
  680. ___
  681. for($i=0; $i<24; $i+=2) { # store A[5][5]
  682. my $j=$i+1;
  683. $code.=<<___;
  684. stp d$i,d$j,[x0,#8*$i]
  685. ___
  686. }
  687. $code.=<<___;
  688. str d24,[x0,#8*$i]
  689. add x0,$len,$bsz // return value
  690. ldp d8,d9,[sp,#16]
  691. ldp d10,d11,[sp,#32]
  692. ldp d12,d13,[sp,#48]
  693. ldp d14,d15,[sp,#64]
  694. ldp x29,x30,[sp],#80
  695. .inst 0xd50323bf // autiasp
  696. ret
  697. .size SHA3_absorb_cext,.-SHA3_absorb_cext
  698. ___
  699. }
  700. {
  701. my ($ctx,$out,$len,$bsz) = map("x$_",(0..3));
  702. $code.=<<___;
  703. .globl SHA3_squeeze_cext
  704. .type SHA3_squeeze_cext,%function
  705. .align 5
  706. SHA3_squeeze_cext:
  707. .inst 0xd503233f // paciasp
  708. stp x29,x30,[sp,#-16]!
  709. add x29,sp,#0
  710. mov x9,$ctx
  711. mov x10,$bsz
  712. .Loop_squeeze_ce:
  713. ldr x4,[x9],#8
  714. cmp $len,#8
  715. blo .Lsqueeze_tail_ce
  716. #ifdef __AARCH64EB__
  717. rev x4,x4
  718. #endif
  719. str x4,[$out],#8
  720. beq .Lsqueeze_done_ce
  721. sub $len,$len,#8
  722. subs x10,x10,#8
  723. bhi .Loop_squeeze_ce
  724. bl KeccakF1600_cext
  725. ldr x30,[sp,#8]
  726. mov x9,$ctx
  727. mov x10,$bsz
  728. b .Loop_squeeze_ce
  729. .align 4
  730. .Lsqueeze_tail_ce:
  731. strb w4,[$out],#1
  732. lsr x4,x4,#8
  733. subs $len,$len,#1
  734. beq .Lsqueeze_done_ce
  735. strb w4,[$out],#1
  736. lsr x4,x4,#8
  737. subs $len,$len,#1
  738. beq .Lsqueeze_done_ce
  739. strb w4,[$out],#1
  740. lsr x4,x4,#8
  741. subs $len,$len,#1
  742. beq .Lsqueeze_done_ce
  743. strb w4,[$out],#1
  744. lsr x4,x4,#8
  745. subs $len,$len,#1
  746. beq .Lsqueeze_done_ce
  747. strb w4,[$out],#1
  748. lsr x4,x4,#8
  749. subs $len,$len,#1
  750. beq .Lsqueeze_done_ce
  751. strb w4,[$out],#1
  752. lsr x4,x4,#8
  753. subs $len,$len,#1
  754. beq .Lsqueeze_done_ce
  755. strb w4,[$out],#1
  756. .Lsqueeze_done_ce:
  757. ldr x29,[sp],#16
  758. .inst 0xd50323bf // autiasp
  759. ret
  760. .size SHA3_squeeze_cext,.-SHA3_squeeze_cext
  761. ___
  762. } }}}
  763. $code.=<<___;
  764. .asciz "Keccak-1600 absorb and squeeze for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  765. ___
  766. { my %opcode = (
  767. "rax1" => 0xce608c00, "eor3" => 0xce000000,
  768. "bcax" => 0xce200000, "xar" => 0xce800000 );
  769. sub unsha3 {
  770. my ($mnemonic,$arg)=@_;
  771. $arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv#]([0-9\-]+))?)?/
  772. &&
  773. sprintf ".inst\t0x%08x\t//%s %s",
  774. $opcode{$mnemonic}|$1|($2<<5)|($3<<16)|(eval($4)<<10),
  775. $mnemonic,$arg;
  776. }
  777. }
  778. foreach(split("\n",$code)) {
  779. s/\`([^\`]*)\`/eval($1)/ge;
  780. m/\bld1r\b/ and s/\.16b/.2d/g or
  781. s/\b(eor3|rax1|xar|bcax)\s+(v.*)/unsha3($1,$2)/ge;
  782. print $_,"\n";
  783. }
  784. close STDOUT or die "error closing STDOUT: $!";