| 1 |
/* sha256-riscv-zvknha-zvkb.c - RISC-V vector crypto implementation of SHA-256 |
| 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 |
|
| 26 |
#include "g10lib.h" |
| 27 |
#include "simd-common-riscv.h" |
| 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 |
static ASM_FUNC_ATTR_INLINE vuint32m1_t |
| 41 |
working_vsha2cl_vv_u32m1(vuint32m1_t hgcd, vuint32m1_t feba, |
| 42 |
vuint32m1_t kw, size_t vl) |
| 43 |
{ |
| 44 |
#ifdef HAVE_BROKEN_VSHA2CL_INTRINSIC |
| 45 |
asm ( |
| 46 |
"vsetvli zero,%3,e32,m1,ta,ma;\n\t" |
| 47 |
"vsha2cl.vv %0,%1,%2;\n\t" |
| 48 |
: "+vr" (hgcd) |
| 49 |
: "vr" (feba), "vr" (kw), "r" (vl) |
| 50 |
: "vl", "vtype" |
| 51 |
); |
| 52 |
return hgcd; |
| 53 |
#else |
| 54 |
return __riscv_vsha2cl_vv_u32m1(hgcd, feba, kw, vl); |
| 55 |
#endif |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/* Quad-round with message expansion (rounds 0-47) */ |
| 60 |
#define QUAD_ROUND_W_SCHED(w0, w1, w2, w3) \ |
| 61 |
v_k = __riscv_vle32_v_u32m1(k, vl); \ |
| 62 |
k += 4; \ |
| 63 |
v_kw = __riscv_vadd_vv_u32m1(v_k, w0, vl); \ |
| 64 |
v_hgcd_work = working_vsha2cl_vv_u32m1(v_hgcd_work, v_feba_work, v_kw, vl); \ |
| 65 |
v_feba_work = __riscv_vsha2ch_vv_u32m1(v_feba_work, v_hgcd_work, v_kw, vl); \ |
| 66 |
v_w_merged = __riscv_vmerge_vvm_u32m1(w2, w1, merge_mask, vl); \ |
| 67 |
w0 = __riscv_vsha2ms_vv_u32m1(w0, v_w_merged, w3, vl); |
| 68 |
|
| 69 |
/* Quad-round without message expansion (rounds 48-63) */ |
| 70 |
#define QUAD_ROUND_NO_SCHED(w0) \ |
| 71 |
v_k = __riscv_vle32_v_u32m1(k, vl); \ |
| 72 |
k += 4; \ |
| 73 |
v_kw = __riscv_vadd_vv_u32m1(v_k, w0, vl); \ |
| 74 |
v_hgcd_work = working_vsha2cl_vv_u32m1(v_hgcd_work, v_feba_work, v_kw, vl); \ |
| 75 |
v_feba_work = __riscv_vsha2ch_vv_u32m1(v_feba_work, v_hgcd_work, v_kw, vl); |
| 76 |
|
| 77 |
|
| 78 |
static ASM_FUNC_ATTR_INLINE vuint32m1_t |
| 79 |
load_and_swap (const byte * p, size_t vl, size_t vl_bytes) |
| 80 |
{ |
| 81 |
vuint8m1_t temp_bytes = __riscv_vle8_v_u8m1(p, vl_bytes); |
| 82 |
return __riscv_vrev8_v_u32m1(__riscv_vreinterpret_v_u8m1_u32m1(temp_bytes), |
| 83 |
vl); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
static ASM_FUNC_ATTR_INLINE void |
| 88 |
sha256_transform_zvknha_zvkb (u32 state[8], const uint8_t * data, |
| 89 |
size_t nblocks) |
| 90 |
{ |
| 91 |
static const u32 k_const[64] = |
| 92 |
{ |
| 93 |
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| 94 |
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 95 |
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 96 |
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 97 |
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| 98 |
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 99 |
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 100 |
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 101 |
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 102 |
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 103 |
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| 104 |
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 105 |
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| 106 |
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 107 |
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 108 |
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 109 |
}; |
| 110 |
static const u32 feba_hgcd_indices[4] = { 20, 16, 4, 0 }; |
| 111 |
static const int feba_offset = 0; |
| 112 |
static const int hgcd_offset = 8 / sizeof(u32); |
| 113 |
size_t vl; |
| 114 |
size_t vl_bytes; |
| 115 |
vuint32m1_t idx; |
| 116 |
vuint32m1_t v_feba_work, v_feba; |
| 117 |
vuint32m1_t v_hgcd_work, v_hgcd; |
| 118 |
vuint32m1_t w0, w1, w2, w3; |
| 119 |
vuint32m1_t v_k, v_kw, v_w_merged; |
| 120 |
vbool32_t merge_mask; |
| 121 |
vuint32m1_t v_feba_hgcd_idx; |
| 122 |
|
| 123 |
vl = 4; |
| 124 |
vl_bytes = vl * 4; |
| 125 |
idx = __riscv_vid_v_u32m1(vl); |
| 126 |
merge_mask = __riscv_vmseq_vx_u32m1_b32(idx, 0, vl); |
| 127 |
|
| 128 |
v_feba_hgcd_idx = __riscv_vle32_v_u32m1(feba_hgcd_indices, vl); |
| 129 |
|
| 130 |
v_feba = __riscv_vluxei32_v_u32m1(state + feba_offset, v_feba_hgcd_idx, vl); |
| 131 |
v_hgcd = __riscv_vluxei32_v_u32m1(state + hgcd_offset, v_feba_hgcd_idx, vl); |
| 132 |
|
| 133 |
while (nblocks > 0) |
| 134 |
{ |
| 135 |
const u32 *k = k_const; |
| 136 |
|
| 137 |
v_feba_work = v_feba; |
| 138 |
v_hgcd_work = v_hgcd; |
| 139 |
|
| 140 |
w0 = load_and_swap(data + 0, vl, vl_bytes); |
| 141 |
w1 = load_and_swap(data + 16, vl, vl_bytes); |
| 142 |
w2 = load_and_swap(data + 32, vl, vl_bytes); |
| 143 |
w3 = load_and_swap(data + 48, vl, vl_bytes); |
| 144 |
|
| 145 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 146 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 147 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 148 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 149 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 150 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 151 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 152 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 153 |
QUAD_ROUND_W_SCHED(w0, w1, w2, w3); |
| 154 |
QUAD_ROUND_W_SCHED(w1, w2, w3, w0); |
| 155 |
QUAD_ROUND_W_SCHED(w2, w3, w0, w1); |
| 156 |
QUAD_ROUND_W_SCHED(w3, w0, w1, w2); |
| 157 |
QUAD_ROUND_NO_SCHED(w0); |
| 158 |
QUAD_ROUND_NO_SCHED(w1); |
| 159 |
QUAD_ROUND_NO_SCHED(w2); |
| 160 |
QUAD_ROUND_NO_SCHED(w3); |
| 161 |
|
| 162 |
v_feba = __riscv_vadd_vv_u32m1(v_feba, v_feba_work, vl); |
| 163 |
v_hgcd = __riscv_vadd_vv_u32m1(v_hgcd, v_hgcd_work, vl); |
| 164 |
|
| 165 |
data += 64; |
| 166 |
nblocks--; |
| 167 |
} |
| 168 |
|
| 169 |
__riscv_vsuxei32_v_u32m1(state + feba_offset, v_feba_hgcd_idx, v_feba, vl); |
| 170 |
__riscv_vsuxei32_v_u32m1(state + hgcd_offset, v_feba_hgcd_idx, v_hgcd, vl); |
| 171 |
|
| 172 |
clear_vec_regs(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
#ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE |
| 177 |
# define FUNC_ATTR_OPT_O2 __attribute__((optimize("-O2"))) |
| 178 |
#else |
| 179 |
# define FUNC_ATTR_OPT_O2 |
| 180 |
#endif |
| 181 |
|
| 182 |
unsigned int ASM_FUNC_ATTR FUNC_ATTR_OPT_O2 |
| 183 |
_gcry_sha256_transform_riscv_zvknha_zvkb(u32 state[8], |
| 184 |
const unsigned char *input_data, |
| 185 |
size_t num_blks) |
| 186 |
{ |
| 187 |
sha256_transform_zvknha_zvkb(state, input_data, num_blks); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
unsigned int ASM_FUNC_ATTR FUNC_ATTR_OPT_O2 |
| 192 |
_gcry_sha256_riscv_v_check_hw(void) |
| 193 |
{ |
| 194 |
return (__riscv_vsetvl_e32m1(4) == 4); |
| 195 |
} |
| 196 |
|
| 197 |
#endif /* HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS */ |