什么是国密算法

国密算法是中国自主研发的密码算法体系,由国家密码管理局制定标准。主要包括:

算法 类型 说明
SM2 非对称加密 基于椭圆曲线,替代 RSA
SM3 哈希算法 替代 SHA-256,输出 256 位
SM4 对称加密 替代 AES,128 位分组

注意:国密算法目前主要用于国内政务、金融等行业。部分国外浏览器和服务商尚未全面支持,如需兼容国际路线,建议同时部署 RSA/ECDSA 证书。

OpenSSL 3.0 国密支持

从 OpenSSL 3.0 开始,原生支持 SM2、SM3、SM4 算法,无需额外编译 gmSSL。

验证国密支持

1
2
openssl version
openssl ecparam -list_curves | grep SM2

输出示例:

1
2
OpenSSL 3.0.19 27 Jan 2026 (Library: OpenSSL 3.0.19)
SM2       : SM2 curve over a 256 bit prime field

生成国密证书

生成 SM2 私钥和自签名证书

1
2
3
4
5
6
# 生成 SM2 私钥
openssl ecparam -genkey -name SM2 -out sm2.key

# 生成自签名证书
openssl req -new -x509 -key sm2.key -out sm2.crt -days 365 \
  -subj "/C=CN/ST=Beijing/L=Beijing/O=Example/CN=example.com"

查看证书信息

1
openssl x509 -in sm2.crt -text -noout

关键输出:

1
2
Signature Algorithm: SM2-with-SM3
Public-Key: (256 bit)

生成 CSR(证书签名请求)

1
2
openssl req -new -key sm2.key -out sm2.csr \
  -subj "/C=CN/ST=Beijing/L=Beijing/O=Example/CN=example.com"

SM3 哈希计算

计算文件 SM3 哈希

1
openssl dgst -SM3 filename.txt

输出示例:

1
SM3(filename.txt)= a8f2e5...

计算字符串哈希

1
echo -n "Hello World" | openssl dgst -SM3

SM4 对称加密

文件加密

1
2
3
4
5
6
7
# 加密文件
openssl enc -SM4-ctr -pbkdf2 -salt -pass pass:your_password \
  -in plaintext.txt -out ciphertext.bin

# 解密文件
openssl enc -SM4-ctr -pbkdf2 -d -salt -pass pass:your_password \
  -in ciphertext.bin -out decrypted.txt

参数说明:

  • -SM4-ctr:SM4 CTR 模式
  • -pbkdf2:更安全的密钥派生
  • -salt:随机盐值
  • -pass:密码

验证加密结果

1
2
3
4
5
6
7
8
# 加密
echo "Secret Data" | openssl enc -SM4-ctr -pbkdf2 -salt -pass pass:test123 -a
# 输出:U2FsdGVkX19FnchLWkJo4LdJJed7TUbt5eP+Pg==

# 解密
echo "U2FsdGVkX19FnchLWkJo4LdJJed7TUbt5eP+Pg==" | openssl enc -SM4-ctr \
  -pbkdf2 -d -salt -pass pass:test123 -a
# 输出:Secret Data

Nginx 国密配置

前提条件

Nginx 需要使用支持国密的 OpenSSL 编译,或者使用预编译的 gmSSL 版本。

配置示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
    listen 443 ssl http2;
    server_name gmssl.example.com;

    # 国密证书(SM2)
    ssl_certificate /path/to/sm2.crt;
    ssl_certificate_key /path/to/sm2.key;

    # 国际证书(RSA/ECDSA)- 兼容不支持国密的浏览器
    ssl_certificate /path/to/rsa.crt;
    ssl_certificate_key /path/to/rsa.key;

    # SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    
    # 国密 cipher suite(需要 gmSSL 版本 Nginx)
    # ssl_ciphers GMSM1-SM3-SM4;

    # 推荐配置:同时支持国密和国际算法
    ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:
                GMSM1-SM3-SM4:!aNULL:!MD5;

    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    location / {
        root /var/www/html;
        index index.html;
    }
}

双证书配置

为实现最大兼容性,建议同时部署国密和国际证书:

1
2
3
4
5
6
# 证书顺序:国密证书在前,国际证书在后
ssl_certificate /path/to/sm2.crt;      # 国密
ssl_certificate_key /path/to/sm2.key;  # 国密私钥

ssl_certificate /path/to/rsa.crt;      # 国际 RSA
ssl_certificate_key /path/to/rsa.key;  # RSA 私钥

这样浏览器会根据自身能力自动选择:

  • 支持国密 → 使用 SM2/SM3/SM4
  • 不支持国密 → 回退到 RSA/ECDSA

验证国密 TLS 连接

使用 s_client 测试

1
2
3
# 测试 SM2 证书连接(需要支持国密的 OpenSSL)
openssl s_client -connect gmssl.example.com:443 \
  -sigalgs SM2withSM3 -cipher 'GMSM1-SM3-SM4'

检查证书链

1
openssl verify -CAfile ca.crt sm2.crt

国密算法安全优势

特性 说明
自主可控 知识产权自主,不受制于人
性能优化 在国产芯片上效率更高
合规要求 满足政务、金融行业法规
抗量子 算法设计考虑了未来量子计算威胁(相对)

注意事项

  1. 浏览器兼容性:主流国际浏览器(Chrome、Firefox)对国密支持有限
  2. 双向认证:国密更适合内部系统或政务平台
  3. 密钥管理:SM2 私钥同样需要安全存储,建议使用 HSM
  4. 算法强度:SM2 相当于 256 位 ECC,强度与 NIST P-256 相当

小结

国密算法为中国网络环境提供了自主可控的加密方案。通过 OpenSSL 3.0 可以轻松生成 SM2 证书、进行 SM3 哈希计算和 SM4 加密。在 Nginx 中配置时,建议采用双证书策略,兼顾国密和国际兼容性。


参考来源