Skip to content

Remove sm4-128-cbc from stream ciphers; add sm4-128-ctr and sm4-128-gcm#3043

Open
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-code-review-comment
Open

Remove sm4-128-cbc from stream ciphers; add sm4-128-ctr and sm4-128-gcm#3043
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-code-review-comment

Conversation

Copilot AI commented Jun 28, 2026

Copy link
Copy Markdown

sm4-128-cbc was incorrectly added as a stream cipher. The stream cipher path only calls mbedtls_cipher_update() — it never calls mbedtls_cipher_finish() and has no padding support, so CBC mode will corrupt or truncate data for non-block-aligned payloads.

Changes

  • src/stream.c / src/stream.h: Drop sm4-128-cbc entirely from stream ciphers. Add sm4-128-ctr only (CTR is a native stream mode). STREAM_CIPHER_NUM 21 → 22. Updated SM4_128_CTR = 13 define (shifts CAST5_CFB and below by one), plus all parallel arrays (supported_stream_ciphers, supported_stream_ciphers_mbedtls, nonce_size, key_size).

  • src/aead.c / src/aead.h: Add sm4-128-gcm as an AEAD cipher (SM4128GCM = 3). Shifts CHACHA20POLY1305IETF to index 4. AEAD_CIPHER_NUM 4 → 5 (6 with xchacha). Adds SM4128GCM fall-through in both aead_cipher_encrypt and aead_cipher_decrypt alongside the existing mbedTLS GCM path.

  • src/utils.c: Update usage text to advertise sm4-128-ctr and sm4-128-gcm.

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).
Copilot AI changed the title [WIP] Fix code based on review comment Remove sm4-128-cbc from stream ciphers; add sm4-128-ctr and sm4-128-gcm Jun 28, 2026
Copilot AI requested a review from madeye June 28, 2026 09:51
@madeye madeye marked this pull request as ready for review June 28, 2026 10:48
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.

2 participants