xref: /freebsd/sys/crypto/aesni/intel_sha1.c (revision fe182ba1d010bb944c16cf04483da8e380dce1cb)
1 /*******************************************************************************
2 * Copyright (c) 2013, Intel Corporation
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 *   notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 *   notice, this list of conditions and the following disclaimer in the
15 *   documentation and/or other materials provided with the
16 *   distribution.
17 *
18 * * Neither the name of the Intel Corporation nor the names of its
19 *   contributors may be used to endorse or promote products derived from
20 *   this software without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION ""AS IS"" AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 ********************************************************************************
35 *
36 * Intel SHA Extensions optimized implementation of a SHA-1 update function
37 *
38 * The function takes a pointer to the current hash values, a pointer to the
39 * input data, and a number of 64 byte blocks to process.  Once all blocks have
40 * been processed, the digest pointer is  updated with the resulting hash value.
41 * The function only processes complete blocks, there is no functionality to
42 * store partial blocks.  All message padding and hash value initialization must
43 * be done outside the update function.
44 *
45 * The indented lines in the loop are instructions related to rounds processing.
46 * The non-indented lines are instructions related to the message schedule.
47 *
48 * Author: Sean Gulley <sean.m.gulley@intel.com>
49 * Date:   July 2013
50 *
51 ********************************************************************************
52 *
53 * Example complier command line:
54 * icc intel_sha_extensions_sha1_intrinsic.c
55 * gcc -msha -msse4 intel_sha_extensions_sha1_intrinsic.c
56 *
57 *******************************************************************************/
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
61 #include <sys/types.h>
62 #include <immintrin.h>
63 
64 #include <crypto/aesni/sha_sse.h>
65 
66 void intel_sha1_step(uint32_t *digest, const char *data, uint32_t num_blks) {
67    __m128i abcd, e0, e1;
68    __m128i abcd_save, e_save;
69    __m128i msg0, msg1, msg2, msg3;
70    __m128i shuf_mask, e_mask;
71 
72 #if 0
73    e_mask    = _mm_set_epi64x(0xFFFFFFFF00000000ull, 0x0000000000000000ull);
74 #else
75    (void)e_mask;
76    e0        = _mm_set_epi64x(0, 0);
77 #endif
78    shuf_mask = _mm_set_epi64x(0x0001020304050607ull, 0x08090a0b0c0d0e0full);
79 
80    // Load initial hash values
81    abcd      = _mm_loadu_si128((__m128i*) digest);
82    e0        = _mm_insert_epi32(e0, *(digest+4), 3);
83    abcd      = _mm_shuffle_epi32(abcd, 0x1B);
84 #if 0
85    e0        = _mm_and_si128(e0, e_mask);
86 #endif
87 
88    while (num_blks > 0) {
89       // Save hash values for addition after rounds
90       abcd_save = abcd;
91       e_save    = e0;
92 
93       // Rounds 0-3
94       msg0 = _mm_loadu_si128((const __m128i*) data);
95       msg0 = _mm_shuffle_epi8(msg0, shuf_mask);
96          e0   = _mm_add_epi32(e0, msg0);
97          e1   = abcd;
98          abcd = _mm_sha1rnds4_epu32(abcd, e0, 0);
99 
100       // Rounds 4-7
101       msg1 = _mm_loadu_si128((const __m128i*) (data+16));
102       msg1 = _mm_shuffle_epi8(msg1, shuf_mask);
103          e1   = _mm_sha1nexte_epu32(e1, msg1);
104          e0   = abcd;
105          abcd = _mm_sha1rnds4_epu32(abcd, e1, 0);
106       msg0 = _mm_sha1msg1_epu32(msg0, msg1);
107 
108       // Rounds 8-11
109       msg2 = _mm_loadu_si128((const __m128i*) (data+32));
110       msg2 = _mm_shuffle_epi8(msg2, shuf_mask);
111          e0   = _mm_sha1nexte_epu32(e0, msg2);
112          e1   = abcd;
113          abcd = _mm_sha1rnds4_epu32(abcd, e0, 0);
114       msg1 = _mm_sha1msg1_epu32(msg1, msg2);
115       msg0 = _mm_xor_si128(msg0, msg2);
116 
117       // Rounds 12-15
118       msg3 = _mm_loadu_si128((const __m128i*) (data+48));
119       msg3 = _mm_shuffle_epi8(msg3, shuf_mask);
120          e1   = _mm_sha1nexte_epu32(e1, msg3);
121          e0   = abcd;
122       msg0 = _mm_sha1msg2_epu32(msg0, msg3);
123          abcd = _mm_sha1rnds4_epu32(abcd, e1, 0);
124       msg2 = _mm_sha1msg1_epu32(msg2, msg3);
125       msg1 = _mm_xor_si128(msg1, msg3);
126 
127       // Rounds 16-19
128          e0   = _mm_sha1nexte_epu32(e0, msg0);
129          e1   = abcd;
130       msg1 = _mm_sha1msg2_epu32(msg1, msg0);
131          abcd = _mm_sha1rnds4_epu32(abcd, e0, 0);
132       msg3 = _mm_sha1msg1_epu32(msg3, msg0);
133       msg2 = _mm_xor_si128(msg2, msg0);
134 
135       // Rounds 20-23
136          e1   = _mm_sha1nexte_epu32(e1, msg1);
137          e0   = abcd;
138       msg2 = _mm_sha1msg2_epu32(msg2, msg1);
139          abcd = _mm_sha1rnds4_epu32(abcd, e1, 1);
140       msg0 = _mm_sha1msg1_epu32(msg0, msg1);
141       msg3 = _mm_xor_si128(msg3, msg1);
142 
143       // Rounds 24-27
144          e0   = _mm_sha1nexte_epu32(e0, msg2);
145          e1   = abcd;
146       msg3 = _mm_sha1msg2_epu32(msg3, msg2);
147          abcd = _mm_sha1rnds4_epu32(abcd, e0, 1);
148       msg1 = _mm_sha1msg1_epu32(msg1, msg2);
149       msg0 = _mm_xor_si128(msg0, msg2);
150 
151       // Rounds 28-31
152          e1   = _mm_sha1nexte_epu32(e1, msg3);
153          e0   = abcd;
154       msg0 = _mm_sha1msg2_epu32(msg0, msg3);
155          abcd = _mm_sha1rnds4_epu32(abcd, e1, 1);
156       msg2 = _mm_sha1msg1_epu32(msg2, msg3);
157       msg1 = _mm_xor_si128(msg1, msg3);
158 
159       // Rounds 32-35
160          e0   = _mm_sha1nexte_epu32(e0, msg0);
161          e1   = abcd;
162       msg1 = _mm_sha1msg2_epu32(msg1, msg0);
163          abcd = _mm_sha1rnds4_epu32(abcd, e0, 1);
164       msg3 = _mm_sha1msg1_epu32(msg3, msg0);
165       msg2 = _mm_xor_si128(msg2, msg0);
166 
167       // Rounds 36-39
168          e1   = _mm_sha1nexte_epu32(e1, msg1);
169          e0   = abcd;
170       msg2 = _mm_sha1msg2_epu32(msg2, msg1);
171          abcd = _mm_sha1rnds4_epu32(abcd, e1, 1);
172       msg0 = _mm_sha1msg1_epu32(msg0, msg1);
173       msg3 = _mm_xor_si128(msg3, msg1);
174 
175       // Rounds 40-43
176          e0   = _mm_sha1nexte_epu32(e0, msg2);
177          e1   = abcd;
178       msg3 = _mm_sha1msg2_epu32(msg3, msg2);
179          abcd = _mm_sha1rnds4_epu32(abcd, e0, 2);
180       msg1 = _mm_sha1msg1_epu32(msg1, msg2);
181       msg0 = _mm_xor_si128(msg0, msg2);
182 
183       // Rounds 44-47
184          e1   = _mm_sha1nexte_epu32(e1, msg3);
185          e0   = abcd;
186       msg0 = _mm_sha1msg2_epu32(msg0, msg3);
187          abcd = _mm_sha1rnds4_epu32(abcd, e1, 2);
188       msg2 = _mm_sha1msg1_epu32(msg2, msg3);
189       msg1 = _mm_xor_si128(msg1, msg3);
190 
191       // Rounds 48-51
192          e0   = _mm_sha1nexte_epu32(e0, msg0);
193          e1   = abcd;
194       msg1 = _mm_sha1msg2_epu32(msg1, msg0);
195          abcd = _mm_sha1rnds4_epu32(abcd, e0, 2);
196       msg3 = _mm_sha1msg1_epu32(msg3, msg0);
197       msg2 = _mm_xor_si128(msg2, msg0);
198 
199       // Rounds 52-55
200          e1   = _mm_sha1nexte_epu32(e1, msg1);
201          e0   = abcd;
202       msg2 = _mm_sha1msg2_epu32(msg2, msg1);
203          abcd = _mm_sha1rnds4_epu32(abcd, e1, 2);
204       msg0 = _mm_sha1msg1_epu32(msg0, msg1);
205       msg3 = _mm_xor_si128(msg3, msg1);
206 
207       // Rounds 56-59
208          e0   = _mm_sha1nexte_epu32(e0, msg2);
209          e1   = abcd;
210       msg3 = _mm_sha1msg2_epu32(msg3, msg2);
211          abcd = _mm_sha1rnds4_epu32(abcd, e0, 2);
212       msg1 = _mm_sha1msg1_epu32(msg1, msg2);
213       msg0 = _mm_xor_si128(msg0, msg2);
214 
215       // Rounds 60-63
216          e1   = _mm_sha1nexte_epu32(e1, msg3);
217          e0   = abcd;
218       msg0 = _mm_sha1msg2_epu32(msg0, msg3);
219          abcd = _mm_sha1rnds4_epu32(abcd, e1, 3);
220       msg2 = _mm_sha1msg1_epu32(msg2, msg3);
221       msg1 = _mm_xor_si128(msg1, msg3);
222 
223       // Rounds 64-67
224          e0   = _mm_sha1nexte_epu32(e0, msg0);
225          e1   = abcd;
226       msg1 = _mm_sha1msg2_epu32(msg1, msg0);
227          abcd = _mm_sha1rnds4_epu32(abcd, e0, 3);
228       msg3 = _mm_sha1msg1_epu32(msg3, msg0);
229       msg2 = _mm_xor_si128(msg2, msg0);
230 
231       // Rounds 68-71
232          e1   = _mm_sha1nexte_epu32(e1, msg1);
233          e0   = abcd;
234       msg2 = _mm_sha1msg2_epu32(msg2, msg1);
235          abcd = _mm_sha1rnds4_epu32(abcd, e1, 3);
236       msg3 = _mm_xor_si128(msg3, msg1);
237 
238       // Rounds 72-75
239          e0   = _mm_sha1nexte_epu32(e0, msg2);
240          e1   = abcd;
241       msg3 = _mm_sha1msg2_epu32(msg3, msg2);
242          abcd = _mm_sha1rnds4_epu32(abcd, e0, 3);
243 
244       // Rounds 76-79
245          e1   = _mm_sha1nexte_epu32(e1, msg3);
246          e0   = abcd;
247          abcd = _mm_sha1rnds4_epu32(abcd, e1, 3);
248 
249       // Add current hash values with previously saved
250       e0   = _mm_sha1nexte_epu32(e0, e_save);
251       abcd = _mm_add_epi32(abcd, abcd_save);
252 
253       data += 64;
254       num_blks--;
255    }
256 
257    abcd = _mm_shuffle_epi32(abcd, 0x1B);
258    _mm_store_si128((__m128i*) digest, abcd);
259    *(digest+4) = _mm_extract_epi32(e0, 3);
260 }
261 
262