Debian Codegraph @

cipher/cipher-gcm-ppc.c

raw
1 /* cipher-gcm-ppc.c - Power 8 vpmsum accelerated Galois Counter Mode
2 * implementation
3 * Copyright (C) 2019 Shawn Landden <[email protected]>
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Based on GHASH implementation by Andy Polyakov from CRYPTOGAMS
21 * distribution (ppc/ghashp8-ppc.pl). Specifically, it uses his register
22 * allocation (which then defers to your compiler's register allocation),
23 * instead of re-implementing Gerald Estrin's Scheme of parallelized
24 * multiplication of polynomials, as I did not understand this algorithm at
25 * the time.
26 *
27 * Original copyright license follows:
28 *
29 * Copyright (c) 2006, CRYPTOGAMS by <[email protected]>
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 *
36 * * Redistributions of source code must retain copyright notices,
37 * this list of conditions and the following disclaimer.
38 *
39 * * Redistributions in binary form must reproduce the above
40 * copyright notice, this list of conditions and the following
41 * disclaimer in the documentation and/or other materials
42 * provided with the distribution.
43 *
44 * * Neither the name of the CRYPTOGAMS nor the names of its
45 * copyright holder and contributors may be used to endorse or
46 * promote products derived from this software without specific
47 * prior written permission.
48 *
49 * ALTERNATIVELY, provided that this notice is retained in full, this
50 * product may be distributed under the terms of the GNU General Public
51 * License (GPL), in which case the provisions of the GPL apply INSTEAD OF
52 * those given above.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
67 */
68
69 #include <config.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <errno.h>
74 #include <stdint.h>
75
76 #include "g10lib.h"
77 #include "cipher.h"
78 #include "bufhelp.h"
79 #include "./cipher-internal.h"
80
81 #ifdef GCM_USE_PPC_VPMSUM
82
83 #include "simd-common-ppc.h"
84 #include <altivec.h>
85
86 #define ALWAYS_INLINE inline __attribute__((always_inline))
87 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
88
89 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
90 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
91
92 #define ALIGNED_16 __attribute__ ((aligned (16)))
93
94 typedef vector unsigned char vector16x_u8;
95 typedef vector signed char vector16x_s8;
96 typedef vector unsigned long long vector2x_u64;
97 typedef vector unsigned long long block;
98
99 static ASM_FUNC_ATTR_INLINE block
100 asm_xor(block a, block b)
101 {
102 block r;
103 __asm__ volatile ("xxlxor %x0, %x1, %x2"
104 : "=wa" (r)
105 : "wa" (a), "wa" (b));
106 return r;
107 }
108
109 static ASM_FUNC_ATTR_INLINE block
110 asm_vpmsumd(block a, block b)
111 {
112 block r;
113 __asm__ volatile ("vpmsumd %0, %1, %2"
114 : "=v" (r)
115 : "v" (a), "v" (b));
116 return r;
117 }
118
119 static ASM_FUNC_ATTR_INLINE block
120 asm_swap_u64(block a)
121 {
122 block r;
123 __asm__ volatile ("xxswapd %x0, %x1"
124 : "=wa" (r)
125 : "wa" (a));
126 return r;
127 }
128
129 static ASM_FUNC_ATTR_INLINE block
130 asm_mergelo(block l, block r)
131 {
132 block ret;
133 __asm__ volatile ("xxmrgld %x0, %x1, %x2\n\t"
134 : "=wa" (ret)
135 : "wa" (l), "wa" (r));
136 return ret;
137 }
138
139 static ASM_FUNC_ATTR_INLINE block
140 asm_mergehi(block l, block r)
141 {
142 block ret;
143 __asm__ volatile ("xxmrghd %x0, %x1, %x2\n\t"
144 : "=wa" (ret)
145 : "wa" (l), "wa" (r));
146 return ret;
147 }
148
149 static ASM_FUNC_ATTR_INLINE block
150 asm_rot_block_left(block a)
151 {
152 block r;
153 block zero = { 0, 0 };
154 __asm__ volatile ("xxmrgld %x0, %x1, %x2"
155 : "=wa" (r)
156 : "wa" (a), "wa" (zero));
157 return r;
158 }
159
160 static ASM_FUNC_ATTR_INLINE block
161 asm_rot_block_right(block a)
162 {
163 block r;
164 block zero = { 0, 0 };
165 __asm__ volatile ("xxsldwi %x0, %x2, %x1, 2"
166 : "=wa" (r)
167 : "wa" (a), "wa" (zero));
168 return r;
169 }
170
171 /* vsl is a slightly strange function in the way the shift is passed... */
172 static ASM_FUNC_ATTR_INLINE block
173 asm_ashl_128(block a, vector16x_u8 shift)
174 {
175 block r;
176 __asm__ volatile ("vsl %0, %1, %2"
177 : "=v" (r)
178 : "v" (a), "v" (shift));
179 return r;
180 }
181
182 #define STORE_TABLE(gcm_table, slot, vec) \
183 vec_store_he (((block)vec), slot * 16, (unsigned char *)(gcm_table));
184
185 static ASM_FUNC_ATTR_INLINE void
186 vec_store_he(block vec, unsigned long offset, unsigned char *ptr)
187 {
188 /* GCC vec_vsx_ld is generating two instructions on little-endian. Use
189 * lxvd2x directly instead. */
190 #if __GNUC__ >= 4
191 if (__builtin_constant_p (offset) && offset == 0)
192 __asm__ volatile ("stxvd2x %x0, 0, %1\n\t"
193 :
194 : "wa" (vec), "r" ((uintptr_t)ptr)
195 : "memory", "r0");
196 else
197 #endif
198 __asm__ volatile ("stxvd2x %x0, %1, %2\n\t"
199 :
200 : "wa" (vec), "r" (offset), "r" ((uintptr_t)ptr)
201 : "memory", "r0");
202 }
203
204 #define VEC_LOAD_BE(in_ptr, bswap_const) \
205 vec_be_swap(vec_load_he (0, (const unsigned char *)(in_ptr)), bswap_const)
206
207 static ASM_FUNC_ATTR_INLINE block
208 vec_load_he(unsigned long offset, const unsigned char *ptr)
209 {
210 block vec;
211 /* GCC vec_vsx_ld is generating two instructions on little-endian. Use
212 * lxvd2x directly instead. */
213 #if __GNUC__ >= 4
214 if (__builtin_constant_p (offset) && offset == 0)
215 __asm__ volatile ("lxvd2x %x0, 0, %1\n\t"
216 : "=wa" (vec)
217 : "r" ((uintptr_t)ptr)
218 : "memory", "r0");
219 else
220 #endif
221 __asm__ volatile ("lxvd2x %x0, %1, %2\n\t"
222 : "=wa" (vec)
223 : "r" (offset), "r" ((uintptr_t)ptr)
224 : "memory", "r0");
225 return vec;
226 }
227
228 static ASM_FUNC_ATTR_INLINE block
229 vec_be_swap(block vec, vector16x_u8 be_bswap_const)
230 {
231 #ifndef WORDS_BIGENDIAN
232 __asm__ volatile ("vperm %0, %1, %1, %2\n\t"
233 : "=v" (vec)
234 : "v" (vec), "v" (be_bswap_const));
235 #else
236 (void)be_bswap_const;
237 #endif
238 return vec;
239 }
240
241 static ASM_FUNC_ATTR_INLINE block
242 vec_dup_byte_elem(block vec, int idx)
243 {
244 #ifndef WORDS_BIGENDIAN
245 return (block)vec_splat((vector16x_s8)vec, idx);
246 #else
247 return (block)vec_splat((vector16x_s8)vec, (15 - idx) & 15);
248 #endif
249 }
250
251 /* Power ghash based on papers:
252 "The Galois/Counter Mode of Operation (GCM)"; David A. McGrew, John Viega
253 "IntelĀ® Carry-Less Multiplication Instruction and its Usage for Computing
254 the GCM Mode - Rev 2.01"; Shay Gueron, Michael E. Kounavis.
255
256 After saving the magic c2 constant and pre-formatted version of the key,
257 we pre-process the key for parallel hashing. This takes advantage of the
258 identity of addition over a galois field being identital to XOR, and thus
259 can be parellized (S 2.2, page 3). We multiply and add (galois field
260 versions) the key over multiple iterations and save the result. This can
261 later be galois added (XORed) with parallel processed input (Estrin's
262 Scheme).
263
264 The ghash "key" is a salt. */
265 void ASM_FUNC_ATTR
266 _gcry_ghash_setup_ppc_vpmsum (void *gcm_table_arg, void *gcm_key)
267 {
268 static const vector16x_u8 bswap_const ALIGNED_16 =
269 { ~7, ~6, ~5, ~4, ~3, ~2, ~1, ~0, ~15, ~14, ~13, ~12, ~11, ~10, ~9, ~8 };
270 static const byte c2[16] ALIGNED_16 =
271 { 0xc2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
272 static const vector16x_u8 one ALIGNED_16 =
273 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
274 uint64_t *gcm_table = gcm_table_arg;
275 block T0, T1, T2;
276 block C2, H, H1, H1l, H1h, H2, H2l, H2h;
277 block H3l, H3, H3h, H4l, H4, H4h, T3, T4;
278 vector16x_s8 most_sig_of_H, t7, carry;
279
280 H = VEC_LOAD_BE(gcm_key, bswap_const);
281 C2 = VEC_LOAD_BE(c2, bswap_const);
282 most_sig_of_H = (vector16x_s8)vec_dup_byte_elem(H, 15);
283 t7 = vec_splat_s8(7);
284 carry = most_sig_of_H >> t7;
285 carry &= (vector16x_s8)C2; /* only interested in certain carries. */
286 H1 = asm_ashl_128(H, one);
287 H1 ^= (block)carry; /* complete the <<< 1 */
288
289 T1 = asm_swap_u64 (H1);
290 H1l = asm_rot_block_right (T1);
291 H1h = asm_rot_block_left (T1);
292 C2 = asm_rot_block_right (C2);
293
294 STORE_TABLE (gcm_table, 0, C2);
295 STORE_TABLE (gcm_table, 1, H1l);
296 STORE_TABLE (gcm_table, 2, T1);
297 STORE_TABLE (gcm_table, 3, H1h);
298
299 /* pre-process coefficients for Gerald Estrin's scheme for parallel
300 * multiplication of polynomials
301 */
302 H2l = asm_vpmsumd (H1l, H1); /* do not need to mask in
303 because 0 * anything -> 0 */
304 H2 = asm_vpmsumd (T1, H1);
305 H2h = asm_vpmsumd (H1h, H1);
306
307 /* reduce 1 */
308 T0 = asm_vpmsumd (H2l, C2);
309
310 H2l ^= asm_rot_block_left (H2);
311 H2h ^= asm_rot_block_right (H2);
312 H2l = asm_swap_u64 (H2l);
313 H2l ^= T0;
314 /* reduce 2 */
315 T0 = asm_swap_u64 (H2l);
316 H2l = asm_vpmsumd (H2l, C2);
317 H2 = H2l ^ H2h ^ T0;
318
319 T2 = asm_swap_u64 (H2);
320 H2l = asm_rot_block_right (T2);
321 H2h = asm_rot_block_left (T2);
322
323 STORE_TABLE (gcm_table, 4, H2l);
324 STORE_TABLE (gcm_table, 5, T2);
325 STORE_TABLE (gcm_table, 6, H2h);
326
327 H3l = asm_vpmsumd (H2l, H1);
328 H4l = asm_vpmsumd (H2l, H2);
329 H3 = asm_vpmsumd (T2, H1);
330 H4 = asm_vpmsumd (T2, H2);
331 H3h = asm_vpmsumd (H2h, H1);
332 H4h = asm_vpmsumd (H2h, H2);
333
334 T3 = asm_vpmsumd (H3l, C2);
335 T4 = asm_vpmsumd (H4l, C2);
336
337 H3l ^= asm_rot_block_left (H3);
338 H3h ^= asm_rot_block_right (H3);
339 H4l ^= asm_rot_block_left (H4);
340 H4h ^= asm_rot_block_right (H4);
341
342 H3 = asm_swap_u64 (H3l);
343 H4 = asm_swap_u64 (H4l);
344
345 H3 ^= T3;
346 H4 ^= T4;
347
348 /* We could have also b64 switched reduce and reduce2, however as we are
349 using the unrotated H and H2 above to vpmsum, this is marginally better. */
350 T3 = asm_swap_u64 (H3);
351 T4 = asm_swap_u64 (H4);
352
353 H3 = asm_vpmsumd (H3, C2);
354 H4 = asm_vpmsumd (H4, C2);
355
356 T3 ^= H3h;
357 T4 ^= H4h;
358 H3 ^= T3;
359 H4 ^= T4;
360 H3 = asm_swap_u64 (H3);
361 H4 = asm_swap_u64 (H4);
362
363 H3l = asm_rot_block_right (H3);
364 H3h = asm_rot_block_left (H3);
365 H4l = asm_rot_block_right (H4);
366 H4h = asm_rot_block_left (H4);
367
368 STORE_TABLE (gcm_table, 7, H3l);
369 STORE_TABLE (gcm_table, 8, H3);
370 STORE_TABLE (gcm_table, 9, H3h);
371 STORE_TABLE (gcm_table, 10, H4l);
372 STORE_TABLE (gcm_table, 11, H4);
373 STORE_TABLE (gcm_table, 12, H4h);
374
375 clear_vec_regs();
376 }
377
378 unsigned int ASM_FUNC_ATTR
379 _gcry_ghash_ppc_vpmsum (byte *result, void *gcm_table,
380 const byte *buf, const size_t nblocks)
381 {
382 static const vector16x_u8 bswap_const ALIGNED_16 =
383 { ~7, ~6, ~5, ~4, ~3, ~2, ~1, ~0, ~15, ~14, ~13, ~12, ~11, ~10, ~9, ~8 };
384 block c2, H0l, H0m, H0h, H4l, H4m, H4h, H2m, H3l, H3m, H3h, Hl;
385 block Hm, Hh, in, in0, in1, in2, in3, Hm_right, Hl_rotate, cur;
386 size_t blocks_remaining = nblocks;
387 size_t not_multiple_of_four;
388 block t0;
389
390 cur = vec_be_swap (vec_load_he (0, result), bswap_const);
391
392 c2 = vec_load_he (0, gcm_table);
393 H0l = vec_load_he (16, gcm_table);
394 H0m = vec_load_he (32, gcm_table);
395 H0h = vec_load_he (48, gcm_table);
396
397 for (not_multiple_of_four = nblocks % 4; not_multiple_of_four;
398 not_multiple_of_four--)
399 {
400 in = vec_be_swap (vec_load_he (0, buf), bswap_const);
401 buf += 16;
402 blocks_remaining--;
403 cur ^= in;
404
405 Hl = asm_vpmsumd (cur, H0l);
406 Hm = asm_vpmsumd (cur, H0m);
407 Hh = asm_vpmsumd (cur, H0h);
408
409 t0 = asm_vpmsumd (Hl, c2);
410
411 Hl ^= asm_rot_block_left (Hm);
412
413 Hm_right = asm_rot_block_right (Hm);
414 Hh ^= Hm_right;
415 Hl_rotate = asm_swap_u64 (Hl);
416 Hl_rotate ^= t0;
417 Hl = asm_swap_u64 (Hl_rotate);
418 Hl_rotate = asm_vpmsumd (Hl_rotate, c2);
419 Hl ^= Hh;
420 Hl ^= Hl_rotate;
421
422 cur = Hl;
423 }
424
425 if (blocks_remaining > 0)
426 {
427 block Xl, Xm, Xh, Xl1, Xm1, Xh1, Xm2, Xl3, Xm3, Xh3, Xl_rotate;
428 block H21l, H21h, merge_l, merge_h;
429 block t1, t2;
430
431 H2m = vec_load_he (48 + 32, gcm_table);
432 H3l = vec_load_he (48 * 2 + 16, gcm_table);
433 H3m = vec_load_he (48 * 2 + 32, gcm_table);
434 H3h = vec_load_he (48 * 2 + 48, gcm_table);
435 H4l = vec_load_he (48 * 3 + 16, gcm_table);
436 H4m = vec_load_he (48 * 3 + 32, gcm_table);
437 H4h = vec_load_he (48 * 3 + 48, gcm_table);
438
439 in0 = vec_load_he (0, buf);
440 in1 = vec_load_he (16, buf);
441 in2 = vec_load_he (32, buf);
442 in3 = vec_load_he (48, buf);
443 buf += 64;
444 in0 = vec_be_swap(in0, bswap_const);
445 in1 = vec_be_swap(in1, bswap_const);
446 in2 = vec_be_swap(in2, bswap_const);
447 in3 = vec_be_swap(in3, bswap_const);
448
449 Xh = asm_xor (in0, cur);
450
451 Xl1 = asm_vpmsumd (in1, H3l);
452 Xm1 = asm_vpmsumd (in1, H3m);
453 Xh1 = asm_vpmsumd (in1, H3h);
454
455 H21l = asm_mergehi (H2m, H0m);
456 H21h = asm_mergelo (H2m, H0m);
457 merge_l = asm_mergelo (in2, in3);
458 merge_h = asm_mergehi (in2, in3);
459
460 Xm2 = asm_vpmsumd (in2, H2m);
461 Xl3 = asm_vpmsumd (merge_l, H21l);
462 Xm3 = asm_vpmsumd (in3, H0m);
463 Xh3 = asm_vpmsumd (merge_h, H21h);
464
465 Xm2 = asm_xor (Xm2, Xm1);
466 Xl3 = asm_xor (Xl3, Xl1);
467 Xm3 = asm_xor (Xm3, Xm2);
468 Xh3 = asm_xor (Xh3, Xh1);
469
470 /* Gerald Estrin's scheme for parallel multiplication of polynomials */
471 for (; blocks_remaining > 4; blocks_remaining -= 4)
472 {
473 in0 = vec_load_he (0, buf);
474 in1 = vec_load_he (16, buf);
475 in2 = vec_load_he (32, buf);
476 in3 = vec_load_he (48, buf);
477 buf += 64;
478 in1 = vec_be_swap(in1, bswap_const);
479 in2 = vec_be_swap(in2, bswap_const);
480 in3 = vec_be_swap(in3, bswap_const);
481 in0 = vec_be_swap(in0, bswap_const);
482
483 Xl = asm_vpmsumd (Xh, H4l);
484 Xm = asm_vpmsumd (Xh, H4m);
485 Xh = asm_vpmsumd (Xh, H4h);
486 Xl1 = asm_vpmsumd (in1, H3l);
487 Xm1 = asm_vpmsumd (in1, H3m);
488 Xh1 = asm_vpmsumd (in1, H3h);
489
490 Xl = asm_xor (Xl, Xl3);
491 Xm = asm_xor (Xm, Xm3);
492 Xh = asm_xor (Xh, Xh3);
493 merge_l = asm_mergelo (in2, in3);
494 merge_h = asm_mergehi (in2, in3);
495
496 t0 = asm_vpmsumd (Xl, c2);
497 Xl3 = asm_vpmsumd (merge_l, H21l);
498 Xh3 = asm_vpmsumd (merge_h, H21h);
499
500 t1 = asm_rot_block_left (Xm);
501 t2 = asm_rot_block_right (Xm);
502 Xl = asm_xor(Xl, t1);
503 Xh = asm_xor(Xh, t2);
504
505 Xl = asm_swap_u64 (Xl);
506 Xl = asm_xor(Xl, t0);
507
508 Xl_rotate = asm_swap_u64 (Xl);
509 Xm2 = asm_vpmsumd (in2, H2m);
510 Xm3 = asm_vpmsumd (in3, H0m);
511 Xl = asm_vpmsumd (Xl, c2);
512
513 Xl3 = asm_xor (Xl3, Xl1);
514 Xh3 = asm_xor (Xh3, Xh1);
515 Xh = asm_xor (Xh, in0);
516 Xm2 = asm_xor (Xm2, Xm1);
517 Xh = asm_xor (Xh, Xl_rotate);
518 Xm3 = asm_xor (Xm3, Xm2);
519 Xh = asm_xor (Xh, Xl);
520 }
521
522 Xl = asm_vpmsumd (Xh, H4l);
523 Xm = asm_vpmsumd (Xh, H4m);
524 Xh = asm_vpmsumd (Xh, H4h);
525
526 Xl = asm_xor (Xl, Xl3);
527 Xm = asm_xor (Xm, Xm3);
528
529 t0 = asm_vpmsumd (Xl, c2);
530
531 Xh = asm_xor (Xh, Xh3);
532 t1 = asm_rot_block_left (Xm);
533 t2 = asm_rot_block_right (Xm);
534 Xl = asm_xor (Xl, t1);
535 Xh = asm_xor (Xh, t2);
536
537 Xl = asm_swap_u64 (Xl);
538 Xl = asm_xor (Xl, t0);
539
540 Xl_rotate = asm_swap_u64 (Xl);
541 Xl = asm_vpmsumd (Xl, c2);
542 Xh = asm_xor (Xh, Xl_rotate);
543 cur = asm_xor (Xh, Xl);
544 }
545
546 vec_store_he (vec_be_swap (cur, bswap_const), 0, result);
547
548 clear_vec_regs();
549
550 return 0;
551 }
552
553 #endif /* GCM_USE_PPC_VPMSUM */