ghash-armv4.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #! /usr/bin/env perl
  2. # Copyright 2010-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. # April 2010
  17. #
  18. # The module implements "4-bit" GCM GHASH function and underlying
  19. # single multiplication operation in GF(2^128). "4-bit" means that it
  20. # uses 256 bytes per-key table [+32 bytes shared table]. There is no
  21. # experimental performance data available yet. The only approximation
  22. # that can be made at this point is based on code size. Inner loop is
  23. # 32 instructions long and on single-issue core should execute in <40
  24. # cycles. Having verified that gcc 3.4 didn't unroll corresponding
  25. # loop, this assembler loop body was found to be ~3x smaller than
  26. # compiler-generated one...
  27. #
  28. # July 2010
  29. #
  30. # Rescheduling for dual-issue pipeline resulted in 8.5% improvement on
  31. # Cortex A8 core and ~25 cycles per processed byte (which was observed
  32. # to be ~3 times faster than gcc-generated code:-)
  33. #
  34. # February 2011
  35. #
  36. # Profiler-assisted and platform-specific optimization resulted in 7%
  37. # improvement on Cortex A8 core and ~23.5 cycles per byte.
  38. #
  39. # March 2011
  40. #
  41. # Add NEON implementation featuring polynomial multiplication, i.e. no
  42. # lookup tables involved. On Cortex A8 it was measured to process one
  43. # byte in 15 cycles or 55% faster than integer-only code.
  44. #
  45. # April 2014
  46. #
  47. # Switch to multiplication algorithm suggested in paper referred
  48. # below and combine it with reduction algorithm from x86 module.
  49. # Performance improvement over previous version varies from 65% on
  50. # Snapdragon S4 to 110% on Cortex A9. In absolute terms Cortex A8
  51. # processes one byte in 8.45 cycles, A9 - in 10.2, A15 - in 7.63,
  52. # Snapdragon S4 - in 9.33.
  53. #
  54. # Câmara, D.; Gouvêa, C. P. L.; López, J. & Dahab, R.: Fast Software
  55. # Polynomial Multiplication on ARM Processors using the NEON Engine.
  56. #
  57. # http://conradoplg.cryptoland.net/files/2010/12/mocrysen13.pdf
  58. # ====================================================================
  59. # Note about "528B" variant. In ARM case it makes lesser sense to
  60. # implement it for following reasons:
  61. #
  62. # - performance improvement won't be anywhere near 50%, because 128-
  63. # bit shift operation is neatly fused with 128-bit xor here, and
  64. # "538B" variant would eliminate only 4-5 instructions out of 32
  65. # in the inner loop (meaning that estimated improvement is ~15%);
  66. # - ARM-based systems are often embedded ones and extra memory
  67. # consumption might be unappreciated (for so little improvement);
  68. #
  69. # Byte order [in]dependence. =========================================
  70. #
  71. # Caller is expected to maintain specific *dword* order in Htable,
  72. # namely with *least* significant dword of 128-bit value at *lower*
  73. # address. This differs completely from C code and has everything to
  74. # do with ldm instruction and order in which dwords are "consumed" by
  75. # algorithm. *Byte* order within these dwords in turn is whatever
  76. # *native* byte order on current platform. See gcm128.c for working
  77. # example...
  78. $flavour = shift;
  79. if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
  80. else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
  81. if ($flavour && $flavour ne "void") {
  82. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  83. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  84. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  85. die "can't locate arm-xlate.pl";
  86. open STDOUT,"| \"$^X\" $xlate $flavour $output";
  87. } else {
  88. open STDOUT,">$output";
  89. }
  90. $Xi="r0"; # argument block
  91. $Htbl="r1";
  92. $inp="r2";
  93. $len="r3";
  94. $Zll="r4"; # variables
  95. $Zlh="r5";
  96. $Zhl="r6";
  97. $Zhh="r7";
  98. $Tll="r8";
  99. $Tlh="r9";
  100. $Thl="r10";
  101. $Thh="r11";
  102. $nlo="r12";
  103. ################# r13 is stack pointer
  104. $nhi="r14";
  105. ################# r15 is program counter
  106. $rem_4bit=$inp; # used in gcm_gmult_4bit
  107. $cnt=$len;
  108. sub Zsmash() {
  109. my $i=12;
  110. my @args=@_;
  111. for ($Zll,$Zlh,$Zhl,$Zhh) {
  112. $code.=<<___;
  113. #if __ARM_ARCH__>=7 && defined(__ARMEL__)
  114. rev $_,$_
  115. str $_,[$Xi,#$i]
  116. #elif defined(__ARMEB__)
  117. str $_,[$Xi,#$i]
  118. #else
  119. mov $Tlh,$_,lsr#8
  120. strb $_,[$Xi,#$i+3]
  121. mov $Thl,$_,lsr#16
  122. strb $Tlh,[$Xi,#$i+2]
  123. mov $Thh,$_,lsr#24
  124. strb $Thl,[$Xi,#$i+1]
  125. strb $Thh,[$Xi,#$i]
  126. #endif
  127. ___
  128. $code.="\t".shift(@args)."\n";
  129. $i-=4;
  130. }
  131. }
  132. $code=<<___;
  133. #include "arm_arch.h"
  134. .text
  135. #if defined(__thumb2__) || defined(__clang__)
  136. .syntax unified
  137. #define ldrplb ldrbpl
  138. #define ldrneb ldrbne
  139. #endif
  140. #if defined(__thumb2__)
  141. .thumb
  142. #else
  143. .code 32
  144. #endif
  145. .type rem_4bit,%object
  146. .align 5
  147. rem_4bit:
  148. .short 0x0000,0x1C20,0x3840,0x2460
  149. .short 0x7080,0x6CA0,0x48C0,0x54E0
  150. .short 0xE100,0xFD20,0xD940,0xC560
  151. .short 0x9180,0x8DA0,0xA9C0,0xB5E0
  152. .size rem_4bit,.-rem_4bit
  153. .type rem_4bit_get,%function
  154. rem_4bit_get:
  155. #if defined(__thumb2__)
  156. adr $rem_4bit,rem_4bit
  157. #else
  158. sub $rem_4bit,pc,#8+32 @ &rem_4bit
  159. #endif
  160. b .Lrem_4bit_got
  161. nop
  162. nop
  163. .size rem_4bit_get,.-rem_4bit_get
  164. .global gcm_ghash_4bit
  165. .type gcm_ghash_4bit,%function
  166. .align 4
  167. gcm_ghash_4bit:
  168. #if defined(__thumb2__)
  169. adr r12,rem_4bit
  170. #else
  171. sub r12,pc,#8+48 @ &rem_4bit
  172. #endif
  173. add $len,$inp,$len @ $len to point at the end
  174. stmdb sp!,{r3-r11,lr} @ save $len/end too
  175. ldmia r12,{r4-r11} @ copy rem_4bit ...
  176. stmdb sp!,{r4-r11} @ ... to stack
  177. ldrb $nlo,[$inp,#15]
  178. ldrb $nhi,[$Xi,#15]
  179. .Louter:
  180. eor $nlo,$nlo,$nhi
  181. and $nhi,$nlo,#0xf0
  182. and $nlo,$nlo,#0x0f
  183. mov $cnt,#14
  184. add $Zhh,$Htbl,$nlo,lsl#4
  185. ldmia $Zhh,{$Zll-$Zhh} @ load Htbl[nlo]
  186. add $Thh,$Htbl,$nhi
  187. ldrb $nlo,[$inp,#14]
  188. and $nhi,$Zll,#0xf @ rem
  189. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  190. add $nhi,$nhi,$nhi
  191. eor $Zll,$Tll,$Zll,lsr#4
  192. ldrh $Tll,[sp,$nhi] @ rem_4bit[rem]
  193. eor $Zll,$Zll,$Zlh,lsl#28
  194. ldrb $nhi,[$Xi,#14]
  195. eor $Zlh,$Tlh,$Zlh,lsr#4
  196. eor $Zlh,$Zlh,$Zhl,lsl#28
  197. eor $Zhl,$Thl,$Zhl,lsr#4
  198. eor $Zhl,$Zhl,$Zhh,lsl#28
  199. eor $Zhh,$Thh,$Zhh,lsr#4
  200. eor $nlo,$nlo,$nhi
  201. and $nhi,$nlo,#0xf0
  202. and $nlo,$nlo,#0x0f
  203. eor $Zhh,$Zhh,$Tll,lsl#16
  204. .Linner:
  205. add $Thh,$Htbl,$nlo,lsl#4
  206. and $nlo,$Zll,#0xf @ rem
  207. subs $cnt,$cnt,#1
  208. add $nlo,$nlo,$nlo
  209. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nlo]
  210. eor $Zll,$Tll,$Zll,lsr#4
  211. eor $Zll,$Zll,$Zlh,lsl#28
  212. eor $Zlh,$Tlh,$Zlh,lsr#4
  213. eor $Zlh,$Zlh,$Zhl,lsl#28
  214. ldrh $Tll,[sp,$nlo] @ rem_4bit[rem]
  215. eor $Zhl,$Thl,$Zhl,lsr#4
  216. #ifdef __thumb2__
  217. it pl
  218. #endif
  219. ldrplb $nlo,[$inp,$cnt]
  220. eor $Zhl,$Zhl,$Zhh,lsl#28
  221. eor $Zhh,$Thh,$Zhh,lsr#4
  222. add $Thh,$Htbl,$nhi
  223. and $nhi,$Zll,#0xf @ rem
  224. eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
  225. add $nhi,$nhi,$nhi
  226. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  227. eor $Zll,$Tll,$Zll,lsr#4
  228. #ifdef __thumb2__
  229. it pl
  230. #endif
  231. ldrplb $Tll,[$Xi,$cnt]
  232. eor $Zll,$Zll,$Zlh,lsl#28
  233. eor $Zlh,$Tlh,$Zlh,lsr#4
  234. ldrh $Tlh,[sp,$nhi]
  235. eor $Zlh,$Zlh,$Zhl,lsl#28
  236. eor $Zhl,$Thl,$Zhl,lsr#4
  237. eor $Zhl,$Zhl,$Zhh,lsl#28
  238. #ifdef __thumb2__
  239. it pl
  240. #endif
  241. eorpl $nlo,$nlo,$Tll
  242. eor $Zhh,$Thh,$Zhh,lsr#4
  243. #ifdef __thumb2__
  244. itt pl
  245. #endif
  246. andpl $nhi,$nlo,#0xf0
  247. andpl $nlo,$nlo,#0x0f
  248. eor $Zhh,$Zhh,$Tlh,lsl#16 @ ^= rem_4bit[rem]
  249. bpl .Linner
  250. ldr $len,[sp,#32] @ re-load $len/end
  251. add $inp,$inp,#16
  252. mov $nhi,$Zll
  253. ___
  254. &Zsmash("cmp\t$inp,$len","\n".
  255. "#ifdef __thumb2__\n".
  256. " it ne\n".
  257. "#endif\n".
  258. " ldrneb $nlo,[$inp,#15]");
  259. $code.=<<___;
  260. bne .Louter
  261. add sp,sp,#36
  262. #if __ARM_ARCH__>=5
  263. ldmia sp!,{r4-r11,pc}
  264. #else
  265. ldmia sp!,{r4-r11,lr}
  266. tst lr,#1
  267. moveq pc,lr @ be binary compatible with V4, yet
  268. bx lr @ interoperable with Thumb ISA:-)
  269. #endif
  270. .size gcm_ghash_4bit,.-gcm_ghash_4bit
  271. .global gcm_gmult_4bit
  272. .type gcm_gmult_4bit,%function
  273. gcm_gmult_4bit:
  274. stmdb sp!,{r4-r11,lr}
  275. ldrb $nlo,[$Xi,#15]
  276. b rem_4bit_get
  277. .Lrem_4bit_got:
  278. and $nhi,$nlo,#0xf0
  279. and $nlo,$nlo,#0x0f
  280. mov $cnt,#14
  281. add $Zhh,$Htbl,$nlo,lsl#4
  282. ldmia $Zhh,{$Zll-$Zhh} @ load Htbl[nlo]
  283. ldrb $nlo,[$Xi,#14]
  284. add $Thh,$Htbl,$nhi
  285. and $nhi,$Zll,#0xf @ rem
  286. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  287. add $nhi,$nhi,$nhi
  288. eor $Zll,$Tll,$Zll,lsr#4
  289. ldrh $Tll,[$rem_4bit,$nhi] @ rem_4bit[rem]
  290. eor $Zll,$Zll,$Zlh,lsl#28
  291. eor $Zlh,$Tlh,$Zlh,lsr#4
  292. eor $Zlh,$Zlh,$Zhl,lsl#28
  293. eor $Zhl,$Thl,$Zhl,lsr#4
  294. eor $Zhl,$Zhl,$Zhh,lsl#28
  295. eor $Zhh,$Thh,$Zhh,lsr#4
  296. and $nhi,$nlo,#0xf0
  297. eor $Zhh,$Zhh,$Tll,lsl#16
  298. and $nlo,$nlo,#0x0f
  299. .Loop:
  300. add $Thh,$Htbl,$nlo,lsl#4
  301. and $nlo,$Zll,#0xf @ rem
  302. subs $cnt,$cnt,#1
  303. add $nlo,$nlo,$nlo
  304. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nlo]
  305. eor $Zll,$Tll,$Zll,lsr#4
  306. eor $Zll,$Zll,$Zlh,lsl#28
  307. eor $Zlh,$Tlh,$Zlh,lsr#4
  308. eor $Zlh,$Zlh,$Zhl,lsl#28
  309. ldrh $Tll,[$rem_4bit,$nlo] @ rem_4bit[rem]
  310. eor $Zhl,$Thl,$Zhl,lsr#4
  311. #ifdef __thumb2__
  312. it pl
  313. #endif
  314. ldrplb $nlo,[$Xi,$cnt]
  315. eor $Zhl,$Zhl,$Zhh,lsl#28
  316. eor $Zhh,$Thh,$Zhh,lsr#4
  317. add $Thh,$Htbl,$nhi
  318. and $nhi,$Zll,#0xf @ rem
  319. eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
  320. add $nhi,$nhi,$nhi
  321. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  322. eor $Zll,$Tll,$Zll,lsr#4
  323. eor $Zll,$Zll,$Zlh,lsl#28
  324. eor $Zlh,$Tlh,$Zlh,lsr#4
  325. ldrh $Tll,[$rem_4bit,$nhi] @ rem_4bit[rem]
  326. eor $Zlh,$Zlh,$Zhl,lsl#28
  327. eor $Zhl,$Thl,$Zhl,lsr#4
  328. eor $Zhl,$Zhl,$Zhh,lsl#28
  329. eor $Zhh,$Thh,$Zhh,lsr#4
  330. #ifdef __thumb2__
  331. itt pl
  332. #endif
  333. andpl $nhi,$nlo,#0xf0
  334. andpl $nlo,$nlo,#0x0f
  335. eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
  336. bpl .Loop
  337. ___
  338. &Zsmash();
  339. $code.=<<___;
  340. #if __ARM_ARCH__>=5
  341. ldmia sp!,{r4-r11,pc}
  342. #else
  343. ldmia sp!,{r4-r11,lr}
  344. tst lr,#1
  345. moveq pc,lr @ be binary compatible with V4, yet
  346. bx lr @ interoperable with Thumb ISA:-)
  347. #endif
  348. .size gcm_gmult_4bit,.-gcm_gmult_4bit
  349. ___
  350. {
  351. my ($Xl,$Xm,$Xh,$IN)=map("q$_",(0..3));
  352. my ($t0,$t1,$t2,$t3)=map("q$_",(8..12));
  353. my ($Hlo,$Hhi,$Hhl,$k48,$k32,$k16)=map("d$_",(26..31));
  354. sub clmul64x64 {
  355. my ($r,$a,$b)=@_;
  356. $code.=<<___;
  357. vext.8 $t0#lo, $a, $a, #1 @ A1
  358. vmull.p8 $t0, $t0#lo, $b @ F = A1*B
  359. vext.8 $r#lo, $b, $b, #1 @ B1
  360. vmull.p8 $r, $a, $r#lo @ E = A*B1
  361. vext.8 $t1#lo, $a, $a, #2 @ A2
  362. vmull.p8 $t1, $t1#lo, $b @ H = A2*B
  363. vext.8 $t3#lo, $b, $b, #2 @ B2
  364. vmull.p8 $t3, $a, $t3#lo @ G = A*B2
  365. vext.8 $t2#lo, $a, $a, #3 @ A3
  366. veor $t0, $t0, $r @ L = E + F
  367. vmull.p8 $t2, $t2#lo, $b @ J = A3*B
  368. vext.8 $r#lo, $b, $b, #3 @ B3
  369. veor $t1, $t1, $t3 @ M = G + H
  370. vmull.p8 $r, $a, $r#lo @ I = A*B3
  371. veor $t0#lo, $t0#lo, $t0#hi @ t0 = (L) (P0 + P1) << 8
  372. vand $t0#hi, $t0#hi, $k48
  373. vext.8 $t3#lo, $b, $b, #4 @ B4
  374. veor $t1#lo, $t1#lo, $t1#hi @ t1 = (M) (P2 + P3) << 16
  375. vand $t1#hi, $t1#hi, $k32
  376. vmull.p8 $t3, $a, $t3#lo @ K = A*B4
  377. veor $t2, $t2, $r @ N = I + J
  378. veor $t0#lo, $t0#lo, $t0#hi
  379. veor $t1#lo, $t1#lo, $t1#hi
  380. veor $t2#lo, $t2#lo, $t2#hi @ t2 = (N) (P4 + P5) << 24
  381. vand $t2#hi, $t2#hi, $k16
  382. vext.8 $t0, $t0, $t0, #15
  383. veor $t3#lo, $t3#lo, $t3#hi @ t3 = (K) (P6 + P7) << 32
  384. vmov.i64 $t3#hi, #0
  385. vext.8 $t1, $t1, $t1, #14
  386. veor $t2#lo, $t2#lo, $t2#hi
  387. vmull.p8 $r, $a, $b @ D = A*B
  388. vext.8 $t3, $t3, $t3, #12
  389. vext.8 $t2, $t2, $t2, #13
  390. veor $t0, $t0, $t1
  391. veor $t2, $t2, $t3
  392. veor $r, $r, $t0
  393. veor $r, $r, $t2
  394. ___
  395. }
  396. $code.=<<___;
  397. #if __ARM_MAX_ARCH__>=7
  398. .arch armv7-a
  399. .fpu neon
  400. .global gcm_init_neon
  401. .type gcm_init_neon,%function
  402. .align 4
  403. gcm_init_neon:
  404. vld1.64 $IN#hi,[r1]! @ load H
  405. vmov.i8 $t0,#0xe1
  406. vld1.64 $IN#lo,[r1]
  407. vshl.i64 $t0#hi,#57
  408. vshr.u64 $t0#lo,#63 @ t0=0xc2....01
  409. vdup.8 $t1,$IN#hi[7]
  410. vshr.u64 $Hlo,$IN#lo,#63
  411. vshr.s8 $t1,#7 @ broadcast carry bit
  412. vshl.i64 $IN,$IN,#1
  413. vand $t0,$t0,$t1
  414. vorr $IN#hi,$Hlo @ H<<<=1
  415. veor $IN,$IN,$t0 @ twisted H
  416. vstmia r0,{$IN}
  417. ret @ bx lr
  418. .size gcm_init_neon,.-gcm_init_neon
  419. .global gcm_gmult_neon
  420. .type gcm_gmult_neon,%function
  421. .align 4
  422. gcm_gmult_neon:
  423. vld1.64 $IN#hi,[$Xi]! @ load Xi
  424. vld1.64 $IN#lo,[$Xi]!
  425. vmov.i64 $k48,#0x0000ffffffffffff
  426. vldmia $Htbl,{$Hlo-$Hhi} @ load twisted H
  427. vmov.i64 $k32,#0x00000000ffffffff
  428. #ifdef __ARMEL__
  429. vrev64.8 $IN,$IN
  430. #endif
  431. vmov.i64 $k16,#0x000000000000ffff
  432. veor $Hhl,$Hlo,$Hhi @ Karatsuba pre-processing
  433. mov $len,#16
  434. b .Lgmult_neon
  435. .size gcm_gmult_neon,.-gcm_gmult_neon
  436. .global gcm_ghash_neon
  437. .type gcm_ghash_neon,%function
  438. .align 4
  439. gcm_ghash_neon:
  440. vld1.64 $Xl#hi,[$Xi]! @ load Xi
  441. vld1.64 $Xl#lo,[$Xi]!
  442. vmov.i64 $k48,#0x0000ffffffffffff
  443. vldmia $Htbl,{$Hlo-$Hhi} @ load twisted H
  444. vmov.i64 $k32,#0x00000000ffffffff
  445. #ifdef __ARMEL__
  446. vrev64.8 $Xl,$Xl
  447. #endif
  448. vmov.i64 $k16,#0x000000000000ffff
  449. veor $Hhl,$Hlo,$Hhi @ Karatsuba pre-processing
  450. .Loop_neon:
  451. vld1.64 $IN#hi,[$inp]! @ load inp
  452. vld1.64 $IN#lo,[$inp]!
  453. #ifdef __ARMEL__
  454. vrev64.8 $IN,$IN
  455. #endif
  456. veor $IN,$Xl @ inp^=Xi
  457. .Lgmult_neon:
  458. ___
  459. &clmul64x64 ($Xl,$Hlo,"$IN#lo"); # H.lo·Xi.lo
  460. $code.=<<___;
  461. veor $IN#lo,$IN#lo,$IN#hi @ Karatsuba pre-processing
  462. ___
  463. &clmul64x64 ($Xm,$Hhl,"$IN#lo"); # (H.lo+H.hi)·(Xi.lo+Xi.hi)
  464. &clmul64x64 ($Xh,$Hhi,"$IN#hi"); # H.hi·Xi.hi
  465. $code.=<<___;
  466. veor $Xm,$Xm,$Xl @ Karatsuba post-processing
  467. veor $Xm,$Xm,$Xh
  468. veor $Xl#hi,$Xl#hi,$Xm#lo
  469. veor $Xh#lo,$Xh#lo,$Xm#hi @ Xh|Xl - 256-bit result
  470. @ equivalent of reduction_avx from ghash-x86_64.pl
  471. vshl.i64 $t1,$Xl,#57 @ 1st phase
  472. vshl.i64 $t2,$Xl,#62
  473. veor $t2,$t2,$t1 @
  474. vshl.i64 $t1,$Xl,#63
  475. veor $t2, $t2, $t1 @
  476. veor $Xl#hi,$Xl#hi,$t2#lo @
  477. veor $Xh#lo,$Xh#lo,$t2#hi
  478. vshr.u64 $t2,$Xl,#1 @ 2nd phase
  479. veor $Xh,$Xh,$Xl
  480. veor $Xl,$Xl,$t2 @
  481. vshr.u64 $t2,$t2,#6
  482. vshr.u64 $Xl,$Xl,#1 @
  483. veor $Xl,$Xl,$Xh @
  484. veor $Xl,$Xl,$t2 @
  485. subs $len,#16
  486. bne .Loop_neon
  487. #ifdef __ARMEL__
  488. vrev64.8 $Xl,$Xl
  489. #endif
  490. sub $Xi,#16
  491. vst1.64 $Xl#hi,[$Xi]! @ write out Xi
  492. vst1.64 $Xl#lo,[$Xi]
  493. ret @ bx lr
  494. .size gcm_ghash_neon,.-gcm_ghash_neon
  495. #endif
  496. ___
  497. }
  498. $code.=<<___;
  499. .asciz "GHASH for ARMv4/NEON, CRYPTOGAMS by <appro\@openssl.org>"
  500. .align 2
  501. ___
  502. foreach (split("\n",$code)) {
  503. s/\`([^\`]*)\`/eval $1/geo;
  504. s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo or
  505. s/\bret\b/bx lr/go or
  506. s/\bbx\s+lr\b/.word\t0xe12fff1e/go; # make it possible to compile with -march=armv4
  507. print $_,"\n";
  508. }
  509. close STDOUT or die "error closing STDOUT: $!"; # enforce flush