You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

431 lines
12 KiB

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