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>
17 ROT8: .octa 0x0e0d0c0f0a09080b0605040702010003
18 ROT16: .octa 0x0d0c0f0e09080b0a0504070601000302
19 CTRINC: .octa 0x00000003000000020000000100000000
23 ENTRY(chacha20_block_xor_ssse3)
24 # %rdi: Input state matrix, s
25 # %rsi: 1 data block output, o
26 # %rdx: 1 data block input, i
28 # This function encrypts one ChaCha20 block by loading the state matrix
29 # in four SSE registers. It performs matrix operation on four words in
30 # parallel, but requireds shuffling to rearrange the words after each
31 # round. 8/16-bit word rotation is done with the slightly better
32 # performing SSSE3 byte shuffling, 7/12-bit word rotation uses
33 # traditional shift+OR.
36 movdqa 0x00(%rdi),%xmm0
37 movdqa 0x10(%rdi),%xmm1
38 movdqa 0x20(%rdi),%xmm2
39 movdqa 0x30(%rdi),%xmm3
45 movdqa ROT8(%rip),%xmm4
46 movdqa ROT16(%rip),%xmm5
52 # x0 += x1, x3 = rotl32(x3 ^ x0, 16)
57 # x2 += x3, x1 = rotl32(x1 ^ x2, 12)
65 # x0 += x1, x3 = rotl32(x3 ^ x0, 8)
70 # x2 += x3, x1 = rotl32(x1 ^ x2, 7)
78 # x1 = shuffle32(x1, MASK(0, 3, 2, 1))
79 pshufd $0x39,%xmm1,%xmm1
80 # x2 = shuffle32(x2, MASK(1, 0, 3, 2))
81 pshufd $0x4e,%xmm2,%xmm2
82 # x3 = shuffle32(x3, MASK(2, 1, 0, 3))
83 pshufd $0x93,%xmm3,%xmm3
85 # x0 += x1, x3 = rotl32(x3 ^ x0, 16)
90 # x2 += x3, x1 = rotl32(x1 ^ x2, 12)
98 # x0 += x1, x3 = rotl32(x3 ^ x0, 8)
103 # x2 += x3, x1 = rotl32(x1 ^ x2, 7)
111 # x1 = shuffle32(x1, MASK(2, 1, 0, 3))
112 pshufd $0x93,%xmm1,%xmm1
113 # x2 = shuffle32(x2, MASK(1, 0, 3, 2))
114 pshufd $0x4e,%xmm2,%xmm2
115 # x3 = shuffle32(x3, MASK(0, 3, 2, 1))
116 pshufd $0x39,%xmm3,%xmm3
121 # o0 = i0 ^ (x0 + s0)
122 movdqu 0x00(%rdx),%xmm4
125 movdqu %xmm0,0x00(%rsi)
126 # o1 = i1 ^ (x1 + s1)
127 movdqu 0x10(%rdx),%xmm5
130 movdqu %xmm1,0x10(%rsi)
131 # o2 = i2 ^ (x2 + s2)
132 movdqu 0x20(%rdx),%xmm6
135 movdqu %xmm2,0x20(%rsi)
136 # o3 = i3 ^ (x3 + s3)
137 movdqu 0x30(%rdx),%xmm7
140 movdqu %xmm3,0x30(%rsi)
143 ENDPROC(chacha20_block_xor_ssse3)
145 ENTRY(chacha20_4block_xor_ssse3)
146 # %rdi: Input state matrix, s
147 # %rsi: 4 data blocks output, o
148 # %rdx: 4 data blocks input, i
150 # This function encrypts four consecutive ChaCha20 blocks by loading the
151 # the state matrix in SSE registers four times. As we need some scratch
152 # registers, we save the first four registers on the stack. The
153 # algorithm performs each operation on the corresponding word of each
154 # state matrix, hence requires no word shuffling. For final XORing step
155 # we transpose the matrix by interleaving 32- and then 64-bit words,
156 # which allows us to do XOR in SSE registers. 8/16-bit word rotation is
157 # done with the slightly better performing SSSE3 byte shuffling,
158 # 7/12-bit word rotation uses traditional shift+OR.
164 # x0..15[0-3] = s0..3[0..3]
165 movq 0x00(%rdi),%xmm1
166 pshufd $0x00,%xmm1,%xmm0
167 pshufd $0x55,%xmm1,%xmm1
168 movq 0x08(%rdi),%xmm3
169 pshufd $0x00,%xmm3,%xmm2
170 pshufd $0x55,%xmm3,%xmm3
171 movq 0x10(%rdi),%xmm5
172 pshufd $0x00,%xmm5,%xmm4
173 pshufd $0x55,%xmm5,%xmm5
174 movq 0x18(%rdi),%xmm7
175 pshufd $0x00,%xmm7,%xmm6
176 pshufd $0x55,%xmm7,%xmm7
177 movq 0x20(%rdi),%xmm9
178 pshufd $0x00,%xmm9,%xmm8
179 pshufd $0x55,%xmm9,%xmm9
180 movq 0x28(%rdi),%xmm11
181 pshufd $0x00,%xmm11,%xmm10
182 pshufd $0x55,%xmm11,%xmm11
183 movq 0x30(%rdi),%xmm13
184 pshufd $0x00,%xmm13,%xmm12
185 pshufd $0x55,%xmm13,%xmm13
186 movq 0x38(%rdi),%xmm15
187 pshufd $0x00,%xmm15,%xmm14
188 pshufd $0x55,%xmm15,%xmm15
190 movdqa %xmm0,0x00(%rsp)
191 movdqa %xmm1,0x10(%rsp)
192 movdqa %xmm2,0x20(%rsp)
193 movdqa %xmm3,0x30(%rsp)
195 movdqa CTRINC(%rip),%xmm1
196 movdqa ROT8(%rip),%xmm2
197 movdqa ROT16(%rip),%xmm3
199 # x12 += counter values 0-3
205 # x0 += x4, x12 = rotl32(x12 ^ x0, 16)
206 movdqa 0x00(%rsp),%xmm0
208 movdqa %xmm0,0x00(%rsp)
211 # x1 += x5, x13 = rotl32(x13 ^ x1, 16)
212 movdqa 0x10(%rsp),%xmm0
214 movdqa %xmm0,0x10(%rsp)
217 # x2 += x6, x14 = rotl32(x14 ^ x2, 16)
218 movdqa 0x20(%rsp),%xmm0
220 movdqa %xmm0,0x20(%rsp)
223 # x3 += x7, x15 = rotl32(x15 ^ x3, 16)
224 movdqa 0x30(%rsp),%xmm0
226 movdqa %xmm0,0x30(%rsp)
230 # x8 += x12, x4 = rotl32(x4 ^ x8, 12)
237 # x9 += x13, x5 = rotl32(x5 ^ x9, 12)
244 # x10 += x14, x6 = rotl32(x6 ^ x10, 12)
251 # x11 += x15, x7 = rotl32(x7 ^ x11, 12)
259 # x0 += x4, x12 = rotl32(x12 ^ x0, 8)
260 movdqa 0x00(%rsp),%xmm0
262 movdqa %xmm0,0x00(%rsp)
265 # x1 += x5, x13 = rotl32(x13 ^ x1, 8)
266 movdqa 0x10(%rsp),%xmm0
268 movdqa %xmm0,0x10(%rsp)
271 # x2 += x6, x14 = rotl32(x14 ^ x2, 8)
272 movdqa 0x20(%rsp),%xmm0
274 movdqa %xmm0,0x20(%rsp)
277 # x3 += x7, x15 = rotl32(x15 ^ x3, 8)
278 movdqa 0x30(%rsp),%xmm0
280 movdqa %xmm0,0x30(%rsp)
284 # x8 += x12, x4 = rotl32(x4 ^ x8, 7)
291 # x9 += x13, x5 = rotl32(x5 ^ x9, 7)
298 # x10 += x14, x6 = rotl32(x6 ^ x10, 7)
305 # x11 += x15, x7 = rotl32(x7 ^ x11, 7)
313 # x0 += x5, x15 = rotl32(x15 ^ x0, 16)
314 movdqa 0x00(%rsp),%xmm0
316 movdqa %xmm0,0x00(%rsp)
319 # x1 += x6, x12 = rotl32(x12 ^ x1, 16)
320 movdqa 0x10(%rsp),%xmm0
322 movdqa %xmm0,0x10(%rsp)
325 # x2 += x7, x13 = rotl32(x13 ^ x2, 16)
326 movdqa 0x20(%rsp),%xmm0
328 movdqa %xmm0,0x20(%rsp)
331 # x3 += x4, x14 = rotl32(x14 ^ x3, 16)
332 movdqa 0x30(%rsp),%xmm0
334 movdqa %xmm0,0x30(%rsp)
338 # x10 += x15, x5 = rotl32(x5 ^ x10, 12)
345 # x11 += x12, x6 = rotl32(x6 ^ x11, 12)
352 # x8 += x13, x7 = rotl32(x7 ^ x8, 12)
359 # x9 += x14, x4 = rotl32(x4 ^ x9, 12)
367 # x0 += x5, x15 = rotl32(x15 ^ x0, 8)
368 movdqa 0x00(%rsp),%xmm0
370 movdqa %xmm0,0x00(%rsp)
373 # x1 += x6, x12 = rotl32(x12 ^ x1, 8)
374 movdqa 0x10(%rsp),%xmm0
376 movdqa %xmm0,0x10(%rsp)
379 # x2 += x7, x13 = rotl32(x13 ^ x2, 8)
380 movdqa 0x20(%rsp),%xmm0
382 movdqa %xmm0,0x20(%rsp)
385 # x3 += x4, x14 = rotl32(x14 ^ x3, 8)
386 movdqa 0x30(%rsp),%xmm0
388 movdqa %xmm0,0x30(%rsp)
392 # x10 += x15, x5 = rotl32(x5 ^ x10, 7)
399 # x11 += x12, x6 = rotl32(x6 ^ x11, 7)
406 # x8 += x13, x7 = rotl32(x7 ^ x8, 7)
413 # x9 += x14, x4 = rotl32(x4 ^ x9, 7)
426 movq 0x00(%rdi),%xmm3
427 pshufd $0x00,%xmm3,%xmm2
428 pshufd $0x55,%xmm3,%xmm3
429 paddd 0x00(%rsp),%xmm2
430 movdqa %xmm2,0x00(%rsp)
431 paddd 0x10(%rsp),%xmm3
432 movdqa %xmm3,0x10(%rsp)
435 movq 0x08(%rdi),%xmm3
436 pshufd $0x00,%xmm3,%xmm2
437 pshufd $0x55,%xmm3,%xmm3
438 paddd 0x20(%rsp),%xmm2
439 movdqa %xmm2,0x20(%rsp)
440 paddd 0x30(%rsp),%xmm3
441 movdqa %xmm3,0x30(%rsp)
445 movq 0x10(%rdi),%xmm3
446 pshufd $0x00,%xmm3,%xmm2
447 pshufd $0x55,%xmm3,%xmm3
452 movq 0x18(%rdi),%xmm3
453 pshufd $0x00,%xmm3,%xmm2
454 pshufd $0x55,%xmm3,%xmm3
460 movq 0x20(%rdi),%xmm3
461 pshufd $0x00,%xmm3,%xmm2
462 pshufd $0x55,%xmm3,%xmm3
467 movq 0x28(%rdi),%xmm3
468 pshufd $0x00,%xmm3,%xmm2
469 pshufd $0x55,%xmm3,%xmm3
475 movq 0x30(%rdi),%xmm3
476 pshufd $0x00,%xmm3,%xmm2
477 pshufd $0x55,%xmm3,%xmm3
482 movq 0x38(%rdi),%xmm3
483 pshufd $0x00,%xmm3,%xmm2
484 pshufd $0x55,%xmm3,%xmm3
488 # x12 += counter values 0-3
491 # interleave 32-bit words in state n, n+1
492 movdqa 0x00(%rsp),%xmm0
493 movdqa 0x10(%rsp),%xmm1
495 punpckldq %xmm1,%xmm2
496 punpckhdq %xmm1,%xmm0
497 movdqa %xmm2,0x00(%rsp)
498 movdqa %xmm0,0x10(%rsp)
499 movdqa 0x20(%rsp),%xmm0
500 movdqa 0x30(%rsp),%xmm1
502 punpckldq %xmm1,%xmm2
503 punpckhdq %xmm1,%xmm0
504 movdqa %xmm2,0x20(%rsp)
505 movdqa %xmm0,0x30(%rsp)
507 punpckldq %xmm5,%xmm4
508 punpckhdq %xmm5,%xmm0
511 punpckldq %xmm7,%xmm6
512 punpckhdq %xmm7,%xmm0
515 punpckldq %xmm9,%xmm8
516 punpckhdq %xmm9,%xmm0
519 punpckldq %xmm11,%xmm10
520 punpckhdq %xmm11,%xmm0
523 punpckldq %xmm13,%xmm12
524 punpckhdq %xmm13,%xmm0
527 punpckldq %xmm15,%xmm14
528 punpckhdq %xmm15,%xmm0
531 # interleave 64-bit words in state n, n+2
532 movdqa 0x00(%rsp),%xmm0
533 movdqa 0x20(%rsp),%xmm1
535 punpcklqdq %xmm1,%xmm2
536 punpckhqdq %xmm1,%xmm0
537 movdqa %xmm2,0x00(%rsp)
538 movdqa %xmm0,0x20(%rsp)
539 movdqa 0x10(%rsp),%xmm0
540 movdqa 0x30(%rsp),%xmm1
542 punpcklqdq %xmm1,%xmm2
543 punpckhqdq %xmm1,%xmm0
544 movdqa %xmm2,0x10(%rsp)
545 movdqa %xmm0,0x30(%rsp)
547 punpcklqdq %xmm6,%xmm4
548 punpckhqdq %xmm6,%xmm0
551 punpcklqdq %xmm7,%xmm5
552 punpckhqdq %xmm7,%xmm0
555 punpcklqdq %xmm10,%xmm8
556 punpckhqdq %xmm10,%xmm0
559 punpcklqdq %xmm11,%xmm9
560 punpckhqdq %xmm11,%xmm0
563 punpcklqdq %xmm14,%xmm12
564 punpckhqdq %xmm14,%xmm0
567 punpcklqdq %xmm15,%xmm13
568 punpckhqdq %xmm15,%xmm0
571 # xor with corresponding input, write to output
572 movdqa 0x00(%rsp),%xmm0
573 movdqu 0x00(%rdx),%xmm1
575 movdqu %xmm0,0x00(%rsi)
576 movdqa 0x10(%rsp),%xmm0
577 movdqu 0x80(%rdx),%xmm1
579 movdqu %xmm0,0x80(%rsi)
580 movdqa 0x20(%rsp),%xmm0
581 movdqu 0x40(%rdx),%xmm1
583 movdqu %xmm0,0x40(%rsi)
584 movdqa 0x30(%rsp),%xmm0
585 movdqu 0xc0(%rdx),%xmm1
587 movdqu %xmm0,0xc0(%rsi)
588 movdqu 0x10(%rdx),%xmm1
590 movdqu %xmm4,0x10(%rsi)
591 movdqu 0x90(%rdx),%xmm1
593 movdqu %xmm5,0x90(%rsi)
594 movdqu 0x50(%rdx),%xmm1
596 movdqu %xmm6,0x50(%rsi)
597 movdqu 0xd0(%rdx),%xmm1
599 movdqu %xmm7,0xd0(%rsi)
600 movdqu 0x20(%rdx),%xmm1
602 movdqu %xmm8,0x20(%rsi)
603 movdqu 0xa0(%rdx),%xmm1
605 movdqu %xmm9,0xa0(%rsi)
606 movdqu 0x60(%rdx),%xmm1
608 movdqu %xmm10,0x60(%rsi)
609 movdqu 0xe0(%rdx),%xmm1
611 movdqu %xmm11,0xe0(%rsi)
612 movdqu 0x30(%rdx),%xmm1
614 movdqu %xmm12,0x30(%rsi)
615 movdqu 0xb0(%rdx),%xmm1
617 movdqu %xmm13,0xb0(%rsi)
618 movdqu 0x70(%rdx),%xmm1
620 movdqu %xmm14,0x70(%rsi)
621 movdqu 0xf0(%rdx),%xmm1
623 movdqu %xmm15,0xf0(%rsi)
627 ENDPROC(chacha20_4block_xor_ssse3)