Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 65E54CD37AF for ; Sun, 10 May 2026 02:15:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wLtgd-0006Hc-5g; Sat, 09 May 2026 22:14:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wLtgZ-0006EG-HG for qemu-devel@nongnu.org; Sat, 09 May 2026 22:14:27 -0400 Received: from mail-oo1-xc34.google.com ([2607:f8b0:4864:20::c34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wLtgX-00033d-GP for qemu-devel@nongnu.org; Sat, 09 May 2026 22:14:27 -0400 Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-694891f8f62so1745034eaf.2 for ; Sat, 09 May 2026 19:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778379263; x=1778984063; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=HOOICPKFuPOobKQgEqvG0aU3Gd8FTKKOy15LPTtnr5w=; b=cHtgAgSpEcRn0MB4zw2wATULgX7LBgysw9phrTxcEE6Yze4Od04KR2M1W7rbbiTftc C0h0b1F7dazS4t0/gXUADIa6ACtXc9ypRAO5vWb/WPHC+OWxk/v5U2XmtLHCfI7ZZLuj sRqpYptNUeB3Z5VPToD0GWNGu7cNXSn73gBEHqh62xzvmYmc58AlPbCh0KA6bf/p+ZE7 ze4iWow2lYvCQwAH+CjGMA4X7ljOpNV3I1H7y5Al3lJRZw3fYwvTENU8uwiXFU8QEXkI ujPDUmIoGqe8wxOoLoAZ4SIjnFoonJGEXc0zPZ0Sf0v41xFrBM0AMqBT0Jv/FkG0F4gC V9wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778379263; x=1778984063; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=HOOICPKFuPOobKQgEqvG0aU3Gd8FTKKOy15LPTtnr5w=; b=HG7xV13tgmyGDcjg7pypBE4ydyRWI4/S5q0cYyrbdh8vyHmtPuvqKVzZTO651m9Ou+ cH/Sw5doR5PXbiIQAbEB0qmEtRAUAF1acXfurRasEPuPMS2IAmouclCzo0gEg6xQ3dkE npF3CFujPFmRlZoP3G+ppV49Pl0xLQtA8r+cF6lXq9Z/Y1WLtHbw5b+jI6r3vaD37ken nTWElqUlScEKIC6ZwbZKDoC73DyZH0eHooDdQ/ZyZ9RjvQ52nbtkVfDJBBiOgD575dCQ jsTTVw6B0CFWswJ7BEEq+uUHaQWKpAAPb9Yq0N6jA+SSPl6tnuDfD4hEMzkdIe+RlDAK sT0g== X-Gm-Message-State: AOJu0YxUTuL5j+BuAWeEFmWwuenq960WC8ZDl8MDtC9L4TRw0U3mmqgO a3E9qW9FnA6ddwFjboZSed6kyDYG91yTmhYa6a6o2qd24rkKtJp92haMEAcDcCrJ X-Gm-Gg: Acq92OE42dnwpG4hxrsciDteglZm6XG8CrJd5L+0SPhK2sr0qrhCkVGKSDqGExWb16x qkd9rXDzwBe4EIiM8xJIM5DL/M8JzMp5JipOeE0YaIfgbPjk2op7YqmAwYyFhFcxSUdLDkUgxP0 kc7npOnZcVEtwduYRtm3LLUBK/iOE2qUwg2RFUgqenE+sArz6I4zU1+tJAWme9fs35rg3QGKx/7 acbSqQotHIGQ4zbSsFotaLIKTI8EnVj1/c6A2OdFD4iX2DTMYYcoEyonYJ0PHlUonMgUpW0V69B N/RshdknVFrmT+32UuytDhCAQd8noWzt0LAqYx2P+vP9o1Cv0zocWORxbfkWkH5ARNG+I83l5ma rPiOJyJNPRA0yYyp2wRn49Uy8Ad0TEPrhVMlHXJYNZC1hzjfinDvB6IjSTLa1lzy9bW7f5W0n5p IksJb1oUVooQkaaeylP8gLPxNuSrATbexBh9q52abMW3EY6UXRqy+rUG+QX0TEuIEt2LkgsWAqV IrXXxtN4hgyQUOAN3iYunn9a8vHzVkgYwTrP3USCJ0zDr/AmhtO1U8Fx+rn29jEqOVE X-Received: by 2002:a05:6820:1893:b0:696:637e:4834 with SMTP id 006d021491bc7-69b25baee69mr4418027eaf.26.1778379262753; Sat, 09 May 2026 19:14:22 -0700 (PDT) Received: from mac.localdomain (71-218-106-55.hlrn.qwest.net. [71.218.106.55]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-69b25dbffdbsm3645643eaf.9.2026.05.09.19.14.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 May 2026 19:14:22 -0700 (PDT) From: James Hilliard Date: Sat, 09 May 2026 20:13:50 -0600 Subject: [PATCH v4 27/32] target/mips: add Octeon SMS4 crypto support MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20260509-mips-octeon-missing-insns-v2-v4-27-d669dcd05c2f@gmail.com> References: <20260509-mips-octeon-missing-insns-v2-v4-0-d669dcd05c2f@gmail.com> In-Reply-To: <20260509-mips-octeon-missing-insns-v2-v4-0-d669dcd05c2f@gmail.com> To: qemu-devel@nongnu.org Cc: Laurent Vivier , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Huacai Chen , James Hilliard , Helge Deller , Pierrick Bouvier , Pierrick Bouvier X-Mailer: b4 0.15.2 Received-SPF: pass client-ip=2607:f8b0:4864:20::c34; envelope-from=james.hilliard1@gmail.com; helo=mail-oo1-xc34.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org On Octeon, the SMS4 engine is exposed through selectors that alias the AES register bank. Add the missing selectors and model the shared RESULT, IV, and key state so the hardware interface matches the processor behaviour. Use the in-tree SM4 tables to implement the block operation without adding a host crypto dependency. Signed-off-by: James Hilliard --- Changes v1 -> v2: - Add selector dispatch updates in octeon_translate.c after moving COP2 decode out of translate.c. (suggested by Philippe Mathieu-Daudé) --- target/mips/cpu.h | 18 ++++++ target/mips/tcg/octeon_crypto.c | 109 +++++++++++++++++++++++++++++++++++++ target/mips/tcg/octeon_translate.c | 4 ++ 3 files changed, 131 insertions(+) diff --git a/target/mips/cpu.h b/target/mips/cpu.h index afe5126564..6994925d50 100644 --- a/target/mips/cpu.h +++ b/target/mips/cpu.h @@ -593,6 +593,20 @@ typedef enum MIPSOcteonCop2Sel { OCTEON_COP2_SEL_AES_DEC0 = 0x010e, OCTEON_COP2_SEL_AES_KEYLENGTH = 0x0110, OCTEON_COP2_SEL_AES_DAT0 = 0x0111, + /* + * SMS4 reuses the AES result/input, IV, and key banks and only adds + * operation selectors for ECB/CBC encrypt/decrypt. + */ + OCTEON_COP2_SEL_SMS4_RESINP0 = OCTEON_COP2_SEL_AES_RESULT0, + OCTEON_COP2_SEL_SMS4_RESINP1 = OCTEON_COP2_SEL_AES_RESULT1, + OCTEON_COP2_SEL_SMS4_IV0 = OCTEON_COP2_SEL_AES_IV0, + OCTEON_COP2_SEL_SMS4_IV1 = OCTEON_COP2_SEL_AES_IV1, + OCTEON_COP2_SEL_SMS4_KEY0 = OCTEON_COP2_SEL_AES_KEY0, + OCTEON_COP2_SEL_SMS4_KEY1 = OCTEON_COP2_SEL_AES_KEY1, + OCTEON_COP2_SEL_SMS4_ENC_CBC0 = OCTEON_COP2_SEL_AES_ENC_CBC0, + OCTEON_COP2_SEL_SMS4_ENC0 = OCTEON_COP2_SEL_AES_ENC0, + OCTEON_COP2_SEL_SMS4_DEC_CBC0 = OCTEON_COP2_SEL_AES_DEC_CBC0, + OCTEON_COP2_SEL_SMS4_DEC0 = OCTEON_COP2_SEL_AES_DEC0, OCTEON_COP2_SEL_CRC_POLYNOMIAL = 0x0200, OCTEON_COP2_SEL_CRC_IV = 0x0201, OCTEON_COP2_SEL_CRC_LEN = 0x0202, @@ -661,6 +675,10 @@ typedef enum MIPSOcteonCop2Sel { OCTEON_COP2_SEL_AES_ENC1 = 0x310b, OCTEON_COP2_SEL_AES_DEC_CBC1 = 0x310d, OCTEON_COP2_SEL_AES_DEC1 = 0x310f, + OCTEON_COP2_SEL_SMS4_ENC_CBC1 = 0x3119, + OCTEON_COP2_SEL_SMS4_ENC1 = 0x311b, + OCTEON_COP2_SEL_SMS4_DEC_CBC1 = 0x311d, + OCTEON_COP2_SEL_SMS4_DEC1 = 0x311f, OCTEON_COP2_SEL_HSH_STARTMD5 = 0x4047, OCTEON_COP2_SEL_SNOW3G_START = 0x404d, OCTEON_COP2_SEL_SNOW3G_MORE = 0x404e, diff --git a/target/mips/tcg/octeon_crypto.c b/target/mips/tcg/octeon_crypto.c index 43012ff027..177d112483 100644 --- a/target/mips/tcg/octeon_crypto.c +++ b/target/mips/tcg/octeon_crypto.c @@ -12,6 +12,7 @@ #include "exec/helper-proto.h" #include "crypto/aes.h" #include "crypto/clmul.h" +#include "crypto/sm4.h" #include "qemu/bitops.h" #include "qemu/host-utils.h" @@ -745,6 +746,57 @@ static int octeon_aes_key_bits(const MIPSOcteonCryptoState *crypto) } } +static inline 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 inline 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 const uint8_t octeon_des_ip[64] = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, @@ -1198,6 +1250,47 @@ static void octeon_aes_store_block(uint64_t regs[2], const uint8_t *block) regs[1] = ldq_be_p(block + 8); } +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 RESULT/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_input, 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_result, out); + if (cbc) { + octeon_aes_store_block(crypto->aes_iv, encrypt ? out : next_iv); + } +} + static void octeon_aes_encrypt_common(MIPSOcteonCryptoState *crypto, bool cbc) { AES_KEY key; @@ -1614,6 +1707,22 @@ void helper_octeon_cop2_dmtc2(CPUMIPSState *env, uint64_t value, crypto->aes_input[1] = q; octeon_aes_decrypt_common(crypto, false); break; + case OCTEON_COP2_SEL_SMS4_ENC_CBC1: + crypto->aes_input[1] = q; + octeon_sms4_crypt_common(crypto, true, true); + break; + case OCTEON_COP2_SEL_SMS4_ENC1: + crypto->aes_input[1] = q; + octeon_sms4_crypt_common(crypto, true, false); + break; + case OCTEON_COP2_SEL_SMS4_DEC_CBC1: + crypto->aes_input[1] = q; + octeon_sms4_crypt_common(crypto, false, true); + break; + case OCTEON_COP2_SEL_SMS4_DEC1: + crypto->aes_input[1] = q; + octeon_sms4_crypt_common(crypto, false, false); + break; case OCTEON_COP2_SEL_GFM_XORMUL1: { uint64_t in[2] = { crypto->gfm_result[0] ^ crypto->gfm_xor0, diff --git a/target/mips/tcg/octeon_translate.c b/target/mips/tcg/octeon_translate.c index d548b84a78..d8379924f1 100644 --- a/target/mips/tcg/octeon_translate.c +++ b/target/mips/tcg/octeon_translate.c @@ -180,6 +180,10 @@ static bool octeon_cop2_is_supported_dmtc2(uint16_t sel) case OCTEON_COP2_SEL_AES_ENC1: case OCTEON_COP2_SEL_AES_DEC_CBC1: case OCTEON_COP2_SEL_AES_DEC1: + case OCTEON_COP2_SEL_SMS4_ENC_CBC1: + case OCTEON_COP2_SEL_SMS4_ENC1: + case OCTEON_COP2_SEL_SMS4_DEC_CBC1: + case OCTEON_COP2_SEL_SMS4_DEC1: return true; default: return false; -- 2.54.0