| 1 |
/* sha512-riscv-zvknhb-zvkb.c - RISC-V vector crypto implementation of SHA-512 |
| 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 |
defined(USE_SHA512) |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
#include "simd-common-riscv.h" |
| 29 |
#include <riscv_vector.h> |
| 30 |
|
| 31 |
|
| 32 |
#define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 33 |
#define NO_INLINE __attribute__((noinline)) |
| 34 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 35 |
|
| 36 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 37 |
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE |
| 38 |
#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE |
| 39 |
|
| 40 |
|
| 41 |
static ASM_FUNC_ATTR_INLINE vuint64m2_t |
| 42 |
working_vsha2cl_vv_u64m2(vuint64m2_t hgcd, vuint64m2_t feba, |
| 43 |
vuint64m2_t kw, size_t vl) |
| 44 |
{ |
| 45 |
#ifdef HAVE_BROKEN_VSHA2CL_INTRINSIC |
| 46 |
asm ( |
| 47 |
"vsetvli zero,%3,e64,m2,ta,ma;\n\t" |
| 48 |
"vsha2cl.vv %0,%1,%2;\n\t" |
| 49 |
: "+vr" (hgcd) |
| 50 |
: "vr" (feba), "vr" (kw), "r" (vl) |
| 51 |
: "vl", "vtype" |
| 52 |
); |
| 53 |
return hgcd; |
| 54 |
#else |
| 55 |
return __riscv_vsha2cl_vv_u64m2(hgcd, feba, kw, vl); |
| 56 |
#endif |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
/* Quad-round with message expansion (rounds 0-63) */ |
| 61 |
#define QUAD_ROUND_W_SCHED(w0, w1, w2, w3) \ |
| 62 |
k_tmp = k; \ |
| 63 |
asm ("" : "+r" (k_tmp) :: "memory"); \ |
| 64 |
v_k = __riscv_vle64_v_u64m2(k_tmp, vl); \ |
| 65 |
k += 4; \ |
| 66 |
v_kw = __riscv_vadd_vv_u64m2(v_k, w0, vl); \ |
| 67 |
v_hgcd_work = working_vsha2cl_vv_u64m2(v_hgcd_work, v_feba_work, v_kw, vl); \ |
| 68 |
v_feba_work = __riscv_vsha2ch_vv_u64m2(v_feba_work, v_hgcd_work, v_kw, vl); \ |
| 69 |
v_w_merged = __riscv_vmerge_vvm_u64m2(w2, w1, merge_mask, vl); \ |
| 70 |
w0 = __riscv_vsha2ms_vv_u64m2(w0, v_w_merged, w3, vl); |
| 71 |
|
| 72 |
/* Quad-round without message expansion (rounds 64-79) */ |
| 73 |
#define QUAD_ROUND_NO_SCHED(w0) \ |
| 74 |
k_tmp = k; \ |
| 75 |
asm ("" : "+r" (k_tmp) :: "memory"); \ |
| 76 |
v_k = __riscv_vle64_v_u64m2(k_tmp, vl); \ |
| 77 |
k += 4; \ |
| 78 |
v_kw = __riscv_vadd_vv_u64m2(v_k, w0, vl); \ |
| 79 |
v_hgcd_work = working_vsha2cl_vv_u64m2(v_hgcd_work, v_feba_work, v_kw, vl); \ |
| 80 |
v_feba_work = __riscv_vsha2ch_vv_u64m2(v_feba_work, v_hgcd_work, v_kw, vl); |
| 81 |
|
| 82 |
|
| 83 |
static ASM_FUNC_ATTR_INLINE vuint64m2_t |
| 84 |
load_and_swap(const byte *p, size_t vl, size_t vl_bytes) |
| 85 |
{ |
| 86 |
vuint8m2_t temp_bytes = __riscv_vle8_v_u8m2(p, vl_bytes); |
| 87 |
return __riscv_vrev8_v_u64m2(__riscv_vreinterpret_v_u8m2_u64m2(temp_bytes), |
| 88 |
vl); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static ASM_FUNC_ATTR_INLINE void |
| 93 |
sha512_transform_zvknhb_zvkb (u64 state[8], const byte *data, |
| 94 |
size_t nblocks, const u64 k_const[80]) |
| 95 |
{ |
| 96 |
static const u64 feba_hgcd_indices[4] = { 40, 32, 8, 0 }; |
| 97 |
static const int feba_offset = 0; |
| 98 |
static const int hgcd_offset = 16 / sizeof(u64); |
| 99 |
size_t vl; |
| 100 |
size_t vl_bytes; |
| 101 |
vuint64m2_t idx; |
| 102 |
vuint64m2_t v_feba_work, v_feba; |
| 103 |
vuint64m2_t v_hgcd_work, v_hgcd; |
| 104 |
vuint64m2_t w0, w1, w2, w3; |
| 105 |
vuint64m2_t v_k, v_kw, v_w_merged; |
| 106 |
vbool32_t merge_mask; |
| 107 |
vuint64m2_t v_feba_hgcd_idx; |
| 108 |
|
| 109 |
vl = 4; |
| 110 |
vl_bytes = vl * 8; |
| 111 |
idx = __riscv_vid_v_u64m2(vl); |
| 112 |
merge_mask = __riscv_vmseq_vx_u64m2_b32(idx, 0, vl); |
| 113 |
|
| 114 |
v_feba_hgcd_idx = __riscv_vle64_v_u64m2(feba_hgcd_indices, vl); |
| 115 |
|
| 116 |
v_feba = __riscv_vluxei64_v_u64m2(state + feba_offset, v_feba_hgcd_idx, vl); |
| 117 |
v_hgcd = __riscv_vluxei64_v_u64m2(state + hgcd_offset, v_feba_hgcd_idx, vl); |
| 118 |
|
| 119 |
while (nblocks > 0) |
| 120 |
{ |
| 121 |
const u64 *k = k_const; |
| 122 |
const u64 *k_tmp; |
| 123 |
|
| 124 |
v_feba_work = v_feba; |
| 125 |
v_hgcd_work = v_hgcd; |
| 126 |
|
| 127 |
w0 = load_and_swap(data + 0, vl, vl_bytes); |
| 128 |
w1 = load_and_swap(data + 32, vl, vl_bytes); |
| 129 |
w2 = load_and_swap(data + 64, vl, vl_bytes); |
| 130 |
w3 = load_and_swap(data + 96, vl, vl_bytes); |
| 131 |
|
| 132 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 133 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 134 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 135 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 136 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 137 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 138 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 139 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 140 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 141 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 142 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 143 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 144 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 145 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 146 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 147 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 148 |
|
| 149 |
QUAD_ROUND_NO_SCHED(w0); |
| 150 |
QUAD_ROUND_NO_SCHED(w1); |
| 151 |
QUAD_ROUND_NO_SCHED(w2); |
| 152 |
QUAD_ROUND_NO_SCHED(w3); |
| 153 |
|
| 154 |
v_feba = __riscv_vadd_vv_u64m2(v_feba, v_feba_work, vl); |
| 155 |
v_hgcd = __riscv_vadd_vv_u64m2(v_hgcd, v_hgcd_work, vl); |
| 156 |
|
| 157 |
data += 128; |
| 158 |
nblocks--; |
| 159 |
} |
| 160 |
|
| 161 |
__riscv_vsuxei64_v_u64m2(state + feba_offset, v_feba_hgcd_idx, v_feba, vl); |
| 162 |
__riscv_vsuxei64_v_u64m2(state + hgcd_offset, v_feba_hgcd_idx, v_hgcd, vl); |
| 163 |
|
| 164 |
clear_vec_regs(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
#ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE |
| 169 |
# define FUNC_ATTR_OPT_O2 __attribute__((optimize("-O2"))) |
| 170 |
#else |
| 171 |
# define FUNC_ATTR_OPT_O2 |
| 172 |
#endif |
| 173 |
|
| 174 |
unsigned int ASM_FUNC_ATTR FUNC_ATTR_OPT_O2 |
| 175 |
_gcry_sha512_transform_riscv_zvknhb_zvkb(u64 state[8], |
| 176 |
const unsigned char *input_data, |
| 177 |
size_t num_blks, |
| 178 |
const u64 k[80]) |
| 179 |
{ |
| 180 |
sha512_transform_zvknhb_zvkb(state, input_data, num_blks, k); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
unsigned int ASM_FUNC_ATTR FUNC_ATTR_OPT_O2 |
| 185 |
_gcry_sha512_riscv_v_check_hw(void) |
| 186 |
{ |
| 187 |
return (__riscv_vsetvl_e64m2(4) == 4); |
| 188 |
} |
| 189 |
|
| 190 |
#endif /* HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS */ |