2 * ChaCha20 256-bit cipher algorithm, RFC7539, x64 SSSE3 functions
4 * Copyright (C) 2015 Martin Willi
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/linkage.h>
14 .section .rodata.cst16.ROT8, "aM", @progbits, 16
16 ROT8: .octa 0x0e0d0c0f0a09080b0605040702010003
17 .section .rodata.cst16.ROT16, "aM", @progbits, 16
19 ROT16: .octa 0x0d0c0f0e09080b0a0504070601000302
20 .section .rodata.cst16.CTRINC, "aM", @progbits, 16
22 CTRINC: .octa 0x00000003000000020000000100000000
26 ENTRY(chacha20_block_xor_ssse3)
27 # %rdi: Input state matrix, s
28 # %rsi: 1 data block output, o
29 # %rdx: 1 data block input, i
31 # This function encrypts one ChaCha20 block by loading the state matrix
32 # in four SSE registers. It performs matrix operation on four words in
33 # parallel, but requireds shuffling to rearrange the words after each
34 # round. 8/16-bit word rotation is done with the slightly better
35 # performing SSSE3 byte shuffling, 7/12-bit word rotation uses
36 # traditional shift+OR.
39 movdqa 0x00(%rdi),%xmm0
40 movdqa 0x10(%rdi),%xmm1
41 movdqa 0x20(%rdi),%xmm2
42 movdqa 0x30(%rdi),%xmm3
48 movdqa ROT8(%rip),%xmm4
49 movdqa ROT16(%rip),%xmm5
55 # x0 += x1, x3 = rotl32(x3 ^ x0, 16)
60 # x2 += x3, x1 = rotl32(x1 ^ x2, 12)
68 # x0 += x1, x3 = rotl32(x3 ^ x0, 8)
73 # x2 += x3, x1 = rotl32(x1 ^ x2, 7)
81 # x1 = shuffle32(x1, MASK(0, 3, 2, 1))
82 pshufd $0x39,%xmm1,%xmm1
83 # x2 = shuffle32(x2, MASK(1, 0, 3, 2))
84 pshufd $0x4e,%xmm2,%xmm2
85 # x3 = shuffle32(x3, MASK(2, 1, 0, 3))
86 pshufd $0x93,%xmm3,%xmm3
88 # x0 += x1, x3 = rotl32(x3 ^ x0, 16)
93 # x2 += x3, x1 = rotl32(x1 ^ x2, 12)
101 # x0 += x1, x3 = rotl32(x3 ^ x0, 8)
106 # x2 += x3, x1 = rotl32(x1 ^ x2, 7)
114 # x1 = shuffle32(x1, MASK(2, 1, 0, 3))
115 pshufd $0x93,%xmm1,%xmm1
116 # x2 = shuffle32(x2, MASK(1, 0, 3, 2))
117 pshufd $0x4e,%xmm2,%xmm2
118 # x3 = shuffle32(x3, MASK(0, 3, 2, 1))
119 pshufd $0x39,%xmm3,%xmm3
124 # o0 = i0 ^ (x0 + s0)
125 movdqu 0x00(%rdx),%xmm4
128 movdqu %xmm0,0x00(%rsi)
129 # o1 = i1 ^ (x1 + s1)
130 movdqu 0x10(%rdx),%xmm5
133 movdqu %xmm1,0x10(%rsi)
134 # o2 = i2 ^ (x2 + s2)
135 movdqu 0x20(%rdx),%xmm6
138 movdqu %xmm2,0x20(%rsi)
139 # o3 = i3 ^ (x3 + s3)
140 movdqu 0x30(%rdx),%xmm7
143 movdqu %xmm3,0x30(%rsi)
146 ENDPROC(chacha20_block_xor_ssse3)
148 ENTRY(chacha20_4block_xor_ssse3)
149 # %rdi: Input state matrix, s
150 # %rsi: 4 data blocks output, o
151 # %rdx: 4 data blocks input, i
153 # This function encrypts four consecutive ChaCha20 blocks by loading the
154 # the state matrix in SSE registers four times. As we need some scratch
155 # registers, we save the first four registers on the stack. The
156 # algorithm performs each operation on the corresponding word of each
157 # state matrix, hence requires no word shuffling. For final XORing step
158 # we transpose the matrix by interleaving 32- and then 64-bit words,
159 # which allows us to do XOR in SSE registers. 8/16-bit word rotation is
160 # done with the slightly better performing SSSE3 byte shuffling,
161 # 7/12-bit word rotation uses traditional shift+OR.
167 # x0..15[0-3] = s0..3[0..3]
168 movq 0x00(%rdi),%xmm1
169 pshufd $0x00,%xmm1,%xmm0
170 pshufd $0x55,%xmm1,%xmm1
171 movq 0x08(%rdi),%xmm3
172 pshufd $0x00,%xmm3,%xmm2
173 pshufd $0x55,%xmm3,%xmm3
174 movq 0x10(%rdi),%xmm5
175 pshufd $0x00,%xmm5,%xmm4
176 pshufd $0x55,%xmm5,%xmm5
177 movq 0x18(%rdi),%xmm7
178 pshufd $0x00,%xmm7,%xmm6
179 pshufd $0x55,%xmm7,%xmm7
180 movq 0x20(%rdi),%xmm9
181 pshufd $0x00,%xmm9,%xmm8
182 pshufd $0x55,%xmm9,%xmm9
183 movq 0x28(%rdi),%xmm11
184 pshufd $0x00,%xmm11,%xmm10
185 pshufd $0x55,%xmm11,%xmm11
186 movq 0x30(%rdi),%xmm13
187 pshufd $0x00,%xmm13,%xmm12
188 pshufd $0x55,%xmm13,%xmm13
189 movq 0x38(%rdi),%xmm15
190 pshufd $0x00,%xmm15,%xmm14
191 pshufd $0x55,%xmm15,%xmm15
193 movdqa %xmm0,0x00(%rsp)
194 movdqa %xmm1,0x10(%rsp)
195 movdqa %xmm2,0x20(%rsp)
196 movdqa %xmm3,0x30(%rsp)
198 movdqa CTRINC(%rip),%xmm1
199 movdqa ROT8(%rip),%xmm2
200 movdqa ROT16(%rip),%xmm3
202 # x12 += counter values 0-3
208 # x0 += x4, x12 = rotl32(x12 ^ x0, 16)
209 movdqa 0x00(%rsp),%xmm0
211 movdqa %xmm0,0x00(%rsp)
214 # x1 += x5, x13 = rotl32(x13 ^ x1, 16)
215 movdqa 0x10(%rsp),%xmm0
217 movdqa %xmm0,0x10(%rsp)
220 # x2 += x6, x14 = rotl32(x14 ^ x2, 16)
221 movdqa 0x20(%rsp),%xmm0
223 movdqa %xmm0,0x20(%rsp)
226 # x3 += x7, x15 = rotl32(x15 ^ x3, 16)
227 movdqa 0x30(%rsp),%xmm0
229 movdqa %xmm0,0x30(%rsp)
233 # x8 += x12, x4 = rotl32(x4 ^ x8, 12)
240 # x9 += x13, x5 = rotl32(x5 ^ x9, 12)
247 # x10 += x14, x6 = rotl32(x6 ^ x10, 12)
254 # x11 += x15, x7 = rotl32(x7 ^ x11, 12)
262 # x0 += x4, x12 = rotl32(x12 ^ x0, 8)
263 movdqa 0x00(%rsp),%xmm0
265 movdqa %xmm0,0x00(%rsp)
268 # x1 += x5, x13 = rotl32(x13 ^ x1, 8)
269 movdqa 0x10(%rsp),%xmm0
271 movdqa %xmm0,0x10(%rsp)
274 # x2 += x6, x14 = rotl32(x14 ^ x2, 8)
275 movdqa 0x20(%rsp),%xmm0
277 movdqa %xmm0,0x20(%rsp)
280 # x3 += x7, x15 = rotl32(x15 ^ x3, 8)
281 movdqa 0x30(%rsp),%xmm0
283 movdqa %xmm0,0x30(%rsp)
287 # x8 += x12, x4 = rotl32(x4 ^ x8, 7)
294 # x9 += x13, x5 = rotl32(x5 ^ x9, 7)
301 # x10 += x14, x6 = rotl32(x6 ^ x10, 7)
308 # x11 += x15, x7 = rotl32(x7 ^ x11, 7)
316 # x0 += x5, x15 = rotl32(x15 ^ x0, 16)
317 movdqa 0x00(%rsp),%xmm0
319 movdqa %xmm0,0x00(%rsp)
322 # x1 += x6, x12 = rotl32(x12 ^ x1, 16)
323 movdqa 0x10(%rsp),%xmm0
325 movdqa %xmm0,0x10(%rsp)
328 # x2 += x7, x13 = rotl32(x13 ^ x2, 16)
329 movdqa 0x20(%rsp),%xmm0
331 movdqa %xmm0,0x20(%rsp)
334 # x3 += x4, x14 = rotl32(x14 ^ x3, 16)
335 movdqa 0x30(%rsp),%xmm0
337 movdqa %xmm0,0x30(%rsp)
341 # x10 += x15, x5 = rotl32(x5 ^ x10, 12)
348 # x11 += x12, x6 = rotl32(x6 ^ x11, 12)
355 # x8 += x13, x7 = rotl32(x7 ^ x8, 12)
362 # x9 += x14, x4 = rotl32(x4 ^ x9, 12)
370 # x0 += x5, x15 = rotl32(x15 ^ x0, 8)
371 movdqa 0x00(%rsp),%xmm0
373 movdqa %xmm0,0x00(%rsp)
376 # x1 += x6, x12 = rotl32(x12 ^ x1, 8)
377 movdqa 0x10(%rsp),%xmm0
379 movdqa %xmm0,0x10(%rsp)
382 # x2 += x7, x13 = rotl32(x13 ^ x2, 8)
383 movdqa 0x20(%rsp),%xmm0
385 movdqa %xmm0,0x20(%rsp)
388 # x3 += x4, x14 = rotl32(x14 ^ x3, 8)
389 movdqa 0x30(%rsp),%xmm0
391 movdqa %xmm0,0x30(%rsp)
395 # x10 += x15, x5 = rotl32(x5 ^ x10, 7)
402 # x11 += x12, x6 = rotl32(x6 ^ x11, 7)
409 # x8 += x13, x7 = rotl32(x7 ^ x8, 7)
416 # x9 += x14, x4 = rotl32(x4 ^ x9, 7)
429 movq 0x00(%rdi),%xmm3
430 pshufd $0x00,%xmm3,%xmm2
431 pshufd $0x55,%xmm3,%xmm3
432 paddd 0x00(%rsp),%xmm2
433 movdqa %xmm2,0x00(%rsp)
434 paddd 0x10(%rsp),%xmm3
435 movdqa %xmm3,0x10(%rsp)
438 movq 0x08(%rdi),%xmm3
439 pshufd $0x00,%xmm3,%xmm2
440 pshufd $0x55,%xmm3,%xmm3
441 paddd 0x20(%rsp),%xmm2
442 movdqa %xmm2,0x20(%rsp)
443 paddd 0x30(%rsp),%xmm3
444 movdqa %xmm3,0x30(%rsp)
448 movq 0x10(%rdi),%xmm3
449 pshufd $0x00,%xmm3,%xmm2
450 pshufd $0x55,%xmm3,%xmm3
455 movq 0x18(%rdi),%xmm3
456 pshufd $0x00,%xmm3,%xmm2
457 pshufd $0x55,%xmm3,%xmm3
463 movq 0x20(%rdi),%xmm3
464 pshufd $0x00,%xmm3,%xmm2
465 pshufd $0x55,%xmm3,%xmm3
470 movq 0x28(%rdi),%xmm3
471 pshufd $0x00,%xmm3,%xmm2
472 pshufd $0x55,%xmm3,%xmm3
478 movq 0x30(%rdi),%xmm3
479 pshufd $0x00,%xmm3,%xmm2
480 pshufd $0x55,%xmm3,%xmm3
485 movq 0x38(%rdi),%xmm3
486 pshufd $0x00,%xmm3,%xmm2
487 pshufd $0x55,%xmm3,%xmm3
491 # x12 += counter values 0-3
494 # interleave 32-bit words in state n, n+1
495 movdqa 0x00(%rsp),%xmm0
496 movdqa 0x10(%rsp),%xmm1
498 punpckldq %xmm1,%xmm2
499 punpckhdq %xmm1,%xmm0
500 movdqa %xmm2,0x00(%rsp)
501 movdqa %xmm0,0x10(%rsp)
502 movdqa 0x20(%rsp),%xmm0
503 movdqa 0x30(%rsp),%xmm1
505 punpckldq %xmm1,%xmm2
506 punpckhdq %xmm1,%xmm0
507 movdqa %xmm2,0x20(%rsp)
508 movdqa %xmm0,0x30(%rsp)
510 punpckldq %xmm5,%xmm4
511 punpckhdq %xmm5,%xmm0
514 punpckldq %xmm7,%xmm6
515 punpckhdq %xmm7,%xmm0
518 punpckldq %xmm9,%xmm8
519 punpckhdq %xmm9,%xmm0
522 punpckldq %xmm11,%xmm10
523 punpckhdq %xmm11,%xmm0
526 punpckldq %xmm13,%xmm12
527 punpckhdq %xmm13,%xmm0
530 punpckldq %xmm15,%xmm14
531 punpckhdq %xmm15,%xmm0
534 # interleave 64-bit words in state n, n+2
535 movdqa 0x00(%rsp),%xmm0
536 movdqa 0x20(%rsp),%xmm1
538 punpcklqdq %xmm1,%xmm2
539 punpckhqdq %xmm1,%xmm0
540 movdqa %xmm2,0x00(%rsp)
541 movdqa %xmm0,0x20(%rsp)
542 movdqa 0x10(%rsp),%xmm0
543 movdqa 0x30(%rsp),%xmm1
545 punpcklqdq %xmm1,%xmm2
546 punpckhqdq %xmm1,%xmm0
547 movdqa %xmm2,0x10(%rsp)
548 movdqa %xmm0,0x30(%rsp)
550 punpcklqdq %xmm6,%xmm4
551 punpckhqdq %xmm6,%xmm0
554 punpcklqdq %xmm7,%xmm5
555 punpckhqdq %xmm7,%xmm0
558 punpcklqdq %xmm10,%xmm8
559 punpckhqdq %xmm10,%xmm0
562 punpcklqdq %xmm11,%xmm9
563 punpckhqdq %xmm11,%xmm0
566 punpcklqdq %xmm14,%xmm12
567 punpckhqdq %xmm14,%xmm0
570 punpcklqdq %xmm15,%xmm13
571 punpckhqdq %xmm15,%xmm0
574 # xor with corresponding input, write to output
575 movdqa 0x00(%rsp),%xmm0
576 movdqu 0x00(%rdx),%xmm1
578 movdqu %xmm0,0x00(%rsi)
579 movdqa 0x10(%rsp),%xmm0
580 movdqu 0x80(%rdx),%xmm1
582 movdqu %xmm0,0x80(%rsi)
583 movdqa 0x20(%rsp),%xmm0
584 movdqu 0x40(%rdx),%xmm1
586 movdqu %xmm0,0x40(%rsi)
587 movdqa 0x30(%rsp),%xmm0
588 movdqu 0xc0(%rdx),%xmm1
590 movdqu %xmm0,0xc0(%rsi)
591 movdqu 0x10(%rdx),%xmm1
593 movdqu %xmm4,0x10(%rsi)
594 movdqu 0x90(%rdx),%xmm1
596 movdqu %xmm5,0x90(%rsi)
597 movdqu 0x50(%rdx),%xmm1
599 movdqu %xmm6,0x50(%rsi)
600 movdqu 0xd0(%rdx),%xmm1
602 movdqu %xmm7,0xd0(%rsi)
603 movdqu 0x20(%rdx),%xmm1
605 movdqu %xmm8,0x20(%rsi)
606 movdqu 0xa0(%rdx),%xmm1
608 movdqu %xmm9,0xa0(%rsi)
609 movdqu 0x60(%rdx),%xmm1
611 movdqu %xmm10,0x60(%rsi)
612 movdqu 0xe0(%rdx),%xmm1
614 movdqu %xmm11,0xe0(%rsi)
615 movdqu 0x30(%rdx),%xmm1
617 movdqu %xmm12,0x30(%rsi)
618 movdqu 0xb0(%rdx),%xmm1
620 movdqu %xmm13,0xb0(%rsi)
621 movdqu 0x70(%rdx),%xmm1
623 movdqu %xmm14,0x70(%rsi)
624 movdqu 0xf0(%rdx),%xmm1
626 movdqu %xmm15,0xf0(%rsi)
630 ENDPROC(chacha20_4block_xor_ssse3)