Debian Codegraph @

cipher/rijndael-vp-riscv.c

raw
1 /* RISC-V vector permutation AES for Libgcrypt
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 * The code is based on the public domain library libvpaes version 0.5
21 * available at http://crypto.stanford.edu/vpaes/ and which carries
22 * this notice:
23 *
24 * libvpaes: constant-time SSSE3 AES encryption and decryption.
25 * version 0.5
26 *
27 * By Mike Hamburg, Stanford University, 2009. Public domain.
28 * I wrote essentially all of this code. I did not write the test
29 * vectors; they are the NIST known answer tests. I hereby release all
30 * the code and documentation here that I wrote into the public domain.
31 *
32 * This is an implementation of AES following my paper,
33 * "Accelerating AES with Vector Permute Instructions"
34 * CHES 2009; http://shiftleft.org/papers/vector_aes/
35 */
36
37 #include <config.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h> /* for memcmp() */
41
42 #include "types.h" /* for byte and u32 typedefs */
43 #include "g10lib.h"
44 #include "cipher.h"
45 #include "bufhelp.h"
46 #include "rijndael-internal.h"
47 #include "cipher-internal.h"
48
49
50 #ifdef USE_VP_RISCV
51
52
53 /**********************************************************************
54 AT&T x86 asm to intrinsics conversion macros (RISC-V)
55 **********************************************************************/
56
57 #include <riscv_vector.h>
58 #include "simd-common-riscv.h"
59
60 /*
61 * SIMD128
62 */
63
64 typedef vuint8m1_t __m128i;
65
66 #define cast_m128i_to_s8(a) (__riscv_vreinterpret_v_u8m1_i8m1(a))
67 #define cast_m128i_to_u32(a) (__riscv_vreinterpret_v_u8m1_u32m1(a))
68 #define cast_m128i_to_u64(a) (__riscv_vreinterpret_v_u8m1_u64m1(a))
69 #define cast_m128i_to_s64(a) (__riscv_vreinterpret_v_u64m1_i64m1(cast_m128i_to_u64(a)))
70
71 #define cast_s8_to_m128i(a) (__riscv_vreinterpret_v_i8m1_u8m1(a))
72 #define cast_u32_to_m128i(a) (__riscv_vreinterpret_v_u32m1_u8m1(a))
73 #define cast_u64_to_m128i(a) (__riscv_vreinterpret_v_u64m1_u8m1(a))
74 #define cast_s64_to_m128i(a) (cast_u64_to_m128i(__riscv_vreinterpret_v_i64m1_u64m1(a)))
75
76 #define pand128(a, o) (o = __riscv_vand_vv_u8m1((o), (a), 16))
77 #define pandn128(a, o) (o = __riscv_vand_vv_u8m1(__riscv_vnot_v_u8m1((o), 16), (a), 16))
78 #define pxor128(a, o) (o = __riscv_vxor_vv_u8m1((o), (a), 16))
79 #define paddb128(a, o) (o = __riscv_vadd_vv_u8m1((o), (a), 16))
80 #define paddd128(a, o) (o = cast_u32_to_m128i(__riscv_vadd_vv_u32m1( \
81 cast_m128i_to_u32(o), \
82 cast_m128i_to_u32(a), 4)))
83 #define paddq128(a, o) (o = cast_u64_to_m128i(__riscv_vadd_vv_u64m1( \
84 cast_m128i_to_u64(o), \
85 cast_m128i_to_u64(a), 2)))
86
87 #define psrld128(s, o) (o = cast_u32_to_m128i(__riscv_vsrl_vx_u32m1(cast_m128i_to_u32(o), (s), 4))
88 #define psraq128(s, o) (o = cast_s64_to_m128i(__riscv_vsra_vx_i64m1(cast_m128i_to_s64(o), (s), 2)))
89 #define psrldq128(s, o) (o = __riscv_vslidedown_vx_u8m1((o), (s), 16))
90 #define pslldq128(s, o) ({ vuint8m1_t __tmp = __riscv_vmv_v_x_u8m1(0, 16); \
91 o = __riscv_vslideup_vx_u8m1(__tmp, (o), (s), 16); })
92 #define psrl_byte_128(s, o) (o = __riscv_vsrl_vx_u8m1((o), (s), 16))
93
94 #define pshufb128(m8, o) (o = __riscv_vrgather_vv_u8m1((o), (m8), 16))
95 #define pshufd128(m32, a, o) ({ static const __m128i_const __tmp1 = PSHUFD_MASK_TO_PSHUFB_MASK(m32); \
96 __m128i __tmp2; \
97 movdqa128(a, o); \
98 movdqa128_memld(&__tmp1, __tmp2); \
99 pshufb128(__tmp2, o); })
100
101 #define pshufd128_0x93(a, o) pshufd128(0x93, a, o)
102 #define pshufd128_0xFF(a, o) (o = cast_u32_to_m128i(__riscv_vrgather_vx_u32m1(cast_m128i_to_u32(a), 3, 4)))
103 #define pshufd128_0xFE(a, o) pshufd128(0xFE, a, o)
104 #define pshufd128_0x4E(a, o) pshufd128(0x4E, a, o)
105
106 #define palignr128(s, a, o) (o = __riscv_vslideup_vx_u8m1(__riscv_vslidedown_vx_u8m1((a), (s), 16), (o), 16 - (s), 16))
107
108 #define movdqa128(a, o) (o = (a))
109
110 #define movdqa128_memld(a, o) (o = __riscv_vle8_v_u8m1((const void *)(a), 16))
111
112 #define pand128_amemld(m, o) pand128(__riscv_vle8_v_u8m1((const void *)(m), 16), (o))
113 #define pxor128_amemld(m, o) pxor128(__riscv_vle8_v_u8m1((const void *)(m), 16), (o))
114 #define paddq128_amemld(m, o) paddq128(__riscv_vle8_v_u8m1((const void *)(m), 16), (o))
115 #define paddd128_amemld(m, o) paddd128(__riscv_vle8_v_u8m1((const void *)(m), 16), (o))
116 #define pshufb128_amemld(m, o) pshufb128(__riscv_vle8_v_u8m1((const void *)(m), 16), (o))
117
118 /* Following operations may have unaligned memory input */
119 #define movdqu128_memld(a, o) (o = __riscv_vle8_v_u8m1((const void *)(a), 16))
120
121 /* Following operations may have unaligned memory output */
122 #define movdqu128_memst(a, o) (__riscv_vse8_v_u8m1((void *)(o), (a), 16))
123
124 /*
125 * SIMD256
126 */
127
128 #define PSHUFD256_MASK_TO_PSHUFB256_MASK(m32) { \
129 M128I_BYTE(((((m32) >> 0) & 0x03) * 4) + 0, \
130 ((((m32) >> 0) & 0x03) * 4) + 1, \
131 ((((m32) >> 0) & 0x03) * 4) + 2, \
132 ((((m32) >> 0) & 0x03) * 4) + 3, \
133 ((((m32) >> 2) & 0x03) * 4) + 0, \
134 ((((m32) >> 2) & 0x03) * 4) + 1, \
135 ((((m32) >> 2) & 0x03) * 4) + 2, \
136 ((((m32) >> 2) & 0x03) * 4) + 3, \
137 ((((m32) >> 4) & 0x03) * 4) + 0, \
138 ((((m32) >> 4) & 0x03) * 4) + 1, \
139 ((((m32) >> 4) & 0x03) * 4) + 2, \
140 ((((m32) >> 4) & 0x03) * 4) + 3, \
141 ((((m32) >> 6) & 0x03) * 4) + 0, \
142 ((((m32) >> 6) & 0x03) * 4) + 1, \
143 ((((m32) >> 6) & 0x03) * 4) + 2, \
144 ((((m32) >> 6) & 0x03) * 4) + 3), \
145 M128I_BYTE(((((m32) >> 0) & 0x03) * 4) + 1 + 16, \
146 ((((m32) >> 0) & 0x03) * 4) + 1 + 16, \
147 ((((m32) >> 0) & 0x03) * 4) + 2 + 16, \
148 ((((m32) >> 0) & 0x03) * 4) + 3 + 16, \
149 ((((m32) >> 2) & 0x03) * 4) + 0 + 16, \
150 ((((m32) >> 2) & 0x03) * 4) + 1 + 16, \
151 ((((m32) >> 2) & 0x03) * 4) + 2 + 16, \
152 ((((m32) >> 2) & 0x03) * 4) + 3 + 16, \
153 ((((m32) >> 4) & 0x03) * 4) + 0 + 16, \
154 ((((m32) >> 4) & 0x03) * 4) + 1 + 16, \
155 ((((m32) >> 4) & 0x03) * 4) + 2 + 16, \
156 ((((m32) >> 4) & 0x03) * 4) + 3 + 16, \
157 ((((m32) >> 6) & 0x03) * 4) + 0 + 16, \
158 ((((m32) >> 6) & 0x03) * 4) + 1 + 16, \
159 ((((m32) >> 6) & 0x03) * 4) + 2 + 16, \
160 ((((m32) >> 6) & 0x03) * 4) + 3 + 16) }
161
162 typedef vuint8m1_t __m256i;
163
164 #define HAVE_SIMD256 1
165
166 #define check_simd256_support() (__riscv_vsetvl_e8m1(32) == 32)
167
168 #define cast_m256i_to_s8(a) cast_m128i_to_s8(a)
169 #define cast_m256i_to_u32(a) cast_m128i_to_u32(a)
170 #define cast_m256i_to_u64(a) cast_m128i_to_u64(a)
171 #define cast_m256i_to_s64(a) cast_m128i_to_s64(a)
172
173 #define cast_s8_to_m256i(a) (__riscv_vreinterpret_v_i8m1_u8m1(a))
174 #define cast_u32_to_m256i(a) (__riscv_vreinterpret_v_u32m1_u8m1(a))
175 #define cast_u64_to_m256i(a) (__riscv_vreinterpret_v_u64m1_u8m1(a))
176 #define cast_s64_to_m256i(a) (cast_u64_to_m128i(__riscv_vreinterpret_v_i64m1_u64m1(a)))
177
178 #define pand256(a, o) (o = __riscv_vand_vv_u8m1((o), (a), 32))
179 #define pandn256(a, o) (o = __riscv_vand_vv_u8m1(__riscv_vnot_v_u8m1((o), 32), (a), 32))
180 #define pxor256(a, o) (o = __riscv_vxor_vv_u8m1((o), (a), 32))
181 #define paddb256(a, o) (o = __riscv_vadd_vv_u8m1((o), (a), 32))
182 #define paddd256(a, o) (o = cast_u32_to_m256i(__riscv_vadd_vv_u32m1( \
183 cast_m256i_to_u32(o), \
184 cast_m256i_to_u32(a), 8)))
185 #define paddq256(a, o) (o = cast_u64_to_m256i(__riscv_vadd_vv_u64m1( \
186 cast_m256i_to_u64(o), \
187 cast_m256i_to_u64(a), 4)))
188
189 #define psrld256(s, o) (o = cast_u32_to_m256i(__riscv_vsrl_vx_u32m1(cast_m256i_to_u32(o), (s), 8))
190 #define psraq256(s, o) (o = cast_s64_to_m256i(__riscv_vsra_vx_i64m1(cast_m256i_to_s64(o), (s), 4)))
191 #define psrl_byte_256(s, o) (o = __riscv_vsrl_vx_u8m1((o), (s), 32))
192
193 /* Note: these are not PSHUFB equavalent as full 256-bit vector is used as
194 * 32 byte table. 256-bit PSHUFB on x86 handles 128-bit lanes separately as
195 * 128-bit 16 byte tables. */
196
197 /* tab32 variant: indexes have values 0..31. Used when 'm8' is constant and
198 * variable data is in 'o'. */
199 #define pshufb256_tab32(m8, o) (o = __riscv_vrgather_vv_u8m1((o), (m8), 32))
200
201 /* tab16 variant: indexes have values 0..16 and only low 128-bit of 'o' is
202 * used. Used when 'o' is constant and variable data is in 'm8'. */
203 #define pshufb256_tab16(m8, o) (o = __riscv_vrgather_vv_u8m1((o), (m8), 32))
204
205 /* Load 16 byte mask for 'pshufb256_tab32' usage as if 256-bit PSHUFB was to be
206 * used as on x86 (two separate 128-bit lanes). */
207 #define load_tab32_mask(m, o) ({ __m128i __tmp_lo128; \
208 __m128i __tmp_hi128; \
209 movdqu128_memld(m, __tmp_lo128); \
210 __tmp_hi128 = __riscv_vadd_vx_u8m1(__tmp_lo128, 16, 16); \
211 o = __riscv_vslideup_vx_u8m1(__tmp_lo128, __tmp_hi128, 16, 32); })
212
213 #define broadcast128_256(a, o) (o = __riscv_vslideup_vx_u8m1((a), (a), 16, 32))
214
215 /* Load 16 byte table for 'pshufb256_tab16' usage. On x86 this would splat
216 * 128-bit table from memory to both 128-bit lanes of 256-bit register.
217 * On RISC-V this just loads memory to lower 128-bits. */
218 #define load_tab16_table(m, o) movdqu128_memld(m, o)
219
220 #define pshufd256(m32, a, o) ({ static const __m128i_const __tmp1 = PSHUFD_MASK_TO_PSHUFB_MASK(m32); \
221 __m256i __tmp2; \
222 movdqa256(a, o); \
223 load_tab32_mask(&__tmp1, __tmp2); \
224 pshufb256_tab32(__tmp2, o); })
225
226 #define pshufd256_0x93(a, o) pshufd256(0x93, a, o)
227
228 #define insert256_hi128(x, o) (o = __riscv_vslideup_vx_u8m1((o), (x), 16, 32))
229 #define extract256_hi128(y, o) (o = __riscv_vslidedown_vx_u8m1((y), 16, 32))
230
231 #define movdqa256(a, o) (o = (a))
232
233 #define movdqa128_256(a, o) (o = (a))
234 #define movdqa256_128(a, o) (o = (a))
235
236 #define movdqa256_memld(a, o) (o = __riscv_vle8_v_u8m1((const void *)(a), 32))
237
238 #define pand256_amemld(m, o) pand128(__riscv_vle8_v_u8m1((const void *)(m), 32), (o))
239 #define pxor256_amemld(m, o) pxor128(__riscv_vle8_v_u8m1((const void *)(m), 32), (o))
240 #define paddq256_amemld(m, o) paddq128(__riscv_vle8_v_u8m1((const void *)(m), 32), (o))
241 #define paddd256_amemld(m, o) paddd128(__riscv_vle8_v_u8m1((const void *)(m), 32), (o))
242 #define pshufb256_amemld(m, o) pshufb128(__riscv_vle8_v_u8m1((const void *)(m), 32), (o))
243 #define broadcast128_256_amemld(m, o) \
244 broadcast128_256(__riscv_vle8_v_u8m1((const void *)(m), 32), (o))
245
246 /* Following operations may have unaligned memory input */
247 #define movdqu256_memld(a, o) (o = __riscv_vle8_v_u8m1((const void *)(a), 32))
248
249 /* Following operations may have unaligned memory output */
250 #define movdqu256_memst(a, o) (__riscv_vse8_v_u8m1((void *)(o), (a), 32))
251
252
253 #ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE
254 # define FUNC_ATTR_OPT __attribute__((optimize("-O2")))
255 #else
256 # define FUNC_ATTR_OPT
257 #endif
258
259 #define SIMD128_OPT_ATTR FUNC_ATTR_OPT
260
261 #define FUNC_ENCRYPT _gcry_aes_vp_riscv_encrypt
262 #define FUNC_DECRYPT _gcry_aes_vp_riscv_decrypt
263 #define FUNC_CFB_ENC _gcry_aes_vp_riscv_cfb_enc
264 #define FUNC_CFB_DEC _gcry_aes_vp_riscv_cfb_dec
265 #define FUNC_CBC_ENC _gcry_aes_vp_riscv_cbc_enc
266 #define FUNC_CBC_DEC _gcry_aes_vp_riscv_cbc_dec
267 #define FUNC_CTR_ENC _gcry_aes_vp_riscv_ctr_enc
268 #define FUNC_CTR32LE_ENC _gcry_aes_vp_riscv_ctr32le_enc
269 #define FUNC_OCB_CRYPT _gcry_aes_vp_riscv_ocb_crypt
270 #define FUNC_OCB_AUTH _gcry_aes_vp_riscv_ocb_auth
271 #define FUNC_ECB_CRYPT _gcry_aes_vp_riscv_ecb_crypt
272 #define FUNC_XTS_CRYPT _gcry_aes_vp_riscv_xts_crypt
273 #define FUNC_SETKEY _gcry_aes_vp_riscv_do_setkey
274 #define FUNC_PREPARE_DEC _gcry_aes_vp_riscv_prepare_decryption
275
276 #include "rijndael-vp-simd128.h"
277
278 int
279 _gcry_aes_vp_riscv_setup_acceleration(RIJNDAEL_context *ctx)
280 {
281 (void)ctx;
282 return (__riscv_vsetvl_e8m1(16) == 16);
283 }
284
285 #endif /* USE_VP_RISCV */