Skip to content

Commit 79a58f0

Browse files
committed
more pr ethersphere#1731 comments
1 parent 245f28f commit 79a58f0

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

pss/crypto/crypto.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func (crypto *defaultCryptoBackend) GenerateSymKey() (string, error) {
372372
return "", fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data")
373373
}
374374

375-
id, err := generateRandomID()
375+
id, err := generateRandomKeyID()
376376
if err != nil {
377377
return "", fmt.Errorf("failed to generate ID: %s", err)
378378
}
@@ -387,13 +387,13 @@ func (crypto *defaultCryptoBackend) GenerateSymKey() (string, error) {
387387
return id, nil
388388
}
389389

390-
// Add a symmetric key ti the store generating an id and returning it
390+
// Add a symmetric key to the store generating an id and returning it
391391
func (crypto *defaultCryptoBackend) AddSymKey(key []byte) (string, error) {
392392
if len(key) != aesKeyLength {
393393
return "", fmt.Errorf("wrong key size: %d", len(key))
394394
}
395395

396-
id, err := generateRandomID()
396+
id, err := generateRandomKeyID()
397397
if err != nil {
398398
return "", fmt.Errorf("failed to generate ID: %s", err)
399399
}
@@ -460,7 +460,7 @@ func (crypto *defaultCryptoBackend) encryptAsymmetric(rawBytes []byte, key *ecds
460460
if !validatePublicKey(key) {
461461
return nil, errInvalidPubkey
462462
}
463-
encrypted, err := ecies.Encrypt(crand.Reader, crypto.importECDSAPublic(key), rawBytes, nil, nil)
463+
encrypted, err := ecies.Encrypt(crand.Reader, crypto.fromECDSAtoECIESPublic(key), rawBytes, nil, nil)
464464
if err == nil {
465465
return encrypted, nil
466466
}
@@ -497,25 +497,26 @@ func (crypto *defaultCryptoBackend) signHash(hash []byte, prv *ecdsa.PrivateKey)
497497
return ethCrypto.Sign(hash, prv)
498498
}
499499

500-
// Obtain public key from the signed message and the signature
500+
// sigToPub obtains public key from the signed message and the signature
501501
func (crypto *defaultCryptoBackend) sigToPub(signed, sig []byte) (*ecdsa.PublicKey, error) {
502502
defer func() { recover() }() // in case of invalid signature
503503
hash := crypto.keccak256(signed)
504504
return ethCrypto.SigToPub(hash, sig)
505505
}
506506

507-
func (crypto *defaultCryptoBackend) importECDSAPublic(key *ecdsa.PublicKey) *ecies.PublicKey {
507+
// fromECDSAtoECIESPublic converts a ecdsa public key to the format needed by the crypti/ecies package
508+
func (crypto *defaultCryptoBackend) fromECDSAtoECIESPublic(key *ecdsa.PublicKey) *ecies.PublicKey {
508509
return ecies.ImportECDSAPublic(key)
509510
}
510511

511-
// GenerateRandomID generates a random string, which is then returned to be used as a key id
512-
func generateRandomID() (id string, err error) {
512+
// generateRandomKeyID generates a random string, which is then returned to be used as a key id
513+
func generateRandomKeyID() (id string, err error) {
513514
buf, err := generateSecureRandomData(keyIDSize)
514515
if err != nil {
515516
return "", err
516517
}
517518
if !validateDataIntegrity(buf, keyIDSize) {
518-
return "", fmt.Errorf("error in generateRandomID: crypto/rand failed to generate random data")
519+
return "", fmt.Errorf("error in generateRandomKeyID: crypto/rand failed to generate random data")
519520
}
520521
id = common.Bytes2Hex(buf)
521522
return id, err

pss/crypto/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package crypto
1818
import (
1919
"crypto/ecdsa"
2020
"fmt"
21-
"sync"
22-
2321
ethCrypto "github.com/ethereum/go-ethereum/crypto"
22+
"github.com/ethereum/go-ethereum/whisper/whisperv6"
23+
"sync"
2424
)
2525

26-
// Utils contains utility methods for tests. Generates and stores asymmetric keys
26+
// Utils contains utility methods only for testing. Generates and stores asymmetric keys
2727
type Utils interface {
2828
GenerateKey() (*ecdsa.PrivateKey, error)
2929
NewKeyPair() (string, error)
@@ -32,7 +32,7 @@ type Utils interface {
3232

3333
type utils struct {
3434
privateKeys map[string]*ecdsa.PrivateKey // Private key storage
35-
keyMu sync.RWMutex // Mutex associated with private key storage1111111111111111111111111111111
35+
keyMu sync.RWMutex // Mutex associated with private key storage
3636
}
3737

3838
func NewUtils() Utils {
@@ -60,7 +60,7 @@ func (utils *utils) NewKeyPair() (string, error) {
6060
return "", fmt.Errorf("failed to generate valid key")
6161
}
6262

63-
id, err := generateRandomID()
63+
id, err := generateRandomKeyID()
6464
if err != nil {
6565
return "", fmt.Errorf("failed to generate ID: %s", err)
6666
}
@@ -77,6 +77,7 @@ func (utils *utils) NewKeyPair() (string, error) {
7777

7878
// GetPrivateKey return a PrivateKey previously generated in NewKeyPair by id
7979
func (utils *utils) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
80+
whisperv6.NewSentMessage()
8081
utils.keyMu.RLock()
8182
defer utils.keyMu.RUnlock()
8283
key := utils.privateKeys[id]

pss/keystore.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ func (ks *KeyStore) getPeerAddress(keyid string, topic Topic) (PssAddress, error
143143
}
144144

145145
// Attempt to decrypt, validate and unpack a symmetrically encrypted message.
146-
// If successful, returns the unpacked receivedMessage struct
147-
// encapsulating the decrypted message, and the id
146+
// If successful, returns the payload of the message and the id
148147
// of the symmetric key used to decrypt the message.
149-
// It fails if decryption of the message fails or if the message is corrupted.
148+
// It fails if decryption of the message fails or if the message is corrupted/not valid.
150149
func (ks *KeyStore) processSym(pssMsg *PssMsg) ([]byte, string, PssAddress, error) {
151150
metrics.GetOrRegisterCounter("pss.process.sym", nil).Inc(1)
152151

@@ -182,8 +181,7 @@ func (ks *KeyStore) processSym(pssMsg *PssMsg) ([]byte, string, PssAddress, erro
182181
}
183182

184183
// Attempt to decrypt, validate and unpack an asymmetrically encrypted message.
185-
// If successful, returns the unpacked receivedMessage struct
186-
// encapsulating the decrypted message, and the byte representation of
184+
// If successful, returns the payload of the message and the hex representation of
187185
// the public key used to decrypt the message.
188186
// It fails if decryption of message fails, or if the message is corrupted.
189187
func (p *Pss) processAsym(pssMsg *PssMsg) ([]byte, string, PssAddress, error) {

0 commit comments

Comments
 (0)