mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 04:06:18 +08:00
fix bug
This commit is contained in:
parent
096578c771
commit
cf6a25bf31
@ -320,6 +320,7 @@ func Encrypt(random io.Reader, pub *ecdsa.PublicKey, msg []byte, opts *Encrypter
|
|||||||
if pub.X.Sign() == 0 && pub.Y.Sign() == 0 {
|
if pub.X.Sign() == 0 && pub.Y.Sign() == 0 {
|
||||||
return nil, errors.New("sm2: invalid public key")
|
return nil, errors.New("sm2: invalid public key")
|
||||||
}
|
}
|
||||||
|
var retryCount int = 0
|
||||||
for {
|
for {
|
||||||
//A1, generate random k
|
//A1, generate random k
|
||||||
k, err := randFieldElement(curve, random)
|
k, err := randFieldElement(curve, random)
|
||||||
@ -335,12 +336,11 @@ func Encrypt(random io.Reader, pub *ecdsa.PublicKey, msg []byte, opts *Encrypter
|
|||||||
x2, y2 := curve.ScalarMult(pub.X, pub.Y, k.Bytes())
|
x2, y2 := curve.ScalarMult(pub.X, pub.Y, k.Bytes())
|
||||||
|
|
||||||
//A5, calculate t=KDF(x2||y2, klen)
|
//A5, calculate t=KDF(x2||y2, klen)
|
||||||
var kdfCount int = 0
|
|
||||||
c2 := kdf.Kdf(sm3.New(), append(toBytes(curve, x2), toBytes(curve, y2)...), msgLen)
|
c2 := kdf.Kdf(sm3.New(), append(toBytes(curve, x2), toBytes(curve, y2)...), msgLen)
|
||||||
if subtle.ConstantTimeAllZero(c2) {
|
if subtle.ConstantTimeAllZero(c2) {
|
||||||
kdfCount++
|
retryCount++
|
||||||
if kdfCount > maxRetryLimit {
|
if retryCount > maxRetryLimit {
|
||||||
return nil, fmt.Errorf("sm2: A5, failed to calculate valid t, tried %v times", kdfCount)
|
return nil, fmt.Errorf("sm2: A5, failed to calculate valid t, tried %v times", retryCount)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user