Debian Codegraph @

cipher/cipher-gcm-riscv-zbb-zbc.c

raw
1 /* cipher-gcm-riscv-zbb-zbc.c - RISC-V Zbb+Zbc accelerated GHASH
2 * Copyright (C) 2025 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_RISCV_ZBB_ZBC
29
30 #define ALWAYS_INLINE inline __attribute__((always_inline))
31 #define NO_INLINE __attribute__((noinline))
32 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
33
34 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
35 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
36 #define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE
37
38 typedef struct { u64 val[2]; } u64x2;
39 typedef struct { u64x2 val[2]; } u64x2x2;
40
41 static ASM_FUNC_ATTR_INLINE u64x2
42 load_aligned_u64x2(const void *ptr)
43 {
44 u64x2 vec;
45
46 asm ("ld %0, 0(%1)"
47 : "=r" (vec.val[0])
48 : "r" (ptr)
49 : "memory");
50 asm ("ld %0, 8(%1)"
51 : "=r" (vec.val[1])
52 : "r" (ptr)
53 : "memory");
54
55 return vec;
56 }
57
58 static ASM_FUNC_ATTR_INLINE u64x2
59 load_unaligned_u64x2(const void *ptr)
60 {
61 if (((uintptr_t)ptr & 7) == 0)
62 {
63 /* aligned load */
64 return load_aligned_u64x2(ptr);
65 }
66 else
67 {
68 /* unaligned load */
69 const bufhelp_u64_t *ptr_u64 = ptr;
70 u64x2 vec;
71 vec.val[0] = ptr_u64[0].a;
72 vec.val[1] = ptr_u64[1].a;
73 return vec;
74 }
75 }
76
77 static ASM_FUNC_ATTR_INLINE void
78 store_aligned_u64x2(void *ptr, u64x2 vec)
79 {
80 asm ("sd %0, 0(%1)"
81 :
82 : "r" (vec.val[0]), "r" (ptr)
83 : "memory");
84 asm ("sd %0, 8(%1)"
85 :
86 : "r" (vec.val[1]), "r" (ptr)
87 : "memory");
88 }
89
90 static ASM_FUNC_ATTR_INLINE u64
91 byteswap_u64(u64 x)
92 {
93 asm (".option push;\n\t"
94 ".option arch, +zbb;\n\t"
95 "rev8 %0, %1;\n\t"
96 ".option pop;\n\t"
97 : "=r" (x)
98 : "r" (x));
99 return x;
100 }
101
102 static ASM_FUNC_ATTR_INLINE u64x2
103 byteswap_u64x2(u64x2 vec)
104 {
105 u64 tmp = byteswap_u64(vec.val[0]);
106 vec.val[0] = byteswap_u64(vec.val[1]);
107 vec.val[1] = tmp;
108 return vec;
109 }
110
111 static ASM_FUNC_ATTR_INLINE u64x2
112 veor_u64x2(u64x2 va, u64x2 vb)
113 {
114 va.val[0] ^= vb.val[0];
115 va.val[1] ^= vb.val[1];
116 return va;
117 }
118
119 /* 64x64 => 128 carry-less multiplication */
120 static ASM_FUNC_ATTR_INLINE u64x2
121 clmul_u64x2(u64 a, u64 b)
122 {
123 u64x2 vec;
124 asm (".option push;\n\t"
125 ".option arch, +zbc;\n\t"
126 "clmul %0, %1, %2;\n\t"
127 ".option pop;\n\t"
128 : "=r" (vec.val[0])
129 : "r" (a), "r" (b));
130 asm (".option push;\n\t"
131 ".option arch, +zbc;\n\t"
132 "clmulh %0, %1, %2;\n\t"
133 ".option pop;\n\t"
134 : "=r" (vec.val[1])
135 : "r" (a), "r" (b));
136 return vec;
137 }
138
139 /* GHASH functions.
140 *
141 * See "Gouvêa, C. P. L. & López, J. Implementing GCM on ARMv8. Topics in
142 * Cryptology — CT-RSA 2015" for details.
143 */
144 static ASM_FUNC_ATTR_INLINE u64x2x2
145 pmul_128x128(u64x2 a, u64x2 b)
146 {
147 u64 a_l = a.val[0];
148 u64 a_h = a.val[1];
149 u64 b_l = b.val[0];
150 u64 b_h = b.val[1];
151 u64 t1_h = b_l ^ b_h;
152 u64 t1_l = a_l ^ a_h;
153 u64x2 r0 = clmul_u64x2(a_l, b_l);
154 u64x2 r1 = clmul_u64x2(a_h, b_h);
155 u64x2 t2 = clmul_u64x2(t1_h, t1_l);
156 u64 t2_l, t2_h;
157 u64 r0_l, r0_h;
158 u64 r1_l, r1_h;
159
160 t2 = veor_u64x2(t2, r0);
161 t2 = veor_u64x2(t2, r1);
162
163 r0_l = r0.val[0];
164 r0_h = r0.val[1];
165 r1_l = r1.val[0];
166 r1_h = r1.val[1];
167 t2_l = t2.val[0];
168 t2_h = t2.val[1];
169
170 r0_h = r0_h ^ t2_l;
171 r1_l = r1_l ^ t2_h;
172
173 r0 = (const u64x2){ .val = { r0_l, r0_h } };
174 r1 = (const u64x2){ .val = { r1_l, r1_h } };
175
176 return (const u64x2x2){ .val = { r0, r1 } };
177 }
178
179 static ASM_FUNC_ATTR_INLINE u64x2
180 reduction(u64x2x2 r0r1)
181 {
182 static const u64 rconst = { U64_C(0xc200000000000000) };
183 u64x2 r0 = r0r1.val[0];
184 u64x2 r1 = r0r1.val[1];
185 u64x2 t = clmul_u64x2(r0.val[0], rconst);
186 r0.val[1] ^= t.val[0];
187 r1.val[0] ^= t.val[1];
188 t = clmul_u64x2(r0.val[1], rconst);
189 r1 = veor_u64x2(r1, t);
190 return veor_u64x2(r0, r1);
191 }
192
193 static ASM_FUNC_ATTR_INLINE unsigned int
194 ghash_polyval_riscv_zbb_zbc(gcry_cipher_hd_t c, byte *result, const byte *buf,
195 size_t nblocks, int is_polyval)
196 {
197 u64x2 rhash;
198 u64x2 rh1;
199 u64x2 rbuf;
200 u64x2x2 rr0rr1;
201
202 if (nblocks == 0)
203 return 0;
204
205 rhash = load_aligned_u64x2(result);
206 rh1 = load_aligned_u64x2(c->u_mode.gcm.u_ghash_key.key);
207
208 rhash = byteswap_u64x2(rhash);
209
210 rbuf = load_unaligned_u64x2(buf);
211 buf += 16;
212 nblocks--;
213
214 rbuf = is_polyval ? rbuf : byteswap_u64x2(rbuf);
215
216 rhash = veor_u64x2(rhash, rbuf);
217
218 while (nblocks)
219 {
220 rbuf = load_unaligned_u64x2(buf);
221 buf += 16;
222 nblocks--;
223
224 rr0rr1 = pmul_128x128(rhash, rh1);
225
226 rbuf = is_polyval ? rbuf : byteswap_u64x2(rbuf);
227
228 rhash = reduction(rr0rr1);
229
230 rhash = veor_u64x2(rhash, rbuf);
231 }
232
233 rr0rr1 = pmul_128x128(rhash, rh1);
234 rhash = reduction(rr0rr1);
235
236 rhash = byteswap_u64x2(rhash);
237
238 store_aligned_u64x2(result, rhash);
239
240 return 0;
241 }
242
243 ASM_FUNC_ATTR_NOINLINE unsigned int
244 _gcry_ghash_riscv_zbb_zbc(gcry_cipher_hd_t c, byte *result, const byte *buf,
245 size_t nblocks)
246 {
247 return ghash_polyval_riscv_zbb_zbc(c, result, buf, nblocks, 0);
248 }
249
250 ASM_FUNC_ATTR_NOINLINE unsigned int
251 _gcry_polyval_riscv_zbb_zbc(gcry_cipher_hd_t c, byte *result, const byte *buf,
252 size_t nblocks)
253 {
254 return ghash_polyval_riscv_zbb_zbc(c, result, buf, nblocks, 1);
255 }
256
257 static ASM_FUNC_ATTR_INLINE void
258 gcm_lsh_1(void *r_out, u64x2 i)
259 {
260 static const u64 rconst = { U64_C(0xc200000000000000) };
261 u64 ia = i.val[0];
262 u64 ib = i.val[1];
263 u64 oa, ob, ma;
264 u64x2 oa_ob;
265
266 ma = (u64)-(ib >> 63);
267 oa = ib >> 63;
268 ob = ia >> 63;
269 ma = ma & rconst;
270 ib = ib << 1;
271 ia = ia << 1;
272 ob = ob | ib;
273 oa = oa | ia;
274 ob = ob ^ ma;
275 oa_ob = (const u64x2){ .val = { oa, ob } };
276 store_aligned_u64x2(r_out, oa_ob);
277 }
278
279 ASM_FUNC_ATTR_NOINLINE void
280 _gcry_ghash_setup_riscv_zbb_zbc(gcry_cipher_hd_t c)
281 {
282 u64x2 rhash = load_aligned_u64x2(c->u_mode.gcm.u_ghash_key.key);
283
284 rhash = byteswap_u64x2(rhash);
285
286 gcm_lsh_1(c->u_mode.gcm.u_ghash_key.key, rhash);
287 }
288
289 #endif /* GCM_USE_RISCV_ZBB_ZBC */