poly1305-armv8.pl 22 KB

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