mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
whisper: message format changed
This commit is contained in:
parent
cd73f59844
commit
fb12a49589
3 changed files with 13 additions and 81 deletions
|
|
@ -198,29 +198,25 @@ func (e *Envelope) OpenSymmetric(key []byte) (msg *ReceivedMessage, err error) {
|
||||||
|
|
||||||
// Open tries to decrypt an envelope, and populates the message fields in case of success.
|
// Open tries to decrypt an envelope, and populates the message fields in case of success.
|
||||||
func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) {
|
func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) {
|
||||||
// The API interface forbids filters doing both symmetric and
|
// The API interface forbids filters doing both symmetric and asymmetric encryption.
|
||||||
// asymmetric encryption.
|
|
||||||
if watcher.expectsAsymmetricEncryption() && watcher.expectsSymmetricEncryption() {
|
if watcher.expectsAsymmetricEncryption() && watcher.expectsSymmetricEncryption() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var symmetric bool
|
|
||||||
if watcher.expectsAsymmetricEncryption() {
|
if watcher.expectsAsymmetricEncryption() {
|
||||||
msg, _ = e.OpenAsymmetric(watcher.KeyAsym)
|
msg, _ = e.OpenAsymmetric(watcher.KeyAsym)
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
symmetric = false
|
|
||||||
msg.Dst = &watcher.KeyAsym.PublicKey
|
msg.Dst = &watcher.KeyAsym.PublicKey
|
||||||
}
|
}
|
||||||
} else if watcher.expectsSymmetricEncryption() {
|
} else if watcher.expectsSymmetricEncryption() {
|
||||||
msg, _ = e.OpenSymmetric(watcher.KeySym)
|
msg, _ = e.OpenSymmetric(watcher.KeySym)
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
symmetric = true
|
|
||||||
msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
ok := msg.ValidateAndParse(symmetric)
|
ok := msg.ValidateAndParse()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,22 +101,13 @@ func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
||||||
// appendPayloadSizeField appends the auxiliary field containing the size of payload
|
// appendPayloadSizeField appends the auxiliary field containing the size of payload
|
||||||
func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
||||||
fieldSize := getAuxFieldSize(payload)
|
fieldSize := getAuxFieldSize(payload)
|
||||||
field := make([]byte, fieldSize)
|
field := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
||||||
|
field = field[:fieldSize]
|
||||||
msg.Raw = append(msg.Raw, field...)
|
msg.Raw = append(msg.Raw, field...)
|
||||||
msg.Raw[0] |= byte(fieldSize)
|
msg.Raw[0] |= byte(fieldSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSizeOfLength returns the number of bytes necessary to encode the size of padding
|
|
||||||
//func getAuxFieldSize(payload []byte) (sz int, err error) {
|
|
||||||
// sz = intSize(len(b)) // first iteration
|
|
||||||
// sz = intSize(len(b) + sz) // second iteration
|
|
||||||
// if sz > 3 {
|
|
||||||
// err = errors.New("oversized padding parameter")
|
|
||||||
// }
|
|
||||||
// return sz, err
|
|
||||||
//}
|
|
||||||
|
|
||||||
// getAuxFieldSize returns the number of bytes necessary to encode the size of payload
|
// getAuxFieldSize returns the number of bytes necessary to encode the size of payload
|
||||||
func getAuxFieldSize(payload []byte) int {
|
func getAuxFieldSize(payload []byte) int {
|
||||||
s := 1
|
s := 1
|
||||||
|
|
@ -144,29 +135,7 @@ func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
||||||
rawSize += AESNonceLength
|
rawSize += AESNonceLength
|
||||||
}
|
}
|
||||||
odd := rawSize % padSizeLimit
|
odd := rawSize % padSizeLimit
|
||||||
|
|
||||||
//if len(params.Padding) != 0 {
|
|
||||||
// // padding data was provided by the Dapp, just use it as is
|
|
||||||
// padSize := len(params.Padding)
|
|
||||||
// padLengthSize, err := intSize(len(params.Padding))
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// totalPadSize := padSize + padLengthSize
|
|
||||||
// buf := make([]byte, 8)
|
|
||||||
// binary.LittleEndian.PutUint32(buf, uint32(totalPadSize))
|
|
||||||
// buf = buf[:padLengthSize]
|
|
||||||
// msg.Raw = append(msg.Raw, buf...)
|
|
||||||
// msg.Raw = append(msg.Raw, params.Padding...)
|
|
||||||
// msg.Raw[0] |= byte(padLengthSize) // number of bytes indicating the padding size
|
|
||||||
//} else if odd != 0 {
|
|
||||||
paddingSize := padSizeLimit - odd
|
paddingSize := padSizeLimit - odd
|
||||||
//if totalPadSize > 255 {
|
|
||||||
// // this algorithm is only valid if padSizeLimit < 256.
|
|
||||||
// // if padSizeLimit will ever change, please fix the algorithm
|
|
||||||
// // (please see also ReceivedMessage.extractPadding() function).
|
|
||||||
// panic("please fix the padding algorithm before releasing new version")
|
|
||||||
//}
|
|
||||||
pad := make([]byte, paddingSize)
|
pad := make([]byte, paddingSize)
|
||||||
_, err := crand.Read(pad)
|
_, err := crand.Read(pad)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -175,10 +144,7 @@ func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
||||||
if !validateSymmetricKey(pad) {
|
if !validateSymmetricKey(pad) {
|
||||||
return errors.New("failed to generate random padding of size " + strconv.Itoa(paddingSize))
|
return errors.New("failed to generate random padding of size " + strconv.Itoa(paddingSize))
|
||||||
}
|
}
|
||||||
//buf[0] = byte(totalPadSize)
|
|
||||||
msg.Raw = append(msg.Raw, pad...)
|
msg.Raw = append(msg.Raw, pad...)
|
||||||
//msg.Raw[0] |= byte(0x1) // number of bytes indicating the padding size
|
|
||||||
//}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,11 +161,10 @@ func (msg *sentMessage) sign(key *ecdsa.PrivateKey) error {
|
||||||
hash := crypto.Keccak256(msg.Raw)
|
hash := crypto.Keccak256(msg.Raw)
|
||||||
signature, err := crypto.Sign(hash, key)
|
signature, err := crypto.Sign(hash, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg.Raw[0] ^= signatureFlag // clear the flag
|
msg.Raw[0] &= (0xFF ^ signatureFlag) // clear the flag
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
msg.Raw = append(msg.Raw, signature...)
|
msg.Raw = append(msg.Raw, signature...)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,8 +262,7 @@ func (msg *sentMessage) Wrap(options *MessageParams) (envelope *Envelope, err er
|
||||||
// decryptSymmetric decrypts a message with a topic key, using AES-GCM-256.
|
// decryptSymmetric decrypts a message with a topic key, using AES-GCM-256.
|
||||||
// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
|
// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
|
||||||
func (msg *ReceivedMessage) decryptSymmetric(key []byte) error {
|
func (msg *ReceivedMessage) decryptSymmetric(key []byte) error {
|
||||||
// In v6, symmetric messages are expected to contain the 12-byte
|
// symmetric messages are expected to contain the 12-byte nonce at the end of the payload
|
||||||
// "salt" at the end of the payload.
|
|
||||||
if len(msg.Raw) < AESNonceLength {
|
if len(msg.Raw) < AESNonceLength {
|
||||||
return errors.New("missing salt or invalid payload in symmetric message")
|
return errors.New("missing salt or invalid payload in symmetric message")
|
||||||
}
|
}
|
||||||
|
|
@ -335,16 +299,12 @@ func (msg *ReceivedMessage) decryptAsymmetric(key *ecdsa.PrivateKey) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks the message validity and extracts the fields in case of success.
|
// Validate checks the message validity and extracts the fields in case of success.
|
||||||
func (msg *ReceivedMessage) ValidateAndParse(symmetric bool) bool {
|
func (msg *ReceivedMessage) ValidateAndParse() bool {
|
||||||
end := len(msg.Raw)
|
end := len(msg.Raw)
|
||||||
if end < 1 {
|
if end < 1 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if symmetric {
|
|
||||||
end -= AESNonceLength
|
|
||||||
}
|
|
||||||
|
|
||||||
if isMessageSigned(msg.Raw[0]) {
|
if isMessageSigned(msg.Raw[0]) {
|
||||||
end -= signatureLength
|
end -= signatureLength
|
||||||
if end <= 1 {
|
if end <= 1 {
|
||||||
|
|
@ -371,33 +331,9 @@ func (msg *ReceivedMessage) ValidateAndParse(symmetric bool) bool {
|
||||||
|
|
||||||
beg += payloadSize
|
beg += payloadSize
|
||||||
msg.Padding = msg.Raw[beg:end]
|
msg.Padding = msg.Raw[beg:end]
|
||||||
|
|
||||||
//padSize, ok := msg.extractPadding(end)
|
|
||||||
//if !ok {
|
|
||||||
// return false
|
|
||||||
//}
|
|
||||||
//msg.Payload = msg.Raw[1+padSize : end]
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractPadding extracts the padding from raw message.
|
|
||||||
// although we don't support sending messages with padding size
|
|
||||||
// exceeding 255 bytes, such messages are perfectly valid, and
|
|
||||||
// can be successfully decrypted.
|
|
||||||
//func (msg *ReceivedMessage) extractPadding(end int) (int, bool) {
|
|
||||||
// payloadSize := 0
|
|
||||||
// auxFieldSize := int(msg.Raw[0] & auxFieldSizeMask) // number of bytes indicating the size of payload
|
|
||||||
// if sz != 0 {
|
|
||||||
// paddingSize = int(bytesToUintLittleEndian(msg.Raw[1 : 1+sz]))
|
|
||||||
// if paddingSize < sz || paddingSize+1 > end {
|
|
||||||
// return 0, false
|
|
||||||
// }
|
|
||||||
// msg.Padding = msg.Raw[1+sz : 1+paddingSize]
|
|
||||||
// }
|
|
||||||
// return paddingSize, true
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Recover retrieves the public key of the message signer.
|
// Recover retrieves the public key of the message signer.
|
||||||
func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
||||||
defer func() { recover() }() // in case of invalid signature
|
defer func() { recover() }() // in case of invalid signature
|
||||||
|
|
|
||||||
|
|
@ -90,8 +90,8 @@ func singleMessageTest(t *testing.T, symmetric bool) {
|
||||||
t.Fatalf("failed to encrypt with seed %d: %s.", seed, err)
|
t.Fatalf("failed to encrypt with seed %d: %s.", seed, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !decrypted.ValidateAndParse(symmetric) {
|
if !decrypted.ValidateAndParse() {
|
||||||
t.Fatalf("failed to validate with seed %d.", seed)
|
t.Fatalf("failed to validate with seed %d, symmetric = %v.", seed, symmetric)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(text, decrypted.Payload) {
|
if !bytes.Equal(text, decrypted.Payload) {
|
||||||
|
|
@ -427,7 +427,7 @@ func TestPaddingAppendedToSymMessages(t *testing.T) {
|
||||||
// payload + flag + aesnonce > 256. Check that the result
|
// payload + flag + aesnonce > 256. Check that the result
|
||||||
// is padded on the next 256 boundary.
|
// is padded on the next 256 boundary.
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
msg.Raw = make([]byte, len(params.Payload)+1+AESNonceLength)
|
msg.Raw = make([]byte, 1+1+len(params.Payload))
|
||||||
|
|
||||||
err := msg.appendPadding(params)
|
err := msg.appendPadding(params)
|
||||||
|
|
||||||
|
|
@ -436,7 +436,7 @@ func TestPaddingAppendedToSymMessages(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(msg.Raw) != 512 {
|
if len(msg.Raw) != 512-AESNonceLength {
|
||||||
t.Errorf("Invalid size %d != 512", len(msg.Raw))
|
t.Errorf("Invalid size %d != 512", len(msg.Raw))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -459,7 +459,7 @@ func TestPaddingAppendedToSymMessagesWithSignature(t *testing.T) {
|
||||||
// payload + flag + aesnonce > 256. Check that the result
|
// payload + flag + aesnonce > 256. Check that the result
|
||||||
// is padded on the next 256 boundary.
|
// is padded on the next 256 boundary.
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
msg.Raw = make([]byte, len(params.Payload)+1+AESNonceLength+signatureLength)
|
msg.Raw = make([]byte, 1+1+len(params.Payload))
|
||||||
|
|
||||||
err = msg.appendPadding(params)
|
err = msg.appendPadding(params)
|
||||||
|
|
||||||
|
|
@ -468,7 +468,7 @@ func TestPaddingAppendedToSymMessagesWithSignature(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(msg.Raw) != 512 {
|
if len(msg.Raw) != 512-AESNonceLength-signatureLength {
|
||||||
t.Errorf("Invalid size %d != 512", len(msg.Raw))
|
t.Errorf("Invalid size %d != 512", len(msg.Raw))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue