2 * ChaCha20 256-bit cipher algorithm, RFC7539, ARM NEON 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.
21 #include <linux/linkage.h>
27 ENTRY(chacha20_block_xor_neon)
28 // r0: Input state matrix, s
29 // r1: 1 data block output, o
30 // r2: 1 data block input, i
33 // This function encrypts one ChaCha20 block by loading the state matrix
34 // in four NEON registers. It performs matrix operation on four words in
35 // parallel, but requireds shuffling to rearrange the words after each
52 // x0 += x1, x3 = rotl32(x3 ^ x0, 16)
57 // x2 += x3, x1 = rotl32(x1 ^ x2, 12)
63 // x0 += x1, x3 = rotl32(x3 ^ x0, 8)
69 // x2 += x3, x1 = rotl32(x1 ^ x2, 7)
75 // x1 = shuffle32(x1, MASK(0, 3, 2, 1))
77 // x2 = shuffle32(x2, MASK(1, 0, 3, 2))
79 // x3 = shuffle32(x3, MASK(2, 1, 0, 3))
80 vext.8 q3, q3, q3, #12
82 // x0 += x1, x3 = rotl32(x3 ^ x0, 16)
87 // x2 += x3, x1 = rotl32(x1 ^ x2, 12)
93 // x0 += x1, x3 = rotl32(x3 ^ x0, 8)
99 // x2 += x3, x1 = rotl32(x1 ^ x2, 7)
105 // x1 = shuffle32(x1, MASK(2, 1, 0, 3))
106 vext.8 q1, q1, q1, #12
107 // x2 = shuffle32(x2, MASK(1, 0, 3, 2))
108 vext.8 q2, q2, q2, #8
109 // x3 = shuffle32(x3, MASK(0, 3, 2, 1))
110 vext.8 q3, q3, q3, #4
119 // o0 = i0 ^ (x0 + s0)
123 // o1 = i1 ^ (x1 + s1)
127 // o2 = i2 ^ (x2 + s2)
131 // o3 = i3 ^ (x3 + s3)
140 ENDPROC(chacha20_block_xor_neon)
143 ENTRY(chacha20_4block_xor_neon)
145 mov ip, sp // preserve the stack pointer
146 sub r3, sp, #0x20 // allocate a 32 byte buffer
147 bic r3, r3, #0x1f // aligned to 32 bytes
150 // r0: Input state matrix, s
151 // r1: 4 data blocks output, o
152 // r2: 4 data blocks input, i
155 // This function encrypts four consecutive ChaCha20 blocks by loading
156 // the state matrix in NEON registers four times. The algorithm performs
157 // each operation on the corresponding word of each state matrix, hence
158 // requires no word shuffling. For final XORing step we transpose the
159 // matrix by interleaving 32- and then 64-bit words, which allows us to
160 // do XOR in NEON registers.
163 // x0..15[0-3] = s0..3[0..3]
165 vld1.32 {q0-q1}, [r0]
166 vld1.32 {q2-q3}, [r3]
171 vld1.32 {q11}, [r3, :128]
174 vadd.i32 q12, q12, q11 // x12 += counter values 0-3
191 // x0 += x4, x12 = rotl32(x12 ^ x0, 16)
192 // x1 += x5, x13 = rotl32(x13 ^ x1, 16)
193 // x2 += x6, x14 = rotl32(x14 ^ x2, 16)
194 // x3 += x7, x15 = rotl32(x15 ^ x3, 16)
210 // x8 += x12, x4 = rotl32(x4 ^ x8, 12)
211 // x9 += x13, x5 = rotl32(x5 ^ x9, 12)
212 // x10 += x14, x6 = rotl32(x6 ^ x10, 12)
213 // x11 += x15, x7 = rotl32(x7 ^ x11, 12)
216 vadd.i32 q10, q10, q14
217 vadd.i32 q11, q11, q15
219 vst1.32 {q8-q9}, [sp, :256]
235 // x0 += x4, x12 = rotl32(x12 ^ x0, 8)
236 // x1 += x5, x13 = rotl32(x13 ^ x1, 8)
237 // x2 += x6, x14 = rotl32(x14 ^ x2, 8)
238 // x3 += x7, x15 = rotl32(x15 ^ x3, 8)
248 vsri.u32 q12, q8, #24
249 vsri.u32 q13, q9, #24
255 vsri.u32 q14, q8, #24
256 vsri.u32 q15, q9, #24
258 vld1.32 {q8-q9}, [sp, :256]
260 // x8 += x12, x4 = rotl32(x4 ^ x8, 7)
261 // x9 += x13, x5 = rotl32(x5 ^ x9, 7)
262 // x10 += x14, x6 = rotl32(x6 ^ x10, 7)
263 // x11 += x15, x7 = rotl32(x7 ^ x11, 7)
266 vadd.i32 q10, q10, q14
267 vadd.i32 q11, q11, q15
269 vst1.32 {q8-q9}, [sp, :256]
285 vld1.32 {q8-q9}, [sp, :256]
287 // x0 += x5, x15 = rotl32(x15 ^ x0, 16)
288 // x1 += x6, x12 = rotl32(x12 ^ x1, 16)
289 // x2 += x7, x13 = rotl32(x13 ^ x2, 16)
290 // x3 += x4, x14 = rotl32(x14 ^ x3, 16)
306 // x10 += x15, x5 = rotl32(x5 ^ x10, 12)
307 // x11 += x12, x6 = rotl32(x6 ^ x11, 12)
308 // x8 += x13, x7 = rotl32(x7 ^ x8, 12)
309 // x9 += x14, x4 = rotl32(x4 ^ x9, 12)
310 vadd.i32 q10, q10, q15
311 vadd.i32 q11, q11, q12
315 vst1.32 {q8-q9}, [sp, :256]
331 // x0 += x5, x15 = rotl32(x15 ^ x0, 8)
332 // x1 += x6, x12 = rotl32(x12 ^ x1, 8)
333 // x2 += x7, x13 = rotl32(x13 ^ x2, 8)
334 // x3 += x4, x14 = rotl32(x14 ^ x3, 8)
344 vsri.u32 q15, q8, #24
345 vsri.u32 q12, q9, #24
351 vsri.u32 q13, q8, #24
352 vsri.u32 q14, q9, #24
354 vld1.32 {q8-q9}, [sp, :256]
356 // x10 += x15, x5 = rotl32(x5 ^ x10, 7)
357 // x11 += x12, x6 = rotl32(x6 ^ x11, 7)
358 // x8 += x13, x7 = rotl32(x7 ^ x8, 7)
359 // x9 += x14, x4 = rotl32(x4 ^ x9, 7)
360 vadd.i32 q10, q10, q15
361 vadd.i32 q11, q11, q12
365 vst1.32 {q8-q9}, [sp, :256]
384 vld1.32 {q8-q9}, [sp, :256]
391 0: ldmia r0!, {r3-r6}
415 // interleave 32-bit words in state n, n+1
421 // interleave 64-bit words in state n, n+2
427 // xor with corresponding input, write to output
428 vld1.8 {q8-q9}, [r2]!
431 vst1.8 {q8-q9}, [r1]!
433 vld1.32 {q8-q9}, [sp, :256]
446 vadd.i32 q10, q10, q0
447 vadd.i32 q11, q11, q4
457 vadd.i32 q12, q12, q0
458 vld1.32 {q0}, [r3, :128]
459 vadd.i32 q13, q13, q4
460 vadd.i32 q12, q12, q0 // x12 += counter values 0-3
464 vadd.i32 q14, q14, q0
465 vadd.i32 q15, q15, q4
467 // interleave 32-bit words in state n, n+1
473 // interleave 64-bit words in state n, n+2
481 vld1.8 {q0-q1}, [r2]!
484 vst1.8 {q0-q1}, [r1]!
486 vld1.8 {q0-q1}, [r2]!
489 vst1.8 {q0-q1}, [r1]!
491 vld1.8 {q0-q1}, [r2]!
494 vst1.8 {q0-q1}, [r1]!
496 vld1.8 {q0-q1}, [r2]!
499 vst1.8 {q0-q1}, [r1]!
501 vld1.8 {q0-q1}, [r2]!
504 vst1.8 {q0-q1}, [r1]!
506 vld1.8 {q0-q1}, [r2]!
509 vst1.8 {q0-q1}, [r1]!
518 ENDPROC(chacha20_4block_xor_neon)
521 CTRINC: .word 0, 1, 2, 3