Debian Codegraph @

cipher/sha1-intel-shaext.c

raw
1 /* sha1-intel-shaext.S - SHAEXT accelerated SHA-1 transform function
2 * Copyright (C) 2018 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 #include <config.h>
21
22 #include "types.h"
23
24 #if defined(HAVE_GCC_INLINE_ASM_SHAEXT) && \
25 defined(HAVE_GCC_INLINE_ASM_SSE41) && defined(USE_SHA1) && \
26 defined(ENABLE_SHAEXT_SUPPORT)
27
28 #if _GCRY_GCC_VERSION >= 40400 /* 4.4 */
29 /* Prevent compiler from issuing SSE instructions between asm blocks. */
30 # pragma GCC target("no-sse")
31 #endif
32 #if __clang__
33 # pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function)
34 #endif
35
36 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
37
38 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
39
40 /* Two macros to be called prior and after the use of SHA-EXT
41 instructions. There should be no external function calls between
42 the use of these macros. There purpose is to make sure that the
43 SSE regsiters are cleared and won't reveal any information about
44 the key or the data. */
45 #ifdef __WIN64__
46 /* XMM6-XMM15 are callee-saved registers on WIN64. */
47 # define shaext_prepare_variable char win64tmp[2*16]
48 # define shaext_prepare_variable_size sizeof(win64tmp)
49 # define shaext_prepare() \
50 do { asm volatile ("movdqu %%xmm6, (%0)\n" \
51 "movdqu %%xmm7, (%1)\n" \
52 : \
53 : "r" (&win64tmp[0]), "r" (&win64tmp[16]) \
54 : "memory"); \
55 } while (0)
56 # define shaext_cleanup(tmp0,tmp1) \
57 do { asm volatile ("movdqu (%0), %%xmm6\n" \
58 "movdqu (%1), %%xmm7\n" \
59 "pxor %%xmm0, %%xmm0\n" \
60 "pxor %%xmm1, %%xmm1\n" \
61 "pxor %%xmm2, %%xmm2\n" \
62 "pxor %%xmm3, %%xmm3\n" \
63 "pxor %%xmm4, %%xmm4\n" \
64 "pxor %%xmm5, %%xmm5\n" \
65 "movdqa %%xmm0, (%2)\n\t" \
66 "movdqa %%xmm0, (%3)\n\t" \
67 : \
68 : "r" (&win64tmp[0]), "r" (&win64tmp[16]), \
69 "r" (tmp0), "r" (tmp1) \
70 : "memory"); \
71 } while (0)
72 #else
73 # define shaext_prepare_variable
74 # define shaext_prepare_variable_size 0
75 # define shaext_prepare() do { } while (0)
76 # define shaext_cleanup(tmp0,tmp1) \
77 do { asm volatile ("pxor %%xmm0, %%xmm0\n" \
78 "pxor %%xmm1, %%xmm1\n" \
79 "pxor %%xmm2, %%xmm2\n" \
80 "pxor %%xmm3, %%xmm3\n" \
81 "pxor %%xmm4, %%xmm4\n" \
82 "pxor %%xmm5, %%xmm5\n" \
83 "pxor %%xmm6, %%xmm6\n" \
84 "pxor %%xmm7, %%xmm7\n" \
85 "movdqa %%xmm0, (%0)\n\t" \
86 "movdqa %%xmm0, (%1)\n\t" \
87 : \
88 : "r" (tmp0), "r" (tmp1) \
89 : "memory"); \
90 } while (0)
91 #endif
92
93 /*
94 * Transform nblks*64 bytes (nblks*16 32-bit words) at DATA.
95 */
96 unsigned int ASM_FUNC_ATTR
97 _gcry_sha1_transform_intel_shaext(void *state, const unsigned char *data,
98 size_t nblks)
99 {
100 static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
101 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
102 char save_buf[2 * 16 + 15];
103 char *abcd_save;
104 char *e_save;
105 shaext_prepare_variable;
106
107 if (nblks == 0)
108 return 0;
109
110 shaext_prepare ();
111
112 asm volatile ("" : "=r" (abcd_save) : "0" (save_buf) : "memory");
113 abcd_save = abcd_save + (-(uintptr_t)abcd_save & 15);
114 e_save = abcd_save + 16;
115
116 /* byteswap mask => XMM7 */
117 asm volatile ("movdqa %[mask], %%xmm7\n\t" /* Preload mask */
118 :
119 : [mask] "m" (*be_mask)
120 : "memory");
121
122 /* Load state.. ABCD => XMM4, E => XMM5 */
123 asm volatile ("movd 16(%[state]), %%xmm5\n\t"
124 "movdqu (%[state]), %%xmm4\n\t"
125 "pslldq $12, %%xmm5\n\t"
126 "pshufd $0x1b, %%xmm4, %%xmm4\n\t"
127 "movdqa %%xmm5, (%[e_save])\n\t"
128 "movdqa %%xmm4, (%[abcd_save])\n\t"
129 :
130 : [state] "r" (state), [abcd_save] "r" (abcd_save),
131 [e_save] "r" (e_save)
132 : "memory" );
133
134 /* DATA => XMM[0..4] */
135 asm volatile ("movdqu 0(%[data]), %%xmm0\n\t"
136 "movdqu 16(%[data]), %%xmm1\n\t"
137 "movdqu 32(%[data]), %%xmm2\n\t"
138 "movdqu 48(%[data]), %%xmm3\n\t"
139 "pshufb %%xmm7, %%xmm0\n\t"
140 "pshufb %%xmm7, %%xmm1\n\t"
141 "pshufb %%xmm7, %%xmm2\n\t"
142 "pshufb %%xmm7, %%xmm3\n\t"
143 :
144 : [data] "r" (data)
145 : "memory" );
146 data += 64;
147
148 while (1)
149 {
150 /* Round 0..3 */
151 asm volatile ("paddd %%xmm0, %%xmm5\n\t"
152 "movdqa %%xmm4, %%xmm6\n\t" /* ABCD => E1 */
153 "sha1rnds4 $0, %%xmm5, %%xmm4\n\t"
154 ::: "memory" );
155
156 /* Round 4..7 */
157 asm volatile ("sha1nexte %%xmm1, %%xmm6\n\t"
158 "movdqa %%xmm4, %%xmm5\n\t"
159 "sha1rnds4 $0, %%xmm6, %%xmm4\n\t"
160 "sha1msg1 %%xmm1, %%xmm0\n\t"
161 ::: "memory" );
162
163 /* Round 8..11 */
164 asm volatile ("sha1nexte %%xmm2, %%xmm5\n\t"
165 "movdqa %%xmm4, %%xmm6\n\t"
166 "sha1rnds4 $0, %%xmm5, %%xmm4\n\t"
167 "sha1msg1 %%xmm2, %%xmm1\n\t"
168 "pxor %%xmm2, %%xmm0\n\t"
169 ::: "memory" );
170
171 #define ROUND(imm, E0, E1, MSG0, MSG1, MSG2, MSG3) \
172 asm volatile ("sha1nexte %%"MSG0", %%"E0"\n\t" \
173 "movdqa %%xmm4, %%"E1"\n\t" \
174 "sha1msg2 %%"MSG0", %%"MSG1"\n\t" \
175 "sha1rnds4 $"imm", %%"E0", %%xmm4\n\t" \
176 "sha1msg1 %%"MSG0", %%"MSG3"\n\t" \
177 "pxor %%"MSG0", %%"MSG2"\n\t" \
178 ::: "memory" )
179
180 /* Rounds 12..15 to 64..67 */
181 ROUND("0", "xmm6", "xmm5", "xmm3", "xmm0", "xmm1", "xmm2");
182 ROUND("0", "xmm5", "xmm6", "xmm0", "xmm1", "xmm2", "xmm3");
183 ROUND("1", "xmm6", "xmm5", "xmm1", "xmm2", "xmm3", "xmm0");
184 ROUND("1", "xmm5", "xmm6", "xmm2", "xmm3", "xmm0", "xmm1");
185 ROUND("1", "xmm6", "xmm5", "xmm3", "xmm0", "xmm1", "xmm2");
186 ROUND("1", "xmm5", "xmm6", "xmm0", "xmm1", "xmm2", "xmm3");
187 ROUND("1", "xmm6", "xmm5", "xmm1", "xmm2", "xmm3", "xmm0");
188 ROUND("2", "xmm5", "xmm6", "xmm2", "xmm3", "xmm0", "xmm1");
189 ROUND("2", "xmm6", "xmm5", "xmm3", "xmm0", "xmm1", "xmm2");
190 ROUND("2", "xmm5", "xmm6", "xmm0", "xmm1", "xmm2", "xmm3");
191 ROUND("2", "xmm6", "xmm5", "xmm1", "xmm2", "xmm3", "xmm0");
192 ROUND("2", "xmm5", "xmm6", "xmm2", "xmm3", "xmm0", "xmm1");
193 ROUND("3", "xmm6", "xmm5", "xmm3", "xmm0", "xmm1", "xmm2");
194 ROUND("3", "xmm5", "xmm6", "xmm0", "xmm1", "xmm2", "xmm3");
195
196 if (--nblks == 0)
197 break;
198
199 /* Round 68..71 */
200 asm volatile ("movdqu 0(%[data]), %%xmm0\n\t"
201 "sha1nexte %%xmm1, %%xmm6\n\t"
202 "movdqa %%xmm4, %%xmm5\n\t"
203 "sha1msg2 %%xmm1, %%xmm2\n\t"
204 "sha1rnds4 $3, %%xmm6, %%xmm4\n\t"
205 "pxor %%xmm1, %%xmm3\n\t"
206 "pshufb %%xmm7, %%xmm0\n\t"
207 :
208 : [data] "r" (data)
209 : "memory" );
210
211 /* Round 72..75 */
212 asm volatile ("movdqu 16(%[data]), %%xmm1\n\t"
213 "sha1nexte %%xmm2, %%xmm5\n\t"
214 "movdqa %%xmm4, %%xmm6\n\t"
215 "sha1msg2 %%xmm2, %%xmm3\n\t"
216 "sha1rnds4 $3, %%xmm5, %%xmm4\n\t"
217 "pshufb %%xmm7, %%xmm1\n\t"
218 :
219 : [data] "r" (data)
220 : "memory" );
221
222 /* Round 76..79 */
223 asm volatile ("movdqu 32(%[data]), %%xmm2\n\t"
224 "sha1nexte %%xmm3, %%xmm6\n\t"
225 "movdqa %%xmm4, %%xmm5\n\t"
226 "sha1rnds4 $3, %%xmm6, %%xmm4\n\t"
227 "pshufb %%xmm7, %%xmm2\n\t"
228 :
229 : [data] "r" (data)
230 : "memory" );
231
232 /* Merge states, store current. */
233 asm volatile ("movdqu 48(%[data]), %%xmm3\n\t"
234 "sha1nexte (%[e_save]), %%xmm5\n\t"
235 "paddd (%[abcd_save]), %%xmm4\n\t"
236 "pshufb %%xmm7, %%xmm3\n\t"
237 "movdqa %%xmm5, (%[e_save])\n\t"
238 "movdqa %%xmm4, (%[abcd_save])\n\t"
239 :
240 : [abcd_save] "r" (abcd_save), [e_save] "r" (e_save),
241 [data] "r" (data)
242 : "memory" );
243
244 data += 64;
245 }
246
247 /* Round 68..71 */
248 asm volatile ("sha1nexte %%xmm1, %%xmm6\n\t"
249 "movdqa %%xmm4, %%xmm5\n\t"
250 "sha1msg2 %%xmm1, %%xmm2\n\t"
251 "sha1rnds4 $3, %%xmm6, %%xmm4\n\t"
252 "pxor %%xmm1, %%xmm3\n\t"
253 ::: "memory" );
254
255 /* Round 72..75 */
256 asm volatile ("sha1nexte %%xmm2, %%xmm5\n\t"
257 "movdqa %%xmm4, %%xmm6\n\t"
258 "sha1msg2 %%xmm2, %%xmm3\n\t"
259 "sha1rnds4 $3, %%xmm5, %%xmm4\n\t"
260 ::: "memory" );
261
262 /* Round 76..79 */
263 asm volatile ("sha1nexte %%xmm3, %%xmm6\n\t"
264 "movdqa %%xmm4, %%xmm5\n\t"
265 "sha1rnds4 $3, %%xmm6, %%xmm4\n\t"
266 ::: "memory" );
267
268 /* Merge states. */
269 asm volatile ("sha1nexte (%[e_save]), %%xmm5\n\t"
270 "paddd (%[abcd_save]), %%xmm4\n\t"
271 :
272 : [abcd_save] "r" (abcd_save), [e_save] "r" (e_save)
273 : "memory" );
274
275 /* Save state */
276 asm volatile ("pshufd $0x1b, %%xmm4, %%xmm4\n\t"
277 "psrldq $12, %%xmm5\n\t"
278 "movdqu %%xmm4, (%[state])\n\t"
279 "movd %%xmm5, 16(%[state])\n\t"
280 :
281 : [state] "r" (state)
282 : "memory" );
283
284 shaext_cleanup (abcd_save, e_save);
285 return 0;
286 }
287
288 #if __clang__
289 # pragma clang attribute pop
290 #endif
291
292 #endif /* HAVE_GCC_INLINE_ASM_SHA_EXT */