| 1 |
/* crc-riscv-zbb-zbc.c - RISC-V Zbb+Zbc accelerated CRC implementation |
| 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 <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
|
| 29 |
#include "bufhelp.h" |
| 30 |
|
| 31 |
|
| 32 |
#if defined (__riscv) && \ |
| 33 |
(__riscv_xlen == 64) && \ |
| 34 |
defined(HAVE_GCC_INLINE_ASM_RISCV) |
| 35 |
|
| 36 |
|
| 37 |
#define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 38 |
#define NO_INLINE __attribute__((noinline)) |
| 39 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 40 |
|
| 41 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 42 |
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE |
| 43 |
#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE |
| 44 |
|
| 45 |
|
| 46 |
typedef struct |
| 47 |
{ |
| 48 |
u64 lo; |
| 49 |
u64 hi; |
| 50 |
} u64x2; |
| 51 |
|
| 52 |
|
| 53 |
/* Constants structure for generic reflected/non-reflected CRC32 CLMUL |
| 54 |
* functions. */ |
| 55 |
struct crc32_consts_s |
| 56 |
{ |
| 57 |
/* k: { x^(32*5), x^(32*3), x^(32*2) } mod P(x) */ |
| 58 |
u64 k[3]; |
| 59 |
/* my_p: { floor(x^64 / P(x)), P(x) } */ |
| 60 |
u64 my_p[2]; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/* CLMUL constants for CRC32 and CRC32RFC1510. */ |
| 65 |
static const struct crc32_consts_s crc32_consts = |
| 66 |
{ |
| 67 |
{ /* k[3] = reverse_33bits( x^(32*y) mod P(x) ) */ |
| 68 |
U64_C(0x1751997d0), U64_C(0x0ccaa009e), /* y = { 5, 3 } */ |
| 69 |
U64_C(0x163cd6124) /* y = 2 */ |
| 70 |
}, |
| 71 |
{ /* my_p[2] = reverse_33bits ( { floor(x^64 / P(x)), P(x) } ) */ |
| 72 |
U64_C(0x1f7011641), U64_C(0x1db710641) |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
/* CLMUL constants for CRC24RFC2440 (polynomial multiplied with x⁸). */ |
| 77 |
static const struct crc32_consts_s crc24rfc2440_consts = |
| 78 |
{ |
| 79 |
{ /* k[3] = x^(32*y) mod P(x) << 32*/ |
| 80 |
U64_C(0xc4b14d00) << 32, U64_C(0xfd7e0c00) << 32, /* y = { 5, 3 } */ |
| 81 |
U64_C(0xd9fe8c00) << 32 /* y = 2 */ |
| 82 |
}, |
| 83 |
{ /* my_p[2] = { floor(x^64 / P(x)), P(x) } */ |
| 84 |
U64_C(0x1f845fe24), U64_C(0x1864cfb00) |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
static ASM_FUNC_ATTR_INLINE u64 |
| 90 |
clmul_low(u64 a, u64 b) |
| 91 |
{ |
| 92 |
u64 out; |
| 93 |
asm (".option push;\n\t" |
| 94 |
".option arch, +zbc;\n\t" |
| 95 |
"clmul %0, %1, %2;\n\t" |
| 96 |
".option pop;\n\t" |
| 97 |
: "=r" (out) |
| 98 |
: "r" (a), "r" (b)); |
| 99 |
return out; |
| 100 |
} |
| 101 |
|
| 102 |
static ASM_FUNC_ATTR_INLINE u64 |
| 103 |
clmul_high(u64 a, u64 b) |
| 104 |
{ |
| 105 |
u64 out; |
| 106 |
asm (".option push;\n\t" |
| 107 |
".option arch, +zbc;\n\t" |
| 108 |
"clmulh %0, %1, %2;\n\t" |
| 109 |
".option pop;\n\t" |
| 110 |
: "=r" (out) |
| 111 |
: "r" (a), "r" (b)); |
| 112 |
return out; |
| 113 |
} |
| 114 |
|
| 115 |
static ASM_FUNC_ATTR_INLINE u64 |
| 116 |
byteswap_u64(u64 x) |
| 117 |
{ |
| 118 |
asm (".option push;\n\t" |
| 119 |
".option arch, +zbb;\n\t" |
| 120 |
"rev8 %0, %1;\n\t" |
| 121 |
".option pop;\n\t" |
| 122 |
: "=r" (x) |
| 123 |
: "r" (x)); |
| 124 |
return x; |
| 125 |
} |
| 126 |
|
| 127 |
static ASM_FUNC_ATTR_INLINE u64 |
| 128 |
and_u64(u64 a, u64 b) |
| 129 |
{ |
| 130 |
asm ("and %0, %1, %2;\n\t" |
| 131 |
: "=r" (a) |
| 132 |
: "r" (a), "r" (b)); |
| 133 |
return a; |
| 134 |
} |
| 135 |
|
| 136 |
static ASM_FUNC_ATTR_INLINE u64x2 |
| 137 |
byteswap_u64x2(u64x2 in) |
| 138 |
{ |
| 139 |
u64x2 out; |
| 140 |
out.lo = byteswap_u64(in.hi); |
| 141 |
out.hi = byteswap_u64(in.lo); |
| 142 |
return out; |
| 143 |
} |
| 144 |
|
| 145 |
static ASM_FUNC_ATTR_INLINE u64 |
| 146 |
byteswap_u32(u64 x) |
| 147 |
{ |
| 148 |
return byteswap_u64(x) >> 32; |
| 149 |
} |
| 150 |
|
| 151 |
static ASM_FUNC_ATTR_INLINE u64 |
| 152 |
load_aligned_u32(const void *ptr) |
| 153 |
{ |
| 154 |
u64 out; |
| 155 |
asm ("lw %0, 0(%1)" |
| 156 |
: "=r" (out) |
| 157 |
: "r" (ptr) |
| 158 |
: "memory"); |
| 159 |
return out; |
| 160 |
} |
| 161 |
|
| 162 |
static ASM_FUNC_ATTR_INLINE u64x2 |
| 163 |
load_aligned_u64x2(const void *ptr) |
| 164 |
{ |
| 165 |
u64x2 vec; |
| 166 |
|
| 167 |
asm ("ld %0, 0(%1)" |
| 168 |
: "=r" (vec.lo) |
| 169 |
: "r" (ptr) |
| 170 |
: "memory"); |
| 171 |
asm ("ld %0, 8(%1)" |
| 172 |
: "=r" (vec.hi) |
| 173 |
: "r" (ptr) |
| 174 |
: "memory"); |
| 175 |
|
| 176 |
return vec; |
| 177 |
} |
| 178 |
|
| 179 |
static ASM_FUNC_ATTR_INLINE u64x2 |
| 180 |
clmul_128(u64 a, u64 b) |
| 181 |
{ |
| 182 |
u64x2 res; |
| 183 |
res.lo = clmul_low(a, b); |
| 184 |
res.hi = clmul_high(a, b); |
| 185 |
return res; |
| 186 |
} |
| 187 |
|
| 188 |
static ASM_FUNC_ATTR_INLINE u64x2 |
| 189 |
xor_128(u64x2 a, u64x2 b) |
| 190 |
{ |
| 191 |
u64x2 res; |
| 192 |
res.lo = a.lo ^ b.lo; |
| 193 |
res.hi = a.hi ^ b.hi; |
| 194 |
return res; |
| 195 |
} |
| 196 |
|
| 197 |
static ASM_FUNC_ATTR_INLINE u64 |
| 198 |
crc32r_reduction_4 (u64 data, u64 crc, const struct crc32_consts_s *consts) |
| 199 |
{ |
| 200 |
u64 step1, step2; |
| 201 |
|
| 202 |
step1 = clmul_low(data, consts->my_p[0]); |
| 203 |
step1 = and_u64(step1, 0xFFFFFFFFU); |
| 204 |
step2 = clmul_low(step1, consts->my_p[1]); |
| 205 |
|
| 206 |
return (step2 >> 32) ^ crc; |
| 207 |
} |
| 208 |
|
| 209 |
static ASM_FUNC_ATTR_INLINE void |
| 210 |
bulk_crc32r (u32 *pcrc, const byte **inbuf, size_t *inlen, |
| 211 |
const struct crc32_consts_s *consts) |
| 212 |
{ |
| 213 |
u64 crc = *pcrc; |
| 214 |
u64 k[2] = { consts->k[0], consts->k[1] }; |
| 215 |
u64x2 x0, x1, x2; |
| 216 |
|
| 217 |
x0 = load_aligned_u64x2(*inbuf); |
| 218 |
x0.lo ^= crc; |
| 219 |
|
| 220 |
*inbuf += 16; |
| 221 |
*inlen -= 16; |
| 222 |
|
| 223 |
/* Fold by 128 bits */ |
| 224 |
while (*inlen >= 16) |
| 225 |
{ |
| 226 |
x2 = load_aligned_u64x2(*inbuf); |
| 227 |
|
| 228 |
x1 = clmul_128(x0.lo, k[0]); |
| 229 |
x0 = clmul_128(x0.hi, k[1]); |
| 230 |
|
| 231 |
x0 = xor_128(x0, x2); |
| 232 |
x0 = xor_128(x0, x1); |
| 233 |
|
| 234 |
*inbuf += 16; |
| 235 |
*inlen -= 16; |
| 236 |
} |
| 237 |
|
| 238 |
/* Reduce 128 bits to 96 bits */ |
| 239 |
x1 = clmul_128(x0.lo, k[1]); |
| 240 |
x1.lo ^= x0.hi; |
| 241 |
|
| 242 |
/* Reduce 96 bits to 64 bits */ |
| 243 |
crc = (x1.lo >> 32) ^ (x1.hi << 32); |
| 244 |
crc ^= clmul_low(x1.lo & 0xFFFFFFFFU, consts->k[2]); |
| 245 |
|
| 246 |
/* Reduce 64 bits to 32 bits */ |
| 247 |
crc = crc32r_reduction_4(crc, crc >> 32, consts); |
| 248 |
|
| 249 |
*pcrc = crc; |
| 250 |
} |
| 251 |
|
| 252 |
static ASM_FUNC_ATTR_INLINE u64 |
| 253 |
tail_crc32r (u64 crc, const byte *inbuf, size_t inlen, |
| 254 |
const struct crc32_consts_s *consts) |
| 255 |
{ |
| 256 |
u64 data; |
| 257 |
|
| 258 |
switch (inlen) |
| 259 |
{ |
| 260 |
case 0: |
| 261 |
default: |
| 262 |
break; |
| 263 |
case 1: |
| 264 |
data = inbuf[0]; |
| 265 |
data ^= crc; |
| 266 |
data <<= 24; |
| 267 |
crc >>= 8; |
| 268 |
crc = crc32r_reduction_4(data, crc, consts); |
| 269 |
break; |
| 270 |
case 2: |
| 271 |
data = (u32)inbuf[0] ^ ((u32)inbuf[1] << 8); |
| 272 |
data ^= crc; |
| 273 |
data <<= 16; |
| 274 |
crc >>= 16; |
| 275 |
crc = crc32r_reduction_4(data, crc, consts); |
| 276 |
break; |
| 277 |
case 3: |
| 278 |
data = (u32)inbuf[0] ^ ((u32)inbuf[1] << 8) ^ ((u32)inbuf[2] << 16); |
| 279 |
data ^= crc; |
| 280 |
data <<= 8; |
| 281 |
crc >>= 24; |
| 282 |
crc = crc32r_reduction_4(data, crc, consts); |
| 283 |
break; |
| 284 |
} |
| 285 |
|
| 286 |
return crc; |
| 287 |
} |
| 288 |
|
| 289 |
static ASM_FUNC_ATTR_INLINE void |
| 290 |
do_crc32r (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 291 |
const struct crc32_consts_s *consts) |
| 292 |
{ |
| 293 |
u64 crc = *pcrc; |
| 294 |
u64 data; |
| 295 |
|
| 296 |
if ((uintptr_t)inbuf & 3) |
| 297 |
{ |
| 298 |
/* align input */ |
| 299 |
size_t unaligned_len = (-(uintptr_t)inbuf) & 3; |
| 300 |
|
| 301 |
unaligned_len = unaligned_len < inlen ? unaligned_len : inlen; |
| 302 |
crc = tail_crc32r(crc, inbuf, unaligned_len, consts); |
| 303 |
|
| 304 |
inbuf += unaligned_len; |
| 305 |
inlen -= unaligned_len; |
| 306 |
} |
| 307 |
|
| 308 |
while (inlen >= 4) |
| 309 |
{ |
| 310 |
data = load_aligned_u32(inbuf); |
| 311 |
data ^= crc; |
| 312 |
|
| 313 |
inlen -= 4; |
| 314 |
inbuf += 4; |
| 315 |
|
| 316 |
crc = crc32r_reduction_4(data, 0, consts); |
| 317 |
} |
| 318 |
|
| 319 |
*pcrc = tail_crc32r(crc, inbuf, inlen, consts); |
| 320 |
} |
| 321 |
|
| 322 |
void ASM_FUNC_ATTR |
| 323 |
_gcry_crc32_riscv_zbb_zbc (u32 *pcrc, const byte *inbuf, size_t inlen) |
| 324 |
{ |
| 325 |
const struct crc32_consts_s *consts = &crc32_consts; |
| 326 |
|
| 327 |
if (!inlen) |
| 328 |
return; |
| 329 |
|
| 330 |
if (inlen >= 16) |
| 331 |
{ |
| 332 |
size_t unaligned_len = (-(uintptr_t)inbuf) & 7; |
| 333 |
if (inlen >= 16 + unaligned_len) |
| 334 |
{ |
| 335 |
if (unaligned_len > 0) |
| 336 |
{ |
| 337 |
/* align input */ |
| 338 |
do_crc32r (pcrc, inbuf, unaligned_len, consts); |
| 339 |
inbuf += unaligned_len; |
| 340 |
inlen -= unaligned_len; |
| 341 |
} |
| 342 |
|
| 343 |
bulk_crc32r (pcrc, &inbuf, &inlen, consts); |
| 344 |
if (!inlen) |
| 345 |
return; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
do_crc32r (pcrc, inbuf, inlen, consts); |
| 350 |
} |
| 351 |
|
| 352 |
static ASM_FUNC_ATTR_INLINE u64 |
| 353 |
crc32_reduction_4 (u64 data, u64 crc, |
| 354 |
const struct crc32_consts_s *consts) |
| 355 |
{ |
| 356 |
u64 step1, step2; |
| 357 |
|
| 358 |
step1 = clmul_high((u64)data << 32, consts->my_p[0]); |
| 359 |
step2 = clmul_low(step1, consts->my_p[1]); |
| 360 |
|
| 361 |
return (byteswap_u64(step2) >> 32) ^ crc; |
| 362 |
} |
| 363 |
|
| 364 |
static ASM_FUNC_ATTR_INLINE void |
| 365 |
bulk_crc32 (u32 *pcrc, const byte **inbuf, size_t *inlen, |
| 366 |
const struct crc32_consts_s *consts) |
| 367 |
{ |
| 368 |
u64 crc = *pcrc; |
| 369 |
u64 k[2] = { consts->k[0], consts->k[1] }; |
| 370 |
u64x2 x0, x1, x2; |
| 371 |
u64 temp; |
| 372 |
|
| 373 |
x0 = load_aligned_u64x2(*inbuf); |
| 374 |
x0.lo ^= crc; |
| 375 |
x0 = byteswap_u64x2(x0); |
| 376 |
|
| 377 |
*inbuf += 16; |
| 378 |
*inlen -= 16; |
| 379 |
|
| 380 |
while (*inlen >= 16) |
| 381 |
{ |
| 382 |
x2 = load_aligned_u64x2(*inbuf); |
| 383 |
|
| 384 |
x1 = clmul_128(x0.hi, k[0]); |
| 385 |
x2 = byteswap_u64x2(x2); |
| 386 |
x0 = clmul_128(x0.lo, k[1]); |
| 387 |
|
| 388 |
x1 = xor_128(x1, x2); |
| 389 |
x0 = xor_128(x0, x1); |
| 390 |
|
| 391 |
*inbuf += 16; |
| 392 |
*inlen -= 16; |
| 393 |
} |
| 394 |
|
| 395 |
/* Reduce 128 bits to 96 bits */ |
| 396 |
x2 = clmul_128(x0.hi, k[1]); |
| 397 |
x2.hi ^= x0.lo; |
| 398 |
|
| 399 |
/* Reduce 96 bits to 64 bits */ |
| 400 |
crc = (x2.hi << 32) ^ (x2.lo >> 32); |
| 401 |
crc ^= clmul_high(and_u64(x2.hi, ~(u64)0xFFFFFFFFU), consts->k[2]); |
| 402 |
|
| 403 |
/* Reduce 64 bits to 32 bits */ |
| 404 |
temp = clmul_high(and_u64(crc, ~(u64)0xFFFFFFFFU), consts->my_p[0]); |
| 405 |
temp = clmul_low(temp, consts->my_p[1]); |
| 406 |
crc = temp ^ (crc & 0xFFFFFFFFU); |
| 407 |
|
| 408 |
crc = byteswap_u32(crc); |
| 409 |
|
| 410 |
*pcrc = crc; |
| 411 |
} |
| 412 |
|
| 413 |
static ASM_FUNC_ATTR_INLINE u64 |
| 414 |
tail_crc32 (u64 crc, const byte *inbuf, size_t inlen, |
| 415 |
const struct crc32_consts_s *consts) |
| 416 |
{ |
| 417 |
u64 data; |
| 418 |
|
| 419 |
switch (inlen) |
| 420 |
{ |
| 421 |
case 0: |
| 422 |
default: |
| 423 |
break; |
| 424 |
case 1: |
| 425 |
data = inbuf[0]; |
| 426 |
data ^= crc; |
| 427 |
data = data & 0xffU; |
| 428 |
crc = crc >> 8; |
| 429 |
crc = crc32_reduction_4(data, crc, consts); |
| 430 |
break; |
| 431 |
case 2: |
| 432 |
data = (u32)inbuf[0] ^ ((u32)inbuf[1] << 8); |
| 433 |
data ^= crc; |
| 434 |
data = byteswap_u32(data << 16); |
| 435 |
crc = crc >> 16; |
| 436 |
crc = crc32_reduction_4(data, crc, consts); |
| 437 |
break; |
| 438 |
case 3: |
| 439 |
data = (u32)inbuf[0] ^ ((u32)inbuf[1] << 8) ^ ((u32)inbuf[2] << 16); |
| 440 |
data ^= crc; |
| 441 |
data = byteswap_u32(data << 8); |
| 442 |
crc = crc >> 24; |
| 443 |
crc = crc32_reduction_4(data, crc, consts); |
| 444 |
break; |
| 445 |
} |
| 446 |
|
| 447 |
return crc; |
| 448 |
} |
| 449 |
|
| 450 |
static ASM_FUNC_ATTR_INLINE void |
| 451 |
do_crc32 (u32 *pcrc, const byte *inbuf, size_t inlen, |
| 452 |
const struct crc32_consts_s *consts) |
| 453 |
{ |
| 454 |
u64 crc = *pcrc; |
| 455 |
u64 data; |
| 456 |
|
| 457 |
if ((uintptr_t)inbuf & 3) |
| 458 |
{ |
| 459 |
/* align input */ |
| 460 |
size_t unaligned_len = (-(uintptr_t)inbuf) & 3; |
| 461 |
|
| 462 |
unaligned_len = unaligned_len < inlen ? unaligned_len : inlen; |
| 463 |
crc = tail_crc32(crc, inbuf, unaligned_len, consts); |
| 464 |
|
| 465 |
inbuf += unaligned_len; |
| 466 |
inlen -= unaligned_len; |
| 467 |
} |
| 468 |
|
| 469 |
while (inlen >= 4) |
| 470 |
{ |
| 471 |
data = load_aligned_u32(inbuf); |
| 472 |
data ^= crc; |
| 473 |
data = byteswap_u32(data); |
| 474 |
|
| 475 |
inlen -= 4; |
| 476 |
inbuf += 4; |
| 477 |
|
| 478 |
crc = crc32_reduction_4(data, 0, consts); |
| 479 |
} |
| 480 |
|
| 481 |
*pcrc = tail_crc32(crc, inbuf, inlen, consts); |
| 482 |
} |
| 483 |
|
| 484 |
void ASM_FUNC_ATTR |
| 485 |
_gcry_crc24rfc2440_riscv_zbb_zbc (u32 *pcrc, const byte *inbuf, size_t inlen) |
| 486 |
{ |
| 487 |
const struct crc32_consts_s *consts = &crc24rfc2440_consts; |
| 488 |
|
| 489 |
if (!inlen) |
| 490 |
return; |
| 491 |
|
| 492 |
if (inlen >= 16) |
| 493 |
{ |
| 494 |
size_t unaligned_len = (-(uintptr_t)inbuf) & 7; |
| 495 |
if (inlen >= 16 + unaligned_len) |
| 496 |
{ |
| 497 |
if (unaligned_len > 0) |
| 498 |
{ |
| 499 |
/* align input */ |
| 500 |
do_crc32 (pcrc, inbuf, unaligned_len, consts); |
| 501 |
inbuf += unaligned_len; |
| 502 |
inlen -= unaligned_len; |
| 503 |
} |
| 504 |
|
| 505 |
bulk_crc32 (pcrc, &inbuf, &inlen, consts); |
| 506 |
if (!inlen) |
| 507 |
return; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
do_crc32 (pcrc, inbuf, inlen, consts); |
| 512 |
} |
| 513 |
|
| 514 |
#endif |