Debian Codegraph @

external/boringssl/crypto/modes/asm/ghashv8-armx.pl

raw
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <[email protected]> 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 <[email protected]> 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 # Denver 0.71 6.02
31 #
32 # (*) presented for reference/comparison purposes;
33
34 $flavour = shift;
35 $output = shift;
36
37 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
38 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
39 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
40 die "can't locate arm-xlate.pl";
41
42 open OUT,"| \"$^X\" $xlate $flavour $output";
43 *STDOUT=*OUT;
44
45 $Xi="x0"; # argument block
46 $Htbl="x1";
47 $inp="x2";
48 $len="x3";
49
50 $inc="x12";
51
52 {
53 my ($Xl,$Xm,$Xh,$IN)=map("q$_",(0..3));
54 my ($t0,$t1,$t2,$xC2,$H,$Hhl,$H2)=map("q$_",(8..14));
55
56 $code=<<___;
57 #include <openssl/arm_arch.h>
58
59 .text
60 ___
61 $code.=<<___ if ($flavour =~ /64/);
62 .arch armv8-a+crypto
63 ___
64 $code.=".fpu neon\n.code 32\n" if ($flavour !~ /64/);
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
98 @ calculate H^2
99 vext.8 $t0,$H,$H,#8 @ Karatsuba pre-processing
100 vpmull.p64 $Xl,$H,$H
101 veor $t0,$t0,$H
102 vpmull2.p64 $Xh,$H,$H
103 vpmull.p64 $Xm,$t0,$t0
104
105 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
106 veor $t2,$Xl,$Xh
107 veor $Xm,$Xm,$t1
108 veor $Xm,$Xm,$t2
109 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase
110
111 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
112 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
113 veor $Xl,$Xm,$t2
114
115 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase
116 vpmull.p64 $Xl,$Xl,$xC2
117 veor $t2,$t2,$Xh
118 veor $H2,$Xl,$t2
119
120 vext.8 $t1,$H2,$H2,#8 @ Karatsuba pre-processing
121 veor $t1,$t1,$H2
122 vext.8 $Hhl,$t0,$t1,#8 @ pack Karatsuba pre-processed
123 vst1.64 {$Hhl-$H2},[x0] @ store Htable[1..2]
124
125 ret
126 .size gcm_init_v8,.-gcm_init_v8
127 ___
128 ################################################################################
129 # void gcm_gmult_v8(u64 Xi[2],const u128 Htable[16]);
130 #
131 # input: Xi - current hash value;
132 # Htable - table precomputed in gcm_init_v8;
133 # output: Xi - next hash value Xi;
134 #
135 $code.=<<___;
136 .global gcm_gmult_v8
137 .type gcm_gmult_v8,%function
138 .align 4
139 gcm_gmult_v8:
140 vld1.64 {$t1},[$Xi] @ load Xi
141 vmov.i8 $xC2,#0xe1
142 vld1.64 {$H-$Hhl},[$Htbl] @ load twisted H, ...
143 vshl.u64 $xC2,$xC2,#57
144 #ifndef __ARMEB__
145 vrev64.8 $t1,$t1
146 #endif
147 vext.8 $IN,$t1,$t1,#8
148
149 vpmull.p64 $Xl,$H,$IN @ H.lo·Xi.lo
150 veor $t1,$t1,$IN @ Karatsuba pre-processing
151 vpmull2.p64 $Xh,$H,$IN @ H.hi·Xi.hi
152 vpmull.p64 $Xm,$Hhl,$t1 @ (H.lo+H.hi)·(Xi.lo+Xi.hi)
153
154 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
155 veor $t2,$Xl,$Xh
156 veor $Xm,$Xm,$t1
157 veor $Xm,$Xm,$t2
158 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
159
160 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
161 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
162 veor $Xl,$Xm,$t2
163
164 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
165 vpmull.p64 $Xl,$Xl,$xC2
166 veor $t2,$t2,$Xh
167 veor $Xl,$Xl,$t2
168
169 #ifndef __ARMEB__
170 vrev64.8 $Xl,$Xl
171 #endif
172 vext.8 $Xl,$Xl,$Xl,#8
173 vst1.64 {$Xl},[$Xi] @ write out Xi
174
175 ret
176 .size gcm_gmult_v8,.-gcm_gmult_v8
177 ___
178 ################################################################################
179 # void gcm_ghash_v8(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
180 #
181 # input: table precomputed in gcm_init_v8;
182 # current hash value Xi;
183 # pointer to input data;
184 # length of input data in bytes, but divisible by block size;
185 # output: next hash value Xi;
186 #
187 $code.=<<___;
188 .global gcm_ghash_v8
189 .type gcm_ghash_v8,%function
190 .align 4
191 gcm_ghash_v8:
192 ___
193 $code.=<<___ if ($flavour !~ /64/);
194 vstmdb sp!,{d8-d15} @ 32-bit ABI says so
195 ___
196 $code.=<<___;
197 vld1.64 {$Xl},[$Xi] @ load [rotated] Xi
198 @ "[rotated]" means that
199 @ loaded value would have
200 @ to be rotated in order to
201 @ make it appear as in
202 @ alorithm specification
203 subs $len,$len,#32 @ see if $len is 32 or larger
204 mov $inc,#16 @ $inc is used as post-
205 @ increment for input pointer;
206 @ as loop is modulo-scheduled
207 @ $inc is zeroed just in time
208 @ to preclude oversteping
209 @ inp[len], which means that
210 @ last block[s] are actually
211 @ loaded twice, but last
212 @ copy is not processed
213 vld1.64 {$H-$Hhl},[$Htbl],#32 @ load twisted H, ..., H^2
214 vmov.i8 $xC2,#0xe1
215 vld1.64 {$H2},[$Htbl]
216 cclr $inc,eq @ is it time to zero $inc?
217 vext.8 $Xl,$Xl,$Xl,#8 @ rotate Xi
218 vld1.64 {$t0},[$inp],#16 @ load [rotated] I[0]
219 vshl.u64 $xC2,$xC2,#57 @ compose 0xc2.0 constant
220 #ifndef __ARMEB__
221 vrev64.8 $t0,$t0
222 vrev64.8 $Xl,$Xl
223 #endif
224 vext.8 $IN,$t0,$t0,#8 @ rotate I[0]
225 b.lo .Lodd_tail_v8 @ $len was less than 32
226 ___
227 { my ($Xln,$Xmn,$Xhn,$In) = map("q$_",(4..7));
228 #######
229 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
230 # [(H*Ii+1) + (H*Xi+1)] mod P =
231 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
232 #
233 $code.=<<___;
234 vld1.64 {$t1},[$inp],$inc @ load [rotated] I[1]
235 #ifndef __ARMEB__
236 vrev64.8 $t1,$t1
237 #endif
238 vext.8 $In,$t1,$t1,#8
239 veor $IN,$IN,$Xl @ I[i]^=Xi
240 vpmull.p64 $Xln,$H,$In @ H·Ii+1
241 veor $t1,$t1,$In @ Karatsuba pre-processing
242 vpmull2.p64 $Xhn,$H,$In
243 b .Loop_mod2x_v8
244
245 .align 4
246 .Loop_mod2x_v8:
247 vext.8 $t2,$IN,$IN,#8
248 subs $len,$len,#32 @ is there more data?
249 vpmull.p64 $Xl,$H2,$IN @ H^2.lo·Xi.lo
250 cclr $inc,lo @ is it time to zero $inc?
251
252 vpmull.p64 $Xmn,$Hhl,$t1
253 veor $t2,$t2,$IN @ Karatsuba pre-processing
254 vpmull2.p64 $Xh,$H2,$IN @ H^2.hi·Xi.hi
255 veor $Xl,$Xl,$Xln @ accumulate
256 vpmull2.p64 $Xm,$Hhl,$t2 @ (H^2.lo+H^2.hi)·(Xi.lo+Xi.hi)
257 vld1.64 {$t0},[$inp],$inc @ load [rotated] I[i+2]
258
259 veor $Xh,$Xh,$Xhn
260 cclr $inc,eq @ is it time to zero $inc?
261 veor $Xm,$Xm,$Xmn
262
263 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
264 veor $t2,$Xl,$Xh
265 veor $Xm,$Xm,$t1
266 vld1.64 {$t1},[$inp],$inc @ load [rotated] I[i+3]
267 #ifndef __ARMEB__
268 vrev64.8 $t0,$t0
269 #endif
270 veor $Xm,$Xm,$t2
271 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
272
273 #ifndef __ARMEB__
274 vrev64.8 $t1,$t1
275 #endif
276 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
277 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
278 vext.8 $In,$t1,$t1,#8
279 vext.8 $IN,$t0,$t0,#8
280 veor $Xl,$Xm,$t2
281 vpmull.p64 $Xln,$H,$In @ H·Ii+1
282 veor $IN,$IN,$Xh @ accumulate $IN early
283
284 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
285 vpmull.p64 $Xl,$Xl,$xC2
286 veor $IN,$IN,$t2
287 veor $t1,$t1,$In @ Karatsuba pre-processing
288 veor $IN,$IN,$Xl
289 vpmull2.p64 $Xhn,$H,$In
290 b.hs .Loop_mod2x_v8 @ there was at least 32 more bytes
291
292 veor $Xh,$Xh,$t2
293 vext.8 $IN,$t0,$t0,#8 @ re-construct $IN
294 adds $len,$len,#32 @ re-construct $len
295 veor $Xl,$Xl,$Xh @ re-construct $Xl
296 b.eq .Ldone_v8 @ is $len zero?
297 ___
298 }
299 $code.=<<___;
300 .Lodd_tail_v8:
301 vext.8 $t2,$Xl,$Xl,#8
302 veor $IN,$IN,$Xl @ inp^=Xi
303 veor $t1,$t0,$t2 @ $t1 is rotated inp^Xi
304
305 vpmull.p64 $Xl,$H,$IN @ H.lo·Xi.lo
306 veor $t1,$t1,$IN @ Karatsuba pre-processing
307 vpmull2.p64 $Xh,$H,$IN @ H.hi·Xi.hi
308 vpmull.p64 $Xm,$Hhl,$t1 @ (H.lo+H.hi)·(Xi.lo+Xi.hi)
309
310 vext.8 $t1,$Xl,$Xh,#8 @ Karatsuba post-processing
311 veor $t2,$Xl,$Xh
312 veor $Xm,$Xm,$t1
313 veor $Xm,$Xm,$t2
314 vpmull.p64 $t2,$Xl,$xC2 @ 1st phase of reduction
315
316 vmov $Xh#lo,$Xm#hi @ Xh|Xm - 256-bit result
317 vmov $Xm#hi,$Xl#lo @ Xm is rotated Xl
318 veor $Xl,$Xm,$t2
319
320 vext.8 $t2,$Xl,$Xl,#8 @ 2nd phase of reduction
321 vpmull.p64 $Xl,$Xl,$xC2
322 veor $t2,$t2,$Xh
323 veor $Xl,$Xl,$t2
324
325 .Ldone_v8:
326 #ifndef __ARMEB__
327 vrev64.8 $Xl,$Xl
328 #endif
329 vext.8 $Xl,$Xl,$Xl,#8
330 vst1.64 {$Xl},[$Xi] @ write out Xi
331
332 ___
333 $code.=<<___ if ($flavour !~ /64/);
334 vldmia sp!,{d8-d15} @ 32-bit ABI says so
335 ___
336 $code.=<<___;
337 ret
338 .size gcm_ghash_v8,.-gcm_ghash_v8
339 ___
340 }
341 $code.=<<___;
342 .asciz "GHASH for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
343 .align 2
344 ___
345
346 if ($flavour =~ /64/) { ######## 64-bit code
347 sub unvmov {
348 my $arg=shift;
349
350 $arg =~ m/q([0-9]+)#(lo|hi),\s*q([0-9]+)#(lo|hi)/o &&
351 sprintf "ins v%d.d[%d],v%d.d[%d]",$1,($2 eq "lo")?0:1,$3,($4 eq "lo")?0:1;
352 }
353 foreach(split("\n",$code)) {
354 s/cclr\s+([wx])([^,]+),\s*([a-z]+)/csel $1$2,$1zr,$1$2,$3/o or
355 s/vmov\.i8/movi/o or # fix up legacy mnemonics
356 s/vmov\s+(.*)/unvmov($1)/geo or
357 s/vext\.8/ext/o or
358 s/vshr\.s/sshr\.s/o or
359 s/vshr/ushr/o or
360 s/^(\s+)v/$1/o or # strip off v prefix
361 s/\bbx\s+lr\b/ret/o;
362
363 s/\bq([0-9]+)\b/"v".($1<8?$1:$1+8).".16b"/geo; # old->new registers
364 s/@\s/\/\//o; # old->new style commentary
365
366 # fix up remainig legacy suffixes
367 s/\.[ui]?8(\s)/$1/o;
368 s/\.[uis]?32//o and s/\.16b/\.4s/go;
369 m/\.p64/o and s/\.16b/\.1q/o; # 1st pmull argument
370 m/l\.p64/o and s/\.16b/\.1d/go; # 2nd and 3rd pmull arguments
371 s/\.[uisp]?64//o and s/\.16b/\.2d/go;
372 s/\.[42]([sd])\[([0-3])\]/\.$1\[$2\]/o;
373
374 print $_,"\n";
375 }
376 } else { ######## 32-bit code
377 sub unvdup32 {
378 my $arg=shift;
379
380 $arg =~ m/q([0-9]+),\s*q([0-9]+)\[([0-3])\]/o &&
381 sprintf "vdup.32 q%d,d%d[%d]",$1,2*$2+($3>>1),$3&1;
382 }
383 sub unvpmullp64 {
384 my ($mnemonic,$arg)=@_;
385
386 if ($arg =~ m/q([0-9]+),\s*q([0-9]+),\s*q([0-9]+)/o) {
387 my $word = 0xf2a00e00|(($1&7)<<13)|(($1&8)<<19)
388 |(($2&7)<<17)|(($2&8)<<4)
389 |(($3&7)<<1) |(($3&8)<<2);
390 $word |= 0x00010001 if ($mnemonic =~ "2");
391 # since ARMv7 instructions are always encoded little-endian.
392 # correct solution is to use .inst directive, but older
393 # assemblers don't implement it:-(
394 sprintf ".byte\t0x%02x,0x%02x,0x%02x,0x%02x\t@ %s %s",
395 $word&0xff,($word>>8)&0xff,
396 ($word>>16)&0xff,($word>>24)&0xff,
397 $mnemonic,$arg;
398 }
399 }
400
401 foreach(split("\n",$code)) {
402 s/\b[wx]([0-9]+)\b/r$1/go; # new->old registers
403 s/\bv([0-9])\.[12468]+[bsd]\b/q$1/go; # new->old registers
404 s/\/\/\s?/@ /o; # new->old style commentary
405
406 # fix up remainig new-style suffixes
407 s/\],#[0-9]+/]!/o;
408
409 s/cclr\s+([^,]+),\s*([a-z]+)/mov$2 $1,#0/o or
410 s/vdup\.32\s+(.*)/unvdup32($1)/geo or
411 s/v?(pmull2?)\.p64\s+(.*)/unvpmullp64($1,$2)/geo or
412 s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo or
413 s/^(\s+)b\./$1b/o or
414 s/^(\s+)ret/$1bx\tlr/o;
415
416 print $_,"\n";
417 }
418 }
419
420 close STDOUT; # enforce flush