sm2: fix RecoverPublicKeysFromSM2Signature

This commit is contained in:
Sun Yimin 2024-08-13 13:34:51 +08:00 committed by GitHub
parent 4c7cf989c7
commit 4517d00cc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 3 deletions

View File

@ -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)
compressFlags := []byte{compressed02, compressed03}
// Rx has one or two possible values, so point R has two or four possible values
for _, x := range pointRx {

View File

@ -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
@ -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 {