# `ExCEP.Crypto.SM4`

Pure Elixir implementation of the Chinese national block cipher **SM4** (GB/T 32907-2016).

Supports:
- Raw 128-bit ECB block encryption/decryption (`encrypt_block/2`, `decrypt_block/2`)
- **SM4-CBC** with PKCS#7 padding (`encrypt_cbc/3`, `decrypt_cbc/3`)
- **SM4-CTR** stream mode (`crypt_ctr/3`)

Implements `ExCEP.Transport.Cipher` for CSG CEP TS6.3 encrypted channels.

# `crypt_ctr`

```elixir
@spec crypt_ctr(binary(), &lt;&lt;_::128&gt;&gt;, &lt;&lt;_::128&gt;&gt;) :: binary()
```

Encrypts or decrypts binary `data` using SM4-CTR mode.

# `decrypt_block`

```elixir
@spec decrypt_block(&lt;&lt;_::128&gt;&gt;, &lt;&lt;_::128&gt;&gt;) :: &lt;&lt;_::128&gt;&gt;
```

Decrypts a single 16-byte block with a 16-byte SM4 key.

# `decrypt_cbc`

```elixir
@spec decrypt_cbc(binary(), &lt;&lt;_::128&gt;&gt;, &lt;&lt;_::128&gt;&gt;) ::
  {:ok, binary()} | {:error, term()}
```

Decrypts SM4-CBC ciphertext with PKCS#7 unpadding.

# `encrypt`

Encrypts `plaintext` with SM4.

## Options
- `:key` - 16-byte SM4 key (required)
- `:mode` - `:cbc` (default, prepends 16-byte random IV) or `:ctr` (prepends 16-byte random nonce)

# `encrypt_block`

```elixir
@spec encrypt_block(&lt;&lt;_::128&gt;&gt;, &lt;&lt;_::128&gt;&gt;) :: &lt;&lt;_::128&gt;&gt;
```

Encrypts a single 16-byte block with a 16-byte SM4 key.

# `encrypt_cbc`

```elixir
@spec encrypt_cbc(binary(), &lt;&lt;_::128&gt;&gt;, &lt;&lt;_::128&gt;&gt;) :: binary()
```

Encrypts binary `plaintext` using SM4-CBC with PKCS#7 padding.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
