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).
This commit is contained in:
copilot-swe-agent[bot]
2026-06-28 09:51:04 +00:00
committed by GitHub
parent 055abe97b0
commit 5351c99616
5 changed files with 27 additions and 17 deletions
+9 -4
View File
@@ -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);
+2 -2
View File
@@ -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);
+13 -10
View File
@@ -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
+1 -1
View File
@@ -37,7 +37,7 @@
#endif
#include <sodium.h>
#define STREAM_CIPHER_NUM 21
#define STREAM_CIPHER_NUM 22
#include "crypto.h"
+2
View File
@@ -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