| 1 |
/* cipher-gcm-riscv-zvkg.c - RISC-V vector cryptography Zvkg accelerated GHASH |
| 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 |
#include "g10lib.h" |
| 23 |
#include "simd-common-riscv.h" |
| 24 |
#include "cipher-internal.h" |
| 25 |
|
| 26 |
#ifdef GCM_USE_RISCV_ZVKG |
| 27 |
|
| 28 |
#include <riscv_vector.h> |
| 29 |
|
| 30 |
|
| 31 |
#define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 32 |
#define NO_INLINE __attribute__((noinline)) |
| 33 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 34 |
|
| 35 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 36 |
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE |
| 37 |
#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE |
| 38 |
|
| 39 |
|
| 40 |
#define cast_u8m1_u32m1(a) __riscv_vreinterpret_v_u8m1_u32m1(a) |
| 41 |
#define cast_u32m1_u8m1(a) __riscv_vreinterpret_v_u32m1_u8m1(a) |
| 42 |
|
| 43 |
|
| 44 |
static ASM_FUNC_ATTR_INLINE vuint32m1_t |
| 45 |
unaligned_load_u32m1(const void *ptr, size_t vl_u32) |
| 46 |
{ |
| 47 |
size_t vl_bytes = vl_u32 * 4; |
| 48 |
|
| 49 |
return cast_u8m1_u32m1(__riscv_vle8_v_u8m1(ptr, vl_bytes)); |
| 50 |
} |
| 51 |
|
| 52 |
static ASM_FUNC_ATTR_INLINE vuint32m1_t |
| 53 |
bswap128_u32m1(vuint32m1_t vec, size_t vl_u32) |
| 54 |
{ |
| 55 |
static const byte bswap128_arr[16] = |
| 56 |
{ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; |
| 57 |
size_t vl_bytes = vl_u32 * 4; |
| 58 |
vuint8m1_t bswap128 = __riscv_vle8_v_u8m1(bswap128_arr, vl_bytes); |
| 59 |
|
| 60 |
return cast_u8m1_u32m1( |
| 61 |
__riscv_vrgather_vv_u8m1(cast_u32m1_u8m1(vec), bswap128, vl_bytes)); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
ASM_FUNC_ATTR_NOINLINE int |
| 66 |
_gcry_ghash_setup_riscv_zvkg(gcry_cipher_hd_t c) |
| 67 |
{ |
| 68 |
(void)c; |
| 69 |
|
| 70 |
if (__riscv_vsetvl_e32m1(4) != 4) |
| 71 |
{ |
| 72 |
return 0; // VLEN=128 not supported. |
| 73 |
} |
| 74 |
|
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
ASM_FUNC_ATTR_NOINLINE unsigned int |
| 79 |
_gcry_ghash_riscv_zvkg(gcry_cipher_hd_t c, byte *result, const byte *buf, |
| 80 |
size_t nblocks) |
| 81 |
{ |
| 82 |
u32 *result_u32 = (void *)result; |
| 83 |
const u32 *key_u32 = (void *)c->u_mode.gcm.u_ghash_key.key; |
| 84 |
size_t vl = 4; |
| 85 |
vuint32m1_t rhash = __riscv_vle32_v_u32m1(result_u32, vl); |
| 86 |
vuint32m1_t rh1 = __riscv_vle32_v_u32m1(key_u32, vl); |
| 87 |
|
| 88 |
while (nblocks) |
| 89 |
{ |
| 90 |
vuint32m1_t data = unaligned_load_u32m1(buf, vl); |
| 91 |
buf += 16; |
| 92 |
nblocks--; |
| 93 |
|
| 94 |
rhash = __riscv_vghsh_vv_u32m1(rhash, rh1, data, vl); |
| 95 |
} |
| 96 |
|
| 97 |
__riscv_vse32_v_u32m1(result_u32, rhash, vl); |
| 98 |
|
| 99 |
clear_vec_regs(); |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
ASM_FUNC_ATTR_NOINLINE unsigned int |
| 105 |
_gcry_polyval_riscv_zvkg(gcry_cipher_hd_t c, byte *result, const byte *buf, |
| 106 |
size_t nblocks) |
| 107 |
{ |
| 108 |
u32 *result_u32 = (void *)result; |
| 109 |
const u32 *key_u32 = (void *)c->u_mode.gcm.u_ghash_key.key; |
| 110 |
size_t vl = 4; |
| 111 |
vuint32m1_t rhash = __riscv_vle32_v_u32m1(result_u32, vl); |
| 112 |
vuint32m1_t rh1 = __riscv_vle32_v_u32m1(key_u32, vl); |
| 113 |
|
| 114 |
while (nblocks) |
| 115 |
{ |
| 116 |
vuint32m1_t data = bswap128_u32m1(unaligned_load_u32m1(buf, vl), vl); |
| 117 |
buf += 16; |
| 118 |
nblocks--; |
| 119 |
|
| 120 |
rhash = __riscv_vghsh_vv_u32m1(rhash, rh1, data, vl); |
| 121 |
} |
| 122 |
|
| 123 |
__riscv_vse32_v_u32m1(result_u32, rhash, vl); |
| 124 |
|
| 125 |
clear_vec_regs(); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
#endif /* GCM_USE_RISCV_V_ZVKG */ |