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 # ====================================================================
10 # GHASH for ARMv8 Crypto Extension, 64-bit polynomial multiplication.
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.
21 # Implement 2x aggregated reduction [see ghash-x86.pl for background
24 # Current performance in cycles per processed byte:
26 # PMULL[2] 32-bit NEON(*)
28 # Cortex-A53 1.01 8.39
29 # Cortex-A57 1.17 7.61
31 # (*) presented for reference/comparison purposes;
36 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
37 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
38 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
39 die "can't locate arm-xlate.pl";
41 open OUT,"| \"$^X\" $xlate $flavour $output";
44 $Xi="x0"; # argument block
52 my ($Xl,$Xm,$Xh,$IN)=map("q$_",(0..3));
53 my ($t0,$t1,$t2,$xC2,$H,$Hhl,$H2)=map("q$_",(8..14));
60 $code.=".arch armv8-a+crypto\n" if ($flavour =~ /64/);
61 $code.=".fpu neon\n.code 32\n" if ($flavour !~ /64/);
63 ################################################################################
64 # void gcm_init_v8(u128 Htable[16],const u64 H[2]);
66 # input: 128-bit H - secret parameter E(K,0^128)
67 # output: precomputed table filled with degrees of twisted H;
68 # H is twisted to handle reverse bitness of GHASH;
69 # only few of 16 slots of Htable[16] are used;
70 # data is opaque to outside world (which allows to
71 # optimize the code independently);
75 .type gcm_init_v8,%function
78 vld1.64 {$t1},[x1] @ load input H
80 vshl.i64 $xC2,$xC2,#57 @ 0xc2.0
84 vext.8 $t0,$t2,$xC2,#8 @ t0=0xc2....01
86 vshr.s32 $t1,$t1,#31 @ broadcast carry bit
91 vorr $IN,$IN,$t2 @ H<<<=1
92 veor $H,$IN,$t0 @ twisted H
93 vst1.64 {$H},[x0],#16 @ store Htable[0]
96 vext.8 $t0,$H,$H,#8 @ Karatsuba pre-processing
100 vpmull.p64 $Xm,$t0,$t0
102 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
106 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
112 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase
113 vpmull.p64 $Xl,$Xl,$xC2
117 vext.8 $t1,$H2,$H2,#8 @ Karatsuba pre-processing
119 vext.8 $Hhl,$t0,$t1,#8 @ pack Karatsuba pre-processed
120 vst1.64 {$Hhl-$H2},[x0] @ store Htable[1..2]
123 .size gcm_init_v8,.-gcm_init_v8
125 ################################################################################
126 # void gcm_gmult_v8(u64 Xi[2],const u128 Htable[16]);
128 # input: Xi - current hash value;
129 # Htable - table precomputed in gcm_init_v8;
130 # output: Xi - next hash value Xi;
134 .type gcm_gmult_v8,%function
137 vld1.64 {$t1},[$Xi] @ load Xi
139 vld1.64 {$H-$Hhl},[$Htbl] @ load twisted H, ...
140 vshl.u64 $xC2,$xC2,#57
144 vext.8 $IN,$t1,$t1,#8
146 vpmull.p64 $Xl,$H,$IN @ H.lo·Xi.lo
147 veor $t1,$t1,$IN @ Karatsuba pre-processing
148 vpmull2.p64 $Xh,$H,$IN @ H.hi·Xi.hi
149 vpmull.p64 $Xm,$Hhl,$t1 @ (H.lo+H.hi)·(Xi.lo+Xi.hi)
151 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
155 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
157 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
158 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
161 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
162 vpmull.p64 $Xl,$Xl,$xC2
169 vext.8 $Xl,$Xl,$Xl,#8
170 vst1.64 {$Xl},[$Xi] @ write out Xi
173 .size gcm_gmult_v8,.-gcm_gmult_v8
175 ################################################################################
176 # void gcm_ghash_v8(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
178 # input: table precomputed in gcm_init_v8;
179 # current hash value Xi;
180 # pointer to input data;
181 # length of input data in bytes, but divisible by block size;
182 # output: next hash value Xi;
186 .type gcm_ghash_v8,%function
190 $code.=<<___ if ($flavour !~ /64/);
191 vstmdb sp!,{d8-d15} @ 32-bit ABI says so
194 vld1.64 {$Xl},[$Xi] @ load [rotated] Xi
195 @ "[rotated]" means that
196 @ loaded value would have
197 @ to be rotated in order to
198 @ make it appear as in
199 @ alorithm specification
200 subs $len,$len,#32 @ see if $len is 32 or larger
201 mov $inc,#16 @ $inc is used as post-
202 @ increment for input pointer;
203 @ as loop is modulo-scheduled
204 @ $inc is zeroed just in time
205 @ to preclude oversteping
206 @ inp[len], which means that
207 @ last block[s] are actually
208 @ loaded twice, but last
209 @ copy is not processed
210 vld1.64 {$H-$Hhl},[$Htbl],#32 @ load twisted H, ..., H^2
212 vld1.64 {$H2},[$Htbl]
213 cclr $inc,eq @ is it time to zero $inc?
214 vext.8 $Xl,$Xl,$Xl,#8 @ rotate Xi
215 vld1.64 {$t0},[$inp],#16 @ load [rotated] I[0]
216 vshl.u64 $xC2,$xC2,#57 @ compose 0xc2.0 constant
221 vext.8 $IN,$t0,$t0,#8 @ rotate I[0]
222 b.lo .Lodd_tail_v8 @ $len was less than 32
224 { my ($Xln,$Xmn,$Xhn,$In) = map("q$_",(4..7));
226 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
227 # [(H*Ii+1) + (H*Xi+1)] mod P =
228 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
231 vld1.64 {$t1},[$inp],$inc @ load [rotated] I[1]
235 vext.8 $In,$t1,$t1,#8
236 veor $IN,$IN,$Xl @ I[i]^=Xi
237 vpmull.p64 $Xln,$H,$In @ H·Ii+1
238 veor $t1,$t1,$In @ Karatsuba pre-processing
239 vpmull2.p64 $Xhn,$H,$In
244 vext.8 $t2,$IN,$IN,#8
245 subs $len,$len,#32 @ is there more data?
246 vpmull.p64 $Xl,$H2,$IN @ H^2.lo·Xi.lo
247 cclr $inc,lo @ is it time to zero $inc?
249 vpmull.p64 $Xmn,$Hhl,$t1
250 veor $t2,$t2,$IN @ Karatsuba pre-processing
251 vpmull2.p64 $Xh,$H2,$IN @ H^2.hi·Xi.hi
252 veor $Xl,$Xl,$Xln @ accumulate
253 vpmull2.p64 $Xm,$Hhl,$t2 @ (H^2.lo+H^2.hi)·(Xi.lo+Xi.hi)
254 vld1.64 {$t0},[$inp],$inc @ load [rotated] I[i+2]
257 cclr $inc,eq @ is it time to zero $inc?
260 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
263 vld1.64 {$t1},[$inp],$inc @ load [rotated] I[i+3]
268 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
273 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
274 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
275 vext.8 $In,$t1,$t1,#8
276 vext.8 $IN,$t0,$t0,#8
278 vpmull.p64 $Xln,$H,$In @ H·Ii+1
279 veor $IN,$IN,$Xh @ accumulate $IN early
281 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
282 vpmull.p64 $Xl,$Xl,$xC2
284 veor $t1,$t1,$In @ Karatsuba pre-processing
286 vpmull2.p64 $Xhn,$H,$In
287 b.hs .Loop_mod2x_v8 @ there was at least 32 more bytes
290 vext.8 $IN,$t0,$t0,#8 @ re-construct $IN
291 adds $len,$len,#32 @ re-construct $len
292 veor $Xl,$Xl,$Xh @ re-construct $Xl
293 b.eq .Ldone_v8 @ is $len zero?
298 vext.8 $t2,$Xl,$Xl,#8
299 veor $IN,$IN,$Xl @ inp^=Xi
300 veor $t1,$t0,$t2 @ $t1 is rotated inp^Xi
302 vpmull.p64 $Xl,$H,$IN @ H.lo·Xi.lo
303 veor $t1,$t1,$IN @ Karatsuba pre-processing
304 vpmull2.p64 $Xh,$H,$IN @ H.hi·Xi.hi
305 vpmull.p64 $Xm,$Hhl,$t1 @ (H.lo+H.hi)·(Xi.lo+Xi.hi)
307 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
311 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
313 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
314 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
317 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
318 vpmull.p64 $Xl,$Xl,$xC2
326 vext.8 $Xl,$Xl,$Xl,#8
327 vst1.64 {$Xl},[$Xi] @ write out Xi
330 $code.=<<___ if ($flavour !~ /64/);
331 vldmia sp!,{d8-d15} @ 32-bit ABI says so
335 .size gcm_ghash_v8,.-gcm_ghash_v8
339 .asciz "GHASH for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
343 if ($flavour =~ /64/) { ######## 64-bit code
347 $arg =~ m/q([0-9]+)#(lo|hi),\s*q([0-9]+)#(lo|hi)/o &&
348 sprintf "ins v%d.d[%d],v%d.d[%d]",$1,($2 eq "lo")?0:1,$3,($4 eq "lo")?0:1;
350 foreach(split("\n",$code)) {
351 s/cclr\s+([wx])([^,]+),\s*([a-z]+)/csel $1$2,$1zr,$1$2,$3/o or
352 s/vmov\.i8/movi/o or # fix up legacy mnemonics
353 s/vmov\s+(.*)/unvmov($1)/geo or
355 s/vshr\.s/sshr\.s/o or
357 s/^(\s+)v/$1/o or # strip off v prefix
360 s/\bq([0-9]+)\b/"v".($1<8?$1:$1+8).".16b"/geo; # old->new registers
361 s/@\s/\/\//o; # old->new style commentary
363 # fix up remainig legacy suffixes
365 s/\.[uis]?32//o and s/\.16b/\.4s/go;
366 m/\.p64/o and s/\.16b/\.1q/o; # 1st pmull argument
367 m/l\.p64/o and s/\.16b/\.1d/go; # 2nd and 3rd pmull arguments
368 s/\.[uisp]?64//o and s/\.16b/\.2d/go;
369 s/\.[42]([sd])\[([0-3])\]/\.$1\[$2\]/o;
373 } else { ######## 32-bit code
377 $arg =~ m/q([0-9]+),\s*q([0-9]+)\[([0-3])\]/o &&
378 sprintf "vdup.32 q%d,d%d[%d]",$1,2*$2+($3>>1),$3&1;
381 my ($mnemonic,$arg)=@_;
383 if ($arg =~ m/q([0-9]+),\s*q([0-9]+),\s*q([0-9]+)/o) {
384 my $word = 0xf2a00e00|(($1&7)<<13)|(($1&8)<<19)
385 |(($2&7)<<17)|(($2&8)<<4)
386 |(($3&7)<<1) |(($3&8)<<2);
387 $word |= 0x00010001 if ($mnemonic =~ "2");
388 # since ARMv7 instructions are always encoded little-endian.
389 # correct solution is to use .inst directive, but older
390 # assemblers don't implement it:-(
391 sprintf ".byte\t0x%02x,0x%02x,0x%02x,0x%02x\t@ %s %s",
392 $word&0xff,($word>>8)&0xff,
393 ($word>>16)&0xff,($word>>24)&0xff,
398 foreach(split("\n",$code)) {
399 s/\b[wx]([0-9]+)\b/r$1/go; # new->old registers
400 s/\bv([0-9])\.[12468]+[bsd]\b/q$1/go; # new->old registers
401 s/\/\/\s?/@ /o; # new->old style commentary
403 # fix up remainig new-style suffixes
406 s/cclr\s+([^,]+),\s*([a-z]+)/mov$2 $1,#0/o or
407 s/vdup\.32\s+(.*)/unvdup32($1)/geo or
408 s/v?(pmull2?)\.p64\s+(.*)/unvpmullp64($1,$2)/geo or
409 s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo or
411 s/^(\s+)ret/$1bx\tlr/o;
417 close STDOUT; # enforce flush