SM4 Encrypt
Chinese National Standard SM4 128-bit Block Cipher (GB/T 32907 / GM/T 0002)
Overview
Encrypts data using the Chinese national standard 128-bit unbalanced Feistel block cipher SM4 (GB/T 32907-2016 / GM/T 0002-2012 / ISO/IEC 18033-3:2021) with a fixed 128-bit (16-byte) key and 32 rounds.
Supports CBC, GCM, CTR, ECB, CFB, and OFB modes over 16-byte blocks.
When to Use
- Implementing ShangMi (SM) cryptographic suites, TLS 1.3 SM4 cipher suites (RFC 8998), or Chinese commercial cryptography compliance tests.
- Testing GB/T 32907 16-byte block test vectors.
Parameter Configuration
| Parameter (ID) | Type & Default | Visibility Condition | Description & Options |
|---|---|---|---|
Cipher Modemode |
selectDefault: ECB |
Always visible | Selects the operating mode (CBC, GCM, CTR, ECB, CFB, or OFB). See Block Cipher Modes for full details.
|
Paddingpadding |
selectDefault: None |
Always visible | 16-byte block padding scheme applied in ECB and CBC modes. See Padding Schemes for full details.
|
Unify ErrorserrorUnify |
booleanDefault: false |
Always visible | Replaces built-in errors with a custom error code/message when enabled. |
Unified Error CodeerrorUnifyCode |
textDefault: "" (empty) |
When errorUnify is true |
Custom error code when Unify Errors is enabled. |
Unified Error MessageerrorUnifyMessage |
textDefault: "" (empty) |
When errorUnify is true |
Custom error message when Unify Errors is enabled. |
Port I/O Specification
See Ports & Connections for general wiring rules, data type colors, and dynamic port creation.
Input Ports
Port Nature: Fixed Input Ports (5 total; conditionally visible ports depend on parameter settings as shown below).
| Port Name | Port ID | Data Type | Required | Visibility Condition | Description |
|---|---|---|---|---|---|
Key |
key |
Bytes |
Required | Always visible | 16-byte (128-bit) SM4 secret key. |
Plaintext |
plaintext |
Bytes |
Required | Always visible | Plaintext bytes (Uint8Array) to encrypt. |
IV |
iv |
Bytes |
Optional | When mode is CBC or CTR |
16-byte IV for CBC, CTR, CFB, and OFB modes (defaults to 16 zero bytes if unconnected). |
Nonce |
nonce |
Bytes |
Optional | When mode is GCM |
12-byte AEAD nonce for GCM mode (defaults to 12 zero bytes if unconnected). |
AAD |
aad |
Bytes |
Optional | When mode is GCM |
Optional Additional Authenticated Data for GCM mode. |
Output Ports
Port Nature: Fixed Output Ports (2 total; conditionally visible ports depend on parameter settings as shown below).
| Port Name | Port ID | Data Type | Visibility Condition | Description |
|---|---|---|---|---|
Ciphertext |
ciphertext |
Bytes |
Always visible | Encrypted output bytes (Uint8Array). |
Auth Tag |
tag |
Bytes |
When mode is GCM |
16-byte authentication tag produced in GCM mode. |
Error Cases
When any of the following conditions occurs at runtime, this node transitions to an ERROR state and downstream nodes connected to its outputs do not execute. Enabling Unify Errors (errorUnify) replaces all error codes below with your configured unified error code and message.
| Error Code | Default Engine Message | Trigger Condition |
|---|---|---|
INVALID_KEY_LENGTH |
Invalid key length | Raised when Key is not exactly 16 bytes. |
INVALID_IV_LENGTH |
Invalid IV length | Raised when a connected IV is not 16 bytes or Nonce is not 12 bytes. |
PLAINTEXT_NOT_BLOCK_ALIGNED |
In NoPad mode the plaintext length must be a multiple of the block size | Raised in ECB/CBC mode when Padding = None and Plaintext length is not a multiple of 16 bytes. |
INVALID_NONCE_LENGTH |
Invalid nonce length | Raised when a connected AEAD or stream-cipher Nonce is not 12 bytes (96 bits). |
Examples
Encrypting with SM4-GCM (RFC 8998)
Perform authenticated encryption using a 16-byte SM4 key and 12-byte nonce.
- Set Cipher Mode to
GCM. - Connect a 16-byte key to
Key, a 12-byte nonce toNonce, and your data toPlaintext.