keccak1600-armv8.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. #!/usr/bin/env perl
  2. # Copyright 2017-2022 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. #include "arm_arch.h"
  79. .text
  80. .align 8 // strategic alignment and padding that allows to use
  81. // address value as loop termination condition...
  82. .quad 0,0,0,0,0,0,0,0
  83. .type iotas,%object
  84. iotas:
  85. .quad 0x0000000000000001
  86. .quad 0x0000000000008082
  87. .quad 0x800000000000808a
  88. .quad 0x8000000080008000
  89. .quad 0x000000000000808b
  90. .quad 0x0000000080000001
  91. .quad 0x8000000080008081
  92. .quad 0x8000000000008009
  93. .quad 0x000000000000008a
  94. .quad 0x0000000000000088
  95. .quad 0x0000000080008009
  96. .quad 0x000000008000000a
  97. .quad 0x000000008000808b
  98. .quad 0x800000000000008b
  99. .quad 0x8000000000008089
  100. .quad 0x8000000000008003
  101. .quad 0x8000000000008002
  102. .quad 0x8000000000000080
  103. .quad 0x000000000000800a
  104. .quad 0x800000008000000a
  105. .quad 0x8000000080008081
  106. .quad 0x8000000000008080
  107. .quad 0x0000000080000001
  108. .quad 0x8000000080008008
  109. .size iotas,.-iotas
  110. ___
  111. {{{
  112. my @A = map([ "x$_", "x".($_+1), "x".($_+2), "x".($_+3), "x".($_+4) ],
  113. (0, 5, 10, 15, 20));
  114. $A[3][3] = "x25"; # x18 is reserved
  115. my @C = map("x$_", (26,27,28,30));
  116. $code.=<<___;
  117. .type KeccakF1600_int,%function
  118. .align 5
  119. KeccakF1600_int:
  120. AARCH64_SIGN_LINK_REGISTER
  121. adr $C[2],iotas
  122. stp $C[2],x30,[sp,#16] // 32 bytes on top are mine
  123. b .Loop
  124. .align 4
  125. .Loop:
  126. ////////////////////////////////////////// Theta
  127. eor $C[0],$A[0][0],$A[1][0]
  128. stp $A[0][4],$A[1][4],[sp,#0] // offload pair...
  129. eor $C[1],$A[0][1],$A[1][1]
  130. eor $C[2],$A[0][2],$A[1][2]
  131. eor $C[3],$A[0][3],$A[1][3]
  132. ___
  133. $C[4]=$A[0][4];
  134. $C[5]=$A[1][4];
  135. $code.=<<___;
  136. eor $C[4],$A[0][4],$A[1][4]
  137. eor $C[0],$C[0],$A[2][0]
  138. eor $C[1],$C[1],$A[2][1]
  139. eor $C[2],$C[2],$A[2][2]
  140. eor $C[3],$C[3],$A[2][3]
  141. eor $C[4],$C[4],$A[2][4]
  142. eor $C[0],$C[0],$A[3][0]
  143. eor $C[1],$C[1],$A[3][1]
  144. eor $C[2],$C[2],$A[3][2]
  145. eor $C[3],$C[3],$A[3][3]
  146. eor $C[4],$C[4],$A[3][4]
  147. eor $C[0],$C[0],$A[4][0]
  148. eor $C[2],$C[2],$A[4][2]
  149. eor $C[1],$C[1],$A[4][1]
  150. eor $C[3],$C[3],$A[4][3]
  151. eor $C[4],$C[4],$A[4][4]
  152. eor $C[5],$C[0],$C[2],ror#63
  153. eor $A[0][1],$A[0][1],$C[5]
  154. eor $A[1][1],$A[1][1],$C[5]
  155. eor $A[2][1],$A[2][1],$C[5]
  156. eor $A[3][1],$A[3][1],$C[5]
  157. eor $A[4][1],$A[4][1],$C[5]
  158. eor $C[5],$C[1],$C[3],ror#63
  159. eor $C[2],$C[2],$C[4],ror#63
  160. eor $C[3],$C[3],$C[0],ror#63
  161. eor $C[4],$C[4],$C[1],ror#63
  162. eor $C[1], $A[0][2],$C[5] // mov $C[1],$A[0][2]
  163. eor $A[1][2],$A[1][2],$C[5]
  164. eor $A[2][2],$A[2][2],$C[5]
  165. eor $A[3][2],$A[3][2],$C[5]
  166. eor $A[4][2],$A[4][2],$C[5]
  167. eor $A[0][0],$A[0][0],$C[4]
  168. eor $A[1][0],$A[1][0],$C[4]
  169. eor $A[2][0],$A[2][0],$C[4]
  170. eor $A[3][0],$A[3][0],$C[4]
  171. eor $A[4][0],$A[4][0],$C[4]
  172. ___
  173. $C[4]=undef;
  174. $C[5]=undef;
  175. $code.=<<___;
  176. ldp $A[0][4],$A[1][4],[sp,#0] // re-load offloaded data
  177. eor $C[0], $A[0][3],$C[2] // mov $C[0],$A[0][3]
  178. eor $A[1][3],$A[1][3],$C[2]
  179. eor $A[2][3],$A[2][3],$C[2]
  180. eor $A[3][3],$A[3][3],$C[2]
  181. eor $A[4][3],$A[4][3],$C[2]
  182. eor $C[2], $A[0][4],$C[3] // mov $C[2],$A[0][4]
  183. eor $A[1][4],$A[1][4],$C[3]
  184. eor $A[2][4],$A[2][4],$C[3]
  185. eor $A[3][4],$A[3][4],$C[3]
  186. eor $A[4][4],$A[4][4],$C[3]
  187. ////////////////////////////////////////// Rho+Pi
  188. mov $C[3],$A[0][1]
  189. ror $A[0][1],$A[1][1],#64-$rhotates[1][1]
  190. //mov $C[1],$A[0][2]
  191. ror $A[0][2],$A[2][2],#64-$rhotates[2][2]
  192. //mov $C[0],$A[0][3]
  193. ror $A[0][3],$A[3][3],#64-$rhotates[3][3]
  194. //mov $C[2],$A[0][4]
  195. ror $A[0][4],$A[4][4],#64-$rhotates[4][4]
  196. ror $A[1][1],$A[1][4],#64-$rhotates[1][4]
  197. ror $A[2][2],$A[2][3],#64-$rhotates[2][3]
  198. ror $A[3][3],$A[3][2],#64-$rhotates[3][2]
  199. ror $A[4][4],$A[4][1],#64-$rhotates[4][1]
  200. ror $A[1][4],$A[4][2],#64-$rhotates[4][2]
  201. ror $A[2][3],$A[3][4],#64-$rhotates[3][4]
  202. ror $A[3][2],$A[2][1],#64-$rhotates[2][1]
  203. ror $A[4][1],$A[1][3],#64-$rhotates[1][3]
  204. ror $A[4][2],$A[2][4],#64-$rhotates[2][4]
  205. ror $A[3][4],$A[4][3],#64-$rhotates[4][3]
  206. ror $A[2][1],$A[1][2],#64-$rhotates[1][2]
  207. ror $A[1][3],$A[3][1],#64-$rhotates[3][1]
  208. ror $A[2][4],$A[4][0],#64-$rhotates[4][0]
  209. ror $A[4][3],$A[3][0],#64-$rhotates[3][0]
  210. ror $A[1][2],$A[2][0],#64-$rhotates[2][0]
  211. ror $A[3][1],$A[1][0],#64-$rhotates[1][0]
  212. ror $A[1][0],$C[0],#64-$rhotates[0][3]
  213. ror $A[2][0],$C[3],#64-$rhotates[0][1]
  214. ror $A[3][0],$C[2],#64-$rhotates[0][4]
  215. ror $A[4][0],$C[1],#64-$rhotates[0][2]
  216. ////////////////////////////////////////// Chi+Iota
  217. bic $C[0],$A[0][2],$A[0][1]
  218. bic $C[1],$A[0][3],$A[0][2]
  219. bic $C[2],$A[0][0],$A[0][4]
  220. bic $C[3],$A[0][1],$A[0][0]
  221. eor $A[0][0],$A[0][0],$C[0]
  222. bic $C[0],$A[0][4],$A[0][3]
  223. eor $A[0][1],$A[0][1],$C[1]
  224. ldr $C[1],[sp,#16]
  225. eor $A[0][3],$A[0][3],$C[2]
  226. eor $A[0][4],$A[0][4],$C[3]
  227. eor $A[0][2],$A[0][2],$C[0]
  228. ldr $C[3],[$C[1]],#8 // Iota[i++]
  229. bic $C[0],$A[1][2],$A[1][1]
  230. tst $C[1],#255 // are we done?
  231. str $C[1],[sp,#16]
  232. bic $C[1],$A[1][3],$A[1][2]
  233. bic $C[2],$A[1][0],$A[1][4]
  234. eor $A[0][0],$A[0][0],$C[3] // A[0][0] ^= Iota
  235. bic $C[3],$A[1][1],$A[1][0]
  236. eor $A[1][0],$A[1][0],$C[0]
  237. bic $C[0],$A[1][4],$A[1][3]
  238. eor $A[1][1],$A[1][1],$C[1]
  239. eor $A[1][3],$A[1][3],$C[2]
  240. eor $A[1][4],$A[1][4],$C[3]
  241. eor $A[1][2],$A[1][2],$C[0]
  242. bic $C[0],$A[2][2],$A[2][1]
  243. bic $C[1],$A[2][3],$A[2][2]
  244. bic $C[2],$A[2][0],$A[2][4]
  245. bic $C[3],$A[2][1],$A[2][0]
  246. eor $A[2][0],$A[2][0],$C[0]
  247. bic $C[0],$A[2][4],$A[2][3]
  248. eor $A[2][1],$A[2][1],$C[1]
  249. eor $A[2][3],$A[2][3],$C[2]
  250. eor $A[2][4],$A[2][4],$C[3]
  251. eor $A[2][2],$A[2][2],$C[0]
  252. bic $C[0],$A[3][2],$A[3][1]
  253. bic $C[1],$A[3][3],$A[3][2]
  254. bic $C[2],$A[3][0],$A[3][4]
  255. bic $C[3],$A[3][1],$A[3][0]
  256. eor $A[3][0],$A[3][0],$C[0]
  257. bic $C[0],$A[3][4],$A[3][3]
  258. eor $A[3][1],$A[3][1],$C[1]
  259. eor $A[3][3],$A[3][3],$C[2]
  260. eor $A[3][4],$A[3][4],$C[3]
  261. eor $A[3][2],$A[3][2],$C[0]
  262. bic $C[0],$A[4][2],$A[4][1]
  263. bic $C[1],$A[4][3],$A[4][2]
  264. bic $C[2],$A[4][0],$A[4][4]
  265. bic $C[3],$A[4][1],$A[4][0]
  266. eor $A[4][0],$A[4][0],$C[0]
  267. bic $C[0],$A[4][4],$A[4][3]
  268. eor $A[4][1],$A[4][1],$C[1]
  269. eor $A[4][3],$A[4][3],$C[2]
  270. eor $A[4][4],$A[4][4],$C[3]
  271. eor $A[4][2],$A[4][2],$C[0]
  272. bne .Loop
  273. ldr x30,[sp,#24]
  274. AARCH64_VALIDATE_LINK_REGISTER
  275. ret
  276. .size KeccakF1600_int,.-KeccakF1600_int
  277. .type KeccakF1600,%function
  278. .align 5
  279. KeccakF1600:
  280. AARCH64_SIGN_LINK_REGISTER
  281. stp x29,x30,[sp,#-128]!
  282. add x29,sp,#0
  283. stp x19,x20,[sp,#16]
  284. stp x21,x22,[sp,#32]
  285. stp x23,x24,[sp,#48]
  286. stp x25,x26,[sp,#64]
  287. stp x27,x28,[sp,#80]
  288. sub sp,sp,#48
  289. str x0,[sp,#32] // offload argument
  290. mov $C[0],x0
  291. ldp $A[0][0],$A[0][1],[x0,#16*0]
  292. ldp $A[0][2],$A[0][3],[$C[0],#16*1]
  293. ldp $A[0][4],$A[1][0],[$C[0],#16*2]
  294. ldp $A[1][1],$A[1][2],[$C[0],#16*3]
  295. ldp $A[1][3],$A[1][4],[$C[0],#16*4]
  296. ldp $A[2][0],$A[2][1],[$C[0],#16*5]
  297. ldp $A[2][2],$A[2][3],[$C[0],#16*6]
  298. ldp $A[2][4],$A[3][0],[$C[0],#16*7]
  299. ldp $A[3][1],$A[3][2],[$C[0],#16*8]
  300. ldp $A[3][3],$A[3][4],[$C[0],#16*9]
  301. ldp $A[4][0],$A[4][1],[$C[0],#16*10]
  302. ldp $A[4][2],$A[4][3],[$C[0],#16*11]
  303. ldr $A[4][4],[$C[0],#16*12]
  304. bl KeccakF1600_int
  305. ldr $C[0],[sp,#32]
  306. stp $A[0][0],$A[0][1],[$C[0],#16*0]
  307. stp $A[0][2],$A[0][3],[$C[0],#16*1]
  308. stp $A[0][4],$A[1][0],[$C[0],#16*2]
  309. stp $A[1][1],$A[1][2],[$C[0],#16*3]
  310. stp $A[1][3],$A[1][4],[$C[0],#16*4]
  311. stp $A[2][0],$A[2][1],[$C[0],#16*5]
  312. stp $A[2][2],$A[2][3],[$C[0],#16*6]
  313. stp $A[2][4],$A[3][0],[$C[0],#16*7]
  314. stp $A[3][1],$A[3][2],[$C[0],#16*8]
  315. stp $A[3][3],$A[3][4],[$C[0],#16*9]
  316. stp $A[4][0],$A[4][1],[$C[0],#16*10]
  317. stp $A[4][2],$A[4][3],[$C[0],#16*11]
  318. str $A[4][4],[$C[0],#16*12]
  319. ldp x19,x20,[x29,#16]
  320. add sp,sp,#48
  321. ldp x21,x22,[x29,#32]
  322. ldp x23,x24,[x29,#48]
  323. ldp x25,x26,[x29,#64]
  324. ldp x27,x28,[x29,#80]
  325. ldp x29,x30,[sp],#128
  326. AARCH64_VALIDATE_LINK_REGISTER
  327. ret
  328. .size KeccakF1600,.-KeccakF1600
  329. .globl SHA3_absorb
  330. .type SHA3_absorb,%function
  331. .align 5
  332. SHA3_absorb:
  333. AARCH64_SIGN_LINK_REGISTER
  334. stp x29,x30,[sp,#-128]!
  335. add x29,sp,#0
  336. stp x19,x20,[sp,#16]
  337. stp x21,x22,[sp,#32]
  338. stp x23,x24,[sp,#48]
  339. stp x25,x26,[sp,#64]
  340. stp x27,x28,[sp,#80]
  341. sub sp,sp,#64
  342. stp x0,x1,[sp,#32] // offload arguments
  343. stp x2,x3,[sp,#48]
  344. mov $C[0],x0 // uint64_t A[5][5]
  345. mov $C[1],x1 // const void *inp
  346. mov $C[2],x2 // size_t len
  347. mov $C[3],x3 // size_t bsz
  348. ldp $A[0][0],$A[0][1],[$C[0],#16*0]
  349. ldp $A[0][2],$A[0][3],[$C[0],#16*1]
  350. ldp $A[0][4],$A[1][0],[$C[0],#16*2]
  351. ldp $A[1][1],$A[1][2],[$C[0],#16*3]
  352. ldp $A[1][3],$A[1][4],[$C[0],#16*4]
  353. ldp $A[2][0],$A[2][1],[$C[0],#16*5]
  354. ldp $A[2][2],$A[2][3],[$C[0],#16*6]
  355. ldp $A[2][4],$A[3][0],[$C[0],#16*7]
  356. ldp $A[3][1],$A[3][2],[$C[0],#16*8]
  357. ldp $A[3][3],$A[3][4],[$C[0],#16*9]
  358. ldp $A[4][0],$A[4][1],[$C[0],#16*10]
  359. ldp $A[4][2],$A[4][3],[$C[0],#16*11]
  360. ldr $A[4][4],[$C[0],#16*12]
  361. b .Loop_absorb
  362. .align 4
  363. .Loop_absorb:
  364. subs $C[0],$C[2],$C[3] // len - bsz
  365. blo .Labsorbed
  366. str $C[0],[sp,#48] // save len - bsz
  367. ___
  368. for (my $i=0; $i<24; $i+=2) {
  369. my $j = $i+1;
  370. $code.=<<___;
  371. ldr $C[0],[$C[1]],#8 // *inp++
  372. #ifdef __AARCH64EB__
  373. rev $C[0],$C[0]
  374. #endif
  375. eor $A[$i/5][$i%5],$A[$i/5][$i%5],$C[0]
  376. cmp $C[3],#8*($i+2)
  377. blo .Lprocess_block
  378. ldr $C[0],[$C[1]],#8 // *inp++
  379. #ifdef __AARCH64EB__
  380. rev $C[0],$C[0]
  381. #endif
  382. eor $A[$j/5][$j%5],$A[$j/5][$j%5],$C[0]
  383. beq .Lprocess_block
  384. ___
  385. }
  386. $code.=<<___;
  387. ldr $C[0],[$C[1]],#8 // *inp++
  388. #ifdef __AARCH64EB__
  389. rev $C[0],$C[0]
  390. #endif
  391. eor $A[4][4],$A[4][4],$C[0]
  392. .Lprocess_block:
  393. str $C[1],[sp,#40] // save inp
  394. bl KeccakF1600_int
  395. ldr $C[1],[sp,#40] // restore arguments
  396. ldp $C[2],$C[3],[sp,#48]
  397. b .Loop_absorb
  398. .align 4
  399. .Labsorbed:
  400. ldr $C[1],[sp,#32]
  401. stp $A[0][0],$A[0][1],[$C[1],#16*0]
  402. stp $A[0][2],$A[0][3],[$C[1],#16*1]
  403. stp $A[0][4],$A[1][0],[$C[1],#16*2]
  404. stp $A[1][1],$A[1][2],[$C[1],#16*3]
  405. stp $A[1][3],$A[1][4],[$C[1],#16*4]
  406. stp $A[2][0],$A[2][1],[$C[1],#16*5]
  407. stp $A[2][2],$A[2][3],[$C[1],#16*6]
  408. stp $A[2][4],$A[3][0],[$C[1],#16*7]
  409. stp $A[3][1],$A[3][2],[$C[1],#16*8]
  410. stp $A[3][3],$A[3][4],[$C[1],#16*9]
  411. stp $A[4][0],$A[4][1],[$C[1],#16*10]
  412. stp $A[4][2],$A[4][3],[$C[1],#16*11]
  413. str $A[4][4],[$C[1],#16*12]
  414. mov x0,$C[2] // return value
  415. ldp x19,x20,[x29,#16]
  416. add sp,sp,#64
  417. ldp x21,x22,[x29,#32]
  418. ldp x23,x24,[x29,#48]
  419. ldp x25,x26,[x29,#64]
  420. ldp x27,x28,[x29,#80]
  421. ldp x29,x30,[sp],#128
  422. AARCH64_VALIDATE_LINK_REGISTER
  423. ret
  424. .size SHA3_absorb,.-SHA3_absorb
  425. ___
  426. {
  427. my ($A_flat,$out,$len,$bsz) = map("x$_",(19..22));
  428. $code.=<<___;
  429. .globl SHA3_squeeze
  430. .type SHA3_squeeze,%function
  431. .align 5
  432. SHA3_squeeze:
  433. AARCH64_SIGN_LINK_REGISTER
  434. stp x29,x30,[sp,#-48]!
  435. add x29,sp,#0
  436. stp x19,x20,[sp,#16]
  437. stp x21,x22,[sp,#32]
  438. mov $A_flat,x0 // put aside arguments
  439. mov $out,x1
  440. mov $len,x2
  441. mov $bsz,x3
  442. .Loop_squeeze:
  443. ldr x4,[x0],#8
  444. cmp $len,#8
  445. blo .Lsqueeze_tail
  446. #ifdef __AARCH64EB__
  447. rev x4,x4
  448. #endif
  449. str x4,[$out],#8
  450. subs $len,$len,#8
  451. beq .Lsqueeze_done
  452. subs x3,x3,#8
  453. bhi .Loop_squeeze
  454. mov x0,$A_flat
  455. bl KeccakF1600
  456. mov x0,$A_flat
  457. mov x3,$bsz
  458. b .Loop_squeeze
  459. .align 4
  460. .Lsqueeze_tail:
  461. strb w4,[$out],#1
  462. lsr x4,x4,#8
  463. subs $len,$len,#1
  464. beq .Lsqueeze_done
  465. strb w4,[$out],#1
  466. lsr x4,x4,#8
  467. subs $len,$len,#1
  468. beq .Lsqueeze_done
  469. strb w4,[$out],#1
  470. lsr x4,x4,#8
  471. subs $len,$len,#1
  472. beq .Lsqueeze_done
  473. strb w4,[$out],#1
  474. lsr x4,x4,#8
  475. subs $len,$len,#1
  476. beq .Lsqueeze_done
  477. strb w4,[$out],#1
  478. lsr x4,x4,#8
  479. subs $len,$len,#1
  480. beq .Lsqueeze_done
  481. strb w4,[$out],#1
  482. lsr x4,x4,#8
  483. subs $len,$len,#1
  484. beq .Lsqueeze_done
  485. strb w4,[$out],#1
  486. .Lsqueeze_done:
  487. ldp x19,x20,[sp,#16]
  488. ldp x21,x22,[sp,#32]
  489. ldp x29,x30,[sp],#48
  490. AARCH64_VALIDATE_LINK_REGISTER
  491. ret
  492. .size SHA3_squeeze,.-SHA3_squeeze
  493. ___
  494. } }}}
  495. {{{
  496. my @A = map([ "v".$_.".16b", "v".($_+1).".16b", "v".($_+2).".16b",
  497. "v".($_+3).".16b", "v".($_+4).".16b" ],
  498. (0, 5, 10, 15, 20));
  499. my @C = map("v$_.16b", (25..31));
  500. my @D = @C[4,5,6,2,3];
  501. $code.=<<___;
  502. .type KeccakF1600_ce,%function
  503. .align 5
  504. KeccakF1600_ce:
  505. mov x9,#24
  506. adr x10,iotas
  507. b .Loop_ce
  508. .align 4
  509. .Loop_ce:
  510. ////////////////////////////////////////////////// Theta
  511. eor3 $C[0],$A[4][0],$A[3][0],$A[2][0]
  512. eor3 $C[1],$A[4][1],$A[3][1],$A[2][1]
  513. eor3 $C[2],$A[4][2],$A[3][2],$A[2][2]
  514. eor3 $C[3],$A[4][3],$A[3][3],$A[2][3]
  515. eor3 $C[4],$A[4][4],$A[3][4],$A[2][4]
  516. eor3 $C[0],$C[0], $A[1][0],$A[0][0]
  517. eor3 $C[1],$C[1], $A[1][1],$A[0][1]
  518. eor3 $C[2],$C[2], $A[1][2],$A[0][2]
  519. eor3 $C[3],$C[3], $A[1][3],$A[0][3]
  520. eor3 $C[4],$C[4], $A[1][4],$A[0][4]
  521. rax1 $C[5],$C[0],$C[2] // D[1]
  522. rax1 $C[6],$C[1],$C[3] // D[2]
  523. rax1 $C[2],$C[2],$C[4] // D[3]
  524. rax1 $C[3],$C[3],$C[0] // D[4]
  525. rax1 $C[4],$C[4],$C[1] // D[0]
  526. ////////////////////////////////////////////////// Theta+Rho+Pi
  527. xar $C[0], $A[0][1],$D[1],#64-$rhotates[0][1] // C[0]=A[2][0]
  528. xar $A[0][1],$A[1][1],$D[1],#64-$rhotates[1][1]
  529. xar $A[1][1],$A[1][4],$D[4],#64-$rhotates[1][4]
  530. xar $A[1][4],$A[4][2],$D[2],#64-$rhotates[4][2]
  531. xar $A[4][2],$A[2][4],$D[4],#64-$rhotates[2][4]
  532. xar $A[2][4],$A[4][0],$D[0],#64-$rhotates[4][0]
  533. xar $C[1], $A[0][2],$D[2],#64-$rhotates[0][2] // C[1]=A[4][0]
  534. xar $A[0][2],$A[2][2],$D[2],#64-$rhotates[2][2]
  535. xar $A[2][2],$A[2][3],$D[3],#64-$rhotates[2][3]
  536. xar $A[2][3],$A[3][4],$D[4],#64-$rhotates[3][4]
  537. xar $A[3][4],$A[4][3],$D[3],#64-$rhotates[4][3]
  538. xar $A[4][3],$A[3][0],$D[0],#64-$rhotates[3][0]
  539. xar $A[3][0],$A[0][4],$D[4],#64-$rhotates[0][4]
  540. xar $D[4], $A[4][4],$D[4],#64-$rhotates[4][4] // D[4]=A[0][4]
  541. xar $A[4][4],$A[4][1],$D[1],#64-$rhotates[4][1]
  542. xar $A[1][3],$A[1][3],$D[3],#64-$rhotates[1][3] // A[1][3]=A[4][1]
  543. xar $A[0][4],$A[3][1],$D[1],#64-$rhotates[3][1] // A[0][4]=A[1][3]
  544. xar $A[3][1],$A[1][0],$D[0],#64-$rhotates[1][0]
  545. xar $A[1][0],$A[0][3],$D[3],#64-$rhotates[0][3]
  546. eor $A[0][0],$A[0][0],$D[0]
  547. xar $D[3], $A[3][3],$D[3],#64-$rhotates[3][3] // D[3]=A[0][3]
  548. xar $A[0][3],$A[3][2],$D[2],#64-$rhotates[3][2] // A[0][3]=A[3][3]
  549. xar $D[1], $A[2][1],$D[1],#64-$rhotates[2][1] // D[1]=A[3][2]
  550. xar $D[2], $A[1][2],$D[2],#64-$rhotates[1][2] // D[2]=A[2][1]
  551. xar $D[0], $A[2][0],$D[0],#64-$rhotates[2][0] // D[0]=A[1][2]
  552. ////////////////////////////////////////////////// Chi+Iota
  553. bcax $A[4][0],$C[1], $A[4][2],$A[1][3] // A[1][3]=A[4][1]
  554. bcax $A[4][1],$A[1][3],$A[4][3],$A[4][2] // A[1][3]=A[4][1]
  555. bcax $A[4][2],$A[4][2],$A[4][4],$A[4][3]
  556. bcax $A[4][3],$A[4][3],$C[1], $A[4][4]
  557. bcax $A[4][4],$A[4][4],$A[1][3],$C[1] // A[1][3]=A[4][1]
  558. ld1r {$C[1]},[x10],#8
  559. bcax $A[3][2],$D[1], $A[3][4],$A[0][3] // A[0][3]=A[3][3]
  560. bcax $A[3][3],$A[0][3],$A[3][0],$A[3][4] // A[0][3]=A[3][3]
  561. bcax $A[3][4],$A[3][4],$A[3][1],$A[3][0]
  562. bcax $A[3][0],$A[3][0],$D[1], $A[3][1]
  563. bcax $A[3][1],$A[3][1],$A[0][3],$D[1] // A[0][3]=A[3][3]
  564. bcax $A[2][0],$C[0], $A[2][2],$D[2]
  565. bcax $A[2][1],$D[2], $A[2][3],$A[2][2]
  566. bcax $A[2][2],$A[2][2],$A[2][4],$A[2][3]
  567. bcax $A[2][3],$A[2][3],$C[0], $A[2][4]
  568. bcax $A[2][4],$A[2][4],$D[2], $C[0]
  569. bcax $A[1][2],$D[0], $A[1][4],$A[0][4] // A[0][4]=A[1][3]
  570. bcax $A[1][3],$A[0][4],$A[1][0],$A[1][4] // A[0][4]=A[1][3]
  571. bcax $A[1][4],$A[1][4],$A[1][1],$A[1][0]
  572. bcax $A[1][0],$A[1][0],$D[0], $A[1][1]
  573. bcax $A[1][1],$A[1][1],$A[0][4],$D[0] // A[0][4]=A[1][3]
  574. bcax $A[0][3],$D[3], $A[0][0],$D[4]
  575. bcax $A[0][4],$D[4], $A[0][1],$A[0][0]
  576. bcax $A[0][0],$A[0][0],$A[0][2],$A[0][1]
  577. bcax $A[0][1],$A[0][1],$D[3], $A[0][2]
  578. bcax $A[0][2],$A[0][2],$D[4], $D[3]
  579. eor $A[0][0],$A[0][0],$C[1]
  580. subs x9,x9,#1
  581. bne .Loop_ce
  582. ret
  583. .size KeccakF1600_ce,.-KeccakF1600_ce
  584. .type KeccakF1600_cext,%function
  585. .align 5
  586. KeccakF1600_cext:
  587. AARCH64_SIGN_LINK_REGISTER
  588. stp x29,x30,[sp,#-80]!
  589. add x29,sp,#0
  590. stp d8,d9,[sp,#16] // per ABI requirement
  591. stp d10,d11,[sp,#32]
  592. stp d12,d13,[sp,#48]
  593. stp d14,d15,[sp,#64]
  594. ___
  595. for($i=0; $i<24; $i+=2) { # load A[5][5]
  596. my $j=$i+1;
  597. $code.=<<___;
  598. ldp d$i,d$j,[x0,#8*$i]
  599. ___
  600. }
  601. $code.=<<___;
  602. ldr d24,[x0,#8*$i]
  603. bl KeccakF1600_ce
  604. ldr x30,[sp,#8]
  605. ___
  606. for($i=0; $i<24; $i+=2) { # store A[5][5]
  607. my $j=$i+1;
  608. $code.=<<___;
  609. stp d$i,d$j,[x0,#8*$i]
  610. ___
  611. }
  612. $code.=<<___;
  613. str d24,[x0,#8*$i]
  614. ldp d8,d9,[sp,#16]
  615. ldp d10,d11,[sp,#32]
  616. ldp d12,d13,[sp,#48]
  617. ldp d14,d15,[sp,#64]
  618. ldr x29,[sp],#80
  619. AARCH64_VALIDATE_LINK_REGISTER
  620. ret
  621. .size KeccakF1600_cext,.-KeccakF1600_cext
  622. ___
  623. {
  624. my ($ctx,$inp,$len,$bsz) = map("x$_",(0..3));
  625. $code.=<<___;
  626. .globl SHA3_absorb_cext
  627. .type SHA3_absorb_cext,%function
  628. .align 5
  629. SHA3_absorb_cext:
  630. AARCH64_SIGN_LINK_REGISTER
  631. stp x29,x30,[sp,#-80]!
  632. add x29,sp,#0
  633. stp d8,d9,[sp,#16] // per ABI requirement
  634. stp d10,d11,[sp,#32]
  635. stp d12,d13,[sp,#48]
  636. stp d14,d15,[sp,#64]
  637. ___
  638. for($i=0; $i<24; $i+=2) { # load A[5][5]
  639. my $j=$i+1;
  640. $code.=<<___;
  641. ldp d$i,d$j,[x0,#8*$i]
  642. ___
  643. }
  644. $code.=<<___;
  645. ldr d24,[x0,#8*$i]
  646. b .Loop_absorb_ce
  647. .align 4
  648. .Loop_absorb_ce:
  649. subs $len,$len,$bsz // len - bsz
  650. blo .Labsorbed_ce
  651. ___
  652. for (my $i=0; $i<24; $i+=2) {
  653. my $j = $i+1;
  654. $code.=<<___;
  655. ldr d31,[$inp],#8 // *inp++
  656. #ifdef __AARCH64EB__
  657. rev64 v31.16b,v31.16b
  658. #endif
  659. eor $A[$i/5][$i%5],$A[$i/5][$i%5],v31.16b
  660. cmp $bsz,#8*($i+2)
  661. blo .Lprocess_block_ce
  662. ldr d31,[$inp],#8 // *inp++
  663. #ifdef __AARCH64EB__
  664. rev64 v31.16b,v31.16b
  665. #endif
  666. eor $A[$j/5][$j%5],$A[$j/5][$j%5],v31.16b
  667. beq .Lprocess_block_ce
  668. ___
  669. }
  670. $code.=<<___;
  671. ldr d31,[$inp],#8 // *inp++
  672. #ifdef __AARCH64EB__
  673. rev64 v31.16b,v31.16b
  674. #endif
  675. eor $A[4][4],$A[4][4],v31.16b
  676. .Lprocess_block_ce:
  677. bl KeccakF1600_ce
  678. b .Loop_absorb_ce
  679. .align 4
  680. .Labsorbed_ce:
  681. ___
  682. for($i=0; $i<24; $i+=2) { # store A[5][5]
  683. my $j=$i+1;
  684. $code.=<<___;
  685. stp d$i,d$j,[x0,#8*$i]
  686. ___
  687. }
  688. $code.=<<___;
  689. str d24,[x0,#8*$i]
  690. add x0,$len,$bsz // return value
  691. ldp d8,d9,[sp,#16]
  692. ldp d10,d11,[sp,#32]
  693. ldp d12,d13,[sp,#48]
  694. ldp d14,d15,[sp,#64]
  695. ldp x29,x30,[sp],#80
  696. AARCH64_VALIDATE_LINK_REGISTER
  697. ret
  698. .size SHA3_absorb_cext,.-SHA3_absorb_cext
  699. ___
  700. }
  701. {
  702. my ($ctx,$out,$len,$bsz) = map("x$_",(0..3));
  703. $code.=<<___;
  704. .globl SHA3_squeeze_cext
  705. .type SHA3_squeeze_cext,%function
  706. .align 5
  707. SHA3_squeeze_cext:
  708. AARCH64_SIGN_LINK_REGISTER
  709. stp x29,x30,[sp,#-16]!
  710. add x29,sp,#0
  711. mov x9,$ctx
  712. mov x10,$bsz
  713. .Loop_squeeze_ce:
  714. ldr x4,[x9],#8
  715. cmp $len,#8
  716. blo .Lsqueeze_tail_ce
  717. #ifdef __AARCH64EB__
  718. rev x4,x4
  719. #endif
  720. str x4,[$out],#8
  721. beq .Lsqueeze_done_ce
  722. sub $len,$len,#8
  723. subs x10,x10,#8
  724. bhi .Loop_squeeze_ce
  725. bl KeccakF1600_cext
  726. ldr x30,[sp,#8]
  727. mov x9,$ctx
  728. mov x10,$bsz
  729. b .Loop_squeeze_ce
  730. .align 4
  731. .Lsqueeze_tail_ce:
  732. strb w4,[$out],#1
  733. lsr x4,x4,#8
  734. subs $len,$len,#1
  735. beq .Lsqueeze_done_ce
  736. strb w4,[$out],#1
  737. lsr x4,x4,#8
  738. subs $len,$len,#1
  739. beq .Lsqueeze_done_ce
  740. strb w4,[$out],#1
  741. lsr x4,x4,#8
  742. subs $len,$len,#1
  743. beq .Lsqueeze_done_ce
  744. strb w4,[$out],#1
  745. lsr x4,x4,#8
  746. subs $len,$len,#1
  747. beq .Lsqueeze_done_ce
  748. strb w4,[$out],#1
  749. lsr x4,x4,#8
  750. subs $len,$len,#1
  751. beq .Lsqueeze_done_ce
  752. strb w4,[$out],#1
  753. lsr x4,x4,#8
  754. subs $len,$len,#1
  755. beq .Lsqueeze_done_ce
  756. strb w4,[$out],#1
  757. .Lsqueeze_done_ce:
  758. ldr x29,[sp],#16
  759. AARCH64_VALIDATE_LINK_REGISTER
  760. ret
  761. .size SHA3_squeeze_cext,.-SHA3_squeeze_cext
  762. ___
  763. } }}}
  764. $code.=<<___;
  765. .asciz "Keccak-1600 absorb and squeeze for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  766. ___
  767. { my %opcode = (
  768. "rax1" => 0xce608c00, "eor3" => 0xce000000,
  769. "bcax" => 0xce200000, "xar" => 0xce800000 );
  770. sub unsha3 {
  771. my ($mnemonic,$arg)=@_;
  772. $arg =~ m/[qv]([0-9]+)[^,]*,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv]([0-9]+)[^,]*(?:,\s*[qv#]([0-9\-]+))?)?/
  773. &&
  774. sprintf ".inst\t0x%08x\t//%s %s",
  775. $opcode{$mnemonic}|$1|($2<<5)|($3<<16)|(eval($4)<<10),
  776. $mnemonic,$arg;
  777. }
  778. }
  779. foreach(split("\n",$code)) {
  780. s/\`([^\`]*)\`/eval($1)/ge;
  781. m/\bld1r\b/ and s/\.16b/.2d/g or
  782. s/\b(eor3|rax1|xar|bcax)\s+(v.*)/unsha3($1,$2)/ge;
  783. print $_,"\n";
  784. }
  785. close STDOUT or die "error closing STDOUT: $!";