Skip to content

Commit

Permalink
add checks in sm2 encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
AlverLyu committed Aug 2, 2019
1 parent 84f5ba9 commit fb9cefa
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sm2/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func Encrypt(pub *ecdsa.PublicKey, data []byte) ([]byte, error) {
copy(encryptData[32-len(x1.Bytes()):], x1.Bytes())
copy(encryptData[64-len(y1.Bytes()):], y1.Bytes())

if pub.X.Sign() == 0 && pub.Y.Sign() == 0 {
return nil, errors.New("invalid public key")
}
x2, y2 = c.ScalarMult(pub.X, pub.Y, k.Bytes())
x2y2 := make([]byte, 64)
copy(x2y2[32-len(x2.Bytes()):], x2.Bytes())
Expand Down Expand Up @@ -175,6 +178,12 @@ func Decrypt(priv *ecdsa.PrivateKey, encryptData []byte) ([]byte, error) {

x1 := new(big.Int).SetBytes(encryptData[:32])
y1 := new(big.Int).SetBytes(encryptData[32:64])
if x1.Sign() == 0 && y1.Sign() == 0 {
return nil, errors.New("C1 is infinity")
}
if !c.IsOnCurve(x1, y1) {
return nil, errors.New("C1 is not on curve")
}

x2, y2 := c.ScalarMult(x1, y1, priv.D.Bytes())
c2 := make([]byte, 64)
Expand Down

0 comments on commit fb9cefa

Please sign in to comment.