1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * This is a SIMD SHA-1 implementation. It requires the Intel(R) Supplemental
4 * SSE3 instruction set extensions introduced in Intel Core Microarchitecture
5 * processors. CPUs supporting Intel(R) AVX extensions will get an additional
8 * This work was inspired by the vectorized implementation of Dean Gaudet.
9 * Additional information on it can be found at:
10 * http://www.arctic.org/~dean/crypto/sha1.html
12 * It was improved upon with more efficient vectorization of the message
13 * scheduling. This implementation has also been optimized for all current and
14 * several future generations of Intel CPUs.
16 * See this article for more information about the implementation details:
17 * http://software.intel.com/en-us/articles/improving-the-performance-of-the-secure-hash-algorithm-1/
19 * Copyright (C) 2010, Intel Corp.
20 * Authors: Maxim Locktyukhin <maxim.locktyukhin@intel.com>
21 * Ronen Zohar <ronen.zohar@intel.com>
23 * Converted to AT&T syntax and adapted for inclusion in the Linux kernel:
24 * Author: Mathias Krause <minipli@googlemail.com>
27 #include <linux/linkage.h>
29 #define CTX %rdi // arg1
30 #define BUF %rsi // arg2
31 #define CNT %rdx // arg3
44 #define BUFFER_PTR %r10
45 #define BUFFER_END %r11
59 #define XMM_SHUFB_BSWAP %xmm10
61 /* we keep window of 64 w[i]+K pre-calculated values in a circular buffer */
62 #define WK(t) (((t) & 15) * 4)(%rsp)
63 #define W_PRECALC_AHEAD 16
66 * This macro implements the SHA-1 function's body for single 64-byte block
67 * param: function's name
69 .macro SHA1_VECTOR_ASM name
77 sub $64, %rsp # allocate workspace
78 and $~15, %rsp # align stack
83 shl $6, CNT # multiply by 64
87 lea K_XMM_AR(%rip), K_BASE
88 xmm_mov BSWAP_SHUFB_CTL(%rip), XMM_SHUFB_BSWAP
90 SHA1_PIPELINED_MAIN_BODY
98 mov %rbp, %rsp # deallocate workspace
108 * This macro implements 80 rounds of SHA-1 for one 64-byte block
110 .macro SHA1_PIPELINED_MAIN_BODY
120 .rept W_PRECALC_AHEAD
163 add $64, BUFFER_PTR # move to the next 64-byte block
164 cmp BUFFER_END, BUFFER_PTR # if the current is the last one use
165 cmovae K_BASE, BUFFER_PTR # dummy source to avoid buffer overrun
179 UPDATE_HASH (HASH_PTR), A
180 UPDATE_HASH 4(HASH_PTR), B
181 UPDATE_HASH 8(HASH_PTR), C
182 UPDATE_HASH 12(HASH_PTR), D
183 UPDATE_HASH 16(HASH_PTR), E
186 cmp K_BASE, BUFFER_PTR # K_BASE means, we reached the end
200 .macro RESTORE_RENAMED_REGS
201 # order is important (REG_C is where it should be)
208 .macro SWAP_REG_NAMES a, b
216 SWAP_REG_NAMES \c, T1
224 SWAP_REG_NAMES \d, T1
231 SWAP_REG_NAMES \c, T1
243 .macro UPDATE_HASH hash, val
249 * RR does two rounds of SHA-1 back to back with W[] pre-calc
250 * t1 = F(b, c, d); e += w(i)
251 * e += t1; b <<= 30; d += w(i+1);
259 .macro RR F, a, b, c, d, e, round
261 \F \b, \c, \d # t1 = F(b, c, d);
262 W_PRECALC (\round + W_PRECALC_AHEAD)
265 add WK(\round + 1), \d
268 W_PRECALC (\round + W_PRECALC_AHEAD + 1)
272 ror $7, \a # (a <<r 5) >>r 7) => a <<r 30)
275 SWAP_REG_NAMES \e, T1
281 # rotate: \a<=\d, \b<=\e, \c<=\a, \d<=\b, \e<=\c
297 .if ((i < 16) || ((i >= 80) && (i < (80 + W_PRECALC_AHEAD))))
298 .set i, ((\r) % 80) # pre-compute for the next iteration
305 .elseif (i < 80) // rounds 32-79
310 .macro W_PRECALC_RESET
322 .macro W_PRECALC_ROTATE
323 .set W_minus_32, W_minus_28
324 .set W_minus_28, W_minus_24
325 .set W_minus_24, W_minus_20
326 .set W_minus_20, W_minus_16
327 .set W_minus_16, W_minus_12
328 .set W_minus_12, W_minus_08
329 .set W_minus_08, W_minus_04
334 .macro W_PRECALC_SSSE3
336 .macro W_PRECALC_00_15
337 W_PRECALC_00_15_SSSE3
339 .macro W_PRECALC_16_31
340 W_PRECALC_16_31_SSSE3
342 .macro W_PRECALC_32_79
343 W_PRECALC_32_79_SSSE3
346 /* message scheduling pre-compute for rounds 0-15 */
347 .macro W_PRECALC_00_15_SSSE3
349 movdqu (i*4)(BUFFER_PTR), W_TMP1
350 .elseif ((i & 3) == 1)
351 pshufb XMM_SHUFB_BSWAP, W_TMP1
353 .elseif ((i & 3) == 2)
354 paddd (K_BASE), W_TMP1
355 .elseif ((i & 3) == 3)
356 movdqa W_TMP1, WK(i&~3)
361 /* message scheduling pre-compute for rounds 16-31
363 * - calculating last 32 w[i] values in 8 XMM registers
364 * - pre-calculate K+w[i] values and store to mem, for later load by ALU add
367 * some "heavy-lifting" vectorization for rounds 16-31 due to w[i]->w[i-3]
368 * dependency, but improves for 32-79
370 .macro W_PRECALC_16_31_SSSE3
371 # blended scheduling of vector and scalar instruction streams, one 4-wide
372 # vector iteration / 4 scalar rounds
375 palignr $8, W_minus_16, W # w[i-14]
376 movdqa W_minus_04, W_TMP1
377 psrldq $4, W_TMP1 # w[i-3]
379 .elseif ((i & 3) == 1)
380 pxor W_minus_16, W_TMP1
385 .elseif ((i & 3) == 2)
392 .elseif ((i & 3) == 3)
396 paddd K_XMM(K_BASE), W_TMP1
397 movdqa W_TMP1, WK(i&~3)
402 /* message scheduling pre-compute for rounds 32-79
404 * in SHA-1 specification: w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]) rol 1
405 * instead we do equal: w[i] = (w[i-6] ^ w[i-16] ^ w[i-28] ^ w[i-32]) rol 2
406 * allows more efficient vectorization since w[i]=>w[i-3] dependency is broken
408 .macro W_PRECALC_32_79_SSSE3
410 movdqa W_minus_04, W_TMP1
411 pxor W_minus_28, W # W is W_minus_32 before xor
412 palignr $8, W_minus_08, W_TMP1
413 .elseif ((i & 3) == 1)
417 .elseif ((i & 3) == 2)
421 .elseif ((i & 3) == 3)
423 paddd K_XMM(K_BASE), W_TMP1
424 movdqa W_TMP1, WK(i&~3)
429 .endm // W_PRECALC_SSSE3
432 #define K1 0x5a827999
433 #define K2 0x6ed9eba1
434 #define K3 0x8f1bbcdc
435 #define K4 0xca62c1d6
461 * SSSE3 optimized implementation:
463 * extern "C" void sha1_transform_ssse3(struct sha1_state *state,
464 * const u8 *data, int blocks);
466 * Note that struct sha1_state is assumed to begin with u32 state[5].
468 SHA1_VECTOR_ASM sha1_transform_ssse3
474 .purgem W_PRECALC_00_15
475 .macro W_PRECALC_00_15
478 .purgem W_PRECALC_16_31
479 .macro W_PRECALC_16_31
482 .purgem W_PRECALC_32_79
483 .macro W_PRECALC_32_79
487 .macro W_PRECALC_00_15_AVX
489 vmovdqu (i*4)(BUFFER_PTR), W_TMP1
490 .elseif ((i & 3) == 1)
491 vpshufb XMM_SHUFB_BSWAP, W_TMP1, W
492 .elseif ((i & 3) == 2)
493 vpaddd (K_BASE), W, W_TMP1
494 .elseif ((i & 3) == 3)
495 vmovdqa W_TMP1, WK(i&~3)
500 .macro W_PRECALC_16_31_AVX
502 vpalignr $8, W_minus_16, W_minus_12, W # w[i-14]
503 vpsrldq $4, W_minus_04, W_TMP1 # w[i-3]
504 vpxor W_minus_08, W, W
505 vpxor W_minus_16, W_TMP1, W_TMP1
506 .elseif ((i & 3) == 1)
508 vpslldq $12, W, W_TMP2
510 .elseif ((i & 3) == 2)
512 vpor W, W_TMP1, W_TMP1
514 vpsrld $30, W_TMP2, W_TMP2
515 .elseif ((i & 3) == 3)
516 vpxor W, W_TMP1, W_TMP1
517 vpxor W_TMP2, W_TMP1, W
518 vpaddd K_XMM(K_BASE), W, W_TMP1
519 vmovdqu W_TMP1, WK(i&~3)
524 .macro W_PRECALC_32_79_AVX
526 vpalignr $8, W_minus_08, W_minus_04, W_TMP1
527 vpxor W_minus_28, W, W # W is W_minus_32 before xor
528 .elseif ((i & 3) == 1)
529 vpxor W_minus_16, W_TMP1, W_TMP1
531 .elseif ((i & 3) == 2)
535 .elseif ((i & 3) == 3)
536 vpaddd K_XMM(K_BASE), W, W_TMP1
537 vmovdqu W_TMP1, WK(i&~3)
542 .endm // W_PRECALC_AVX
551 /* AVX optimized implementation:
552 * extern "C" void sha1_transform_avx(struct sha1_state *state,
553 * const u8 *data, int blocks);
555 SHA1_VECTOR_ASM sha1_transform_avx