1#! /usr/bin/env perl
2# Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the OpenSSL license (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9
10# ====================================================================
11# Written by Andy Polyakov <appro@openssl.org> for 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 http://www.openssl.org/~appro/cryptogams/.
15#
16# Specific modes and adaptation for Linux kernel by Ard Biesheuvel
17# of Linaro. Permission to use under GPL terms is granted.
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 (for further details see
37# http://www.openssl.org/~appro/Snapdragon-S4.html).
38#
39# Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
40# manages in 20.0 cycles].
41#
42# When comparing to x86_64 results keep in mind that NEON unit is
43# [mostly] single-issue and thus can't [fully] benefit from
44# instruction-level parallelism. And when comparing to aes-armv4
45# results keep in mind key schedule conversion overhead (see
46# bsaes-x86_64.pl for further details)...
47#
48#						<appro@openssl.org>
49
50# April-August 2013
51# Add CBC, CTR and XTS subroutines and adapt for kernel use; courtesy of Ard.
52
53$flavour = shift;
54if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
55else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
56
57if ($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} else {
65    open STDOUT,">$output";
66}
67
68my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
69my @XMM=map("q$_",(0..15));
70
71{
72my ($key,$rounds,$const)=("r4","r5","r6");
73
74sub Dlo()   { shift=~m|q([1]?[0-9])|?"d".($1*2):"";     }
75sub Dhi()   { shift=~m|q([1]?[0-9])|?"d".($1*2+1):"";   }
76
77sub Sbox {
78# input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
79# output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
80my @b=@_[0..7];
81my @t=@_[8..11];
82my @s=@_[12..15];
83	&InBasisChange	(@b);
84	&Inv_GF256	(@b[6,5,0,3,7,1,4,2],@t,@s);
85	&OutBasisChange	(@b[7,1,4,2,6,5,0,3]);
86}
87
88sub InBasisChange {
89# input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
90# output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
91my @b=@_[0..7];
92$code.=<<___;
93	veor	@b[2], @b[2], @b[1]
94	veor	@b[5], @b[5], @b[6]
95	veor	@b[3], @b[3], @b[0]
96	veor	@b[6], @b[6], @b[2]
97	veor	@b[5], @b[5], @b[0]
98
99	veor	@b[6], @b[6], @b[3]
100	veor	@b[3], @b[3], @b[7]
101	veor	@b[7], @b[7], @b[5]
102	veor	@b[3], @b[3], @b[4]
103	veor	@b[4], @b[4], @b[5]
104
105	veor	@b[2], @b[2], @b[7]
106	veor	@b[3], @b[3], @b[1]
107	veor	@b[1], @b[1], @b[5]
108___
109}
110
111sub OutBasisChange {
112# input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
113# output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
114my @b=@_[0..7];
115$code.=<<___;
116	veor	@b[0], @b[0], @b[6]
117	veor	@b[1], @b[1], @b[4]
118	veor	@b[4], @b[4], @b[6]
119	veor	@b[2], @b[2], @b[0]
120	veor	@b[6], @b[6], @b[1]
121
122	veor	@b[1], @b[1], @b[5]
123	veor	@b[5], @b[5], @b[3]
124	veor	@b[3], @b[3], @b[7]
125	veor	@b[7], @b[7], @b[5]
126	veor	@b[2], @b[2], @b[5]
127
128	veor	@b[4], @b[4], @b[7]
129___
130}
131
132sub InvSbox {
133# input in lsb 	> [b0, b1, b2, b3, b4, b5, b6, b7] < msb
134# output in lsb	> [b0, b1, b6, b4, b2, b7, b3, b5] < msb
135my @b=@_[0..7];
136my @t=@_[8..11];
137my @s=@_[12..15];
138	&InvInBasisChange	(@b);
139	&Inv_GF256		(@b[5,1,2,6,3,7,0,4],@t,@s);
140	&InvOutBasisChange	(@b[3,7,0,4,5,1,2,6]);
141}
142
143sub InvInBasisChange {		# OutBasisChange in reverse (with twist)
144my @b=@_[5,1,2,6,3,7,0,4];
145$code.=<<___
146	 veor	@b[1], @b[1], @b[7]
147	veor	@b[4], @b[4], @b[7]
148
149	veor	@b[7], @b[7], @b[5]
150	 veor	@b[1], @b[1], @b[3]
151	veor	@b[2], @b[2], @b[5]
152	veor	@b[3], @b[3], @b[7]
153
154	veor	@b[6], @b[6], @b[1]
155	veor	@b[2], @b[2], @b[0]
156	 veor	@b[5], @b[5], @b[3]
157	veor	@b[4], @b[4], @b[6]
158	veor	@b[0], @b[0], @b[6]
159	veor	@b[1], @b[1], @b[4]
160___
161}
162
163sub InvOutBasisChange {		# InBasisChange in reverse
164my @b=@_[2,5,7,3,6,1,0,4];
165$code.=<<___;
166	veor	@b[1], @b[1], @b[5]
167	veor	@b[2], @b[2], @b[7]
168
169	veor	@b[3], @b[3], @b[1]
170	veor	@b[4], @b[4], @b[5]
171	veor	@b[7], @b[7], @b[5]
172	veor	@b[3], @b[3], @b[4]
173	 veor 	@b[5], @b[5], @b[0]
174	veor	@b[3], @b[3], @b[7]
175	 veor	@b[6], @b[6], @b[2]
176	 veor	@b[2], @b[2], @b[1]
177	veor	@b[6], @b[6], @b[3]
178
179	veor	@b[3], @b[3], @b[0]
180	veor	@b[5], @b[5], @b[6]
181___
182}
183
184sub Mul_GF4 {
185#;*************************************************************
186#;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
187#;*************************************************************
188my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
189$code.=<<___;
190	veor 	$t0, $y0, $y1
191	vand	$t0, $t0, $x0
192	veor	$x0, $x0, $x1
193	vand	$t1, $x1, $y0
194	vand	$x0, $x0, $y1
195	veor	$x1, $t1, $t0
196	veor	$x0, $x0, $t1
197___
198}
199
200sub Mul_GF4_N {				# not used, see next subroutine
201# multiply and scale by N
202my ($x0,$x1,$y0,$y1,$t0)=@_;
203$code.=<<___;
204	veor	$t0, $y0, $y1
205	vand	$t0, $t0, $x0
206	veor	$x0, $x0, $x1
207	vand	$x1, $x1, $y0
208	vand	$x0, $x0, $y1
209	veor	$x1, $x1, $x0
210	veor	$x0, $x0, $t0
211___
212}
213
214sub Mul_GF4_N_GF4 {
215# interleaved Mul_GF4_N and Mul_GF4
216my ($x0,$x1,$y0,$y1,$t0,
217    $x2,$x3,$y2,$y3,$t1)=@_;
218$code.=<<___;
219	veor	$t0, $y0, $y1
220	 veor 	$t1, $y2, $y3
221	vand	$t0, $t0, $x0
222	 vand	$t1, $t1, $x2
223	veor	$x0, $x0, $x1
224	 veor	$x2, $x2, $x3
225	vand	$x1, $x1, $y0
226	 vand	$x3, $x3, $y2
227	vand	$x0, $x0, $y1
228	 vand	$x2, $x2, $y3
229	veor	$x1, $x1, $x0
230	 veor	$x2, $x2, $x3
231	veor	$x0, $x0, $t0
232	 veor	$x3, $x3, $t1
233___
234}
235sub Mul_GF16_2 {
236my @x=@_[0..7];
237my @y=@_[8..11];
238my @t=@_[12..15];
239$code.=<<___;
240	veor	@t[0], @x[0], @x[2]
241	veor	@t[1], @x[1], @x[3]
242___
243	&Mul_GF4  	(@x[0], @x[1], @y[0], @y[1], @t[2..3]);
244$code.=<<___;
245	veor	@y[0], @y[0], @y[2]
246	veor	@y[1], @y[1], @y[3]
247___
248	Mul_GF4_N_GF4	(@t[0], @t[1], @y[0], @y[1], @t[3],
249			 @x[2], @x[3], @y[2], @y[3], @t[2]);
250$code.=<<___;
251	veor	@x[0], @x[0], @t[0]
252	veor	@x[2], @x[2], @t[0]
253	veor	@x[1], @x[1], @t[1]
254	veor	@x[3], @x[3], @t[1]
255
256	veor	@t[0], @x[4], @x[6]
257	veor	@t[1], @x[5], @x[7]
258___
259	&Mul_GF4_N_GF4	(@t[0], @t[1], @y[0], @y[1], @t[3],
260			 @x[6], @x[7], @y[2], @y[3], @t[2]);
261$code.=<<___;
262	veor	@y[0], @y[0], @y[2]
263	veor	@y[1], @y[1], @y[3]
264___
265	&Mul_GF4  	(@x[4], @x[5], @y[0], @y[1], @t[2..3]);
266$code.=<<___;
267	veor	@x[4], @x[4], @t[0]
268	veor	@x[6], @x[6], @t[0]
269	veor	@x[5], @x[5], @t[1]
270	veor	@x[7], @x[7], @t[1]
271___
272}
273sub Inv_GF256 {
274#;********************************************************************
275#;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144)       *
276#;********************************************************************
277my @x=@_[0..7];
278my @t=@_[8..11];
279my @s=@_[12..15];
280# direct optimizations from hardware
281$code.=<<___;
282	veor	@t[3], @x[4], @x[6]
283	veor	@t[2], @x[5], @x[7]
284	veor	@t[1], @x[1], @x[3]
285	veor	@s[1], @x[7], @x[6]
286	 vmov	@t[0], @t[2]
287	veor	@s[0], @x[0], @x[2]
288
289	vorr	@t[2], @t[2], @t[1]
290	veor	@s[3], @t[3], @t[0]
291	vand	@s[2], @t[3], @s[0]
292	vorr	@t[3], @t[3], @s[0]
293	veor	@s[0], @s[0], @t[1]
294	vand	@t[0], @t[0], @t[1]
295	veor	@t[1], @x[3], @x[2]
296	vand	@s[3], @s[3], @s[0]
297	vand	@s[1], @s[1], @t[1]
298	veor	@t[1], @x[4], @x[5]
299	veor	@s[0], @x[1], @x[0]
300	veor	@t[3], @t[3], @s[1]
301	veor	@t[2], @t[2], @s[1]
302	vand	@s[1], @t[1], @s[0]
303	vorr	@t[1], @t[1], @s[0]
304	veor	@t[3], @t[3], @s[3]
305	veor	@t[0], @t[0], @s[1]
306	veor	@t[2], @t[2], @s[2]
307	veor	@t[1], @t[1], @s[3]
308	veor	@t[0], @t[0], @s[2]
309	vand	@s[0], @x[7], @x[3]
310	veor	@t[1], @t[1], @s[2]
311	vand	@s[1], @x[6], @x[2]
312	vand	@s[2], @x[5], @x[1]
313	vorr	@s[3], @x[4], @x[0]
314	veor	@t[3], @t[3], @s[0]
315	veor	@t[1], @t[1], @s[2]
316	veor	@t[0], @t[0], @s[3]
317	veor	@t[2], @t[2], @s[1]
318
319	@ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
320
321	@ new smaller inversion
322
323	vand	@s[2], @t[3], @t[1]
324	vmov	@s[0], @t[0]
325
326	veor	@s[1], @t[2], @s[2]
327	veor	@s[3], @t[0], @s[2]
328	veor	@s[2], @t[0], @s[2]	@ @s[2]=@s[3]
329
330	vbsl	@s[1], @t[1], @t[0]
331	vbsl	@s[3], @t[3], @t[2]
332	veor	@t[3], @t[3], @t[2]
333
334	vbsl	@s[0], @s[1], @s[2]
335	vbsl	@t[0], @s[2], @s[1]
336
337	vand	@s[2], @s[0], @s[3]
338	veor	@t[1], @t[1], @t[0]
339
340	veor	@s[2], @s[2], @t[3]
341___
342# output in s3, s2, s1, t1
343
344# Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
345
346# Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
347	&Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
348
349### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
350}
351
352# AES linear components
353
354sub ShiftRows {
355my @x=@_[0..7];
356my @t=@_[8..11];
357my $mask=pop;
358$code.=<<___;
359	vldmia	$key!, {@t[0]-@t[3]}
360	veor	@t[0], @t[0], @x[0]
361	veor	@t[1], @t[1], @x[1]
362	vtbl.8	`&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
363	vtbl.8	`&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
364	vldmia	$key!, {@t[0]}
365	veor	@t[2], @t[2], @x[2]
366	vtbl.8	`&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
367	vtbl.8	`&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
368	vldmia	$key!, {@t[1]}
369	veor	@t[3], @t[3], @x[3]
370	vtbl.8	`&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
371	vtbl.8	`&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
372	vldmia	$key!, {@t[2]}
373	vtbl.8	`&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
374	vtbl.8	`&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
375	vldmia	$key!, {@t[3]}
376	veor	@t[0], @t[0], @x[4]
377	veor	@t[1], @t[1], @x[5]
378	vtbl.8	`&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
379	vtbl.8	`&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
380	veor	@t[2], @t[2], @x[6]
381	vtbl.8	`&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
382	vtbl.8	`&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
383	veor	@t[3], @t[3], @x[7]
384	vtbl.8	`&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
385	vtbl.8	`&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
386	vtbl.8	`&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
387	vtbl.8	`&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
388___
389}
390
391sub MixColumns {
392# modified to emit output in order suitable for feeding back to aesenc[last]
393my @x=@_[0..7];
394my @t=@_[8..15];
395my $inv=@_[16];	# optional
396$code.=<<___;
397	vext.8	@t[0], @x[0], @x[0], #12	@ x0 <<< 32
398	vext.8	@t[1], @x[1], @x[1], #12
399	 veor	@x[0], @x[0], @t[0]		@ x0 ^ (x0 <<< 32)
400	vext.8	@t[2], @x[2], @x[2], #12
401	 veor	@x[1], @x[1], @t[1]
402	vext.8	@t[3], @x[3], @x[3], #12
403	 veor	@x[2], @x[2], @t[2]
404	vext.8	@t[4], @x[4], @x[4], #12
405	 veor	@x[3], @x[3], @t[3]
406	vext.8	@t[5], @x[5], @x[5], #12
407	 veor	@x[4], @x[4], @t[4]
408	vext.8	@t[6], @x[6], @x[6], #12
409	 veor	@x[5], @x[5], @t[5]
410	vext.8	@t[7], @x[7], @x[7], #12
411	 veor	@x[6], @x[6], @t[6]
412
413	veor	@t[1], @t[1], @x[0]
414	 veor	@x[7], @x[7], @t[7]
415	 vext.8	@x[0], @x[0], @x[0], #8		@ (x0 ^ (x0 <<< 32)) <<< 64)
416	veor	@t[2], @t[2], @x[1]
417	veor	@t[0], @t[0], @x[7]
418	veor	@t[1], @t[1], @x[7]
419	 vext.8	@x[1], @x[1], @x[1], #8
420	veor	@t[5], @t[5], @x[4]
421	 veor	@x[0], @x[0], @t[0]
422	veor	@t[6], @t[6], @x[5]
423	 veor	@x[1], @x[1], @t[1]
424	 vext.8	@t[0], @x[4], @x[4], #8
425	veor	@t[4], @t[4], @x[3]
426	 vext.8	@t[1], @x[5], @x[5], #8
427	veor	@t[7], @t[7], @x[6]
428	 vext.8	@x[4], @x[3], @x[3], #8
429	veor	@t[3], @t[3], @x[2]
430	 vext.8	@x[5], @x[7], @x[7], #8
431	veor	@t[4], @t[4], @x[7]
432	 vext.8	@x[3], @x[6], @x[6], #8
433	veor	@t[3], @t[3], @x[7]
434	 vext.8	@x[6], @x[2], @x[2], #8
435	veor	@x[7], @t[1], @t[5]
436___
437$code.=<<___ if (!$inv);
438	veor	@x[2], @t[0], @t[4]
439	veor	@x[4], @x[4], @t[3]
440	veor	@x[5], @x[5], @t[7]
441	veor	@x[3], @x[3], @t[6]
442	 @ vmov	@x[2], @t[0]
443	veor	@x[6], @x[6], @t[2]
444	 @ vmov	@x[7], @t[1]
445___
446$code.=<<___ if ($inv);
447	veor	@t[3], @t[3], @x[4]
448	veor	@x[5], @x[5], @t[7]
449	veor	@x[2], @x[3], @t[6]
450	veor	@x[3], @t[0], @t[4]
451	veor	@x[4], @x[6], @t[2]
452	vmov	@x[6], @t[3]
453	 @ vmov	@x[7], @t[1]
454___
455}
456
457sub InvMixColumns_orig {
458my @x=@_[0..7];
459my @t=@_[8..15];
460
461$code.=<<___;
462	@ multiplication by 0x0e
463	vext.8	@t[7], @x[7], @x[7], #12
464	vmov	@t[2], @x[2]
465	veor	@x[2], @x[2], @x[5]		@ 2 5
466	veor	@x[7], @x[7], @x[5]		@ 7 5
467	vext.8	@t[0], @x[0], @x[0], #12
468	vmov	@t[5], @x[5]
469	veor	@x[5], @x[5], @x[0]		@ 5 0		[1]
470	veor	@x[0], @x[0], @x[1]		@ 0 1
471	vext.8	@t[1], @x[1], @x[1], #12
472	veor	@x[1], @x[1], @x[2]		@ 1 25
473	veor	@x[0], @x[0], @x[6]		@ 01 6		[2]
474	vext.8	@t[3], @x[3], @x[3], #12
475	veor	@x[1], @x[1], @x[3]		@ 125 3		[4]
476	veor	@x[2], @x[2], @x[0]		@ 25 016	[3]
477	veor	@x[3], @x[3], @x[7]		@ 3 75
478	veor	@x[7], @x[7], @x[6]		@ 75 6		[0]
479	vext.8	@t[6], @x[6], @x[6], #12
480	vmov	@t[4], @x[4]
481	veor	@x[6], @x[6], @x[4]		@ 6 4
482	veor	@x[4], @x[4], @x[3]		@ 4 375		[6]
483	veor	@x[3], @x[3], @x[7]		@ 375 756=36
484	veor	@x[6], @x[6], @t[5]		@ 64 5		[7]
485	veor	@x[3], @x[3], @t[2]		@ 36 2
486	vext.8	@t[5], @t[5], @t[5], #12
487	veor	@x[3], @x[3], @t[4]		@ 362 4		[5]
488___
489					my @y = @x[7,5,0,2,1,3,4,6];
490$code.=<<___;
491	@ multiplication by 0x0b
492	veor	@y[1], @y[1], @y[0]
493	veor	@y[0], @y[0], @t[0]
494	vext.8	@t[2], @t[2], @t[2], #12
495	veor	@y[1], @y[1], @t[1]
496	veor	@y[0], @y[0], @t[5]
497	vext.8	@t[4], @t[4], @t[4], #12
498	veor	@y[1], @y[1], @t[6]
499	veor	@y[0], @y[0], @t[7]
500	veor	@t[7], @t[7], @t[6]		@ clobber t[7]
501
502	veor	@y[3], @y[3], @t[0]
503	 veor	@y[1], @y[1], @y[0]
504	vext.8	@t[0], @t[0], @t[0], #12
505	veor	@y[2], @y[2], @t[1]
506	veor	@y[4], @y[4], @t[1]
507	vext.8	@t[1], @t[1], @t[1], #12
508	veor	@y[2], @y[2], @t[2]
509	veor	@y[3], @y[3], @t[2]
510	veor	@y[5], @y[5], @t[2]
511	veor	@y[2], @y[2], @t[7]
512	vext.8	@t[2], @t[2], @t[2], #12
513	veor	@y[3], @y[3], @t[3]
514	veor	@y[6], @y[6], @t[3]
515	veor	@y[4], @y[4], @t[3]
516	veor	@y[7], @y[7], @t[4]
517	vext.8	@t[3], @t[3], @t[3], #12
518	veor	@y[5], @y[5], @t[4]
519	veor	@y[7], @y[7], @t[7]
520	veor	@t[7], @t[7], @t[5]		@ clobber t[7] even more
521	veor	@y[3], @y[3], @t[5]
522	veor	@y[4], @y[4], @t[4]
523
524	veor	@y[5], @y[5], @t[7]
525	vext.8	@t[4], @t[4], @t[4], #12
526	veor	@y[6], @y[6], @t[7]
527	veor	@y[4], @y[4], @t[7]
528
529	veor	@t[7], @t[7], @t[5]
530	vext.8	@t[5], @t[5], @t[5], #12
531
532	@ multiplication by 0x0d
533	veor	@y[4], @y[4], @y[7]
534	 veor	@t[7], @t[7], @t[6]		@ restore t[7]
535	veor	@y[7], @y[7], @t[4]
536	vext.8	@t[6], @t[6], @t[6], #12
537	veor	@y[2], @y[2], @t[0]
538	veor	@y[7], @y[7], @t[5]
539	vext.8	@t[7], @t[7], @t[7], #12
540	veor	@y[2], @y[2], @t[2]
541
542	veor	@y[3], @y[3], @y[1]
543	veor	@y[1], @y[1], @t[1]
544	veor	@y[0], @y[0], @t[0]
545	veor	@y[3], @y[3], @t[0]
546	veor	@y[1], @y[1], @t[5]
547	veor	@y[0], @y[0], @t[5]
548	vext.8	@t[0], @t[0], @t[0], #12
549	veor	@y[1], @y[1], @t[7]
550	veor	@y[0], @y[0], @t[6]
551	veor	@y[3], @y[3], @y[1]
552	veor	@y[4], @y[4], @t[1]
553	vext.8	@t[1], @t[1], @t[1], #12
554
555	veor	@y[7], @y[7], @t[7]
556	veor	@y[4], @y[4], @t[2]
557	veor	@y[5], @y[5], @t[2]
558	veor	@y[2], @y[2], @t[6]
559	veor	@t[6], @t[6], @t[3]		@ clobber t[6]
560	vext.8	@t[2], @t[2], @t[2], #12
561	veor	@y[4], @y[4], @y[7]
562	veor	@y[3], @y[3], @t[6]
563
564	veor	@y[6], @y[6], @t[6]
565	veor	@y[5], @y[5], @t[5]
566	vext.8	@t[5], @t[5], @t[5], #12
567	veor	@y[6], @y[6], @t[4]
568	vext.8	@t[4], @t[4], @t[4], #12
569	veor	@y[5], @y[5], @t[6]
570	veor	@y[6], @y[6], @t[7]
571	vext.8	@t[7], @t[7], @t[7], #12
572	veor	@t[6], @t[6], @t[3]		@ restore t[6]
573	vext.8	@t[3], @t[3], @t[3], #12
574
575	@ multiplication by 0x09
576	veor	@y[4], @y[4], @y[1]
577	veor	@t[1], @t[1], @y[1]		@ t[1]=y[1]
578	veor	@t[0], @t[0], @t[5]		@ clobber t[0]
579	vext.8	@t[6], @t[6], @t[6], #12
580	veor	@t[1], @t[1], @t[5]
581	veor	@y[3], @y[3], @t[0]
582	veor	@t[0], @t[0], @y[0]		@ t[0]=y[0]
583	veor	@t[1], @t[1], @t[6]
584	veor	@t[6], @t[6], @t[7]		@ clobber t[6]
585	veor	@y[4], @y[4], @t[1]
586	veor	@y[7], @y[7], @t[4]
587	veor	@y[6], @y[6], @t[3]
588	veor	@y[5], @y[5], @t[2]
589	veor	@t[4], @t[4], @y[4]		@ t[4]=y[4]
590	veor	@t[3], @t[3], @y[3]		@ t[3]=y[3]
591	veor	@t[5], @t[5], @y[5]		@ t[5]=y[5]
592	veor	@t[2], @t[2], @y[2]		@ t[2]=y[2]
593	veor	@t[3], @t[3], @t[7]
594	veor	@XMM[5], @t[5], @t[6]
595	veor	@XMM[6], @t[6], @y[6]		@ t[6]=y[6]
596	veor	@XMM[2], @t[2], @t[6]
597	veor	@XMM[7], @t[7], @y[7]		@ t[7]=y[7]
598
599	vmov	@XMM[0], @t[0]
600	vmov	@XMM[1], @t[1]
601	@ vmov	@XMM[2], @t[2]
602	vmov	@XMM[3], @t[3]
603	vmov	@XMM[4], @t[4]
604	@ vmov	@XMM[5], @t[5]
605	@ vmov	@XMM[6], @t[6]
606	@ vmov	@XMM[7], @t[7]
607___
608}
609
610sub InvMixColumns {
611my @x=@_[0..7];
612my @t=@_[8..15];
613
614# Thanks to Jussi Kivilinna for providing pointer to
615#
616# | 0e 0b 0d 09 |   | 02 03 01 01 |   | 05 00 04 00 |
617# | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
618# | 0d 09 0e 0b |   | 01 01 02 03 |   | 04 00 05 00 |
619# | 0b 0d 09 0e |   | 03 01 01 02 |   | 00 04 00 05 |
620
621$code.=<<___;
622	@ multiplication by 0x05-0x00-0x04-0x00
623	vext.8	@t[0], @x[0], @x[0], #8
624	vext.8	@t[6], @x[6], @x[6], #8
625	vext.8	@t[7], @x[7], @x[7], #8
626	veor	@t[0], @t[0], @x[0]
627	vext.8	@t[1], @x[1], @x[1], #8
628	veor	@t[6], @t[6], @x[6]
629	vext.8	@t[2], @x[2], @x[2], #8
630	veor	@t[7], @t[7], @x[7]
631	vext.8	@t[3], @x[3], @x[3], #8
632	veor	@t[1], @t[1], @x[1]
633	vext.8	@t[4], @x[4], @x[4], #8
634	veor	@t[2], @t[2], @x[2]
635	vext.8	@t[5], @x[5], @x[5], #8
636	veor	@t[3], @t[3], @x[3]
637	veor	@t[4], @t[4], @x[4]
638	veor	@t[5], @t[5], @x[5]
639
640	 veor	@x[0], @x[0], @t[6]
641	 veor	@x[1], @x[1], @t[6]
642	 veor	@x[2], @x[2], @t[0]
643	 veor	@x[4], @x[4], @t[2]
644	 veor	@x[3], @x[3], @t[1]
645	 veor	@x[1], @x[1], @t[7]
646	 veor	@x[2], @x[2], @t[7]
647	 veor	@x[4], @x[4], @t[6]
648	 veor	@x[5], @x[5], @t[3]
649	 veor	@x[3], @x[3], @t[6]
650	 veor	@x[6], @x[6], @t[4]
651	 veor	@x[4], @x[4], @t[7]
652	 veor	@x[5], @x[5], @t[7]
653	 veor	@x[7], @x[7], @t[5]
654___
655	&MixColumns	(@x,@t,1);	# flipped 2<->3 and 4<->6
656}
657
658sub swapmove {
659my ($a,$b,$n,$mask,$t)=@_;
660$code.=<<___;
661	vshr.u64	$t, $b, #$n
662	veor		$t, $t, $a
663	vand		$t, $t, $mask
664	veor		$a, $a, $t
665	vshl.u64	$t, $t, #$n
666	veor		$b, $b, $t
667___
668}
669sub swapmove2x {
670my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
671$code.=<<___;
672	vshr.u64	$t0, $b0, #$n
673	 vshr.u64	$t1, $b1, #$n
674	veor		$t0, $t0, $a0
675	 veor		$t1, $t1, $a1
676	vand		$t0, $t0, $mask
677	 vand		$t1, $t1, $mask
678	veor		$a0, $a0, $t0
679	vshl.u64	$t0, $t0, #$n
680	 veor		$a1, $a1, $t1
681	 vshl.u64	$t1, $t1, #$n
682	veor		$b0, $b0, $t0
683	 veor		$b1, $b1, $t1
684___
685}
686
687sub bitslice {
688my @x=reverse(@_[0..7]);
689my ($t0,$t1,$t2,$t3)=@_[8..11];
690$code.=<<___;
691	vmov.i8	$t0,#0x55			@ compose .LBS0
692	vmov.i8	$t1,#0x33			@ compose .LBS1
693___
694	&swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
695	&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
696$code.=<<___;
697	vmov.i8	$t0,#0x0f			@ compose .LBS2
698___
699	&swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
700	&swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
701
702	&swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
703	&swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
704}
705
706$code.=<<___;
707#ifndef __KERNEL__
708# include <GFp/arm_arch.h>
709
710# define VFP_ABI_PUSH	vstmdb	sp!,{d8-d15}
711# define VFP_ABI_POP	vldmia	sp!,{d8-d15}
712# define VFP_ABI_FRAME	0x40
713#else
714# define VFP_ABI_PUSH
715# define VFP_ABI_POP
716# define VFP_ABI_FRAME	0
717# define BSAES_ASM_EXTENDED_KEY
718# define __ARM_ARCH__ __LINUX_ARM_ARCH__
719# define __ARM_MAX_ARCH__ 7
720#endif
721
722#ifdef __thumb__
723# define adrl adr
724#endif
725
726#if __ARM_MAX_ARCH__>=7
727.arch	armv7-a
728.fpu	neon
729
730.text
731.syntax	unified 	@ ARMv7-capable assembler is expected to handle this
732#if defined(__thumb2__) && !defined(__APPLE__)
733.thumb
734#else
735.code   32
736# undef __thumb2__
737#endif
738
739.type	_bsaes_const,%object
740.align	6
741_bsaes_const:
742.LM0ISR:	@ InvShiftRows constants
743	.quad	0x0a0e0206070b0f03, 0x0004080c0d010509
744.LISR:
745	.quad	0x0504070602010003, 0x0f0e0d0c080b0a09
746.LISRM0:
747	.quad	0x01040b0e0205080f, 0x0306090c00070a0d
748.LM0SR:		@ ShiftRows constants
749	.quad	0x0a0e02060f03070b, 0x0004080c05090d01
750.LSR:
751	.quad	0x0504070600030201, 0x0f0e0d0c0a09080b
752.LSRM0:
753	.quad	0x0304090e00050a0f, 0x01060b0c0207080d
754.LM0:
755	.quad	0x02060a0e03070b0f, 0x0004080c0105090d
756.LREVM0SR:
757	.quad	0x090d01050c000408, 0x03070b0f060a0e02
758.asciz	"Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
759.align	6
760.size	_bsaes_const,.-_bsaes_const
761
762.type	_bsaes_encrypt8,%function
763.align	4
764_bsaes_encrypt8:
765	adr	$const,.
766	vldmia	$key!, {@XMM[9]}		@ round 0 key
767#if defined(__thumb2__) || defined(__APPLE__)
768	adr	$const,.LM0SR
769#else
770	sub	$const,$const,#_bsaes_encrypt8-.LM0SR
771#endif
772
773	vldmia	$const!, {@XMM[8]}		@ .LM0SR
774_bsaes_encrypt8_alt:
775	veor	@XMM[10], @XMM[0], @XMM[9]	@ xor with round0 key
776	veor	@XMM[11], @XMM[1], @XMM[9]
777	 vtbl.8	`&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
778	 vtbl.8	`&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
779	veor	@XMM[12], @XMM[2], @XMM[9]
780	 vtbl.8	`&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
781	 vtbl.8	`&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
782	veor	@XMM[13], @XMM[3], @XMM[9]
783	 vtbl.8	`&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
784	 vtbl.8	`&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
785	veor	@XMM[14], @XMM[4], @XMM[9]
786	 vtbl.8	`&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
787	 vtbl.8	`&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
788	veor	@XMM[15], @XMM[5], @XMM[9]
789	 vtbl.8	`&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
790	 vtbl.8	`&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
791	veor	@XMM[10], @XMM[6], @XMM[9]
792	 vtbl.8	`&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
793	 vtbl.8	`&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
794	veor	@XMM[11], @XMM[7], @XMM[9]
795	 vtbl.8	`&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
796	 vtbl.8	`&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
797	 vtbl.8	`&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
798	 vtbl.8	`&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
799_bsaes_encrypt8_bitslice:
800___
801	&bitslice	(@XMM[0..7, 8..11]);
802$code.=<<___;
803	sub	$rounds,$rounds,#1
804	b	.Lenc_sbox
805.align	4
806.Lenc_loop:
807___
808	&ShiftRows	(@XMM[0..7, 8..12]);
809$code.=".Lenc_sbox:\n";
810	&Sbox		(@XMM[0..7, 8..15]);
811$code.=<<___;
812	subs	$rounds,$rounds,#1
813	bcc	.Lenc_done
814___
815	&MixColumns	(@XMM[0,1,4,6,3,7,2,5, 8..15]);
816$code.=<<___;
817	vldmia	$const, {@XMM[12]}		@ .LSR
818	ite	eq				@ Thumb2 thing, samity check in ARM
819	addeq	$const,$const,#0x10
820	bne	.Lenc_loop
821	vldmia	$const, {@XMM[12]}		@ .LSRM0
822	b	.Lenc_loop
823.align	4
824.Lenc_done:
825___
826	# output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
827	&bitslice	(@XMM[0,1,4,6,3,7,2,5, 8..11]);
828$code.=<<___;
829	vldmia	$key, {@XMM[8]}			@ last round key
830	veor	@XMM[4], @XMM[4], @XMM[8]
831	veor	@XMM[6], @XMM[6], @XMM[8]
832	veor	@XMM[3], @XMM[3], @XMM[8]
833	veor	@XMM[7], @XMM[7], @XMM[8]
834	veor	@XMM[2], @XMM[2], @XMM[8]
835	veor	@XMM[5], @XMM[5], @XMM[8]
836	veor	@XMM[0], @XMM[0], @XMM[8]
837	veor	@XMM[1], @XMM[1], @XMM[8]
838	bx	lr
839.size	_bsaes_encrypt8,.-_bsaes_encrypt8
840___
841}
842{
843my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
844
845sub bitslice_key {
846my @x=reverse(@_[0..7]);
847my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
848
849	&swapmove	(@x[0,1],1,$bs0,$t2,$t3);
850$code.=<<___;
851	@ &swapmove(@x[2,3],1,$t0,$t2,$t3);
852	vmov	@x[2], @x[0]
853	vmov	@x[3], @x[1]
854___
855	#&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
856
857	&swapmove2x	(@x[0,2,1,3],2,$bs1,$t2,$t3);
858$code.=<<___;
859	@ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
860	vmov	@x[4], @x[0]
861	vmov	@x[6], @x[2]
862	vmov	@x[5], @x[1]
863	vmov	@x[7], @x[3]
864___
865	&swapmove2x	(@x[0,4,1,5],4,$bs2,$t2,$t3);
866	&swapmove2x	(@x[2,6,3,7],4,$bs2,$t2,$t3);
867}
868
869$code.=<<___;
870.type	_bsaes_key_convert,%function
871.align	4
872_bsaes_key_convert:
873	adr	$const,.
874	vld1.8	{@XMM[7]},  [$inp]!		@ load round 0 key
875#if defined(__thumb2__) || defined(__APPLE__)
876	adr	$const,.LM0
877#else
878	sub	$const,$const,#_bsaes_key_convert-.LM0
879#endif
880	vld1.8	{@XMM[15]}, [$inp]!		@ load round 1 key
881
882	vmov.i8	@XMM[8],  #0x01			@ bit masks
883	vmov.i8	@XMM[9],  #0x02
884	vmov.i8	@XMM[10], #0x04
885	vmov.i8	@XMM[11], #0x08
886	vmov.i8	@XMM[12], #0x10
887	vmov.i8	@XMM[13], #0x20
888	vldmia	$const, {@XMM[14]}		@ .LM0
889
890#ifdef __ARMEL__
891	vrev32.8	@XMM[7],  @XMM[7]
892	vrev32.8	@XMM[15], @XMM[15]
893#endif
894	sub	$rounds,$rounds,#1
895	vstmia	$out!, {@XMM[7]}		@ save round 0 key
896	b	.Lkey_loop
897
898.align	4
899.Lkey_loop:
900	vtbl.8	`&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
901	vtbl.8	`&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
902	vmov.i8	@XMM[6],  #0x40
903	vmov.i8	@XMM[15], #0x80
904
905	vtst.8	@XMM[0], @XMM[7], @XMM[8]
906	vtst.8	@XMM[1], @XMM[7], @XMM[9]
907	vtst.8	@XMM[2], @XMM[7], @XMM[10]
908	vtst.8	@XMM[3], @XMM[7], @XMM[11]
909	vtst.8	@XMM[4], @XMM[7], @XMM[12]
910	vtst.8	@XMM[5], @XMM[7], @XMM[13]
911	vtst.8	@XMM[6], @XMM[7], @XMM[6]
912	vtst.8	@XMM[7], @XMM[7], @XMM[15]
913	vld1.8	{@XMM[15]}, [$inp]!		@ load next round key
914	vmvn	@XMM[0], @XMM[0]		@ "pnot"
915	vmvn	@XMM[1], @XMM[1]
916	vmvn	@XMM[5], @XMM[5]
917	vmvn	@XMM[6], @XMM[6]
918#ifdef __ARMEL__
919	vrev32.8	@XMM[15], @XMM[15]
920#endif
921	subs	$rounds,$rounds,#1
922	vstmia	$out!,{@XMM[0]-@XMM[7]}		@ write bit-sliced round key
923	bne	.Lkey_loop
924
925	vmov.i8	@XMM[7],#0x63			@ compose .L63
926	@ don't save last round key
927	bx	lr
928.size	_bsaes_key_convert,.-_bsaes_key_convert
929___
930}
931
932{
933my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
934my $const = "r6";	# shared with _bsaes_encrypt8_alt
935my $keysched = "sp";
936
937$code.=<<___;
938.extern	GFp_aes_nohw_encrypt
939.global	GFp_bsaes_ctr32_encrypt_blocks
940.type	GFp_bsaes_ctr32_encrypt_blocks,%function
941.align	5
942GFp_bsaes_ctr32_encrypt_blocks:
943	cmp	$len, #8			@ use plain AES for
944	blo	.Lctr_enc_short			@ small sizes
945
946	mov	ip, sp
947	stmdb	sp!, {r4-r10, lr}
948	VFP_ABI_PUSH
949	ldr	$ctr, [ip]			@ ctr is 1st arg on the stack
950	sub	sp, sp, #0x10			@ scratch space to carry over the ctr
951	mov	$fp, sp				@ save sp
952
953	ldr	$rounds, [$key, #240]		@ get # of rounds
954#ifndef	BSAES_ASM_EXTENDED_KEY
955	@ allocate the key schedule on the stack
956	sub	r12, sp, $rounds, lsl#7		@ 128 bytes per inner round key
957	add	r12, #`128-32`			@ size of bit-sliced key schedule
958
959	@ populate the key schedule
960	mov	r4, $key			@ pass key
961	mov	r5, $rounds			@ pass # of rounds
962	mov	sp, r12				@ sp is $keysched
963	bl	_bsaes_key_convert
964	veor	@XMM[7],@XMM[7],@XMM[15]	@ fix up last round key
965	vstmia	r12, {@XMM[7]}			@ save last round key
966
967	vld1.8	{@XMM[0]}, [$ctr]		@ load counter
968#ifdef	__APPLE__
969	mov	$ctr, #:lower16:(.LREVM0SR-.LM0)
970	add	$ctr, $const, $ctr
971#else
972	add	$ctr, $const, #.LREVM0SR-.LM0	@ borrow $ctr
973#endif
974	vldmia	$keysched, {@XMM[4]}		@ load round0 key
975#else
976	ldr	r12, [$key, #244]
977	eors	r12, #1
978	beq	0f
979
980	@ populate the key schedule
981	str	r12, [$key, #244]
982	mov	r4, $key			@ pass key
983	mov	r5, $rounds			@ pass # of rounds
984	add	r12, $key, #248			@ pass key schedule
985	bl	_bsaes_key_convert
986	veor	@XMM[7],@XMM[7],@XMM[15]	@ fix up last round key
987	vstmia	r12, {@XMM[7]}			@ save last round key
988
989.align	2
9900:	add	r12, $key, #248
991	vld1.8	{@XMM[0]}, [$ctr]		@ load counter
992	adrl	$ctr, .LREVM0SR			@ borrow $ctr
993	vldmia	r12, {@XMM[4]}			@ load round0 key
994	sub	sp, #0x10			@ place for adjusted round0 key
995#endif
996
997	vmov.i32	@XMM[8],#1		@ compose 1<<96
998	veor		@XMM[9],@XMM[9],@XMM[9]
999	vrev32.8	@XMM[0],@XMM[0]
1000	vext.8		@XMM[8],@XMM[9],@XMM[8],#4
1001	vrev32.8	@XMM[4],@XMM[4]
1002	vadd.u32	@XMM[9],@XMM[8],@XMM[8]	@ compose 2<<96
1003	vstmia	$keysched, {@XMM[4]}		@ save adjusted round0 key
1004	b	.Lctr_enc_loop
1005
1006.align	4
1007.Lctr_enc_loop:
1008	vadd.u32	@XMM[10], @XMM[8], @XMM[9]	@ compose 3<<96
1009	vadd.u32	@XMM[1], @XMM[0], @XMM[8]	@ +1
1010	vadd.u32	@XMM[2], @XMM[0], @XMM[9]	@ +2
1011	vadd.u32	@XMM[3], @XMM[0], @XMM[10]	@ +3
1012	vadd.u32	@XMM[4], @XMM[1], @XMM[10]
1013	vadd.u32	@XMM[5], @XMM[2], @XMM[10]
1014	vadd.u32	@XMM[6], @XMM[3], @XMM[10]
1015	vadd.u32	@XMM[7], @XMM[4], @XMM[10]
1016	vadd.u32	@XMM[10], @XMM[5], @XMM[10]	@ next counter
1017
1018	@ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1019	@ to flip byte order in 32-bit counter
1020
1021	vldmia		$keysched, {@XMM[9]}		@ load round0 key
1022#ifndef	BSAES_ASM_EXTENDED_KEY
1023	add		r4, $keysched, #0x10		@ pass next round key
1024#else
1025	add		r4, $key, #`248+16`
1026#endif
1027	vldmia		$ctr, {@XMM[8]}			@ .LREVM0SR
1028	mov		r5, $rounds			@ pass rounds
1029	vstmia		$fp, {@XMM[10]}			@ save next counter
1030#ifdef	__APPLE__
1031	mov		$const, #:lower16:(.LREVM0SR-.LSR)
1032	sub		$const, $ctr, $const
1033#else
1034	sub		$const, $ctr, #.LREVM0SR-.LSR	@ pass constants
1035#endif
1036
1037	bl		_bsaes_encrypt8_alt
1038
1039	subs		$len, $len, #8
1040	blo		.Lctr_enc_loop_done
1041
1042	vld1.8		{@XMM[8]-@XMM[9]}, [$inp]!	@ load input
1043	vld1.8		{@XMM[10]-@XMM[11]}, [$inp]!
1044	veor		@XMM[0], @XMM[8]
1045	veor		@XMM[1], @XMM[9]
1046	vld1.8		{@XMM[12]-@XMM[13]}, [$inp]!
1047	veor		@XMM[4], @XMM[10]
1048	veor		@XMM[6], @XMM[11]
1049	vld1.8		{@XMM[14]-@XMM[15]}, [$inp]!
1050	veor		@XMM[3], @XMM[12]
1051	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1052	veor		@XMM[7], @XMM[13]
1053	veor		@XMM[2], @XMM[14]
1054	vst1.8		{@XMM[4]}, [$out]!
1055	veor		@XMM[5], @XMM[15]
1056	vst1.8		{@XMM[6]}, [$out]!
1057	vmov.i32	@XMM[8], #1			@ compose 1<<96
1058	vst1.8		{@XMM[3]}, [$out]!
1059	veor		@XMM[9], @XMM[9], @XMM[9]
1060	vst1.8		{@XMM[7]}, [$out]!
1061	vext.8		@XMM[8], @XMM[9], @XMM[8], #4
1062	vst1.8		{@XMM[2]}, [$out]!
1063	vadd.u32	@XMM[9],@XMM[8],@XMM[8]		@ compose 2<<96
1064	vst1.8		{@XMM[5]}, [$out]!
1065	vldmia		$fp, {@XMM[0]}			@ load counter
1066
1067	bne		.Lctr_enc_loop
1068	b		.Lctr_enc_done
1069
1070.align	4
1071.Lctr_enc_loop_done:
1072	add		$len, $len, #8
1073	vld1.8		{@XMM[8]}, [$inp]!	@ load input
1074	veor		@XMM[0], @XMM[8]
1075	vst1.8		{@XMM[0]}, [$out]!	@ write output
1076	cmp		$len, #2
1077	blo		.Lctr_enc_done
1078	vld1.8		{@XMM[9]}, [$inp]!
1079	veor		@XMM[1], @XMM[9]
1080	vst1.8		{@XMM[1]}, [$out]!
1081	beq		.Lctr_enc_done
1082	vld1.8		{@XMM[10]}, [$inp]!
1083	veor		@XMM[4], @XMM[10]
1084	vst1.8		{@XMM[4]}, [$out]!
1085	cmp		$len, #4
1086	blo		.Lctr_enc_done
1087	vld1.8		{@XMM[11]}, [$inp]!
1088	veor		@XMM[6], @XMM[11]
1089	vst1.8		{@XMM[6]}, [$out]!
1090	beq		.Lctr_enc_done
1091	vld1.8		{@XMM[12]}, [$inp]!
1092	veor		@XMM[3], @XMM[12]
1093	vst1.8		{@XMM[3]}, [$out]!
1094	cmp		$len, #6
1095	blo		.Lctr_enc_done
1096	vld1.8		{@XMM[13]}, [$inp]!
1097	veor		@XMM[7], @XMM[13]
1098	vst1.8		{@XMM[7]}, [$out]!
1099	beq		.Lctr_enc_done
1100	vld1.8		{@XMM[14]}, [$inp]
1101	veor		@XMM[2], @XMM[14]
1102	vst1.8		{@XMM[2]}, [$out]!
1103
1104.Lctr_enc_done:
1105	vmov.i32	q0, #0
1106	vmov.i32	q1, #0
1107#ifndef	BSAES_ASM_EXTENDED_KEY
1108.Lctr_enc_bzero:			@ wipe key schedule [if any]
1109	vstmia		$keysched!, {q0-q1}
1110	cmp		$keysched, $fp
1111	bne		.Lctr_enc_bzero
1112#else
1113	vstmia		$keysched, {q0-q1}
1114#endif
1115
1116	mov	sp, $fp
1117	add	sp, #0x10		@ add sp,$fp,#0x10 is no good for thumb
1118	VFP_ABI_POP
1119	ldmia	sp!, {r4-r10, pc}	@ return
1120
1121.align	4
1122.Lctr_enc_short:
1123	ldr	ip, [sp]		@ ctr pointer is passed on stack
1124	stmdb	sp!, {r4-r8, lr}
1125
1126	mov	r4, $inp		@ copy arguments
1127	mov	r5, $out
1128	mov	r6, $len
1129	mov	r7, $key
1130	ldr	r8, [ip, #12]		@ load counter LSW
1131	vld1.8	{@XMM[1]}, [ip]		@ load whole counter value
1132#ifdef __ARMEL__
1133	rev	r8, r8
1134#endif
1135	sub	sp, sp, #0x10
1136	vst1.8	{@XMM[1]}, [sp]		@ copy counter value
1137	sub	sp, sp, #0x10
1138
1139.Lctr_enc_short_loop:
1140	add	r0, sp, #0x10		@ input counter value
1141	mov	r1, sp			@ output on the stack
1142	mov	r2, r7			@ key
1143
1144	bl	GFp_aes_nohw_encrypt
1145
1146	vld1.8	{@XMM[0]}, [r4]!	@ load input
1147	vld1.8	{@XMM[1]}, [sp]		@ load encrypted counter
1148	add	r8, r8, #1
1149#ifdef __ARMEL__
1150	rev	r0, r8
1151	str	r0, [sp, #0x1c]		@ next counter value
1152#else
1153	str	r8, [sp, #0x1c]		@ next counter value
1154#endif
1155	veor	@XMM[0],@XMM[0],@XMM[1]
1156	vst1.8	{@XMM[0]}, [r5]!	@ store output
1157	subs	r6, r6, #1
1158	bne	.Lctr_enc_short_loop
1159
1160	vmov.i32	q0, #0
1161	vmov.i32	q1, #0
1162	vstmia		sp!, {q0-q1}
1163
1164	ldmia	sp!, {r4-r8, pc}
1165.size	GFp_bsaes_ctr32_encrypt_blocks,.-GFp_bsaes_ctr32_encrypt_blocks
1166___
1167}
1168$code.=<<___;
1169#endif
1170___
1171
1172$code =~ s/\`([^\`]*)\`/eval($1)/gem;
1173
1174open SELF,$0;
1175while(<SELF>) {
1176	next if (/^#!/);
1177        last if (!s/^#/@/ and !/^$/);
1178        print;
1179}
1180close SELF;
1181
1182print $code;
1183
1184close STDOUT;
1185