Skip to content

Commit

Permalink
sm2: fix RecoverPublicKeysFromSM2Signature
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Aug 13, 2024
1 parent 4c7cf98 commit 4517d00
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sm2/sm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func RecoverPublicKeysFromSM2Signature(hash, sig []byte) ([]*ecdsa.PublicKey, er
pointRx = append(pointRx, s)
}
pubs := make([]*ecdsa.PublicKey, 0, 4)
bytes := make([]byte, len(rBytes)+1)
bytes := make([]byte, len(32)+1)

Check failure on line 765 in sm2/sm2.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, arm64)

invalid argument: 32 (untyped int constant) for len

Check failure on line 765 in sm2/sm2.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, arm64)

invalid argument: 32 (untyped int constant) for len

Check failure on line 765 in sm2/sm2.go

View workflow job for this annotation

GitHub Actions / build (1.19)

invalid argument: 32 (untyped int constant) for len

Check failure on line 765 in sm2/sm2.go

View workflow job for this annotation

GitHub Actions / build (1.21)

invalid argument: 32 (untyped int constant) for len

Check failure on line 765 in sm2/sm2.go

View workflow job for this annotation

GitHub Actions / build (1.22)

invalid argument: 32 (untyped int constant) for len
compressFlags := []byte{compressed02, compressed03}
// Rx has one or two possible values, so point R has two or four possible values
for _, x := range pointRx {
Expand Down
35 changes: 33 additions & 2 deletions sm2/sm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ func TestSignVerify(t *testing.T) {
}
}

func TestRecoverPublicKeysFromSM2Signature(t *testing.T) {
priv, _ := GenerateKey(rand.Reader)
func testRecoverPublicKeysFromSM2Signature(t *testing.T, priv *PrivateKey) {
tests := []struct {
name string
plainText string
Expand Down Expand Up @@ -511,6 +510,38 @@ func TestRecoverPublicKeysFromSM2Signature(t *testing.T) {
}
}

func TestRecoverPublicKeysFromSM2Signature(t *testing.T) {
priv, _ := GenerateKey(rand.Reader)
testRecoverPublicKeysFromSM2Signature(t, priv)
keyInt := bigFromHex("d6833540d019e0438a5dd73b414f26ab43d8064b99671206944e284dbd969093")
priv, _ = NewPrivateKeyFromInt(keyInt)
testRecoverPublicKeysFromSM2Signature(t, priv)

// failed case
hashValue, _ := CalculateSM2Hash(&priv.PublicKey, []byte("encryption standard encryption "), nil)
signature, _ := hex.DecodeString("3045022000cd0b56bf6be810032d28ff27d6f3468f1f1a09bcf8581f30a5de6692c85ea602210096ba29c086134af1be139dd572f2f2908f30e01fd0c28e06a687cbb0ff6e33ce")
// verify signature with public key
if !VerifyASN1(&priv.PublicKey, hashValue, signature) {
t.Errorf("failed to verify hash for sig=%x, priv=%x", signature, priv.D.Bytes())
}
pubs, err := RecoverPublicKeysFromSM2Signature(hashValue, signature)
if err != nil {
t.Fatalf("recover failed %v", err)
}
found := false
for _, pub := range pubs {
if !VerifyASN1(pub, hashValue, signature) {
t.Errorf("failed to verify hash for sig=%x, priv=%x", signature, priv.D.Bytes())
}
if pub.Equal(&priv.PublicKey) {
found = true
}
}
if !found {
t.Errorf("recover failed, not found public key for sig=%x, priv=%x", signature, priv.D.Bytes())
}
}

func TestSignVerifyLegacy(t *testing.T) {
priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
tests := []struct {
Expand Down

1 comment on commit 4517d00

@emmansun
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.