mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
whisper: key generation updated
This commit is contained in:
parent
7231c605cc
commit
74a900ac41
2 changed files with 22 additions and 4 deletions
|
|
@ -130,7 +130,7 @@ func (msg *SentMessage) appendPadding(params *MessageParams) {
|
||||||
panic("please fix the padding algorithm before releasing new version")
|
panic("please fix the padding algorithm before releasing new version")
|
||||||
}
|
}
|
||||||
buf := make([]byte, padSize)
|
buf := make([]byte, padSize)
|
||||||
randomize(buf[1:]) // change to: err = mrand.Read(buf[1:])
|
randomize(buf[1:])
|
||||||
buf[0] = byte(padSize)
|
buf[0] = byte(padSize)
|
||||||
if params.Padding != nil {
|
if params.Padding != nil {
|
||||||
copy(buf[1:], params.Padding)
|
copy(buf[1:], params.Padding)
|
||||||
|
|
@ -208,7 +208,10 @@ func (msg *SentMessage) encryptSymmetric(key []byte) (salt []byte, nonce []byte,
|
||||||
_, err = crand.Read(nonce)
|
_, err = crand.Read(nonce)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
} else if !validateSymmetricKey(nonce) {
|
||||||
|
return nil, nil, errors.New("crypto/rand failed to generate nonce")
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Raw = aesgcm.Seal(nil, nonce, msg.Raw, nil)
|
msg.Raw = aesgcm.Seal(nil, nonce, msg.Raw, nil)
|
||||||
return salt, nonce, nil
|
return salt, nonce, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
mrand "math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -82,6 +83,9 @@ func NewWhisper(server MailServer) *Whisper {
|
||||||
Run: whisper.HandlePeer,
|
Run: whisper.HandlePeer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
seed := time.Now().Unix()
|
||||||
|
mrand.Seed(seed)
|
||||||
|
|
||||||
return whisper
|
return whisper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,12 +181,23 @@ func (w *Whisper) GetIdentity(pubKey string) *ecdsa.PrivateKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) GenerateSymKey(name string) error {
|
func (w *Whisper) GenerateSymKey(name string) error {
|
||||||
buf := make([]byte, aesKeyLength*2)
|
const size = aesKeyLength * 2
|
||||||
_, err := crand.Read(buf) // todo: check how safe is this function
|
buf := make([]byte, size)
|
||||||
|
buf2 := make([]byte, size)
|
||||||
|
_, err := crand.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !validateSymmetricKey(buf) {
|
} else if !validateSymmetricKey(buf) {
|
||||||
return fmt.Errorf("crypto/rand failed to generate random data")
|
return fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data")
|
||||||
|
}
|
||||||
|
|
||||||
|
randomize(buf2)
|
||||||
|
if !validateSymmetricKey(buf2) {
|
||||||
|
return fmt.Errorf("error in GenerateSymKey: math/rand failed to generate random data")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
buf[i] ^= buf2[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
key := buf[:aesKeyLength]
|
key := buf[:aesKeyLength]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue