mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 20:26:19 +08:00
sm2: fix RecoverPublicKeysFromSM2Signature
This commit is contained in:
parent
4c7cf989c7
commit
4517d00cc1
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user