文章

SM2密码算法加密签名消息语法规范

SM2密码算法加密签名消息语法规范

该文记录对 SM2 密码算法加密签名消息语法规范的理解。

SM2密码算法加密签名消息语法规范

  • GM/T 0010-2012 SM2 密码算法加密签名消息语法规范

1. OID 定义

对象标识符 OID对象标识符定义 
1.2.156.10197.6.1.4.2SM2密码算法加密签名消息语法规范 
1.2.156.10197.6.1.4.2.1数据类型 data 
1.2.156.10197.6.1.4.2.2签名数据类型 signedData 
1.2.156.10197.6.1.4.2.3数字信封数据类型 envelopedData 
1.2.156.10197.6.1.4.2.4签名及数字信封数据类型 signedAndEnvelopedData 
1.2.156.10197.6.1.4.2.5加密数据类型 encryptedData 
1.2.156.10197.6.1.4.2.6密钥协商类型 keyAgreementInfo 

2. 类型定义

2.1. 内容交换通用语法结构

1
2
3
4
ContentInfo ::= SEQUENCE {
    contentType ContentType,
    content[0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
}
  • ContentType: 是上述 OID 定义
  • content[0]: 是下面类型数据

3. 签名数据类型

graph LR
    signedData[签名数据] --> version[版本号]
    signedData --> digestAlgorithms[消息摘要算法标识集合]
    signedData --> contentInfo[被签名的数据内容]
    signedData --> certificates
    signedData --> crls
    signedData --> signerInfos[签名者信息集合]
    signerInfos --> si_version[语法版本号]
    signerInfos --> si_issuerAndSerialNumber[证书颁发者DN和ID]
    signerInfos --> si_digestAlgorithm[SM3摘要算法]
    signerInfos --> si_authenticatedAttributes
    signerInfos --> si_digestEncryptionAlgorithm[SM2-1 椭圆曲线]
    signerInfos --> si_encryptedDigest[签名者私钥签名结果]
    signerInfos --> si_unauthenticatedAttributes
  • 证书集合:可选
  • 证书吊销列表集合:可选
  • 签名者签名属性:可选
  • 未签名者签名属性:可选

4. 数字信封数据类型

graph LR
    EnvelopedData[数字信封数据] --> version[版本号]
    EnvelopedData --> recipientInfos[接收者集合]
    EnvelopedData --> encryptedContentInfo[加密信息]
    recipientInfos --> ri_version[语法版本]
    recipientInfos --> ri_issuerAndSerialNumber[证书颁发者DN和ID]
    recipientInfos --> ri_keyEncryptionAlgorithm[SM2-3椭圆曲线加密算法]
    recipientInfos --> ri_encryptedKey[数据加密密文]
    encryptedContentInfo --> contentType[内容类型]
    encryptedContentInfo --> contentEncryptionAlgorithm[内容加密算法]
    encryptedContentInfo --> encryptedContent
    encryptedContentInfo --> sharedInfo1
    encryptedContentInfo --> sharedInfo2
  • 内容加密结果:可选
  • 协商好的共享信息1:可选
  • 协商好的共享信息2:可选

5. 签名及数字信封数据类型

graph LR
    SignedAndEnvelopedData[签名及数字信封] --> version[版本号]
    SignedAndEnvelopedData --> recipientInfos[接受者信息集合]
    SignedAndEnvelopedData --> digestAlgorithms[消息摘要算法标识集合]
    SignedAndEnvelopedData --> encryptedContentInfo[加密信息]
    SignedAndEnvelopedData --> certificates
    SignedAndEnvelopedData --> crls
    SignedAndEnvelopedData --> signerInfos[签名者信息集合]
    encryptedContentInfo --> contentType[内容类型]
    encryptedContentInfo --> contentEncryptionAlgorithm[内容加密算法]
    encryptedContentInfo --> encryptedContent
    encryptedContentInfo --> sharedInfo1
    encryptedContentInfo --> sharedInfo2
    signerInfos --> si_version[语法版本号]
    signerInfos --> si_issuerAndSerialNumber[证书颁发者DN和ID]
    signerInfos --> si_digestAlgorithm[SM3摘要算法]
    signerInfos --> si_authenticatedAttributes
    signerInfos --> si_digestEncryptionAlgorithm[SM2-1 椭圆曲线]
    signerInfos --> si_encryptedDigest[签名者私钥签名结果]
    signerInfos --> si_unauthenticatedAttributes

同上

6. 加密数据类型

graph LR
    EncryptedData[加密数据] --> version[版本号]
    EncryptedData --> encryptedContentInfo[加密信息]

7. 密钥协商类型

graph LR
    KeyAgreemenntInfo[密钥协商] --> version[版本号]
    KeyAgreemenntInfo --> tempPublicKeyR[临时公钥]
    KeyAgreemenntInfo --> userCertificate[用户证书]
    KeyAgreemenntInfo --> userID[用户标识]

8. 代码实现

https://github.com/august295/EnDeCode

参考

本文由作者按照 CC BY 4.0 进行授权