Debian Codegraph @

cipher/sha512-ssse3-i386.c

raw
1 /* sha512-ssse3-i386.c - i386/SSSE3 implementation of SHA-512 transform
2 * Copyright (C) 2019 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * SHA512 Message Expansion (I2 and W2 macros) based on implementation
22 * from file "sha512-ssse3-amd64.s":
23 ************************************************************************
24 * Copyright (c) 2012, Intel Corporation
25 *
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions are
30 * met:
31 *
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 *
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the
38 * distribution.
39 *
40 * * Neither the name of the Intel Corporation nor the names of its
41 * contributors may be used to endorse or promote products derived from
42 * this software without specific prior written permission.
43 *
44 *
45 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY
46 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR
49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 ************************************************************************
57 */
58
59 #include <config.h>
60
61 #if defined(__i386__) && SIZEOF_UNSIGNED_LONG == 4 && __GNUC__ >= 4 && \
62 defined(HAVE_GCC_INLINE_ASM_SSSE3) && defined(USE_SHA512)
63
64 #include "bufhelp.h"
65
66
67 #if _GCRY_GCC_VERSION >= 40400 /* 4.4 */
68 /* Prevent compiler from issuing SSE/MMX instructions between asm blocks. */
69 # pragma GCC target("no-sse")
70 # pragma GCC target("no-mmx")
71 #endif
72 #if __clang__
73 # pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function)
74 # pragma clang attribute push (__attribute__((target("no-mmx"))), apply_to = function)
75 #endif
76
77
78 #define ALWAYS_INLINE inline __attribute__((always_inline))
79 #define NO_INLINE __attribute__((noinline))
80 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
81
82 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
83 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
84 #define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE
85
86
87 static const u64 K[80] __attribute__ ((aligned (16))) =
88 {
89 U64_C(0x428a2f98d728ae22), U64_C(0x7137449123ef65cd),
90 U64_C(0xb5c0fbcfec4d3b2f), U64_C(0xe9b5dba58189dbbc),
91 U64_C(0x3956c25bf348b538), U64_C(0x59f111f1b605d019),
92 U64_C(0x923f82a4af194f9b), U64_C(0xab1c5ed5da6d8118),
93 U64_C(0xd807aa98a3030242), U64_C(0x12835b0145706fbe),
94 U64_C(0x243185be4ee4b28c), U64_C(0x550c7dc3d5ffb4e2),
95 U64_C(0x72be5d74f27b896f), U64_C(0x80deb1fe3b1696b1),
96 U64_C(0x9bdc06a725c71235), U64_C(0xc19bf174cf692694),
97 U64_C(0xe49b69c19ef14ad2), U64_C(0xefbe4786384f25e3),
98 U64_C(0x0fc19dc68b8cd5b5), U64_C(0x240ca1cc77ac9c65),
99 U64_C(0x2de92c6f592b0275), U64_C(0x4a7484aa6ea6e483),
100 U64_C(0x5cb0a9dcbd41fbd4), U64_C(0x76f988da831153b5),
101 U64_C(0x983e5152ee66dfab), U64_C(0xa831c66d2db43210),
102 U64_C(0xb00327c898fb213f), U64_C(0xbf597fc7beef0ee4),
103 U64_C(0xc6e00bf33da88fc2), U64_C(0xd5a79147930aa725),
104 U64_C(0x06ca6351e003826f), U64_C(0x142929670a0e6e70),
105 U64_C(0x27b70a8546d22ffc), U64_C(0x2e1b21385c26c926),
106 U64_C(0x4d2c6dfc5ac42aed), U64_C(0x53380d139d95b3df),
107 U64_C(0x650a73548baf63de), U64_C(0x766a0abb3c77b2a8),
108 U64_C(0x81c2c92e47edaee6), U64_C(0x92722c851482353b),
109 U64_C(0xa2bfe8a14cf10364), U64_C(0xa81a664bbc423001),
110 U64_C(0xc24b8b70d0f89791), U64_C(0xc76c51a30654be30),
111 U64_C(0xd192e819d6ef5218), U64_C(0xd69906245565a910),
112 U64_C(0xf40e35855771202a), U64_C(0x106aa07032bbd1b8),
113 U64_C(0x19a4c116b8d2d0c8), U64_C(0x1e376c085141ab53),
114 U64_C(0x2748774cdf8eeb99), U64_C(0x34b0bcb5e19b48a8),
115 U64_C(0x391c0cb3c5c95a63), U64_C(0x4ed8aa4ae3418acb),
116 U64_C(0x5b9cca4f7763e373), U64_C(0x682e6ff3d6b2b8a3),
117 U64_C(0x748f82ee5defb2fc), U64_C(0x78a5636f43172f60),
118 U64_C(0x84c87814a1f0ab72), U64_C(0x8cc702081a6439ec),
119 U64_C(0x90befffa23631e28), U64_C(0xa4506cebde82bde9),
120 U64_C(0xbef9a3f7b2c67915), U64_C(0xc67178f2e372532b),
121 U64_C(0xca273eceea26619c), U64_C(0xd186b8c721c0c207),
122 U64_C(0xeada7dd6cde0eb1e), U64_C(0xf57d4f7fee6ed178),
123 U64_C(0x06f067aa72176fba), U64_C(0x0a637dc5a2c898a6),
124 U64_C(0x113f9804bef90dae), U64_C(0x1b710b35131c471b),
125 U64_C(0x28db77f523047d84), U64_C(0x32caab7b40c72493),
126 U64_C(0x3c9ebe0a15c9bebc), U64_C(0x431d67c49c100d4c),
127 U64_C(0x4cc5d4becb3e42b6), U64_C(0x597f299cfc657e2a),
128 U64_C(0x5fcb6fab3ad6faec), U64_C(0x6c44198c4a475817)
129 };
130
131 static const unsigned char bshuf_mask[16] __attribute__ ((aligned (16))) =
132 { 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8 };
133
134
135 /* SHA2 round */
136 #define RA "%%mm0"
137 #define RB "%%mm1"
138 #define RC "%%mm2"
139 #define RD "%%mm3"
140 #define RE "%%mm4"
141 #define RF "%%mm5"
142 #define RG "%%mm6"
143 #define RH "%%mm7"
144
145 #define Rx(a,b,c,d,e,f,g,h,wk) \
146 asm volatile (/* Cho + Sum1 */ \
147 "movq2dq "a", %%xmm2;\n\t" \
148 "movq "e", "a";\n\t" \
149 "movq2dq "c", %%xmm3;\n\t" \
150 "movq "e", "c";\n\t" \
151 "movq2dq "b", %%xmm4;\n\t" \
152 "movq "e", "b";\n\t" \
153 "psrlq $(41-18), "c";\n\t" \
154 "pandn "g", "a";\n\t" \
155 "pxor "e", "c";\n\t" \
156 "pand "f", "b";\n\t" \
157 "psrlq $(18-14), "c";\n\t" \
158 "paddq "a", "h";\n\t" \
159 wk(a) \
160 "pxor "e", "c";\n\t" \
161 "paddq "b", "h";\n\t" \
162 "psrlq $(14), "c";\n\t" \
163 "movq "e", "b";\n\t" \
164 "psllq $(50-46), "b";\n\t" \
165 "paddq "a", "h";\n\t" \
166 "movdq2q %%xmm2, "a";\n\t" \
167 "pxor "e", "b";\n\t" \
168 "psllq $(46-23), "b";\n\t" \
169 "pxor "e", "b";\n\t" \
170 "psllq $(23), "b";\n\t" \
171 "pxor "b", "c";\n\t" \
172 "movdq2q %%xmm4, "b";\n\t" \
173 "paddq "c", "h";\n\t" \
174 "movdq2q %%xmm3, "c";\n\t" \
175 \
176 /* Maj + Sum0 */ \
177 "movq2dq "e", %%xmm2;\n\t" \
178 "movq "a", "e";\n\t" \
179 "movq2dq "g", %%xmm3;\n\t" \
180 "movq "a", "g";\n\t" \
181 "movq2dq "f", %%xmm4;\n\t" \
182 "movq "a", "f";\n\t" \
183 "psrlq $(39-34), "g";\n\t" \
184 "pxor "b", "e";\n\t" \
185 "pxor "a", "g";\n\t" \
186 "pand "b", "f";\n\t" \
187 "psrlq $(34-28), "g";\n\t" \
188 "pand "c", "e";\n\t" \
189 "pxor "a", "g";\n\t" \
190 "paddq "h", "d";\n\t" \
191 "paddq "f", "h";\n\t" \
192 "movdq2q %%xmm4, "f";\n\t" \
193 "psrlq $28, "g";\n\t" \
194 "paddq "e", "h";\n\t" \
195 "movq "a", "e";\n\t" \
196 "psllq $(36-30), "e";\n\t" \
197 "pxor "a", "e";\n\t" \
198 "psllq $(30-25), "e";\n\t" \
199 "pxor "a", "e";\n\t" \
200 "psllq $(25), "e";\n\t" \
201 "pxor "e", "g";\n\t" \
202 "movdq2q %%xmm2, "e";\n\t" \
203 "paddq "g", "h";\n\t" \
204 "movdq2q %%xmm3, "g";\n\t" \
205 \
206 : \
207 : \
208 : "memory" )
209
210 #define WK0(tmp) "movdq2q %%xmm0, "tmp";\n\t" \
211 "pshufd $0xee, %%xmm0, %%xmm0;\n\t"
212
213 #define WK1(tmp) "movdq2q %%xmm0, "tmp";\n\t"
214
215 /* Message expansion */
216 #define I2(i) \
217 asm volatile ("movdqu %[inbuf], %%xmm0;\n\t" \
218 "pshufb %%xmm6, %%xmm0;\n\t" \
219 "movdqu %%xmm0, %[w];\n\t" \
220 "paddq %[k], %%xmm0;\n\t" \
221 : \
222 : [k] "m" (K[i]), \
223 [w] "m" (w[i]), \
224 [inbuf] "m" (data[(i)*8]) \
225 : "memory" )
226
227 #define W2(i) \
228 asm volatile ("movdqu %[w_t_m_2], %%xmm2;\n\t" \
229 "movdqa %%xmm2, %%xmm0;\n\t" \
230 "movdqu %[w_t_m_15], %%xmm5;\n\t" \
231 : \
232 : [w_t_m_2] "m" (w[(i)-2]), \
233 [w_t_m_15] "m" (w[(i)-15]) \
234 : "memory" ); \
235 asm volatile ("movdqa %%xmm5, %%xmm3;\n\t" \
236 "psrlq $(61-19), %%xmm0;\n\t" \
237 "psrlq $(8-7), %%xmm3;\n\t" \
238 "pxor %%xmm2, %%xmm0;\n\t" \
239 "pxor %%xmm5, %%xmm3;\n\t" \
240 "psrlq $(19-6), %%xmm0;\n\t" \
241 "psrlq $(7-1), %%xmm3;\n\t" \
242 "pxor %%xmm2, %%xmm0;\n\t" \
243 "pxor %%xmm5, %%xmm3;\n\t" \
244 "psrlq $6, %%xmm0;\n\t" \
245 "psrlq $1, %%xmm3;\n\t" \
246 "movdqa %%xmm2, %%xmm1;\n\t" \
247 "movdqa %%xmm5, %%xmm4;\n\t" \
248 "psllq $(61-19), %%xmm1;\n\t" \
249 "psllq $(8-1), %%xmm4;\n\t" \
250 "pxor %%xmm2, %%xmm1;\n\t" \
251 "pxor %%xmm5, %%xmm4;\n\t" \
252 "psllq $(64-61), %%xmm1;\n\t" \
253 "psllq $(64-8), %%xmm4;\n\t" \
254 "pxor %%xmm1, %%xmm0;\n\t" \
255 "movdqu %[w_t_m_16], %%xmm2;\n\t" \
256 "pxor %%xmm4, %%xmm3;\n\t" \
257 "movdqu %[w_t_m_7], %%xmm1;\n\t" \
258 : \
259 : [w_t_m_7] "m" (w[(i)-7]), \
260 [w_t_m_16] "m" (w[(i)-16]) \
261 : "memory" ); \
262 asm volatile ("paddq %%xmm3, %%xmm0;\n\t" \
263 "paddq %%xmm2, %%xmm0;\n\t" \
264 "paddq %%xmm1, %%xmm0;\n\t" \
265 "movdqu %%xmm0, %[w_t_m_0];\n\t" \
266 "paddq %[k], %%xmm0;\n\t" \
267 : [w_t_m_0] "=m" (w[(i)-0]) \
268 : [k] "m" (K[i]) \
269 : "memory" )
270
271 unsigned int ASM_FUNC_ATTR
272 _gcry_sha512_transform_i386_ssse3(u64 state[8], const unsigned char *data,
273 size_t nblks)
274 {
275 unsigned int t;
276 u64 w[80];
277
278 /* Load state to MMX registers. */
279 asm volatile ("movq 8*0(%[state]), "RA";\n\t"
280 "movq 8*1(%[state]), "RB";\n\t"
281 "movq 8*2(%[state]), "RC";\n\t"
282 "movq 8*3(%[state]), "RD";\n\t"
283 "movq 8*4(%[state]), "RE";\n\t"
284 "movq 8*5(%[state]), "RF";\n\t"
285 "movq 8*6(%[state]), "RG";\n\t"
286 "movq 8*7(%[state]), "RH";\n\t"
287 :
288 : [state] "r" (state)
289 : "memory" );
290
291 asm volatile ("movdqa %[bshuf_mask], %%xmm6;\n\t"
292 :
293 : [bshuf_mask] "m" (*bshuf_mask)
294 : "memory" );
295
296 while (nblks)
297 {
298 I2(0);
299 Rx(RA, RB, RC, RD, RE, RF, RG, RH, WK0);
300 Rx(RH, RA, RB, RC, RD, RE, RF, RG, WK1);
301 I2(2);
302 Rx(RG, RH, RA, RB, RC, RD, RE, RF, WK0);
303 Rx(RF, RG, RH, RA, RB, RC, RD, RE, WK1);
304 I2(4);
305 Rx(RE, RF, RG, RH, RA, RB, RC, RD, WK0);
306 Rx(RD, RE, RF, RG, RH, RA, RB, RC, WK1);
307 I2(6);
308 Rx(RC, RD, RE, RF, RG, RH, RA, RB, WK0);
309 Rx(RB, RC, RD, RE, RF, RG, RH, RA, WK1);
310 I2(8);
311 Rx(RA, RB, RC, RD, RE, RF, RG, RH, WK0);
312 Rx(RH, RA, RB, RC, RD, RE, RF, RG, WK1);
313 I2(10);
314 Rx(RG, RH, RA, RB, RC, RD, RE, RF, WK0);
315 Rx(RF, RG, RH, RA, RB, RC, RD, RE, WK1);
316 I2(12);
317 Rx(RE, RF, RG, RH, RA, RB, RC, RD, WK0);
318 Rx(RD, RE, RF, RG, RH, RA, RB, RC, WK1);
319 I2(14);
320 Rx(RC, RD, RE, RF, RG, RH, RA, RB, WK0);
321 Rx(RB, RC, RD, RE, RF, RG, RH, RA, WK1);
322 data += 128;
323
324 for (t = 16; t < 80; t += 16)
325 {
326 W2(t + 0);
327 Rx(RA, RB, RC, RD, RE, RF, RG, RH, WK0);
328 Rx(RH, RA, RB, RC, RD, RE, RF, RG, WK1);
329 W2(t + 2);
330 Rx(RG, RH, RA, RB, RC, RD, RE, RF, WK0);
331 Rx(RF, RG, RH, RA, RB, RC, RD, RE, WK1);
332 W2(t + 4);
333 Rx(RE, RF, RG, RH, RA, RB, RC, RD, WK0);
334 Rx(RD, RE, RF, RG, RH, RA, RB, RC, WK1);
335 W2(t + 6);
336 Rx(RC, RD, RE, RF, RG, RH, RA, RB, WK0);
337 Rx(RB, RC, RD, RE, RF, RG, RH, RA, WK1);
338 W2(t + 8);
339 Rx(RA, RB, RC, RD, RE, RF, RG, RH, WK0);
340 Rx(RH, RA, RB, RC, RD, RE, RF, RG, WK1);
341 W2(t + 10);
342 Rx(RG, RH, RA, RB, RC, RD, RE, RF, WK0);
343 Rx(RF, RG, RH, RA, RB, RC, RD, RE, WK1);
344 W2(t + 12);
345 Rx(RE, RF, RG, RH, RA, RB, RC, RD, WK0);
346 Rx(RD, RE, RF, RG, RH, RA, RB, RC, WK1);
347 W2(t + 14);
348 Rx(RC, RD, RE, RF, RG, RH, RA, RB, WK0);
349 Rx(RB, RC, RD, RE, RF, RG, RH, RA, WK1);
350 }
351
352 asm volatile ("paddq 8*0(%[state]), "RA";\n\t"
353 "paddq 8*1(%[state]), "RB";\n\t"
354 "paddq 8*2(%[state]), "RC";\n\t"
355 "paddq 8*3(%[state]), "RD";\n\t"
356 "paddq 8*4(%[state]), "RE";\n\t"
357 "paddq 8*5(%[state]), "RF";\n\t"
358 "paddq 8*6(%[state]), "RG";\n\t"
359 "paddq 8*7(%[state]), "RH";\n\t"
360 "movq "RA", 8*0(%[state]);\n\t"
361 "movq "RB", 8*1(%[state]);\n\t"
362 "movq "RC", 8*2(%[state]);\n\t"
363 "movq "RD", 8*3(%[state]);\n\t"
364 "movq "RE", 8*4(%[state]);\n\t"
365 "movq "RF", 8*5(%[state]);\n\t"
366 "movq "RG", 8*6(%[state]);\n\t"
367 "movq "RH", 8*7(%[state]);\n\t"
368 :
369 : [state] "r" (state)
370 : "memory" );
371
372 nblks--;
373 }
374
375 /* Clear registers */
376 asm volatile ("pxor %%xmm0, %%xmm0;\n\t"
377 "pxor %%xmm1, %%xmm1;\n\t"
378 "pxor %%xmm2, %%xmm2;\n\t"
379 "pxor %%xmm3, %%xmm3;\n\t"
380 "pxor %%xmm4, %%xmm4;\n\t"
381 "pxor %%xmm5, %%xmm5;\n\t"
382 "pxor %%xmm6, %%xmm6;\n\t"
383 "pxor %%mm0, %%mm0;\n\t"
384 "pxor %%mm1, %%mm1;\n\t"
385 "pxor %%mm2, %%mm2;\n\t"
386 "pxor %%mm3, %%mm3;\n\t"
387 "pxor %%mm4, %%mm4;\n\t"
388 "pxor %%mm5, %%mm5;\n\t"
389 "pxor %%mm6, %%mm6;\n\t"
390 "pxor %%mm7, %%mm7;\n\t"
391 "emms;\n\t"
392 :
393 :
394 : "memory" );
395
396 return sizeof(w);
397 }
398
399 #if __clang__
400 # pragma clang attribute pop
401 # pragma clang attribute pop
402 #endif
403
404 #endif