|
@@ -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",
|