Debian Codegraph @

cipher/sm4-armv9-aarch64-sve-ce.S

raw
1 /* sm4-armv9-aarch64-sve-ce.S - ARMv9/AArch64 SVE Cryptography accelerated SM4
2 *
3 * Copyright (C) 2022 Alibaba Group.
4 * Copyright (C) 2022 Tianjia Zhang <[email protected]>
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "asm-common-aarch64.h"
23
24 #if defined(__AARCH64EL__) && \
25 defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) && \
26 defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO) && \
27 defined(HAVE_GCC_INLINE_ASM_AARCH64_SVE) && \
28 defined(HAVE_GCC_INLINE_ASM_AARCH64_SVE2) && \
29 defined(USE_SM4)
30
31 .cpu generic+simd+crypto+sve+sve2
32
33 /* Constants */
34
35 SECTION_RODATA
36 .align 4
37 ELF(.type _gcry_sm4_armv9_svesm4_consts,@object)
38 _gcry_sm4_armv9_svesm4_consts:
39 .Lbswap128_mask:
40 .byte 0x0c, 0x0d, 0x0e, 0x0f, 0x08, 0x09, 0x0a, 0x0b
41 .byte 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03
42 .byte 0x1c, 0x1d, 0x1e, 0x1f, 0x18, 0x19, 0x1a, 0x1b
43 .byte 0x14, 0x15, 0x16, 0x17, 0x10, 0x11, 0x12, 0x13
44 .byte 0x2c, 0x2d, 0x2e, 0x2f, 0x28, 0x29, 0x2a, 0x2b
45 .byte 0x24, 0x25, 0x26, 0x27, 0x20, 0x21, 0x22, 0x23
46 .byte 0x3c, 0x3d, 0x3e, 0x3f, 0x38, 0x39, 0x3a, 0x3b
47 .byte 0x34, 0x35, 0x36, 0x37, 0x30, 0x31, 0x32, 0x33
48 .byte 0x4c, 0x4d, 0x4e, 0x4f, 0x48, 0x49, 0x4a, 0x4b
49 .byte 0x44, 0x45, 0x46, 0x47, 0x40, 0x41, 0x42, 0x43
50 .byte 0x5c, 0x5d, 0x5e, 0x5f, 0x58, 0x59, 0x5a, 0x5b
51 .byte 0x54, 0x55, 0x56, 0x57, 0x50, 0x51, 0x52, 0x53
52 .byte 0x6c, 0x6d, 0x6e, 0x6f, 0x68, 0x69, 0x6a, 0x6b
53 .byte 0x64, 0x65, 0x66, 0x67, 0x60, 0x61, 0x62, 0x63
54 .byte 0x7c, 0x7d, 0x7e, 0x7f, 0x78, 0x79, 0x7a, 0x7b
55 .byte 0x74, 0x75, 0x76, 0x77, 0x70, 0x71, 0x72, 0x73
56 .byte 0x8c, 0x8d, 0x8e, 0x8f, 0x88, 0x89, 0x8a, 0x8b
57 .byte 0x84, 0x85, 0x86, 0x87, 0x80, 0x81, 0x82, 0x83
58 .byte 0x9c, 0x9d, 0x9e, 0x9f, 0x98, 0x99, 0x9a, 0x9b
59 .byte 0x94, 0x95, 0x96, 0x97, 0x90, 0x91, 0x92, 0x93
60 .byte 0xac, 0xad, 0xae, 0xaf, 0xa8, 0xa9, 0xaa, 0xab
61 .byte 0xa4, 0xa5, 0xa6, 0xa7, 0xa0, 0xa1, 0xa2, 0xa3
62 .byte 0xbc, 0xbd, 0xbe, 0xbf, 0xb8, 0xb9, 0xba, 0xbb
63 .byte 0xb4, 0xb5, 0xb6, 0xb7, 0xb0, 0xb1, 0xb2, 0xb3
64 .byte 0xcc, 0xcd, 0xce, 0xcf, 0xc8, 0xc9, 0xca, 0xcb
65 .byte 0xc4, 0xc5, 0xc6, 0xc7, 0xc0, 0xc1, 0xc2, 0xc3
66 .byte 0xdc, 0xdd, 0xde, 0xdf, 0xd8, 0xd9, 0xda, 0xdb
67 .byte 0xd4, 0xd5, 0xd6, 0xd7, 0xd0, 0xd1, 0xd2, 0xd3
68 .byte 0xec, 0xed, 0xee, 0xef, 0xe8, 0xe9, 0xea, 0xeb
69 .byte 0xe4, 0xe5, 0xe6, 0xe7, 0xe0, 0xe1, 0xe2, 0xe3
70 .byte 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb
71 .byte 0xf4, 0xf5, 0xf6, 0xf7, 0xf0, 0xf1, 0xf2, 0xf3
72
73 .Lle128_inc:
74 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
75 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
76 .byte 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
77 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
78 .byte 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
79 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
80 .byte 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
81 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
82 .byte 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
83 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
84 .byte 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
85 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
86 .byte 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
87 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
88 .byte 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
89 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
90 .byte 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
91 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
92 .byte 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
93 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
94 .byte 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
95 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
96 .byte 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
97 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
98 .byte 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
99 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
100 .byte 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
101 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
102 .byte 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
103 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
104 .byte 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
105 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
106 ELF(.size _gcry_sm4_armv9_svesm4_consts,.-_gcry_sm4_armv9_svesm4_consts)
107
108 /* Register macros */
109
110 #define RCTR z16
111 #define RCTRv v16
112 #define RIV z16
113 #define RIVv v16
114 #define RSWAP128 z17
115 #define RZERO z18
116 #define RLE128_INC z19
117
118 #define RTMP0 z20
119 #define RTMP1 z21
120 #define RTMP2 z22
121 #define RTMP3 z23
122 #define RTMP0v v20
123
124 #define vecnum_z0 0
125 #define vecnum_z1 1
126 #define vecnum_z2 2
127 #define vecnum_z3 3
128 #define vecnum_z4 4
129 #define vecnum_z5 5
130 #define vecnum_z6 6
131 #define vecnum_z7 7
132 #define vecnum_z8 8
133 #define vecnum_z9 9
134 #define vecnum_z10 10
135 #define vecnum_z11 11
136 #define vecnum_z12 12
137 #define vecnum_z13 13
138 #define vecnum_z14 14
139 #define vecnum_z15 15
140 #define vecnum_z16 16
141 #define vecnum_z24 24
142 #define vecnum_z25 25
143 #define vecnum_z26 26
144 #define vecnum_z27 27
145 #define vecnum_z28 28
146 #define vecnum_z29 29
147 #define vecnum_z30 30
148 #define vecnum_z31 31
149
150 #define vecnum_v0 0
151 #define vecnum_v15 15
152 #define vecnum_v24 24
153 #define vecnum_v25 25
154 #define vecnum_v26 26
155 #define vecnum_v27 27
156 #define vecnum_v28 28
157 #define vecnum_v29 29
158 #define vecnum_v30 30
159 #define vecnum_v31 31
160
161 #define sm4e_ce(vd, vn) \
162 .inst (0xcec08400 | (vecnum_##vn << 5) | vecnum_##vd)
163
164 #define sm4e_sve(zd, zm) \
165 .inst (0x4523e000 | (vecnum_##zm << 5) | vecnum_##zd)
166
167 /* Helper macros. */
168
169 #define PREPARE() \
170 GET_DATA_POINTER(x7, .Lbswap128_mask); \
171 ptrue p0.b, ALL; \
172 rdvl x5, #1; \
173 ld1b {RSWAP128.b}, p0/z, [x7]; \
174 \
175 ld1 {v24.16b-v27.16b}, [x0], #64; \
176 ld1 {v28.16b-v31.16b}, [x0]; \
177 dup z24.q, z24.q[0]; \
178 dup z25.q, z25.q[0]; \
179 dup z26.q, z26.q[0]; \
180 dup z27.q, z27.q[0]; \
181 dup z28.q, z28.q[0]; \
182 dup z29.q, z29.q[0]; \
183 dup z30.q, z30.q[0]; \
184 dup z31.q, z31.q[0];
185
186
187 #define SM4_SVE_CE_CRYPT_BLK(b0) \
188 revb b0.s, p0/m, b0.s; \
189 sm4e_sve(b0, z24); \
190 sm4e_sve(b0, z25); \
191 sm4e_sve(b0, z26); \
192 sm4e_sve(b0, z27); \
193 sm4e_sve(b0, z28); \
194 sm4e_sve(b0, z29); \
195 sm4e_sve(b0, z30); \
196 sm4e_sve(b0, z31); \
197 tbl b0.b, {b0.b}, RSWAP128.b; \
198 revb b0.s, p0/m, b0.s;
199
200
201 #define SM4_SVE_CE_CRYPT_BLK4(b0, b1, b2, b3) \
202 revb b0.s, p0/m, b0.s; \
203 revb b1.s, p0/m, b1.s; \
204 revb b2.s, p0/m, b2.s; \
205 revb b3.s, p0/m, b3.s; \
206 sm4e_sve(b0, z24); \
207 sm4e_sve(b1, z24); \
208 sm4e_sve(b2, z24); \
209 sm4e_sve(b3, z24); \
210 sm4e_sve(b0, z25); \
211 sm4e_sve(b1, z25); \
212 sm4e_sve(b2, z25); \
213 sm4e_sve(b3, z25); \
214 sm4e_sve(b0, z26); \
215 sm4e_sve(b1, z26); \
216 sm4e_sve(b2, z26); \
217 sm4e_sve(b3, z26); \
218 sm4e_sve(b0, z27); \
219 sm4e_sve(b1, z27); \
220 sm4e_sve(b2, z27); \
221 sm4e_sve(b3, z27); \
222 sm4e_sve(b0, z28); \
223 sm4e_sve(b1, z28); \
224 sm4e_sve(b2, z28); \
225 sm4e_sve(b3, z28); \
226 sm4e_sve(b0, z29); \
227 sm4e_sve(b1, z29); \
228 sm4e_sve(b2, z29); \
229 sm4e_sve(b3, z29); \
230 sm4e_sve(b0, z30); \
231 sm4e_sve(b1, z30); \
232 sm4e_sve(b2, z30); \
233 sm4e_sve(b3, z30); \
234 sm4e_sve(b0, z31); \
235 sm4e_sve(b1, z31); \
236 sm4e_sve(b2, z31); \
237 sm4e_sve(b3, z31); \
238 tbl b0.b, {b0.b}, RSWAP128.b; \
239 tbl b1.b, {b1.b}, RSWAP128.b; \
240 tbl b2.b, {b2.b}, RSWAP128.b; \
241 tbl b3.b, {b3.b}, RSWAP128.b; \
242 revb b0.s, p0/m, b0.s; \
243 revb b1.s, p0/m, b1.s; \
244 revb b2.s, p0/m, b2.s; \
245 revb b3.s, p0/m, b3.s;
246
247
248 #define SM4_SVE_CE_CRYPT_BLK8(b0, b1, b2, b3, b4, b5, b6, b7) \
249 revb b0.s, p0/m, b0.s; \
250 revb b1.s, p0/m, b1.s; \
251 revb b2.s, p0/m, b2.s; \
252 revb b3.s, p0/m, b3.s; \
253 revb b4.s, p0/m, b4.s; \
254 revb b5.s, p0/m, b5.s; \
255 revb b6.s, p0/m, b6.s; \
256 revb b7.s, p0/m, b7.s; \
257 sm4e_sve(b0, z24); \
258 sm4e_sve(b1, z24); \
259 sm4e_sve(b2, z24); \
260 sm4e_sve(b3, z24); \
261 sm4e_sve(b4, z24); \
262 sm4e_sve(b5, z24); \
263 sm4e_sve(b6, z24); \
264 sm4e_sve(b7, z24); \
265 sm4e_sve(b0, z25); \
266 sm4e_sve(b1, z25); \
267 sm4e_sve(b2, z25); \
268 sm4e_sve(b3, z25); \
269 sm4e_sve(b4, z25); \
270 sm4e_sve(b5, z25); \
271 sm4e_sve(b6, z25); \
272 sm4e_sve(b7, z25); \
273 sm4e_sve(b0, z26); \
274 sm4e_sve(b1, z26); \
275 sm4e_sve(b2, z26); \
276 sm4e_sve(b3, z26); \
277 sm4e_sve(b4, z26); \
278 sm4e_sve(b5, z26); \
279 sm4e_sve(b6, z26); \
280 sm4e_sve(b7, z26); \
281 sm4e_sve(b0, z27); \
282 sm4e_sve(b1, z27); \
283 sm4e_sve(b2, z27); \
284 sm4e_sve(b3, z27); \
285 sm4e_sve(b4, z27); \
286 sm4e_sve(b5, z27); \
287 sm4e_sve(b6, z27); \
288 sm4e_sve(b7, z27); \
289 sm4e_sve(b0, z28); \
290 sm4e_sve(b1, z28); \
291 sm4e_sve(b2, z28); \
292 sm4e_sve(b3, z28); \
293 sm4e_sve(b4, z28); \
294 sm4e_sve(b5, z28); \
295 sm4e_sve(b6, z28); \
296 sm4e_sve(b7, z28); \
297 sm4e_sve(b0, z29); \
298 sm4e_sve(b1, z29); \
299 sm4e_sve(b2, z29); \
300 sm4e_sve(b3, z29); \
301 sm4e_sve(b4, z29); \
302 sm4e_sve(b5, z29); \
303 sm4e_sve(b6, z29); \
304 sm4e_sve(b7, z29); \
305 sm4e_sve(b0, z30); \
306 sm4e_sve(b1, z30); \
307 sm4e_sve(b2, z30); \
308 sm4e_sve(b3, z30); \
309 sm4e_sve(b4, z30); \
310 sm4e_sve(b5, z30); \
311 sm4e_sve(b6, z30); \
312 sm4e_sve(b7, z30); \
313 sm4e_sve(b0, z31); \
314 sm4e_sve(b1, z31); \
315 sm4e_sve(b2, z31); \
316 sm4e_sve(b3, z31); \
317 sm4e_sve(b4, z31); \
318 sm4e_sve(b5, z31); \
319 sm4e_sve(b6, z31); \
320 sm4e_sve(b7, z31); \
321 tbl b0.b, {b0.b}, RSWAP128.b; \
322 tbl b1.b, {b1.b}, RSWAP128.b; \
323 tbl b2.b, {b2.b}, RSWAP128.b; \
324 tbl b3.b, {b3.b}, RSWAP128.b; \
325 tbl b4.b, {b4.b}, RSWAP128.b; \
326 tbl b5.b, {b5.b}, RSWAP128.b; \
327 tbl b6.b, {b6.b}, RSWAP128.b; \
328 tbl b7.b, {b7.b}, RSWAP128.b; \
329 revb b0.s, p0/m, b0.s; \
330 revb b1.s, p0/m, b1.s; \
331 revb b2.s, p0/m, b2.s; \
332 revb b3.s, p0/m, b3.s; \
333 revb b4.s, p0/m, b4.s; \
334 revb b5.s, p0/m, b5.s; \
335 revb b6.s, p0/m, b6.s; \
336 revb b7.s, p0/m, b7.s;
337
338
339 #define SM4_CE_CRYPT_BLK(b0) \
340 rev32 b0.16b, b0.16b; \
341 sm4e_ce(b0, v24); \
342 sm4e_ce(b0, v25); \
343 sm4e_ce(b0, v26); \
344 sm4e_ce(b0, v27); \
345 sm4e_ce(b0, v28); \
346 sm4e_ce(b0, v29); \
347 sm4e_ce(b0, v30); \
348 sm4e_ce(b0, v31); \
349 rev64 b0.4s, b0.4s; \
350 ext b0.16b, b0.16b, b0.16b, #8; \
351 rev32 b0.16b, b0.16b;
352
353 .text
354
355 .align 4
356 .global _gcry_sm4_armv9_sve_ce_crypt
357 ELF(.type _gcry_sm4_armv9_sve_ce_crypt,%function;)
358 _gcry_sm4_armv9_sve_ce_crypt:
359 /* input:
360 * x0: round key array, CTX
361 * x1: dst
362 * x2: src
363 * x3: nblocks
364 */
365 CFI_STARTPROC();
366
367 PREPARE();
368
369 .Lcrypt_loop_blks:
370 sub x3, x3, x5, LSR #1; /* x3 - (8 * VL) */
371 tbnz x3, #63, .Lcrypt_tail8;
372
373 ld1b {z0.b}, p0/z, [x2];
374 ld1b {z1.b}, p0/z, [x2, #1, MUL VL];
375 ld1b {z2.b}, p0/z, [x2, #2, MUL VL];
376 ld1b {z3.b}, p0/z, [x2, #3, MUL VL];
377 ld1b {z4.b}, p0/z, [x2, #4, MUL VL];
378 ld1b {z5.b}, p0/z, [x2, #5, MUL VL];
379 ld1b {z6.b}, p0/z, [x2, #6, MUL VL];
380 ld1b {z7.b}, p0/z, [x2, #7, MUL VL];
381 addvl x2, x2, #8;
382
383 SM4_SVE_CE_CRYPT_BLK8(z0, z1, z2, z3, z4, z5, z6, z7);
384
385 st1b {z0.b}, p0, [x1];
386 st1b {z1.b}, p0, [x1, #1, MUL VL];
387 st1b {z2.b}, p0, [x1, #2, MUL VL];
388 st1b {z3.b}, p0, [x1, #3, MUL VL];
389 st1b {z4.b}, p0, [x1, #4, MUL VL];
390 st1b {z5.b}, p0, [x1, #5, MUL VL];
391 st1b {z6.b}, p0, [x1, #6, MUL VL];
392 st1b {z7.b}, p0, [x1, #7, MUL VL];
393 addvl x1, x1, #8;
394
395 cbz x3, .Lcrypt_end;
396 b .Lcrypt_loop_blks;
397
398 .Lcrypt_tail8:
399 add x3, x3, x5, LSR #1;
400 cmp x3, x5, LSR #2;
401 blt .Lcrypt_tail4;
402
403 sub x3, x3, x5, LSR #2; /* x3 - (4 * VL) */
404
405 ld1b {z0.b}, p0/z, [x2];
406 ld1b {z1.b}, p0/z, [x2, #1, MUL VL];
407 ld1b {z2.b}, p0/z, [x2, #2, MUL VL];
408 ld1b {z3.b}, p0/z, [x2, #3, MUL VL];
409 addvl x2, x2, #4;
410
411 SM4_SVE_CE_CRYPT_BLK4(z0, z1, z2, z3);
412
413 st1b {z0.b}, p0, [x1];
414 st1b {z1.b}, p0, [x1, #1, MUL VL];
415 st1b {z2.b}, p0, [x1, #2, MUL VL];
416 st1b {z3.b}, p0, [x1, #3, MUL VL];
417 addvl x1, x1, #4;
418
419 cbz x3, .Lcrypt_end;
420
421 .Lcrypt_tail4:
422 cmp x3, x5, LSR #4;
423 blt .Lcrypt_tail;
424
425 sub x3, x3, x5, LSR #4; /* x3 - VL */
426
427 ld1b {z0.b}, p0/z, [x2];
428 addvl x2, x2, #1;
429
430 SM4_SVE_CE_CRYPT_BLK(z0);
431
432 st1b {z0.b}, p0, [x1];
433 addvl x1, x1, #1;
434
435 cbz x3, .Lcrypt_end;
436
437 .Lcrypt_tail:
438 sub x3, x3, #1;
439
440 ld1 {v0.16b}, [x2], #16;
441 SM4_CE_CRYPT_BLK(v0);
442 st1 {v0.16b}, [x1], #16;
443
444 cbnz x3, .Lcrypt_tail;
445
446 .Lcrypt_end:
447 ret_spec_stop;
448 CFI_ENDPROC();
449 ELF(.size _gcry_sm4_armv9_sve_ce_crypt,.-_gcry_sm4_armv9_sve_ce_crypt;)
450
451 .align 4
452 .global _gcry_sm4_armv9_sve_ce_cbc_dec
453 ELF(.type _gcry_sm4_armv9_sve_ce_cbc_dec,%function;)
454 _gcry_sm4_armv9_sve_ce_cbc_dec:
455 /* input:
456 * x0: round key array, CTX
457 * x1: dst
458 * x2: src
459 * x3: iv (big endian, 128 bit)
460 * x4: nblocks
461 */
462 CFI_STARTPROC();
463 VPUSH_ABI;
464
465 PREPARE();
466 ld1 {RIVv.16b}, [x3];
467 ext RIV.b, RIV.b, RIV.b, #16;
468
469 .Lcbc_loop_blks:
470 sub x4, x4, x5, LSR #1; /* x4 - (8 * VL) */
471 tbnz x4, #63, .Lcbc_tail8;
472
473 ld1b {z15.b}, p0/z, [x2];
474 ld1b {z14.b}, p0/z, [x2, #1, MUL VL];
475 ld1b {z13.b}, p0/z, [x2, #2, MUL VL];
476 ld1b {z12.b}, p0/z, [x2, #3, MUL VL];
477 ld1b {z11.b}, p0/z, [x2, #4, MUL VL];
478 ld1b {z10.b}, p0/z, [x2, #5, MUL VL];
479 ld1b {z9.b}, p0/z, [x2, #6, MUL VL];
480 ld1b {z8.b}, p0/z, [x2, #7, MUL VL];
481 rev z0.b, z15.b;
482 rev z1.b, z14.b;
483 rev z2.b, z13.b;
484 rev z3.b, z12.b;
485 rev z4.b, z11.b;
486 rev z5.b, z10.b;
487 rev z6.b, z9.b;
488 rev z7.b, z8.b;
489 rev RTMP0.b, RIV.b;
490 ext z7.b, z7.b, z6.b, #16;
491 ext z6.b, z6.b, z5.b, #16;
492 ext z5.b, z5.b, z4.b, #16;
493 ext z4.b, z4.b, z3.b, #16;
494 ext z3.b, z3.b, z2.b, #16;
495 ext z2.b, z2.b, z1.b, #16;
496 ext z1.b, z1.b, z0.b, #16;
497 ext z0.b, z0.b, RTMP0.b, #16;
498 rev z7.b, z7.b;
499 rev z6.b, z6.b;
500 rev z5.b, z5.b;
501 rev z4.b, z4.b;
502 rev z3.b, z3.b;
503 rev z2.b, z2.b;
504 rev z1.b, z1.b;
505 rev z0.b, z0.b;
506 mov RIV.d, z8.d;
507
508 SM4_SVE_CE_CRYPT_BLK8(z15, z14, z13, z12, z11, z10, z9, z8);
509
510 eor z0.d, z0.d, z15.d;
511 eor z1.d, z1.d, z14.d;
512 eor z2.d, z2.d, z13.d;
513 eor z3.d, z3.d, z12.d;
514 eor z4.d, z4.d, z11.d;
515 eor z5.d, z5.d, z10.d;
516 eor z6.d, z6.d, z9.d;
517 eor z7.d, z7.d, z8.d;
518 st1b {z0.b}, p0, [x1];
519 st1b {z1.b}, p0, [x1, #1, MUL VL];
520 st1b {z2.b}, p0, [x1, #2, MUL VL];
521 st1b {z3.b}, p0, [x1, #3, MUL VL];
522 st1b {z4.b}, p0, [x1, #4, MUL VL];
523 st1b {z5.b}, p0, [x1, #5, MUL VL];
524 st1b {z6.b}, p0, [x1, #6, MUL VL];
525 st1b {z7.b}, p0, [x1, #7, MUL VL];
526 addvl x2, x2, #8;
527 addvl x1, x1, #8;
528
529 cbz x4, .Lcbc_end;
530 b .Lcbc_loop_blks;
531
532 .Lcbc_tail8:
533 add x4, x4, x5, LSR #1;
534 cmp x4, x5, LSR #2;
535 blt .Lcbc_tail4;
536
537 sub x4, x4, x5, LSR #2; /* x4 - (4 * VL) */
538
539 ld1b {z15.b}, p0/z, [x2];
540 ld1b {z14.b}, p0/z, [x2, #1, MUL VL];
541 ld1b {z13.b}, p0/z, [x2, #2, MUL VL];
542 ld1b {z12.b}, p0/z, [x2, #3, MUL VL];
543 rev z0.b, z15.b;
544 rev z1.b, z14.b;
545 rev z2.b, z13.b;
546 rev z3.b, z12.b;
547 rev RTMP0.b, RIV.b;
548 ext z3.b, z3.b, z2.b, #16;
549 ext z2.b, z2.b, z1.b, #16;
550 ext z1.b, z1.b, z0.b, #16;
551 ext z0.b, z0.b, RTMP0.b, #16;
552 rev z3.b, z3.b;
553 rev z2.b, z2.b;
554 rev z1.b, z1.b;
555 rev z0.b, z0.b;
556 mov RIV.d, z12.d;
557
558 SM4_SVE_CE_CRYPT_BLK4(z15, z14, z13, z12);
559
560 eor z0.d, z0.d, z15.d;
561 eor z1.d, z1.d, z14.d;
562 eor z2.d, z2.d, z13.d;
563 eor z3.d, z3.d, z12.d;
564 st1b {z0.b}, p0, [x1];
565 st1b {z1.b}, p0, [x1, #1, MUL VL];
566 st1b {z2.b}, p0, [x1, #2, MUL VL];
567 st1b {z3.b}, p0, [x1, #3, MUL VL];
568 addvl x2, x2, #4;
569 addvl x1, x1, #4;
570
571 cbz x4, .Lcbc_end;
572
573 .Lcbc_tail4:
574 cmp x4, x5, LSR #4;
575 blt .Lcbc_tail_ce;
576
577 sub x4, x4, x5, LSR #4; /* x4 - VL */
578
579 ld1b {z15.b}, p0/z, [x2];
580 rev RTMP0.b, RIV.b;
581 rev z0.b, z15.b;
582 ext z0.b, z0.b, RTMP0.b, #16;
583 rev z0.b, z0.b;
584 mov RIV.d, z15.d;
585
586 SM4_SVE_CE_CRYPT_BLK(z15);
587
588 eor z0.d, z0.d, z15.d;
589 st1b {z0.b}, p0, [x1];
590 addvl x2, x2, #1;
591 addvl x1, x1, #1;
592
593 cbz x4, .Lcbc_end;
594 b .Lcbc_tail4;
595
596 .Lcbc_tail_ce:
597 rev RIV.s, RIV.s;
598 tbl RIV.b, {RIV.b}, RSWAP128.b;
599
600 .Lcbc_tail:
601 sub x4, x4, #1;
602
603 ld1 {v15.16b}, [x2], #16;
604 mov v0.16b, RIVv.16b;
605 mov RIVv.16b, v15.16b;
606 SM4_CE_CRYPT_BLK(v15);
607 eor v0.16b, v0.16b, v15.16b;
608 st1 {v0.16b}, [x1], #16;
609
610 cbnz x4, .Lcbc_tail;
611
612 ext RIV.b, RIV.b, RIV.b, #16;
613
614 .Lcbc_end:
615 /* store new IV */
616 rev RIV.s, RIV.s;
617 tbl RIV.b, {RIV.b}, RSWAP128.b;
618 st1 {RIVv.16b}, [x3];
619
620 VPOP_ABI;
621 CLEAR_ALL_REGS();
622 ret_spec_stop;
623 CFI_ENDPROC();
624 ELF(.size _gcry_sm4_armv9_sve_ce_cbc_dec,.-_gcry_sm4_armv9_sve_ce_cbc_dec;)
625
626 .align 4
627 .global _gcry_sm4_armv9_sve_ce_cfb_dec
628 ELF(.type _gcry_sm4_armv9_sve_ce_cfb_dec,%function;)
629 _gcry_sm4_armv9_sve_ce_cfb_dec:
630 /* input:
631 * x0: round key array, CTX
632 * x1: dst
633 * x2: src
634 * x3: iv (big endian, 128 bit)
635 * x4: nblocks
636 */
637 CFI_STARTPROC();
638 VPUSH_ABI;
639
640 PREPARE();
641 ld1 {RIVv.16b}, [x3];
642 ext RIV.b, RIV.b, RIV.b, #16;
643
644 .Lcfb_loop_blks:
645 sub x4, x4, x5, LSR #1; /* x4 - (8 * VL) */
646 tbnz x4, #63, .Lcfb_tail8;
647
648 ld1b {z15.b}, p0/z, [x2];
649 ld1b {z14.b}, p0/z, [x2, #1, MUL VL];
650 ld1b {z13.b}, p0/z, [x2, #2, MUL VL];
651 ld1b {z12.b}, p0/z, [x2, #3, MUL VL];
652 ld1b {z11.b}, p0/z, [x2, #4, MUL VL];
653 ld1b {z10.b}, p0/z, [x2, #5, MUL VL];
654 ld1b {z9.b}, p0/z, [x2, #6, MUL VL];
655 ld1b {z8.b}, p0/z, [x2, #7, MUL VL];
656 rev z0.b, z15.b;
657 rev z1.b, z14.b;
658 rev z2.b, z13.b;
659 rev z3.b, z12.b;
660 rev z4.b, z11.b;
661 rev z5.b, z10.b;
662 rev z6.b, z9.b;
663 rev z7.b, z8.b;
664 rev RTMP0.b, RIV.b;
665 ext z7.b, z7.b, z6.b, #16;
666 ext z6.b, z6.b, z5.b, #16;
667 ext z5.b, z5.b, z4.b, #16;
668 ext z4.b, z4.b, z3.b, #16;
669 ext z3.b, z3.b, z2.b, #16;
670 ext z2.b, z2.b, z1.b, #16;
671 ext z1.b, z1.b, z0.b, #16;
672 ext z0.b, z0.b, RTMP0.b, #16;
673 rev z7.b, z7.b;
674 rev z6.b, z6.b;
675 rev z5.b, z5.b;
676 rev z4.b, z4.b;
677 rev z3.b, z3.b;
678 rev z2.b, z2.b;
679 rev z1.b, z1.b;
680 rev z0.b, z0.b;
681 mov RIV.d, z8.d;
682
683 SM4_SVE_CE_CRYPT_BLK8(z0, z1, z2, z3, z4, z5, z6, z7);
684
685 eor z0.d, z0.d, z15.d;
686 eor z1.d, z1.d, z14.d;
687 eor z2.d, z2.d, z13.d;
688 eor z3.d, z3.d, z12.d;
689 eor z4.d, z4.d, z11.d;
690 eor z5.d, z5.d, z10.d;
691 eor z6.d, z6.d, z9.d;
692 eor z7.d, z7.d, z8.d;
693 st1b {z0.b}, p0, [x1];
694 st1b {z1.b}, p0, [x1, #1, MUL VL];
695 st1b {z2.b}, p0, [x1, #2, MUL VL];
696 st1b {z3.b}, p0, [x1, #3, MUL VL];
697 st1b {z4.b}, p0, [x1, #4, MUL VL];
698 st1b {z5.b}, p0, [x1, #5, MUL VL];
699 st1b {z6.b}, p0, [x1, #6, MUL VL];
700 st1b {z7.b}, p0, [x1, #7, MUL VL];
701 addvl x2, x2, #8;
702 addvl x1, x1, #8;
703
704 cbz x4, .Lcfb_end;
705 b .Lcfb_loop_blks;
706
707 .Lcfb_tail8:
708 add x4, x4, x5, LSR #1;
709 cmp x4, x5, LSR #2;
710 blt .Lcfb_tail4;
711
712 sub x4, x4, x5, LSR #2; /* x4 - (4 * VL) */
713
714 ld1b {z15.b}, p0/z, [x2];
715 ld1b {z14.b}, p0/z, [x2, #1, MUL VL];
716 ld1b {z13.b}, p0/z, [x2, #2, MUL VL];
717 ld1b {z12.b}, p0/z, [x2, #3, MUL VL];
718 rev z0.b, z15.b;
719 rev z1.b, z14.b;
720 rev z2.b, z13.b;
721 rev z3.b, z12.b;
722 rev RTMP0.b, RIV.b;
723 ext z3.b, z3.b, z2.b, #16;
724 ext z2.b, z2.b, z1.b, #16;
725 ext z1.b, z1.b, z0.b, #16;
726 ext z0.b, z0.b, RTMP0.b, #16;
727 rev z3.b, z3.b;
728 rev z2.b, z2.b;
729 rev z1.b, z1.b;
730 rev z0.b, z0.b;
731 mov RIV.d, z12.d;
732
733 SM4_SVE_CE_CRYPT_BLK4(z0, z1, z2, z3);
734
735 eor z0.d, z0.d, z15.d;
736 eor z1.d, z1.d, z14.d;
737 eor z2.d, z2.d, z13.d;
738 eor z3.d, z3.d, z12.d;
739 st1b {z0.b}, p0, [x1];
740 st1b {z1.b}, p0, [x1, #1, MUL VL];
741 st1b {z2.b}, p0, [x1, #2, MUL VL];
742 st1b {z3.b}, p0, [x1, #3, MUL VL];
743 addvl x2, x2, #4;
744 addvl x1, x1, #4;
745
746 cbz x4, .Lcfb_end;
747
748 .Lcfb_tail4:
749 cmp x4, x5, LSR #4;
750 blt .Lcfb_tail_ce;
751
752 sub x4, x4, x5, LSR #4; /* x4 - VL */
753
754 ld1b {z15.b}, p0/z, [x2];
755 rev RTMP0.b, RIV.b;
756 rev z0.b, z15.b;
757 ext z0.b, z0.b, RTMP0.b, #16;
758 rev z0.b, z0.b;
759 mov RIV.d, z15.d;
760
761 SM4_SVE_CE_CRYPT_BLK(z0);
762
763 eor z0.d, z0.d, z15.d;
764 st1b {z0.b}, p0, [x1];
765 addvl x2, x2, #1;
766 addvl x1, x1, #1;
767
768 cbz x4, .Lcfb_end;
769 b .Lcfb_tail4;
770
771 .Lcfb_tail_ce:
772 rev RIV.s, RIV.s;
773 tbl RIV.b, {RIV.b}, RSWAP128.b;
774
775 .Lcfb_tail:
776 sub x4, x4, #1;
777
778 ld1 {v15.16b}, [x2], #16;
779 mov v0.16b, RIVv.16b;
780 mov RIVv.16b, v15.16b;
781 SM4_CE_CRYPT_BLK(v0);
782 eor v0.16b, v0.16b, v15.16b;
783 st1 {v0.16b}, [x1], #16;
784
785 cbnz x4, .Lcfb_tail;
786
787 ext RIV.b, RIV.b, RIV.b, #16;
788
789 .Lcfb_end:
790 /* store new IV */
791 rev RIV.s, RIV.s;
792 tbl RIV.b, {RIV.b}, RSWAP128.b;
793 st1 {RIVv.16b}, [x3];
794
795 VPOP_ABI;
796 CLEAR_ALL_REGS();
797 ret_spec_stop;
798 CFI_ENDPROC();
799 ELF(.size _gcry_sm4_armv9_sve_ce_cfb_dec,.-_gcry_sm4_armv9_sve_ce_cfb_dec;)
800
801 .align 4
802 .global _gcry_sm4_armv9_sve_ce_ctr_enc
803 ELF(.type _gcry_sm4_armv9_sve_ce_ctr_enc,%function;)
804 _gcry_sm4_armv9_sve_ce_ctr_enc:
805 /* input:
806 * x0: round key array, CTX
807 * x1: dst
808 * x2: src
809 * x3: ctr (big endian, 128 bit)
810 * x4: nblocks
811 */
812 CFI_STARTPROC();
813
814 PREPARE();
815
816 dup RZERO.d, #0;
817 GET_DATA_POINTER(x6, .Lle128_inc);
818 ld1b {RLE128_INC.b}, p0/z, [x6];
819
820 ldp x7, x8, [x3];
821 rev x7, x7;
822 rev x8, x8;
823
824 #define inc_le128(zctr) \
825 mov RCTRv.d[1], x8; \
826 mov RCTRv.d[0], x7; \
827 mov zctr.d, RLE128_INC.d; \
828 dup RCTR.q, RCTR.q[0]; \
829 adds x8, x8, x5, LSR #4; \
830 adc x7, x7, xzr; \
831 adclt zctr.d, RCTR.d, RZERO.d; \
832 adclt RCTR.d, zctr.d, RZERO.d; \
833 trn1 zctr.d, RCTR.d, zctr.d; \
834 revb zctr.d, p0/m, zctr.d;
835
836 .Lctr_loop_blks:
837 sub x4, x4, x5, LSR #1; /* x4 - (8 * VL) */
838 tbnz x4, #63, .Lctr_tail8;
839
840 inc_le128(z0);
841 inc_le128(z1);
842 inc_le128(z2);
843 inc_le128(z3);
844 inc_le128(z4);
845 inc_le128(z5);
846 inc_le128(z6);
847 inc_le128(z7);
848
849 SM4_SVE_CE_CRYPT_BLK8(z0, z1, z2, z3, z4, z5, z6, z7);
850
851 ld1b {RTMP0.b}, p0/z, [x2];
852 ld1b {RTMP1.b}, p0/z, [x2, #1, MUL VL];
853 ld1b {RTMP2.b}, p0/z, [x2, #2, MUL VL];
854 ld1b {RTMP3.b}, p0/z, [x2, #3, MUL VL];
855 eor z0.d, z0.d, RTMP0.d;
856 eor z1.d, z1.d, RTMP1.d;
857 eor z2.d, z2.d, RTMP2.d;
858 eor z3.d, z3.d, RTMP3.d;
859 ld1b {RTMP0.b}, p0/z, [x2, #4, MUL VL];
860 ld1b {RTMP1.b}, p0/z, [x2, #5, MUL VL];
861 ld1b {RTMP2.b}, p0/z, [x2, #6, MUL VL];
862 ld1b {RTMP3.b}, p0/z, [x2, #7, MUL VL];
863 eor z4.d, z4.d, RTMP0.d;
864 eor z5.d, z5.d, RTMP1.d;
865 eor z6.d, z6.d, RTMP2.d;
866 eor z7.d, z7.d, RTMP3.d;
867 addvl x2, x2, #8;
868
869 st1b {z0.b}, p0, [x1];
870 st1b {z1.b}, p0, [x1, #1, MUL VL];
871 st1b {z2.b}, p0, [x1, #2, MUL VL];
872 st1b {z3.b}, p0, [x1, #3, MUL VL];
873 st1b {z4.b}, p0, [x1, #4, MUL VL];
874 st1b {z5.b}, p0, [x1, #5, MUL VL];
875 st1b {z6.b}, p0, [x1, #6, MUL VL];
876 st1b {z7.b}, p0, [x1, #7, MUL VL];
877 addvl x1, x1, #8;
878
879 cbz x4, .Lctr_end;
880 b .Lctr_loop_blks;
881
882 .Lctr_tail8:
883 add x4, x4, x5, LSR #1;
884 cmp x4, x5, LSR #2;
885 blt .Lctr_tail4;
886
887 sub x4, x4, x5, LSR #2; /* x4 - (4 * VL) */
888
889 inc_le128(z0);
890 inc_le128(z1);
891 inc_le128(z2);
892 inc_le128(z3);
893
894 SM4_SVE_CE_CRYPT_BLK4(z0, z1, z2, z3);
895
896 ld1b {RTMP0.b}, p0/z, [x2];
897 ld1b {RTMP1.b}, p0/z, [x2, #1, MUL VL];
898 ld1b {RTMP2.b}, p0/z, [x2, #2, MUL VL];
899 ld1b {RTMP3.b}, p0/z, [x2, #3, MUL VL];
900 eor z0.d, z0.d, RTMP0.d;
901 eor z1.d, z1.d, RTMP1.d;
902 eor z2.d, z2.d, RTMP2.d;
903 eor z3.d, z3.d, RTMP3.d;
904 st1b {z0.b}, p0, [x1];
905 st1b {z1.b}, p0, [x1, #1, MUL VL];
906 st1b {z2.b}, p0, [x1, #2, MUL VL];
907 st1b {z3.b}, p0, [x1, #3, MUL VL];
908 addvl x2, x2, #4;
909 addvl x1, x1, #4;
910
911 cbz x4, .Lctr_end;
912
913 .Lctr_tail4:
914 cmp x4, x5, LSR #4;
915 blt .Lctr_tail;
916
917 sub x4, x4, x5, LSR #4; /* x4 - VL */
918
919 inc_le128(z0);
920 SM4_SVE_CE_CRYPT_BLK(z0);
921 ld1b {RTMP0.b}, p0/z, [x2];
922 eor z0.d, z0.d, RTMP0.d;
923 st1b {z0.b}, p0, [x1];
924 addvl x2, x2, #1;
925 addvl x1, x1, #1;
926
927 cbz x4, .Lctr_end;
928 b .Lctr_tail4;
929
930 .Lctr_tail:
931 sub x4, x4, #1;
932
933 /* inc_le128 for CE */
934 mov v0.d[1], x8;
935 mov v0.d[0], x7;
936 adds x8, x8, #1;
937 adc x7, x7, xzr;
938 rev64 v0.16b, v0.16b;
939
940 SM4_CE_CRYPT_BLK(v0);
941 ld1 {RTMP0v.16b}, [x2], #16;
942 eor v0.16b, v0.16b, RTMP0v.16b;
943 st1 {v0.16b}, [x1], #16;
944
945 cbnz x4, .Lctr_tail;
946
947 .Lctr_end:
948 /* store new CTR */
949 rev x7, x7;
950 rev x8, x8;
951 stp x7, x8, [x3];
952
953 CLEAR_ALL_REGS();
954 ret_spec_stop;
955 CFI_ENDPROC();
956 ELF(.size _gcry_sm4_armv9_sve_ce_ctr_enc,.-_gcry_sm4_armv9_sve_ce_ctr_enc;)
957
958 .align 4
959 .global _gcry_sm4_armv9_sve_get_vl
960 ELF(.type _gcry_sm4_armv9_sve_get_vl,%function;)
961 _gcry_sm4_armv9_sve_get_vl:
962 CFI_STARTPROC();
963
964 /* VL in bytes */
965 rdvl x0, #1;
966
967 ret_spec_stop;
968 CFI_ENDPROC();
969 ELF(.size _gcry_sm4_armv9_sve_get_vl,.-_gcry_sm4_armv9_sve_get_vl;)
970
971 #endif