@@ -349,7 +349,7 @@ func (crypto *defaultCryptoBackend) GenerateSymKey() (string, error) {
349
349
key , err := crypto .generateSecureRandomData (aesKeyLength )
350
350
if err != nil {
351
351
return "" , err
352
- } else if ! crypto . validateDataIntegrity (key , aesKeyLength ) {
352
+ } else if ! validateDataIntegrity (key , aesKeyLength ) {
353
353
return "" , fmt .Errorf ("error in GenerateSymKey: crypto/rand failed to generate random data" )
354
354
}
355
355
@@ -452,7 +452,7 @@ func (crypto *defaultCryptoBackend) decryptAsymmetric(rawBytes []byte, key *ecds
452
452
}
453
453
454
454
func (crypto * defaultCryptoBackend ) encryptSymmetric (rawBytes []byte , key []byte ) ([]byte , error ) {
455
- if ! crypto . validateDataIntegrity (key , aesKeyLength ) {
455
+ if ! validateDataIntegrity (key , aesKeyLength ) {
456
456
return nil , errInvalidSymkey
457
457
}
458
458
block , err := aes .NewCipher (key )
@@ -489,7 +489,7 @@ func (crypto *defaultCryptoBackend) generateRandomID() (id string, err error) {
489
489
if err != nil {
490
490
return "" , err
491
491
}
492
- if ! crypto . validateDataIntegrity (buf , keyIDSize ) {
492
+ if ! validateDataIntegrity (buf , keyIDSize ) {
493
493
return "" , fmt .Errorf ("error in generateRandomID: crypto/rand failed to generate random data" )
494
494
}
495
495
id = common .Bytes2Hex (buf )
@@ -514,19 +514,19 @@ func (crypto *defaultCryptoBackend) generateSecureRandomData(length int) ([]byte
514
514
_ , err := crand .Read (x )
515
515
if err != nil {
516
516
return nil , err
517
- } else if ! crypto . validateDataIntegrity (x , length ) {
517
+ } else if ! validateDataIntegrity (x , length ) {
518
518
return nil , errSecureRandomData
519
519
}
520
520
_ , err = mrand .Read (y )
521
521
if err != nil {
522
522
return nil , err
523
- } else if ! crypto . validateDataIntegrity (y , length ) {
523
+ } else if ! validateDataIntegrity (y , length ) {
524
524
return nil , errSecureRandomData
525
525
}
526
526
for i := 0 ; i < length ; i ++ {
527
527
res [i ] = x [i ] ^ y [i ]
528
528
}
529
- if ! crypto . validateDataIntegrity (res , length ) {
529
+ if ! validateDataIntegrity (res , length ) {
530
530
return nil , errSecureRandomData
531
531
}
532
532
return res , nil
@@ -536,24 +536,12 @@ func (crypto *defaultCryptoBackend) importECDSAPublic(key *ecdsa.PublicKey) *eci
536
536
return ecies .ImportECDSAPublic (key )
537
537
}
538
538
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
-
553
539
func (crypto * defaultCryptoBackend ) GenerateKey () (* ecdsa.PrivateKey , error ) {
554
540
return ethCrypto .GenerateKey ()
555
541
}
556
542
543
+ // CryptoUtils
544
+
557
545
// NewKeyPair generates a new cryptographic identity for the client, and injects
558
546
// it into the known identities for message decryption. Returns ID of the new key pair.
559
547
func (crypto * defaultCryptoBackend ) NewKeyPair (ctx context.Context ) (string , error ) {
@@ -595,6 +583,18 @@ func (crypto *defaultCryptoBackend) GetPrivateKey(id string) (*ecdsa.PrivateKey,
595
583
596
584
// Util functions
597
585
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
+
598
598
// validatePrivateKey checks the format of the given private key.
599
599
func validatePrivateKey (k * ecdsa.PrivateKey ) bool {
600
600
if k == nil || k .D == nil || k .D .Sign () == 0 {
0 commit comments