/*1* Intel SHA Extensions optimized implementation of a SHA-1 update function2*3* This file is provided under a dual BSD/GPLv2 license. When using or4* redistributing this file, you may do so under either license.5*6* GPL LICENSE SUMMARY7*8* Copyright(c) 2015 Intel Corporation.9*10* This program is free software; you can redistribute it and/or modify11* it under the terms of version 2 of the GNU General Public License as12* published by the Free Software Foundation.13*14* This program is distributed in the hope that it will be useful, but15* WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17* General Public License for more details.18*19* Contact Information:20* Sean Gulley <[email protected]>21* Tim Chen <[email protected]>22*23* BSD LICENSE24*25* Copyright(c) 2015 Intel Corporation.26*27* Redistribution and use in source and binary forms, with or without28* modification, are permitted provided that the following conditions29* are met:30*31* * Redistributions of source code must retain the above copyright32* notice, this list of conditions and the following disclaimer.33* * Redistributions in binary form must reproduce the above copyright34* notice, this list of conditions and the following disclaimer in35* the documentation and/or other materials provided with the36* distribution.37* * Neither the name of Intel Corporation nor the names of its38* contributors may be used to endorse or promote products derived39* from this software without specific prior written permission.40*41* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS42* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT43* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR44* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT45* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,46* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT47* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,48* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY49* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT50* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE51* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.52*53*/5455#include <linux/linkage.h>5657#define STATE_PTR %rdi /* 1st arg */58#define DATA_PTR %rsi /* 2nd arg */59#define NUM_BLKS %rdx /* 3rd arg */6061#define ABCD %xmm062#define E0 %xmm1 /* Need two E's b/c they ping pong */63#define E1 %xmm264#define MSG0 %xmm365#define MSG1 %xmm466#define MSG2 %xmm567#define MSG3 %xmm668#define SHUF_MASK %xmm769#define ABCD_SAVED %xmm870#define E0_SAVED %xmm97172.macro do_4rounds i, m0, m1, m2, m3, e0, e173.if \i < 1674movdqu \i*4(DATA_PTR), \m075pshufb SHUF_MASK, \m076.endif77.if \i == 078paddd \m0, \e079.else80sha1nexte \m0, \e081.endif82movdqa ABCD, \e183.if \i >= 12 && \i < 7684sha1msg2 \m0, \m185.endif86sha1rnds4 $\i / 20, \e0, ABCD87.if \i >= 4 && \i < 6888sha1msg1 \m0, \m389.endif90.if \i >= 8 && \i < 7291pxor \m0, \m292.endif93.endm9495/*96* Intel SHA Extensions optimized implementation of a SHA-1 block function97*98* This function takes a pointer to the current SHA-1 state, a pointer to the99* input data, and the number of 64-byte blocks to process. The number of100* blocks to process is assumed to be nonzero. Once all blocks have been101* processed, the state is updated with the new state. This function only102* processes complete blocks. State initialization, buffering of partial103* blocks, and digest finalization are expected to be handled elsewhere.104*105* void sha1_ni_transform(struct sha1_block_state *state,106* const u8 *data, size_t nblocks)107*/108.text109SYM_FUNC_START(sha1_ni_transform)110111/* Load the initial state from STATE_PTR. */112pxor E0, E0113pinsrd $3, 16(STATE_PTR), E0114movdqu (STATE_PTR), ABCD115pshufd $0x1B, ABCD, ABCD116117movdqa PSHUFFLE_BYTE_FLIP_MASK(%rip), SHUF_MASK118119.Lnext_block:120/* Save the state for addition after the rounds. */121movdqa E0, E0_SAVED122movdqa ABCD, ABCD_SAVED123124.irp i, 0, 16, 32, 48, 64125do_4rounds (\i + 0), MSG0, MSG1, MSG2, MSG3, E0, E1126do_4rounds (\i + 4), MSG1, MSG2, MSG3, MSG0, E1, E0127do_4rounds (\i + 8), MSG2, MSG3, MSG0, MSG1, E0, E1128do_4rounds (\i + 12), MSG3, MSG0, MSG1, MSG2, E1, E0129.endr130131/* Add the previous state (before the rounds) to the current state. */132sha1nexte E0_SAVED, E0133paddd ABCD_SAVED, ABCD134135/* Advance to the next block, or break if there are no more blocks. */136add $64, DATA_PTR137dec NUM_BLKS138jnz .Lnext_block139140/* Store the new state to STATE_PTR. */141pextrd $3, E0, 16(STATE_PTR)142pshufd $0x1B, ABCD, ABCD143movdqu ABCD, (STATE_PTR)144145RET146SYM_FUNC_END(sha1_ni_transform)147148.section .rodata.cst16.PSHUFFLE_BYTE_FLIP_MASK, "aM", @progbits, 16149.align 16150PSHUFFLE_BYTE_FLIP_MASK:151.octa 0x000102030405060708090a0b0c0d0e0f152153154