fix: noise public key serialization

This commit is contained in:
Richard Ramos 2022-12-15 20:50:59 -04:00 committed by RichΛrd
parent ad33eabe39
commit 5511e55227
1 changed files with 33 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
n "github.com/waku-org/noise" n "github.com/waku-org/noise"
) )
@ -206,9 +207,13 @@ func (p *PayloadV2) Serialize() ([]byte, error) {
} }
func isProtocolIDSupported(protocolID WakuNoiseProtocolID) bool { func isProtocolIDSupported(protocolID WakuNoiseProtocolID) bool {
return protocolID == Noise_K1K1_25519_ChaChaPoly_SHA256 || protocolID == Noise_XK1_25519_ChaChaPoly_SHA256 || return protocolID == Noise_K1K1_25519_ChaChaPoly_SHA256 ||
protocolID == Noise_XX_25519_ChaChaPoly_SHA256 || protocolID == Noise_XXpsk0_25519_ChaChaPoly_SHA256 || protocolID == Noise_XK1_25519_ChaChaPoly_SHA256 ||
protocolID == ChaChaPoly || protocolID == None protocolID == Noise_XX_25519_ChaChaPoly_SHA256 ||
protocolID == Noise_XXpsk0_25519_ChaChaPoly_SHA256 ||
protocolID == ChaChaPoly ||
protocolID == Noise_WakuPairing_25519_ChaChaPoly_SHA256 ||
protocolID == None
} }
const ChaChaPolyTagSize = byte(16) const ChaChaPolyTagSize = byte(16)
@ -227,7 +232,6 @@ func DeserializePayloadV2(payload []byte) (*PayloadV2, error) {
} }
// We read the Protocol ID // We read the Protocol ID
// TODO: when the list of supported protocol ID is defined, check if read protocol ID is supported
if err := binary.Read(payloadBuf, binary.BigEndian, &result.ProtocolId); err != nil { if err := binary.Read(payloadBuf, binary.BigEndian, &result.ProtocolId); err != nil {
return nil, err return nil, err
} }
@ -272,8 +276,7 @@ func DeserializePayloadV2(payload []byte) (*PayloadV2, error) {
} }
handshakeMessages = append(handshakeMessages, pk) handshakeMessages = append(handshakeMessages, pk)
written += uint8(len(serializedPK)) written += uint8(1 + pkLen)
} else if flag == 1 { } else if flag == 1 {
// If the key is encrypted, we only read the encrypted X coordinate and the authorization tag, and we deserialize into a Noise Public Key // If the key is encrypted, we only read the encrypted X coordinate and the authorization tag, and we deserialize into a Noise Public Key
pkLen := ed25519.PublicKeySize + ChaChaPolyTagSize pkLen := ed25519.PublicKeySize + ChaChaPolyTagSize
@ -294,7 +297,7 @@ func DeserializePayloadV2(payload []byte) (*PayloadV2, error) {
} }
handshakeMessages = append(handshakeMessages, pk) handshakeMessages = append(handshakeMessages, pk)
written += uint8(len(serializedPK)) written += uint8(1 + pkLen)
// TODO: duplicated // TODO: duplicated
} else { } else {
return nil, errors.New("invalid flag for Noise public key") return nil, errors.New("invalid flag for Noise public key")
@ -315,3 +318,26 @@ func DeserializePayloadV2(payload []byte) (*PayloadV2, error) {
return result, nil return result, nil
} }
// Decodes a WakuMessage to a PayloadV2
// Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
func DecodePayloadV2(message *pb.WakuMessage) (*PayloadV2, error) {
if message.Version != 2 {
return nil, errors.New("wrong message version while decoding payload")
}
return DeserializePayloadV2(message.Payload)
}
// Encodes a PayloadV2 to a WakuMessage
// Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
func EncodePayloadV2(payload2 *PayloadV2) (*pb.WakuMessage, error) {
serializedPayload2, err := payload2.Serialize()
if err != nil {
return nil, err
}
return &pb.WakuMessage{
Payload: serializedPayload2,
Version: 2,
}, nil
}