Commit e94d1a2fe0 for qemu.org
commit e94d1a2fe0d9517c90a3903de9ec909e167b4aff
Author: James Hilliard <james.hilliard1@gmail.com>
Date: Mon Jun 8 12:59:34 2026 -0600
target/mips: add Octeon SMS4 COP2 helpers
Add helper support for the Octeon SMS4 operation selectors. SMS4 reuses
the AES RESINP, IV, and key banks, so the helpers share the existing AES
state while implementing the SMS4 ECB/CBC encrypt and decrypt operations.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Message-ID: <20260608-mips-octeon-missing-insns-v2-v16-9-daef7a0d8b04@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
diff --git a/target/mips/helper.h b/target/mips/helper.h
index 39c226ded9..4949b94657 100644
--- a/target/mips/helper.h
+++ b/target/mips/helper.h
@@ -56,6 +56,10 @@ DEF_HELPER_2(octeon_cp2_mt_aes_enc_cbc1, void, env, i64)
DEF_HELPER_2(octeon_cp2_mt_aes_enc1, void, env, i64)
DEF_HELPER_2(octeon_cp2_mt_aes_dec_cbc1, void, env, i64)
DEF_HELPER_2(octeon_cp2_mt_aes_dec1, void, env, i64)
+DEF_HELPER_2(octeon_cp2_mt_sms4_enc_cbc1, void, env, i64)
+DEF_HELPER_2(octeon_cp2_mt_sms4_enc1, void, env, i64)
+DEF_HELPER_2(octeon_cp2_mt_sms4_dec_cbc1, void, env, i64)
+DEF_HELPER_2(octeon_cp2_mt_sms4_dec1, void, env, i64)
/* microMIPS functions */
DEF_HELPER_4(lwm, void, env, tl, tl, i32)
diff --git a/target/mips/tcg/octeon_crypto.c b/target/mips/tcg/octeon_crypto.c
index 117e3a5026..bcffd17ab6 100644
--- a/target/mips/tcg/octeon_crypto.c
+++ b/target/mips/tcg/octeon_crypto.c
@@ -985,6 +985,129 @@ void helper_octeon_cp2_mt_aes_dec1(CPUMIPSState *env, uint64_t value)
octeon_aes_decrypt_common(crypto, false);
}
+static uint32_t octeon_sms4_t(uint32_t x)
+{
+ x = sm4_subword(x);
+ return x ^ rol32(x, 2) ^ rol32(x, 10) ^ rol32(x, 18) ^ rol32(x, 24);
+}
+
+static uint32_t octeon_sms4_t_key(uint32_t x)
+{
+ x = sm4_subword(x);
+ return x ^ rol32(x, 13) ^ rol32(x, 23);
+}
+
+static void octeon_sms4_expand_key(const uint8_t *key, uint32_t round_keys[32])
+{
+ static const uint32_t fk[4] = {
+ 0xa3b1bac6U, 0x56aa3350U, 0x677d9197U, 0xb27022dcU,
+ };
+ uint32_t k[36];
+
+ for (int i = 0; i < 4; i++) {
+ k[i] = ldl_be_p(key + i * 4) ^ fk[i];
+ }
+ for (int i = 0; i < 32; i++) {
+ k[i + 4] = k[i] ^ octeon_sms4_t_key(k[i + 1] ^ k[i + 2] ^
+ k[i + 3] ^ sm4_ck[i]);
+ round_keys[i] = k[i + 4];
+ }
+}
+
+static void octeon_sms4_crypt_block(const uint8_t *in, uint8_t *out,
+ const uint32_t round_keys[32],
+ bool encrypt)
+{
+ uint32_t x[36];
+
+ for (int i = 0; i < 4; i++) {
+ x[i] = ldl_be_p(in + i * 4);
+ }
+ for (int i = 0; i < 32; i++) {
+ uint32_t rk = round_keys[encrypt ? i : 31 - i];
+
+ x[i + 4] = x[i] ^ octeon_sms4_t(x[i + 1] ^ x[i + 2] ^
+ x[i + 3] ^ rk);
+ }
+ stl_be_p(out, x[35]);
+ stl_be_p(out + 4, x[34]);
+ stl_be_p(out + 8, x[33]);
+ stl_be_p(out + 12, x[32]);
+}
+
+static void octeon_sms4_crypt_common(MIPSOcteonCryptoState *crypto,
+ bool encrypt, bool cbc)
+{
+ uint8_t key[16];
+ uint8_t in[16];
+ uint8_t out[16];
+ uint8_t iv[16];
+ uint8_t next_iv[16];
+ uint32_t round_keys[32];
+
+ /*
+ * SMS4 aliases the AES state onto the RESINP, IV, and KEY banks,
+ * with only the operation selectors remaining distinct.
+ */
+ octeon_aes_load_key(crypto, key, sizeof(key));
+ octeon_aes_load_block(crypto->aes_resinp, in);
+ if (cbc) {
+ octeon_aes_load_block(crypto->aes_iv, iv);
+ if (encrypt) {
+ for (int i = 0; i < sizeof(in); i++) {
+ in[i] ^= iv[i];
+ }
+ } else {
+ memcpy(next_iv, in, sizeof(next_iv));
+ }
+ }
+
+ octeon_sms4_expand_key(key, round_keys);
+ octeon_sms4_crypt_block(in, out, round_keys, encrypt);
+ if (cbc && !encrypt) {
+ for (int i = 0; i < sizeof(out); i++) {
+ out[i] ^= iv[i];
+ }
+ }
+
+ octeon_aes_store_block(crypto->aes_resinp, out);
+ if (cbc) {
+ octeon_aes_store_block(crypto->aes_iv, encrypt ? out : next_iv);
+ }
+}
+
+void helper_octeon_cp2_mt_sms4_enc_cbc1(CPUMIPSState *env, uint64_t value)
+{
+ MIPSOcteonCryptoState *crypto = &env->octeon_crypto;
+
+ crypto->aes_resinp[1] = value;
+ octeon_sms4_crypt_common(crypto, true, true);
+}
+
+void helper_octeon_cp2_mt_sms4_enc1(CPUMIPSState *env, uint64_t value)
+{
+ MIPSOcteonCryptoState *crypto = &env->octeon_crypto;
+
+ crypto->aes_resinp[1] = value;
+ octeon_sms4_crypt_common(crypto, true, false);
+}
+
+void helper_octeon_cp2_mt_sms4_dec_cbc1(CPUMIPSState *env, uint64_t value)
+{
+ MIPSOcteonCryptoState *crypto = &env->octeon_crypto;
+
+ crypto->aes_resinp[1] = value;
+ octeon_sms4_crypt_common(crypto, false, true);
+}
+
+void helper_octeon_cp2_mt_sms4_dec1(CPUMIPSState *env, uint64_t value)
+{
+ MIPSOcteonCryptoState *crypto = &env->octeon_crypto;
+
+ crypto->aes_resinp[1] = value;
+ octeon_sms4_crypt_common(crypto, false, false);
+}
+
void helper_octeon_cp2_mt_snow3g_start(CPUMIPSState *env, uint64_t value)
{
octeon_snow3g_start(&env->octeon_crypto, value);