1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Ard Biesheuvel <ardb@kernel.org>
3 Date: Fri, 8 Nov 2019 13:22:13 +0100
4 Subject: [PATCH] crypto: arm/chacha - import Eric Biggers's scalar accelerated
7 commit 29621d099f9c642b22a69dc8e7e20c108473a392 upstream.
9 Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
10 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
11 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
13 arch/arm/crypto/chacha-scalar-core.S | 461 +++++++++++++++++++++++++++
14 1 file changed, 461 insertions(+)
15 create mode 100644 arch/arm/crypto/chacha-scalar-core.S
18 +++ b/arch/arm/crypto/chacha-scalar-core.S
20 +/* SPDX-License-Identifier: GPL-2.0 */
22 + * Copyright (C) 2018 Google, Inc.
25 +#include <linux/linkage.h>
26 +#include <asm/assembler.h>
31 + * 16 registers would be needed to hold the state matrix, but only 14 are
32 + * available because 'sp' and 'pc' cannot be used. So we spill the elements
33 + * (x8, x9) to the stack and swap them out with (x10, x11). This adds one
34 + * 'ldrd' and one 'strd' instruction per round.
36 + * All rotates are performed using the implicit rotate operand accepted by the
37 + * 'add' and 'eor' instructions. This is faster than using explicit rotate
38 + * instructions. To make this work, we allow the values in the second and last
39 + * rows of the ChaCha state matrix (rows 'b' and 'd') to temporarily have the
40 + * wrong rotation amount. The rotation amount is then fixed up just in time
41 + * when the values are used. 'brot' is the number of bits the values in row 'b'
42 + * need to be rotated right to arrive at the correct values, and 'drot'
43 + * similarly for row 'd'. (brot, drot) start out as (0, 0) but we make it such
44 + * that they end up as (25, 24) after every round.
47 + // ChaCha state registers
56 + X8_X10 .req r8 // shared by x8 and x10
57 + X9_X11 .req r9 // shared by x9 and x11
64 + // "expand 32-byte k"
65 + .word 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
71 +.macro __rev out, in, t0, t1, t2
72 +.if __LINUX_ARM_ARCH__ >= 6
76 + and \t1, \in, #0xff00
77 + and \t2, \in, #0xff0000
78 + orr \out, \t0, \in, lsr #24
79 + orr \out, \out, \t1, lsl #8
80 + orr \out, \out, \t2, lsr #8
84 +.macro _le32_bswap x, t0, t1, t2
86 + __rev \x, \x, \t0, \t1, \t2
90 +.macro _le32_bswap_4x a, b, c, d, t0, t1, t2
91 + _le32_bswap \a, \t0, \t1, \t2
92 + _le32_bswap \b, \t0, \t1, \t2
93 + _le32_bswap \c, \t0, \t1, \t2
94 + _le32_bswap \d, \t0, \t1, \t2
97 +.macro __ldrd a, b, src, offset
98 +#if __LINUX_ARM_ARCH__ >= 6
99 + ldrd \a, \b, [\src, #\offset]
101 + ldr \a, [\src, #\offset]
102 + ldr \b, [\src, #\offset + 4]
106 +.macro __strd a, b, dst, offset
107 +#if __LINUX_ARM_ARCH__ >= 6
108 + strd \a, \b, [\dst, #\offset]
110 + str \a, [\dst, #\offset]
111 + str \b, [\dst, #\offset + 4]
115 +.macro _halfround a1, b1, c1, d1, a2, b2, c2, d2
117 + // a += b; d ^= a; d = rol(d, 16);
118 + add \a1, \a1, \b1, ror #brot
119 + add \a2, \a2, \b2, ror #brot
120 + eor \d1, \a1, \d1, ror #drot
121 + eor \d2, \a2, \d2, ror #drot
122 + // drot == 32 - 16 == 16
124 + // c += d; b ^= c; b = rol(b, 12);
125 + add \c1, \c1, \d1, ror #16
126 + add \c2, \c2, \d2, ror #16
127 + eor \b1, \c1, \b1, ror #brot
128 + eor \b2, \c2, \b2, ror #brot
129 + // brot == 32 - 12 == 20
131 + // a += b; d ^= a; d = rol(d, 8);
132 + add \a1, \a1, \b1, ror #20
133 + add \a2, \a2, \b2, ror #20
134 + eor \d1, \a1, \d1, ror #16
135 + eor \d2, \a2, \d2, ror #16
136 + // drot == 32 - 8 == 24
138 + // c += d; b ^= c; b = rol(b, 7);
139 + add \c1, \c1, \d1, ror #24
140 + add \c2, \c2, \d2, ror #24
141 + eor \b1, \c1, \b1, ror #20
142 + eor \b2, \c2, \b2, ror #20
143 + // brot == 32 - 7 == 25
150 + // quarterrounds: (x0, x4, x8, x12) and (x1, x5, x9, x13)
151 + _halfround X0, X4, X8_X10, X12, X1, X5, X9_X11, X13
153 + // save (x8, x9); restore (x10, x11)
154 + __strd X8_X10, X9_X11, sp, 0
155 + __ldrd X8_X10, X9_X11, sp, 8
157 + // quarterrounds: (x2, x6, x10, x14) and (x3, x7, x11, x15)
158 + _halfround X2, X6, X8_X10, X14, X3, X7, X9_X11, X15
165 + // quarterrounds: (x0, x5, x10, x15) and (x1, x6, x11, x12)
166 + _halfround X0, X5, X8_X10, X15, X1, X6, X9_X11, X12
168 + // save (x10, x11); restore (x8, x9)
169 + __strd X8_X10, X9_X11, sp, 8
170 + __ldrd X8_X10, X9_X11, sp, 0
172 + // quarterrounds: (x2, x7, x8, x13) and (x3, x4, x9, x14)
173 + _halfround X2, X7, X8_X10, X13, X3, X4, X9_X11, X14
176 +.macro _chacha_permute nrounds
184 +.macro _chacha nrounds
187 + // Stack: unused0-unused1 x10-x11 x0-x15 OUT IN LEN
188 + // Registers contain x0-x9,x12-x15.
190 + // Do the core ChaCha permutation to update x0-x15.
191 + _chacha_permute \nrounds
194 + // Stack: x10-x11 orig_x0-orig_x15 OUT IN LEN
195 + // Registers contain x0-x9,x12-x15.
196 + // x4-x7 are rotated by 'brot'; x12-x15 are rotated by 'drot'.
198 + // Free up some registers (r8-r12,r14) by pushing (x8-x9,x12-x15).
199 + push {X8_X10, X9_X11, X12, X13, X14, X15}
201 + // Load (OUT, IN, LEN).
203 + ldr r12, [sp, #100]
204 + ldr r11, [sp, #104]
208 + // Use slow path if fewer than 64 bytes remain.
210 + blt .Lxor_slowpath\@
212 + // Use slow path if IN and/or OUT isn't 4-byte aligned. Needed even on
213 + // ARMv6+, since ldmia and stmia (used below) still require alignment.
215 + bne .Lxor_slowpath\@
217 + // Fast path: XOR 64 bytes of aligned data.
219 + // Stack: x8-x9 x12-x15 x10-x11 orig_x0-orig_x15 OUT IN LEN
220 + // Registers: r0-r7 are x0-x7; r8-r11 are free; r12 is IN; r14 is OUT.
221 + // x4-x7 are rotated by 'brot'; x12-x15 are rotated by 'drot'.
224 + __ldrd r8, r9, sp, 32
225 + __ldrd r10, r11, sp, 40
230 + _le32_bswap_4x X0, X1, X2, X3, r8, r9, r10
231 + ldmia r12!, {r8-r11}
236 + stmia r14!, {X0-X3}
239 + __ldrd r8, r9, sp, 48
240 + __ldrd r10, r11, sp, 56
241 + add X4, r8, X4, ror #brot
242 + add X5, r9, X5, ror #brot
243 + ldmia r12!, {X0-X3}
244 + add X6, r10, X6, ror #brot
245 + add X7, r11, X7, ror #brot
246 + _le32_bswap_4x X4, X5, X6, X7, r8, r9, r10
251 + stmia r14!, {X4-X7}
254 + pop {r0-r7} // (x8-x9,x12-x15,x10-x11)
255 + __ldrd r8, r9, sp, 32
256 + __ldrd r10, r11, sp, 40
257 + add r0, r0, r8 // x8
258 + add r1, r1, r9 // x9
259 + add r6, r6, r10 // x10
260 + add r7, r7, r11 // x11
261 + _le32_bswap_4x r0, r1, r6, r7, r8, r9, r10
262 + ldmia r12!, {r8-r11}
263 + eor r0, r0, r8 // x8
264 + eor r1, r1, r9 // x9
265 + eor r6, r6, r10 // x10
266 + eor r7, r7, r11 // x11
267 + stmia r14!, {r0,r1,r6,r7}
268 + ldmia r12!, {r0,r1,r6,r7}
269 + __ldrd r8, r9, sp, 48
270 + __ldrd r10, r11, sp, 56
271 + add r2, r8, r2, ror #drot // x12
272 + add r3, r9, r3, ror #drot // x13
273 + add r4, r10, r4, ror #drot // x14
274 + add r5, r11, r5, ror #drot // x15
275 + _le32_bswap_4x r2, r3, r4, r5, r9, r10, r11
276 + ldr r9, [sp, #72] // load LEN
277 + eor r2, r2, r0 // x12
278 + eor r3, r3, r1 // x13
279 + eor r4, r4, r6 // x14
280 + eor r5, r5, r7 // x15
281 + subs r9, #64 // decrement and check LEN
282 + stmia r14!, {r2-r5}
286 +.Lprepare_for_next_block\@:
288 + // Stack: x0-x15 OUT IN LEN
290 + // Increment block counter (x12)
293 + // Store updated (OUT, IN, LEN)
300 + // Store updated block counter (x12)
305 + // Reload state and do next block
306 + ldmia r14!, {r0-r11} // load x0-x11
307 + __strd r10, r11, sp, 8 // store x10-x11 before state
308 + ldmia r14, {r10-r12,r14} // load x12-x15
312 + // Slow path: < 64 bytes remaining, or unaligned input or output buffer.
313 + // We handle it by storing the 64 bytes of keystream to the stack, then
314 + // XOR-ing the needed portion with the data.
316 + // Allocate keystream buffer
320 + // Stack: ks0-ks15 x8-x9 x12-x15 x10-x11 orig_x0-orig_x15 OUT IN LEN
321 + // Registers: r0-r7 are x0-x7; r8-r11 are free; r12 is IN; r14 is &ks0.
322 + // x4-x7 are rotated by 'brot'; x12-x15 are rotated by 'drot'.
324 + // Save keystream for x0-x3
325 + __ldrd r8, r9, sp, 96
326 + __ldrd r10, r11, sp, 104
331 + _le32_bswap_4x X0, X1, X2, X3, r8, r9, r10
332 + stmia r14!, {X0-X3}
334 + // Save keystream for x4-x7
335 + __ldrd r8, r9, sp, 112
336 + __ldrd r10, r11, sp, 120
337 + add X4, r8, X4, ror #brot
338 + add X5, r9, X5, ror #brot
339 + add X6, r10, X6, ror #brot
340 + add X7, r11, X7, ror #brot
341 + _le32_bswap_4x X4, X5, X6, X7, r8, r9, r10
343 + stmia r14!, {X4-X7}
345 + // Save keystream for x8-x15
346 + ldm r8, {r0-r7} // (x8-x9,x12-x15,x10-x11)
347 + __ldrd r8, r9, sp, 128
348 + __ldrd r10, r11, sp, 136
349 + add r0, r0, r8 // x8
350 + add r1, r1, r9 // x9
351 + add r6, r6, r10 // x10
352 + add r7, r7, r11 // x11
353 + _le32_bswap_4x r0, r1, r6, r7, r8, r9, r10
354 + stmia r14!, {r0,r1,r6,r7}
355 + __ldrd r8, r9, sp, 144
356 + __ldrd r10, r11, sp, 152
357 + add r2, r8, r2, ror #drot // x12
358 + add r3, r9, r3, ror #drot // x13
359 + add r4, r10, r4, ror #drot // x14
360 + add r5, r11, r5, ror #drot // x15
361 + _le32_bswap_4x r2, r3, r4, r5, r9, r10, r11
364 + // Stack: ks0-ks15 unused0-unused7 x0-x15 OUT IN LEN
365 + // Registers: r8 is block counter, r12 is IN.
367 + ldr r9, [sp, #168] // LEN
368 + ldr r14, [sp, #160] // OUT
373 + // r1 is number of bytes to XOR, in range [1, 64]
375 +.if __LINUX_ARM_ARCH__ < 6
377 + tst r2, #3 // IN or OUT misaligned?
378 + bne .Lxor_next_byte\@
381 + // XOR a word at a time
384 + blt .Lxor_words_done\@
390 + b .Lxor_slowpath_done\@
393 + beq .Lxor_slowpath_done\@
395 + // XOR a byte at a time
402 + bne .Lxor_next_byte\@
404 +.Lxor_slowpath_done\@:
407 + bgt .Lprepare_for_next_block\@
413 + * void chacha20_arm(u8 *out, const u8 *in, size_t len, const u32 key[8],
414 + * const u32 iv[4]);
417 + cmp r2, #0 // len == 0?
420 + push {r0-r2,r4-r11,lr}
422 + // Push state x0-x15 onto stack.
423 + // Also store an extra copy of x10-x11 just before the state.
425 + ldr r4, [sp, #48] // iv
430 + ldm r4, {X12,X13,X14,X15}
431 + stmdb r0!, {X12,X13,X14,X15}
434 + __ldrd X8_X10, X9_X11, r3, 24
435 + __strd X8_X10, X9_X11, sp, 8
436 + stmdb r0!, {X8_X10, X9_X11}
437 + ldm r3, {X4-X9_X11}
438 + stmdb r0!, {X4-X9_X11}
440 + // constants: x0-x3
441 + adrl X3, .Lexpand_32byte_k
443 + __strd X0, X1, sp, 16
444 + __strd X2, X3, sp, 24
450 +ENDPROC(chacha20_arm)
453 + * void hchacha20_arm(const u32 state[16], u32 out[8]);
455 +ENTRY(hchacha20_arm)
456 + push {r1,r4-r11,lr}
459 + ldmia r14!, {r0-r11} // load x0-x11
460 + push {r10-r11} // store x10-x11 to stack
461 + ldm r14, {r10-r12,r14} // load x12-x15
466 + // Skip over (unused0-unused1, x10-x11)
469 + // Fix up rotations of x12-x15
470 + ror X12, X12, #drot
471 + ror X13, X13, #drot
472 + pop {r4} // load 'out'
473 + ror X14, X14, #drot
474 + ror X15, X15, #drot
476 + // Store (x0-x3,x12-x15) to 'out'
477 + stm r4, {X0,X1,X2,X3,X12,X13,X14,X15}
480 +ENDPROC(hchacha20_arm)