Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/aes/asm/bsaes-armv7.pl

raw
1 #! /usr/bin/env perl
2 # Copyright 2012-2025 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (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 # ====================================================================
11 # Written by Andy Polyakov, @dot-asm, initially for use in the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see https://github.com/dot-asm/cryptogams/.
15 #
16 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
17 # of Linaro.
18 # ====================================================================
19
20 # Bit-sliced AES for ARM NEON
21 #
22 # February 2012.
23 #
24 # This implementation is direct adaptation of bsaes-x86_64 module for
25 # ARM NEON. Except that this module is endian-neutral [in sense that
26 # it can be compiled for either endianness] by courtesy of vld1.8's
27 # neutrality. Initial version doesn't implement interface to OpenSSL,
28 # only low-level primitives and unsupported entry points, just enough
29 # to collect performance results, which for Cortex-A8 core are:
30 #
31 # encrypt 19.5 cycles per byte processed with 128-bit key
32 # decrypt 22.1 cycles per byte processed with 128-bit key
33 # key conv. 440 cycles per 128-bit key/0.18 of 8x block
34 #
35 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
36 # which is [much] worse than anticipated
37 #
38 # Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
39 # manages in 20.0 cycles].
40 #
41 # When comparing to x86_64 results keep in mind that NEON unit is
42 # [mostly] single-issue and thus can't [fully] benefit from
43 # instruction-level parallelism. And when comparing to aes-armv4
44 # results keep in mind key schedule conversion overhead (see
45 # bsaes-x86_64.pl for further details)...
46 #
47 # <https://github.com/dot-asm>
48
49 # April-August 2013
50 # Add CBC, CTR and XTS subroutines and adapt for kernel use; courtesy of Ard.
51
52 # $output is the last argument if it looks like a file (it has an extension)
53 # $flavour is the first argument if it doesn't look like a file
54 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
55 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
56
57 if ($flavour && $flavour ne "void") {
58 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
59 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
60 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
61 die "can't locate arm-xlate.pl";
62
63 open STDOUT,"| \"$^X\" $xlate $flavour \"$output\""
64 or die "can't call $xlate: $!";
65 } else {
66 $output and open STDOUT,">$output";
67 }
68
69 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
70 my @XMM=map("q$_",(0..15));
71
72 {
73 my ($key,$rounds,$const)=("r4","r5","r6");
74
75 sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
76 sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
77
78 sub Sbox {
79 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
80 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
81 my @b=@_[0..7];
82 my @t=@_[8..11];
83 my @s=@_[12..15];
84 &InBasisChange (@b);
85 &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
86 &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
87 }
88
89 sub InBasisChange {
90 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
91 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
92 my @b=@_[0..7];
93 $code.=<<___;
94 veor @b[2], @b[2], @b[1]
95 veor @b[5], @b[5], @b[6]
96 veor @b[3], @b[3], @b[0]
97 veor @b[6], @b[6], @b[2]
98 veor @b[5], @b[5], @b[0]
99
100 veor @b[6], @b[6], @b[3]
101 veor @b[3], @b[3], @b[7]
102 veor @b[7], @b[7], @b[5]
103 veor @b[3], @b[3], @b[4]
104 veor @b[4], @b[4], @b[5]
105
106 veor @b[2], @b[2], @b[7]
107 veor @b[3], @b[3], @b[1]
108 veor @b[1], @b[1], @b[5]
109 ___
110 }
111
112 sub OutBasisChange {
113 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
114 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
115 my @b=@_[0..7];
116 $code.=<<___;
117 veor @b[0], @b[0], @b[6]
118 veor @b[1], @b[1], @b[4]
119 veor @b[4], @b[4], @b[6]
120 veor @b[2], @b[2], @b[0]
121 veor @b[6], @b[6], @b[1]
122
123 veor @b[1], @b[1], @b[5]
124 veor @b[5], @b[5], @b[3]
125 veor @b[3], @b[3], @b[7]
126 veor @b[7], @b[7], @b[5]
127 veor @b[2], @b[2], @b[5]
128
129 veor @b[4], @b[4], @b[7]
130 ___
131 }
132
133 sub InvSbox {
134 # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
135 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
136 my @b=@_[0..7];
137 my @t=@_[8..11];
138 my @s=@_[12..15];
139 &InvInBasisChange (@b);
140 &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
141 &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
142 }
143
144 sub InvInBasisChange { # OutBasisChange in reverse (with twist)
145 my @b=@_[5,1,2,6,3,7,0,4];
146 $code.=<<___
147 veor @b[1], @b[1], @b[7]
148 veor @b[4], @b[4], @b[7]
149
150 veor @b[7], @b[7], @b[5]
151 veor @b[1], @b[1], @b[3]
152 veor @b[2], @b[2], @b[5]
153 veor @b[3], @b[3], @b[7]
154
155 veor @b[6], @b[6], @b[1]
156 veor @b[2], @b[2], @b[0]
157 veor @b[5], @b[5], @b[3]
158 veor @b[4], @b[4], @b[6]
159 veor @b[0], @b[0], @b[6]
160 veor @b[1], @b[1], @b[4]
161 ___
162 }
163
164 sub InvOutBasisChange { # InBasisChange in reverse
165 my @b=@_[2,5,7,3,6,1,0,4];
166 $code.=<<___;
167 veor @b[1], @b[1], @b[5]
168 veor @b[2], @b[2], @b[7]
169
170 veor @b[3], @b[3], @b[1]
171 veor @b[4], @b[4], @b[5]
172 veor @b[7], @b[7], @b[5]
173 veor @b[3], @b[3], @b[4]
174 veor @b[5], @b[5], @b[0]
175 veor @b[3], @b[3], @b[7]
176 veor @b[6], @b[6], @b[2]
177 veor @b[2], @b[2], @b[1]
178 veor @b[6], @b[6], @b[3]
179
180 veor @b[3], @b[3], @b[0]
181 veor @b[5], @b[5], @b[6]
182 ___
183 }
184
185 sub Mul_GF4 {
186 #;*************************************************************
187 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
188 #;*************************************************************
189 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
190 $code.=<<___;
191 veor $t0, $y0, $y1
192 vand $t0, $t0, $x0
193 veor $x0, $x0, $x1
194 vand $t1, $x1, $y0
195 vand $x0, $x0, $y1
196 veor $x1, $t1, $t0
197 veor $x0, $x0, $t1
198 ___
199 }
200
201 sub Mul_GF4_N { # not used, see next subroutine
202 # multiply and scale by N
203 my ($x0,$x1,$y0,$y1,$t0)=@_;
204 $code.=<<___;
205 veor $t0, $y0, $y1
206 vand $t0, $t0, $x0
207 veor $x0, $x0, $x1
208 vand $x1, $x1, $y0
209 vand $x0, $x0, $y1
210 veor $x1, $x1, $x0
211 veor $x0, $x0, $t0
212 ___
213 }
214
215 sub Mul_GF4_N_GF4 {
216 # interleaved Mul_GF4_N and Mul_GF4
217 my ($x0,$x1,$y0,$y1,$t0,
218 $x2,$x3,$y2,$y3,$t1)=@_;
219 $code.=<<___;
220 veor $t0, $y0, $y1
221 veor $t1, $y2, $y3
222 vand $t0, $t0, $x0
223 vand $t1, $t1, $x2
224 veor $x0, $x0, $x1
225 veor $x2, $x2, $x3
226 vand $x1, $x1, $y0
227 vand $x3, $x3, $y2
228 vand $x0, $x0, $y1
229 vand $x2, $x2, $y3
230 veor $x1, $x1, $x0
231 veor $x2, $x2, $x3
232 veor $x0, $x0, $t0
233 veor $x3, $x3, $t1
234 ___
235 }
236 sub Mul_GF16_2 {
237 my @x=@_[0..7];
238 my @y=@_[8..11];
239 my @t=@_[12..15];
240 $code.=<<___;
241 veor @t[0], @x[0], @x[2]
242 veor @t[1], @x[1], @x[3]
243 ___
244 &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
245 $code.=<<___;
246 veor @y[0], @y[0], @y[2]
247 veor @y[1], @y[1], @y[3]
248 ___
249 Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
250 @x[2], @x[3], @y[2], @y[3], @t[2]);
251 $code.=<<___;
252 veor @x[0], @x[0], @t[0]
253 veor @x[2], @x[2], @t[0]
254 veor @x[1], @x[1], @t[1]
255 veor @x[3], @x[3], @t[1]
256
257 veor @t[0], @x[4], @x[6]
258 veor @t[1], @x[5], @x[7]
259 ___
260 &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
261 @x[6], @x[7], @y[2], @y[3], @t[2]);
262 $code.=<<___;
263 veor @y[0], @y[0], @y[2]
264 veor @y[1], @y[1], @y[3]
265 ___
266 &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
267 $code.=<<___;
268 veor @x[4], @x[4], @t[0]
269 veor @x[6], @x[6], @t[0]
270 veor @x[5], @x[5], @t[1]
271 veor @x[7], @x[7], @t[1]
272 ___
273 }
274 sub Inv_GF256 {
275 #;********************************************************************
276 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
277 #;********************************************************************
278 my @x=@_[0..7];
279 my @t=@_[8..11];
280 my @s=@_[12..15];
281 # direct optimizations from hardware
282 $code.=<<___;
283 veor @t[3], @x[4], @x[6]
284 veor @t[2], @x[5], @x[7]
285 veor @t[1], @x[1], @x[3]
286 veor @s[1], @x[7], @x[6]
287 vmov @t[0], @t[2]
288 veor @s[0], @x[0], @x[2]
289
290 vorr @t[2], @t[2], @t[1]
291 veor @s[3], @t[3], @t[0]
292 vand @s[2], @t[3], @s[0]
293 vorr @t[3], @t[3], @s[0]
294 veor @s[0], @s[0], @t[1]
295 vand @t[0], @t[0], @t[1]
296 veor @t[1], @x[3], @x[2]
297 vand @s[3], @s[3], @s[0]
298 vand @s[1], @s[1], @t[1]
299 veor @t[1], @x[4], @x[5]
300 veor @s[0], @x[1], @x[0]
301 veor @t[3], @t[3], @s[1]
302 veor @t[2], @t[2], @s[1]
303 vand @s[1], @t[1], @s[0]
304 vorr @t[1], @t[1], @s[0]
305 veor @t[3], @t[3], @s[3]
306 veor @t[0], @t[0], @s[1]
307 veor @t[2], @t[2], @s[2]
308 veor @t[1], @t[1], @s[3]
309 veor @t[0], @t[0], @s[2]
310 vand @s[0], @x[7], @x[3]
311 veor @t[1], @t[1], @s[2]
312 vand @s[1], @x[6], @x[2]
313 vand @s[2], @x[5], @x[1]
314 vorr @s[3], @x[4], @x[0]
315 veor @t[3], @t[3], @s[0]
316 veor @t[1], @t[1], @s[2]
317 veor @t[0], @t[0], @s[3]
318 veor @t[2], @t[2], @s[1]
319
320 @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
321
322 @ new smaller inversion
323
324 vand @s[2], @t[3], @t[1]
325 vmov @s[0], @t[0]
326
327 veor @s[1], @t[2], @s[2]
328 veor @s[3], @t[0], @s[2]
329 veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
330
331 vbsl @s[1], @t[1], @t[0]
332 vbsl @s[3], @t[3], @t[2]
333 veor @t[3], @t[3], @t[2]
334
335 vbsl @s[0], @s[1], @s[2]
336 vbsl @t[0], @s[2], @s[1]
337
338 vand @s[2], @s[0], @s[3]
339 veor @t[1], @t[1], @t[0]
340
341 veor @s[2], @s[2], @t[3]
342 ___
343 # output in s3, s2, s1, t1
344
345 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
346
347 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
348 &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
349
350 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
351 }
352
353 # AES linear components
354
355 sub ShiftRows {
356 my @x=@_[0..7];
357 my @t=@_[8..11];
358 my $mask=pop;
359 $code.=<<___;
360 vldmia $key!, {@t[0]-@t[3]}
361 veor @t[0], @t[0], @x[0]
362 veor @t[1], @t[1], @x[1]
363 vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
364 vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
365 vldmia $key!, {@t[0]}
366 veor @t[2], @t[2], @x[2]
367 vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
368 vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
369 vldmia $key!, {@t[1]}
370 veor @t[3], @t[3], @x[3]
371 vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
372 vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
373 vldmia $key!, {@t[2]}
374 vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
375 vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
376 vldmia $key!, {@t[3]}
377 veor @t[0], @t[0], @x[4]
378 veor @t[1], @t[1], @x[5]
379 vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
380 vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
381 veor @t[2], @t[2], @x[6]
382 vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
383 vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
384 veor @t[3], @t[3], @x[7]
385 vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
386 vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
387 vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
388 vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
389 ___
390 }
391
392 sub MixColumns {
393 # modified to emit output in order suitable for feeding back to aesenc[last]
394 my @x=@_[0..7];
395 my @t=@_[8..15];
396 my $inv=@_[16]; # optional
397 $code.=<<___;
398 vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
399 vext.8 @t[1], @x[1], @x[1], #12
400 veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
401 vext.8 @t[2], @x[2], @x[2], #12
402 veor @x[1], @x[1], @t[1]
403 vext.8 @t[3], @x[3], @x[3], #12
404 veor @x[2], @x[2], @t[2]
405 vext.8 @t[4], @x[4], @x[4], #12
406 veor @x[3], @x[3], @t[3]
407 vext.8 @t[5], @x[5], @x[5], #12
408 veor @x[4], @x[4], @t[4]
409 vext.8 @t[6], @x[6], @x[6], #12
410 veor @x[5], @x[5], @t[5]
411 vext.8 @t[7], @x[7], @x[7], #12
412 veor @x[6], @x[6], @t[6]
413
414 veor @t[1], @t[1], @x[0]
415 veor @x[7], @x[7], @t[7]
416 vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
417 veor @t[2], @t[2], @x[1]
418 veor @t[0], @t[0], @x[7]
419 veor @t[1], @t[1], @x[7]
420 vext.8 @x[1], @x[1], @x[1], #8
421 veor @t[5], @t[5], @x[4]
422 veor @x[0], @x[0], @t[0]
423 veor @t[6], @t[6], @x[5]
424 veor @x[1], @x[1], @t[1]
425 vext.8 @t[0], @x[4], @x[4], #8
426 veor @t[4], @t[4], @x[3]
427 vext.8 @t[1], @x[5], @x[5], #8
428 veor @t[7], @t[7], @x[6]
429 vext.8 @x[4], @x[3], @x[3], #8
430 veor @t[3], @t[3], @x[2]
431 vext.8 @x[5], @x[7], @x[7], #8
432 veor @t[4], @t[4], @x[7]
433 vext.8 @x[3], @x[6], @x[6], #8
434 veor @t[3], @t[3], @x[7]
435 vext.8 @x[6], @x[2], @x[2], #8
436 veor @x[7], @t[1], @t[5]
437 ___
438 $code.=<<___ if (!$inv);
439 veor @x[2], @t[0], @t[4]
440 veor @x[4], @x[4], @t[3]
441 veor @x[5], @x[5], @t[7]
442 veor @x[3], @x[3], @t[6]
443 @ vmov @x[2], @t[0]
444 veor @x[6], @x[6], @t[2]
445 @ vmov @x[7], @t[1]
446 ___
447 $code.=<<___ if ($inv);
448 veor @t[3], @t[3], @x[4]
449 veor @x[5], @x[5], @t[7]
450 veor @x[2], @x[3], @t[6]
451 veor @x[3], @t[0], @t[4]
452 veor @x[4], @x[6], @t[2]
453 vmov @x[6], @t[3]
454 @ vmov @x[7], @t[1]
455 ___
456 }
457
458 sub InvMixColumns_orig {
459 my @x=@_[0..7];
460 my @t=@_[8..15];
461
462 $code.=<<___;
463 @ multiplication by 0x0e
464 vext.8 @t[7], @x[7], @x[7], #12
465 vmov @t[2], @x[2]
466 veor @x[2], @x[2], @x[5] @ 2 5
467 veor @x[7], @x[7], @x[5] @ 7 5
468 vext.8 @t[0], @x[0], @x[0], #12
469 vmov @t[5], @x[5]
470 veor @x[5], @x[5], @x[0] @ 5 0 [1]
471 veor @x[0], @x[0], @x[1] @ 0 1
472 vext.8 @t[1], @x[1], @x[1], #12
473 veor @x[1], @x[1], @x[2] @ 1 25
474 veor @x[0], @x[0], @x[6] @ 01 6 [2]
475 vext.8 @t[3], @x[3], @x[3], #12
476 veor @x[1], @x[1], @x[3] @ 125 3 [4]
477 veor @x[2], @x[2], @x[0] @ 25 016 [3]
478 veor @x[3], @x[3], @x[7] @ 3 75
479 veor @x[7], @x[7], @x[6] @ 75 6 [0]
480 vext.8 @t[6], @x[6], @x[6], #12
481 vmov @t[4], @x[4]
482 veor @x[6], @x[6], @x[4] @ 6 4
483 veor @x[4], @x[4], @x[3] @ 4 375 [6]
484 veor @x[3], @x[3], @x[7] @ 375 756=36
485 veor @x[6], @x[6], @t[5] @ 64 5 [7]
486 veor @x[3], @x[3], @t[2] @ 36 2
487 vext.8 @t[5], @t[5], @t[5], #12
488 veor @x[3], @x[3], @t[4] @ 362 4 [5]
489 ___
490 my @y = @x[7,5,0,2,1,3,4,6];
491 $code.=<<___;
492 @ multiplication by 0x0b
493 veor @y[1], @y[1], @y[0]
494 veor @y[0], @y[0], @t[0]
495 vext.8 @t[2], @t[2], @t[2], #12
496 veor @y[1], @y[1], @t[1]
497 veor @y[0], @y[0], @t[5]
498 vext.8 @t[4], @t[4], @t[4], #12
499 veor @y[1], @y[1], @t[6]
500 veor @y[0], @y[0], @t[7]
501 veor @t[7], @t[7], @t[6] @ clobber t[7]
502
503 veor @y[3], @y[3], @t[0]
504 veor @y[1], @y[1], @y[0]
505 vext.8 @t[0], @t[0], @t[0], #12
506 veor @y[2], @y[2], @t[1]
507 veor @y[4], @y[4], @t[1]
508 vext.8 @t[1], @t[1], @t[1], #12
509 veor @y[2], @y[2], @t[2]
510 veor @y[3], @y[3], @t[2]
511 veor @y[5], @y[5], @t[2]
512 veor @y[2], @y[2], @t[7]
513 vext.8 @t[2], @t[2], @t[2], #12
514 veor @y[3], @y[3], @t[3]
515 veor @y[6], @y[6], @t[3]
516 veor @y[4], @y[4], @t[3]
517 veor @y[7], @y[7], @t[4]
518 vext.8 @t[3], @t[3], @t[3], #12
519 veor @y[5], @y[5], @t[4]
520 veor @y[7], @y[7], @t[7]
521 veor @t[7], @t[7], @t[5] @ clobber t[7] even more
522 veor @y[3], @y[3], @t[5]
523 veor @y[4], @y[4], @t[4]
524
525 veor @y[5], @y[5], @t[7]
526 vext.8 @t[4], @t[4], @t[4], #12
527 veor @y[6], @y[6], @t[7]
528 veor @y[4], @y[4], @t[7]
529
530 veor @t[7], @t[7], @t[5]
531 vext.8 @t[5], @t[5], @t[5], #12
532
533 @ multiplication by 0x0d
534 veor @y[4], @y[4], @y[7]
535 veor @t[7], @t[7], @t[6] @ restore t[7]
536 veor @y[7], @y[7], @t[4]
537 vext.8 @t[6], @t[6], @t[6], #12
538 veor @y[2], @y[2], @t[0]
539 veor @y[7], @y[7], @t[5]
540 vext.8 @t[7], @t[7], @t[7], #12
541 veor @y[2], @y[2], @t[2]
542
543 veor @y[3], @y[3], @y[1]
544 veor @y[1], @y[1], @t[1]
545 veor @y[0], @y[0], @t[0]
546 veor @y[3], @y[3], @t[0]
547 veor @y[1], @y[1], @t[5]
548 veor @y[0], @y[0], @t[5]
549 vext.8 @t[0], @t[0], @t[0], #12
550 veor @y[1], @y[1], @t[7]
551 veor @y[0], @y[0], @t[6]
552 veor @y[3], @y[3], @y[1]
553 veor @y[4], @y[4], @t[1]
554 vext.8 @t[1], @t[1], @t[1], #12
555
556 veor @y[7], @y[7], @t[7]
557 veor @y[4], @y[4], @t[2]
558 veor @y[5], @y[5], @t[2]
559 veor @y[2], @y[2], @t[6]
560 veor @t[6], @t[6], @t[3] @ clobber t[6]
561 vext.8 @t[2], @t[2], @t[2], #12
562 veor @y[4], @y[4], @y[7]
563 veor @y[3], @y[3], @t[6]
564
565 veor @y[6], @y[6], @t[6]
566 veor @y[5], @y[5], @t[5]
567 vext.8 @t[5], @t[5], @t[5], #12
568 veor @y[6], @y[6], @t[4]
569 vext.8 @t[4], @t[4], @t[4], #12
570 veor @y[5], @y[5], @t[6]
571 veor @y[6], @y[6], @t[7]
572 vext.8 @t[7], @t[7], @t[7], #12
573 veor @t[6], @t[6], @t[3] @ restore t[6]
574 vext.8 @t[3], @t[3], @t[3], #12
575
576 @ multiplication by 0x09
577 veor @y[4], @y[4], @y[1]
578 veor @t[1], @t[1], @y[1] @ t[1]=y[1]
579 veor @t[0], @t[0], @t[5] @ clobber t[0]
580 vext.8 @t[6], @t[6], @t[6], #12
581 veor @t[1], @t[1], @t[5]
582 veor @y[3], @y[3], @t[0]
583 veor @t[0], @t[0], @y[0] @ t[0]=y[0]
584 veor @t[1], @t[1], @t[6]
585 veor @t[6], @t[6], @t[7] @ clobber t[6]
586 veor @y[4], @y[4], @t[1]
587 veor @y[7], @y[7], @t[4]
588 veor @y[6], @y[6], @t[3]
589 veor @y[5], @y[5], @t[2]
590 veor @t[4], @t[4], @y[4] @ t[4]=y[4]
591 veor @t[3], @t[3], @y[3] @ t[3]=y[3]
592 veor @t[5], @t[5], @y[5] @ t[5]=y[5]
593 veor @t[2], @t[2], @y[2] @ t[2]=y[2]
594 veor @t[3], @t[3], @t[7]
595 veor @XMM[5], @t[5], @t[6]
596 veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
597 veor @XMM[2], @t[2], @t[6]
598 veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
599
600 vmov @XMM[0], @t[0]
601 vmov @XMM[1], @t[1]
602 @ vmov @XMM[2], @t[2]
603 vmov @XMM[3], @t[3]
604 vmov @XMM[4], @t[4]
605 @ vmov @XMM[5], @t[5]
606 @ vmov @XMM[6], @t[6]
607 @ vmov @XMM[7], @t[7]
608 ___
609 }
610
611 sub InvMixColumns {
612 my @x=@_[0..7];
613 my @t=@_[8..15];
614
615 # Thanks to Jussi Kivilinna for providing pointer to
616 #
617 # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
618 # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
619 # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
620 # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
621
622 $code.=<<___;
623 @ multiplication by 0x05-0x00-0x04-0x00
624 vext.8 @t[0], @x[0], @x[0], #8
625 vext.8 @t[6], @x[6], @x[6], #8
626 vext.8 @t[7], @x[7], @x[7], #8
627 veor @t[0], @t[0], @x[0]
628 vext.8 @t[1], @x[1], @x[1], #8
629 veor @t[6], @t[6], @x[6]
630 vext.8 @t[2], @x[2], @x[2], #8
631 veor @t[7], @t[7], @x[7]
632 vext.8 @t[3], @x[3], @x[3], #8
633 veor @t[1], @t[1], @x[1]
634 vext.8 @t[4], @x[4], @x[4], #8
635 veor @t[2], @t[2], @x[2]
636 vext.8 @t[5], @x[5], @x[5], #8
637 veor @t[3], @t[3], @x[3]
638 veor @t[4], @t[4], @x[4]
639 veor @t[5], @t[5], @x[5]
640
641 veor @x[0], @x[0], @t[6]
642 veor @x[1], @x[1], @t[6]
643 veor @x[2], @x[2], @t[0]
644 veor @x[4], @x[4], @t[2]
645 veor @x[3], @x[3], @t[1]
646 veor @x[1], @x[1], @t[7]
647 veor @x[2], @x[2], @t[7]
648 veor @x[4], @x[4], @t[6]
649 veor @x[5], @x[5], @t[3]
650 veor @x[3], @x[3], @t[6]
651 veor @x[6], @x[6], @t[4]
652 veor @x[4], @x[4], @t[7]
653 veor @x[5], @x[5], @t[7]
654 veor @x[7], @x[7], @t[5]
655 ___
656 &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
657 }
658
659 sub swapmove {
660 my ($a,$b,$n,$mask,$t)=@_;
661 $code.=<<___;
662 vshr.u64 $t, $b, #$n
663 veor $t, $t, $a
664 vand $t, $t, $mask
665 veor $a, $a, $t
666 vshl.u64 $t, $t, #$n
667 veor $b, $b, $t
668 ___
669 }
670 sub swapmove2x {
671 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
672 $code.=<<___;
673 vshr.u64 $t0, $b0, #$n
674 vshr.u64 $t1, $b1, #$n
675 veor $t0, $t0, $a0
676 veor $t1, $t1, $a1
677 vand $t0, $t0, $mask
678 vand $t1, $t1, $mask
679 veor $a0, $a0, $t0
680 vshl.u64 $t0, $t0, #$n
681 veor $a1, $a1, $t1
682 vshl.u64 $t1, $t1, #$n
683 veor $b0, $b0, $t0
684 veor $b1, $b1, $t1
685 ___
686 }
687
688 sub bitslice {
689 my @x=reverse(@_[0..7]);
690 my ($t0,$t1,$t2,$t3)=@_[8..11];
691 $code.=<<___;
692 vmov.i8 $t0,#0x55 @ compose .LBS0
693 vmov.i8 $t1,#0x33 @ compose .LBS1
694 ___
695 &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
696 &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
697 $code.=<<___;
698 vmov.i8 $t0,#0x0f @ compose .LBS2
699 ___
700 &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
701 &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
702
703 &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
704 &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
705 }
706
707 $code.=<<___;
708 #ifndef __KERNEL__
709 # include "arm_arch.h"
710
711 # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
712 # define VFP_ABI_POP vldmia sp!,{d8-d15}
713 # define VFP_ABI_FRAME 0x40
714 #else
715 # define VFP_ABI_PUSH
716 # define VFP_ABI_POP
717 # define VFP_ABI_FRAME 0
718 # define BSAES_ASM_EXTENDED_KEY
719 # define XTS_CHAIN_TWEAK
720 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
721 # define __ARM_MAX_ARCH__ 7
722 #endif
723
724 #ifdef __thumb__
725 # define adrl adr
726 #endif
727
728 #if __ARM_MAX_ARCH__>=7
729 .arch armv7-a
730 .fpu neon
731
732 .syntax unified @ ARMv7-capable assembler is expected to handle this
733 #if defined(__thumb2__) && !defined(__APPLE__)
734 .thumb
735 #else
736 .code 32
737 # undef __thumb2__
738 #endif
739
740 .text
741
742 .type _bsaes_decrypt8,%function
743 .align 4
744 _bsaes_decrypt8:
745 adr $const,.
746 vldmia $key!, {@XMM[9]} @ round 0 key
747 #if defined(__thumb2__) || defined(__APPLE__)
748 adr $const,.LM0ISR
749 #else
750 add $const,$const,#.LM0ISR-_bsaes_decrypt8
751 #endif
752
753 vldmia $const!, {@XMM[8]} @ .LM0ISR
754 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
755 veor @XMM[11], @XMM[1], @XMM[9]
756 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
757 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
758 veor @XMM[12], @XMM[2], @XMM[9]
759 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
760 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
761 veor @XMM[13], @XMM[3], @XMM[9]
762 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
763 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
764 veor @XMM[14], @XMM[4], @XMM[9]
765 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
766 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
767 veor @XMM[15], @XMM[5], @XMM[9]
768 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
769 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
770 veor @XMM[10], @XMM[6], @XMM[9]
771 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
772 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
773 veor @XMM[11], @XMM[7], @XMM[9]
774 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
775 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
776 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
777 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
778 ___
779 &bitslice (@XMM[0..7, 8..11]);
780 $code.=<<___;
781 sub $rounds,$rounds,#1
782 b .Ldec_sbox
783 .align 4
784 .Ldec_loop:
785 ___
786 &ShiftRows (@XMM[0..7, 8..12]);
787 $code.=".Ldec_sbox:\n";
788 &InvSbox (@XMM[0..7, 8..15]);
789 $code.=<<___;
790 subs $rounds,$rounds,#1
791 bcc .Ldec_done
792 ___
793 &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
794 $code.=<<___;
795 vldmia $const, {@XMM[12]} @ .LISR
796 ite eq @ Thumb2 thing, sanity check in ARM
797 addeq $const,$const,#0x10
798 bne .Ldec_loop
799 vldmia $const, {@XMM[12]} @ .LISRM0
800 b .Ldec_loop
801 .align 4
802 .Ldec_done:
803 ___
804 &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
805 $code.=<<___;
806 vldmia $key, {@XMM[8]} @ last round key
807 veor @XMM[6], @XMM[6], @XMM[8]
808 veor @XMM[4], @XMM[4], @XMM[8]
809 veor @XMM[2], @XMM[2], @XMM[8]
810 veor @XMM[7], @XMM[7], @XMM[8]
811 veor @XMM[3], @XMM[3], @XMM[8]
812 veor @XMM[5], @XMM[5], @XMM[8]
813 veor @XMM[0], @XMM[0], @XMM[8]
814 veor @XMM[1], @XMM[1], @XMM[8]
815 bx lr
816 .size _bsaes_decrypt8,.-_bsaes_decrypt8
817
818 .type _bsaes_const,%object
819 .align 6
820 _bsaes_const:
821 .LM0ISR: @ InvShiftRows constants
822 .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
823 .LISR:
824 .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
825 .LISRM0:
826 .quad 0x01040b0e0205080f, 0x0306090c00070a0d
827 .LM0SR: @ ShiftRows constants
828 .quad 0x0a0e02060f03070b, 0x0004080c05090d01
829 .LSR:
830 .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
831 .LSRM0:
832 .quad 0x0304090e00050a0f, 0x01060b0c0207080d
833 .LM0:
834 .quad 0x02060a0e03070b0f, 0x0004080c0105090d
835 .LREVM0SR:
836 .quad 0x090d01050c000408, 0x03070b0f060a0e02
837 .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <https://github.com/dot-asm>"
838 .align 6
839 .size _bsaes_const,.-_bsaes_const
840
841 .type _bsaes_encrypt8,%function
842 .align 4
843 _bsaes_encrypt8:
844 adr $const,.
845 vldmia $key!, {@XMM[9]} @ round 0 key
846 #if defined(__thumb2__) || defined(__APPLE__)
847 adr $const,.LM0SR
848 #else
849 sub $const,$const,#_bsaes_encrypt8-.LM0SR
850 #endif
851
852 vldmia $const!, {@XMM[8]} @ .LM0SR
853 _bsaes_encrypt8_alt:
854 veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
855 veor @XMM[11], @XMM[1], @XMM[9]
856 vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
857 vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
858 veor @XMM[12], @XMM[2], @XMM[9]
859 vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
860 vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
861 veor @XMM[13], @XMM[3], @XMM[9]
862 vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
863 vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
864 veor @XMM[14], @XMM[4], @XMM[9]
865 vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
866 vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
867 veor @XMM[15], @XMM[5], @XMM[9]
868 vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
869 vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
870 veor @XMM[10], @XMM[6], @XMM[9]
871 vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
872 vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
873 veor @XMM[11], @XMM[7], @XMM[9]
874 vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
875 vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
876 vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
877 vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
878 _bsaes_encrypt8_bitslice:
879 ___
880 &bitslice (@XMM[0..7, 8..11]);
881 $code.=<<___;
882 sub $rounds,$rounds,#1
883 b .Lenc_sbox
884 .align 4
885 .Lenc_loop:
886 ___
887 &ShiftRows (@XMM[0..7, 8..12]);
888 $code.=".Lenc_sbox:\n";
889 &Sbox (@XMM[0..7, 8..15]);
890 $code.=<<___;
891 subs $rounds,$rounds,#1
892 bcc .Lenc_done
893 ___
894 &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
895 $code.=<<___;
896 vldmia $const, {@XMM[12]} @ .LSR
897 ite eq @ Thumb2 thing, samity check in ARM
898 addeq $const,$const,#0x10
899 bne .Lenc_loop
900 vldmia $const, {@XMM[12]} @ .LSRM0
901 b .Lenc_loop
902 .align 4
903 .Lenc_done:
904 ___
905 # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
906 &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
907 $code.=<<___;
908 vldmia $key, {@XMM[8]} @ last round key
909 veor @XMM[4], @XMM[4], @XMM[8]
910 veor @XMM[6], @XMM[6], @XMM[8]
911 veor @XMM[3], @XMM[3], @XMM[8]
912 veor @XMM[7], @XMM[7], @XMM[8]
913 veor @XMM[2], @XMM[2], @XMM[8]
914 veor @XMM[5], @XMM[5], @XMM[8]
915 veor @XMM[0], @XMM[0], @XMM[8]
916 veor @XMM[1], @XMM[1], @XMM[8]
917 bx lr
918 .size _bsaes_encrypt8,.-_bsaes_encrypt8
919 ___
920 }
921 {
922 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
923
924 sub bitslice_key {
925 my @x=reverse(@_[0..7]);
926 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
927
928 &swapmove (@x[0,1],1,$bs0,$t2,$t3);
929 $code.=<<___;
930 @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
931 vmov @x[2], @x[0]
932 vmov @x[3], @x[1]
933 ___
934 #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
935
936 &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
937 $code.=<<___;
938 @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
939 vmov @x[4], @x[0]
940 vmov @x[6], @x[2]
941 vmov @x[5], @x[1]
942 vmov @x[7], @x[3]
943 ___
944 &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
945 &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
946 }
947
948 $code.=<<___;
949 .type _bsaes_key_convert,%function
950 .align 4
951 _bsaes_key_convert:
952 adr $const,.
953 vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
954 #if defined(__thumb2__) || defined(__APPLE__)
955 adr $const,.LM0
956 #else
957 sub $const,$const,#_bsaes_key_convert-.LM0
958 #endif
959 vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
960
961 vmov.i8 @XMM[8], #0x01 @ bit masks
962 vmov.i8 @XMM[9], #0x02
963 vmov.i8 @XMM[10], #0x04
964 vmov.i8 @XMM[11], #0x08
965 vmov.i8 @XMM[12], #0x10
966 vmov.i8 @XMM[13], #0x20
967 vldmia $const, {@XMM[14]} @ .LM0
968
969 #ifdef __ARMEL__
970 vrev32.8 @XMM[7], @XMM[7]
971 vrev32.8 @XMM[15], @XMM[15]
972 #endif
973 sub $rounds,$rounds,#1
974 vstmia $out!, {@XMM[7]} @ save round 0 key
975 b .Lkey_loop
976
977 .align 4
978 .Lkey_loop:
979 vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
980 vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
981 vmov.i8 @XMM[6], #0x40
982 vmov.i8 @XMM[15], #0x80
983
984 vtst.8 @XMM[0], @XMM[7], @XMM[8]
985 vtst.8 @XMM[1], @XMM[7], @XMM[9]
986 vtst.8 @XMM[2], @XMM[7], @XMM[10]
987 vtst.8 @XMM[3], @XMM[7], @XMM[11]
988 vtst.8 @XMM[4], @XMM[7], @XMM[12]
989 vtst.8 @XMM[5], @XMM[7], @XMM[13]
990 vtst.8 @XMM[6], @XMM[7], @XMM[6]
991 vtst.8 @XMM[7], @XMM[7], @XMM[15]
992 vld1.8 {@XMM[15]}, [$inp]! @ load next round key
993 vmvn @XMM[0], @XMM[0] @ "pnot"
994 vmvn @XMM[1], @XMM[1]
995 vmvn @XMM[5], @XMM[5]
996 vmvn @XMM[6], @XMM[6]
997 #ifdef __ARMEL__
998 vrev32.8 @XMM[15], @XMM[15]
999 #endif
1000 subs $rounds,$rounds,#1
1001 vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
1002 bne .Lkey_loop
1003
1004 vmov.i8 @XMM[7],#0x63 @ compose .L63
1005 @ don't save last round key
1006 bx lr
1007 .size _bsaes_key_convert,.-_bsaes_key_convert
1008 ___
1009 }
1010
1011 if (0) { # following four functions are unsupported interface
1012 # used for benchmarking...
1013 $code.=<<___;
1014 .globl bsaes_enc_key_convert
1015 .type bsaes_enc_key_convert,%function
1016 .align 4
1017 bsaes_enc_key_convert:
1018 stmdb sp!,{r4-r6,lr}
1019 vstmdb sp!,{d8-d15} @ ABI specification says so
1020
1021 ldr r5,[$inp,#240] @ pass rounds
1022 mov r4,$inp @ pass key
1023 mov r12,$out @ pass key schedule
1024 bl _bsaes_key_convert
1025 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1026 vstmia r12, {@XMM[7]} @ save last round key
1027
1028 vldmia sp!,{d8-d15}
1029 ldmia sp!,{r4-r6,pc}
1030 .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
1031
1032 .globl bsaes_encrypt_128
1033 .type bsaes_encrypt_128,%function
1034 .align 4
1035 bsaes_encrypt_128:
1036 stmdb sp!,{r4-r6,lr}
1037 vstmdb sp!,{d8-d15} @ ABI specification says so
1038 .Lenc128_loop:
1039 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1040 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1041 mov r4,$key @ pass the key
1042 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1043 mov r5,#10 @ pass rounds
1044 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1045
1046 bl _bsaes_encrypt8
1047
1048 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1049 vst1.8 {@XMM[4]}, [$out]!
1050 vst1.8 {@XMM[6]}, [$out]!
1051 vst1.8 {@XMM[3]}, [$out]!
1052 vst1.8 {@XMM[7]}, [$out]!
1053 vst1.8 {@XMM[2]}, [$out]!
1054 subs $len,$len,#0x80
1055 vst1.8 {@XMM[5]}, [$out]!
1056 bhi .Lenc128_loop
1057
1058 vldmia sp!,{d8-d15}
1059 ldmia sp!,{r4-r6,pc}
1060 .size bsaes_encrypt_128,.-bsaes_encrypt_128
1061
1062 .globl bsaes_dec_key_convert
1063 .type bsaes_dec_key_convert,%function
1064 .align 4
1065 bsaes_dec_key_convert:
1066 stmdb sp!,{r4-r6,lr}
1067 vstmdb sp!,{d8-d15} @ ABI specification says so
1068
1069 ldr r5,[$inp,#240] @ pass rounds
1070 mov r4,$inp @ pass key
1071 mov r12,$out @ pass key schedule
1072 bl _bsaes_key_convert
1073 vldmia $out, {@XMM[6]}
1074 vstmia r12, {@XMM[15]} @ save last round key
1075 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1076 vstmia $out, {@XMM[7]}
1077
1078 vldmia sp!,{d8-d15}
1079 ldmia sp!,{r4-r6,pc}
1080 .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
1081
1082 .globl bsaes_decrypt_128
1083 .type bsaes_decrypt_128,%function
1084 .align 4
1085 bsaes_decrypt_128:
1086 stmdb sp!,{r4-r6,lr}
1087 vstmdb sp!,{d8-d15} @ ABI specification says so
1088 .Ldec128_loop:
1089 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1090 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1091 mov r4,$key @ pass the key
1092 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1093 mov r5,#10 @ pass rounds
1094 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1095
1096 bl _bsaes_decrypt8
1097
1098 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1099 vst1.8 {@XMM[6]}, [$out]!
1100 vst1.8 {@XMM[4]}, [$out]!
1101 vst1.8 {@XMM[2]}, [$out]!
1102 vst1.8 {@XMM[7]}, [$out]!
1103 vst1.8 {@XMM[3]}, [$out]!
1104 subs $len,$len,#0x80
1105 vst1.8 {@XMM[5]}, [$out]!
1106 bhi .Ldec128_loop
1107
1108 vldmia sp!,{d8-d15}
1109 ldmia sp!,{r4-r6,pc}
1110 .size bsaes_decrypt_128,.-bsaes_decrypt_128
1111 ___
1112 }
1113 {
1114 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1115 my ($keysched)=("sp");
1116
1117 $code.=<<___;
1118 .extern AES_cbc_encrypt
1119 .extern AES_decrypt
1120
1121 .global ossl_bsaes_cbc_encrypt
1122 .type ossl_bsaes_cbc_encrypt,%function
1123 .align 5
1124 ossl_bsaes_cbc_encrypt:
1125 #ifndef __KERNEL__
1126 cmp $len, #128
1127 #ifndef __thumb__
1128 blo AES_cbc_encrypt
1129 #else
1130 bhs .Lcbc_do_bsaes
1131 b AES_cbc_encrypt
1132 .Lcbc_do_bsaes:
1133 #endif
1134 #endif
1135
1136 @ it is up to the caller to make sure we are called with enc == 0
1137
1138 mov ip, sp
1139 stmdb sp!, {r4-r10, lr}
1140 VFP_ABI_PUSH
1141 ldr $ivp, [ip] @ IV is 1st arg on the stack
1142 mov $len, $len, lsr#4 @ len in 16 byte blocks
1143 sub sp, #0x10 @ scratch space to carry over the IV
1144 mov $fp, sp @ save sp
1145
1146 ldr $rounds, [$key, #240] @ get # of rounds
1147 #ifndef BSAES_ASM_EXTENDED_KEY
1148 @ allocate the key schedule on the stack
1149 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1150 add r12, #`128-32` @ sifze of bit-slices key schedule
1151
1152 @ populate the key schedule
1153 mov r4, $key @ pass key
1154 mov r5, $rounds @ pass # of rounds
1155 mov sp, r12 @ sp is $keysched
1156 bl _bsaes_key_convert
1157 vldmia $keysched, {@XMM[6]}
1158 vstmia r12, {@XMM[15]} @ save last round key
1159 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1160 vstmia $keysched, {@XMM[7]}
1161 #else
1162 ldr r12, [$key, #244]
1163 eors r12, #1
1164 beq 0f
1165
1166 @ populate the key schedule
1167 str r12, [$key, #244]
1168 mov r4, $key @ pass key
1169 mov r5, $rounds @ pass # of rounds
1170 add r12, $key, #248 @ pass key schedule
1171 bl _bsaes_key_convert
1172 add r4, $key, #248
1173 vldmia r4, {@XMM[6]}
1174 vstmia r12, {@XMM[15]} @ save last round key
1175 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
1176 vstmia r4, {@XMM[7]}
1177
1178 .align 2
1179 0:
1180 #endif
1181
1182 vld1.8 {@XMM[15]}, [$ivp] @ load IV
1183 b .Lcbc_dec_loop
1184
1185 .align 4
1186 .Lcbc_dec_loop:
1187 subs $len, $len, #0x8
1188 bmi .Lcbc_dec_loop_finish
1189
1190 vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
1191 vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
1192 #ifndef BSAES_ASM_EXTENDED_KEY
1193 mov r4, $keysched @ pass the key
1194 #else
1195 add r4, $key, #248
1196 #endif
1197 vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
1198 mov r5, $rounds
1199 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
1200 sub $inp, $inp, #0x60
1201 vstmia $fp, {@XMM[15]} @ put aside IV
1202
1203 bl _bsaes_decrypt8
1204
1205 vldmia $fp, {@XMM[14]} @ reload IV
1206 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1207 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1208 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1209 veor @XMM[1], @XMM[1], @XMM[8]
1210 veor @XMM[6], @XMM[6], @XMM[9]
1211 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1212 veor @XMM[4], @XMM[4], @XMM[10]
1213 veor @XMM[2], @XMM[2], @XMM[11]
1214 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1215 veor @XMM[7], @XMM[7], @XMM[12]
1216 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1217 veor @XMM[3], @XMM[3], @XMM[13]
1218 vst1.8 {@XMM[6]}, [$out]!
1219 veor @XMM[5], @XMM[5], @XMM[14]
1220 vst1.8 {@XMM[4]}, [$out]!
1221 vst1.8 {@XMM[2]}, [$out]!
1222 vst1.8 {@XMM[7]}, [$out]!
1223 vst1.8 {@XMM[3]}, [$out]!
1224 vst1.8 {@XMM[5]}, [$out]!
1225
1226 b .Lcbc_dec_loop
1227
1228 .Lcbc_dec_loop_finish:
1229 adds $len, $len, #8
1230 beq .Lcbc_dec_done
1231
1232 vld1.8 {@XMM[0]}, [$inp]! @ load input
1233 cmp $len, #2
1234 blo .Lcbc_dec_one
1235 vld1.8 {@XMM[1]}, [$inp]!
1236 #ifndef BSAES_ASM_EXTENDED_KEY
1237 mov r4, $keysched @ pass the key
1238 #else
1239 add r4, $key, #248
1240 #endif
1241 mov r5, $rounds
1242 vstmia $fp, {@XMM[15]} @ put aside IV
1243 beq .Lcbc_dec_two
1244 vld1.8 {@XMM[2]}, [$inp]!
1245 cmp $len, #4
1246 blo .Lcbc_dec_three
1247 vld1.8 {@XMM[3]}, [$inp]!
1248 beq .Lcbc_dec_four
1249 vld1.8 {@XMM[4]}, [$inp]!
1250 cmp $len, #6
1251 blo .Lcbc_dec_five
1252 vld1.8 {@XMM[5]}, [$inp]!
1253 beq .Lcbc_dec_six
1254 vld1.8 {@XMM[6]}, [$inp]!
1255 sub $inp, $inp, #0x70
1256
1257 bl _bsaes_decrypt8
1258
1259 vldmia $fp, {@XMM[14]} @ reload IV
1260 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1261 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1262 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1263 veor @XMM[1], @XMM[1], @XMM[8]
1264 veor @XMM[6], @XMM[6], @XMM[9]
1265 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1266 veor @XMM[4], @XMM[4], @XMM[10]
1267 veor @XMM[2], @XMM[2], @XMM[11]
1268 vld1.8 {@XMM[15]}, [$inp]!
1269 veor @XMM[7], @XMM[7], @XMM[12]
1270 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1271 veor @XMM[3], @XMM[3], @XMM[13]
1272 vst1.8 {@XMM[6]}, [$out]!
1273 vst1.8 {@XMM[4]}, [$out]!
1274 vst1.8 {@XMM[2]}, [$out]!
1275 vst1.8 {@XMM[7]}, [$out]!
1276 vst1.8 {@XMM[3]}, [$out]!
1277 b .Lcbc_dec_done
1278 .align 4
1279 .Lcbc_dec_six:
1280 sub $inp, $inp, #0x60
1281 bl _bsaes_decrypt8
1282 vldmia $fp,{@XMM[14]} @ reload IV
1283 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1284 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1285 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1286 veor @XMM[1], @XMM[1], @XMM[8]
1287 veor @XMM[6], @XMM[6], @XMM[9]
1288 vld1.8 {@XMM[12]}, [$inp]!
1289 veor @XMM[4], @XMM[4], @XMM[10]
1290 veor @XMM[2], @XMM[2], @XMM[11]
1291 vld1.8 {@XMM[15]}, [$inp]!
1292 veor @XMM[7], @XMM[7], @XMM[12]
1293 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1294 vst1.8 {@XMM[6]}, [$out]!
1295 vst1.8 {@XMM[4]}, [$out]!
1296 vst1.8 {@XMM[2]}, [$out]!
1297 vst1.8 {@XMM[7]}, [$out]!
1298 b .Lcbc_dec_done
1299 .align 4
1300 .Lcbc_dec_five:
1301 sub $inp, $inp, #0x50
1302 bl _bsaes_decrypt8
1303 vldmia $fp, {@XMM[14]} @ reload IV
1304 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1305 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1306 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1307 veor @XMM[1], @XMM[1], @XMM[8]
1308 veor @XMM[6], @XMM[6], @XMM[9]
1309 vld1.8 {@XMM[15]}, [$inp]!
1310 veor @XMM[4], @XMM[4], @XMM[10]
1311 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1312 veor @XMM[2], @XMM[2], @XMM[11]
1313 vst1.8 {@XMM[6]}, [$out]!
1314 vst1.8 {@XMM[4]}, [$out]!
1315 vst1.8 {@XMM[2]}, [$out]!
1316 b .Lcbc_dec_done
1317 .align 4
1318 .Lcbc_dec_four:
1319 sub $inp, $inp, #0x40
1320 bl _bsaes_decrypt8
1321 vldmia $fp, {@XMM[14]} @ reload IV
1322 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1323 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1324 vld1.8 {@XMM[10]}, [$inp]!
1325 veor @XMM[1], @XMM[1], @XMM[8]
1326 veor @XMM[6], @XMM[6], @XMM[9]
1327 vld1.8 {@XMM[15]}, [$inp]!
1328 veor @XMM[4], @XMM[4], @XMM[10]
1329 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1330 vst1.8 {@XMM[6]}, [$out]!
1331 vst1.8 {@XMM[4]}, [$out]!
1332 b .Lcbc_dec_done
1333 .align 4
1334 .Lcbc_dec_three:
1335 sub $inp, $inp, #0x30
1336 bl _bsaes_decrypt8
1337 vldmia $fp, {@XMM[14]} @ reload IV
1338 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
1339 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1340 vld1.8 {@XMM[15]}, [$inp]!
1341 veor @XMM[1], @XMM[1], @XMM[8]
1342 veor @XMM[6], @XMM[6], @XMM[9]
1343 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1344 vst1.8 {@XMM[6]}, [$out]!
1345 b .Lcbc_dec_done
1346 .align 4
1347 .Lcbc_dec_two:
1348 sub $inp, $inp, #0x20
1349 bl _bsaes_decrypt8
1350 vldmia $fp, {@XMM[14]} @ reload IV
1351 vld1.8 {@XMM[8]}, [$inp]! @ reload input
1352 veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
1353 vld1.8 {@XMM[15]}, [$inp]! @ reload input
1354 veor @XMM[1], @XMM[1], @XMM[8]
1355 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1356 b .Lcbc_dec_done
1357 .align 4
1358 .Lcbc_dec_one:
1359 sub $inp, $inp, #0x10
1360 mov $rounds, $out @ save original out pointer
1361 mov $out, $fp @ use the iv scratch space as out buffer
1362 mov r2, $key
1363 vmov @XMM[4],@XMM[15] @ just in case ensure that IV
1364 vmov @XMM[5],@XMM[0] @ and input are preserved
1365 bl AES_decrypt
1366 vld1.8 {@XMM[0]}, [$fp] @ load result
1367 veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
1368 vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
1369 vst1.8 {@XMM[0]}, [$rounds] @ write output
1370
1371 .Lcbc_dec_done:
1372 #ifndef BSAES_ASM_EXTENDED_KEY
1373 vmov.i32 q0, #0
1374 vmov.i32 q1, #0
1375 .Lcbc_dec_bzero: @ wipe key schedule [if any]
1376 vstmia $keysched!, {q0-q1}
1377 cmp $keysched, $fp
1378 bne .Lcbc_dec_bzero
1379 #endif
1380
1381 mov sp, $fp
1382 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1383 vst1.8 {@XMM[15]}, [$ivp] @ return IV
1384 VFP_ABI_POP
1385 ldmia sp!, {r4-r10, pc}
1386 .size ossl_bsaes_cbc_encrypt,.-ossl_bsaes_cbc_encrypt
1387 ___
1388 }
1389 {
1390 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1391 my $const = "r6"; # shared with _bsaes_encrypt8_alt
1392 my $keysched = "sp";
1393
1394 $code.=<<___;
1395 .extern AES_encrypt
1396 .global ossl_bsaes_ctr32_encrypt_blocks
1397 .type ossl_bsaes_ctr32_encrypt_blocks,%function
1398 .align 5
1399 ossl_bsaes_ctr32_encrypt_blocks:
1400 cmp $len, #8 @ use plain AES for
1401 blo .Lctr_enc_short @ small sizes
1402
1403 mov ip, sp
1404 stmdb sp!, {r4-r10, lr}
1405 VFP_ABI_PUSH
1406 ldr $ctr, [ip] @ ctr is 1st arg on the stack
1407 sub sp, sp, #0x10 @ scratch space to carry over the ctr
1408 mov $fp, sp @ save sp
1409
1410 ldr $rounds, [$key, #240] @ get # of rounds
1411 #ifndef BSAES_ASM_EXTENDED_KEY
1412 @ allocate the key schedule on the stack
1413 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1414 add r12, #`128-32` @ size of bit-sliced key schedule
1415
1416 @ populate the key schedule
1417 mov r4, $key @ pass key
1418 mov r5, $rounds @ pass # of rounds
1419 mov sp, r12 @ sp is $keysched
1420 bl _bsaes_key_convert
1421 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1422 vstmia r12, {@XMM[7]} @ save last round key
1423
1424 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1425 #ifdef __APPLE__
1426 mov $ctr, #:lower16:(.LREVM0SR-.LM0)
1427 add $ctr, $const, $ctr
1428 #else
1429 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1430 #endif
1431 vldmia $keysched, {@XMM[4]} @ load round0 key
1432 #else
1433 ldr r12, [$key, #244]
1434 eors r12, #1
1435 beq 0f
1436
1437 @ populate the key schedule
1438 str r12, [$key, #244]
1439 mov r4, $key @ pass key
1440 mov r5, $rounds @ pass # of rounds
1441 add r12, $key, #248 @ pass key schedule
1442 bl _bsaes_key_convert
1443 veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
1444 vstmia r12, {@XMM[7]} @ save last round key
1445
1446 .align 2
1447 0: add r12, $key, #248
1448 vld1.8 {@XMM[0]}, [$ctr] @ load counter
1449 add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
1450 vldmia r12, {@XMM[4]} @ load round0 key
1451 sub sp, #0x10 @ place for adjusted round0 key
1452 #endif
1453
1454 vmov.i32 @XMM[8],#1 @ compose 1<<96
1455 veor @XMM[9],@XMM[9],@XMM[9]
1456 vrev32.8 @XMM[0],@XMM[0]
1457 vext.8 @XMM[8],@XMM[9],@XMM[8],#4
1458 vrev32.8 @XMM[4],@XMM[4]
1459 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1460 vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
1461 b .Lctr_enc_loop
1462
1463 .align 4
1464 .Lctr_enc_loop:
1465 vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
1466 vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
1467 vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
1468 vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
1469 vadd.u32 @XMM[4], @XMM[1], @XMM[10]
1470 vadd.u32 @XMM[5], @XMM[2], @XMM[10]
1471 vadd.u32 @XMM[6], @XMM[3], @XMM[10]
1472 vadd.u32 @XMM[7], @XMM[4], @XMM[10]
1473 vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
1474
1475 @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1476 @ to flip byte order in 32-bit counter
1477
1478 vldmia $keysched, {@XMM[9]} @ load round0 key
1479 #ifndef BSAES_ASM_EXTENDED_KEY
1480 add r4, $keysched, #0x10 @ pass next round key
1481 #else
1482 add r4, $key, #`248+16`
1483 #endif
1484 vldmia $ctr, {@XMM[8]} @ .LREVM0SR
1485 mov r5, $rounds @ pass rounds
1486 vstmia $fp, {@XMM[10]} @ save next counter
1487 #ifdef __APPLE__
1488 mov $const, #:lower16:(.LREVM0SR-.LSR)
1489 sub $const, $ctr, $const
1490 #else
1491 sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
1492 #endif
1493
1494 bl _bsaes_encrypt8_alt
1495
1496 subs $len, $len, #8
1497 blo .Lctr_enc_loop_done
1498
1499 vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
1500 vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
1501 veor @XMM[0], @XMM[8]
1502 veor @XMM[1], @XMM[9]
1503 vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
1504 veor @XMM[4], @XMM[10]
1505 veor @XMM[6], @XMM[11]
1506 vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
1507 veor @XMM[3], @XMM[12]
1508 vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
1509 veor @XMM[7], @XMM[13]
1510 veor @XMM[2], @XMM[14]
1511 vst1.8 {@XMM[4]}, [$out]!
1512 veor @XMM[5], @XMM[15]
1513 vst1.8 {@XMM[6]}, [$out]!
1514 vmov.i32 @XMM[8], #1 @ compose 1<<96
1515 vst1.8 {@XMM[3]}, [$out]!
1516 veor @XMM[9], @XMM[9], @XMM[9]
1517 vst1.8 {@XMM[7]}, [$out]!
1518 vext.8 @XMM[8], @XMM[9], @XMM[8], #4
1519 vst1.8 {@XMM[2]}, [$out]!
1520 vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1521 vst1.8 {@XMM[5]}, [$out]!
1522 vldmia $fp, {@XMM[0]} @ load counter
1523
1524 bne .Lctr_enc_loop
1525 b .Lctr_enc_done
1526
1527 .align 4
1528 .Lctr_enc_loop_done:
1529 add $len, $len, #8
1530 vld1.8 {@XMM[8]}, [$inp]! @ load input
1531 veor @XMM[0], @XMM[8]
1532 vst1.8 {@XMM[0]}, [$out]! @ write output
1533 cmp $len, #2
1534 blo .Lctr_enc_done
1535 vld1.8 {@XMM[9]}, [$inp]!
1536 veor @XMM[1], @XMM[9]
1537 vst1.8 {@XMM[1]}, [$out]!
1538 beq .Lctr_enc_done
1539 vld1.8 {@XMM[10]}, [$inp]!
1540 veor @XMM[4], @XMM[10]
1541 vst1.8 {@XMM[4]}, [$out]!
1542 cmp $len, #4
1543 blo .Lctr_enc_done
1544 vld1.8 {@XMM[11]}, [$inp]!
1545 veor @XMM[6], @XMM[11]
1546 vst1.8 {@XMM[6]}, [$out]!
1547 beq .Lctr_enc_done
1548 vld1.8 {@XMM[12]}, [$inp]!
1549 veor @XMM[3], @XMM[12]
1550 vst1.8 {@XMM[3]}, [$out]!
1551 cmp $len, #6
1552 blo .Lctr_enc_done
1553 vld1.8 {@XMM[13]}, [$inp]!
1554 veor @XMM[7], @XMM[13]
1555 vst1.8 {@XMM[7]}, [$out]!
1556 beq .Lctr_enc_done
1557 vld1.8 {@XMM[14]}, [$inp]
1558 veor @XMM[2], @XMM[14]
1559 vst1.8 {@XMM[2]}, [$out]!
1560
1561 .Lctr_enc_done:
1562 vmov.i32 q0, #0
1563 vmov.i32 q1, #0
1564 #ifndef BSAES_ASM_EXTENDED_KEY
1565 .Lctr_enc_bzero: @ wipe key schedule [if any]
1566 vstmia $keysched!, {q0-q1}
1567 cmp $keysched, $fp
1568 bne .Lctr_enc_bzero
1569 #else
1570 vstmia $keysched, {q0-q1}
1571 #endif
1572
1573 mov sp, $fp
1574 add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
1575 VFP_ABI_POP
1576 ldmia sp!, {r4-r10, pc} @ return
1577
1578 .align 4
1579 .Lctr_enc_short:
1580 ldr ip, [sp] @ ctr pointer is passed on stack
1581 stmdb sp!, {r4-r8, lr}
1582
1583 mov r4, $inp @ copy arguments
1584 mov r5, $out
1585 mov r6, $len
1586 mov r7, $key
1587 ldr r8, [ip, #12] @ load counter LSW
1588 vld1.8 {@XMM[1]}, [ip] @ load whole counter value
1589 #ifdef __ARMEL__
1590 rev r8, r8
1591 #endif
1592 sub sp, sp, #0x10
1593 vst1.8 {@XMM[1]}, [sp] @ copy counter value
1594 sub sp, sp, #0x10
1595
1596 .Lctr_enc_short_loop:
1597 add r0, sp, #0x10 @ input counter value
1598 mov r1, sp @ output on the stack
1599 mov r2, r7 @ key
1600
1601 bl AES_encrypt
1602
1603 vld1.8 {@XMM[0]}, [r4]! @ load input
1604 vld1.8 {@XMM[1]}, [sp] @ load encrypted counter
1605 add r8, r8, #1
1606 #ifdef __ARMEL__
1607 rev r0, r8
1608 str r0, [sp, #0x1c] @ next counter value
1609 #else
1610 str r8, [sp, #0x1c] @ next counter value
1611 #endif
1612 veor @XMM[0],@XMM[0],@XMM[1]
1613 vst1.8 {@XMM[0]}, [r5]! @ store output
1614 subs r6, r6, #1
1615 bne .Lctr_enc_short_loop
1616
1617 vmov.i32 q0, #0
1618 vmov.i32 q1, #0
1619 vstmia sp!, {q0-q1}
1620
1621 ldmia sp!, {r4-r8, pc}
1622 .size ossl_bsaes_ctr32_encrypt_blocks,.-ossl_bsaes_ctr32_encrypt_blocks
1623 ___
1624 }
1625 {
1626 ######################################################################
1627 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1628 # const AES_KEY *key1, const AES_KEY *key2,
1629 # const unsigned char iv[16]);
1630 #
1631 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1632 my $const="r6"; # returned by _bsaes_key_convert
1633 my $twmask=@XMM[5];
1634 my @T=@XMM[6..7];
1635
1636 $code.=<<___;
1637 .globl ossl_bsaes_xts_encrypt
1638 .type ossl_bsaes_xts_encrypt,%function
1639 .align 4
1640 ossl_bsaes_xts_encrypt:
1641 mov ip, sp
1642 stmdb sp!, {r4-r10, lr} @ 0x20
1643 VFP_ABI_PUSH
1644 mov r6, sp @ future $fp
1645
1646 mov $inp, r0
1647 mov $out, r1
1648 mov $len, r2
1649 mov $key, r3
1650
1651 sub r0, sp, #0x10 @ 0x10
1652 bic r0, #0xf @ align at 16 bytes
1653 mov sp, r0
1654
1655 #ifdef XTS_CHAIN_TWEAK
1656 ldr r0, [ip] @ pointer to input tweak
1657 #else
1658 @ generate initial tweak
1659 ldr r0, [ip, #4] @ iv[]
1660 mov r1, sp
1661 ldr r2, [ip, #0] @ key2
1662 bl AES_encrypt
1663 mov r0,sp @ pointer to initial tweak
1664 #endif
1665
1666 ldr $rounds, [$key, #240] @ get # of rounds
1667 mov $fp, r6
1668 #ifndef BSAES_ASM_EXTENDED_KEY
1669 @ allocate the key schedule on the stack
1670 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
1671 @ add r12, #`128-32` @ size of bit-sliced key schedule
1672 sub r12, #`32+16` @ place for tweak[9]
1673
1674 @ populate the key schedule
1675 mov r4, $key @ pass key
1676 mov r5, $rounds @ pass # of rounds
1677 mov sp, r12
1678 add r12, #0x90 @ pass key schedule
1679 bl _bsaes_key_convert
1680 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1681 vstmia r12, {@XMM[7]} @ save last round key
1682 #else
1683 ldr r12, [$key, #244]
1684 eors r12, #1
1685 beq 0f
1686
1687 str r12, [$key, #244]
1688 mov r4, $key @ pass key
1689 mov r5, $rounds @ pass # of rounds
1690 add r12, $key, #248 @ pass key schedule
1691 bl _bsaes_key_convert
1692 veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
1693 vstmia r12, {@XMM[7]}
1694
1695 .align 2
1696 0: sub sp, #0x90 @ place for tweak[9]
1697 #endif
1698
1699 vld1.8 {@XMM[8]}, [r0] @ initial tweak
1700 adr $magic, .Lxts_magic
1701
1702 subs $len, #0x80
1703 blo .Lxts_enc_short
1704 b .Lxts_enc_loop
1705
1706 .align 4
1707 .Lxts_enc_loop:
1708 vldmia $magic, {$twmask} @ load XTS magic
1709 vshr.s64 @T[0], @XMM[8], #63
1710 mov r0, sp
1711 vand @T[0], @T[0], $twmask
1712 ___
1713 for($i=9;$i<16;$i++) {
1714 $code.=<<___;
1715 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1716 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1717 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1718 vshr.s64 @T[1], @XMM[$i], #63
1719 veor @XMM[$i], @XMM[$i], @T[0]
1720 vand @T[1], @T[1], $twmask
1721 ___
1722 @T=reverse(@T);
1723
1724 $code.=<<___ if ($i>=10);
1725 vld1.8 {@XMM[$i-10]}, [$inp]!
1726 ___
1727 $code.=<<___ if ($i>=11);
1728 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1729 ___
1730 }
1731 $code.=<<___;
1732 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
1733 vst1.64 {@XMM[15]}, [r0,:128]!
1734 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1735 veor @XMM[8], @XMM[8], @T[0]
1736 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1737
1738 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
1739 veor @XMM[5], @XMM[5], @XMM[13]
1740 #ifndef BSAES_ASM_EXTENDED_KEY
1741 add r4, sp, #0x90 @ pass key schedule
1742 #else
1743 add r4, $key, #248 @ pass key schedule
1744 #endif
1745 veor @XMM[6], @XMM[6], @XMM[14]
1746 mov r5, $rounds @ pass rounds
1747 veor @XMM[7], @XMM[7], @XMM[15]
1748 mov r0, sp
1749
1750 bl _bsaes_encrypt8
1751
1752 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1753 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1754 veor @XMM[0], @XMM[0], @XMM[ 8]
1755 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1756 veor @XMM[1], @XMM[1], @XMM[ 9]
1757 veor @XMM[8], @XMM[4], @XMM[10]
1758 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1759 veor @XMM[9], @XMM[6], @XMM[11]
1760 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
1761 veor @XMM[10], @XMM[3], @XMM[12]
1762 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1763 veor @XMM[11], @XMM[7], @XMM[13]
1764 veor @XMM[12], @XMM[2], @XMM[14]
1765 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1766 veor @XMM[13], @XMM[5], @XMM[15]
1767 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
1768
1769 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1770
1771 subs $len, #0x80
1772 bpl .Lxts_enc_loop
1773
1774 .Lxts_enc_short:
1775 adds $len, #0x70
1776 bmi .Lxts_enc_done
1777
1778 vldmia $magic, {$twmask} @ load XTS magic
1779 vshr.s64 @T[0], @XMM[8], #63
1780 mov r0, sp
1781 vand @T[0], @T[0], $twmask
1782 ___
1783 for($i=9;$i<16;$i++) {
1784 $code.=<<___;
1785 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1786 vst1.64 {@XMM[$i-1]}, [r0,:128]!
1787 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1788 vshr.s64 @T[1], @XMM[$i], #63
1789 veor @XMM[$i], @XMM[$i], @T[0]
1790 vand @T[1], @T[1], $twmask
1791 ___
1792 @T=reverse(@T);
1793
1794 $code.=<<___ if ($i>=10);
1795 vld1.8 {@XMM[$i-10]}, [$inp]!
1796 subs $len, #0x10
1797 bmi .Lxts_enc_`$i-9`
1798 ___
1799 $code.=<<___ if ($i>=11);
1800 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1801 ___
1802 }
1803 $code.=<<___;
1804 sub $len, #0x10
1805 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
1806
1807 vld1.8 {@XMM[6]}, [$inp]!
1808 veor @XMM[5], @XMM[5], @XMM[13]
1809 #ifndef BSAES_ASM_EXTENDED_KEY
1810 add r4, sp, #0x90 @ pass key schedule
1811 #else
1812 add r4, $key, #248 @ pass key schedule
1813 #endif
1814 veor @XMM[6], @XMM[6], @XMM[14]
1815 mov r5, $rounds @ pass rounds
1816 mov r0, sp
1817
1818 bl _bsaes_encrypt8
1819
1820 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1821 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1822 veor @XMM[0], @XMM[0], @XMM[ 8]
1823 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1824 veor @XMM[1], @XMM[1], @XMM[ 9]
1825 veor @XMM[8], @XMM[4], @XMM[10]
1826 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1827 veor @XMM[9], @XMM[6], @XMM[11]
1828 vld1.64 {@XMM[14]}, [r0,:128]!
1829 veor @XMM[10], @XMM[3], @XMM[12]
1830 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1831 veor @XMM[11], @XMM[7], @XMM[13]
1832 veor @XMM[12], @XMM[2], @XMM[14]
1833 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1834 vst1.8 {@XMM[12]}, [$out]!
1835
1836 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1837 b .Lxts_enc_done
1838 .align 4
1839 .Lxts_enc_6:
1840 veor @XMM[4], @XMM[4], @XMM[12]
1841 #ifndef BSAES_ASM_EXTENDED_KEY
1842 add r4, sp, #0x90 @ pass key schedule
1843 #else
1844 add r4, $key, #248 @ pass key schedule
1845 #endif
1846 veor @XMM[5], @XMM[5], @XMM[13]
1847 mov r5, $rounds @ pass rounds
1848 mov r0, sp
1849
1850 bl _bsaes_encrypt8
1851
1852 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1853 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1854 veor @XMM[0], @XMM[0], @XMM[ 8]
1855 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
1856 veor @XMM[1], @XMM[1], @XMM[ 9]
1857 veor @XMM[8], @XMM[4], @XMM[10]
1858 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1859 veor @XMM[9], @XMM[6], @XMM[11]
1860 veor @XMM[10], @XMM[3], @XMM[12]
1861 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1862 veor @XMM[11], @XMM[7], @XMM[13]
1863 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
1864
1865 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1866 b .Lxts_enc_done
1867
1868 @ put this in range for both ARM and Thumb mode adr instructions
1869 .align 5
1870 .Lxts_magic:
1871 .quad 1, 0x87
1872
1873 .align 5
1874 .Lxts_enc_5:
1875 veor @XMM[3], @XMM[3], @XMM[11]
1876 #ifndef BSAES_ASM_EXTENDED_KEY
1877 add r4, sp, #0x90 @ pass key schedule
1878 #else
1879 add r4, $key, #248 @ pass key schedule
1880 #endif
1881 veor @XMM[4], @XMM[4], @XMM[12]
1882 mov r5, $rounds @ pass rounds
1883 mov r0, sp
1884
1885 bl _bsaes_encrypt8
1886
1887 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1888 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1889 veor @XMM[0], @XMM[0], @XMM[ 8]
1890 vld1.64 {@XMM[12]}, [r0,:128]!
1891 veor @XMM[1], @XMM[1], @XMM[ 9]
1892 veor @XMM[8], @XMM[4], @XMM[10]
1893 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1894 veor @XMM[9], @XMM[6], @XMM[11]
1895 veor @XMM[10], @XMM[3], @XMM[12]
1896 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1897 vst1.8 {@XMM[10]}, [$out]!
1898
1899 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1900 b .Lxts_enc_done
1901 .align 4
1902 .Lxts_enc_4:
1903 veor @XMM[2], @XMM[2], @XMM[10]
1904 #ifndef BSAES_ASM_EXTENDED_KEY
1905 add r4, sp, #0x90 @ pass key schedule
1906 #else
1907 add r4, $key, #248 @ pass key schedule
1908 #endif
1909 veor @XMM[3], @XMM[3], @XMM[11]
1910 mov r5, $rounds @ pass rounds
1911 mov r0, sp
1912
1913 bl _bsaes_encrypt8
1914
1915 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1916 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
1917 veor @XMM[0], @XMM[0], @XMM[ 8]
1918 veor @XMM[1], @XMM[1], @XMM[ 9]
1919 veor @XMM[8], @XMM[4], @XMM[10]
1920 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1921 veor @XMM[9], @XMM[6], @XMM[11]
1922 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
1923
1924 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1925 b .Lxts_enc_done
1926 .align 4
1927 .Lxts_enc_3:
1928 veor @XMM[1], @XMM[1], @XMM[9]
1929 #ifndef BSAES_ASM_EXTENDED_KEY
1930 add r4, sp, #0x90 @ pass key schedule
1931 #else
1932 add r4, $key, #248 @ pass key schedule
1933 #endif
1934 veor @XMM[2], @XMM[2], @XMM[10]
1935 mov r5, $rounds @ pass rounds
1936 mov r0, sp
1937
1938 bl _bsaes_encrypt8
1939
1940 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1941 vld1.64 {@XMM[10]}, [r0,:128]!
1942 veor @XMM[0], @XMM[0], @XMM[ 8]
1943 veor @XMM[1], @XMM[1], @XMM[ 9]
1944 veor @XMM[8], @XMM[4], @XMM[10]
1945 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1946 vst1.8 {@XMM[8]}, [$out]!
1947
1948 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1949 b .Lxts_enc_done
1950 .align 4
1951 .Lxts_enc_2:
1952 veor @XMM[0], @XMM[0], @XMM[8]
1953 #ifndef BSAES_ASM_EXTENDED_KEY
1954 add r4, sp, #0x90 @ pass key schedule
1955 #else
1956 add r4, $key, #248 @ pass key schedule
1957 #endif
1958 veor @XMM[1], @XMM[1], @XMM[9]
1959 mov r5, $rounds @ pass rounds
1960 mov r0, sp
1961
1962 bl _bsaes_encrypt8
1963
1964 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
1965 veor @XMM[0], @XMM[0], @XMM[ 8]
1966 veor @XMM[1], @XMM[1], @XMM[ 9]
1967 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
1968
1969 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
1970 b .Lxts_enc_done
1971 .align 4
1972 .Lxts_enc_1:
1973 mov r0, sp
1974 veor @XMM[0], @XMM[0], @XMM[8]
1975 mov r1, sp
1976 vst1.8 {@XMM[0]}, [sp,:128]
1977 mov r2, $key
1978 mov r4, $fp @ preserve fp
1979
1980 bl AES_encrypt
1981
1982 vld1.8 {@XMM[0]}, [sp,:128]
1983 veor @XMM[0], @XMM[0], @XMM[8]
1984 vst1.8 {@XMM[0]}, [$out]!
1985 mov $fp, r4
1986
1987 vmov @XMM[8], @XMM[9] @ next round tweak
1988
1989 .Lxts_enc_done:
1990 #ifndef XTS_CHAIN_TWEAK
1991 adds $len, #0x10
1992 beq .Lxts_enc_ret
1993 sub r6, $out, #0x10
1994
1995 .Lxts_enc_steal:
1996 ldrb r0, [$inp], #1
1997 ldrb r1, [$out, #-0x10]
1998 strb r0, [$out, #-0x10]
1999 strb r1, [$out], #1
2000
2001 subs $len, #1
2002 bhi .Lxts_enc_steal
2003
2004 vld1.8 {@XMM[0]}, [r6]
2005 mov r0, sp
2006 veor @XMM[0], @XMM[0], @XMM[8]
2007 mov r1, sp
2008 vst1.8 {@XMM[0]}, [sp,:128]
2009 mov r2, $key
2010 mov r4, $fp @ preserve fp
2011
2012 bl AES_encrypt
2013
2014 vld1.8 {@XMM[0]}, [sp,:128]
2015 veor @XMM[0], @XMM[0], @XMM[8]
2016 vst1.8 {@XMM[0]}, [r6]
2017 mov $fp, r4
2018 #endif
2019
2020 .Lxts_enc_ret:
2021 bic r0, $fp, #0xf
2022 vmov.i32 q0, #0
2023 vmov.i32 q1, #0
2024 #ifdef XTS_CHAIN_TWEAK
2025 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2026 #endif
2027 .Lxts_enc_bzero: @ wipe key schedule [if any]
2028 vstmia sp!, {q0-q1}
2029 cmp sp, r0
2030 bne .Lxts_enc_bzero
2031
2032 mov sp, $fp
2033 #ifdef XTS_CHAIN_TWEAK
2034 vst1.8 {@XMM[8]}, [r1]
2035 #endif
2036 VFP_ABI_POP
2037 ldmia sp!, {r4-r10, pc} @ return
2038
2039 .size ossl_bsaes_xts_encrypt,.-ossl_bsaes_xts_encrypt
2040
2041 .globl ossl_bsaes_xts_decrypt
2042 .type ossl_bsaes_xts_decrypt,%function
2043 .align 4
2044 ossl_bsaes_xts_decrypt:
2045 mov ip, sp
2046 stmdb sp!, {r4-r10, lr} @ 0x20
2047 VFP_ABI_PUSH
2048 mov r6, sp @ future $fp
2049
2050 mov $inp, r0
2051 mov $out, r1
2052 mov $len, r2
2053 mov $key, r3
2054
2055 sub r0, sp, #0x10 @ 0x10
2056 bic r0, #0xf @ align at 16 bytes
2057 mov sp, r0
2058
2059 #ifdef XTS_CHAIN_TWEAK
2060 ldr r0, [ip] @ pointer to input tweak
2061 #else
2062 @ generate initial tweak
2063 ldr r0, [ip, #4] @ iv[]
2064 mov r1, sp
2065 ldr r2, [ip, #0] @ key2
2066 bl AES_encrypt
2067 mov r0, sp @ pointer to initial tweak
2068 #endif
2069
2070 ldr $rounds, [$key, #240] @ get # of rounds
2071 mov $fp, r6
2072 #ifndef BSAES_ASM_EXTENDED_KEY
2073 @ allocate the key schedule on the stack
2074 sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
2075 @ add r12, #`128-32` @ size of bit-sliced key schedule
2076 sub r12, #`32+16` @ place for tweak[9]
2077
2078 @ populate the key schedule
2079 mov r4, $key @ pass key
2080 mov r5, $rounds @ pass # of rounds
2081 mov sp, r12
2082 add r12, #0x90 @ pass key schedule
2083 bl _bsaes_key_convert
2084 add r4, sp, #0x90
2085 vldmia r4, {@XMM[6]}
2086 vstmia r12, {@XMM[15]} @ save last round key
2087 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2088 vstmia r4, {@XMM[7]}
2089 #else
2090 ldr r12, [$key, #244]
2091 eors r12, #1
2092 beq 0f
2093
2094 str r12, [$key, #244]
2095 mov r4, $key @ pass key
2096 mov r5, $rounds @ pass # of rounds
2097 add r12, $key, #248 @ pass key schedule
2098 bl _bsaes_key_convert
2099 add r4, $key, #248
2100 vldmia r4, {@XMM[6]}
2101 vstmia r12, {@XMM[15]} @ save last round key
2102 veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
2103 vstmia r4, {@XMM[7]}
2104
2105 .align 2
2106 0: sub sp, #0x90 @ place for tweak[9]
2107 #endif
2108 vld1.8 {@XMM[8]}, [r0] @ initial tweak
2109 adr $magic, .Lxts_magic
2110
2111 #ifndef XTS_CHAIN_TWEAK
2112 tst $len, #0xf @ if not multiple of 16
2113 it ne @ Thumb2 thing, sanity check in ARM
2114 subne $len, #0x10 @ subtract another 16 bytes
2115 #endif
2116 subs $len, #0x80
2117
2118 blo .Lxts_dec_short
2119 b .Lxts_dec_loop
2120
2121 .align 4
2122 .Lxts_dec_loop:
2123 vldmia $magic, {$twmask} @ load XTS magic
2124 vshr.s64 @T[0], @XMM[8], #63
2125 mov r0, sp
2126 vand @T[0], @T[0], $twmask
2127 ___
2128 for($i=9;$i<16;$i++) {
2129 $code.=<<___;
2130 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2131 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2132 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2133 vshr.s64 @T[1], @XMM[$i], #63
2134 veor @XMM[$i], @XMM[$i], @T[0]
2135 vand @T[1], @T[1], $twmask
2136 ___
2137 @T=reverse(@T);
2138
2139 $code.=<<___ if ($i>=10);
2140 vld1.8 {@XMM[$i-10]}, [$inp]!
2141 ___
2142 $code.=<<___ if ($i>=11);
2143 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2144 ___
2145 }
2146 $code.=<<___;
2147 vadd.u64 @XMM[8], @XMM[15], @XMM[15]
2148 vst1.64 {@XMM[15]}, [r0,:128]!
2149 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2150 veor @XMM[8], @XMM[8], @T[0]
2151 vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2152
2153 vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
2154 veor @XMM[5], @XMM[5], @XMM[13]
2155 #ifndef BSAES_ASM_EXTENDED_KEY
2156 add r4, sp, #0x90 @ pass key schedule
2157 #else
2158 add r4, $key, #248 @ pass key schedule
2159 #endif
2160 veor @XMM[6], @XMM[6], @XMM[14]
2161 mov r5, $rounds @ pass rounds
2162 veor @XMM[7], @XMM[7], @XMM[15]
2163 mov r0, sp
2164
2165 bl _bsaes_decrypt8
2166
2167 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2168 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2169 veor @XMM[0], @XMM[0], @XMM[ 8]
2170 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2171 veor @XMM[1], @XMM[1], @XMM[ 9]
2172 veor @XMM[8], @XMM[6], @XMM[10]
2173 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2174 veor @XMM[9], @XMM[4], @XMM[11]
2175 vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
2176 veor @XMM[10], @XMM[2], @XMM[12]
2177 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2178 veor @XMM[11], @XMM[7], @XMM[13]
2179 veor @XMM[12], @XMM[3], @XMM[14]
2180 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2181 veor @XMM[13], @XMM[5], @XMM[15]
2182 vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
2183
2184 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2185
2186 subs $len, #0x80
2187 bpl .Lxts_dec_loop
2188
2189 .Lxts_dec_short:
2190 adds $len, #0x70
2191 bmi .Lxts_dec_done
2192
2193 vldmia $magic, {$twmask} @ load XTS magic
2194 vshr.s64 @T[0], @XMM[8], #63
2195 mov r0, sp
2196 vand @T[0], @T[0], $twmask
2197 ___
2198 for($i=9;$i<16;$i++) {
2199 $code.=<<___;
2200 vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2201 vst1.64 {@XMM[$i-1]}, [r0,:128]!
2202 vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2203 vshr.s64 @T[1], @XMM[$i], #63
2204 veor @XMM[$i], @XMM[$i], @T[0]
2205 vand @T[1], @T[1], $twmask
2206 ___
2207 @T=reverse(@T);
2208
2209 $code.=<<___ if ($i>=10);
2210 vld1.8 {@XMM[$i-10]}, [$inp]!
2211 subs $len, #0x10
2212 bmi .Lxts_dec_`$i-9`
2213 ___
2214 $code.=<<___ if ($i>=11);
2215 veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2216 ___
2217 }
2218 $code.=<<___;
2219 sub $len, #0x10
2220 vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
2221
2222 vld1.8 {@XMM[6]}, [$inp]!
2223 veor @XMM[5], @XMM[5], @XMM[13]
2224 #ifndef BSAES_ASM_EXTENDED_KEY
2225 add r4, sp, #0x90 @ pass key schedule
2226 #else
2227 add r4, $key, #248 @ pass key schedule
2228 #endif
2229 veor @XMM[6], @XMM[6], @XMM[14]
2230 mov r5, $rounds @ pass rounds
2231 mov r0, sp
2232
2233 bl _bsaes_decrypt8
2234
2235 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2236 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2237 veor @XMM[0], @XMM[0], @XMM[ 8]
2238 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2239 veor @XMM[1], @XMM[1], @XMM[ 9]
2240 veor @XMM[8], @XMM[6], @XMM[10]
2241 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2242 veor @XMM[9], @XMM[4], @XMM[11]
2243 vld1.64 {@XMM[14]}, [r0,:128]!
2244 veor @XMM[10], @XMM[2], @XMM[12]
2245 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2246 veor @XMM[11], @XMM[7], @XMM[13]
2247 veor @XMM[12], @XMM[3], @XMM[14]
2248 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2249 vst1.8 {@XMM[12]}, [$out]!
2250
2251 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2252 b .Lxts_dec_done
2253 .align 4
2254 .Lxts_dec_6:
2255 vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
2256
2257 veor @XMM[4], @XMM[4], @XMM[12]
2258 #ifndef BSAES_ASM_EXTENDED_KEY
2259 add r4, sp, #0x90 @ pass key schedule
2260 #else
2261 add r4, $key, #248 @ pass key schedule
2262 #endif
2263 veor @XMM[5], @XMM[5], @XMM[13]
2264 mov r5, $rounds @ pass rounds
2265 mov r0, sp
2266
2267 bl _bsaes_decrypt8
2268
2269 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2270 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2271 veor @XMM[0], @XMM[0], @XMM[ 8]
2272 vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
2273 veor @XMM[1], @XMM[1], @XMM[ 9]
2274 veor @XMM[8], @XMM[6], @XMM[10]
2275 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2276 veor @XMM[9], @XMM[4], @XMM[11]
2277 veor @XMM[10], @XMM[2], @XMM[12]
2278 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2279 veor @XMM[11], @XMM[7], @XMM[13]
2280 vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
2281
2282 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2283 b .Lxts_dec_done
2284 .align 4
2285 .Lxts_dec_5:
2286 veor @XMM[3], @XMM[3], @XMM[11]
2287 #ifndef BSAES_ASM_EXTENDED_KEY
2288 add r4, sp, #0x90 @ pass key schedule
2289 #else
2290 add r4, $key, #248 @ pass key schedule
2291 #endif
2292 veor @XMM[4], @XMM[4], @XMM[12]
2293 mov r5, $rounds @ pass rounds
2294 mov r0, sp
2295
2296 bl _bsaes_decrypt8
2297
2298 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2299 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2300 veor @XMM[0], @XMM[0], @XMM[ 8]
2301 vld1.64 {@XMM[12]}, [r0,:128]!
2302 veor @XMM[1], @XMM[1], @XMM[ 9]
2303 veor @XMM[8], @XMM[6], @XMM[10]
2304 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2305 veor @XMM[9], @XMM[4], @XMM[11]
2306 veor @XMM[10], @XMM[2], @XMM[12]
2307 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2308 vst1.8 {@XMM[10]}, [$out]!
2309
2310 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2311 b .Lxts_dec_done
2312 .align 4
2313 .Lxts_dec_4:
2314 veor @XMM[2], @XMM[2], @XMM[10]
2315 #ifndef BSAES_ASM_EXTENDED_KEY
2316 add r4, sp, #0x90 @ pass key schedule
2317 #else
2318 add r4, $key, #248 @ pass key schedule
2319 #endif
2320 veor @XMM[3], @XMM[3], @XMM[11]
2321 mov r5, $rounds @ pass rounds
2322 mov r0, sp
2323
2324 bl _bsaes_decrypt8
2325
2326 vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2327 vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
2328 veor @XMM[0], @XMM[0], @XMM[ 8]
2329 veor @XMM[1], @XMM[1], @XMM[ 9]
2330 veor @XMM[8], @XMM[6], @XMM[10]
2331 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2332 veor @XMM[9], @XMM[4], @XMM[11]
2333 vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
2334
2335 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2336 b .Lxts_dec_done
2337 .align 4
2338 .Lxts_dec_3:
2339 veor @XMM[1], @XMM[1], @XMM[9]
2340 #ifndef BSAES_ASM_EXTENDED_KEY
2341 add r4, sp, #0x90 @ pass key schedule
2342 #else
2343 add r4, $key, #248 @ pass key schedule
2344 #endif
2345 veor @XMM[2], @XMM[2], @XMM[10]
2346 mov r5, $rounds @ pass rounds
2347 mov r0, sp
2348
2349 bl _bsaes_decrypt8
2350
2351 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2352 vld1.64 {@XMM[10]}, [r0,:128]!
2353 veor @XMM[0], @XMM[0], @XMM[ 8]
2354 veor @XMM[1], @XMM[1], @XMM[ 9]
2355 veor @XMM[8], @XMM[6], @XMM[10]
2356 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2357 vst1.8 {@XMM[8]}, [$out]!
2358
2359 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2360 b .Lxts_dec_done
2361 .align 4
2362 .Lxts_dec_2:
2363 veor @XMM[0], @XMM[0], @XMM[8]
2364 #ifndef BSAES_ASM_EXTENDED_KEY
2365 add r4, sp, #0x90 @ pass key schedule
2366 #else
2367 add r4, $key, #248 @ pass key schedule
2368 #endif
2369 veor @XMM[1], @XMM[1], @XMM[9]
2370 mov r5, $rounds @ pass rounds
2371 mov r0, sp
2372
2373 bl _bsaes_decrypt8
2374
2375 vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
2376 veor @XMM[0], @XMM[0], @XMM[ 8]
2377 veor @XMM[1], @XMM[1], @XMM[ 9]
2378 vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
2379
2380 vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
2381 b .Lxts_dec_done
2382 .align 4
2383 .Lxts_dec_1:
2384 mov r0, sp
2385 veor @XMM[0], @XMM[0], @XMM[8]
2386 mov r1, sp
2387 vst1.8 {@XMM[0]}, [sp,:128]
2388 mov r5, $magic @ preserve magic
2389 mov r2, $key
2390 mov r4, $fp @ preserve fp
2391
2392 bl AES_decrypt
2393
2394 vld1.8 {@XMM[0]}, [sp,:128]
2395 veor @XMM[0], @XMM[0], @XMM[8]
2396 vst1.8 {@XMM[0]}, [$out]!
2397 mov $fp, r4
2398 mov $magic, r5
2399
2400 vmov @XMM[8], @XMM[9] @ next round tweak
2401
2402 .Lxts_dec_done:
2403 #ifndef XTS_CHAIN_TWEAK
2404 adds $len, #0x10
2405 beq .Lxts_dec_ret
2406
2407 @ calculate one round of extra tweak for the stolen ciphertext
2408 vldmia $magic, {$twmask}
2409 vshr.s64 @XMM[6], @XMM[8], #63
2410 vand @XMM[6], @XMM[6], $twmask
2411 vadd.u64 @XMM[9], @XMM[8], @XMM[8]
2412 vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2413 veor @XMM[9], @XMM[9], @XMM[6]
2414
2415 @ perform the final decryption with the last tweak value
2416 vld1.8 {@XMM[0]}, [$inp]!
2417 mov r0, sp
2418 veor @XMM[0], @XMM[0], @XMM[9]
2419 mov r1, sp
2420 vst1.8 {@XMM[0]}, [sp,:128]
2421 mov r2, $key
2422 mov r4, $fp @ preserve fp
2423
2424 bl AES_decrypt
2425
2426 vld1.8 {@XMM[0]}, [sp,:128]
2427 veor @XMM[0], @XMM[0], @XMM[9]
2428 vst1.8 {@XMM[0]}, [$out]
2429
2430 mov r6, $out
2431 .Lxts_dec_steal:
2432 ldrb r1, [$out]
2433 ldrb r0, [$inp], #1
2434 strb r1, [$out, #0x10]
2435 strb r0, [$out], #1
2436
2437 subs $len, #1
2438 bhi .Lxts_dec_steal
2439
2440 vld1.8 {@XMM[0]}, [r6]
2441 mov r0, sp
2442 veor @XMM[0], @XMM[8]
2443 mov r1, sp
2444 vst1.8 {@XMM[0]}, [sp,:128]
2445 mov r2, $key
2446
2447 bl AES_decrypt
2448
2449 vld1.8 {@XMM[0]}, [sp,:128]
2450 veor @XMM[0], @XMM[0], @XMM[8]
2451 vst1.8 {@XMM[0]}, [r6]
2452 mov $fp, r4
2453 #endif
2454
2455 .Lxts_dec_ret:
2456 bic r0, $fp, #0xf
2457 vmov.i32 q0, #0
2458 vmov.i32 q1, #0
2459 #ifdef XTS_CHAIN_TWEAK
2460 ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
2461 #endif
2462 .Lxts_dec_bzero: @ wipe key schedule [if any]
2463 vstmia sp!, {q0-q1}
2464 cmp sp, r0
2465 bne .Lxts_dec_bzero
2466
2467 mov sp, $fp
2468 #ifdef XTS_CHAIN_TWEAK
2469 vst1.8 {@XMM[8]}, [r1]
2470 #endif
2471 VFP_ABI_POP
2472 ldmia sp!, {r4-r10, pc} @ return
2473
2474 .size ossl_bsaes_xts_decrypt,.-ossl_bsaes_xts_decrypt
2475 ___
2476 }
2477 $code.=<<___;
2478 #endif
2479 ___
2480
2481 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2482
2483 open SELF,$0;
2484 while(<SELF>) {
2485 next if (/^#!/);
2486 last if (!s/^#/@/ and !/^$/);
2487 print;
2488 }
2489 close SELF;
2490
2491 print $code;
2492
2493 close STDOUT or die "error closing STDOUT: $!";