Debian Codegraph @

cipher/rijndael-riscv-zvkned.c

raw
1 /* rijndael-riscv-zvkned.c - RISC-V vector crypto implementation of AES
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(HAVE_COMPATIBLE_CC_RISCV_VECTOR_CRYPTO_INTRINSICS)
25
26 #include "g10lib.h"
27 #include "simd-common-riscv.h"
28 #include "rijndael-internal.h"
29 #include "cipher-internal.h"
30
31 #include <riscv_vector.h>
32
33
34 #define ALWAYS_INLINE inline __attribute__((always_inline))
35 #define NO_INLINE __attribute__((noinline))
36 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
37
38 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
39 #define ASM_FUNC_ATTR_INLINE ALWAYS_INLINE ASM_FUNC_ATTR
40 #define ASM_FUNC_ATTR_NOINLINE NO_INLINE ASM_FUNC_ATTR
41
42 #ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE
43 # define FUNC_ATTR_OPT_O2 __attribute__((optimize("-O2")))
44 #else
45 # define FUNC_ATTR_OPT_O2
46 #endif
47
48
49 /*
50 * Helper macro and functions
51 */
52
53 #define cast_u8m1_u32m1(a) __riscv_vreinterpret_v_u8m1_u32m1(a)
54 #define cast_u8m1_u64m1(a) __riscv_vreinterpret_v_u8m1_u64m1(a)
55 #define cast_u32m1_u8m1(a) __riscv_vreinterpret_v_u32m1_u8m1(a)
56 #define cast_u32m1_u64m1(a) __riscv_vreinterpret_v_u32m1_u64m1(a)
57 #define cast_u64m1_u8m1(a) __riscv_vreinterpret_v_u64m1_u8m1(a)
58
59 #define cast_u8m2_u32m2(a) __riscv_vreinterpret_v_u8m2_u32m2(a)
60 #define cast_u32m2_u8m2(a) __riscv_vreinterpret_v_u32m2_u8m2(a)
61
62 #define cast_u8m4_u32m4(a) __riscv_vreinterpret_v_u8m4_u32m4(a)
63 #define cast_u32m4_u8m4(a) __riscv_vreinterpret_v_u32m4_u8m4(a)
64
65 #define cast_u64m1_u32m1(a) __riscv_vreinterpret_v_u64m1_u32m1(a)
66 #define cast_u32m1_u64m1(a) __riscv_vreinterpret_v_u32m1_u64m1(a)
67
68 #define cast_u64m1_i64m1(a) __riscv_vreinterpret_v_u64m1_i64m1(a)
69 #define cast_i64m1_u64m1(a) __riscv_vreinterpret_v_i64m1_u64m1(a)
70
71 #define memory_barrier_with_vec(a) __asm__("" : "+vr"(a) :: "memory")
72
73
74 static ASM_FUNC_ATTR_INLINE vuint32m1_t
75 bswap128_u32m1(vuint32m1_t vec, size_t vl_u32)
76 {
77 static const byte bswap128_arr[16] =
78 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
79 size_t vl_bytes = vl_u32 * 4;
80 vuint8m1_t bswap128 = __riscv_vle8_v_u8m1(bswap128_arr, vl_bytes);
81
82 return cast_u8m1_u32m1(
83 __riscv_vrgather_vv_u8m1(cast_u32m1_u8m1(vec), bswap128, vl_bytes));
84 }
85
86 static ASM_FUNC_ATTR_INLINE vuint32m1_t
87 unaligned_load_u32m1(const void *ptr, size_t vl_u32)
88 {
89 size_t vl_bytes = vl_u32 * 4;
90
91 return cast_u8m1_u32m1(__riscv_vle8_v_u8m1(ptr, vl_bytes));
92 }
93
94 static ASM_FUNC_ATTR_INLINE void
95 unaligned_store_u32m1(void *ptr, vuint32m1_t vec, size_t vl_u32)
96 {
97 size_t vl_bytes = vl_u32 * 4;
98
99 __riscv_vse8_v_u8m1(ptr, cast_u32m1_u8m1(vec), vl_bytes);
100 }
101
102 static ASM_FUNC_ATTR_INLINE vuint32m4_t
103 unaligned_load_u32m4(const void *ptr, size_t vl_u32)
104 {
105 size_t vl_bytes = vl_u32 * 4;
106
107 return cast_u8m4_u32m4(__riscv_vle8_v_u8m4(ptr, vl_bytes));
108 }
109
110 static ASM_FUNC_ATTR_INLINE void
111 unaligned_store_u32m4(void *ptr, vuint32m4_t vec, size_t vl_u32)
112 {
113 size_t vl_bytes = vl_u32 * 4;
114
115 __riscv_vse8_v_u8m4(ptr, cast_u32m4_u8m4(vec), vl_bytes);
116 }
117
118 static vuint32m1_t
119 vxor_u8_u32m1(vuint32m1_t a, vuint32m1_t b, size_t vl_u32)
120 {
121 size_t vl_bytes = vl_u32 * 4;
122
123 return cast_u8m1_u32m1(__riscv_vxor_vv_u8m1(cast_u32m1_u8m1(a),
124 cast_u32m1_u8m1(b), vl_bytes));
125 }
126
127 static vuint32m4_t
128 vxor_u8_u32m4(vuint32m4_t a, vuint32m4_t b, size_t vl_u32)
129 {
130 size_t vl_bytes = vl_u32 * 4;
131
132 return cast_u8m4_u32m4(__riscv_vxor_vv_u8m4(cast_u32m4_u8m4(a),
133 cast_u32m4_u8m4(b), vl_bytes));
134 }
135
136
137 /*
138 * HW support detection
139 */
140
141 int ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
142 _gcry_aes_riscv_zvkned_setup_acceleration(RIJNDAEL_context *ctx)
143 {
144 (void)ctx;
145 return (__riscv_vsetvl_e32m1(4) == 4);
146 }
147
148
149 /*
150 * Key expansion
151 */
152
153 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
154 aes128_riscv_setkey (RIJNDAEL_context *ctx, const byte *key)
155 {
156 size_t vl = 4;
157
158 vuint32m1_t round_key = unaligned_load_u32m1 (key, vl);
159 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[0][0], round_key, vl);
160
161 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 1, vl);
162 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[1][0], round_key, vl);
163
164 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 2, vl);
165 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[2][0], round_key, vl);
166
167 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 3, vl);
168 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[3][0], round_key, vl);
169
170 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 4, vl);
171 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[4][0], round_key, vl);
172
173 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 5, vl);
174 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[5][0], round_key, vl);
175
176 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 6, vl);
177 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[6][0], round_key, vl);
178
179 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 7, vl);
180 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[7][0], round_key, vl);
181
182 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 8, vl);
183 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[8][0], round_key, vl);
184
185 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 9, vl);
186 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[9][0], round_key, vl);
187
188 round_key = __riscv_vaeskf1_vi_u32m1 (round_key, 10, vl);
189 __riscv_vse32_v_u32m1 (&ctx->keyschenc32[10][0], round_key, vl);
190
191 clear_vec_regs();
192 }
193
194 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
195 aes192_riscv_setkey (RIJNDAEL_context *ctx, const byte *key)
196 {
197 size_t vl = 4;
198 u32 *w = &ctx->keyschenc32[0][0];
199 u32 wr;
200 vuint32m1_t rk_0_7;
201 vuint32m1_t rk_4_11;
202
203 rk_0_7 = unaligned_load_u32m1 (&key[0], vl);
204 rk_4_11 = unaligned_load_u32m1 (&key[8], vl);
205 __riscv_vse32_v_u32m1 (&w[0], rk_0_7, vl);
206 __riscv_vse32_v_u32m1 (&w[2], rk_4_11, vl);
207
208 #define AES192_KF1_GEN(out, input, round192, vl) \
209 ({ \
210 vuint32m1_t temp_vec = __riscv_vmv_v_x_u32m1(0, (vl)); \
211 temp_vec = __riscv_vslide1down_vx_u32m1(temp_vec, (input), (vl)); \
212 temp_vec = __riscv_vaeskf1_vi_u32m1(temp_vec, (round192), (vl)); \
213 (out) = __riscv_vmv_x_s_u32m1_u32(temp_vec); \
214 })
215
216 #define AES192_EXPAND_BLOCK(w, round192, wr, last) \
217 ({ \
218 (w)[(round192) * 6 + 0] = (w)[(round192) * 6 - 6] ^ (wr); \
219 (w)[(round192) * 6 + 1] = (w)[(round192) * 6 - 5] ^ (w)[(round192) * 6 + 0]; \
220 (w)[(round192) * 6 + 2] = (w)[(round192) * 6 - 4] ^ (w)[(round192) * 6 + 1]; \
221 (w)[(round192) * 6 + 3] = (w)[(round192) * 6 - 3] ^ (w)[(round192) * 6 + 2]; \
222 if (!(last)) \
223 { \
224 (w)[(round192) * 6 + 4] = (w)[(round192) * 6 - 2] ^ (w)[(round192) * 6 + 3]; \
225 (w)[(round192) * 6 + 5] = (w)[(round192) * 6 - 1] ^ (w)[(round192) * 6 + 4]; \
226 } \
227 })
228
229 AES192_KF1_GEN(wr, w[5], 1, vl);
230 AES192_EXPAND_BLOCK(w, 1, wr, 0);
231
232 AES192_KF1_GEN(wr, w[11], 2, vl);
233 AES192_EXPAND_BLOCK(w, 2, wr, 0);
234
235 AES192_KF1_GEN(wr, w[17], 3, vl);
236 AES192_EXPAND_BLOCK(w, 3, wr, 0);
237
238 AES192_KF1_GEN(wr, w[23], 4, vl);
239 AES192_EXPAND_BLOCK(w, 4, wr, 0);
240
241 AES192_KF1_GEN(wr, w[29], 5, vl);
242 AES192_EXPAND_BLOCK(w, 5, wr, 0);
243
244 AES192_KF1_GEN(wr, w[35], 6, vl);
245 AES192_EXPAND_BLOCK(w, 6, wr, 0);
246
247 AES192_KF1_GEN(wr, w[41], 7, vl);
248 AES192_EXPAND_BLOCK(w, 7, wr, 0);
249
250 AES192_KF1_GEN(wr, w[47], 8, vl);
251 AES192_EXPAND_BLOCK(w, 8, wr, 1);
252
253 #undef AES192_KF1_GEN
254 #undef AES192_EXPAND_BLOCK
255
256 clear_vec_regs();
257 }
258
259 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
260 aes256_riscv_setkey (RIJNDAEL_context *ctx, const byte *key)
261 {
262 size_t vl = 4;
263
264 vuint32m1_t rk_a = unaligned_load_u32m1 (&key[0], vl);
265 vuint32m1_t rk_b = unaligned_load_u32m1 (&key[16], vl);
266
267 __riscv_vse32_v_u32m1(&ctx->keyschenc32[0][0], rk_a, vl);
268 __riscv_vse32_v_u32m1(&ctx->keyschenc32[1][0], rk_b, vl);
269
270 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 2, vl);
271 __riscv_vse32_v_u32m1(&ctx->keyschenc32[2][0], rk_a, vl);
272
273 rk_b = __riscv_vaeskf2_vi_u32m1(rk_b, rk_a, 3, vl);
274 __riscv_vse32_v_u32m1(&ctx->keyschenc32[3][0], rk_b, vl);
275
276 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 4, vl);
277 __riscv_vse32_v_u32m1(&ctx->keyschenc32[4][0], rk_a, vl);
278
279 rk_b = __riscv_vaeskf2_vi_u32m1(rk_b, rk_a, 5, vl);
280 __riscv_vse32_v_u32m1(&ctx->keyschenc32[5][0], rk_b, vl);
281
282 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 6, vl);
283 __riscv_vse32_v_u32m1(&ctx->keyschenc32[6][0], rk_a, vl);
284
285 rk_b = __riscv_vaeskf2_vi_u32m1(rk_b, rk_a, 7, vl);
286 __riscv_vse32_v_u32m1(&ctx->keyschenc32[7][0], rk_b, vl);
287
288 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 8, vl);
289 __riscv_vse32_v_u32m1(&ctx->keyschenc32[8][0], rk_a, vl);
290
291 rk_b = __riscv_vaeskf2_vi_u32m1(rk_b, rk_a, 9, vl);
292 __riscv_vse32_v_u32m1(&ctx->keyschenc32[9][0], rk_b, vl);
293
294 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 10, vl);
295 __riscv_vse32_v_u32m1(&ctx->keyschenc32[10][0], rk_a, vl);
296
297 rk_b = __riscv_vaeskf2_vi_u32m1(rk_b, rk_a, 11, vl);
298 __riscv_vse32_v_u32m1(&ctx->keyschenc32[11][0], rk_b, vl);
299
300 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 12, vl);
301 __riscv_vse32_v_u32m1(&ctx->keyschenc32[12][0], rk_a, vl);
302
303 rk_b = __riscv_vaeskf2_vi_u32m1(rk_b, rk_a, 13, vl);
304 __riscv_vse32_v_u32m1(&ctx->keyschenc32[13][0], rk_b, vl);
305
306 rk_a = __riscv_vaeskf2_vi_u32m1(rk_a, rk_b, 14, vl);
307 __riscv_vse32_v_u32m1(&ctx->keyschenc32[14][0], rk_a, vl);
308
309 clear_vec_regs();
310 }
311
312 void ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
313 _gcry_aes_riscv_zvkned_setkey (RIJNDAEL_context *ctx, const byte *key)
314 {
315 unsigned int rounds = ctx->rounds;
316
317 if (rounds < 12)
318 {
319 aes128_riscv_setkey(ctx, key);
320 }
321 else if (rounds == 12)
322 {
323 aes192_riscv_setkey(ctx, key);
324 _gcry_burn_stack(64);
325 }
326 else
327 {
328 aes256_riscv_setkey(ctx, key);
329 }
330 }
331
332 static ASM_FUNC_ATTR_INLINE void
333 do_prepare_decryption(RIJNDAEL_context *ctx)
334 {
335 u32 *ekey = (u32 *)(void *)ctx->keyschenc;
336 u32 *dkey = (u32 *)(void *)ctx->keyschdec;
337 int rounds = ctx->rounds;
338 size_t vl = 4;
339 int rr;
340 int r;
341
342 #define COPY_KEY() \
343 __riscv_vse32_v_u32m1(dkey + r * 4, \
344 __riscv_vle32_v_u32m1(ekey + rr * 4, vl), \
345 vl)
346
347 r = 0;
348 rr = rounds;
349 COPY_KEY(); r++; rr--;
350 COPY_KEY(); r++; rr--;
351 COPY_KEY(); r++; rr--;
352 COPY_KEY(); r++; rr--;
353 COPY_KEY(); r++; rr--;
354 COPY_KEY(); r++; rr--;
355 COPY_KEY(); r++; rr--;
356 COPY_KEY(); r++; rr--;
357 COPY_KEY(); r++; rr--;
358 COPY_KEY(); r++; rr--;
359 if (rr > 0)
360 {
361 COPY_KEY(); r++; rr--;
362 COPY_KEY(); r++; rr--;
363 if (rr > 0)
364 {
365 COPY_KEY(); r++; rr--;
366 COPY_KEY(); r++; rr--;
367 }
368 }
369 COPY_KEY();
370
371 #undef COPY_KEY
372 }
373
374 void ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
375 _gcry_aes_riscv_zvkned_prepare_decryption(RIJNDAEL_context *ctx)
376 {
377 do_prepare_decryption(ctx);
378 clear_vec_regs();
379 }
380
381
382 /*
383 * Encryption / Decryption
384 */
385
386 #define ROUND_KEY_VARIABLES \
387 vuint32m1_t rk0, rk1, rk2, rk3, rk4, rk5, rk6, rk7, rk8; \
388 vuint32m1_t rk9, rk10, rk11, rk12, rk13, rk_last;
389
390 #define PRELOAD_ROUND_KEYS(rk, nrounds, vl) \
391 do { \
392 rk0 = __riscv_vle32_v_u32m1(rk + 0 * 4, vl); \
393 rk1 = __riscv_vle32_v_u32m1(rk + 1 * 4, vl); \
394 rk2 = __riscv_vle32_v_u32m1(rk + 2 * 4, vl); \
395 rk3 = __riscv_vle32_v_u32m1(rk + 3 * 4, vl); \
396 rk4 = __riscv_vle32_v_u32m1(rk + 4 * 4, vl); \
397 rk5 = __riscv_vle32_v_u32m1(rk + 5 * 4, vl); \
398 rk6 = __riscv_vle32_v_u32m1(rk + 6 * 4, vl); \
399 rk7 = __riscv_vle32_v_u32m1(rk + 7 * 4, vl); \
400 rk8 = __riscv_vle32_v_u32m1(rk + 8 * 4, vl); \
401 rk9 = __riscv_vle32_v_u32m1(rk + 9 * 4, vl); \
402 if (UNLIKELY(nrounds >= 12)) \
403 { \
404 rk10 = __riscv_vle32_v_u32m1(rk + 10 * 4, vl); \
405 rk11 = __riscv_vle32_v_u32m1(rk + 11 * 4, vl); \
406 if (LIKELY(nrounds > 12)) \
407 { \
408 rk12 = __riscv_vle32_v_u32m1(rk + 12 * 4, vl); \
409 rk13 = __riscv_vle32_v_u32m1(rk + 13 * 4, vl); \
410 } \
411 else \
412 { \
413 rk12 = __riscv_vundefined_u32m1(); \
414 rk13 = __riscv_vundefined_u32m1(); \
415 } \
416 } \
417 else \
418 { \
419 rk10 = __riscv_vundefined_u32m1(); \
420 rk11 = __riscv_vundefined_u32m1(); \
421 rk12 = __riscv_vundefined_u32m1(); \
422 rk13 = __riscv_vundefined_u32m1(); \
423 } \
424 rk_last = __riscv_vle32_v_u32m1(rk + nrounds * 4, vl); \
425 } while (0)
426
427 #ifdef HAVE_BROKEN_VAES_VS_INTRINSIC
428 #define AES_CRYPT(e_d, mx, nrounds, blk, vlen) \
429 asm ( "vsetvli zero,%[vl],e32,"#mx",ta,ma;\n\t" \
430 "vaesz.vs %[block],%[rk0];\n\t" \
431 "vaes"#e_d"m.vs %[block],%[rk1];\n\t" \
432 "vaes"#e_d"m.vs %[block],%[rk2];\n\t" \
433 "vaes"#e_d"m.vs %[block],%[rk3];\n\t" \
434 "vaes"#e_d"m.vs %[block],%[rk4];\n\t" \
435 "vaes"#e_d"m.vs %[block],%[rk5];\n\t" \
436 "vaes"#e_d"m.vs %[block],%[rk6];\n\t" \
437 "vaes"#e_d"m.vs %[block],%[rk7];\n\t" \
438 "vaes"#e_d"m.vs %[block],%[rk8];\n\t" \
439 "vaes"#e_d"m.vs %[block],%[rk9];\n\t" \
440 "blt %[rounds],%[num12],.Lcryptlast%=;\n\t" \
441 "vaes"#e_d"m.vs %[block],%[rk10];\n\t" \
442 "vaes"#e_d"m.vs %[block],%[rk11];\n\t" \
443 "beq %[rounds],%[num12],.Lcryptlast%=;\n\t" \
444 "vaes"#e_d"m.vs %[block],%[rk12];\n\t" \
445 "vaes"#e_d"m.vs %[block],%[rk13];\n\t" \
446 ".Lcryptlast%=:\n\t" \
447 "vaes"#e_d"f.vs %[block],%[rk_last];\n\t" \
448 : [block] "+vr" (blk) \
449 : [vl] "r" (vlen), [rounds] "r" (nrounds), [num12] "r" (12), \
450 [rk0] "vr" (rk0), [rk1] "vr" (rk1), [rk2] "vr" (rk2), \
451 [rk3] "vr" (rk3), [rk4] "vr" (rk4), [rk5] "vr" (rk5), \
452 [rk6] "vr" (rk6), [rk7] "vr" (rk7), [rk8] "vr" (rk8), \
453 [rk9] "vr" (rk9), [rk10] "vr" (rk10), [rk11] "vr" (rk11), \
454 [rk12] "vr" (rk12), [rk13] "vr" (rk13), \
455 [rk_last] "vr" (rk_last) \
456 : "vl")
457 #else
458 #define AES_CRYPT(e_d, mx, rounds, block, vl) \
459 ({ \
460 (block) = __riscv_vaesz_vs_u32m1_u32##mx((block), rk0, (vl)); \
461 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk1, (vl)); \
462 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk2, (vl)); \
463 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk3, (vl)); \
464 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk4, (vl)); \
465 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk5, (vl)); \
466 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk6, (vl)); \
467 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk7, (vl)); \
468 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk8, (vl)); \
469 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk9, (vl)); \
470 if (UNLIKELY((rounds) >= 12)) \
471 { \
472 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk10, (vl)); \
473 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk11, (vl)); \
474 if (LIKELY((rounds) > 12)) \
475 { \
476 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk12, (vl)); \
477 (block) = __riscv_vaes##e_d##m_vs_u32m1_u32##mx((block), rk13, (vl)); \
478 } \
479 } \
480 (block) = __riscv_vaes##e_d##f_vs_u32m1_u32##mx((block), rk_last, (vl)); \
481 })
482 #endif
483
484 unsigned int ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
485 _gcry_aes_riscv_zvkned_encrypt (const RIJNDAEL_context *ctx, unsigned char *out,
486 const unsigned char *in)
487 {
488 const u32 *rk = ctx->keyschenc32[0];
489 int rounds = ctx->rounds;
490 size_t vl = 4;
491 vuint32m1_t block;
492 ROUND_KEY_VARIABLES;
493
494 PRELOAD_ROUND_KEYS (rk, rounds, vl);
495
496 block = unaligned_load_u32m1(in, vl);
497
498 AES_CRYPT(e, m1, rounds, block, vl);
499
500 unaligned_store_u32m1(out, block, vl);
501
502 clear_vec_regs();
503
504 return 0; /* does not use stack */
505 }
506
507 unsigned int ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
508 _gcry_aes_riscv_zvkned_decrypt (const RIJNDAEL_context *ctx, unsigned char *out,
509 const unsigned char *in)
510 {
511 const u32 *rk = ctx->keyschdec32[0];
512 int rounds = ctx->rounds;
513 size_t vl = 4;
514 vuint32m1_t block;
515 ROUND_KEY_VARIABLES;
516
517 PRELOAD_ROUND_KEYS (rk, rounds, vl);
518
519 block = unaligned_load_u32m1(in, vl);
520
521 AES_CRYPT(d, m1, rounds, block, vl);
522
523 unaligned_store_u32m1(out, block, vl);
524
525 clear_vec_regs();
526
527 return 0; /* does not use stack */
528 }
529
530 static ASM_FUNC_ATTR_INLINE void
531 aes_riscv_zvkned_ecb_crypt (void *context, void *outbuf_arg,
532 const void *inbuf_arg, size_t nblocks, int encrypt)
533 {
534 RIJNDAEL_context *ctx = context;
535 unsigned char *outbuf = outbuf_arg;
536 const unsigned char *inbuf = inbuf_arg;
537 const u32 *rk = encrypt ? ctx->keyschenc32[0] : ctx->keyschdec32[0];
538 int rounds = ctx->rounds;
539 size_t vl = 4;
540 ROUND_KEY_VARIABLES;
541
542 if (!encrypt && !ctx->decryption_prepared)
543 {
544 do_prepare_decryption(ctx);
545 ctx->decryption_prepared = 1;
546 }
547
548 PRELOAD_ROUND_KEYS (rk, rounds, vl);
549
550 for (; nblocks >= 4; nblocks -= 4)
551 {
552 vuint32m4_t blocks;
553
554 blocks = unaligned_load_u32m4(inbuf, vl * 4);
555
556 if (encrypt)
557 AES_CRYPT(e, m4, rounds, blocks, vl * 4);
558 else
559 AES_CRYPT(d, m4, rounds, blocks, vl * 4);
560
561 unaligned_store_u32m4(outbuf, blocks, vl * 4);
562
563 inbuf += 4 * BLOCKSIZE;
564 outbuf += 4 * BLOCKSIZE;
565 }
566
567 for (; nblocks; nblocks--)
568 {
569 vuint32m1_t block;
570
571 block = unaligned_load_u32m1(inbuf, vl);
572
573 if (encrypt)
574 AES_CRYPT(e, m1, rounds, block, vl);
575 else
576 AES_CRYPT(d, m1, rounds, block, vl);
577
578 unaligned_store_u32m1(outbuf, block, vl);
579
580 inbuf += BLOCKSIZE;
581 outbuf += BLOCKSIZE;
582 }
583
584 clear_vec_regs();
585 }
586
587 static void ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
588 aes_riscv_zvkned_ecb_enc (void *context, void *outbuf_arg,
589 const void *inbuf_arg, size_t nblocks)
590 {
591 aes_riscv_zvkned_ecb_crypt (context, outbuf_arg, inbuf_arg, nblocks, 1);
592 }
593
594 static void ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
595 aes_riscv_zvkned_ecb_dec (void *context, void *outbuf_arg,
596 const void *inbuf_arg, size_t nblocks)
597 {
598 aes_riscv_zvkned_ecb_crypt (context, outbuf_arg, inbuf_arg, nblocks, 0);
599 }
600
601 void ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
602 _gcry_aes_riscv_zvkned_ecb_crypt (void *context, void *outbuf_arg,
603 const void *inbuf_arg, size_t nblocks,
604 int encrypt)
605 {
606 if (encrypt)
607 aes_riscv_zvkned_ecb_enc (context, outbuf_arg, inbuf_arg, nblocks);
608 else
609 aes_riscv_zvkned_ecb_dec (context, outbuf_arg, inbuf_arg, nblocks);
610 }
611
612 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
613 _gcry_aes_riscv_zvkned_cfb_enc (void *context, unsigned char *iv_arg,
614 void *outbuf_arg, const void *inbuf_arg,
615 size_t nblocks)
616 {
617 RIJNDAEL_context *ctx = context;
618 unsigned char *outbuf = outbuf_arg;
619 const unsigned char *inbuf = inbuf_arg;
620 const u32 *rk = ctx->keyschenc32[0];
621 int rounds = ctx->rounds;
622 size_t vl = 4;
623 size_t vl_bytes = vl * 4;
624 vuint32m1_t iv;
625 ROUND_KEY_VARIABLES;
626
627 PRELOAD_ROUND_KEYS (rk, rounds, vl);
628
629 iv = unaligned_load_u32m1(iv_arg, vl);
630
631 for (; nblocks; nblocks--)
632 {
633 vuint8m1_t data = __riscv_vle8_v_u8m1(inbuf, vl_bytes);
634
635 AES_CRYPT(e, m1, rounds, iv, vl);
636
637 data = __riscv_vxor_vv_u8m1(cast_u32m1_u8m1(iv), data, vl_bytes);
638 __riscv_vse8_v_u8m1(outbuf, data, vl_bytes);
639 iv = cast_u8m1_u32m1(data);
640
641 outbuf += BLOCKSIZE;
642 inbuf += BLOCKSIZE;
643 }
644
645 unaligned_store_u32m1(iv_arg, iv, vl);
646
647 clear_vec_regs();
648 }
649
650 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
651 _gcry_aes_riscv_zvkned_cbc_enc (void *context, unsigned char *iv_arg,
652 void *outbuf_arg, const void *inbuf_arg,
653 size_t nblocks, int cbc_mac)
654 {
655 RIJNDAEL_context *ctx = context;
656 unsigned char *outbuf = outbuf_arg;
657 const unsigned char *inbuf = inbuf_arg;
658 size_t outbuf_add = (!cbc_mac) * BLOCKSIZE;
659 const u32 *rk = ctx->keyschenc32[0];
660 int rounds = ctx->rounds;
661 size_t vl = 4;
662 size_t vl_bytes = vl * 4;
663 vuint32m1_t iv;
664 ROUND_KEY_VARIABLES;
665
666 PRELOAD_ROUND_KEYS (rk, rounds, vl);
667
668 iv = unaligned_load_u32m1(iv_arg, vl);
669
670 for (; nblocks; nblocks--)
671 {
672 vuint8m1_t data = __riscv_vle8_v_u8m1(inbuf, vl_bytes);
673 iv = cast_u8m1_u32m1(
674 __riscv_vxor_vv_u8m1(data, cast_u32m1_u8m1(iv), vl_bytes));
675
676 AES_CRYPT(e, m1, rounds, iv, vl);
677
678 __riscv_vse8_v_u8m1(outbuf, cast_u32m1_u8m1(iv), vl_bytes);
679
680 inbuf += BLOCKSIZE;
681 outbuf += outbuf_add;
682 }
683
684 unaligned_store_u32m1(iv_arg, iv, vl);
685
686 clear_vec_regs();
687 }
688
689 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
690 _gcry_aes_riscv_zvkned_ctr_enc (void *context, unsigned char *ctr_arg,
691 void *outbuf_arg, const void *inbuf_arg,
692 size_t nblocks)
693 {
694 static const byte add_u8_array[4][16] =
695 {
696 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
697 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 },
698 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
699 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 }
700 };
701 static const u64 carry_add[2] = { 1, 1 };
702 static const u64 nocarry_add[2] = { 1, 0 };
703 RIJNDAEL_context *ctx = context;
704 unsigned char *outbuf = outbuf_arg;
705 const unsigned char *inbuf = inbuf_arg;
706 const u32 *rk = ctx->keyschenc32[0];
707 int rounds = ctx->rounds;
708 size_t vl = 4;
709 size_t vl_bytes = vl * 4;
710 u64 ctrlow;
711 vuint32m1_t ctr;
712 vuint8m1_t add1;
713 ROUND_KEY_VARIABLES;
714
715 PRELOAD_ROUND_KEYS (rk, rounds, vl);
716
717 add1 = __riscv_vle8_v_u8m1(add_u8_array[0], vl_bytes);
718 ctr = unaligned_load_u32m1(ctr_arg, vl);
719 ctrlow = __riscv_vmv_x_s_u64m1_u64(cast_u32m1_u64m1(bswap128_u32m1(ctr, vl)));
720
721 memory_barrier_with_vec(add1);
722
723 if (nblocks >= 4)
724 {
725 vuint8m1_t add2 = __riscv_vle8_v_u8m1(add_u8_array[1], vl_bytes);
726 vuint8m1_t add3 = __riscv_vle8_v_u8m1(add_u8_array[2], vl_bytes);
727 vuint8m1_t add4 = __riscv_vle8_v_u8m1(add_u8_array[3], vl_bytes);
728
729 memory_barrier_with_vec(add2);
730 memory_barrier_with_vec(add3);
731 memory_barrier_with_vec(add4);
732
733 for (; nblocks >= 4; nblocks -= 4)
734 {
735 vuint8m4_t data4blks;
736 vuint32m4_t ctr4blks;
737
738 /* detect if 8-bit carry handling is needed */
739 if (UNLIKELY(((ctrlow += 4) & 0xff) <= 3))
740 {
741 static const u64 *adders[5][4] =
742 {
743 { nocarry_add, nocarry_add, nocarry_add, carry_add },
744 { nocarry_add, nocarry_add, carry_add, nocarry_add },
745 { nocarry_add, carry_add, nocarry_add, nocarry_add },
746 { carry_add, nocarry_add, nocarry_add, nocarry_add },
747 { nocarry_add, nocarry_add, nocarry_add, nocarry_add }
748 };
749 unsigned int idx = ctrlow <= 3 ? ctrlow : 4;
750 vuint64m1_t ctr_u64;
751 vuint32m1_t ctr_u32_1;
752 vuint32m1_t ctr_u32_2;
753 vuint32m1_t ctr_u32_3;
754 vuint32m1_t ctr_u32_4;
755 vuint64m1_t add_u64;
756
757 /* Byte swap counter */
758 ctr_u64 = cast_u32m1_u64m1(bswap128_u32m1(ctr, vl));
759
760 /* Addition with carry handling */
761 add_u64 = __riscv_vle64_v_u64m1(adders[idx][0], vl / 2);
762 ctr_u64 = __riscv_vadd_vv_u64m1(ctr_u64, add_u64, vl / 2);
763 ctr_u32_1 = cast_u64m1_u32m1(ctr_u64);
764
765 add_u64 = __riscv_vle64_v_u64m1(adders[idx][1], vl / 2);
766 ctr_u64 = __riscv_vadd_vv_u64m1(ctr_u64, add_u64, vl / 2);
767 ctr_u32_2 = cast_u64m1_u32m1(ctr_u64);
768
769 add_u64 = __riscv_vle64_v_u64m1(adders[idx][2], vl / 2);
770 ctr_u64 = __riscv_vadd_vv_u64m1(ctr_u64, add_u64, vl / 2);
771 ctr_u32_3 = cast_u64m1_u32m1(ctr_u64);
772
773 add_u64 = __riscv_vle64_v_u64m1(adders[idx][3], vl / 2);
774 ctr_u64 = __riscv_vadd_vv_u64m1(ctr_u64, add_u64, vl / 2);
775 ctr_u32_4 = cast_u64m1_u32m1(ctr_u64);
776
777 /* Byte swap counters */
778 ctr_u32_1 = bswap128_u32m1(ctr_u32_1, vl);
779 ctr_u32_2 = bswap128_u32m1(ctr_u32_2, vl);
780 ctr_u32_3 = bswap128_u32m1(ctr_u32_3, vl);
781 ctr_u32_4 = bswap128_u32m1(ctr_u32_4, vl);
782
783 ctr4blks = __riscv_vundefined_u32m4();
784 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 0, ctr);
785 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 1, ctr_u32_1);
786 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 2, ctr_u32_2);
787 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 3, ctr_u32_3);
788 ctr = ctr_u32_4;
789 }
790 else
791 {
792 /* Fast path addition without carry handling */
793 vuint8m1_t ctr_u8 = cast_u32m1_u8m1(ctr);
794 vuint8m1_t ctr1 = __riscv_vadd_vv_u8m1(ctr_u8, add1, vl_bytes);
795 vuint8m1_t ctr2 = __riscv_vadd_vv_u8m1(ctr_u8, add2, vl_bytes);
796 vuint8m1_t ctr3 = __riscv_vadd_vv_u8m1(ctr_u8, add3, vl_bytes);
797 vuint8m4_t ctr0123_u8 = __riscv_vundefined_u8m4();
798
799 ctr = cast_u8m1_u32m1(__riscv_vadd_vv_u8m1(ctr_u8, add4,
800 vl_bytes));
801
802 ctr0123_u8 = __riscv_vset_v_u8m1_u8m4(ctr0123_u8, 0, ctr_u8);
803 ctr0123_u8 = __riscv_vset_v_u8m1_u8m4(ctr0123_u8, 1, ctr1);
804 ctr0123_u8 = __riscv_vset_v_u8m1_u8m4(ctr0123_u8, 2, ctr2);
805 ctr0123_u8 = __riscv_vset_v_u8m1_u8m4(ctr0123_u8, 3, ctr3);
806
807 ctr4blks = cast_u8m4_u32m4(ctr0123_u8);
808 }
809
810 data4blks = __riscv_vle8_v_u8m4(inbuf, vl_bytes * 4);
811
812 AES_CRYPT(e, m4, rounds, ctr4blks, vl * 4);
813
814 data4blks = __riscv_vxor_vv_u8m4(cast_u32m4_u8m4(ctr4blks), data4blks,
815 vl_bytes * 4);
816 __riscv_vse8_v_u8m4(outbuf, data4blks, vl_bytes * 4);
817
818 inbuf += 4 * BLOCKSIZE;
819 outbuf += 4 * BLOCKSIZE;
820 }
821 }
822
823 for (; nblocks; nblocks--)
824 {
825 vuint32m1_t block = ctr;
826 vuint8m1_t data = __riscv_vle8_v_u8m1(inbuf, vl_bytes);
827
828 /* detect if 8-bit carry handling is needed */
829 if (UNLIKELY((++ctrlow & 0xff) == 0))
830 {
831 const u64 *add_arr = UNLIKELY(ctrlow == 0) ? carry_add : nocarry_add;
832 vuint64m1_t add_val = __riscv_vle64_v_u64m1(add_arr, vl / 2);
833
834 /* Byte swap counter */
835 ctr = bswap128_u32m1(ctr, vl);
836
837 /* Addition with carry handling */
838 ctr = cast_u64m1_u32m1(__riscv_vadd_vv_u64m1(cast_u32m1_u64m1(ctr),
839 add_val, vl / 2));
840
841 /* Byte swap counter */
842 ctr = bswap128_u32m1(ctr, vl);
843 }
844 else
845 {
846 /* Fast path addition without carry handling */
847 ctr = cast_u8m1_u32m1(__riscv_vadd_vv_u8m1(cast_u32m1_u8m1(ctr),
848 add1, vl_bytes));
849 }
850
851 AES_CRYPT(e, m1, rounds, block, vl);
852
853 data = __riscv_vxor_vv_u8m1(cast_u32m1_u8m1(block), data, vl_bytes);
854 __riscv_vse8_v_u8m1(outbuf, data, vl_bytes);
855
856 inbuf += BLOCKSIZE;
857 outbuf += BLOCKSIZE;
858 }
859
860 unaligned_store_u32m1(ctr_arg, ctr, vl);
861
862 clear_vec_regs();
863 }
864
865 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
866 _gcry_aes_riscv_zvkned_ctr32le_enc (void *context, unsigned char *ctr_arg,
867 void *outbuf_arg, const void *inbuf_arg,
868 size_t nblocks)
869 {
870 static const u32 add_u32_array[4][16] =
871 {
872 { 1, }, { 2, }, { 3, }, { 4, }
873 };
874 RIJNDAEL_context *ctx = context;
875 unsigned char *outbuf = outbuf_arg;
876 const unsigned char *inbuf = inbuf_arg;
877 const u32 *rk = ctx->keyschenc32[0];
878 int rounds = ctx->rounds;
879 size_t vl = 4;
880 size_t vl_bytes = vl * 4;
881 vuint32m1_t ctr;
882 vuint32m1_t add1;
883 ROUND_KEY_VARIABLES;
884
885 PRELOAD_ROUND_KEYS (rk, rounds, vl);
886
887 add1 = __riscv_vle32_v_u32m1(add_u32_array[0], vl);
888 ctr = unaligned_load_u32m1(ctr_arg, vl);
889
890 memory_barrier_with_vec(add1);
891
892 if (nblocks >= 4)
893 {
894 vuint32m1_t add2 = __riscv_vle32_v_u32m1(add_u32_array[1], vl);
895 vuint32m1_t add3 = __riscv_vle32_v_u32m1(add_u32_array[2], vl);
896 vuint32m1_t add4 = __riscv_vle32_v_u32m1(add_u32_array[3], vl);
897
898 memory_barrier_with_vec(add2);
899 memory_barrier_with_vec(add3);
900 memory_barrier_with_vec(add4);
901
902 for (; nblocks >= 4; nblocks -= 4)
903 {
904 vuint32m1_t ctr1 = __riscv_vadd_vv_u32m1(ctr, add1, vl);
905 vuint32m1_t ctr2 = __riscv_vadd_vv_u32m1(ctr, add2, vl);
906 vuint32m1_t ctr3 = __riscv_vadd_vv_u32m1(ctr, add3, vl);
907 vuint32m4_t ctr4blks = __riscv_vundefined_u32m4();
908 vuint8m4_t data4blks;
909
910 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 0, ctr);
911 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 1, ctr1);
912 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 2, ctr2);
913 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 3, ctr3);
914 ctr = __riscv_vadd_vv_u32m1(ctr, add4, vl);
915
916 data4blks = __riscv_vle8_v_u8m4(inbuf, vl_bytes * 4);
917
918 AES_CRYPT(e, m4, rounds, ctr4blks, vl * 4);
919
920 data4blks = __riscv_vxor_vv_u8m4(cast_u32m4_u8m4(ctr4blks), data4blks,
921 vl_bytes * 4);
922 __riscv_vse8_v_u8m4(outbuf, data4blks, vl_bytes * 4);
923
924 inbuf += 4 * BLOCKSIZE;
925 outbuf += 4 * BLOCKSIZE;
926 }
927 }
928
929 for (; nblocks; nblocks--)
930 {
931 vuint32m1_t block = ctr;
932 vuint8m1_t data = __riscv_vle8_v_u8m1(inbuf, vl_bytes);
933
934 ctr = __riscv_vadd_vv_u32m1(ctr, add1, vl);
935
936 AES_CRYPT(e, m1, rounds, block, vl);
937
938 data = __riscv_vxor_vv_u8m1(cast_u32m1_u8m1(block), data, vl_bytes);
939 __riscv_vse8_v_u8m1(outbuf, data, vl_bytes);
940
941 inbuf += BLOCKSIZE;
942 outbuf += BLOCKSIZE;
943 }
944
945 unaligned_store_u32m1(ctr_arg, ctr, vl);
946
947 clear_vec_regs();
948 }
949
950 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
951 _gcry_aes_riscv_zvkned_cfb_dec (void *context, unsigned char *iv_arg,
952 void *outbuf_arg, const void *inbuf_arg,
953 size_t nblocks)
954 {
955 RIJNDAEL_context *ctx = context;
956 unsigned char *outbuf = outbuf_arg;
957 const unsigned char *inbuf = inbuf_arg;
958 const u32 *rk = ctx->keyschenc32[0];
959 int rounds = ctx->rounds;
960 size_t vl = 4;
961 vuint32m1_t iv;
962 ROUND_KEY_VARIABLES;
963
964 PRELOAD_ROUND_KEYS (rk, rounds, vl);
965
966 iv = unaligned_load_u32m1(iv_arg, vl);
967
968 for (; nblocks >= 4; nblocks -= 4)
969 {
970 vuint32m4_t data4blks = unaligned_load_u32m4(inbuf, vl * 4);
971 vuint32m1_t iv1 = __riscv_vget_v_u32m4_u32m1(data4blks, 0);
972 vuint32m1_t iv2 = __riscv_vget_v_u32m4_u32m1(data4blks, 1);
973 vuint32m1_t iv3 = __riscv_vget_v_u32m4_u32m1(data4blks, 2);
974 vuint32m1_t iv4 = __riscv_vget_v_u32m4_u32m1(data4blks, 3);
975 vuint32m4_t iv4blks = __riscv_vundefined_u32m4();
976
977 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 0, iv);
978 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 1, iv1);
979 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 2, iv2);
980 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 3, iv3);
981 iv = iv4;
982
983 AES_CRYPT(e, m4, rounds, iv4blks, vl * 4);
984
985 data4blks = vxor_u8_u32m4(iv4blks, data4blks, vl * 4);
986 unaligned_store_u32m4(outbuf, data4blks, vl * 4);
987
988 inbuf += 4 * BLOCKSIZE;
989 outbuf += 4 * BLOCKSIZE;
990 }
991
992 for (; nblocks; nblocks--)
993 {
994 vuint32m1_t data = unaligned_load_u32m1(inbuf, vl);
995 vuint32m1_t new_iv = data;
996
997 AES_CRYPT(e, m1, rounds, iv, vl);
998
999 data = vxor_u8_u32m1(iv, data, vl);
1000 unaligned_store_u32m1(outbuf, data, vl);
1001 iv = new_iv;
1002
1003 inbuf += BLOCKSIZE;
1004 outbuf += BLOCKSIZE;
1005 }
1006
1007 unaligned_store_u32m1(iv_arg, iv, vl);
1008
1009 clear_vec_regs();
1010 }
1011
1012 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
1013 _gcry_aes_riscv_zvkned_cbc_dec (void *context, unsigned char *iv_arg,
1014 void *outbuf_arg, const void *inbuf_arg,
1015 size_t nblocks)
1016 {
1017 RIJNDAEL_context *ctx = context;
1018 unsigned char *outbuf = outbuf_arg;
1019 const unsigned char *inbuf = inbuf_arg;
1020 const u32 *rk = ctx->keyschdec32[0];
1021 int rounds = ctx->rounds;
1022 size_t vl = 4;
1023 vuint32m1_t iv;
1024 ROUND_KEY_VARIABLES;
1025
1026 if (!ctx->decryption_prepared)
1027 {
1028 do_prepare_decryption(ctx);
1029 ctx->decryption_prepared = 1;
1030 }
1031
1032 PRELOAD_ROUND_KEYS (rk, rounds, vl);
1033
1034 iv = unaligned_load_u32m1(iv_arg, vl);
1035
1036 for (; nblocks >= 4; nblocks -= 4)
1037 {
1038 vuint32m4_t data4blks = unaligned_load_u32m4(inbuf, vl * 4);
1039 vuint32m1_t iv1 = __riscv_vget_v_u32m4_u32m1(data4blks, 0);
1040 vuint32m1_t iv2 = __riscv_vget_v_u32m4_u32m1(data4blks, 1);
1041 vuint32m1_t iv3 = __riscv_vget_v_u32m4_u32m1(data4blks, 2);
1042 vuint32m1_t iv4 = __riscv_vget_v_u32m4_u32m1(data4blks, 3);
1043 vuint32m4_t iv4blks = __riscv_vundefined_u32m4();
1044
1045 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 0, iv);
1046 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 1, iv1);
1047 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 2, iv2);
1048 iv4blks = __riscv_vset_v_u32m1_u32m4(iv4blks, 3, iv3);
1049
1050 AES_CRYPT(d, m4, rounds, data4blks, vl * 4);
1051
1052 data4blks = vxor_u8_u32m4(iv4blks, data4blks, vl * 4);
1053 unaligned_store_u32m4(outbuf, data4blks, vl * 4);
1054 iv = iv4;
1055
1056 inbuf += 4 * BLOCKSIZE;
1057 outbuf += 4 * BLOCKSIZE;
1058 }
1059
1060 for (; nblocks; nblocks--)
1061 {
1062 vuint32m1_t data = unaligned_load_u32m1(inbuf, vl);
1063 vuint32m1_t new_iv = data;
1064
1065 AES_CRYPT(d, m1, rounds, data, vl);
1066
1067 data = vxor_u8_u32m1(iv, data, vl);
1068 unaligned_store_u32m1(outbuf, data, vl);
1069 iv = new_iv;
1070
1071 inbuf += BLOCKSIZE;
1072 outbuf += BLOCKSIZE;
1073 }
1074
1075 unaligned_store_u32m1(iv_arg, iv, vl);
1076
1077 clear_vec_regs();
1078 }
1079
1080 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 size_t
1081 aes_riscv_ocb_enc (gcry_cipher_hd_t c, void *outbuf_arg,
1082 const void *inbuf_arg, size_t nblocks)
1083 {
1084 RIJNDAEL_context *ctx = (void *)&c->context.c;
1085 unsigned char *outbuf = outbuf_arg;
1086 const unsigned char *inbuf = inbuf_arg;
1087 u64 n = c->u_mode.ocb.data_nblocks;
1088 const u32 *rk = ctx->keyschenc32[0];
1089 int rounds = ctx->rounds;
1090 size_t vl = 4;
1091 size_t vl_bytes = vl * 4;
1092 vuint32m1_t iv;
1093 vuint32m1_t ctr;
1094 ROUND_KEY_VARIABLES;
1095
1096 PRELOAD_ROUND_KEYS (rk, rounds, vl);
1097
1098 /* Preload Offset and Checksum */
1099 iv = unaligned_load_u32m1(c->u_iv.iv, vl);
1100 ctr = unaligned_load_u32m1(c->u_ctr.ctr, vl);
1101
1102 if (nblocks >= 4)
1103 {
1104 vuint32m4_t ctr4blks = __riscv_vundefined_u32m4();
1105 vuint32m1_t zero = __riscv_vmv_v_x_u32m1(0, vl);
1106
1107 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 0, ctr);
1108 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 1, zero);
1109 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 2, zero);
1110 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 3, zero);
1111
1112 for (; nblocks >= 4; nblocks -= 4)
1113 {
1114 const unsigned char *l;
1115 vuint8m1_t l_ntzi;
1116 vuint32m4_t data4blks = unaligned_load_u32m4(inbuf, vl * 4);
1117 vuint32m4_t offsets = __riscv_vundefined_u32m4();
1118
1119 /* Checksum_i = Checksum_{i-1} xor P_i */
1120 ctr4blks = vxor_u8_u32m4(ctr4blks, data4blks, vl * 4);
1121
1122 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1123 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
1124 l = ocb_get_l(c, ++n);
1125 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1126 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1127 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 0, iv);
1128
1129 l = ocb_get_l(c, ++n);
1130 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1131 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1132 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 1, iv);
1133
1134 l = ocb_get_l(c, ++n);
1135 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1136 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1137 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 2, iv);
1138
1139 l = ocb_get_l(c, ++n);
1140 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1141 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1142 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 3, iv);
1143
1144 data4blks = vxor_u8_u32m4(offsets, data4blks, vl * 4);
1145
1146 AES_CRYPT(e, m4, rounds, data4blks, vl * 4);
1147
1148 data4blks = vxor_u8_u32m4(offsets, data4blks, vl * 4);
1149
1150 unaligned_store_u32m4(outbuf, data4blks, vl * 4);
1151
1152 inbuf += 4 * BLOCKSIZE;
1153 outbuf += 4 * BLOCKSIZE;
1154 }
1155
1156 /* Checksum_i = Checksum_{i-1} xor P_i */
1157 ctr = vxor_u8_u32m1(__riscv_vget_v_u32m4_u32m1(ctr4blks, 0),
1158 __riscv_vget_v_u32m4_u32m1(ctr4blks, 1), vl);
1159 ctr = vxor_u8_u32m1(ctr, __riscv_vget_v_u32m4_u32m1(ctr4blks, 2), vl);
1160 ctr = vxor_u8_u32m1(ctr, __riscv_vget_v_u32m4_u32m1(ctr4blks, 3), vl);
1161 }
1162
1163 for (; nblocks; nblocks--)
1164 {
1165 const unsigned char *l;
1166 vuint8m1_t l_ntzi;
1167 vuint32m1_t data;
1168
1169 data = unaligned_load_u32m1(inbuf, vl);
1170
1171 /* Checksum_i = Checksum_{i-1} xor P_i */
1172 ctr = vxor_u8_u32m1(ctr, data, vl);
1173
1174 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1175 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
1176 l = ocb_get_l(c, ++n);
1177 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1178 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1179
1180 data = vxor_u8_u32m1(data, iv, vl);
1181
1182 AES_CRYPT(e, m1, rounds, data, vl);
1183
1184 data = vxor_u8_u32m1(iv, data, vl);
1185 unaligned_store_u32m1(outbuf, data, vl);
1186
1187 inbuf += BLOCKSIZE;
1188 outbuf += BLOCKSIZE;
1189 }
1190
1191 c->u_mode.ocb.data_nblocks = n;
1192
1193 unaligned_store_u32m1(c->u_iv.iv, iv, vl);
1194 unaligned_store_u32m1(c->u_ctr.ctr, ctr, vl);
1195
1196 clear_vec_regs();
1197
1198 return 0;
1199 }
1200
1201 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 size_t
1202 aes_riscv_ocb_dec (gcry_cipher_hd_t c, void *outbuf_arg,
1203 const void *inbuf_arg, size_t nblocks)
1204 {
1205 RIJNDAEL_context *ctx = (void *)&c->context.c;
1206 unsigned char *outbuf = outbuf_arg;
1207 const unsigned char *inbuf = inbuf_arg;
1208 u64 n = c->u_mode.ocb.data_nblocks;
1209 const u32 *rk = ctx->keyschdec32[0];
1210 int rounds = ctx->rounds;
1211 size_t vl = 4;
1212 size_t vl_bytes = vl * 4;
1213 vuint32m1_t iv;
1214 vuint32m1_t ctr;
1215 ROUND_KEY_VARIABLES;
1216
1217 if (!ctx->decryption_prepared)
1218 {
1219 do_prepare_decryption(ctx);
1220 ctx->decryption_prepared = 1;
1221 }
1222
1223 PRELOAD_ROUND_KEYS (rk, rounds, vl);
1224
1225 /* Preload Offset and Checksum */
1226 iv = unaligned_load_u32m1(c->u_iv.iv, vl);
1227 ctr = unaligned_load_u32m1(c->u_ctr.ctr, vl);
1228
1229 if (nblocks >= 4)
1230 {
1231 vuint32m4_t ctr4blks = __riscv_vundefined_u32m4();
1232 vuint32m1_t zero = __riscv_vmv_v_x_u32m1(0, vl);
1233
1234 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 0, ctr);
1235 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 1, zero);
1236 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 2, zero);
1237 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 3, zero);
1238
1239 for (; nblocks >= 4; nblocks -= 4)
1240 {
1241 const unsigned char *l;
1242 vuint8m1_t l_ntzi;
1243 vuint32m4_t data4blks = unaligned_load_u32m4(inbuf, vl * 4);
1244 vuint32m4_t offsets = __riscv_vundefined_u32m4();
1245
1246 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1247 /* P_i = Offset_i xor ENCIPHER(K, C_i xor Offset_i) */
1248 l = ocb_get_l(c, ++n);
1249 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1250 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1251 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 0, iv);
1252
1253 l = ocb_get_l(c, ++n);
1254 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1255 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1256 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 1, iv);
1257
1258 l = ocb_get_l(c, ++n);
1259 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1260 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1261 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 2, iv);
1262
1263 l = ocb_get_l(c, ++n);
1264 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1265 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1266 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 3, iv);
1267
1268 data4blks = vxor_u8_u32m4(offsets, data4blks, vl * 4);
1269
1270 AES_CRYPT(d, m4, rounds, data4blks, vl * 4);
1271
1272 data4blks = vxor_u8_u32m4(offsets, data4blks, vl * 4);
1273
1274 unaligned_store_u32m4(outbuf, data4blks, vl * 4);
1275
1276 /* Checksum_i = Checksum_{i-1} xor P_i */
1277 ctr4blks = vxor_u8_u32m4(ctr4blks, data4blks, vl * 4);
1278
1279 inbuf += 4 * BLOCKSIZE;
1280 outbuf += 4 * BLOCKSIZE;
1281 }
1282
1283 /* Checksum_i = Checksum_{i-1} xor P_i */
1284 ctr = vxor_u8_u32m1(__riscv_vget_v_u32m4_u32m1(ctr4blks, 0),
1285 __riscv_vget_v_u32m4_u32m1(ctr4blks, 1), vl);
1286 ctr = vxor_u8_u32m1(ctr, __riscv_vget_v_u32m4_u32m1(ctr4blks, 2), vl);
1287 ctr = vxor_u8_u32m1(ctr, __riscv_vget_v_u32m4_u32m1(ctr4blks, 3), vl);
1288 }
1289
1290 for (; nblocks; nblocks--)
1291 {
1292 const unsigned char *l;
1293 vuint8m1_t l_ntzi;
1294 vuint8m1_t data;
1295 vuint32m1_t block;
1296
1297 l = ocb_get_l(c, ++n);
1298
1299 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1300 /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
1301 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1302 data = __riscv_vle8_v_u8m1(inbuf, vl_bytes);
1303 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1304 data = __riscv_vxor_vv_u8m1(data, cast_u32m1_u8m1(iv), vl_bytes);
1305 block = cast_u8m1_u32m1(data);
1306
1307 AES_CRYPT(d, m1, rounds, block, vl);
1308
1309 block = vxor_u8_u32m1(iv, block, vl);
1310 unaligned_store_u32m1(outbuf, block, vl);
1311
1312 /* Checksum_i = Checksum_{i-1} xor P_i */
1313 ctr = vxor_u8_u32m1(ctr, block, vl);
1314
1315 inbuf += BLOCKSIZE;
1316 outbuf += BLOCKSIZE;
1317 }
1318
1319 c->u_mode.ocb.data_nblocks = n;
1320
1321 unaligned_store_u32m1(c->u_iv.iv, iv, vl);
1322 unaligned_store_u32m1(c->u_ctr.ctr, ctr, vl);
1323
1324 clear_vec_regs();
1325
1326 return 0;
1327 }
1328
1329 size_t ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
1330 _gcry_aes_riscv_zvkned_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
1331 const void *inbuf_arg, size_t nblocks,
1332 int encrypt)
1333 {
1334 if (encrypt)
1335 return aes_riscv_ocb_enc(c, outbuf_arg, inbuf_arg, nblocks);
1336 else
1337 return aes_riscv_ocb_dec(c, outbuf_arg, inbuf_arg, nblocks);
1338 }
1339
1340 size_t ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2
1341 _gcry_aes_riscv_zvkned_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
1342 size_t nblocks)
1343 {
1344 RIJNDAEL_context *ctx = (void *)&c->context.c;
1345 const unsigned char *abuf = abuf_arg;
1346 u64 n = c->u_mode.ocb.aad_nblocks;
1347 const u32 *rk = ctx->keyschenc32[0];
1348 int rounds = ctx->rounds;
1349 size_t vl = 4;
1350 size_t vl_bytes = vl * 4;
1351 vuint32m1_t iv;
1352 vuint32m1_t ctr;
1353 ROUND_KEY_VARIABLES;
1354
1355 PRELOAD_ROUND_KEYS (rk, rounds, vl);
1356
1357 /* Preload Offset and Sum */
1358 iv = unaligned_load_u32m1(c->u_mode.ocb.aad_offset, vl);
1359 ctr = unaligned_load_u32m1(c->u_mode.ocb.aad_sum, vl);
1360
1361 if (nblocks >= 4)
1362 {
1363 vuint32m4_t ctr4blks = __riscv_vundefined_u32m4();
1364 vuint32m1_t zero = __riscv_vmv_v_x_u32m1(0, vl);
1365
1366 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 0, ctr);
1367 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 1, zero);
1368 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 2, zero);
1369 ctr4blks = __riscv_vset_v_u32m1_u32m4(ctr4blks, 3, zero);
1370
1371 for (; nblocks >= 4; nblocks -= 4)
1372 {
1373 const unsigned char *l;
1374 vuint8m1_t l_ntzi;
1375 vuint32m4_t data4blks = unaligned_load_u32m4(abuf, vl * 4);
1376 vuint32m4_t offsets = __riscv_vundefined_u32m4();
1377
1378 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1379 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
1380 l = ocb_get_l(c, ++n);
1381 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1382 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1383 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 0, iv);
1384
1385 l = ocb_get_l(c, ++n);
1386 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1387 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1388 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 1, iv);
1389
1390 l = ocb_get_l(c, ++n);
1391 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1392 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1393 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 2, iv);
1394
1395 l = ocb_get_l(c, ++n);
1396 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1397 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1398 offsets = __riscv_vset_v_u32m1_u32m4(offsets, 3, iv);
1399
1400 data4blks = vxor_u8_u32m4(offsets, data4blks, vl * 4);
1401
1402 AES_CRYPT(e, m4, rounds, data4blks, vl * 4);
1403
1404 ctr4blks = vxor_u8_u32m4(ctr4blks, data4blks, vl * 4);
1405
1406 abuf += 4 * BLOCKSIZE;
1407 }
1408
1409 /* Checksum_i = Checksum_{i-1} xor P_i */
1410 ctr = vxor_u8_u32m1(__riscv_vget_v_u32m4_u32m1(ctr4blks, 0),
1411 __riscv_vget_v_u32m4_u32m1(ctr4blks, 1), vl);
1412 ctr = vxor_u8_u32m1(ctr, __riscv_vget_v_u32m4_u32m1(ctr4blks, 2), vl);
1413 ctr = vxor_u8_u32m1(ctr, __riscv_vget_v_u32m4_u32m1(ctr4blks, 3), vl);
1414 }
1415
1416 for (; nblocks; nblocks--)
1417 {
1418 const unsigned char *l;
1419 vuint8m1_t l_ntzi;
1420 vuint32m1_t data;
1421
1422 data = unaligned_load_u32m1(abuf, vl);
1423
1424 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1425 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
1426 l = ocb_get_l(c, ++n);
1427 l_ntzi = __riscv_vle8_v_u8m1(l, vl_bytes);
1428 iv = vxor_u8_u32m1(iv, cast_u8m1_u32m1(l_ntzi), vl);
1429
1430 data = vxor_u8_u32m1(data, iv, vl);
1431
1432 AES_CRYPT(e, m1, rounds, data, vl);
1433
1434 ctr = vxor_u8_u32m1(ctr, data, vl);
1435
1436 abuf += BLOCKSIZE;
1437 }
1438
1439 c->u_mode.ocb.aad_nblocks = n;
1440
1441 unaligned_store_u32m1(c->u_mode.ocb.aad_offset, iv, vl);
1442 unaligned_store_u32m1(c->u_mode.ocb.aad_sum, ctr, vl);
1443
1444 clear_vec_regs();
1445
1446 return 0;
1447 }
1448
1449 static const u64 xts_gfmul_const[2] = { 0x87, 0x01 };
1450 static const u64 xts_swap64_const[2] = { 1, 0 };
1451
1452 static ASM_FUNC_ATTR_INLINE vuint32m1_t
1453 xts_gfmul_byA (vuint32m1_t vec_in, vuint64m1_t xts_gfmul,
1454 vuint64m1_t xts_swap64, size_t vl)
1455 {
1456 vuint64m1_t in_u64 = cast_u32m1_u64m1(vec_in);
1457 vuint64m1_t tmp1;
1458
1459 tmp1 =
1460 __riscv_vrgather_vv_u64m1(cast_u32m1_u64m1(vec_in), xts_swap64, vl / 2);
1461 tmp1 = cast_i64m1_u64m1(
1462 __riscv_vsra_vx_i64m1(cast_u64m1_i64m1(tmp1), 63, vl / 2));
1463 in_u64 = __riscv_vadd_vv_u64m1(in_u64, in_u64, vl / 2);
1464 tmp1 = __riscv_vand_vv_u64m1(tmp1, xts_gfmul, vl / 2);
1465
1466 return cast_u64m1_u32m1(__riscv_vxor_vv_u64m1(in_u64, tmp1, vl / 2));
1467 }
1468
1469 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
1470 aes_riscv_xts_enc (void *context, unsigned char *tweak_arg, void *outbuf_arg,
1471 const void *inbuf_arg, size_t nblocks)
1472 {
1473 RIJNDAEL_context *ctx = context;
1474 unsigned char *outbuf = outbuf_arg;
1475 const unsigned char *inbuf = inbuf_arg;
1476 const u32 *rk = ctx->keyschenc32[0];
1477 int rounds = ctx->rounds;
1478 size_t vl = 4;
1479 vuint32m1_t tweak;
1480 vuint64m1_t xts_gfmul = __riscv_vle64_v_u64m1(xts_gfmul_const, vl / 2);
1481 vuint64m1_t xts_swap64 = __riscv_vle64_v_u64m1(xts_swap64_const, vl / 2);
1482 ROUND_KEY_VARIABLES;
1483
1484 PRELOAD_ROUND_KEYS (rk, rounds, vl);
1485
1486 /* Preload tweak */
1487 tweak = unaligned_load_u32m1(tweak_arg, vl);
1488
1489 memory_barrier_with_vec(xts_gfmul);
1490 memory_barrier_with_vec(xts_swap64);
1491
1492 for (; nblocks >= 4; nblocks -= 4)
1493 {
1494 vuint32m4_t data4blks = unaligned_load_u32m4(inbuf, vl * 4);
1495 vuint32m4_t tweaks = __riscv_vundefined_u32m4();
1496
1497 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 0, tweak);
1498 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1499 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 1, tweak);
1500 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1501 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 2, tweak);
1502 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1503 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 3, tweak);
1504 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1505
1506 data4blks = vxor_u8_u32m4(tweaks, data4blks, vl * 4);
1507
1508 AES_CRYPT(e, m4, rounds, data4blks, vl * 4);
1509
1510 data4blks = vxor_u8_u32m4(tweaks, data4blks, vl * 4);
1511
1512 unaligned_store_u32m4(outbuf, data4blks, vl * 4);
1513
1514 inbuf += 4 * BLOCKSIZE;
1515 outbuf += 4 * BLOCKSIZE;
1516 }
1517
1518 for (; nblocks; nblocks--)
1519 {
1520 vuint32m1_t data = unaligned_load_u32m1(inbuf, vl);
1521 vuint32m1_t tweak0 = tweak;
1522
1523 data = vxor_u8_u32m1(data, tweak0, vl);
1524 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1525
1526 AES_CRYPT(e, m1, rounds, data, vl);
1527
1528 data = vxor_u8_u32m1(data, tweak0, vl);
1529 unaligned_store_u32m1(outbuf, data, vl);
1530
1531 inbuf += BLOCKSIZE;
1532 outbuf += BLOCKSIZE;
1533 }
1534
1535 unaligned_store_u32m1(tweak_arg, tweak, vl);
1536
1537 clear_vec_regs();
1538 }
1539
1540 static ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
1541 aes_riscv_xts_dec (void *context, unsigned char *tweak_arg, void *outbuf_arg,
1542 const void *inbuf_arg, size_t nblocks)
1543 {
1544 RIJNDAEL_context *ctx = context;
1545 unsigned char *outbuf = outbuf_arg;
1546 const unsigned char *inbuf = inbuf_arg;
1547 const u32 *rk = ctx->keyschdec32[0];
1548 int rounds = ctx->rounds;
1549 size_t vl = 4;
1550 vuint32m1_t tweak;
1551 vuint64m1_t xts_gfmul = __riscv_vle64_v_u64m1(xts_gfmul_const, vl / 2);
1552 vuint64m1_t xts_swap64 = __riscv_vle64_v_u64m1(xts_swap64_const, vl / 2);
1553 ROUND_KEY_VARIABLES;
1554
1555 if (!ctx->decryption_prepared)
1556 {
1557 do_prepare_decryption(ctx);
1558 ctx->decryption_prepared = 1;
1559 }
1560
1561 PRELOAD_ROUND_KEYS (rk, rounds, vl);
1562
1563 /* Preload tweak */
1564 tweak = unaligned_load_u32m1(tweak_arg, vl);
1565
1566 memory_barrier_with_vec(xts_gfmul);
1567 memory_barrier_with_vec(xts_swap64);
1568
1569 for (; nblocks >= 4; nblocks -= 4)
1570 {
1571 vuint32m4_t data4blks = unaligned_load_u32m4(inbuf, vl * 4);
1572 vuint32m4_t tweaks = __riscv_vundefined_u32m4();
1573
1574 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 0, tweak);
1575 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1576 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 1, tweak);
1577 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1578 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 2, tweak);
1579 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1580 tweaks = __riscv_vset_v_u32m1_u32m4(tweaks, 3, tweak);
1581 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1582
1583 data4blks = vxor_u8_u32m4(tweaks, data4blks, vl * 4);
1584
1585 AES_CRYPT(d, m4, rounds, data4blks, vl * 4);
1586
1587 data4blks = vxor_u8_u32m4(tweaks, data4blks, vl * 4);
1588
1589 unaligned_store_u32m4(outbuf, data4blks, vl * 4);
1590
1591 inbuf += 4 * BLOCKSIZE;
1592 outbuf += 4 * BLOCKSIZE;
1593 }
1594
1595 for (; nblocks; nblocks--)
1596 {
1597 vuint32m1_t data = unaligned_load_u32m1(inbuf, vl);
1598 vuint32m1_t tweak0 = tweak;
1599
1600 data = vxor_u8_u32m1(data, tweak0, vl);
1601 tweak = xts_gfmul_byA(tweak, xts_gfmul, xts_swap64, vl);
1602
1603 AES_CRYPT(d, m1, rounds, data, vl);
1604
1605 data = vxor_u8_u32m1(data, tweak0, vl);
1606 unaligned_store_u32m1(outbuf, data, vl);
1607
1608 inbuf += BLOCKSIZE;
1609 outbuf += BLOCKSIZE;
1610 }
1611
1612 unaligned_store_u32m1(tweak_arg, tweak, vl);
1613
1614 clear_vec_regs();
1615 }
1616
1617 ASM_FUNC_ATTR_NOINLINE FUNC_ATTR_OPT_O2 void
1618 _gcry_aes_riscv_zvkned_xts_crypt (void *context, unsigned char *tweak_arg,
1619 void *outbuf_arg, const void *inbuf_arg,
1620 size_t nblocks, int encrypt)
1621 {
1622 if (encrypt)
1623 aes_riscv_xts_enc(context, tweak_arg, outbuf_arg, inbuf_arg, nblocks);
1624 else
1625 aes_riscv_xts_dec(context, tweak_arg, outbuf_arg, inbuf_arg, nblocks);
1626 }
1627
1628 #endif /* HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS */