Debian Codegraph @

cipher/sha512-ppc.c

raw
1 /* sha512-ppc.c - PowerPC vcrypto implementation of SHA-512 transform
2 * Copyright (C) 2019,2023 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 #if defined(ENABLE_PPC_CRYPTO_SUPPORT) && \
23 defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
24 defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \
25 defined(USE_SHA512) && \
26 __GNUC__ >= 4
27
28 #include "simd-common-ppc.h"
29 #include <altivec.h>
30 #include "bufhelp.h"
31
32
33 typedef vector unsigned char vector16x_u8;
34 typedef vector unsigned long long vector2x_u64;
35
36
37 #define ALWAYS_INLINE inline __attribute__((always_inline))
38 #define NO_INLINE __attribute__((noinline))
39 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
40
41 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
42 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
43 #define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE
44
45 #ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE
46 # define FUNC_ATTR_OPT_O2 __attribute__((optimize("-O2")))
47 #else
48 # define FUNC_ATTR_OPT_O2
49 #endif
50
51
52 #if defined(__clang__) && defined(HAVE_CLANG_ATTRIBUTE_PPC_TARGET)
53 # define FUNC_ATTR_TARGET_P8 __attribute__((target("arch=pwr8")))
54 # define FUNC_ATTR_TARGET_P9 __attribute__((target("arch=pwr9")))
55 #elif defined(HAVE_GCC_ATTRIBUTE_PPC_TARGET)
56 # define FUNC_ATTR_TARGET_P8 __attribute__((target("cpu=power8")))
57 # define FUNC_ATTR_TARGET_P9 __attribute__((target("cpu=power9")))
58 #else
59 # define FUNC_ATTR_TARGET_P8
60 # define FUNC_ATTR_TARGET_P9
61 #endif
62
63
64 static const vector2x_u64 K[80] =
65 {
66 { U64_C(0x428a2f98d728ae22), U64_C(0x7137449123ef65cd) },
67 { U64_C(0xb5c0fbcfec4d3b2f), U64_C(0xe9b5dba58189dbbc) },
68 { U64_C(0x3956c25bf348b538), U64_C(0x59f111f1b605d019) },
69 { U64_C(0x923f82a4af194f9b), U64_C(0xab1c5ed5da6d8118) },
70 { U64_C(0xd807aa98a3030242), U64_C(0x12835b0145706fbe) },
71 { U64_C(0x243185be4ee4b28c), U64_C(0x550c7dc3d5ffb4e2) },
72 { U64_C(0x72be5d74f27b896f), U64_C(0x80deb1fe3b1696b1) },
73 { U64_C(0x9bdc06a725c71235), U64_C(0xc19bf174cf692694) },
74 { U64_C(0xe49b69c19ef14ad2), U64_C(0xefbe4786384f25e3) },
75 { U64_C(0x0fc19dc68b8cd5b5), U64_C(0x240ca1cc77ac9c65) },
76 { U64_C(0x2de92c6f592b0275), U64_C(0x4a7484aa6ea6e483) },
77 { U64_C(0x5cb0a9dcbd41fbd4), U64_C(0x76f988da831153b5) },
78 { U64_C(0x983e5152ee66dfab), U64_C(0xa831c66d2db43210) },
79 { U64_C(0xb00327c898fb213f), U64_C(0xbf597fc7beef0ee4) },
80 { U64_C(0xc6e00bf33da88fc2), U64_C(0xd5a79147930aa725) },
81 { U64_C(0x06ca6351e003826f), U64_C(0x142929670a0e6e70) },
82 { U64_C(0x27b70a8546d22ffc), U64_C(0x2e1b21385c26c926) },
83 { U64_C(0x4d2c6dfc5ac42aed), U64_C(0x53380d139d95b3df) },
84 { U64_C(0x650a73548baf63de), U64_C(0x766a0abb3c77b2a8) },
85 { U64_C(0x81c2c92e47edaee6), U64_C(0x92722c851482353b) },
86 { U64_C(0xa2bfe8a14cf10364), U64_C(0xa81a664bbc423001) },
87 { U64_C(0xc24b8b70d0f89791), U64_C(0xc76c51a30654be30) },
88 { U64_C(0xd192e819d6ef5218), U64_C(0xd69906245565a910) },
89 { U64_C(0xf40e35855771202a), U64_C(0x106aa07032bbd1b8) },
90 { U64_C(0x19a4c116b8d2d0c8), U64_C(0x1e376c085141ab53) },
91 { U64_C(0x2748774cdf8eeb99), U64_C(0x34b0bcb5e19b48a8) },
92 { U64_C(0x391c0cb3c5c95a63), U64_C(0x4ed8aa4ae3418acb) },
93 { U64_C(0x5b9cca4f7763e373), U64_C(0x682e6ff3d6b2b8a3) },
94 { U64_C(0x748f82ee5defb2fc), U64_C(0x78a5636f43172f60) },
95 { U64_C(0x84c87814a1f0ab72), U64_C(0x8cc702081a6439ec) },
96 { U64_C(0x90befffa23631e28), U64_C(0xa4506cebde82bde9) },
97 { U64_C(0xbef9a3f7b2c67915), U64_C(0xc67178f2e372532b) },
98 { U64_C(0xca273eceea26619c), U64_C(0xd186b8c721c0c207) },
99 { U64_C(0xeada7dd6cde0eb1e), U64_C(0xf57d4f7fee6ed178) },
100 { U64_C(0x06f067aa72176fba), U64_C(0x0a637dc5a2c898a6) },
101 { U64_C(0x113f9804bef90dae), U64_C(0x1b710b35131c471b) },
102 { U64_C(0x28db77f523047d84), U64_C(0x32caab7b40c72493) },
103 { U64_C(0x3c9ebe0a15c9bebc), U64_C(0x431d67c49c100d4c) },
104 { U64_C(0x4cc5d4becb3e42b6), U64_C(0x597f299cfc657e2a) },
105 { U64_C(0x5fcb6fab3ad6faec), U64_C(0x6c44198c4a475817) }
106 };
107
108
109 static ASM_FUNC_ATTR_INLINE vector2x_u64
110 vec_rol_elems(vector2x_u64 v, unsigned int idx)
111 {
112 #ifndef WORDS_BIGENDIAN
113 return vec_sld (v, v, (16 - (8 * idx)) & 15);
114 #else
115 return vec_sld (v, v, (8 * idx) & 15);
116 #endif
117 }
118
119
120 static ASM_FUNC_ATTR_INLINE vector2x_u64
121 vec_merge_idx0_elems(vector2x_u64 v0, vector2x_u64 v1)
122 {
123 return vec_mergeh (v0, v1);
124 }
125
126
127 static ASM_FUNC_ATTR_INLINE vector2x_u64
128 vec_vshasigma_u64(vector2x_u64 v, unsigned int a, unsigned int b)
129 {
130 __asm__ ("vshasigmad %0,%1,%2,%3"
131 : "=v" (v)
132 : "v" (v), "g" (a), "g" (b)
133 : "memory");
134 return v;
135 }
136
137
138 static ASM_FUNC_ATTR_INLINE vector2x_u64
139 vec_add_u64(vector2x_u64 v, vector2x_u64 w)
140 {
141 __asm__ ("vaddudm %0,%1,%2"
142 : "=v" (v)
143 : "v" (v), "v" (w)
144 : "memory");
145 return v;
146 }
147
148
149 static ASM_FUNC_ATTR_INLINE vector2x_u64
150 vec_u64_load(unsigned long offset, const void *ptr)
151 {
152 vector2x_u64 vecu64;
153 #if __GNUC__ >= 4
154 if (__builtin_constant_p (offset) && offset == 0)
155 __asm__ ("lxvd2x %x0,0,%1\n\t"
156 : "=wa" (vecu64)
157 : "r" ((uintptr_t)ptr)
158 : "memory");
159 else
160 #endif
161 __asm__ ("lxvd2x %x0,%1,%2\n\t"
162 : "=wa" (vecu64)
163 : "r" (offset), "r" ((uintptr_t)ptr)
164 : "memory", "r0");
165 #ifndef WORDS_BIGENDIAN
166 __asm__ ("xxswapd %x0, %x1"
167 : "=wa" (vecu64)
168 : "wa" (vecu64));
169 #endif
170 return vecu64;
171 }
172
173
174 static ASM_FUNC_ATTR_INLINE void
175 vec_u64_store(vector2x_u64 vecu64, unsigned long offset, void *ptr)
176 {
177 #ifndef WORDS_BIGENDIAN
178 __asm__ ("xxswapd %x0, %x1"
179 : "=wa" (vecu64)
180 : "wa" (vecu64));
181 #endif
182 #if __GNUC__ >= 4
183 if (__builtin_constant_p (offset) && offset == 0)
184 __asm__ ("stxvd2x %x0,0,%1\n\t"
185 :
186 : "wa" (vecu64), "r" ((uintptr_t)ptr)
187 : "memory");
188 else
189 #endif
190 __asm__ ("stxvd2x %x0,%1,%2\n\t"
191 :
192 : "wa" (vecu64), "r" (offset), "r" ((uintptr_t)ptr)
193 : "memory", "r0");
194 }
195
196
197 static ASM_FUNC_ATTR_INLINE vector2x_u64
198 vec_u64_load_be(unsigned long offset, const void *ptr)
199 {
200 vector2x_u64 vecu64;
201 #if __GNUC__ >= 4
202 if (__builtin_constant_p (offset) && offset == 0)
203 __asm__ volatile ("lxvd2x %x0,0,%1\n\t"
204 : "=wa" (vecu64)
205 : "r" ((uintptr_t)ptr)
206 : "memory");
207 else
208 #endif
209 __asm__ volatile ("lxvd2x %x0,%1,%2\n\t"
210 : "=wa" (vecu64)
211 : "r" (offset), "r" ((uintptr_t)ptr)
212 : "memory", "r0");
213 #ifndef WORDS_BIGENDIAN
214 return (vector2x_u64)vec_reve((vector16x_u8)vecu64);
215 #else
216 return vecu64;
217 #endif
218 }
219
220
221 /* SHA2 round in vector registers */
222 #define R(a,b,c,d,e,f,g,h,ki,w) do \
223 { \
224 t1 = vec_add_u64((h), (w)); \
225 t2 = Cho((e),(f),(g)); \
226 t1 = vec_add_u64(t1, GETK(ki)); \
227 t1 = vec_add_u64(t1, t2); \
228 t1 = Sum1add(t1, e); \
229 t2 = Maj((a),(b),(c)); \
230 t2 = Sum0add(t2, a); \
231 h = vec_add_u64(t1, t2); \
232 d += t1; \
233 } while (0)
234
235 #define GETK(kidx) \
236 ({ \
237 if (((kidx) % 2) == 0) \
238 { \
239 ktmp = *(kptr++); \
240 if ((kidx) < 79) \
241 asm volatile("" : "+r" (kptr) :: "memory"); \
242 } \
243 else \
244 { \
245 ktmp = vec_mergel(ktmp, ktmp); \
246 } \
247 ktmp; \
248 })
249
250 #define Cho(b, c, d) (vec_sel(d, c, b))
251
252 #define Maj(c, d, b) (vec_sel(c, b, c ^ d))
253
254 #define Sum0(x) (vec_vshasigma_u64(x, 1, 0))
255
256 #define Sum1(x) (vec_vshasigma_u64(x, 1, 15))
257
258 #define S0(x) (vec_vshasigma_u64(x, 0, 0))
259
260 #define S1(x) (vec_vshasigma_u64(x, 0, 15))
261
262 #define Xadd(X, d, x) vec_add_u64(d, X(x))
263
264 #define Sum0add(d, x) Xadd(Sum0, d, x)
265
266 #define Sum1add(d, x) Xadd(Sum1, d, x)
267
268 #define S0add(d, x) Xadd(S0, d, x)
269
270 #define S1add(d, x) Xadd(S1, d, x)
271
272 #define I(i) \
273 ({ \
274 if (((i) % 2) == 0) \
275 { \
276 w[i] = vec_u64_load_be(0, data); \
277 data += 2 * 8; \
278 if ((i) / 2 < 7) \
279 asm volatile("" : "+r"(data) :: "memory"); \
280 } \
281 else \
282 { \
283 w[i] = vec_mergel(w[(i) - 1], w[(i) - 1]); \
284 } \
285 })
286
287 #define WN(i) ({ w[(i)&0x0f] += w[((i)-7) &0x0f]; \
288 w[(i)&0x0f] = S0add(w[(i)&0x0f], w[((i)-15)&0x0f]); \
289 w[(i)&0x0f] = S1add(w[(i)&0x0f], w[((i)-2) &0x0f]); })
290
291 #define W(i) ({ vector2x_u64 r = w[(i)&0x0f]; WN(i); r; })
292
293 #define L(i) w[(i)&0x0f]
294
295 #define I2(i) \
296 ({ \
297 if (((i) % 2) == 0) \
298 { \
299 w[i] = vec_u64_load_be(0, data); \
300 } \
301 else \
302 { \
303 vector2x_u64 it1 = vec_u64_load_be(128, data); \
304 vector2x_u64 it2 = vec_mergeh(w[(i) - 1], it1); \
305 w[i] = vec_mergel(w[(i) - 1], it1); \
306 w[(i) - 1] = it2; \
307 if ((i) < 15) \
308 { \
309 data += 2 * 8; \
310 asm volatile("" : "+r"(data) :: "memory"); \
311 } \
312 else \
313 { \
314 data += 2 * 8 + 128; \
315 asm volatile("" : "+r"(data) :: "memory"); \
316 } \
317 } \
318 })
319
320 #define W2(i) \
321 ({ \
322 vector2x_u64 wt1 = w[(i)&0x0f]; \
323 WN(i); \
324 w2[(i) / 2] = (((i) % 2) == 0) ? wt1 : vec_mergel(w2[(i) / 2], wt1); \
325 wt1; \
326 })
327
328 #define L2(i) \
329 ({ \
330 vector2x_u64 lt1 = w[(i)&0x0f]; \
331 w2[(i) / 2] = (((i) % 2) == 0) ? lt1 : vec_mergel(w2[(i) / 2], lt1); \
332 lt1; \
333 })
334
335 #define WL(i) \
336 ({ \
337 vector2x_u64 wlt1 = w2[(i) / 2]; \
338 if (((i) % 2) == 0 && (i) < 79) \
339 w2[(i) / 2] = vec_mergel(wlt1, wlt1); \
340 wlt1; \
341 })
342
343 static ASM_FUNC_ATTR_INLINE FUNC_ATTR_OPT_O2 unsigned int
344 sha512_transform_ppc(u64 state[8], const unsigned char *data, size_t nblks)
345 {
346 vector2x_u64 h0, h1, h2, h3, h4, h5, h6, h7;
347 vector2x_u64 a, b, c, d, e, f, g, h, t1, t2;
348 vector2x_u64 w[16];
349 vector2x_u64 w2[80 / 2];
350
351 h0 = vec_u64_load (8 * 0, (unsigned long long *)state);
352 h1 = vec_rol_elems (h0, 1);
353 h2 = vec_u64_load (8 * 2, (unsigned long long *)state);
354 h3 = vec_rol_elems (h2, 1);
355 h4 = vec_u64_load (8 * 4, (unsigned long long *)state);
356 h5 = vec_rol_elems (h4, 1);
357 h6 = vec_u64_load (8 * 6, (unsigned long long *)state);
358 h7 = vec_rol_elems (h6, 1);
359
360 while (nblks >= 2)
361 {
362 const vector2x_u64 *kptr = K;
363 vector2x_u64 ktmp;
364
365 a = h0;
366 b = h1;
367 c = h2;
368 d = h3;
369 e = h4;
370 f = h5;
371 g = h6;
372 h = h7;
373
374 I2(0); I2(1); I2(2); I2(3);
375 I2(4); I2(5); I2(6); I2(7);
376 I2(8); I2(9); I2(10); I2(11);
377 I2(12); I2(13); I2(14); I2(15);
378
379 R(a, b, c, d, e, f, g, h, 0, W2(0));
380 R(h, a, b, c, d, e, f, g, 1, W2(1));
381 R(g, h, a, b, c, d, e, f, 2, W2(2));
382 R(f, g, h, a, b, c, d, e, 3, W2(3));
383 R(e, f, g, h, a, b, c, d, 4, W2(4));
384 R(d, e, f, g, h, a, b, c, 5, W2(5));
385 R(c, d, e, f, g, h, a, b, 6, W2(6));
386 R(b, c, d, e, f, g, h, a, 7, W2(7));
387
388 R(a, b, c, d, e, f, g, h, 8, W2(8));
389 R(h, a, b, c, d, e, f, g, 9, W2(9));
390 R(g, h, a, b, c, d, e, f, 10, W2(10));
391 R(f, g, h, a, b, c, d, e, 11, W2(11));
392 R(e, f, g, h, a, b, c, d, 12, W2(12));
393 R(d, e, f, g, h, a, b, c, 13, W2(13));
394 R(c, d, e, f, g, h, a, b, 14, W2(14));
395 R(b, c, d, e, f, g, h, a, 15, W2(15));
396
397 R(a, b, c, d, e, f, g, h, 16, W2(16));
398 R(h, a, b, c, d, e, f, g, 17, W2(17));
399 R(g, h, a, b, c, d, e, f, 18, W2(18));
400 R(f, g, h, a, b, c, d, e, 19, W2(19));
401 R(e, f, g, h, a, b, c, d, 20, W2(20));
402 R(d, e, f, g, h, a, b, c, 21, W2(21));
403 R(c, d, e, f, g, h, a, b, 22, W2(22));
404 R(b, c, d, e, f, g, h, a, 23, W2(23));
405 R(a, b, c, d, e, f, g, h, 24, W2(24));
406 R(h, a, b, c, d, e, f, g, 25, W2(25));
407 R(g, h, a, b, c, d, e, f, 26, W2(26));
408 R(f, g, h, a, b, c, d, e, 27, W2(27));
409 R(e, f, g, h, a, b, c, d, 28, W2(28));
410 R(d, e, f, g, h, a, b, c, 29, W2(29));
411 R(c, d, e, f, g, h, a, b, 30, W2(30));
412 R(b, c, d, e, f, g, h, a, 31, W2(31));
413
414 R(a, b, c, d, e, f, g, h, 32, W2(32));
415 R(h, a, b, c, d, e, f, g, 33, W2(33));
416 R(g, h, a, b, c, d, e, f, 34, W2(34));
417 R(f, g, h, a, b, c, d, e, 35, W2(35));
418 R(e, f, g, h, a, b, c, d, 36, W2(36));
419 R(d, e, f, g, h, a, b, c, 37, W2(37));
420 R(c, d, e, f, g, h, a, b, 38, W2(38));
421 R(b, c, d, e, f, g, h, a, 39, W2(39));
422 R(a, b, c, d, e, f, g, h, 40, W2(40));
423 R(h, a, b, c, d, e, f, g, 41, W2(41));
424 R(g, h, a, b, c, d, e, f, 42, W2(42));
425 R(f, g, h, a, b, c, d, e, 43, W2(43));
426 R(e, f, g, h, a, b, c, d, 44, W2(44));
427 R(d, e, f, g, h, a, b, c, 45, W2(45));
428 R(c, d, e, f, g, h, a, b, 46, W2(46));
429 R(b, c, d, e, f, g, h, a, 47, W2(47));
430
431 R(a, b, c, d, e, f, g, h, 48, W2(48));
432 R(h, a, b, c, d, e, f, g, 49, W2(49));
433 R(g, h, a, b, c, d, e, f, 50, W2(50));
434 R(f, g, h, a, b, c, d, e, 51, W2(51));
435 R(e, f, g, h, a, b, c, d, 52, W2(52));
436 R(d, e, f, g, h, a, b, c, 53, W2(53));
437 R(c, d, e, f, g, h, a, b, 54, W2(54));
438 R(b, c, d, e, f, g, h, a, 55, W2(55));
439 R(a, b, c, d, e, f, g, h, 56, W2(56));
440 R(h, a, b, c, d, e, f, g, 57, W2(57));
441 R(g, h, a, b, c, d, e, f, 58, W2(58));
442 R(f, g, h, a, b, c, d, e, 59, W2(59));
443 R(e, f, g, h, a, b, c, d, 60, W2(60));
444 R(d, e, f, g, h, a, b, c, 61, W2(61));
445 R(c, d, e, f, g, h, a, b, 62, W2(62));
446 R(b, c, d, e, f, g, h, a, 63, W2(63));
447
448 R(a, b, c, d, e, f, g, h, 64, L2(64));
449 R(h, a, b, c, d, e, f, g, 65, L2(65));
450 R(g, h, a, b, c, d, e, f, 66, L2(66));
451 R(f, g, h, a, b, c, d, e, 67, L2(67));
452 R(e, f, g, h, a, b, c, d, 68, L2(68));
453 R(d, e, f, g, h, a, b, c, 69, L2(69));
454 R(c, d, e, f, g, h, a, b, 70, L2(70));
455 R(b, c, d, e, f, g, h, a, 71, L2(71));
456 R(a, b, c, d, e, f, g, h, 72, L2(72));
457 R(h, a, b, c, d, e, f, g, 73, L2(73));
458 R(g, h, a, b, c, d, e, f, 74, L2(74));
459 R(f, g, h, a, b, c, d, e, 75, L2(75));
460 R(e, f, g, h, a, b, c, d, 76, L2(76));
461 R(d, e, f, g, h, a, b, c, 77, L2(77));
462 R(c, d, e, f, g, h, a, b, 78, L2(78));
463 R(b, c, d, e, f, g, h, a, 79, L2(79));
464
465 h0 += a;
466 h1 += b;
467 h2 += c;
468 h3 += d;
469 h4 += e;
470 h5 += f;
471 h6 += g;
472 h7 += h;
473
474 kptr = K;
475
476 a = h0;
477 b = h1;
478 c = h2;
479 d = h3;
480 e = h4;
481 f = h5;
482 g = h6;
483 h = h7;
484
485 R(a, b, c, d, e, f, g, h, 0, WL(0));
486 R(h, a, b, c, d, e, f, g, 1, WL(1));
487 R(g, h, a, b, c, d, e, f, 2, WL(2));
488 R(f, g, h, a, b, c, d, e, 3, WL(3));
489 R(e, f, g, h, a, b, c, d, 4, WL(4));
490 R(d, e, f, g, h, a, b, c, 5, WL(5));
491 R(c, d, e, f, g, h, a, b, 6, WL(6));
492 R(b, c, d, e, f, g, h, a, 7, WL(7));
493
494 R(a, b, c, d, e, f, g, h, 8, WL(8));
495 R(h, a, b, c, d, e, f, g, 9, WL(9));
496 R(g, h, a, b, c, d, e, f, 10, WL(10));
497 R(f, g, h, a, b, c, d, e, 11, WL(11));
498 R(e, f, g, h, a, b, c, d, 12, WL(12));
499 R(d, e, f, g, h, a, b, c, 13, WL(13));
500 R(c, d, e, f, g, h, a, b, 14, WL(14));
501 R(b, c, d, e, f, g, h, a, 15, WL(15));
502
503 R(a, b, c, d, e, f, g, h, 16, WL(16));
504 R(h, a, b, c, d, e, f, g, 17, WL(17));
505 R(g, h, a, b, c, d, e, f, 18, WL(18));
506 R(f, g, h, a, b, c, d, e, 19, WL(19));
507 R(e, f, g, h, a, b, c, d, 20, WL(20));
508 R(d, e, f, g, h, a, b, c, 21, WL(21));
509 R(c, d, e, f, g, h, a, b, 22, WL(22));
510 R(b, c, d, e, f, g, h, a, 23, WL(23));
511 R(a, b, c, d, e, f, g, h, 24, WL(24));
512 R(h, a, b, c, d, e, f, g, 25, WL(25));
513 R(g, h, a, b, c, d, e, f, 26, WL(26));
514 R(f, g, h, a, b, c, d, e, 27, WL(27));
515 R(e, f, g, h, a, b, c, d, 28, WL(28));
516 R(d, e, f, g, h, a, b, c, 29, WL(29));
517 R(c, d, e, f, g, h, a, b, 30, WL(30));
518 R(b, c, d, e, f, g, h, a, 31, WL(31));
519
520 R(a, b, c, d, e, f, g, h, 32, WL(32));
521 R(h, a, b, c, d, e, f, g, 33, WL(33));
522 R(g, h, a, b, c, d, e, f, 34, WL(34));
523 R(f, g, h, a, b, c, d, e, 35, WL(35));
524 R(e, f, g, h, a, b, c, d, 36, WL(36));
525 R(d, e, f, g, h, a, b, c, 37, WL(37));
526 R(c, d, e, f, g, h, a, b, 38, WL(38));
527 R(b, c, d, e, f, g, h, a, 39, WL(39));
528 R(a, b, c, d, e, f, g, h, 40, WL(40));
529 R(h, a, b, c, d, e, f, g, 41, WL(41));
530 R(g, h, a, b, c, d, e, f, 42, WL(42));
531 R(f, g, h, a, b, c, d, e, 43, WL(43));
532 R(e, f, g, h, a, b, c, d, 44, WL(44));
533 R(d, e, f, g, h, a, b, c, 45, WL(45));
534 R(c, d, e, f, g, h, a, b, 46, WL(46));
535 R(b, c, d, e, f, g, h, a, 47, WL(47));
536
537 R(a, b, c, d, e, f, g, h, 48, WL(48));
538 R(h, a, b, c, d, e, f, g, 49, WL(49));
539 R(g, h, a, b, c, d, e, f, 50, WL(50));
540 R(f, g, h, a, b, c, d, e, 51, WL(51));
541 R(e, f, g, h, a, b, c, d, 52, WL(52));
542 R(d, e, f, g, h, a, b, c, 53, WL(53));
543 R(c, d, e, f, g, h, a, b, 54, WL(54));
544 R(b, c, d, e, f, g, h, a, 55, WL(55));
545 R(a, b, c, d, e, f, g, h, 56, WL(56));
546 R(h, a, b, c, d, e, f, g, 57, WL(57));
547 R(g, h, a, b, c, d, e, f, 58, WL(58));
548 R(f, g, h, a, b, c, d, e, 59, WL(59));
549 R(e, f, g, h, a, b, c, d, 60, WL(60));
550 R(d, e, f, g, h, a, b, c, 61, WL(61));
551 R(c, d, e, f, g, h, a, b, 62, WL(62));
552 R(b, c, d, e, f, g, h, a, 63, WL(63));
553
554 R(a, b, c, d, e, f, g, h, 64, WL(64));
555 R(h, a, b, c, d, e, f, g, 65, WL(65));
556 R(g, h, a, b, c, d, e, f, 66, WL(66));
557 R(f, g, h, a, b, c, d, e, 67, WL(67));
558 R(e, f, g, h, a, b, c, d, 68, WL(68));
559 R(d, e, f, g, h, a, b, c, 69, WL(69));
560 R(c, d, e, f, g, h, a, b, 70, WL(70));
561 R(b, c, d, e, f, g, h, a, 71, WL(71));
562 R(a, b, c, d, e, f, g, h, 72, WL(72));
563 R(h, a, b, c, d, e, f, g, 73, WL(73));
564 R(g, h, a, b, c, d, e, f, 74, WL(74));
565 R(f, g, h, a, b, c, d, e, 75, WL(75));
566 R(e, f, g, h, a, b, c, d, 76, WL(76));
567 R(d, e, f, g, h, a, b, c, 77, WL(77));
568 R(c, d, e, f, g, h, a, b, 78, WL(78));
569 R(b, c, d, e, f, g, h, a, 79, WL(79));
570
571 h0 += a;
572 h1 += b;
573 h2 += c;
574 h3 += d;
575 h4 += e;
576 h5 += f;
577 h6 += g;
578 h7 += h;
579
580 nblks -= 2;
581 }
582
583 if (nblks)
584 {
585 const vector2x_u64 *kptr = K;
586 vector2x_u64 ktmp;
587
588 a = h0;
589 b = h1;
590 c = h2;
591 d = h3;
592 e = h4;
593 f = h5;
594 g = h6;
595 h = h7;
596
597 I(0); I(1); I(2); I(3);
598 I(4); I(5); I(6); I(7);
599 I(8); I(9); I(10); I(11);
600 I(12); I(13); I(14); I(15);
601
602 R(a, b, c, d, e, f, g, h, 0, W(0));
603 R(h, a, b, c, d, e, f, g, 1, W(1));
604 R(g, h, a, b, c, d, e, f, 2, W(2));
605 R(f, g, h, a, b, c, d, e, 3, W(3));
606 R(e, f, g, h, a, b, c, d, 4, W(4));
607 R(d, e, f, g, h, a, b, c, 5, W(5));
608 R(c, d, e, f, g, h, a, b, 6, W(6));
609 R(b, c, d, e, f, g, h, a, 7, W(7));
610
611 R(a, b, c, d, e, f, g, h, 8, W(8));
612 R(h, a, b, c, d, e, f, g, 9, W(9));
613 R(g, h, a, b, c, d, e, f, 10, W(10));
614 R(f, g, h, a, b, c, d, e, 11, W(11));
615 R(e, f, g, h, a, b, c, d, 12, W(12));
616 R(d, e, f, g, h, a, b, c, 13, W(13));
617 R(c, d, e, f, g, h, a, b, 14, W(14));
618 R(b, c, d, e, f, g, h, a, 15, W(15));
619
620 R(a, b, c, d, e, f, g, h, 16, W(16));
621 R(h, a, b, c, d, e, f, g, 17, W(17));
622 R(g, h, a, b, c, d, e, f, 18, W(18));
623 R(f, g, h, a, b, c, d, e, 19, W(19));
624 R(e, f, g, h, a, b, c, d, 20, W(20));
625 R(d, e, f, g, h, a, b, c, 21, W(21));
626 R(c, d, e, f, g, h, a, b, 22, W(22));
627 R(b, c, d, e, f, g, h, a, 23, W(23));
628 R(a, b, c, d, e, f, g, h, 24, W(24));
629 R(h, a, b, c, d, e, f, g, 25, W(25));
630 R(g, h, a, b, c, d, e, f, 26, W(26));
631 R(f, g, h, a, b, c, d, e, 27, W(27));
632 R(e, f, g, h, a, b, c, d, 28, W(28));
633 R(d, e, f, g, h, a, b, c, 29, W(29));
634 R(c, d, e, f, g, h, a, b, 30, W(30));
635 R(b, c, d, e, f, g, h, a, 31, W(31));
636
637 R(a, b, c, d, e, f, g, h, 32, W(32));
638 R(h, a, b, c, d, e, f, g, 33, W(33));
639 R(g, h, a, b, c, d, e, f, 34, W(34));
640 R(f, g, h, a, b, c, d, e, 35, W(35));
641 R(e, f, g, h, a, b, c, d, 36, W(36));
642 R(d, e, f, g, h, a, b, c, 37, W(37));
643 R(c, d, e, f, g, h, a, b, 38, W(38));
644 R(b, c, d, e, f, g, h, a, 39, W(39));
645 R(a, b, c, d, e, f, g, h, 40, W(40));
646 R(h, a, b, c, d, e, f, g, 41, W(41));
647 R(g, h, a, b, c, d, e, f, 42, W(42));
648 R(f, g, h, a, b, c, d, e, 43, W(43));
649 R(e, f, g, h, a, b, c, d, 44, W(44));
650 R(d, e, f, g, h, a, b, c, 45, W(45));
651 R(c, d, e, f, g, h, a, b, 46, W(46));
652 R(b, c, d, e, f, g, h, a, 47, W(47));
653
654 R(a, b, c, d, e, f, g, h, 48, W(48));
655 R(h, a, b, c, d, e, f, g, 49, W(49));
656 R(g, h, a, b, c, d, e, f, 50, W(50));
657 R(f, g, h, a, b, c, d, e, 51, W(51));
658 R(e, f, g, h, a, b, c, d, 52, W(52));
659 R(d, e, f, g, h, a, b, c, 53, W(53));
660 R(c, d, e, f, g, h, a, b, 54, W(54));
661 R(b, c, d, e, f, g, h, a, 55, W(55));
662 R(a, b, c, d, e, f, g, h, 56, W(56));
663 R(h, a, b, c, d, e, f, g, 57, W(57));
664 R(g, h, a, b, c, d, e, f, 58, W(58));
665 R(f, g, h, a, b, c, d, e, 59, W(59));
666 R(e, f, g, h, a, b, c, d, 60, W(60));
667 R(d, e, f, g, h, a, b, c, 61, W(61));
668 R(c, d, e, f, g, h, a, b, 62, W(62));
669 R(b, c, d, e, f, g, h, a, 63, W(63));
670
671 R(a, b, c, d, e, f, g, h, 64, L(64));
672 R(h, a, b, c, d, e, f, g, 65, L(65));
673 R(g, h, a, b, c, d, e, f, 66, L(66));
674 R(f, g, h, a, b, c, d, e, 67, L(67));
675 R(e, f, g, h, a, b, c, d, 68, L(68));
676 R(d, e, f, g, h, a, b, c, 69, L(69));
677 R(c, d, e, f, g, h, a, b, 70, L(70));
678 R(b, c, d, e, f, g, h, a, 71, L(71));
679 R(a, b, c, d, e, f, g, h, 72, L(72));
680 R(h, a, b, c, d, e, f, g, 73, L(73));
681 R(g, h, a, b, c, d, e, f, 74, L(74));
682 R(f, g, h, a, b, c, d, e, 75, L(75));
683 R(e, f, g, h, a, b, c, d, 76, L(76));
684 R(d, e, f, g, h, a, b, c, 77, L(77));
685 R(c, d, e, f, g, h, a, b, 78, L(78));
686 R(b, c, d, e, f, g, h, a, 79, L(79));
687
688 h0 += a;
689 h1 += b;
690 h2 += c;
691 h3 += d;
692 h4 += e;
693 h5 += f;
694 h6 += g;
695 h7 += h;
696
697 nblks--;
698 }
699
700 h0 = vec_merge_idx0_elems (h0, h1);
701 h2 = vec_merge_idx0_elems (h2, h3);
702 h4 = vec_merge_idx0_elems (h4, h5);
703 h6 = vec_merge_idx0_elems (h6, h7);
704 vec_u64_store (h0, 8 * 0, (unsigned long long *)state);
705 vec_u64_store (h2, 8 * 2, (unsigned long long *)state);
706 vec_u64_store (h4, 8 * 4, (unsigned long long *)state);
707 vec_u64_store (h6, 8 * 6, (unsigned long long *)state);
708
709 clear_vec_regs();
710
711 return sizeof(w) + sizeof(w2);
712 }
713
714 unsigned int ASM_FUNC_ATTR FUNC_ATTR_TARGET_P8 FUNC_ATTR_OPT_O2
715 _gcry_sha512_transform_ppc8(u64 state[8], const unsigned char *data,
716 size_t nblks)
717 {
718 return sha512_transform_ppc(state, data, nblks);
719 }
720
721 unsigned int ASM_FUNC_ATTR FUNC_ATTR_TARGET_P9 FUNC_ATTR_OPT_O2
722 _gcry_sha512_transform_ppc9(u64 state[8], const unsigned char *data,
723 size_t nblks)
724 {
725 return sha512_transform_ppc(state, data, nblks);
726 }
727
728 #endif /* ENABLE_PPC_CRYPTO_SUPPORT */