ghashv8-armx.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # GHASH for ARMv8 Crypto Extension, 64-bit polynomial multiplication.
  11. #
  12. # June 2014
  13. #
  14. # Initial version was developed in tight cooperation with Ard
  15. # Biesheuvel <ard.biesheuvel@linaro.org> from bits-n-pieces from
  16. # other assembly modules. Just like aesv8-armx.pl this module
  17. # supports both AArch32 and AArch64 execution modes.
  18. #
  19. # July 2014
  20. #
  21. # Implement 2x aggregated reduction [see ghash-x86.pl for background
  22. # information].
  23. #
  24. # Current performance in cycles per processed byte:
  25. #
  26. # PMULL[2] 32-bit NEON(*)
  27. # Apple A7 0.92 5.62
  28. # Cortex-A53 1.01 8.39
  29. # Cortex-A57 1.17 7.61
  30. #
  31. # (*) presented for reference/comparison purposes;
  32. $flavour = shift;
  33. open STDOUT,">".shift;
  34. $Xi="x0"; # argument block
  35. $Htbl="x1";
  36. $inp="x2";
  37. $len="x3";
  38. $inc="x12";
  39. {
  40. my ($Xl,$Xm,$Xh,$IN)=map("q$_",(0..3));
  41. my ($t0,$t1,$t2,$xC2,$H,$Hhl,$H2)=map("q$_",(8..14));
  42. $code=<<___;
  43. #include "arm_arch.h"
  44. .text
  45. ___
  46. $code.=".arch armv8-a+crypto\n" if ($flavour =~ /64/);
  47. $code.=".fpu neon\n.code 32\n" if ($flavour !~ /64/);
  48. ################################################################################
  49. # void gcm_init_v8(u128 Htable[16],const u64 H[2]);
  50. #
  51. # input: 128-bit H - secret parameter E(K,0^128)
  52. # output: precomputed table filled with degrees of twisted H;
  53. # H is twisted to handle reverse bitness of GHASH;
  54. # only few of 16 slots of Htable[16] are used;
  55. # data is opaque to outside world (which allows to
  56. # optimize the code independently);
  57. #
  58. $code.=<<___;
  59. .global gcm_init_v8
  60. .type gcm_init_v8,%function
  61. .align 4
  62. gcm_init_v8:
  63. vld1.64 {$t1},[x1] @ load input H
  64. vmov.i8 $xC2,#0xe1
  65. vshl.i64 $xC2,$xC2,#57 @ 0xc2.0
  66. vext.8 $IN,$t1,$t1,#8
  67. vshr.u64 $t2,$xC2,#63
  68. vdup.32 $t1,${t1}[1]
  69. vext.8 $t0,$t2,$xC2,#8 @ t0=0xc2....01
  70. vshr.u64 $t2,$IN,#63
  71. vshr.s32 $t1,$t1,#31 @ broadcast carry bit
  72. vand $t2,$t2,$t0
  73. vshl.i64 $IN,$IN,#1
  74. vext.8 $t2,$t2,$t2,#8
  75. vand $t0,$t0,$t1
  76. vorr $IN,$IN,$t2 @ H<<<=1
  77. veor $H,$IN,$t0 @ twisted H
  78. vst1.64 {$H},[x0],#16 @ store Htable[0]
  79. @ calculate H^2
  80. vext.8 $t0,$H,$H,#8 @ Karatsuba pre-processing
  81. vpmull.p64 $Xl,$H,$H
  82. veor $t0,$t0,$H
  83. vpmull2.p64 $Xh,$H,$H
  84. vpmull.p64 $Xm,$t0,$t0
  85. vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
  86. veor $t2,$Xl,$Xh
  87. veor $Xm,$Xm,$t1
  88. veor $Xm,$Xm,$t2
  89. vpmull.p64 $t2,$Xl,$xC2 @ 1st phase
  90. vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
  91. vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
  92. veor $Xl,$Xm,$t2
  93. vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase
  94. vpmull.p64 $Xl,$Xl,$xC2
  95. veor $t2,$t2,$Xh
  96. veor $H2,$Xl,$t2
  97. vext.8 $t1,$H2,$H2,#8 @ Karatsuba pre-processing
  98. veor $t1,$t1,$H2
  99. vext.8 $Hhl,$t0,$t1,#8 @ pack Karatsuba pre-processed
  100. vst1.64 {$Hhl-$H2},[x0] @ store Htable[1..2]
  101. ret
  102. .size gcm_init_v8,.-gcm_init_v8
  103. ___
  104. ################################################################################
  105. # void gcm_gmult_v8(u64 Xi[2],const u128 Htable[16]);
  106. #
  107. # input: Xi - current hash value;
  108. # Htable - table precomputed in gcm_init_v8;
  109. # output: Xi - next hash value Xi;
  110. #
  111. $code.=<<___;
  112. .global gcm_gmult_v8
  113. .type gcm_gmult_v8,%function
  114. .align 4
  115. gcm_gmult_v8:
  116. vld1.64 {$t1},[$Xi] @ load Xi
  117. vmov.i8 $xC2,#0xe1
  118. vld1.64 {$H-$Hhl},[$Htbl] @ load twisted H, ...
  119. vshl.u64 $xC2,$xC2,#57
  120. #ifndef __ARMEB__
  121. vrev64.8 $t1,$t1
  122. #endif
  123. vext.8 $IN,$t1,$t1,#8
  124. vpmull.p64 $Xl,$H,$IN @ H.lo·Xi.lo
  125. veor $t1,$t1,$IN @ Karatsuba pre-processing
  126. vpmull2.p64 $Xh,$H,$IN @ H.hi·Xi.hi
  127. vpmull.p64 $Xm,$Hhl,$t1 @ (H.lo+H.hi)·(Xi.lo+Xi.hi)
  128. vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
  129. veor $t2,$Xl,$Xh
  130. veor $Xm,$Xm,$t1
  131. veor $Xm,$Xm,$t2
  132. vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
  133. vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
  134. vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
  135. veor $Xl,$Xm,$t2
  136. vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
  137. vpmull.p64 $Xl,$Xl,$xC2
  138. veor $t2,$t2,$Xh
  139. veor $Xl,$Xl,$t2
  140. #ifndef __ARMEB__
  141. vrev64.8 $Xl,$Xl
  142. #endif
  143. vext.8 $Xl,$Xl,$Xl,#8
  144. vst1.64 {$Xl},[$Xi] @ write out Xi
  145. ret
  146. .size gcm_gmult_v8,.-gcm_gmult_v8
  147. ___
  148. ################################################################################
  149. # void gcm_ghash_v8(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
  150. #
  151. # input: table precomputed in gcm_init_v8;
  152. # current hash value Xi;
  153. # pointer to input data;
  154. # length of input data in bytes, but divisible by block size;
  155. # output: next hash value Xi;
  156. #
  157. $code.=<<___;
  158. .global gcm_ghash_v8
  159. .type gcm_ghash_v8,%function
  160. .align 4
  161. gcm_ghash_v8:
  162. ___
  163. $code.=<<___ if ($flavour !~ /64/);
  164. vstmdb sp!,{d8-d15} @ 32-bit ABI says so
  165. ___
  166. $code.=<<___;
  167. vld1.64 {$Xl},[$Xi] @ load [rotated] Xi
  168. @ "[rotated]" means that
  169. @ loaded value would have
  170. @ to be rotated in order to
  171. @ make it appear as in
  172. @ alorithm specification
  173. subs $len,$len,#32 @ see if $len is 32 or larger
  174. mov $inc,#16 @ $inc is used as post-
  175. @ increment for input pointer;
  176. @ as loop is modulo-scheduled
  177. @ $inc is zeroed just in time
  178. @ to preclude oversteping
  179. @ inp[len], which means that
  180. @ last block[s] are actually
  181. @ loaded twice, but last
  182. @ copy is not processed
  183. vld1.64 {$H-$Hhl},[$Htbl],#32 @ load twisted H, ..., H^2
  184. vmov.i8 $xC2,#0xe1
  185. vld1.64 {$H2},[$Htbl]
  186. cclr $inc,eq @ is it time to zero $inc?
  187. vext.8 $Xl,$Xl,$Xl,#8 @ rotate Xi
  188. vld1.64 {$t0},[$inp],#16 @ load [rotated] I[0]
  189. vshl.u64 $xC2,$xC2,#57 @ compose 0xc2.0 constant
  190. #ifndef __ARMEB__
  191. vrev64.8 $t0,$t0
  192. vrev64.8 $Xl,$Xl
  193. #endif
  194. vext.8 $IN,$t0,$t0,#8 @ rotate I[0]
  195. b.lo .Lodd_tail_v8 @ $len was less than 32
  196. ___
  197. { my ($Xln,$Xmn,$Xhn,$In) = map("q$_",(4..7));
  198. #######
  199. # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
  200. # [(H*Ii+1) + (H*Xi+1)] mod P =
  201. # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
  202. #
  203. $code.=<<___;
  204. vld1.64 {$t1},[$inp],$inc @ load [rotated] I[1]
  205. #ifndef __ARMEB__
  206. vrev64.8 $t1,$t1
  207. #endif
  208. vext.8 $In,$t1,$t1,#8
  209. veor $IN,$IN,$Xl @ I[i]^=Xi
  210. vpmull.p64 $Xln,$H,$In @ H·Ii+1
  211. veor $t1,$t1,$In @ Karatsuba pre-processing
  212. vpmull2.p64 $Xhn,$H,$In
  213. b .Loop_mod2x_v8
  214. .align 4
  215. .Loop_mod2x_v8:
  216. vext.8 $t2,$IN,$IN,#8
  217. subs $len,$len,#32 @ is there more data?
  218. vpmull.p64 $Xl,$H2,$IN @ H^2.lo·Xi.lo
  219. cclr $inc,lo @ is it time to zero $inc?
  220. vpmull.p64 $Xmn,$Hhl,$t1
  221. veor $t2,$t2,$IN @ Karatsuba pre-processing
  222. vpmull2.p64 $Xh,$H2,$IN @ H^2.hi·Xi.hi
  223. veor $Xl,$Xl,$Xln @ accumulate
  224. vpmull2.p64 $Xm,$Hhl,$t2 @ (H^2.lo+H^2.hi)·(Xi.lo+Xi.hi)
  225. vld1.64 {$t0},[$inp],$inc @ load [rotated] I[i+2]
  226. veor $Xh,$Xh,$Xhn
  227. cclr $inc,eq @ is it time to zero $inc?
  228. veor $Xm,$Xm,$Xmn
  229. vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
  230. veor $t2,$Xl,$Xh
  231. veor $Xm,$Xm,$t1
  232. vld1.64 {$t1},[$inp],$inc @ load [rotated] I[i+3]
  233. #ifndef __ARMEB__
  234. vrev64.8 $t0,$t0
  235. #endif
  236. veor $Xm,$Xm,$t2
  237. vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
  238. #ifndef __ARMEB__
  239. vrev64.8 $t1,$t1
  240. #endif
  241. vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
  242. vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
  243. vext.8 $In,$t1,$t1,#8
  244. vext.8 $IN,$t0,$t0,#8
  245. veor $Xl,$Xm,$t2
  246. vpmull.p64 $Xln,$H,$In @ H·Ii+1
  247. veor $IN,$IN,$Xh @ accumulate $IN early
  248. vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
  249. vpmull.p64 $Xl,$Xl,$xC2
  250. veor $IN,$IN,$t2
  251. veor $t1,$t1,$In @ Karatsuba pre-processing
  252. veor $IN,$IN,$Xl
  253. vpmull2.p64 $Xhn,$H,$In
  254. b.hs .Loop_mod2x_v8 @ there was at least 32 more bytes
  255. veor $Xh,$Xh,$t2
  256. vext.8 $IN,$t0,$t0,#8 @ re-construct $IN
  257. adds $len,$len,#32 @ re-construct $len
  258. veor $Xl,$Xl,$Xh @ re-construct $Xl
  259. b.eq .Ldone_v8 @ is $len zero?
  260. ___
  261. }
  262. $code.=<<___;
  263. .Lodd_tail_v8:
  264. vext.8 $t2,$Xl,$Xl,#8
  265. veor $IN,$IN,$Xl @ inp^=Xi
  266. veor $t1,$t0,$t2 @ $t1 is rotated inp^Xi
  267. vpmull.p64 $Xl,$H,$IN @ H.lo·Xi.lo
  268. veor $t1,$t1,$IN @ Karatsuba pre-processing
  269. vpmull2.p64 $Xh,$H,$IN @ H.hi·Xi.hi
  270. vpmull.p64 $Xm,$Hhl,$t1 @ (H.lo+H.hi)·(Xi.lo+Xi.hi)
  271. vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
  272. veor $t2,$Xl,$Xh
  273. veor $Xm,$Xm,$t1
  274. veor $Xm,$Xm,$t2
  275. vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
  276. vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
  277. vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
  278. veor $Xl,$Xm,$t2
  279. vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
  280. vpmull.p64 $Xl,$Xl,$xC2
  281. veor $t2,$t2,$Xh
  282. veor $Xl,$Xl,$t2
  283. .Ldone_v8:
  284. #ifndef __ARMEB__
  285. vrev64.8 $Xl,$Xl
  286. #endif
  287. vext.8 $Xl,$Xl,$Xl,#8
  288. vst1.64 {$Xl},[$Xi] @ write out Xi
  289. ___
  290. $code.=<<___ if ($flavour !~ /64/);
  291. vldmia sp!,{d8-d15} @ 32-bit ABI says so
  292. ___
  293. $code.=<<___;
  294. ret
  295. .size gcm_ghash_v8,.-gcm_ghash_v8
  296. ___
  297. }
  298. $code.=<<___;
  299. .asciz "GHASH for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
  300. .align 2
  301. ___
  302. if ($flavour =~ /64/) { ######## 64-bit code
  303. sub unvmov {
  304. my $arg=shift;
  305. $arg =~ m/q([0-9]+)#(lo|hi),\s*q([0-9]+)#(lo|hi)/o &&
  306. sprintf "ins v%d.d[%d],v%d.d[%d]",$1,($2 eq "lo")?0:1,$3,($4 eq "lo")?0:1;
  307. }
  308. foreach(split("\n",$code)) {
  309. s/cclr\s+([wx])([^,]+),\s*([a-z]+)/csel $1$2,$1zr,$1$2,$3/o or
  310. s/vmov\.i8/movi/o or # fix up legacy mnemonics
  311. s/vmov\s+(.*)/unvmov($1)/geo or
  312. s/vext\.8/ext/o or
  313. s/vshr\.s/sshr\.s/o or
  314. s/vshr/ushr/o or
  315. s/^(\s+)v/$1/o or # strip off v prefix
  316. s/\bbx\s+lr\b/ret/o;
  317. s/\bq([0-9]+)\b/"v".($1<8?$1:$1+8).".16b"/geo; # old->new registers
  318. s/@\s/\/\//o; # old->new style commentary
  319. # fix up remainig legacy suffixes
  320. s/\.[ui]?8(\s)/$1/o;
  321. s/\.[uis]?32//o and s/\.16b/\.4s/go;
  322. m/\.p64/o and s/\.16b/\.1q/o; # 1st pmull argument
  323. m/l\.p64/o and s/\.16b/\.1d/go; # 2nd and 3rd pmull arguments
  324. s/\.[uisp]?64//o and s/\.16b/\.2d/go;
  325. s/\.[42]([sd])\[([0-3])\]/\.$1\[$2\]/o;
  326. print $_,"\n";
  327. }
  328. } else { ######## 32-bit code
  329. sub unvdup32 {
  330. my $arg=shift;
  331. $arg =~ m/q([0-9]+),\s*q([0-9]+)\[([0-3])\]/o &&
  332. sprintf "vdup.32 q%d,d%d[%d]",$1,2*$2+($3>>1),$3&1;
  333. }
  334. sub unvpmullp64 {
  335. my ($mnemonic,$arg)=@_;
  336. if ($arg =~ m/q([0-9]+),\s*q([0-9]+),\s*q([0-9]+)/o) {
  337. my $word = 0xf2a00e00|(($1&7)<<13)|(($1&8)<<19)
  338. |(($2&7)<<17)|(($2&8)<<4)
  339. |(($3&7)<<1) |(($3&8)<<2);
  340. $word |= 0x00010001 if ($mnemonic =~ "2");
  341. # since ARMv7 instructions are always encoded little-endian.
  342. # correct solution is to use .inst directive, but older
  343. # assemblers don't implement it:-(
  344. sprintf ".byte\t0x%02x,0x%02x,0x%02x,0x%02x\t@ %s %s",
  345. $word&0xff,($word>>8)&0xff,
  346. ($word>>16)&0xff,($word>>24)&0xff,
  347. $mnemonic,$arg;
  348. }
  349. }
  350. foreach(split("\n",$code)) {
  351. s/\b[wx]([0-9]+)\b/r$1/go; # new->old registers
  352. s/\bv([0-9])\.[12468]+[bsd]\b/q$1/go; # new->old registers
  353. s/\/\/\s?/@ /o; # new->old style commentary
  354. # fix up remainig new-style suffixes
  355. s/\],#[0-9]+/]!/o;
  356. s/cclr\s+([^,]+),\s*([a-z]+)/mov$2 $1,#0/o or
  357. s/vdup\.32\s+(.*)/unvdup32($1)/geo or
  358. s/v?(pmull2?)\.p64\s+(.*)/unvpmullp64($1,$2)/geo or
  359. s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo or
  360. s/^(\s+)b\./$1b/o or
  361. s/^(\s+)ret/$1bx\tlr/o;
  362. print $_,"\n";
  363. }
  364. }
  365. close STDOUT; # enforce flush