Skip to content

Add SM4 cipher (GB/T 32907-2016) support from SM4 enabled mbed TLS library#2424

Open
Low-power wants to merge 2 commits into
shadowsocks:masterfrom
Low-power:sm4
Open

Add SM4 cipher (GB/T 32907-2016) support from SM4 enabled mbed TLS library#2424
Low-power wants to merge 2 commits into
shadowsocks:masterfrom
Low-power:sm4

Conversation

@Low-power

@Low-power Low-power commented Jul 9, 2019

Copy link
Copy Markdown
Contributor

Hello.

There is a SM4 cipher support for mbed TLS in pull request Mbed-TLS/mbedtls#1165 some years. Unfortunately that pull request was still left unmerged and became outdated in current mbed TLS versions; however I still managed to merge that pull request into the long term support branch 2.7, and with this minor modification to shadowsocks-libev, the SM4 ciphers appears fully functional in shadowsocks-libev.
The implemented ciphers in shadowsocks-libev are sm4-128-cbc, sm4-128-ctr and sm4-128-gcm.

Until Mbed-TLS/mbedtls#1165 can be merged, mbed TLS woudn't offically supporting SM4 cipher, however I opened this pull request for discuss whether to check a downstream version of mbed TLS of this SM4 support, in shadowsocks-libev.

My mbed TLS branch with SM4 support merged is available at https://git.nsscn.top/Low-power/mbedtls/tree/mbedtls-2.7-sm4

@madeye

madeye commented Jul 9, 2019

Copy link
Copy Markdown
Contributor

For any cipher change, please go to https://github.com/shadowsocks/shadowsocks-org/issues

@ttimasdf

ttimasdf commented Nov 21, 2019

Copy link
Copy Markdown

Using SM4 for anti-censoring...This PR could definitely piss off those officers of OSCCA 🤣

@ghost

ghost commented Nov 21, 2019

Copy link
Copy Markdown

anti-censoring

Maybe anti-(NSA's)censoring🤣

@Low-power

Copy link
Copy Markdown
Contributor Author

Since the old repository I hosted my Mbed TLS fork has long gone, I republished the repository at https://github.com/Low-power/mbedtls/tree/mbedtls-2.7-sm4 for anyone interested in that.

zonyitoo added a commit to shadowsocks/shadowsocks-crypto that referenced this pull request Oct 23, 2023
- https://datatracker.ietf.org/doc/html/rfc8998
- shadowsocks/shadowsocks-libev#2424

If anyone interested in trying these non-standard ciphers, please take
some time to get some background knowledge about the SM Cipher Suites.
zonyitoo added a commit to shadowsocks/shadowsocks-crypto that referenced this pull request Oct 23, 2023
- https://datatracker.ietf.org/doc/html/rfc8998
- shadowsocks/shadowsocks-libev#2424

If anyone interested in trying these non-standard ciphers, please take
some time to get some background knowledge about the SM Cipher Suites.
@madeye

madeye commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for the SM4 (GB/T 32907-2016) support. This has drifted and now conflicts with master: it still patches the removed m4/ autotools files (the build is CMake-only now). Could you rebase onto current master and drop the m4/ changes? Keeping open as an enhancement.

@Low-power

Copy link
Copy Markdown
Contributor Author

I didn't find the mbed TLS cipher availability checks in current CMake scripts, as previously implemented in m4/mbedtls.m4. I added only the SM4 cipher availability checking there, so I think this file can simply be removed without changing of CMake scripts.

@madeye madeye requested a review from Copilot June 28, 2026 09:30
@madeye madeye marked this pull request as ready for review June 28, 2026 09:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds SM4 cipher identifiers to shadowsocks-libev’s supported cipher lists so builds using an SM4-enabled mbed TLS can select sm4-128-ctr (stream) and sm4-128-gcm (AEAD), and it advertises the new ciphers in --help.

Changes:

  • Add SM4 stream cipher entries (sm4-128-cbc, sm4-128-ctr) and bump STREAM_CIPHER_NUM.
  • Add SM4 AEAD entry (sm4-128-gcm) and bump AEAD_CIPHER_NUM.
  • Update CLI usage text to list the SM4 ciphers.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/utils.c Advertises SM4 ciphers in CLI usage output.
src/stream.h Increases stream cipher count to accommodate SM4 entries.
src/stream.c Adds SM4 stream cipher names, mbed TLS mappings, and nonce/key sizes.
src/aead.h Increases AEAD cipher count to accommodate SM4-GCM.
src/aead.c Adds SM4-GCM to AEAD lists and routes it through the mbed TLS AEAD path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/stream.c
Comment on lines 112 to 116
"camellia-192-cfb",
"camellia-256-cfb",
"sm4-128-cbc",
"sm4-128-ctr",
"cast5-cfb",
Comment thread src/aead.c
Comment on lines 140 to 144
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
Copilot AI added a commit that referenced this pull request Jun 28, 2026
… in AEAD cipher table

PR #2424 review comment fix: supported_aead_ciphers_tag_size was not updated
when sm4-128-gcm was added, causing chacha20-ietf-poly1305 and xchacha20-ietf-poly1305
to receive a zero tag size, breaking AEAD encryption/decryption.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants