Debian Codegraph @

cipher/chacha20-riscv-v.c

raw
1 /* chacha20-riscv-v.c - RISC-V vector implementation of ChaCha20
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 #if defined (__riscv) && \
23 defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS) && \
24 defined(USE_CHACHA20)
25
26 #include "simd-common-riscv.h"
27 #include <riscv_vector.h>
28 #include "bufhelp.h"
29
30
31 #define ALWAYS_INLINE inline __attribute__((always_inline))
32 #define NO_INLINE __attribute__((noinline))
33 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
34
35 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
36 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
37 #define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE
38
39
40 /**********************************************************************
41 RISC-V vector extension chacha20
42 **********************************************************************/
43
44 #define ROTATE16(v) __riscv_vreinterpret_v_u16m1_u32m1( \
45 __riscv_vrgather_vv_u16m1( \
46 __riscv_vreinterpret_v_u32m1_u16m1(v), \
47 rot16, vl * 2))
48 #define ROTATE8(v) __riscv_vreinterpret_v_u8m1_u32m1( \
49 __riscv_vrgather_vv_u8m1( \
50 __riscv_vreinterpret_v_u32m1_u8m1(v), \
51 rot8, vl * 4))
52 #define ROTATE(v, c) __riscv_vadd_vv_u32m1( \
53 __riscv_vsll_vx_u32m1((v), (c), vl), \
54 __riscv_vsrl_vx_u32m1((v), 32 - (c), vl), vl)
55 #define XOR(v, w) __riscv_vxor_vv_u32m1((v), (w), vl)
56 #define PLUS(v, w) __riscv_vadd_vv_u32m1((v), (w), vl)
57 #define WORD_ROL(v, c) __riscv_vrgather_vv_u32m1((v), (rol##c), vl)
58
59 #define QUARTERROUND_4(a0, b0, c0, d0, a1, b1, c1, d1, \
60 a2, b2, c2, d2, a3, b3, c3, d3) \
61 a0 = PLUS(a0, b0); a1 = PLUS(a1, b1); \
62 a2 = PLUS(a2, b2); a3 = PLUS(a3, b3); \
63 d0 = XOR(d0, a0); d1 = XOR(d1, a1); \
64 d2 = XOR(d2, a2); d3 = XOR(d3, a3); \
65 d0 = ROTATE16(d0); d1 = ROTATE16(d1); \
66 d2 = ROTATE16(d2); d3 = ROTATE16(d3); \
67 c0 = PLUS(c0, d0); c1 = PLUS(c1, d1); \
68 c2 = PLUS(c2, d2); c3 = PLUS(c3, d3); \
69 b0 = XOR(b0, c0); b1 = XOR(b1, c1); \
70 b2 = XOR(b2, c2); b3 = XOR(b3, c3); \
71 b0 = ROTATE(b0, 12); b1 = ROTATE(b1, 12); \
72 b2 = ROTATE(b2, 12); b3 = ROTATE(b3, 12); \
73 a0 = PLUS(a0, b0); a1 = PLUS(a1, b1); \
74 a2 = PLUS(a2, b2); a3 = PLUS(a3, b3); \
75 d0 = XOR(d0, a0); d1 = XOR(d1, a1); \
76 d2 = XOR(d2, a2); d3 = XOR(d3, a3); \
77 d0 = ROTATE8(d0); d1 = ROTATE8(d1); \
78 d2 = ROTATE8(d2); d3 = ROTATE8(d3); \
79 c0 = PLUS(c0, d0); c1 = PLUS(c1, d1); \
80 c2 = PLUS(c2, d2); c3 = PLUS(c3, d3); \
81 b0 = XOR(b0, c0); b1 = XOR(b1, c1); \
82 b2 = XOR(b2, c2); b3 = XOR(b3, c3); \
83 b0 = ROTATE(b0, 7); b1 = ROTATE(b1, 7); \
84 b2 = ROTATE(b2, 7); b3 = ROTATE(b3, 7);
85
86 #define QUARTERROUND4_2(x0, x1, x2, x3, y0, y1, y2, y3, rol_x1, rol_x2, rol_x3) \
87 x0 = PLUS(x0, x1); y0 = PLUS(y0, y1); \
88 x3 = XOR(x3, x0); y3 = XOR(y3, y0); \
89 x3 = ROTATE16(x3); y3 = ROTATE16(y3); \
90 x2 = PLUS(x2, x3); y2 = PLUS(y2, y3); \
91 x1 = XOR(x1, x2); y1 = XOR(y1, y2); \
92 x1 = ROTATE(x1, 12); y1 = ROTATE(y1, 12); \
93 x0 = PLUS(x0, x1); y0 = PLUS(y0, y1); \
94 x3 = XOR(x3, x0); y3 = XOR(y3, y0); \
95 x3 = ROTATE8(x3); y3 = ROTATE8(y3); \
96 x2 = PLUS(x2, x3); y2 = PLUS(y2, y3); \
97 x3 = WORD_ROL(x3, rol_x3); y3 = WORD_ROL(y3, rol_x3);\
98 x1 = XOR(x1, x2); y1 = XOR(y1, y2); \
99 x2 = WORD_ROL(x2, rol_x2); y2 = WORD_ROL(y2, rol_x2); \
100 x1 = ROTATE(x1, 7); y1 = ROTATE(y1, 7); \
101 x1 = WORD_ROL(x1, rol_x1); y1 = WORD_ROL(y1, rol_x1);
102
103 #define QUARTERROUND4(x0, x1, x2, x3, rol_x1, rol_x2, rol_x3) \
104 x0 = PLUS(x0, x1); x3 = XOR(x3, x0); x3 = ROTATE16(x3); \
105 x2 = PLUS(x2, x3); x1 = XOR(x1, x2); x1 = ROTATE(x1, 12); \
106 x0 = PLUS(x0, x1); x3 = XOR(x3, x0); x3 = ROTATE8(x3); \
107 x2 = PLUS(x2, x3); \
108 x3 = WORD_ROL(x3, rol_x3); \
109 x1 = XOR(x1, x2); \
110 x2 = WORD_ROL(x2, rol_x2); \
111 x1= ROTATE(x1, 7); \
112 x1 = WORD_ROL(x1, rol_x1);
113
114 #define ADD_U64(a, b) __riscv_vreinterpret_v_u64m1_u32m1( \
115 __riscv_vadd_vv_u64m1( \
116 __riscv_vreinterpret_v_u32m1_u64m1(a), \
117 __riscv_vreinterpret_v_u32m1_u64m1(b), vl / 2))
118
119 #define vxor_v_u32m1_u32m1x8(data, idx, vs, vl) \
120 __riscv_vset_v_u32m1_u32m1x8((data), (idx), \
121 __riscv_vxor_vv_u32m1( \
122 __riscv_vget_v_u32m1x8_u32m1((data), (idx)), (vs), (vl)))
123
124 static ASM_FUNC_ATTR_INLINE vuint16m1_t
125 gen_rot16(size_t vl)
126 {
127 return __riscv_vxor_vx_u16m1(__riscv_vid_v_u16m1(vl * 2), 1, vl * 2);
128 }
129
130 static ASM_FUNC_ATTR_INLINE vuint8m1_t
131 gen_rot8(size_t vl)
132 {
133 vuint8m1_t rot8, rot8_hi;
134
135 rot8 = __riscv_vid_v_u8m1(vl * 4);
136 rot8_hi = __riscv_vand_vx_u8m1(rot8, ~3, vl * 4);
137 rot8 = __riscv_vadd_vx_u8m1(rot8, 3, vl * 4);
138 rot8 = __riscv_vand_vx_u8m1(rot8, 3, vl * 4);
139 rot8 = __riscv_vadd_vv_u8m1(rot8, rot8_hi, vl * 4);
140
141 return rot8;
142 }
143
144 static ASM_FUNC_ATTR_INLINE vuint16m2_t
145 gen_indexes(size_t vl, size_t stride)
146 {
147 vuint16m2_t idx = __riscv_vid_v_u16m2(vl * 4);
148 vuint16m2_t idx_lo = __riscv_vand_vx_u16m2(idx, 3, vl * 4);
149 vuint16m2_t idx_hi = __riscv_vsrl_vx_u16m2(idx, 2, vl * 4);
150 idx_hi = __riscv_vmul_vx_u16m2(idx_hi, stride, vl * 4);
151 return __riscv_vadd_vv_u16m2(idx_hi, idx_lo, vl * 4);
152 }
153
154 static ASM_FUNC_ATTR_INLINE vuint32m1x8_t
155 unaligned_vlsseg8e32_v_u32m1x8(const void *src, size_t vl)
156 {
157 const byte *bsrc = src;
158 vuint16m2_t indexes;
159 vuint8m1_t b0, b1, b2, b3, b4, b5, b6, b7;
160 vuint32m1x8_t data;
161
162 if (LIKELY(((uintptr_t)src & 3) == 0))
163 {
164 /* Fast path for 32-bit aligned loads. */
165 return __riscv_vlsseg8e32_v_u32m1x8(src, 64, vl);
166 }
167
168 indexes = gen_indexes(4 * vl, 64);
169
170 b0 = __riscv_vluxei16_v_u8m1(bsrc + 0 * 4, indexes, vl * 4);
171 b1 = __riscv_vluxei16_v_u8m1(bsrc + 1 * 4, indexes, vl * 4);
172 b2 = __riscv_vluxei16_v_u8m1(bsrc + 2 * 4, indexes, vl * 4);
173 b3 = __riscv_vluxei16_v_u8m1(bsrc + 3 * 4, indexes, vl * 4);
174 b4 = __riscv_vluxei16_v_u8m1(bsrc + 4 * 4, indexes, vl * 4);
175 b5 = __riscv_vluxei16_v_u8m1(bsrc + 5 * 4, indexes, vl * 4);
176 b6 = __riscv_vluxei16_v_u8m1(bsrc + 6 * 4, indexes, vl * 4);
177 b7 = __riscv_vluxei16_v_u8m1(bsrc + 7 * 4, indexes, vl * 4);
178
179 data = __riscv_vundefined_u32m1x8();
180 data = __riscv_vset_v_u32m1_u32m1x8(
181 data, 0, __riscv_vreinterpret_v_u8m1_u32m1(b0));
182 data = __riscv_vset_v_u32m1_u32m1x8(
183 data, 1, __riscv_vreinterpret_v_u8m1_u32m1(b1));
184 data = __riscv_vset_v_u32m1_u32m1x8(
185 data, 2, __riscv_vreinterpret_v_u8m1_u32m1(b2));
186 data = __riscv_vset_v_u32m1_u32m1x8(
187 data, 3, __riscv_vreinterpret_v_u8m1_u32m1(b3));
188 data = __riscv_vset_v_u32m1_u32m1x8(
189 data, 4, __riscv_vreinterpret_v_u8m1_u32m1(b4));
190 data = __riscv_vset_v_u32m1_u32m1x8(
191 data, 5, __riscv_vreinterpret_v_u8m1_u32m1(b5));
192 data = __riscv_vset_v_u32m1_u32m1x8(
193 data, 6, __riscv_vreinterpret_v_u8m1_u32m1(b6));
194 data = __riscv_vset_v_u32m1_u32m1x8(
195 data, 7, __riscv_vreinterpret_v_u8m1_u32m1(b7));
196
197 return data;
198 }
199
200 static ASM_FUNC_ATTR_INLINE void
201 unaligned_vssseg8e32_v_u32m1x8(void *dst, vuint32m1x8_t data, size_t vl)
202 {
203 byte *bdst = dst;
204 vuint16m2_t indexes;
205 vuint8m1_t b0, b1, b2, b3, b4, b5, b6, b7;
206
207 if (LIKELY(((uintptr_t)dst & 3) == 0))
208 {
209 /* Fast path for 32-bit aligned stores. */
210 __riscv_vssseg8e32_v_u32m1x8(dst, 64, data, vl);
211 return;
212 }
213
214 indexes = gen_indexes(4 * vl, 64);
215
216 b0 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 0));
217 b1 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 1));
218 b2 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 2));
219 b3 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 3));
220 b4 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 4));
221 b5 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 5));
222 b6 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 6));
223 b7 = __riscv_vreinterpret_v_u32m1_u8m1(__riscv_vget_v_u32m1x8_u32m1(data, 7));
224
225 __riscv_vsuxei16_v_u8m1(bdst + 0 * 4, indexes, b0, vl * 4);
226 __riscv_vsuxei16_v_u8m1(bdst + 1 * 4, indexes, b1, vl * 4);
227 __riscv_vsuxei16_v_u8m1(bdst + 2 * 4, indexes, b2, vl * 4);
228 __riscv_vsuxei16_v_u8m1(bdst + 3 * 4, indexes, b3, vl * 4);
229 __riscv_vsuxei16_v_u8m1(bdst + 4 * 4, indexes, b4, vl * 4);
230 __riscv_vsuxei16_v_u8m1(bdst + 5 * 4, indexes, b5, vl * 4);
231 __riscv_vsuxei16_v_u8m1(bdst + 6 * 4, indexes, b6, vl * 4);
232 __riscv_vsuxei16_v_u8m1(bdst + 7 * 4, indexes, b7, vl * 4);
233 }
234
235 static ASM_FUNC_ATTR_INLINE unsigned int
236 chacha20_rvv_blocks(u32 *input, byte *dst, const byte *src, size_t nblks)
237 {
238 unsigned int i;
239
240 if (nblks == 0)
241 return 0;
242
243 /* Try use vector implementation when there is 4 or more blocks. */
244 if (nblks >= 4)
245 {
246 size_t vl = __riscv_vsetvl_e32m1(nblks) < 4
247 ? __riscv_vsetvl_e32m1(4) : __riscv_vsetvl_e32m1(nblks);
248 vuint32m1_t x0, x1, x2, x3, x4, x5, x6, x7;
249 vuint32m1_t x8, x9, x10, x11, x12, x13, x14, x15;
250 u32 s0, s1, s2, s3, s4, s5, s6, s7;
251 u32 s8, s9, s10, s11, s12, s13, s14, s15;
252 vuint16m1_t rot16 = gen_rot16(vl);
253 vuint8m1_t rot8 = gen_rot8(vl);
254
255 s0 = input[0];
256 s1 = input[1];
257 s2 = input[2];
258 s3 = input[3];
259 s4 = input[4];
260 s5 = input[5];
261 s6 = input[6];
262 s7 = input[7];
263 s8 = input[8];
264 s9 = input[9];
265 s10 = input[10];
266 s11 = input[11];
267 s12 = input[12];
268 s13 = input[13];
269 s14 = input[14];
270 s15 = input[15];
271
272 while (nblks >= 4)
273 {
274 vuint32m1_t ctr;
275 vbool32_t carry;
276 vuint32m1x8_t data;
277
278 if (vl < 4)
279 break;
280
281 x0 = __riscv_vmv_v_x_u32m1(s0, vl);
282 x1 = __riscv_vmv_v_x_u32m1(s1, vl);
283 x2 = __riscv_vmv_v_x_u32m1(s2, vl);
284 x3 = __riscv_vmv_v_x_u32m1(s3, vl);
285 x4 = __riscv_vmv_v_x_u32m1(s4, vl);
286 x5 = __riscv_vmv_v_x_u32m1(s5, vl);
287 x6 = __riscv_vmv_v_x_u32m1(s6, vl);
288 x7 = __riscv_vmv_v_x_u32m1(s7, vl);
289 x8 = __riscv_vmv_v_x_u32m1(s8, vl);
290 x9 = __riscv_vmv_v_x_u32m1(s9, vl);
291 x10 = __riscv_vmv_v_x_u32m1(s10, vl);
292 x11 = __riscv_vmv_v_x_u32m1(s11, vl);
293 x13 = __riscv_vmv_v_x_u32m1(s13, vl);
294 x14 = __riscv_vmv_v_x_u32m1(s14, vl);
295 x15 = __riscv_vmv_v_x_u32m1(s15, vl);
296
297 ctr = __riscv_vid_v_u32m1(vl);
298 carry = __riscv_vmadc_vx_u32m1_b32(ctr, s12, vl);
299 ctr = __riscv_vadd_vx_u32m1(ctr, s12, vl);
300 x12 = ctr;
301 x13 = __riscv_vadc_vxm_u32m1(x13, 0, carry, vl);
302
303 for (i = 20; i > 0; i -= 2)
304 {
305 QUARTERROUND_4(x0, x4, x8, x12,
306 x1, x5, x9, x13,
307 x2, x6, x10, x14,
308 x3, x7, x11, x15);
309 QUARTERROUND_4(x0, x5, x10, x15,
310 x1, x6, x11, x12,
311 x2, x7, x8, x13,
312 x3, x4, x9, x14);
313 }
314
315 x0 = __riscv_vadd_vx_u32m1(x0, s0, vl);
316 x1 = __riscv_vadd_vx_u32m1(x1, s1, vl);
317 x2 = __riscv_vadd_vx_u32m1(x2, s2, vl);
318 x3 = __riscv_vadd_vx_u32m1(x3, s3, vl);
319 x4 = __riscv_vadd_vx_u32m1(x4, s4, vl);
320 x5 = __riscv_vadd_vx_u32m1(x5, s5, vl);
321 x6 = __riscv_vadd_vx_u32m1(x6, s6, vl);
322 x7 = __riscv_vadd_vx_u32m1(x7, s7, vl);
323 x8 = __riscv_vadd_vx_u32m1(x8, s8, vl);
324 x9 = __riscv_vadd_vx_u32m1(x9, s9, vl);
325 x10 = __riscv_vadd_vx_u32m1(x10, s10, vl);
326 x11 = __riscv_vadd_vx_u32m1(x11, s11, vl);
327 x12 = __riscv_vadd_vv_u32m1(x12, ctr, vl);
328 x13 = __riscv_vadc_vxm_u32m1(x13, s13, carry, vl);
329 x14 = __riscv_vadd_vx_u32m1(x14, s14, vl);
330 x15 = __riscv_vadd_vx_u32m1(x15, s15, vl);
331
332 s12 += vl;
333 s13 += s12 < vl;
334
335 data = unaligned_vlsseg8e32_v_u32m1x8((const void *)src, vl);
336
337 data = vxor_v_u32m1_u32m1x8(data, 0, x0, vl);
338 data = vxor_v_u32m1_u32m1x8(data, 1, x1, vl);
339 data = vxor_v_u32m1_u32m1x8(data, 2, x2, vl);
340 data = vxor_v_u32m1_u32m1x8(data, 3, x3, vl);
341 data = vxor_v_u32m1_u32m1x8(data, 4, x4, vl);
342 data = vxor_v_u32m1_u32m1x8(data, 5, x5, vl);
343 data = vxor_v_u32m1_u32m1x8(data, 6, x6, vl);
344 data = vxor_v_u32m1_u32m1x8(data, 7, x7, vl);
345
346 unaligned_vssseg8e32_v_u32m1x8((void *)dst, data, vl);
347
348 data = unaligned_vlsseg8e32_v_u32m1x8((const void *)(src + 32), vl);
349
350 data = vxor_v_u32m1_u32m1x8(data, 0, x8, vl);
351 data = vxor_v_u32m1_u32m1x8(data, 1, x9, vl);
352 data = vxor_v_u32m1_u32m1x8(data, 2, x10, vl);
353 data = vxor_v_u32m1_u32m1x8(data, 3, x11, vl);
354 data = vxor_v_u32m1_u32m1x8(data, 4, x12, vl);
355 data = vxor_v_u32m1_u32m1x8(data, 5, x13, vl);
356 data = vxor_v_u32m1_u32m1x8(data, 6, x14, vl);
357 data = vxor_v_u32m1_u32m1x8(data, 7, x15, vl);
358
359 unaligned_vssseg8e32_v_u32m1x8((void *)(dst + 32), data, vl);
360
361 src += vl * 64;
362 dst += vl * 64;
363 nblks -= vl;
364 vl = __riscv_vsetvl_e32m1(nblks) < 4
365 ? __riscv_vsetvl_e32m1(4) : __riscv_vsetvl_e32m1(nblks);
366 }
367
368 input[12] = s12;
369 input[13] = s13;
370 }
371
372 /* Use SIMD implementation for remaining blocks. */
373 if (nblks > 0)
374 {
375 static const u32 rol_const[3][4] =
376 {
377 { 1, 2, 3, 0 },
378 { 2, 3, 0, 1 },
379 { 3, 0, 1, 2 }
380 };
381 static const u32 one_u64_const[4] = { 1, 0, 0, 0 };
382 size_t vl = 4;
383 vuint32m1_t rol1, rol2, rol3;
384 vuint32m1_t one_u64;
385 vuint32m1_t v0, v1, v2, v3;
386 vuint32m1_t v4, v5, v6, v7;
387 vuint32m1_t state0, state1, state2, state3;
388 vuint8m1_t i0, i1, i2, i3;
389 vuint8m1_t i4, i5, i6, i7;
390 vuint16m1_t rot16 = gen_rot16(vl);
391 vuint8m1_t rot8 = gen_rot8(vl);
392
393 rol1 = __riscv_vle32_v_u32m1(rol_const[0], vl);
394 rol2 = __riscv_vle32_v_u32m1(rol_const[1], vl);
395 rol3 = __riscv_vle32_v_u32m1(rol_const[2], vl);
396 one_u64 = __riscv_vle32_v_u32m1(one_u64_const, vl);
397
398 state0 = __riscv_vle32_v_u32m1(&input[0], vl);
399 state1 = __riscv_vle32_v_u32m1(&input[4], vl);
400 state2 = __riscv_vle32_v_u32m1(&input[8], vl);
401 state3 = __riscv_vle32_v_u32m1(&input[12], vl);
402
403 input[12] += nblks;
404 input[13] += input[12] < nblks;
405
406 /* SIMD 2x block implementation */
407 while (nblks >= 2)
408 {
409 v0 = state0;
410 v1 = state1;
411 v2 = state2;
412 v3 = state3;
413
414 v4 = state0;
415 v5 = state1;
416 v6 = state2;
417 v7 = state3;
418 v7 = ADD_U64(v7, one_u64);
419
420 i0 = __riscv_vle8_v_u8m1(src + 0 * 16, vl * 4);
421 i1 = __riscv_vle8_v_u8m1(src + 1 * 16, vl * 4);
422 i2 = __riscv_vle8_v_u8m1(src + 2 * 16, vl * 4);
423 i3 = __riscv_vle8_v_u8m1(src + 3 * 16, vl * 4);
424
425 for (i = 20; i > 0; i -= 2)
426 {
427 QUARTERROUND4_2(v0, v1, v2, v3, v4, v5, v6, v7, 1, 2, 3);
428 QUARTERROUND4_2(v0, v1, v2, v3, v4, v5, v6, v7, 3, 2, 1);
429 }
430
431 v0 = __riscv_vadd_vv_u32m1(v0, state0, vl);
432 v1 = __riscv_vadd_vv_u32m1(v1, state1, vl);
433 v2 = __riscv_vadd_vv_u32m1(v2, state2, vl);
434 v3 = __riscv_vadd_vv_u32m1(v3, state3, vl);
435 state3 = ADD_U64(state3, one_u64);
436
437 v0 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i0),
438 v0, vl);
439 v1 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i1),
440 v1, vl);
441 v2 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i2),
442 v2, vl);
443 v3 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i3),
444 v3, vl);
445
446 v4 = __riscv_vadd_vv_u32m1(v4, state0, vl);
447 v5 = __riscv_vadd_vv_u32m1(v5, state1, vl);
448 v6 = __riscv_vadd_vv_u32m1(v6, state2, vl);
449 v7 = __riscv_vadd_vv_u32m1(v7, state3, vl);
450 state3 = ADD_U64(state3, one_u64);
451
452 i4 = __riscv_vle8_v_u8m1(src + 4 * 16, vl * 4);
453 i5 = __riscv_vle8_v_u8m1(src + 5 * 16, vl * 4);
454 i6 = __riscv_vle8_v_u8m1(src + 6 * 16, vl * 4);
455 i7 = __riscv_vle8_v_u8m1(src + 7 * 16, vl * 4);
456
457 __riscv_vse8_v_u8m1(dst + 0 * 16,
458 __riscv_vreinterpret_v_u32m1_u8m1(v0), vl * 4);
459 __riscv_vse8_v_u8m1(dst + 1 * 16,
460 __riscv_vreinterpret_v_u32m1_u8m1(v1), vl * 4);
461 __riscv_vse8_v_u8m1(dst + 2 * 16,
462 __riscv_vreinterpret_v_u32m1_u8m1(v2), vl * 4);
463 __riscv_vse8_v_u8m1(dst + 3 * 16,
464 __riscv_vreinterpret_v_u32m1_u8m1(v3), vl * 4);
465
466 v4 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i4),
467 v4, vl);
468 v5 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i5),
469 v5, vl);
470 v6 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i6),
471 v6, vl);
472 v7 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i7),
473 v7, vl);
474
475 __riscv_vse8_v_u8m1(dst + 4 * 16,
476 __riscv_vreinterpret_v_u32m1_u8m1(v4), vl * 4);
477 __riscv_vse8_v_u8m1(dst + 5 * 16,
478 __riscv_vreinterpret_v_u32m1_u8m1(v5), vl * 4);
479 __riscv_vse8_v_u8m1(dst + 6 * 16,
480 __riscv_vreinterpret_v_u32m1_u8m1(v6), vl * 4);
481 __riscv_vse8_v_u8m1(dst + 7 * 16,
482 __riscv_vreinterpret_v_u32m1_u8m1(v7), vl * 4);
483
484 src += 2 * 64;
485 dst += 2 * 64;
486
487 nblks -= 2;
488 }
489
490 /* 1x block implementation */
491 while (nblks)
492 {
493 v0 = state0;
494 v1 = state1;
495 v2 = state2;
496 v3 = state3;
497
498 i0 = __riscv_vle8_v_u8m1(src + 0 * 16, vl * 4);
499 i1 = __riscv_vle8_v_u8m1(src + 1 * 16, vl * 4);
500 i2 = __riscv_vle8_v_u8m1(src + 2 * 16, vl * 4);
501 i3 = __riscv_vle8_v_u8m1(src + 3 * 16, vl * 4);
502
503 for (i = 20; i > 0; i -= 2)
504 {
505 QUARTERROUND4(v0, v1, v2, v3, 1, 2, 3);
506 QUARTERROUND4(v0, v1, v2, v3, 3, 2, 1);
507 }
508
509 v0 = __riscv_vadd_vv_u32m1(v0, state0, vl);
510 v1 = __riscv_vadd_vv_u32m1(v1, state1, vl);
511 v2 = __riscv_vadd_vv_u32m1(v2, state2, vl);
512 v3 = __riscv_vadd_vv_u32m1(v3, state3, vl);
513
514 state3 = ADD_U64(state3, one_u64);
515
516 v0 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i0),
517 v0, vl);
518 v1 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i1),
519 v1, vl);
520 v2 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i2),
521 v2, vl);
522 v3 = __riscv_vxor_vv_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(i3),
523 v3, vl);
524 __riscv_vse8_v_u8m1(dst + 0 * 16,
525 __riscv_vreinterpret_v_u32m1_u8m1(v0), vl * 4);
526 __riscv_vse8_v_u8m1(dst + 1 * 16,
527 __riscv_vreinterpret_v_u32m1_u8m1(v1), vl * 4);
528 __riscv_vse8_v_u8m1(dst + 2 * 16,
529 __riscv_vreinterpret_v_u32m1_u8m1(v2), vl * 4);
530 __riscv_vse8_v_u8m1(dst + 3 * 16,
531 __riscv_vreinterpret_v_u32m1_u8m1(v3), vl * 4);
532 src += 64;
533 dst += 64;
534
535 nblks--;
536 }
537 }
538
539 clear_vec_regs();
540
541 return 0;
542 }
543
544
545 #ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE
546 # define FUNC_ATTR_OPT_O2 __attribute__((optimize("-O2")))
547 #else
548 # define FUNC_ATTR_OPT_O2
549 #endif
550
551
552 unsigned int ASM_FUNC_ATTR FUNC_ATTR_OPT_O2
553 _gcry_chacha20_riscv_v_blocks(u32 *state, byte *dst, const byte *src,
554 size_t nblks)
555 {
556 return chacha20_rvv_blocks(state, dst, src, nblks);
557 }
558
559 unsigned int ASM_FUNC_ATTR FUNC_ATTR_OPT_O2
560 _gcry_chacha20_riscv_v_check_hw(void)
561 {
562 return (__riscv_vsetvl_e8m1(16) == 16);
563 }
564
565 #endif /* HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS */