一、标准清单

标准 内容
GM/T 0024-2014 SSL VPN 技术规范
GB/T 38636-2020 信息安全技术 传输层密码协议(TLCP)
GM/T 0003.3-2012 SM2 密钥交换协议
GM/T 0003.4-2012 SM2 公钥加密算法
GM/T 0009-2012 SM2 密码算法使用规范
RFC 8998 TLS 1.3 的 SM2/SM3/SM4 套件(延伸)

二、消息格式速查

ClientHello:

Text
1
2
3
4
5
6
version (2)
random (32)
session_id_len (1) + session_id
cipher_suites_len (2) + cipher_suites
compression_len (1) + methods
extensions_len (2) + extensions

ServerHello:

Text
1
2
3
4
5
6
version (2)
random (32)
session_id_len (1) + session_id
cipher_suite (2)
compression (1)
extensions_len (2) + extensions

Certificate:

Text
1
2
3
4
cert_list_len (3)
repeat:
cert_len (3)
cert_der

ServerKeyExchange(ECC):

Text
1
2
sig_len (2)
sig (SM2 DER)

ServerKeyExchange(ECDHE):

Text
1
2
3
4
5
6
7
ServerECDHEParams:
curve_type (1) = 03
named_curve (2) = 0029
pubkey_len (1) = 41
pubkey (65) = 04 || x || y
sig_len (2)
sig (SM2 DER)

ClientKeyExchange(ECC):

Text
1
2
length (2)
SM2 ciphertext DER

ClientKeyExchange(ECDHE):

Text
1
2
3
4
5
6
length (2)
ClientECDHEParams:
curve_type (1) = 03
named_curve (2) = 0029
pubkey_len (1) = 41
pubkey (65)

CertificateRequest:

Text
1
2
3
certificate_types_len (1) + types
sig_algs_len (2) + sig_algs
ca_names_len (2) + ca_names

CertificateVerify:

Text
1
2
sig_len (2)
sig (SM2 DER)

三、常量速查

常量 值
TLCP 版本 0x0101
ECDHE_SM4_CBC_SM3 0xE011
ECC_SM4_CBC_SM3 0xE013
ECDHE_SM4_GCM_SM3 0xE051
ECC_SM4_GCM_SM3 0xE053
curveSM2 41(0x0029)
SM2 默认 ID 1234567812345678
预主密钥长度 48 字节
主密钥长度 48 字节
CBC key_block 96 字节
GCM key_block 40 字节
SM4 分组/密钥 16 字节
HMAC-SM3 输出 32 字节
GCM tag 16 字节

四、公式速查

SM2 加密:

$\begin{aligned}
C_1 &= [k]G \
(x_2, y_2) &= [k]P \
t &= \mathrm{KDF}(x_2 \parallel y_2,\ \lvert M \rvert) \
C_2 &= M \oplus t \
C_3 &= \mathrm{SM3}(x_2 \parallel M \parallel y_2)
\end{aligned}$

SM2 解密:

$\begin{aligned}
(x_2, y_2) &= [d]C_1 \
t &= \mathrm{KDF}(x_2 \parallel y_2,\ \lvert C_2 \rvert) \
M &= C_2 \oplus t \
\mathrm{SM3}(x_2 \parallel M \parallel y_2) &= C_3
\end{aligned}$

SM2 密钥交换:

$\begin{aligned}
x_{1_bar} &= 2^w + (x_1 \mathbin{&} (2^w - 1)) \
x_{2_bar} &= 2^w + (x_2 \mathbin{&} (2^w - 1)) \
t_A &= (d_A + x_{1_bar}r_A) \bmod n \
t_B &= (d_B + x_{2_bar}r_B) \bmod n \
U &= [h t_A](P_B + [x_{2_bar}]R_B) \
V &= [h t_B](P_A + [x_{1_bar}]R_A) \
U &= V = [h t_A t_B]G \
&\quad \mathrm{KDF}(x_U \parallel y_U \parallel Z_A \parallel Z_B,\ 48)
\end{aligned}$

PRF-SM3:

$\begin{aligned}
A(0) &= \mathrm{seed} \
A(i) &= \operatorname{HMAC-SM3}(\mathrm{secret}, A(i-1)) \
\mathrm{P_hash} &= \operatorname{HMAC-SM3}(\mathrm{secret}, A(1)\parallel\mathrm{seed}) \parallel \cdots
\end{aligned}$

key_block 布局:

$\begin{aligned}
\mathrm{CBC}: &\ \mathrm{client_MAC}(32) \parallel \mathrm{server_MAC}(32) \parallel \mathrm{client_key}(16) \parallel \mathrm{server_key}(16) \
\mathrm{GCM}: &\ \mathrm{client_key}(16) \parallel \mathrm{server_key}(16) \parallel \mathrm{client_iv}(4) \parallel \mathrm{server_iv}(4)
\end{aligned}$