sm2: update document

This commit is contained in:
Sun Yimin 2025-01-17 13:54:16 +08:00 committed by GitHub
parent 50a5e49d38
commit 80a264c995
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View File

@ -186,6 +186,13 @@ func ExampleVerifyASN1WithSM2() {
} }
``` ```
### 如何处理不用UID的签名、验签
#### 签名
也是使用sm2私钥的`Sign`方法,只是```SignerOpts```传入`nil`或者其它非`SM2SignerOption`即可,那么,你自己负责预先计算杂凑值,当然如何计算杂凑值,由你自己说了算了。
#### 验签
调用`sm2.VerifyASN1`方法,同样,你自己负责预先计算杂凑值,确保杂凑算法和签名时使用的杂凑算法保持一致。
## 密钥交换协议 ## 密钥交换协议
这里有两个实现一个是传统实现位于sm2包中另外一个参考最新go语言的实现在ecdh包中。在这里不详细介绍使用方法一般只有tls/tlcp才会用到普通应用通常不会涉及这一块感兴趣的话可以参考github.com/Trisia/gotlcp中的应用。 这里有两个实现一个是传统实现位于sm2包中另外一个参考最新go语言的实现在ecdh包中。在这里不详细介绍使用方法一般只有tls/tlcp才会用到普通应用通常不会涉及这一块感兴趣的话可以参考github.com/Trisia/gotlcp中的应用。

View File

@ -213,7 +213,7 @@ var defaultUID = []byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x
// CalculateZA ZA = H256(ENTLA || IDA || a || b || xG || yG || xA || yA). // CalculateZA ZA = H256(ENTLA || IDA || a || b || xG || yG || xA || yA).
// Compliance with GB/T 32918.2-2016 5.5. // Compliance with GB/T 32918.2-2016 5.5.
// //
// This function will not use default UID even the uid argument is empty. // This function will NOT use default UID even the uid argument is empty.
// Reference: GM/T 0009-2023 Chapter 8.1. // Reference: GM/T 0009-2023 Chapter 8.1.
func CalculateZA(pub *ecdsa.PublicKey, uid []byte) ([]byte, error) { func CalculateZA(pub *ecdsa.PublicKey, uid []byte) ([]byte, error) {
uidLen := len(uid) uidLen := len(uid)
@ -451,7 +451,7 @@ var ErrInvalidSignature = errors.New("sm2: invalid signature")
// //
// Compliance with GB/T 32918.2-2016 regardless it's SM2 curve or not. // Compliance with GB/T 32918.2-2016 regardless it's SM2 curve or not.
// Caller should make sure the hash's correctness, in other words, // Caller should make sure the hash's correctness, in other words,
// the caller must pre-calculate the hash value. // the caller must pre-compute the hash value.
func VerifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool { func VerifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool {
switch pub.Curve.Params() { switch pub.Curve.Params() {
case P256().Params(): case P256().Params():