2
0

poly1305-armv8.pl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. #! /usr/bin/env perl
  2. # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. # This module implements Poly1305 hash for ARMv8.
  17. #
  18. # June 2015
  19. #
  20. # Numbers are cycles per processed byte with poly1305_blocks alone.
  21. #
  22. # IALU/gcc-4.9 NEON
  23. #
  24. # Apple A7 1.86/+5% 0.72
  25. # Cortex-A53 2.69/+58% 1.47
  26. # Cortex-A57 2.70/+7% 1.14
  27. # Denver 1.64/+50% 1.18(*)
  28. # X-Gene 2.13/+68% 2.27
  29. # Mongoose 1.77/+75% 1.12
  30. # Kryo 2.70/+55% 1.13
  31. #
  32. # (*) estimate based on resources availability is less than 1.0,
  33. # i.e. measured result is worse than expected, presumably binary
  34. # translator is not almighty;
  35. $flavour=shift;
  36. $output=shift;
  37. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  38. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  39. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  40. die "can't locate arm-xlate.pl";
  41. open OUT,"| \"$^X\" $xlate $flavour $output";
  42. *STDOUT=*OUT;
  43. my ($ctx,$inp,$len,$padbit) = map("x$_",(0..3));
  44. my ($mac,$nonce)=($inp,$len);
  45. my ($h0,$h1,$h2,$r0,$r1,$s1,$t0,$t1,$d0,$d1,$d2) = map("x$_",(4..14));
  46. $code.=<<___;
  47. #include "arm_arch.h"
  48. .text
  49. // forward "declarations" are required for Apple
  50. .extern OPENSSL_armcap_P
  51. .hidden OPENSSL_armcap_P
  52. .globl poly1305_init
  53. .hidden poly1305_init
  54. .globl poly1305_blocks
  55. .hidden poly1305_blocks
  56. .globl poly1305_emit
  57. .hidden poly1305_emit
  58. .type poly1305_init,%function
  59. .align 5
  60. poly1305_init:
  61. cmp $inp,xzr
  62. stp xzr,xzr,[$ctx] // zero hash value
  63. stp xzr,xzr,[$ctx,#16] // [along with is_base2_26]
  64. csel x0,xzr,x0,eq
  65. b.eq .Lno_key
  66. #ifdef __ILP32__
  67. ldrsw $t1,.LOPENSSL_armcap_P
  68. #else
  69. ldr $t1,.LOPENSSL_armcap_P
  70. #endif
  71. adr $t0,.LOPENSSL_armcap_P
  72. ldp $r0,$r1,[$inp] // load key
  73. mov $s1,#0xfffffffc0fffffff
  74. movk $s1,#0x0fff,lsl#48
  75. ldr w17,[$t0,$t1]
  76. #ifdef __ARMEB__
  77. rev $r0,$r0 // flip bytes
  78. rev $r1,$r1
  79. #endif
  80. and $r0,$r0,$s1 // &=0ffffffc0fffffff
  81. and $s1,$s1,#-4
  82. and $r1,$r1,$s1 // &=0ffffffc0ffffffc
  83. stp $r0,$r1,[$ctx,#32] // save key value
  84. tst w17,#ARMV7_NEON
  85. adr $d0,poly1305_blocks
  86. adr $r0,poly1305_blocks_neon
  87. adr $d1,poly1305_emit
  88. adr $r1,poly1305_emit_neon
  89. csel $d0,$d0,$r0,eq
  90. csel $d1,$d1,$r1,eq
  91. #ifdef __ILP32__
  92. stp w12,w13,[$len]
  93. #else
  94. stp $d0,$d1,[$len]
  95. #endif
  96. mov x0,#1
  97. .Lno_key:
  98. ret
  99. .size poly1305_init,.-poly1305_init
  100. .type poly1305_blocks,%function
  101. .align 5
  102. poly1305_blocks:
  103. ands $len,$len,#-16
  104. b.eq .Lno_data
  105. ldp $h0,$h1,[$ctx] // load hash value
  106. ldp $r0,$r1,[$ctx,#32] // load key value
  107. ldr $h2,[$ctx,#16]
  108. add $s1,$r1,$r1,lsr#2 // s1 = r1 + (r1 >> 2)
  109. b .Loop
  110. .align 5
  111. .Loop:
  112. ldp $t0,$t1,[$inp],#16 // load input
  113. sub $len,$len,#16
  114. #ifdef __ARMEB__
  115. rev $t0,$t0
  116. rev $t1,$t1
  117. #endif
  118. adds $h0,$h0,$t0 // accumulate input
  119. adcs $h1,$h1,$t1
  120. mul $d0,$h0,$r0 // h0*r0
  121. adc $h2,$h2,$padbit
  122. umulh $d1,$h0,$r0
  123. mul $t0,$h1,$s1 // h1*5*r1
  124. umulh $t1,$h1,$s1
  125. adds $d0,$d0,$t0
  126. mul $t0,$h0,$r1 // h0*r1
  127. adc $d1,$d1,$t1
  128. umulh $d2,$h0,$r1
  129. adds $d1,$d1,$t0
  130. mul $t0,$h1,$r0 // h1*r0
  131. adc $d2,$d2,xzr
  132. umulh $t1,$h1,$r0
  133. adds $d1,$d1,$t0
  134. mul $t0,$h2,$s1 // h2*5*r1
  135. adc $d2,$d2,$t1
  136. mul $t1,$h2,$r0 // h2*r0
  137. adds $d1,$d1,$t0
  138. adc $d2,$d2,$t1
  139. and $t0,$d2,#-4 // final reduction
  140. and $h2,$d2,#3
  141. add $t0,$t0,$d2,lsr#2
  142. adds $h0,$d0,$t0
  143. adcs $h1,$d1,xzr
  144. adc $h2,$h2,xzr
  145. cbnz $len,.Loop
  146. stp $h0,$h1,[$ctx] // store hash value
  147. str $h2,[$ctx,#16]
  148. .Lno_data:
  149. ret
  150. .size poly1305_blocks,.-poly1305_blocks
  151. .type poly1305_emit,%function
  152. .align 5
  153. poly1305_emit:
  154. ldp $h0,$h1,[$ctx] // load hash base 2^64
  155. ldr $h2,[$ctx,#16]
  156. ldp $t0,$t1,[$nonce] // load nonce
  157. adds $d0,$h0,#5 // compare to modulus
  158. adcs $d1,$h1,xzr
  159. adc $d2,$h2,xzr
  160. tst $d2,#-4 // see if it's carried/borrowed
  161. csel $h0,$h0,$d0,eq
  162. csel $h1,$h1,$d1,eq
  163. #ifdef __ARMEB__
  164. ror $t0,$t0,#32 // flip nonce words
  165. ror $t1,$t1,#32
  166. #endif
  167. adds $h0,$h0,$t0 // accumulate nonce
  168. adc $h1,$h1,$t1
  169. #ifdef __ARMEB__
  170. rev $h0,$h0 // flip output bytes
  171. rev $h1,$h1
  172. #endif
  173. stp $h0,$h1,[$mac] // write result
  174. ret
  175. .size poly1305_emit,.-poly1305_emit
  176. ___
  177. my ($R0,$R1,$S1,$R2,$S2,$R3,$S3,$R4,$S4) = map("v$_.4s",(0..8));
  178. my ($IN01_0,$IN01_1,$IN01_2,$IN01_3,$IN01_4) = map("v$_.2s",(9..13));
  179. my ($IN23_0,$IN23_1,$IN23_2,$IN23_3,$IN23_4) = map("v$_.2s",(14..18));
  180. my ($ACC0,$ACC1,$ACC2,$ACC3,$ACC4) = map("v$_.2d",(19..23));
  181. my ($H0,$H1,$H2,$H3,$H4) = map("v$_.2s",(24..28));
  182. my ($T0,$T1,$MASK) = map("v$_",(29..31));
  183. my ($in2,$zeros)=("x16","x17");
  184. my $is_base2_26 = $zeros; # borrow
  185. $code.=<<___;
  186. .type poly1305_mult,%function
  187. .align 5
  188. poly1305_mult:
  189. mul $d0,$h0,$r0 // h0*r0
  190. umulh $d1,$h0,$r0
  191. mul $t0,$h1,$s1 // h1*5*r1
  192. umulh $t1,$h1,$s1
  193. adds $d0,$d0,$t0
  194. mul $t0,$h0,$r1 // h0*r1
  195. adc $d1,$d1,$t1
  196. umulh $d2,$h0,$r1
  197. adds $d1,$d1,$t0
  198. mul $t0,$h1,$r0 // h1*r0
  199. adc $d2,$d2,xzr
  200. umulh $t1,$h1,$r0
  201. adds $d1,$d1,$t0
  202. mul $t0,$h2,$s1 // h2*5*r1
  203. adc $d2,$d2,$t1
  204. mul $t1,$h2,$r0 // h2*r0
  205. adds $d1,$d1,$t0
  206. adc $d2,$d2,$t1
  207. and $t0,$d2,#-4 // final reduction
  208. and $h2,$d2,#3
  209. add $t0,$t0,$d2,lsr#2
  210. adds $h0,$d0,$t0
  211. adcs $h1,$d1,xzr
  212. adc $h2,$h2,xzr
  213. ret
  214. .size poly1305_mult,.-poly1305_mult
  215. .type poly1305_splat,%function
  216. .align 5
  217. poly1305_splat:
  218. and x12,$h0,#0x03ffffff // base 2^64 -> base 2^26
  219. ubfx x13,$h0,#26,#26
  220. extr x14,$h1,$h0,#52
  221. and x14,x14,#0x03ffffff
  222. ubfx x15,$h1,#14,#26
  223. extr x16,$h2,$h1,#40
  224. str w12,[$ctx,#16*0] // r0
  225. add w12,w13,w13,lsl#2 // r1*5
  226. str w13,[$ctx,#16*1] // r1
  227. add w13,w14,w14,lsl#2 // r2*5
  228. str w12,[$ctx,#16*2] // s1
  229. str w14,[$ctx,#16*3] // r2
  230. add w14,w15,w15,lsl#2 // r3*5
  231. str w13,[$ctx,#16*4] // s2
  232. str w15,[$ctx,#16*5] // r3
  233. add w15,w16,w16,lsl#2 // r4*5
  234. str w14,[$ctx,#16*6] // s3
  235. str w16,[$ctx,#16*7] // r4
  236. str w15,[$ctx,#16*8] // s4
  237. ret
  238. .size poly1305_splat,.-poly1305_splat
  239. .type poly1305_blocks_neon,%function
  240. .align 5
  241. poly1305_blocks_neon:
  242. ldr $is_base2_26,[$ctx,#24]
  243. cmp $len,#128
  244. b.hs .Lblocks_neon
  245. cbz $is_base2_26,poly1305_blocks
  246. .Lblocks_neon:
  247. .inst 0xd503233f // paciasp
  248. stp x29,x30,[sp,#-80]!
  249. add x29,sp,#0
  250. ands $len,$len,#-16
  251. b.eq .Lno_data_neon
  252. cbz $is_base2_26,.Lbase2_64_neon
  253. ldp w10,w11,[$ctx] // load hash value base 2^26
  254. ldp w12,w13,[$ctx,#8]
  255. ldr w14,[$ctx,#16]
  256. tst $len,#31
  257. b.eq .Leven_neon
  258. ldp $r0,$r1,[$ctx,#32] // load key value
  259. add $h0,x10,x11,lsl#26 // base 2^26 -> base 2^64
  260. lsr $h1,x12,#12
  261. adds $h0,$h0,x12,lsl#52
  262. add $h1,$h1,x13,lsl#14
  263. adc $h1,$h1,xzr
  264. lsr $h2,x14,#24
  265. adds $h1,$h1,x14,lsl#40
  266. adc $d2,$h2,xzr // can be partially reduced...
  267. ldp $d0,$d1,[$inp],#16 // load input
  268. sub $len,$len,#16
  269. add $s1,$r1,$r1,lsr#2 // s1 = r1 + (r1 >> 2)
  270. and $t0,$d2,#-4 // ... so reduce
  271. and $h2,$d2,#3
  272. add $t0,$t0,$d2,lsr#2
  273. adds $h0,$h0,$t0
  274. adcs $h1,$h1,xzr
  275. adc $h2,$h2,xzr
  276. #ifdef __ARMEB__
  277. rev $d0,$d0
  278. rev $d1,$d1
  279. #endif
  280. adds $h0,$h0,$d0 // accumulate input
  281. adcs $h1,$h1,$d1
  282. adc $h2,$h2,$padbit
  283. bl poly1305_mult
  284. ldr x30,[sp,#8]
  285. cbz $padbit,.Lstore_base2_64_neon
  286. and x10,$h0,#0x03ffffff // base 2^64 -> base 2^26
  287. ubfx x11,$h0,#26,#26
  288. extr x12,$h1,$h0,#52
  289. and x12,x12,#0x03ffffff
  290. ubfx x13,$h1,#14,#26
  291. extr x14,$h2,$h1,#40
  292. cbnz $len,.Leven_neon
  293. stp w10,w11,[$ctx] // store hash value base 2^26
  294. stp w12,w13,[$ctx,#8]
  295. str w14,[$ctx,#16]
  296. b .Lno_data_neon
  297. .align 4
  298. .Lstore_base2_64_neon:
  299. stp $h0,$h1,[$ctx] // store hash value base 2^64
  300. stp $h2,xzr,[$ctx,#16] // note that is_base2_26 is zeroed
  301. b .Lno_data_neon
  302. .align 4
  303. .Lbase2_64_neon:
  304. ldp $r0,$r1,[$ctx,#32] // load key value
  305. ldp $h0,$h1,[$ctx] // load hash value base 2^64
  306. ldr $h2,[$ctx,#16]
  307. tst $len,#31
  308. b.eq .Linit_neon
  309. ldp $d0,$d1,[$inp],#16 // load input
  310. sub $len,$len,#16
  311. add $s1,$r1,$r1,lsr#2 // s1 = r1 + (r1 >> 2)
  312. #ifdef __ARMEB__
  313. rev $d0,$d0
  314. rev $d1,$d1
  315. #endif
  316. adds $h0,$h0,$d0 // accumulate input
  317. adcs $h1,$h1,$d1
  318. adc $h2,$h2,$padbit
  319. bl poly1305_mult
  320. .Linit_neon:
  321. and x10,$h0,#0x03ffffff // base 2^64 -> base 2^26
  322. ubfx x11,$h0,#26,#26
  323. extr x12,$h1,$h0,#52
  324. and x12,x12,#0x03ffffff
  325. ubfx x13,$h1,#14,#26
  326. extr x14,$h2,$h1,#40
  327. stp d8,d9,[sp,#16] // meet ABI requirements
  328. stp d10,d11,[sp,#32]
  329. stp d12,d13,[sp,#48]
  330. stp d14,d15,[sp,#64]
  331. fmov ${H0},x10
  332. fmov ${H1},x11
  333. fmov ${H2},x12
  334. fmov ${H3},x13
  335. fmov ${H4},x14
  336. ////////////////////////////////// initialize r^n table
  337. mov $h0,$r0 // r^1
  338. add $s1,$r1,$r1,lsr#2 // s1 = r1 + (r1 >> 2)
  339. mov $h1,$r1
  340. mov $h2,xzr
  341. add $ctx,$ctx,#48+12
  342. bl poly1305_splat
  343. bl poly1305_mult // r^2
  344. sub $ctx,$ctx,#4
  345. bl poly1305_splat
  346. bl poly1305_mult // r^3
  347. sub $ctx,$ctx,#4
  348. bl poly1305_splat
  349. bl poly1305_mult // r^4
  350. sub $ctx,$ctx,#4
  351. bl poly1305_splat
  352. ldr x30,[sp,#8]
  353. add $in2,$inp,#32
  354. adr $zeros,.Lzeros
  355. subs $len,$len,#64
  356. csel $in2,$zeros,$in2,lo
  357. mov x4,#1
  358. str x4,[$ctx,#-24] // set is_base2_26
  359. sub $ctx,$ctx,#48 // restore original $ctx
  360. b .Ldo_neon
  361. .align 4
  362. .Leven_neon:
  363. add $in2,$inp,#32
  364. adr $zeros,.Lzeros
  365. subs $len,$len,#64
  366. csel $in2,$zeros,$in2,lo
  367. stp d8,d9,[sp,#16] // meet ABI requirements
  368. stp d10,d11,[sp,#32]
  369. stp d12,d13,[sp,#48]
  370. stp d14,d15,[sp,#64]
  371. fmov ${H0},x10
  372. fmov ${H1},x11
  373. fmov ${H2},x12
  374. fmov ${H3},x13
  375. fmov ${H4},x14
  376. .Ldo_neon:
  377. ldp x8,x12,[$in2],#16 // inp[2:3] (or zero)
  378. ldp x9,x13,[$in2],#48
  379. lsl $padbit,$padbit,#24
  380. add x15,$ctx,#48
  381. #ifdef __ARMEB__
  382. rev x8,x8
  383. rev x12,x12
  384. rev x9,x9
  385. rev x13,x13
  386. #endif
  387. and x4,x8,#0x03ffffff // base 2^64 -> base 2^26
  388. and x5,x9,#0x03ffffff
  389. ubfx x6,x8,#26,#26
  390. ubfx x7,x9,#26,#26
  391. add x4,x4,x5,lsl#32 // bfi x4,x5,#32,#32
  392. extr x8,x12,x8,#52
  393. extr x9,x13,x9,#52
  394. add x6,x6,x7,lsl#32 // bfi x6,x7,#32,#32
  395. fmov $IN23_0,x4
  396. and x8,x8,#0x03ffffff
  397. and x9,x9,#0x03ffffff
  398. ubfx x10,x12,#14,#26
  399. ubfx x11,x13,#14,#26
  400. add x12,$padbit,x12,lsr#40
  401. add x13,$padbit,x13,lsr#40
  402. add x8,x8,x9,lsl#32 // bfi x8,x9,#32,#32
  403. fmov $IN23_1,x6
  404. add x10,x10,x11,lsl#32 // bfi x10,x11,#32,#32
  405. add x12,x12,x13,lsl#32 // bfi x12,x13,#32,#32
  406. fmov $IN23_2,x8
  407. fmov $IN23_3,x10
  408. fmov $IN23_4,x12
  409. ldp x8,x12,[$inp],#16 // inp[0:1]
  410. ldp x9,x13,[$inp],#48
  411. ld1 {$R0,$R1,$S1,$R2},[x15],#64
  412. ld1 {$S2,$R3,$S3,$R4},[x15],#64
  413. ld1 {$S4},[x15]
  414. #ifdef __ARMEB__
  415. rev x8,x8
  416. rev x12,x12
  417. rev x9,x9
  418. rev x13,x13
  419. #endif
  420. and x4,x8,#0x03ffffff // base 2^64 -> base 2^26
  421. and x5,x9,#0x03ffffff
  422. ubfx x6,x8,#26,#26
  423. ubfx x7,x9,#26,#26
  424. add x4,x4,x5,lsl#32 // bfi x4,x5,#32,#32
  425. extr x8,x12,x8,#52
  426. extr x9,x13,x9,#52
  427. add x6,x6,x7,lsl#32 // bfi x6,x7,#32,#32
  428. fmov $IN01_0,x4
  429. and x8,x8,#0x03ffffff
  430. and x9,x9,#0x03ffffff
  431. ubfx x10,x12,#14,#26
  432. ubfx x11,x13,#14,#26
  433. add x12,$padbit,x12,lsr#40
  434. add x13,$padbit,x13,lsr#40
  435. add x8,x8,x9,lsl#32 // bfi x8,x9,#32,#32
  436. fmov $IN01_1,x6
  437. add x10,x10,x11,lsl#32 // bfi x10,x11,#32,#32
  438. add x12,x12,x13,lsl#32 // bfi x12,x13,#32,#32
  439. movi $MASK.2d,#-1
  440. fmov $IN01_2,x8
  441. fmov $IN01_3,x10
  442. fmov $IN01_4,x12
  443. ushr $MASK.2d,$MASK.2d,#38
  444. b.ls .Lskip_loop
  445. .align 4
  446. .Loop_neon:
  447. ////////////////////////////////////////////////////////////////
  448. // ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2
  449. // ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^3+inp[7]*r
  450. // \___________________/
  451. // ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2+inp[8])*r^2
  452. // ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^4+inp[7]*r^2+inp[9])*r
  453. // \___________________/ \____________________/
  454. //
  455. // Note that we start with inp[2:3]*r^2. This is because it
  456. // doesn't depend on reduction in previous iteration.
  457. ////////////////////////////////////////////////////////////////
  458. // d4 = h0*r4 + h1*r3 + h2*r2 + h3*r1 + h4*r0
  459. // d3 = h0*r3 + h1*r2 + h2*r1 + h3*r0 + h4*5*r4
  460. // d2 = h0*r2 + h1*r1 + h2*r0 + h3*5*r4 + h4*5*r3
  461. // d1 = h0*r1 + h1*r0 + h2*5*r4 + h3*5*r3 + h4*5*r2
  462. // d0 = h0*r0 + h1*5*r4 + h2*5*r3 + h3*5*r2 + h4*5*r1
  463. subs $len,$len,#64
  464. umull $ACC4,$IN23_0,${R4}[2]
  465. csel $in2,$zeros,$in2,lo
  466. umull $ACC3,$IN23_0,${R3}[2]
  467. umull $ACC2,$IN23_0,${R2}[2]
  468. ldp x8,x12,[$in2],#16 // inp[2:3] (or zero)
  469. umull $ACC1,$IN23_0,${R1}[2]
  470. ldp x9,x13,[$in2],#48
  471. umull $ACC0,$IN23_0,${R0}[2]
  472. #ifdef __ARMEB__
  473. rev x8,x8
  474. rev x12,x12
  475. rev x9,x9
  476. rev x13,x13
  477. #endif
  478. umlal $ACC4,$IN23_1,${R3}[2]
  479. and x4,x8,#0x03ffffff // base 2^64 -> base 2^26
  480. umlal $ACC3,$IN23_1,${R2}[2]
  481. and x5,x9,#0x03ffffff
  482. umlal $ACC2,$IN23_1,${R1}[2]
  483. ubfx x6,x8,#26,#26
  484. umlal $ACC1,$IN23_1,${R0}[2]
  485. ubfx x7,x9,#26,#26
  486. umlal $ACC0,$IN23_1,${S4}[2]
  487. add x4,x4,x5,lsl#32 // bfi x4,x5,#32,#32
  488. umlal $ACC4,$IN23_2,${R2}[2]
  489. extr x8,x12,x8,#52
  490. umlal $ACC3,$IN23_2,${R1}[2]
  491. extr x9,x13,x9,#52
  492. umlal $ACC2,$IN23_2,${R0}[2]
  493. add x6,x6,x7,lsl#32 // bfi x6,x7,#32,#32
  494. umlal $ACC1,$IN23_2,${S4}[2]
  495. fmov $IN23_0,x4
  496. umlal $ACC0,$IN23_2,${S3}[2]
  497. and x8,x8,#0x03ffffff
  498. umlal $ACC4,$IN23_3,${R1}[2]
  499. and x9,x9,#0x03ffffff
  500. umlal $ACC3,$IN23_3,${R0}[2]
  501. ubfx x10,x12,#14,#26
  502. umlal $ACC2,$IN23_3,${S4}[2]
  503. ubfx x11,x13,#14,#26
  504. umlal $ACC1,$IN23_3,${S3}[2]
  505. add x8,x8,x9,lsl#32 // bfi x8,x9,#32,#32
  506. umlal $ACC0,$IN23_3,${S2}[2]
  507. fmov $IN23_1,x6
  508. add $IN01_2,$IN01_2,$H2
  509. add x12,$padbit,x12,lsr#40
  510. umlal $ACC4,$IN23_4,${R0}[2]
  511. add x13,$padbit,x13,lsr#40
  512. umlal $ACC3,$IN23_4,${S4}[2]
  513. add x10,x10,x11,lsl#32 // bfi x10,x11,#32,#32
  514. umlal $ACC2,$IN23_4,${S3}[2]
  515. add x12,x12,x13,lsl#32 // bfi x12,x13,#32,#32
  516. umlal $ACC1,$IN23_4,${S2}[2]
  517. fmov $IN23_2,x8
  518. umlal $ACC0,$IN23_4,${S1}[2]
  519. fmov $IN23_3,x10
  520. ////////////////////////////////////////////////////////////////
  521. // (hash+inp[0:1])*r^4 and accumulate
  522. add $IN01_0,$IN01_0,$H0
  523. fmov $IN23_4,x12
  524. umlal $ACC3,$IN01_2,${R1}[0]
  525. ldp x8,x12,[$inp],#16 // inp[0:1]
  526. umlal $ACC0,$IN01_2,${S3}[0]
  527. ldp x9,x13,[$inp],#48
  528. umlal $ACC4,$IN01_2,${R2}[0]
  529. umlal $ACC1,$IN01_2,${S4}[0]
  530. umlal $ACC2,$IN01_2,${R0}[0]
  531. #ifdef __ARMEB__
  532. rev x8,x8
  533. rev x12,x12
  534. rev x9,x9
  535. rev x13,x13
  536. #endif
  537. add $IN01_1,$IN01_1,$H1
  538. umlal $ACC3,$IN01_0,${R3}[0]
  539. umlal $ACC4,$IN01_0,${R4}[0]
  540. and x4,x8,#0x03ffffff // base 2^64 -> base 2^26
  541. umlal $ACC2,$IN01_0,${R2}[0]
  542. and x5,x9,#0x03ffffff
  543. umlal $ACC0,$IN01_0,${R0}[0]
  544. ubfx x6,x8,#26,#26
  545. umlal $ACC1,$IN01_0,${R1}[0]
  546. ubfx x7,x9,#26,#26
  547. add $IN01_3,$IN01_3,$H3
  548. add x4,x4,x5,lsl#32 // bfi x4,x5,#32,#32
  549. umlal $ACC3,$IN01_1,${R2}[0]
  550. extr x8,x12,x8,#52
  551. umlal $ACC4,$IN01_1,${R3}[0]
  552. extr x9,x13,x9,#52
  553. umlal $ACC0,$IN01_1,${S4}[0]
  554. add x6,x6,x7,lsl#32 // bfi x6,x7,#32,#32
  555. umlal $ACC2,$IN01_1,${R1}[0]
  556. fmov $IN01_0,x4
  557. umlal $ACC1,$IN01_1,${R0}[0]
  558. and x8,x8,#0x03ffffff
  559. add $IN01_4,$IN01_4,$H4
  560. and x9,x9,#0x03ffffff
  561. umlal $ACC3,$IN01_3,${R0}[0]
  562. ubfx x10,x12,#14,#26
  563. umlal $ACC0,$IN01_3,${S2}[0]
  564. ubfx x11,x13,#14,#26
  565. umlal $ACC4,$IN01_3,${R1}[0]
  566. add x8,x8,x9,lsl#32 // bfi x8,x9,#32,#32
  567. umlal $ACC1,$IN01_3,${S3}[0]
  568. fmov $IN01_1,x6
  569. umlal $ACC2,$IN01_3,${S4}[0]
  570. add x12,$padbit,x12,lsr#40
  571. umlal $ACC3,$IN01_4,${S4}[0]
  572. add x13,$padbit,x13,lsr#40
  573. umlal $ACC0,$IN01_4,${S1}[0]
  574. add x10,x10,x11,lsl#32 // bfi x10,x11,#32,#32
  575. umlal $ACC4,$IN01_4,${R0}[0]
  576. add x12,x12,x13,lsl#32 // bfi x12,x13,#32,#32
  577. umlal $ACC1,$IN01_4,${S2}[0]
  578. fmov $IN01_2,x8
  579. umlal $ACC2,$IN01_4,${S3}[0]
  580. fmov $IN01_3,x10
  581. fmov $IN01_4,x12
  582. /////////////////////////////////////////////////////////////////
  583. // lazy reduction as discussed in "NEON crypto" by D.J. Bernstein
  584. // and P. Schwabe
  585. //
  586. // [see discussion in poly1305-armv4 module]
  587. ushr $T0.2d,$ACC3,#26
  588. xtn $H3,$ACC3
  589. ushr $T1.2d,$ACC0,#26
  590. and $ACC0,$ACC0,$MASK.2d
  591. add $ACC4,$ACC4,$T0.2d // h3 -> h4
  592. bic $H3,#0xfc,lsl#24 // &=0x03ffffff
  593. add $ACC1,$ACC1,$T1.2d // h0 -> h1
  594. ushr $T0.2d,$ACC4,#26
  595. xtn $H4,$ACC4
  596. ushr $T1.2d,$ACC1,#26
  597. xtn $H1,$ACC1
  598. bic $H4,#0xfc,lsl#24
  599. add $ACC2,$ACC2,$T1.2d // h1 -> h2
  600. add $ACC0,$ACC0,$T0.2d
  601. shl $T0.2d,$T0.2d,#2
  602. shrn $T1.2s,$ACC2,#26
  603. xtn $H2,$ACC2
  604. add $ACC0,$ACC0,$T0.2d // h4 -> h0
  605. bic $H1,#0xfc,lsl#24
  606. add $H3,$H3,$T1.2s // h2 -> h3
  607. bic $H2,#0xfc,lsl#24
  608. shrn $T0.2s,$ACC0,#26
  609. xtn $H0,$ACC0
  610. ushr $T1.2s,$H3,#26
  611. bic $H3,#0xfc,lsl#24
  612. bic $H0,#0xfc,lsl#24
  613. add $H1,$H1,$T0.2s // h0 -> h1
  614. add $H4,$H4,$T1.2s // h3 -> h4
  615. b.hi .Loop_neon
  616. .Lskip_loop:
  617. dup $IN23_2,${IN23_2}[0]
  618. add $IN01_2,$IN01_2,$H2
  619. ////////////////////////////////////////////////////////////////
  620. // multiply (inp[0:1]+hash) or inp[2:3] by r^2:r^1
  621. adds $len,$len,#32
  622. b.ne .Long_tail
  623. dup $IN23_2,${IN01_2}[0]
  624. add $IN23_0,$IN01_0,$H0
  625. add $IN23_3,$IN01_3,$H3
  626. add $IN23_1,$IN01_1,$H1
  627. add $IN23_4,$IN01_4,$H4
  628. .Long_tail:
  629. dup $IN23_0,${IN23_0}[0]
  630. umull2 $ACC0,$IN23_2,${S3}
  631. umull2 $ACC3,$IN23_2,${R1}
  632. umull2 $ACC4,$IN23_2,${R2}
  633. umull2 $ACC2,$IN23_2,${R0}
  634. umull2 $ACC1,$IN23_2,${S4}
  635. dup $IN23_1,${IN23_1}[0]
  636. umlal2 $ACC0,$IN23_0,${R0}
  637. umlal2 $ACC2,$IN23_0,${R2}
  638. umlal2 $ACC3,$IN23_0,${R3}
  639. umlal2 $ACC4,$IN23_0,${R4}
  640. umlal2 $ACC1,$IN23_0,${R1}
  641. dup $IN23_3,${IN23_3}[0]
  642. umlal2 $ACC0,$IN23_1,${S4}
  643. umlal2 $ACC3,$IN23_1,${R2}
  644. umlal2 $ACC2,$IN23_1,${R1}
  645. umlal2 $ACC4,$IN23_1,${R3}
  646. umlal2 $ACC1,$IN23_1,${R0}
  647. dup $IN23_4,${IN23_4}[0]
  648. umlal2 $ACC3,$IN23_3,${R0}
  649. umlal2 $ACC4,$IN23_3,${R1}
  650. umlal2 $ACC0,$IN23_3,${S2}
  651. umlal2 $ACC1,$IN23_3,${S3}
  652. umlal2 $ACC2,$IN23_3,${S4}
  653. umlal2 $ACC3,$IN23_4,${S4}
  654. umlal2 $ACC0,$IN23_4,${S1}
  655. umlal2 $ACC4,$IN23_4,${R0}
  656. umlal2 $ACC1,$IN23_4,${S2}
  657. umlal2 $ACC2,$IN23_4,${S3}
  658. b.eq .Lshort_tail
  659. ////////////////////////////////////////////////////////////////
  660. // (hash+inp[0:1])*r^4:r^3 and accumulate
  661. add $IN01_0,$IN01_0,$H0
  662. umlal $ACC3,$IN01_2,${R1}
  663. umlal $ACC0,$IN01_2,${S3}
  664. umlal $ACC4,$IN01_2,${R2}
  665. umlal $ACC1,$IN01_2,${S4}
  666. umlal $ACC2,$IN01_2,${R0}
  667. add $IN01_1,$IN01_1,$H1
  668. umlal $ACC3,$IN01_0,${R3}
  669. umlal $ACC0,$IN01_0,${R0}
  670. umlal $ACC4,$IN01_0,${R4}
  671. umlal $ACC1,$IN01_0,${R1}
  672. umlal $ACC2,$IN01_0,${R2}
  673. add $IN01_3,$IN01_3,$H3
  674. umlal $ACC3,$IN01_1,${R2}
  675. umlal $ACC0,$IN01_1,${S4}
  676. umlal $ACC4,$IN01_1,${R3}
  677. umlal $ACC1,$IN01_1,${R0}
  678. umlal $ACC2,$IN01_1,${R1}
  679. add $IN01_4,$IN01_4,$H4
  680. umlal $ACC3,$IN01_3,${R0}
  681. umlal $ACC0,$IN01_3,${S2}
  682. umlal $ACC4,$IN01_3,${R1}
  683. umlal $ACC1,$IN01_3,${S3}
  684. umlal $ACC2,$IN01_3,${S4}
  685. umlal $ACC3,$IN01_4,${S4}
  686. umlal $ACC0,$IN01_4,${S1}
  687. umlal $ACC4,$IN01_4,${R0}
  688. umlal $ACC1,$IN01_4,${S2}
  689. umlal $ACC2,$IN01_4,${S3}
  690. .Lshort_tail:
  691. ////////////////////////////////////////////////////////////////
  692. // horizontal add
  693. addp $ACC3,$ACC3,$ACC3
  694. ldp d8,d9,[sp,#16] // meet ABI requirements
  695. addp $ACC0,$ACC0,$ACC0
  696. ldp d10,d11,[sp,#32]
  697. addp $ACC4,$ACC4,$ACC4
  698. ldp d12,d13,[sp,#48]
  699. addp $ACC1,$ACC1,$ACC1
  700. ldp d14,d15,[sp,#64]
  701. addp $ACC2,$ACC2,$ACC2
  702. ////////////////////////////////////////////////////////////////
  703. // lazy reduction, but without narrowing
  704. ushr $T0.2d,$ACC3,#26
  705. and $ACC3,$ACC3,$MASK.2d
  706. ushr $T1.2d,$ACC0,#26
  707. and $ACC0,$ACC0,$MASK.2d
  708. add $ACC4,$ACC4,$T0.2d // h3 -> h4
  709. add $ACC1,$ACC1,$T1.2d // h0 -> h1
  710. ushr $T0.2d,$ACC4,#26
  711. and $ACC4,$ACC4,$MASK.2d
  712. ushr $T1.2d,$ACC1,#26
  713. and $ACC1,$ACC1,$MASK.2d
  714. add $ACC2,$ACC2,$T1.2d // h1 -> h2
  715. add $ACC0,$ACC0,$T0.2d
  716. shl $T0.2d,$T0.2d,#2
  717. ushr $T1.2d,$ACC2,#26
  718. and $ACC2,$ACC2,$MASK.2d
  719. add $ACC0,$ACC0,$T0.2d // h4 -> h0
  720. add $ACC3,$ACC3,$T1.2d // h2 -> h3
  721. ushr $T0.2d,$ACC0,#26
  722. and $ACC0,$ACC0,$MASK.2d
  723. ushr $T1.2d,$ACC3,#26
  724. and $ACC3,$ACC3,$MASK.2d
  725. add $ACC1,$ACC1,$T0.2d // h0 -> h1
  726. add $ACC4,$ACC4,$T1.2d // h3 -> h4
  727. ////////////////////////////////////////////////////////////////
  728. // write the result, can be partially reduced
  729. st4 {$ACC0,$ACC1,$ACC2,$ACC3}[0],[$ctx],#16
  730. st1 {$ACC4}[0],[$ctx]
  731. .Lno_data_neon:
  732. ldr x29,[sp],#80
  733. .inst 0xd50323bf // autiasp
  734. ret
  735. .size poly1305_blocks_neon,.-poly1305_blocks_neon
  736. .type poly1305_emit_neon,%function
  737. .align 5
  738. poly1305_emit_neon:
  739. ldr $is_base2_26,[$ctx,#24]
  740. cbz $is_base2_26,poly1305_emit
  741. ldp w10,w11,[$ctx] // load hash value base 2^26
  742. ldp w12,w13,[$ctx,#8]
  743. ldr w14,[$ctx,#16]
  744. add $h0,x10,x11,lsl#26 // base 2^26 -> base 2^64
  745. lsr $h1,x12,#12
  746. adds $h0,$h0,x12,lsl#52
  747. add $h1,$h1,x13,lsl#14
  748. adc $h1,$h1,xzr
  749. lsr $h2,x14,#24
  750. adds $h1,$h1,x14,lsl#40
  751. adc $h2,$h2,xzr // can be partially reduced...
  752. ldp $t0,$t1,[$nonce] // load nonce
  753. and $d0,$h2,#-4 // ... so reduce
  754. add $d0,$d0,$h2,lsr#2
  755. and $h2,$h2,#3
  756. adds $h0,$h0,$d0
  757. adcs $h1,$h1,xzr
  758. adc $h2,$h2,xzr
  759. adds $d0,$h0,#5 // compare to modulus
  760. adcs $d1,$h1,xzr
  761. adc $d2,$h2,xzr
  762. tst $d2,#-4 // see if it's carried/borrowed
  763. csel $h0,$h0,$d0,eq
  764. csel $h1,$h1,$d1,eq
  765. #ifdef __ARMEB__
  766. ror $t0,$t0,#32 // flip nonce words
  767. ror $t1,$t1,#32
  768. #endif
  769. adds $h0,$h0,$t0 // accumulate nonce
  770. adc $h1,$h1,$t1
  771. #ifdef __ARMEB__
  772. rev $h0,$h0 // flip output bytes
  773. rev $h1,$h1
  774. #endif
  775. stp $h0,$h1,[$mac] // write result
  776. ret
  777. .size poly1305_emit_neon,.-poly1305_emit_neon
  778. .align 5
  779. .Lzeros:
  780. .long 0,0,0,0,0,0,0,0
  781. .LOPENSSL_armcap_P:
  782. #ifdef __ILP32__
  783. .long OPENSSL_armcap_P-.
  784. #else
  785. .quad OPENSSL_armcap_P-.
  786. #endif
  787. .asciz "Poly1305 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  788. .align 2
  789. ___
  790. foreach (split("\n",$code)) {
  791. s/\b(shrn\s+v[0-9]+)\.[24]d/$1.2s/ or
  792. s/\b(fmov\s+)v([0-9]+)[^,]*,\s*x([0-9]+)/$1d$2,x$3/ or
  793. (m/\bdup\b/ and (s/\.[24]s/.2d/g or 1)) or
  794. (m/\b(eor|and)/ and (s/\.[248][sdh]/.16b/g or 1)) or
  795. (m/\bum(ul|la)l\b/ and (s/\.4s/.2s/g or 1)) or
  796. (m/\bum(ul|la)l2\b/ and (s/\.2s/.4s/g or 1)) or
  797. (m/\bst[1-4]\s+{[^}]+}\[/ and (s/\.[24]d/.s/g or 1));
  798. s/\.[124]([sd])\[/.$1\[/;
  799. print $_,"\n";
  800. }
  801. close STDOUT or die "error closing STDOUT: $!";