- Introduce the SM4 cipher algorithms (OSCCA GB/T 32907-2016).
- intel_iommu: Add missed sanity check for 256-bit invalidation queue
- linux-user: use 'max' instead of 'qemu32' / 'qemu64' by default
- chardev/baum: Use definitions to avoid dynamic stack allocation
- ui/console: Get tab completion working again in the SDL monitor vc
- s390x/tcg: Fix opcode for lzrf
- virtiofsd: use g_date_time_get_microsecond to get subsecond
- ui/curses: Avoid dynamic stack allocation
- target/m68k: always call gen_exit_tb() after writes to SR
- target/m68k: Perform writback before modifying SR
- target/m68k: Fix MACSR to CCR
- target/m68k: Implement atomic test-and-set
- block/nvme: nvme_process_completion() fix bound for cid
- hw/pci-host: pnv_phb{3, 4}: Fix heap out-of-bound access failure
- target/ppc: Zero second doubleword of VSR registers for FPR insns
- target/ppc: Set OV32 when OV is set
- target/ppc: Zero second doubleword for VSX madd instructions
- target/ppc: Set result to QNaN for DENBCD when VXCVI occurs
- hw/pci: Add parenthesis to PCI_BUILD_BDF macro
- intel_iommu: Send IQE event when setting reserved bit in IQT_TAIL
- acpi: cpuhp: fix guest-visible maximum access size to the legacy reg block
- acpi: ged: Add macro for acpi sleep control register
- hw/pci-bridge: Add a Kconfig switch for the normal PCI bridge
- ui/vnc: fix handling of VNC_FEATURE_XVP
- s390/sclp: fix SCLP facility map
- docs/tools/qemu-img.rst: fix typo (sumarizes)
- chardev/char: fix qemu_chr_is_busy() check
- edu: fix DMA range upper bound check
- platform-bus: fix refcount leak
- hw/net/virtio-net: fix qemu set used ring flag even vhost started
- hw/net/can/sja1000: fix bug for single acceptance filter and standard frame
- tests/avocado: fix typo in replay_linux
- util/userfaultfd: Remove unused uffd_poll_events
- hw/core/ptimer: fix timer zero period condition for freq > 1GHz
- hcd-ohci: Drop ohci_service_iso_td() if ed->head & OHCI_DPTR_MASK is zero
- tests/unit/test-vmstate: Avoid dynamic stack allocation
- hw/usb/hcd-ohci: Use definition to avoid dynamic stack allocation
- hw/i386/multiboot: Avoid dynamic stack allocation
- hw/ppc/spapr: Fix code style problems reported by checkpatch
- chardev/baum: Replace magic values by X_MAX / Y_MAX definitions
- hw/intc/xics: Avoid dynamic stack allocation
- hw/net/e1000e_core: Use definition to avoid dynamic stack allocation
- intel_iommu: Fix invalidation descriptor type field
- configs: Fix typo in the sh4-softmmu devices config file
Signed-off-by: Jiabo Feng <fengjiabo1@huawei.com>
(cherry picked from commit 9813ed21ec2499c50cb58ac5fb114a1641708eb2)
342 lines
12 KiB
Diff
342 lines
12 KiB
Diff
From b8752afb94b20ec6ea0892260a689cfedbf6f1a5 Mon Sep 17 00:00:00 2001
|
|
From: gaochuanji <gaochuanji@inspur.com>
|
|
Date: Thu, 19 Sep 2024 10:56:42 +0800
|
|
Subject: [PATCH] Introduce the SM4 cipher algorithms (OSCCA GB/T 32907-2016).
|
|
MIME-Version: 1.0
|
|
Content-Type: text/plain; charset=UTF-8
|
|
Content-Transfer-Encoding: 8bit
|
|
|
|
SM4 (GBT.32907-2016) is a cryptographic standard issued by the
|
|
Organization of State Commercial Administration of China (OSCCA)
|
|
as an authorized cryptographic algorithms for the use within China.
|
|
|
|
Detect the SM4 cipher algorithms and enable the feature silently
|
|
if it is available.
|
|
|
|
Signed-off-by: Hyman Huang <yong.huang@smartx.com>
|
|
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
|
|
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
|
|
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
|
|
Signed-off-by: cheliequan <cheliequan@inspur.com>
|
|
---
|
|
crypto/block-luks.c | 11 ++++++++
|
|
crypto/cipher-gcrypt.c.inc | 8 ++++++
|
|
crypto/cipher-nettle.c.inc | 49 +++++++++++++++++++++++++++++++++
|
|
crypto/cipher.c | 6 ++++
|
|
meson.build | 49 +++++++++++++++++++++++++++++++++
|
|
qapi/crypto.json | 5 +++-
|
|
tests/unit/test-crypto-cipher.c | 13 +++++++++
|
|
7 files changed, 140 insertions(+), 1 deletion(-)
|
|
|
|
diff --git a/crypto/block-luks.c b/crypto/block-luks.c
|
|
index fe8f04ffb2..0626092739 100644
|
|
--- a/crypto/block-luks.c
|
|
+++ b/crypto/block-luks.c
|
|
@@ -126,12 +126,23 @@ qcrypto_block_luks_cipher_size_map_twofish[] = {
|
|
{ 0, 0 },
|
|
};
|
|
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+static const QCryptoBlockLUKSCipherSizeMap
|
|
+qcrypto_block_luks_cipher_size_map_sm4[] = {
|
|
+ { 16, QCRYPTO_CIPHER_ALG_SM4},
|
|
+ { 0, 0 },
|
|
+};
|
|
+#endif
|
|
+
|
|
static const QCryptoBlockLUKSCipherNameMap
|
|
qcrypto_block_luks_cipher_name_map[] = {
|
|
{ "aes", qcrypto_block_luks_cipher_size_map_aes },
|
|
{ "cast5", qcrypto_block_luks_cipher_size_map_cast5 },
|
|
{ "serpent", qcrypto_block_luks_cipher_size_map_serpent },
|
|
{ "twofish", qcrypto_block_luks_cipher_size_map_twofish },
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ { "sm4", qcrypto_block_luks_cipher_size_map_sm4},
|
|
+#endif
|
|
};
|
|
|
|
|
|
diff --git a/crypto/cipher-gcrypt.c.inc b/crypto/cipher-gcrypt.c.inc
|
|
index a6a0117717..1377cbaf14 100644
|
|
--- a/crypto/cipher-gcrypt.c.inc
|
|
+++ b/crypto/cipher-gcrypt.c.inc
|
|
@@ -35,6 +35,9 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
|
|
case QCRYPTO_CIPHER_ALG_SERPENT_256:
|
|
case QCRYPTO_CIPHER_ALG_TWOFISH_128:
|
|
case QCRYPTO_CIPHER_ALG_TWOFISH_256:
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ case QCRYPTO_CIPHER_ALG_SM4:
|
|
+#endif
|
|
break;
|
|
default:
|
|
return false;
|
|
@@ -219,6 +222,11 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
|
|
case QCRYPTO_CIPHER_ALG_TWOFISH_256:
|
|
gcryalg = GCRY_CIPHER_TWOFISH;
|
|
break;
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ case QCRYPTO_CIPHER_ALG_SM4:
|
|
+ gcryalg = GCRY_CIPHER_SM4;
|
|
+ break;
|
|
+#endif
|
|
default:
|
|
error_setg(errp, "Unsupported cipher algorithm %s",
|
|
QCryptoCipherAlgorithm_str(alg));
|
|
diff --git a/crypto/cipher-nettle.c.inc b/crypto/cipher-nettle.c.inc
|
|
index 24cc61f87b..42b39e18a2 100644
|
|
--- a/crypto/cipher-nettle.c.inc
|
|
+++ b/crypto/cipher-nettle.c.inc
|
|
@@ -33,6 +33,9 @@
|
|
#ifndef CONFIG_QEMU_PRIVATE_XTS
|
|
#include <nettle/xts.h>
|
|
#endif
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+#include <nettle/sm4.h>
|
|
+#endif
|
|
|
|
static inline bool qcrypto_length_check(size_t len, size_t blocksize,
|
|
Error **errp)
|
|
@@ -426,6 +429,30 @@ DEFINE_ECB_CBC_CTR_XTS(qcrypto_nettle_twofish,
|
|
QCryptoNettleTwofish, TWOFISH_BLOCK_SIZE,
|
|
twofish_encrypt_native, twofish_decrypt_native)
|
|
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+typedef struct QCryptoNettleSm4 {
|
|
+ QCryptoCipher base;
|
|
+ struct sm4_ctx key[2];
|
|
+} QCryptoNettleSm4;
|
|
+
|
|
+static void sm4_encrypt_native(void *ctx, size_t length,
|
|
+ uint8_t *dst, const uint8_t *src)
|
|
+{
|
|
+ struct sm4_ctx *keys = ctx;
|
|
+ sm4_crypt(&keys[0], length, dst, src);
|
|
+}
|
|
+
|
|
+static void sm4_decrypt_native(void *ctx, size_t length,
|
|
+ uint8_t *dst, const uint8_t *src)
|
|
+{
|
|
+ struct sm4_ctx *keys = ctx;
|
|
+ sm4_crypt(&keys[1], length, dst, src);
|
|
+}
|
|
+
|
|
+DEFINE_ECB(qcrypto_nettle_sm4,
|
|
+ QCryptoNettleSm4, SM4_BLOCK_SIZE,
|
|
+ sm4_encrypt_native, sm4_decrypt_native)
|
|
+#endif
|
|
|
|
bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
|
|
QCryptoCipherMode mode)
|
|
@@ -443,6 +470,9 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
|
|
case QCRYPTO_CIPHER_ALG_TWOFISH_128:
|
|
case QCRYPTO_CIPHER_ALG_TWOFISH_192:
|
|
case QCRYPTO_CIPHER_ALG_TWOFISH_256:
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ case QCRYPTO_CIPHER_ALG_SM4:
|
|
+#endif
|
|
break;
|
|
default:
|
|
return false;
|
|
@@ -701,6 +731,25 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
|
|
|
|
return &ctx->base;
|
|
}
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ case QCRYPTO_CIPHER_ALG_SM4:
|
|
+ {
|
|
+ QCryptoNettleSm4 *ctx = g_new0(QCryptoNettleSm4, 1);
|
|
+
|
|
+ switch (mode) {
|
|
+ case QCRYPTO_CIPHER_MODE_ECB:
|
|
+ ctx->base.driver = &qcrypto_nettle_sm4_driver_ecb;
|
|
+ break;
|
|
+ default:
|
|
+ goto bad_cipher_mode;
|
|
+ }
|
|
+
|
|
+ sm4_set_encrypt_key(&ctx->key[0], key);
|
|
+ sm4_set_decrypt_key(&ctx->key[1], key);
|
|
+
|
|
+ return &ctx->base;
|
|
+ }
|
|
+#endif
|
|
|
|
default:
|
|
error_setg(errp, "Unsupported cipher algorithm %s",
|
|
diff --git a/crypto/cipher.c b/crypto/cipher.c
|
|
index 74b09a5b26..5f512768ea 100644
|
|
--- a/crypto/cipher.c
|
|
+++ b/crypto/cipher.c
|
|
@@ -38,6 +38,9 @@ static const size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ [QCRYPTO_CIPHER_ALG_SM4] = 16,
|
|
+#endif
|
|
};
|
|
|
|
static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
|
@@ -53,6 +56,9 @@ static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ [QCRYPTO_CIPHER_ALG_SM4] = 16,
|
|
+#endif
|
|
};
|
|
|
|
static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
|
|
diff --git a/meson.build b/meson.build
|
|
index 45bc69bf0c..1a225b51f3 100644
|
|
--- a/meson.build
|
|
+++ b/meson.build
|
|
@@ -1010,6 +1010,7 @@ endif
|
|
# gcrypt over nettle for performance reasons.
|
|
gcrypt = not_found
|
|
nettle = not_found
|
|
+crypto_sm4 = not_found
|
|
xts = 'none'
|
|
|
|
if get_option('nettle').enabled() and get_option('gcrypt').enabled()
|
|
@@ -1035,6 +1036,17 @@ if not gnutls_crypto.found()
|
|
gcrypt,
|
|
cc.find_library('gpg-error', required: true, kwargs: static_kwargs)])
|
|
endif
|
|
+ crypto_sm4 = gcrypt
|
|
+ # SM4 ALG is available in libgcrypt >= 1.9
|
|
+ if gcrypt.found() and not cc.links('''
|
|
+ #include <gcrypt.h>
|
|
+ int main(void) {
|
|
+ gcry_cipher_hd_t handler;
|
|
+ gcry_cipher_open(&handler, GCRY_CIPHER_SM4, GCRY_CIPHER_MODE_ECB, 0);
|
|
+ return 0;
|
|
+ }''', dependencies: gcrypt)
|
|
+ crypto_sm4 = not_found
|
|
+ endif
|
|
endif
|
|
if (not get_option('nettle').auto() or have_system) and not gcrypt.found()
|
|
nettle = dependency('nettle', version: '>=3.4',
|
|
@@ -1044,6 +1056,18 @@ if not gnutls_crypto.found()
|
|
if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle)
|
|
xts = 'private'
|
|
endif
|
|
+ crypto_sm4 = nettle
|
|
+ # SM4 ALG is available in nettle >= 3.9
|
|
+ if nettle.found() and not cc.links('''
|
|
+ #include <nettle/sm4.h>
|
|
+ int main(void) {
|
|
+ struct sm4_ctx ctx;
|
|
+ unsigned char key[16] = {0};
|
|
+ sm4_set_encrypt_key(&ctx, key);
|
|
+ return 0;
|
|
+ }''', dependencies: nettle)
|
|
+ crypto_sm4 = not_found
|
|
+ endif
|
|
endif
|
|
endif
|
|
|
|
@@ -1411,6 +1435,17 @@ if get_option('virtfs').enabled()
|
|
elif not have_system
|
|
error('virtio-9p (virtfs) needs system emulation support')
|
|
endif
|
|
+ crypto_sm4 = gcrypt
|
|
+ # SM4 ALG is available in libgcrypt >= 1.9
|
|
+ if gcrypt.found() and not cc.links('''
|
|
+ #include <gcrypt.h>
|
|
+ int main(void) {
|
|
+ gcry_cipher_hd_t handler;
|
|
+ gcry_cipher_open(&handler, GCRY_CIPHER_SM4, GCRY_CIPHER_MODE_ECB, 0);
|
|
+ return 0;
|
|
+ }''', dependencies: gcrypt)
|
|
+ crypto_sm4 = not_found
|
|
+ endif
|
|
endif
|
|
elif get_option('virtfs').disabled()
|
|
have_virtfs = false
|
|
@@ -1487,6 +1522,7 @@ config_host_data.set('CONFIG_GNUTLS', gnutls.found())
|
|
config_host_data.set('CONFIG_GNUTLS_CRYPTO', gnutls_crypto.found())
|
|
config_host_data.set('CONFIG_GCRYPT', gcrypt.found())
|
|
config_host_data.set('CONFIG_NETTLE', nettle.found())
|
|
+config_host_data.set('CONFIG_CRYPTO_SM4', crypto_sm4.found())
|
|
config_host_data.set('CONFIG_QEMU_PRIVATE_XTS', xts == 'private')
|
|
config_host_data.set('CONFIG_MALLOC_TRIM', has_malloc_trim)
|
|
config_host_data.set('CONFIG_STATX', has_statx)
|
|
@@ -2093,6 +2129,18 @@ if capstone_opt in ['enabled', 'auto', 'system']
|
|
if capstone_opt == 'system'
|
|
error('system capstone requested, it does not appear to work')
|
|
endif
|
|
+ crypto_sm4 = nettle
|
|
+ # SM4 ALG is available in nettle >= 3.9
|
|
+ if nettle.found() and not cc.links('''
|
|
+ #include <nettle/sm4.h>
|
|
+ int main(void) {
|
|
+ struct sm4_ctx ctx;
|
|
+ unsigned char key[16] = {0};
|
|
+ sm4_set_encrypt_key(&ctx, key);
|
|
+ return 0;
|
|
+ }''', dependencies: nettle)
|
|
+ crypto_sm4 = not_found
|
|
+ endif
|
|
endif
|
|
|
|
if capstone.found()
|
|
@@ -3432,6 +3480,7 @@ summary_info += {'nettle': nettle}
|
|
if nettle.found()
|
|
summary_info += {' XTS': xts != 'private'}
|
|
endif
|
|
+summary_info += {'SM4 ALG support': crypto_sm4}
|
|
summary_info += {'crypto afalg': config_host.has_key('CONFIG_AF_ALG')}
|
|
summary_info += {'rng-none': config_host.has_key('CONFIG_RNG_NONE')}
|
|
summary_info += {'Linux keyring': config_host.has_key('CONFIG_SECRET_KEYRING')}
|
|
diff --git a/qapi/crypto.json b/qapi/crypto.json
|
|
index 1ec54c15ca..60e706ae09 100644
|
|
--- a/qapi/crypto.json
|
|
+++ b/qapi/crypto.json
|
|
@@ -75,6 +75,8 @@
|
|
# @twofish-128: Twofish with 128 bit / 16 byte keys
|
|
# @twofish-192: Twofish with 192 bit / 24 byte keys
|
|
# @twofish-256: Twofish with 256 bit / 32 byte keys
|
|
+# @sm4: SM4 with 128 bit / 16 byte keys (since 9.0)
|
|
+#
|
|
# Since: 2.6
|
|
##
|
|
{ 'enum': 'QCryptoCipherAlgorithm',
|
|
@@ -83,7 +85,8 @@
|
|
'des', '3des',
|
|
'cast5-128',
|
|
'serpent-128', 'serpent-192', 'serpent-256',
|
|
- 'twofish-128', 'twofish-192', 'twofish-256']}
|
|
+ 'twofish-128', 'twofish-192', 'twofish-256',
|
|
+ 'sm4']}
|
|
|
|
|
|
##
|
|
diff --git a/tests/unit/test-crypto-cipher.c b/tests/unit/test-crypto-cipher.c
|
|
index d9d9d078ff..11ab1a54fc 100644
|
|
--- a/tests/unit/test-crypto-cipher.c
|
|
+++ b/tests/unit/test-crypto-cipher.c
|
|
@@ -382,6 +382,19 @@ static QCryptoCipherTestData test_data[] = {
|
|
.plaintext = "90afe91bb288544f2c32dc239b2635e6",
|
|
.ciphertext = "6cb4561c40bf0a9705931cb6d408e7fa",
|
|
},
|
|
+#ifdef CONFIG_CRYPTO_SM4
|
|
+ {
|
|
+ /* SM4, GB/T 32907-2016, Appendix A.1 */
|
|
+ .path = "/crypto/cipher/sm4",
|
|
+ .alg = QCRYPTO_CIPHER_ALG_SM4,
|
|
+ .mode = QCRYPTO_CIPHER_MODE_ECB,
|
|
+ .key = "0123456789abcdeffedcba9876543210",
|
|
+ .plaintext =
|
|
+ "0123456789abcdeffedcba9876543210",
|
|
+ .ciphertext =
|
|
+ "681edf34d206965e86b3e94f536e4246",
|
|
+ },
|
|
+#endif
|
|
{
|
|
/* #1 32 byte key, 32 byte PTX */
|
|
.path = "/crypto/cipher/aes-xts-128-1",
|
|
--
|
|
2.41.0.windows.1
|
|
|