fix defect

This commit is contained in:
Sun Yimin 2022-08-25 12:54:59 +08:00 committed by GitHub
parent eedd5ebc2b
commit 7ebdf00640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 8 deletions

View File

@ -337,8 +337,7 @@ func Encrypt(random io.Reader, pub *ecdsa.PublicKey, msg []byte, opts *Encrypter
//A5, calculate t=KDF(x2||y2, klen) //A5, calculate t=KDF(x2||y2, klen)
var kdfCount int = 0 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)
success := subtle.ConstantTimeAllZero(c2) if subtle.ConstantTimeAllZero(c2) {
if !success {
kdfCount++ kdfCount++
if kdfCount > maxRetryLimit { if kdfCount > 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", kdfCount)
@ -399,8 +398,7 @@ func rawDecrypt(priv *PrivateKey, x1, y1 *big.Int, c2, c3 []byte) ([]byte, error
x2, y2 := curve.ScalarMult(x1, y1, priv.D.Bytes()) x2, y2 := curve.ScalarMult(x1, y1, priv.D.Bytes())
msgLen := len(c2) msgLen := len(c2)
msg := kdf.Kdf(sm3.New(), append(toBytes(curve, x2), toBytes(curve, y2)...), msgLen) msg := kdf.Kdf(sm3.New(), append(toBytes(curve, x2), toBytes(curve, y2)...), msgLen)
success := subtle.ConstantTimeAllZero(c2) if subtle.ConstantTimeAllZero(c2) {
if !success {
return nil, errors.New("sm2: invalid cipher text") return nil, errors.New("sm2: invalid cipher text")
} }

View File

@ -228,8 +228,7 @@ func WrapKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte,
buffer = append(buffer, uid...) buffer = append(buffer, uid...)
key = kdf.Kdf(sm3.New(), buffer, kLen) key = kdf.Kdf(sm3.New(), buffer, kLen)
ok = subtle.ConstantTimeAllZero(key) if !subtle.ConstantTimeAllZero(key) {
if ok {
break break
} }
} }
@ -300,8 +299,7 @@ func UnwrapKey(priv *EncryptPrivateKey, uid []byte, cipher *bn256.G1, kLen int)
buffer = append(buffer, uid...) buffer = append(buffer, uid...)
key := kdf.Kdf(sm3.New(), buffer, kLen) key := kdf.Kdf(sm3.New(), buffer, kLen)
ok := subtle.ConstantTimeAllZero(key) if subtle.ConstantTimeAllZero(key) {
if !ok {
return nil, errors.New("sm9: invalid cipher") return nil, errors.New("sm9: invalid cipher")
} }
return key, nil return key, nil