Debian Codegraph @

cipher/cipher-gcm-aarch64-simd.c

raw
1 /* cipher-gcm-aarch64-simd.c - ARM/NEON accelerated GHASH
2 * Copyright (C) 2019-2024 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 #include "g10lib.h"
24 #include "cipher.h"
25 #include "bufhelp.h"
26 #include "./cipher-internal.h"
27
28 #ifdef GCM_USE_AARCH64
29
30 #include "simd-common-aarch64.h"
31 #include <arm_neon.h>
32
33 #define ALWAYS_INLINE inline __attribute__((always_inline))
34 #define NO_INLINE __attribute__((noinline))
35 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
36
37 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
38 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
39 #define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE
40
41 static ASM_FUNC_ATTR_INLINE uint64x2_t
42 byteswap_u64x2(uint64x2_t vec)
43 {
44 vec = (uint64x2_t)vrev64q_u8((uint8x16_t)vec);
45 vec = (uint64x2_t)vextq_u8((uint8x16_t)vec, (uint8x16_t)vec, 8);
46 return vec;
47 }
48
49 static ASM_FUNC_ATTR_INLINE uint64x2_t
50 veor_u64x2(uint64x2_t va, uint64x2_t vb)
51 {
52 return (uint64x2_t)veorq_u8((uint8x16_t)va, (uint8x16_t)vb);
53 }
54
55 static ASM_FUNC_ATTR_INLINE uint64x1_t
56 veor_u64x1(uint64x1_t va, uint64x1_t vb)
57 {
58 return (uint64x1_t)veor_u8((uint8x8_t)va, (uint8x8_t)vb);
59 }
60
61 static ASM_FUNC_ATTR_INLINE uint64x1_t
62 vand_u64x1(uint64x1_t va, uint64x1_t vb)
63 {
64 return (uint64x1_t)vand_u8((uint8x8_t)va, (uint8x8_t)vb);
65 }
66
67 static ASM_FUNC_ATTR_INLINE uint64x1_t
68 vorr_u64x1(uint64x1_t va, uint64x1_t vb)
69 {
70 return (uint64x1_t)vorr_u8((uint8x8_t)va, (uint8x8_t)vb);
71 }
72
73 /* 64x64=>128 carry-less multiplication using vmull.p8 instruction.
74 *
75 * From "Câmara, D.; Gouvêa, C. P. L.; López, J. & Dahab, R. Fast Software
76 * Polynomial Multiplication on ARM Processors using the NEON Engine. The
77 * Second International Workshop on Modern Cryptography and Security
78 * Engineering — MoCrySEn, 2013". */
79 static ASM_FUNC_ATTR_INLINE uint64x2_t
80 emulate_vmull_p64(uint64x1_t ad, uint64x1_t bd)
81 {
82 static const uint64x1_t k0 = { 0 };
83 static const uint64x1_t k16 = { U64_C(0xffff) };
84 static const uint64x1_t k32 = { U64_C(0xffffffff) };
85 static const uint64x1_t k48 = { U64_C(0xffffffffffff) };
86 uint64x1_t rl;
87 uint64x2_t rq;
88 uint64x1_t t0l;
89 uint64x1_t t0h;
90 uint64x2_t t0q;
91 uint64x1_t t1l;
92 uint64x1_t t1h;
93 uint64x2_t t1q;
94 uint64x1_t t2l;
95 uint64x1_t t2h;
96 uint64x2_t t2q;
97 uint64x1_t t3l;
98 uint64x1_t t3h;
99 uint64x2_t t3q;
100
101 t0l = (uint64x1_t)vext_u8((uint8x8_t)ad, (uint8x8_t)ad, 1);
102 t0q = (uint64x2_t)vmull_p8((poly8x8_t)t0l, (poly8x8_t)bd);
103
104 rl = (uint64x1_t)vext_u8((uint8x8_t)bd, (uint8x8_t)bd, 1);
105 rq = (uint64x2_t)vmull_p8((poly8x8_t)ad, (poly8x8_t)rl);
106
107 t1l = (uint64x1_t)vext_u8((uint8x8_t)ad, (uint8x8_t)ad, 2);
108 t1q = (uint64x2_t)vmull_p8((poly8x8_t)t1l, (poly8x8_t)bd);
109
110 t3l = (uint64x1_t)vext_u8((uint8x8_t)bd, (uint8x8_t)bd, 2);
111 t3q = (uint64x2_t)vmull_p8((poly8x8_t)ad, (poly8x8_t)t3l);
112
113 t2l = (uint64x1_t)vext_u8((uint8x8_t)ad, (uint8x8_t)ad, 3);
114 t2q = (uint64x2_t)vmull_p8((poly8x8_t)t2l, (poly8x8_t)bd);
115
116 t0q = veor_u64x2(t0q, rq);
117 t0l = vget_low_u64(t0q);
118 t0h = vget_high_u64(t0q);
119
120 rl = (uint64x1_t)vext_u8((uint8x8_t)bd, (uint8x8_t)bd, 3);
121 rq = (uint64x2_t)vmull_p8((poly8x8_t)ad, (poly8x8_t)rl);
122
123 t1q = veor_u64x2(t1q, t3q);
124 t1l = vget_low_u64(t1q);
125 t1h = vget_high_u64(t1q);
126
127 t3l = (uint64x1_t)vext_u8((uint8x8_t)bd, (uint8x8_t)bd, 4);
128 t3q = (uint64x2_t)vmull_p8((poly8x8_t)ad, (poly8x8_t)t3l);
129 t3l = vget_low_u64(t3q);
130 t3h = vget_high_u64(t3q);
131
132 t0l = veor_u64x1(t0l, t0h);
133 t0h = vand_u64x1(t0h, k48);
134 t1l = veor_u64x1(t1l, t1h);
135 t1h = vand_u64x1(t1h, k32);
136 t2q = veor_u64x2(t2q, rq);
137 t2l = vget_low_u64(t2q);
138 t2h = vget_high_u64(t2q);
139 t0l = veor_u64x1(t0l, t0h);
140 t1l = veor_u64x1(t1l, t1h);
141 t2l = veor_u64x1(t2l, t2h);
142 t2h = vand_u64x1(t2h, k16);
143 t3l = veor_u64x1(t3l, t3h);
144 t3h = k0;
145 t0q = vcombine_u64(t0l, t0h);
146 t0q = (uint64x2_t)vextq_u8((uint8x16_t)t0q, (uint8x16_t)t0q, 15);
147 t2l = veor_u64x1(t2l, t2h);
148 t1q = vcombine_u64(t1l, t1h);
149 t1q = (uint64x2_t)vextq_u8((uint8x16_t)t1q, (uint8x16_t)t1q, 14);
150 rq = (uint64x2_t)vmull_p8((poly8x8_t)ad, (poly8x8_t)bd);
151 t2q = vcombine_u64(t2l, t2h);
152 t2q = (uint64x2_t)vextq_u8((uint8x16_t)t2q, (uint8x16_t)t2q, 13);
153 t3q = vcombine_u64(t3l, t3h);
154 t3q = (uint64x2_t)vextq_u8((uint8x16_t)t3q, (uint8x16_t)t3q, 12);
155 t0q = veor_u64x2(t0q, t1q);
156 t2q = veor_u64x2(t2q, t3q);
157 rq = veor_u64x2(rq, t0q);
158 rq = veor_u64x2(rq, t2q);
159 return rq;
160 }
161
162 /* GHASH functions.
163 *
164 * See "Gouvêa, C. P. L. & López, J. Implementing GCM on ARMv8. Topics in
165 * Cryptology — CT-RSA 2015" for details.
166 */
167 static ASM_FUNC_ATTR_INLINE uint64x2x2_t
168 pmul_128x128(uint64x2_t a, uint64x2_t b)
169 {
170 uint64x1_t a_l = vget_low_u64(a);
171 uint64x1_t a_h = vget_high_u64(a);
172 uint64x1_t b_l = vget_low_u64(b);
173 uint64x1_t b_h = vget_high_u64(b);
174 uint64x1_t t1_h = veor_u64x1(b_l, b_h);
175 uint64x1_t t1_l = veor_u64x1(a_l, a_h);
176 uint64x2_t r0 = emulate_vmull_p64(a_l, b_l);
177 uint64x2_t r1 = emulate_vmull_p64(a_h, b_h);
178 uint64x2_t t2 = emulate_vmull_p64(t1_h, t1_l);
179 uint64x1_t t2_l, t2_h;
180 uint64x1_t r0_l, r0_h;
181 uint64x1_t r1_l, r1_h;
182
183 t2 = veor_u64x2(t2, r0);
184 t2 = veor_u64x2(t2, r1);
185
186 r0_l = vget_low_u64(r0);
187 r0_h = vget_high_u64(r0);
188 r1_l = vget_low_u64(r1);
189 r1_h = vget_high_u64(r1);
190 t2_l = vget_low_u64(t2);
191 t2_h = vget_high_u64(t2);
192
193 r0_h = veor_u64x1(r0_h, t2_l);
194 r1_l = veor_u64x1(r1_l, t2_h);
195
196 r0 = vcombine_u64(r0_l, r0_h);
197 r1 = vcombine_u64(r1_l, r1_h);
198
199 return (const uint64x2x2_t){ .val = { r0, r1 } };
200 }
201
202 /* Reduction using Xor and Shift.
203 *
204 * See "Shay Gueron, Michael E. Kounavis. Intel Carry-Less Multiplication
205 * Instruction and its Usage for Computing the GCM Mode" for details.
206 */
207 static ASM_FUNC_ATTR_INLINE uint64x2_t
208 reduction(uint64x2x2_t r0r1)
209 {
210 static const uint64x2_t k0 = { U64_C(0), U64_C(0) };
211 uint64x2_t r0 = r0r1.val[0];
212 uint64x2_t r1 = r0r1.val[1];
213 uint64x2_t t0q;
214 uint64x2_t t1q;
215 uint64x2_t t2q;
216 uint64x2_t t;
217
218 t0q = (uint64x2_t)vshlq_n_u32((uint32x4_t)r0, 31);
219 t1q = (uint64x2_t)vshlq_n_u32((uint32x4_t)r0, 30);
220 t2q = (uint64x2_t)vshlq_n_u32((uint32x4_t)r0, 25);
221 t0q = veor_u64x2(t0q, t1q);
222 t0q = veor_u64x2(t0q, t2q);
223 t = (uint64x2_t)vextq_u8((uint8x16_t)t0q, (uint8x16_t)k0, 4);
224 t0q = (uint64x2_t)vextq_u8((uint8x16_t)k0, (uint8x16_t)t0q, 16 - 12);
225 r0 = veor_u64x2(r0, t0q);
226 t0q = (uint64x2_t)vshrq_n_u32((uint32x4_t)r0, 1);
227 t1q = (uint64x2_t)vshrq_n_u32((uint32x4_t)r0, 2);
228 t2q = (uint64x2_t)vshrq_n_u32((uint32x4_t)r0, 7);
229 t0q = veor_u64x2(t0q, t1q);
230 t0q = veor_u64x2(t0q, t2q);
231 t0q = veor_u64x2(t0q, t);
232 r0 = veor_u64x2(r0, t0q);
233 return veor_u64x2(r0, r1);
234 }
235
236 ASM_FUNC_ATTR_NOINLINE unsigned int
237 _gcry_ghash_aarch64_simd(gcry_cipher_hd_t c, byte *result, const byte *buf,
238 size_t nblocks)
239 {
240 uint64x2_t rhash;
241 uint64x2_t rh1;
242 uint64x2_t rbuf;
243 uint64x2x2_t rr0rr1;
244
245 if (nblocks == 0)
246 return 0;
247
248 rhash = vld1q_u64((const void *)result);
249 rh1 = vld1q_u64((const void *)c->u_mode.gcm.u_ghash_key.key);
250
251 rhash = byteswap_u64x2(rhash);
252
253 rbuf = vld1q_u64((const void *)buf);
254 buf += 16;
255 nblocks--;
256
257 rbuf = byteswap_u64x2(rbuf);
258
259 rhash = veor_u64x2(rhash, rbuf);
260
261 while (nblocks)
262 {
263 rbuf = vld1q_u64((const void *)buf);
264 buf += 16;
265 nblocks--;
266
267 rr0rr1 = pmul_128x128(rhash, rh1);
268
269 rbuf = byteswap_u64x2(rbuf);
270
271 rhash = reduction(rr0rr1);
272
273 rhash = veor_u64x2(rhash, rbuf);
274 }
275
276 rr0rr1 = pmul_128x128(rhash, rh1);
277 rhash = reduction(rr0rr1);
278
279 rhash = byteswap_u64x2(rhash);
280
281 vst1q_u64((void *)result, rhash);
282
283 clear_vec_regs();
284
285 return 0;
286 }
287
288 static ASM_FUNC_ATTR_INLINE void
289 gcm_lsh_1(void *r_out, uint64x2_t i)
290 {
291 static const uint64x1_t const_d = { U64_C(0xc200000000000000) };
292 uint64x1_t ia = vget_low_u64(i);
293 uint64x1_t ib = vget_high_u64(i);
294 uint64x1_t oa, ob, ma;
295 uint64x1x2_t oa_ob;
296
297 ma = (uint64x1_t)vshr_n_s64((int64x1_t)ib, 63);
298 oa = vshr_n_u64(ib, 63);
299 ob = vshr_n_u64(ia, 63);
300 ma = vand_u64x1(ma, const_d);
301 ib = vshl_n_u64(ib, 1);
302 ia = vshl_n_u64(ia, 1);
303 ob = vorr_u64x1(ob, ib);
304 oa = vorr_u64x1(oa, ia);
305 ob = veor_u64x1(ob, ma);
306 oa_ob = (const uint64x1x2_t){ .val = { oa, ob } };
307 vst2_u64(r_out, oa_ob);
308 }
309
310 ASM_FUNC_ATTR_NOINLINE void
311 _gcry_ghash_setup_aarch64_simd(gcry_cipher_hd_t c)
312 {
313 uint64x2_t rhash = vld1q_u64((const void *)c->u_mode.gcm.u_ghash_key.key);
314
315 rhash = byteswap_u64x2(rhash);
316
317 gcm_lsh_1(c->u_mode.gcm.u_ghash_key.key, rhash);
318
319 clear_vec_regs();
320 }
321
322 #endif /* GCM_USE_AARCH64 */