summaryrefslogtreecommitdiffstats
path: root/target/riscv/crypto_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'target/riscv/crypto_helper.c')
-rw-r--r--target/riscv/crypto_helper.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/target/riscv/crypto_helper.c b/target/riscv/crypto_helper.c
index cb4783a1e9..2ef30281b1 100644
--- a/target/riscv/crypto_helper.c
+++ b/target/riscv/crypto_helper.c
@@ -271,4 +271,32 @@ target_ulong HELPER(aes64im)(target_ulong rs1)
return result;
}
+
+target_ulong HELPER(sm4ed)(target_ulong rs1, target_ulong rs2,
+ target_ulong shamt)
+{
+ uint32_t sb_in = (uint8_t)(rs2 >> shamt);
+ uint32_t sb_out = (uint32_t)sm4_sbox[sb_in];
+
+ uint32_t x = sb_out ^ (sb_out << 8) ^ (sb_out << 2) ^ (sb_out << 18) ^
+ ((sb_out & 0x3f) << 26) ^ ((sb_out & 0xC0) << 10);
+
+ uint32_t rotl = rol32(x, shamt);
+
+ return sext32_xlen(rotl ^ (uint32_t)rs1);
+}
+
+target_ulong HELPER(sm4ks)(target_ulong rs1, target_ulong rs2,
+ target_ulong shamt)
+{
+ uint32_t sb_in = (uint8_t)(rs2 >> shamt);
+ uint32_t sb_out = sm4_sbox[sb_in];
+
+ uint32_t x = sb_out ^ ((sb_out & 0x07) << 29) ^ ((sb_out & 0xFE) << 7) ^
+ ((sb_out & 0x01) << 23) ^ ((sb_out & 0xF8) << 13);
+
+ uint32_t rotl = rol32(x, shamt);
+
+ return sext32_xlen(rotl ^ (uint32_t)rs1);
+}
#undef sext32_xlen