Skip to content

Commit

Permalink
sm2: update document
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Jan 17, 2025
1 parent 50a5e49 commit 80a264c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/sm2.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ func ExampleVerifyASN1WithSM2() {
}
```

### 如何处理不用UID的签名、验签?
#### 签名
也是使用sm2私钥的`Sign`方法,只是```SignerOpts```传入`nil`或者其它非`SM2SignerOption`即可,那么,你自己负责预先计算杂凑值,当然如何计算杂凑值,由你自己说了算了。

#### 验签
调用`sm2.VerifyASN1`方法,同样,你自己负责预先计算杂凑值,确保杂凑算法和签名时使用的杂凑算法保持一致。

## 密钥交换协议
这里有两个实现,一个是传统实现,位于sm2包中;另外一个参考最新go语言的实现在ecdh包中。在这里不详细介绍使用方法,一般只有tls/tlcp才会用到,普通应用通常不会涉及这一块,感兴趣的话可以参考github.com/Trisia/gotlcp中的应用。

Expand Down
4 changes: 2 additions & 2 deletions sm2/sm2_dsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
// 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.
func CalculateZA(pub *ecdsa.PublicKey, uid []byte) ([]byte, error) {
uidLen := len(uid)
Expand Down Expand Up @@ -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.
// 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 {
switch pub.Curve.Params() {
case P256().Params():
Expand Down

0 comments on commit 80a264c

Please sign in to comment.