2 * ChaCha/XChaCha NEON helper functions
4 * Copyright (C) 2016 Linaro, Ltd. <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 * ChaCha20 256-bit cipher algorithm, RFC7539, x64 SSE3 functions
13 * Copyright (C) 2015 Martin Willi
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
22 * NEON doesn't have a rotate instruction. The alternatives are, more or less:
24 * (a) vshl.u32 + vsri.u32 (needs temporary register)
25 * (b) vshl.u32 + vshr.u32 + vorr (needs temporary register)
26 * (c) vrev32.16 (16-bit rotations only)
27 * (d) vtbl.8 + vtbl.8 (multiple of 8 bits rotations only,
30 * ChaCha has 16, 12, 8, and 7-bit rotations. For the 12 and 7-bit rotations,
31 * the only choices are (a) and (b). We use (a) since it takes two-thirds the
32 * cycles of (b) on both Cortex-A7 and Cortex-A53.
34 * For the 16-bit rotation, we use vrev32.16 since it's consistently fastest
35 * and doesn't need a temporary register.
37 * For the 8-bit rotation, we use vtbl.8 + vtbl.8. On Cortex-A7, this sequence
38 * is twice as fast as (a), even when doing (a) on multiple registers
39 * simultaneously to eliminate the stall between vshl and vsri. Also, it
40 * parallelizes better when temporary registers are scarce.
42 * A disadvantage is that on Cortex-A53, the vtbl sequence is the same speed as
43 * (a), so the need to load the rotation table actually makes the vtbl method
44 * slightly slower overall on that CPU (~1.3% slower ChaCha20). Still, it
45 * seems to be a good compromise to get a more significant speed boost on some
46 * CPUs, e.g. ~4.8% faster ChaCha20 on Cortex-A7.
49 #include <linux/linkage.h>
56 * chacha_permute - permute one block
58 * Permute one 64-byte block where the state matrix is stored in the four NEON
59 * registers q0-q3. It performs matrix operations on four words in parallel,
60 * but requires shuffling to rearrange the words after each round.
62 * The round count is given in r3.
64 * Clobbers: r3, ip, q4-q5
69 vld1.8 {d10}, [ip, :64]
72 // x0 += x1, x3 = rotl32(x3 ^ x0, 16)
77 // x2 += x3, x1 = rotl32(x1 ^ x2, 12)
83 // x0 += x1, x3 = rotl32(x3 ^ x0, 8)
89 // x2 += x3, x1 = rotl32(x1 ^ x2, 7)
95 // x1 = shuffle32(x1, MASK(0, 3, 2, 1))
97 // x2 = shuffle32(x2, MASK(1, 0, 3, 2))
99 // x3 = shuffle32(x3, MASK(2, 1, 0, 3))
100 vext.8 q3, q3, q3, #12
102 // x0 += x1, x3 = rotl32(x3 ^ x0, 16)
107 // x2 += x3, x1 = rotl32(x1 ^ x2, 12)
113 // x0 += x1, x3 = rotl32(x3 ^ x0, 8)
119 // x2 += x3, x1 = rotl32(x1 ^ x2, 7)
125 // x1 = shuffle32(x1, MASK(2, 1, 0, 3))
126 vext.8 q1, q1, q1, #12
127 // x2 = shuffle32(x2, MASK(1, 0, 3, 2))
128 vext.8 q2, q2, q2, #8
129 // x3 = shuffle32(x3, MASK(0, 3, 2, 1))
130 vext.8 q3, q3, q3, #4
136 ENDPROC(chacha_permute)
138 ENTRY(chacha_block_xor_neon)
139 // r0: Input state matrix, s
140 // r1: 1 data block output, o
141 // r2: 1 data block input, i
147 vld1.32 {q0-q1}, [r0]
148 vld1.32 {q2-q3}, [ip]
161 // o0 = i0 ^ (x0 + s0)
165 // o1 = i1 ^ (x1 + s1)
169 // o2 = i2 ^ (x2 + s2)
173 // o3 = i3 ^ (x3 + s3)
182 ENDPROC(chacha_block_xor_neon)
184 ENTRY(hchacha_block_neon)
185 // r0: Input state matrix, s
186 // r1: output (8 32-bit words)
190 vld1.32 {q0-q1}, [r0]!
191 vld1.32 {q2-q3}, [r0]
200 ENDPROC(hchacha_block_neon)
203 .Lctrinc: .word 0, 1, 2, 3
204 .Lrol8_table: .byte 3, 0, 1, 2, 7, 4, 5, 6
207 ENTRY(chacha_4block_xor_neon)
209 mov r4, sp // preserve the stack pointer
210 sub ip, sp, #0x20 // allocate a 32 byte buffer
211 bic ip, ip, #0x1f // aligned to 32 bytes
214 // r0: Input state matrix, s
215 // r1: 4 data blocks output, o
216 // r2: 4 data blocks input, i
220 // This function encrypts four consecutive ChaCha blocks by loading
221 // the state matrix in NEON registers four times. The algorithm performs
222 // each operation on the corresponding word of each state matrix, hence
223 // requires no word shuffling. The words are re-interleaved before the
224 // final addition of the original state and the XORing step.
227 // x0..15[0-3] = s0..15[0-3]
229 vld1.32 {q0-q1}, [r0]
230 vld1.32 {q2-q3}, [ip]
235 vld1.32 {q4}, [r5, :128]
240 vadd.u32 q12, q12, q4 // x12 += counter values 0-3
256 vld1.32 {q8-q9}, [sp, :256]
258 // x0 += x4, x12 = rotl32(x12 ^ x0, 16)
259 // x1 += x5, x13 = rotl32(x13 ^ x1, 16)
260 // x2 += x6, x14 = rotl32(x14 ^ x2, 16)
261 // x3 += x7, x15 = rotl32(x15 ^ x3, 16)
277 // x8 += x12, x4 = rotl32(x4 ^ x8, 12)
278 // x9 += x13, x5 = rotl32(x5 ^ x9, 12)
279 // x10 += x14, x6 = rotl32(x6 ^ x10, 12)
280 // x11 += x15, x7 = rotl32(x7 ^ x11, 12)
283 vadd.i32 q10, q10, q14
284 vadd.i32 q11, q11, q15
286 vst1.32 {q8-q9}, [sp, :256]
302 // x0 += x4, x12 = rotl32(x12 ^ x0, 8)
303 // x1 += x5, x13 = rotl32(x13 ^ x1, 8)
304 // x2 += x6, x14 = rotl32(x14 ^ x2, 8)
305 // x3 += x7, x15 = rotl32(x15 ^ x3, 8)
306 vld1.8 {d16}, [ip, :64]
317 vtbl.8 d24, {d24}, d16
318 vtbl.8 d25, {d25}, d16
319 vtbl.8 d26, {d26}, d16
320 vtbl.8 d27, {d27}, d16
321 vtbl.8 d28, {d28}, d16
322 vtbl.8 d29, {d29}, d16
323 vtbl.8 d30, {d30}, d16
324 vtbl.8 d31, {d31}, d16
326 vld1.32 {q8-q9}, [sp, :256]
328 // x8 += x12, x4 = rotl32(x4 ^ x8, 7)
329 // x9 += x13, x5 = rotl32(x5 ^ x9, 7)
330 // x10 += x14, x6 = rotl32(x6 ^ x10, 7)
331 // x11 += x15, x7 = rotl32(x7 ^ x11, 7)
334 vadd.i32 q10, q10, q14
335 vadd.i32 q11, q11, q15
337 vst1.32 {q8-q9}, [sp, :256]
353 vld1.32 {q8-q9}, [sp, :256]
355 // x0 += x5, x15 = rotl32(x15 ^ x0, 16)
356 // x1 += x6, x12 = rotl32(x12 ^ x1, 16)
357 // x2 += x7, x13 = rotl32(x13 ^ x2, 16)
358 // x3 += x4, x14 = rotl32(x14 ^ x3, 16)
374 // x10 += x15, x5 = rotl32(x5 ^ x10, 12)
375 // x11 += x12, x6 = rotl32(x6 ^ x11, 12)
376 // x8 += x13, x7 = rotl32(x7 ^ x8, 12)
377 // x9 += x14, x4 = rotl32(x4 ^ x9, 12)
378 vadd.i32 q10, q10, q15
379 vadd.i32 q11, q11, q12
383 vst1.32 {q8-q9}, [sp, :256]
399 // x0 += x5, x15 = rotl32(x15 ^ x0, 8)
400 // x1 += x6, x12 = rotl32(x12 ^ x1, 8)
401 // x2 += x7, x13 = rotl32(x13 ^ x2, 8)
402 // x3 += x4, x14 = rotl32(x14 ^ x3, 8)
403 vld1.8 {d16}, [ip, :64]
414 vtbl.8 d30, {d30}, d16
415 vtbl.8 d31, {d31}, d16
416 vtbl.8 d24, {d24}, d16
417 vtbl.8 d25, {d25}, d16
418 vtbl.8 d26, {d26}, d16
419 vtbl.8 d27, {d27}, d16
420 vtbl.8 d28, {d28}, d16
421 vtbl.8 d29, {d29}, d16
423 vld1.32 {q8-q9}, [sp, :256]
425 // x10 += x15, x5 = rotl32(x5 ^ x10, 7)
426 // x11 += x12, x6 = rotl32(x6 ^ x11, 7)
427 // x8 += x13, x7 = rotl32(x7 ^ x8, 7)
428 // x9 += x14, x4 = rotl32(x4 ^ x9, 7)
429 vadd.i32 q10, q10, q15
430 vadd.i32 q11, q11, q12
434 vst1.32 {q8-q9}, [sp, :256]
453 // x0..7[0-3] are in q0-q7, x10..15[0-3] are in q10-q15.
454 // x8..9[0-3] are on the stack.
456 // Re-interleave the words in the first two rows of each block (x0..7).
457 // Also add the counter values 0-3 to x12[0-3].
458 vld1.32 {q8}, [r5, :128] // load counter values 0-3
459 vzip.32 q0, q1 // => (0 1 0 1) (0 1 0 1)
460 vzip.32 q2, q3 // => (2 3 2 3) (2 3 2 3)
461 vzip.32 q4, q5 // => (4 5 4 5) (4 5 4 5)
462 vzip.32 q6, q7 // => (6 7 6 7) (6 7 6 7)
463 vadd.u32 q12, q8 // x12 += counter values 0-3
466 vld1.32 {q8-q9}, [r0]! // load s0..7
470 // Swap q1 and q4 so that we'll free up consecutive registers (q0-q1)
471 // after XORing the first 32 bytes.
474 // First two rows of each block are (q0 q1) (q2 q6) (q4 q5) (q3 q7)
476 // x0..3[0-3] += s0..3[0-3] (add orig state to 1st row of each block)
482 // x4..7[0-3] += s4..7[0-3] (add orig state to 2nd row of each block)
488 // XOR first 32 bytes using keystream from first two rows of first block
489 vld1.8 {q8-q9}, [r2]!
492 vst1.8 {q8-q9}, [r1]!
494 // Re-interleave the words in the last two rows of each block (x8..15).
495 vld1.32 {q8-q9}, [sp, :256]
496 vzip.32 q12, q13 // => (12 13 12 13) (12 13 12 13)
497 vzip.32 q14, q15 // => (14 15 14 15) (14 15 14 15)
498 vzip.32 q8, q9 // => (8 9 8 9) (8 9 8 9)
499 vzip.32 q10, q11 // => (10 11 10 11) (10 11 10 11)
500 vld1.32 {q0-q1}, [r0] // load s8..15
506 // Last two rows of each block are (q8 q12) (q10 q14) (q9 q13) (q11 q15)
508 // x8..11[0-3] += s8..11[0-3] (add orig state to 3rd row of each block)
510 vadd.u32 q10, q10, q0
512 vadd.u32 q11, q11, q0
514 // x12..15[0-3] += s12..15[0-3] (add orig state to 4th row of each block)
515 vadd.u32 q12, q12, q1
516 vadd.u32 q14, q14, q1
517 vadd.u32 q13, q13, q1
518 vadd.u32 q15, q15, q1
520 // XOR the rest of the data with the keystream
522 vld1.8 {q0-q1}, [r2]!
525 vst1.8 {q0-q1}, [r1]!
527 vld1.8 {q0-q1}, [r2]!
530 vst1.8 {q0-q1}, [r1]!
532 vld1.8 {q0-q1}, [r2]!
535 vst1.8 {q0-q1}, [r1]!
537 vld1.8 {q0-q1}, [r2]!
540 vst1.8 {q0-q1}, [r1]!
542 vld1.8 {q0-q1}, [r2]!
545 vst1.8 {q0-q1}, [r1]!
547 vld1.8 {q0-q1}, [r2]!
550 vst1.8 {q0-q1}, [r1]!
553 mov sp, r4 // restore original stack pointer
560 ENDPROC(chacha_4block_xor_neon)