This commit is contained in:
Sun Yimin 2022-11-01 15:38:28 +08:00 committed by GitHub
parent 096578c771
commit cf6a25bf31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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