Skip to content

Commit 86a3cd7

Browse files
committed
Fixed pr comments
1 parent 7833f8c commit 86a3cd7

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

pss/crypto/crypto.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func (crypto *defaultCryptoBackend) GenerateSymKey() (string, error) {
349349
key, err := crypto.generateSecureRandomData(aesKeyLength)
350350
if err != nil {
351351
return "", err
352-
} else if !crypto.validateDataIntegrity(key, aesKeyLength) {
352+
} else if !validateDataIntegrity(key, aesKeyLength) {
353353
return "", fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data")
354354
}
355355

@@ -452,7 +452,7 @@ func (crypto *defaultCryptoBackend) decryptAsymmetric(rawBytes []byte, key *ecds
452452
}
453453

454454
func (crypto *defaultCryptoBackend) encryptSymmetric(rawBytes []byte, key []byte) ([]byte, error) {
455-
if !crypto.validateDataIntegrity(key, aesKeyLength) {
455+
if !validateDataIntegrity(key, aesKeyLength) {
456456
return nil, errInvalidSymkey
457457
}
458458
block, err := aes.NewCipher(key)
@@ -489,7 +489,7 @@ func (crypto *defaultCryptoBackend) generateRandomID() (id string, err error) {
489489
if err != nil {
490490
return "", err
491491
}
492-
if !crypto.validateDataIntegrity(buf, keyIDSize) {
492+
if !validateDataIntegrity(buf, keyIDSize) {
493493
return "", fmt.Errorf("error in generateRandomID: crypto/rand failed to generate random data")
494494
}
495495
id = common.Bytes2Hex(buf)
@@ -514,19 +514,19 @@ func (crypto *defaultCryptoBackend) generateSecureRandomData(length int) ([]byte
514514
_, err := crand.Read(x)
515515
if err != nil {
516516
return nil, err
517-
} else if !crypto.validateDataIntegrity(x, length) {
517+
} else if !validateDataIntegrity(x, length) {
518518
return nil, errSecureRandomData
519519
}
520520
_, err = mrand.Read(y)
521521
if err != nil {
522522
return nil, err
523-
} else if !crypto.validateDataIntegrity(y, length) {
523+
} else if !validateDataIntegrity(y, length) {
524524
return nil, errSecureRandomData
525525
}
526526
for i := 0; i < length; i++ {
527527
res[i] = x[i] ^ y[i]
528528
}
529-
if !crypto.validateDataIntegrity(res, length) {
529+
if !validateDataIntegrity(res, length) {
530530
return nil, errSecureRandomData
531531
}
532532
return res, nil
@@ -536,24 +536,12 @@ func (crypto *defaultCryptoBackend) importECDSAPublic(key *ecdsa.PublicKey) *eci
536536
return ecies.ImportECDSAPublic(key)
537537
}
538538

539-
// validateDataIntegrity returns false if the data have the wrong or contains all zeros,
540-
// which is the simplest and the most common bug.
541-
func (crypto *defaultCryptoBackend) validateDataIntegrity(k []byte, expectedSize int) bool {
542-
if len(k) != expectedSize {
543-
return false
544-
}
545-
if expectedSize > 3 && containsOnlyZeros(k) {
546-
return false
547-
}
548-
return true
549-
}
550-
551-
// CryptoUtils
552-
553539
func (crypto *defaultCryptoBackend) GenerateKey() (*ecdsa.PrivateKey, error) {
554540
return ethCrypto.GenerateKey()
555541
}
556542

543+
// CryptoUtils
544+
557545
// NewKeyPair generates a new cryptographic identity for the client, and injects
558546
// it into the known identities for message decryption. Returns ID of the new key pair.
559547
func (crypto *defaultCryptoBackend) NewKeyPair(ctx context.Context) (string, error) {
@@ -595,6 +583,18 @@ func (crypto *defaultCryptoBackend) GetPrivateKey(id string) (*ecdsa.PrivateKey,
595583

596584
// Util functions
597585

586+
// validateDataIntegrity returns false if the data have the wrong size or contains all zeros,
587+
// which is the simplest and the most common bug.
588+
func validateDataIntegrity(k []byte, expectedSize int) bool {
589+
if len(k) != expectedSize {
590+
return false
591+
}
592+
if expectedSize > 3 && containsOnlyZeros(k) {
593+
return false
594+
}
595+
return true
596+
}
597+
598598
// validatePrivateKey checks the format of the given private key.
599599
func validatePrivateKey(k *ecdsa.PrivateKey) bool {
600600
if k == nil || k.D == nil || k.D.Sign() == 0 {

0 commit comments

Comments
 (0)