From 055abe97b017ed7e519863a65de09c522cfa0944 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 28 Jun 2026 09:42:03 +0000 Subject: [PATCH 1/2] Initial plan From 5351c996161cd306d498d0969132811ea55aa414 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 28 Jun 2026 09:51:04 +0000 Subject: [PATCH 2/2] Fix sm4-128-cbc incorrectly added as stream cipher The review comment correctly identified that sm4-128-cbc cannot work as a stream cipher because the stream implementation only calls mbedtls_cipher_update() and never mbedtls_cipher_finish() for the final block, and CBC mode requires block-aligned input. Remove sm4-128-cbc from the stream cipher list entirely. sm4-128-ctr (CTR mode) is retained as it is a proper stream cipher mode. sm4-128-gcm is also added as an AEAD cipher (unrelated to the stream cipher issue). --- src/aead.c | 13 +++++++++---- src/aead.h | 4 ++-- src/stream.c | 23 +++++++++++++---------- src/stream.h | 2 +- src/utils.c | 2 ++ 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/aead.c b/src/aead.c index 6f84d75a8..787723aa5 100644 --- a/src/aead.c +++ b/src/aead.c @@ -44,15 +44,16 @@ #define AES128GCM 0 #define AES192GCM 1 #define AES256GCM 2 +#define SM4128GCM 3 /* * methods above requires gcm context * methods below doesn't require it, * then we need to fake one */ -#define CHACHA20POLY1305IETF 3 +#define CHACHA20POLY1305IETF 4 #ifdef FS_HAVE_XCHACHA20IETF -#define XCHACHA20POLY1305IETF 4 +#define XCHACHA20POLY1305IETF 5 #endif #define CHUNK_SIZE_LEN 2 @@ -108,6 +109,7 @@ const char *supported_aead_ciphers[AEAD_CIPHER_NUM] = { "aes-128-gcm", "aes-192-gcm", "aes-256-gcm", + "sm4-128-gcm", "chacha20-ietf-poly1305", #ifdef FS_HAVE_XCHACHA20IETF "xchacha20-ietf-poly1305" @@ -121,6 +123,7 @@ static const char *supported_aead_ciphers_mbedtls[AEAD_CIPHER_NUM] = { "AES-128-GCM", "AES-192-GCM", "AES-256-GCM", + "SM4-128-GCM", CIPHER_UNSUPPORTED, #ifdef FS_HAVE_XCHACHA20IETF CIPHER_UNSUPPORTED @@ -128,14 +131,14 @@ static const char *supported_aead_ciphers_mbedtls[AEAD_CIPHER_NUM] = { }; static const int supported_aead_ciphers_nonce_size[AEAD_CIPHER_NUM] = { - 12, 12, 12, 12, + 12, 12, 12, 12, 12, #ifdef FS_HAVE_XCHACHA20IETF 24 #endif }; static const int supported_aead_ciphers_key_size[AEAD_CIPHER_NUM] = { - 16, 24, 32, 32, + 16, 24, 32, 16, 32, #ifdef FS_HAVE_XCHACHA20IETF 32 #endif @@ -177,6 +180,7 @@ aead_cipher_encrypt(cipher_ctx_t *cipher_ctx, // Otherwise, just use the mbedTLS one with crappy AES-NI. case AES192GCM: case AES128GCM: + case SM4128GCM: #if MBEDTLS_VERSION_NUMBER < 0x03000000 err = mbedtls_cipher_auth_encrypt(cipher_ctx->evp, n, nlen, ad, adlen, m, mlen, c, clen, c + mlen, tlen); @@ -230,6 +234,7 @@ aead_cipher_decrypt(cipher_ctx_t *cipher_ctx, // Otherwise, just use the mbedTLS one with crappy AES-NI. case AES192GCM: case AES128GCM: + case SM4128GCM: #if MBEDTLS_VERSION_NUMBER < 0x03000000 err = mbedtls_cipher_auth_decrypt(cipher_ctx->evp, n, nlen, ad, adlen, m, mlen - tlen, p, plen, m + mlen - tlen, tlen); diff --git a/src/aead.h b/src/aead.h index 3f2408f9d..f9e5d568c 100644 --- a/src/aead.h +++ b/src/aead.h @@ -28,9 +28,9 @@ // currently, XCHACHA20POLY1305IETF is not released yet // XCHACHA20POLY1305 is removed in upstream #ifdef FS_HAVE_XCHACHA20IETF -#define AEAD_CIPHER_NUM 5 +#define AEAD_CIPHER_NUM 6 #else -#define AEAD_CIPHER_NUM 4 +#define AEAD_CIPHER_NUM 5 #endif int aead_encrypt_all(buffer_t *, cipher_t *, size_t); diff --git a/src/stream.c b/src/stream.c index 849c51182..87fac82dc 100644 --- a/src/stream.c +++ b/src/stream.c @@ -86,14 +86,15 @@ #define CAMELLIA_128_CFB 10 #define CAMELLIA_192_CFB 11 #define CAMELLIA_256_CFB 12 -#define CAST5_CFB 13 -#define DES_CFB 14 -#define IDEA_CFB 15 -#define RC2_CFB 16 -#define SEED_CFB 17 -#define SALSA20 18 -#define CHACHA20 19 -#define CHACHA20IETF 20 +#define SM4_128_CTR 13 +#define CAST5_CFB 14 +#define DES_CFB 15 +#define IDEA_CFB 16 +#define RC2_CFB 17 +#define SEED_CFB 18 +#define SALSA20 19 +#define CHACHA20 20 +#define CHACHA20IETF 21 const char *supported_stream_ciphers[STREAM_CIPHER_NUM] = { "table", @@ -109,6 +110,7 @@ const char *supported_stream_ciphers[STREAM_CIPHER_NUM] = { "camellia-128-cfb", "camellia-192-cfb", "camellia-256-cfb", + "sm4-128-ctr", "cast5-cfb", "des-cfb", "idea-cfb", @@ -133,6 +135,7 @@ static const char *supported_stream_ciphers_mbedtls[STREAM_CIPHER_NUM] = { "CAMELLIA-128-CFB128", "CAMELLIA-192-CFB128", "CAMELLIA-256-CFB128", + "SM4-128-CTR", CIPHER_UNSUPPORTED, CIPHER_UNSUPPORTED, CIPHER_UNSUPPORTED, @@ -144,11 +147,11 @@ static const char *supported_stream_ciphers_mbedtls[STREAM_CIPHER_NUM] = { }; static const int supported_stream_ciphers_nonce_size[STREAM_CIPHER_NUM] = { - 0, 0, 16, 16, 16, 16, 16, 16, 16, 8, 16, 16, 16, 8, 8, 8, 8, 16, 8, 8, 12 + 0, 0, 16, 16, 16, 16, 16, 16, 16, 8, 16, 16, 16, 16, 8, 8, 8, 8, 16, 8, 8, 12 }; static const int supported_stream_ciphers_key_size[STREAM_CIPHER_NUM] = { - 0, 16, 16, 16, 24, 32, 16, 24, 32, 16, 16, 24, 32, 16, 8, 16, 16, 16, 32, 32, 32 + 0, 16, 16, 16, 24, 32, 16, 24, 32, 16, 16, 24, 32, 16, 16, 8, 16, 16, 16, 32, 32, 32 }; static int diff --git a/src/stream.h b/src/stream.h index 35bd98f8e..e19757db6 100644 --- a/src/stream.h +++ b/src/stream.h @@ -37,7 +37,7 @@ #endif #include -#define STREAM_CIPHER_NUM 21 +#define STREAM_CIPHER_NUM 22 #include "crypto.h" diff --git a/src/utils.c b/src/utils.c index d3ff2aba6..e68b27e96 100644 --- a/src/utils.c +++ b/src/utils.c @@ -322,6 +322,8 @@ usage() " camellia-128-cfb, camellia-192-cfb,\n"); printf( " camellia-256-cfb, bf-cfb,\n"); + printf( + " sm4-128-ctr, sm4-128-gcm,\n"); printf( " chacha20-ietf-poly1305,\n"); #ifdef FS_HAVE_XCHACHA20IETF