2 * This is a SIMD SHA-1 implementation. It requires the Intel(R) Supplemental
3 * SSE3 instruction set extensions introduced in Intel Core Microarchitecture
4 * processors. CPUs supporting Intel(R) AVX extensions will get an additional
7 * This work was inspired by the vectorized implementation of Dean Gaudet.
8 * Additional information on it can be found at:
9 * http://www.arctic.org/~dean/crypto/sha1.html
11 * It was improved upon with more efficient vectorization of the message
12 * scheduling. This implementation has also been optimized for all current and
13 * several future generations of Intel CPUs.
15 * See this article for more information about the implementation details:
16 * http://software.intel.com/en-us/articles/improving-the-performance-of-the-secure-hash-algorithm-1/
18 * Copyright (C) 2010, Intel Corp.
19 * Authors: Maxim Locktyukhin <maxim.locktyukhin@intel.com>
20 * Ronen Zohar <ronen.zohar@intel.com>
22 * Converted to AT&T syntax and adapted for inclusion in the Linux kernel:
23 * Author: Mathias Krause <minipli@googlemail.com>
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
31 #include <linux/linkage.h>
33 #define CTX %rdi // arg1
34 #define BUF %rsi // arg2
35 #define CNT %rdx // arg3
48 #define BUFFER_PTR %r10
49 #define BUFFER_END %r11
63 #define XMM_SHUFB_BSWAP %xmm10
65 /* we keep window of 64 w[i]+K pre-calculated values in a circular buffer */
66 #define WK(t) (((t) & 15) * 4)(%rsp)
67 #define W_PRECALC_AHEAD 16
70 * This macro implements the SHA-1 function's body for single 64-byte block
71 * param: function's name
73 .macro SHA1_VECTOR_ASM name
81 sub $64, %rsp # allocate workspace
82 and $~15, %rsp # align stack
87 shl $6, CNT # multiply by 64
91 lea K_XMM_AR(%rip), K_BASE
92 xmm_mov BSWAP_SHUFB_CTL(%rip), XMM_SHUFB_BSWAP
94 SHA1_PIPELINED_MAIN_BODY
102 mov %rbp, %rsp # deallocate workspace
112 * This macro implements 80 rounds of SHA-1 for one 64-byte block
114 .macro SHA1_PIPELINED_MAIN_BODY
124 .rept W_PRECALC_AHEAD
167 add $64, BUFFER_PTR # move to the next 64-byte block
168 cmp BUFFER_END, BUFFER_PTR # if the current is the last one use
169 cmovae K_BASE, BUFFER_PTR # dummy source to avoid buffer overrun
183 UPDATE_HASH (HASH_PTR), A
184 UPDATE_HASH 4(HASH_PTR), B
185 UPDATE_HASH 8(HASH_PTR), C
186 UPDATE_HASH 12(HASH_PTR), D
187 UPDATE_HASH 16(HASH_PTR), E
190 cmp K_BASE, BUFFER_PTR # K_BASE means, we reached the end
204 .macro RESTORE_RENAMED_REGS
205 # order is important (REG_C is where it should be)
212 .macro SWAP_REG_NAMES a, b
220 SWAP_REG_NAMES \c, T1
228 SWAP_REG_NAMES \d, T1
235 SWAP_REG_NAMES \c, T1
247 .macro UPDATE_HASH hash, val
253 * RR does two rounds of SHA-1 back to back with W[] pre-calc
254 * t1 = F(b, c, d); e += w(i)
255 * e += t1; b <<= 30; d += w(i+1);
263 .macro RR F, a, b, c, d, e, round
265 \F \b, \c, \d # t1 = F(b, c, d);
266 W_PRECALC (\round + W_PRECALC_AHEAD)
269 add WK(\round + 1), \d
272 W_PRECALC (\round + W_PRECALC_AHEAD + 1)
276 ror $7, \a # (a <<r 5) >>r 7) => a <<r 30)
279 SWAP_REG_NAMES \e, T1
285 # rotate: \a<=\d, \b<=\e, \c<=\a, \d<=\b, \e<=\c
301 .if ((i < 16) || ((i >= 80) && (i < (80 + W_PRECALC_AHEAD))))
302 .set i, ((\r) % 80) # pre-compute for the next iteration
309 .elseif (i < 80) // rounds 32-79
314 .macro W_PRECALC_RESET
326 .macro W_PRECALC_ROTATE
327 .set W_minus_32, W_minus_28
328 .set W_minus_28, W_minus_24
329 .set W_minus_24, W_minus_20
330 .set W_minus_20, W_minus_16
331 .set W_minus_16, W_minus_12
332 .set W_minus_12, W_minus_08
333 .set W_minus_08, W_minus_04
338 .macro W_PRECALC_SSSE3
340 .macro W_PRECALC_00_15
341 W_PRECALC_00_15_SSSE3
343 .macro W_PRECALC_16_31
344 W_PRECALC_16_31_SSSE3
346 .macro W_PRECALC_32_79
347 W_PRECALC_32_79_SSSE3
350 /* message scheduling pre-compute for rounds 0-15 */
351 .macro W_PRECALC_00_15_SSSE3
353 movdqu (i*4)(BUFFER_PTR), W_TMP1
354 .elseif ((i & 3) == 1)
355 pshufb XMM_SHUFB_BSWAP, W_TMP1
357 .elseif ((i & 3) == 2)
358 paddd (K_BASE), W_TMP1
359 .elseif ((i & 3) == 3)
360 movdqa W_TMP1, WK(i&~3)
365 /* message scheduling pre-compute for rounds 16-31
367 * - calculating last 32 w[i] values in 8 XMM registers
368 * - pre-calculate K+w[i] values and store to mem, for later load by ALU add
371 * some "heavy-lifting" vectorization for rounds 16-31 due to w[i]->w[i-3]
372 * dependency, but improves for 32-79
374 .macro W_PRECALC_16_31_SSSE3
375 # blended scheduling of vector and scalar instruction streams, one 4-wide
376 # vector iteration / 4 scalar rounds
379 palignr $8, W_minus_16, W # w[i-14]
380 movdqa W_minus_04, W_TMP1
381 psrldq $4, W_TMP1 # w[i-3]
383 .elseif ((i & 3) == 1)
384 pxor W_minus_16, W_TMP1
389 .elseif ((i & 3) == 2)
396 .elseif ((i & 3) == 3)
400 paddd K_XMM(K_BASE), W_TMP1
401 movdqa W_TMP1, WK(i&~3)
406 /* message scheduling pre-compute for rounds 32-79
408 * in SHA-1 specification: w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]) rol 1
409 * instead we do equal: w[i] = (w[i-6] ^ w[i-16] ^ w[i-28] ^ w[i-32]) rol 2
410 * allows more efficient vectorization since w[i]=>w[i-3] dependency is broken
412 .macro W_PRECALC_32_79_SSSE3
414 movdqa W_minus_04, W_TMP1
415 pxor W_minus_28, W # W is W_minus_32 before xor
416 palignr $8, W_minus_08, W_TMP1
417 .elseif ((i & 3) == 1)
421 .elseif ((i & 3) == 2)
425 .elseif ((i & 3) == 3)
427 paddd K_XMM(K_BASE), W_TMP1
428 movdqa W_TMP1, WK(i&~3)
433 .endm // W_PRECALC_SSSE3
436 #define K1 0x5a827999
437 #define K2 0x6ed9eba1
438 #define K3 0x8f1bbcdc
439 #define K4 0xca62c1d6
464 /* SSSE3 optimized implementation:
465 * extern "C" void sha1_transform_ssse3(u32 *digest, const char *data, u32 *ws,
466 * unsigned int rounds);
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(u32 *digest, const char *data, u32 *ws,
553 * unsigned int rounds);
555 SHA1_VECTOR_ASM sha1_transform_avx