2022-04-01 07:37:13 +02:00
|
|
|
# Waku Noise Protocols for Waku Payload Encryption
|
|
|
|
## See spec for more details:
|
|
|
|
## https://github.com/vacp2p/rfc/tree/master/content/docs/rfcs/35
|
|
|
|
##
|
|
|
|
## Implementation partially inspired by noise-libp2p:
|
|
|
|
## https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/secure/noise.nim
|
2022-03-30 23:15:15 +02:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-03-30 23:30:33 +02:00
|
|
|
import std/[oids, options, tables]
|
2022-03-30 23:15:15 +02:00
|
|
|
import chronos
|
|
|
|
import chronicles
|
|
|
|
import bearssl
|
2022-03-30 23:30:33 +02:00
|
|
|
import strutils
|
|
|
|
import stew/[endians2]
|
2022-03-30 23:15:15 +02:00
|
|
|
import nimcrypto/[utils, sha2, hmac]
|
|
|
|
|
|
|
|
import libp2p/stream/[connection]
|
2022-03-30 23:30:33 +02:00
|
|
|
import libp2p/peerid
|
|
|
|
import libp2p/peerinfo
|
2022-03-30 23:15:15 +02:00
|
|
|
import libp2p/protobuf/minprotobuf
|
|
|
|
import libp2p/utility
|
|
|
|
import libp2p/errors
|
2022-03-30 23:30:33 +02:00
|
|
|
import libp2p/crypto/[crypto, chacha20poly1305, curve25519]
|
|
|
|
|
2022-03-30 23:15:15 +02:00
|
|
|
|
|
|
|
logScope:
|
2022-04-04 17:46:51 +02:00
|
|
|
topics = "wakunoise"
|
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
#################################################################
|
|
|
|
|
|
|
|
# Constants and data structures
|
2022-03-30 23:15:15 +02:00
|
|
|
|
|
|
|
const
|
2022-04-06 14:37:02 +02:00
|
|
|
# EmptyKey represents a non-initialized ChaChaPolyKey
|
2022-03-30 23:15:15 +02:00
|
|
|
EmptyKey = default(ChaChaPolyKey)
|
2022-04-06 14:37:02 +02:00
|
|
|
# The maximum ChaChaPoly allowed nonce in Noise Handshakes
|
|
|
|
NonceMax = uint64.high - 1
|
2022-03-30 23:15:15 +02:00
|
|
|
|
|
|
|
type
|
2022-04-06 14:37:02 +02:00
|
|
|
# Default underlying elliptic curve arithmetic (useful for switching to multiple ECs)
|
|
|
|
# Current default is Curve25519
|
|
|
|
EllipticCurveKey = Curve25519Key
|
|
|
|
|
|
|
|
# An EllipticCurveKey (public, private) key pair
|
2022-03-30 23:30:33 +02:00
|
|
|
KeyPair* = object
|
2022-04-06 14:37:02 +02:00
|
|
|
privateKey: EllipticCurveKey
|
|
|
|
publicKey: EllipticCurveKey
|
2022-03-30 23:30:33 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# A Noise public key is a public key exchanged during Noise handshakes (no private part)
|
|
|
|
# This follows https://rfc.vac.dev/spec/35/#public-keys-serialization
|
|
|
|
# pk contains the X coordinate of the public key, if unencrypted (this implies flag = 0)
|
|
|
|
# or the encryption of the X coordinate concatenated with the authorization tag, if encrypted (this implies flag = 1)
|
2022-03-30 23:30:33 +02:00
|
|
|
NoisePublicKey* = object
|
|
|
|
flag: uint8
|
|
|
|
pk: seq[byte]
|
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# A ChaChaPoly ciphertext (data) + authorization tag (tag)
|
2022-03-30 23:15:15 +02:00
|
|
|
ChaChaPolyCiphertext* = object
|
2022-04-06 14:37:02 +02:00
|
|
|
data*: seq[byte]
|
|
|
|
tag*: ChaChaPolyTag
|
2022-03-30 23:15:15 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# A ChaChaPoly Cipher State containing key (k), nonce (nonce) and associated data (ad)
|
2022-03-30 23:15:15 +02:00
|
|
|
ChaChaPolyCipherState* = object
|
2022-04-06 14:37:02 +02:00
|
|
|
k: ChaChaPolyKey
|
|
|
|
nonce: ChaChaPolyNonce
|
|
|
|
ad: seq[byte]
|
2022-03-30 23:15:15 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Some useful error types
|
2022-03-30 23:15:15 +02:00
|
|
|
NoiseError* = object of LPError
|
2022-03-30 23:30:33 +02:00
|
|
|
NoiseHandshakeError* = object of NoiseError
|
2022-04-06 14:37:02 +02:00
|
|
|
NoiseEmptyChaChaPolyInput* = object of NoiseError
|
2022-03-30 23:15:15 +02:00
|
|
|
NoiseDecryptTagError* = object of NoiseError
|
2022-04-06 14:37:02 +02:00
|
|
|
NoiseNonceMaxError* = object of NoiseError
|
2022-03-30 23:30:33 +02:00
|
|
|
NoisePublicKeyError* = object of NoiseError
|
|
|
|
NoiseMalformedHandshake* = object of NoiseError
|
|
|
|
|
|
|
|
|
2022-04-04 17:46:51 +02:00
|
|
|
#################################################################
|
|
|
|
|
|
|
|
# Utilities
|
|
|
|
|
|
|
|
# Generates random byte sequences of given size
|
2022-04-06 14:37:02 +02:00
|
|
|
proc randomSeqByte*(rng: var BrHmacDrbgContext, size: int): seq[byte] =
|
|
|
|
var output = newSeq[byte](size.uint32)
|
2022-04-04 17:46:51 +02:00
|
|
|
brHmacDrbgGenerate(rng, output)
|
|
|
|
return output
|
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Generate random Curve25519 (public, private) key pairs
|
|
|
|
proc genKeyPair*(rng: var BrHmacDrbgContext): KeyPair =
|
|
|
|
var keyPair: KeyPair
|
|
|
|
keyPair.privateKey = EllipticCurveKey.random(rng)
|
|
|
|
keyPair.publicKey = keyPair.privateKey.public()
|
|
|
|
return keyPair
|
|
|
|
|
2022-03-30 23:30:33 +02:00
|
|
|
|
|
|
|
#################################################################
|
|
|
|
|
2022-04-04 17:46:51 +02:00
|
|
|
# ChaChaPoly Symmetric Cipher
|
2022-03-30 23:15:15 +02:00
|
|
|
|
|
|
|
# ChaChaPoly encryption
|
2022-04-04 17:46:51 +02:00
|
|
|
# It takes a Cipher State (with key, nonce, and associated data) and encrypts a plaintext
|
2022-04-06 14:37:02 +02:00
|
|
|
# The cipher state in not changed
|
2022-03-30 23:15:15 +02:00
|
|
|
proc encrypt*(
|
|
|
|
state: ChaChaPolyCipherState,
|
|
|
|
plaintext: openArray[byte]): ChaChaPolyCiphertext
|
2022-04-06 14:37:02 +02:00
|
|
|
{.noinit, raises: [Defect, NoiseEmptyChaChaPolyInput].} =
|
|
|
|
# If plaintext is empty, we raise an error
|
|
|
|
if plaintext == @[]:
|
|
|
|
raise newException(NoiseEmptyChaChaPolyInput, "Tried to encrypt empty plaintext")
|
2022-04-04 17:46:51 +02:00
|
|
|
var ciphertext: ChaChaPolyCiphertext
|
2022-04-06 14:37:02 +02:00
|
|
|
# Since ChaChaPoly's library "encrypt" primitive directly changes the input plaintext to the ciphertext,
|
|
|
|
# we copy the plaintext into the ciphertext variable and we pass the latter to encrypt
|
2022-04-04 17:46:51 +02:00
|
|
|
ciphertext.data.add plaintext
|
2022-03-30 23:15:15 +02:00
|
|
|
#TODO: add padding
|
2022-04-06 14:37:02 +02:00
|
|
|
# ChaChaPoly.encrypt takes as input: the key (k), the nonce (nonce), a data structure for storing the computed authorization tag (tag),
|
|
|
|
# the plaintext (overwritten to ciphertext) (data), the associated data (ad)
|
2022-04-04 17:46:51 +02:00
|
|
|
ChaChaPoly.encrypt(state.k, state.nonce, ciphertext.tag, ciphertext.data, state.ad)
|
|
|
|
return ciphertext
|
|
|
|
|
|
|
|
# ChaChaPoly decryption
|
|
|
|
# It takes a Cipher State (with key, nonce, and associated data) and decrypts a ciphertext
|
2022-04-06 14:37:02 +02:00
|
|
|
# The cipher state is not changed
|
2022-03-30 23:15:15 +02:00
|
|
|
proc decrypt*(
|
|
|
|
state: ChaChaPolyCipherState,
|
|
|
|
ciphertext: ChaChaPolyCiphertext): seq[byte]
|
2022-04-06 14:37:02 +02:00
|
|
|
{.raises: [Defect, NoiseEmptyChaChaPolyInput, NoiseDecryptTagError].} =
|
|
|
|
# If ciphertext is empty, we raise an error
|
|
|
|
if ciphertext.data == @[]:
|
|
|
|
raise newException(NoiseEmptyChaChaPolyInput, "Tried to decrypt empty ciphertext")
|
2022-03-30 23:15:15 +02:00
|
|
|
var
|
2022-04-06 14:37:02 +02:00
|
|
|
# The input authorization tag
|
2022-03-30 23:15:15 +02:00
|
|
|
tagIn = ciphertext.tag
|
2022-04-06 14:37:02 +02:00
|
|
|
# The authorization tag computed during decryption
|
2022-03-30 23:15:15 +02:00
|
|
|
tagOut: ChaChaPolyTag
|
2022-04-06 14:37:02 +02:00
|
|
|
# Since ChaChaPoly's library "decrypt" primitive directly changes the input ciphertext to the plaintext,
|
|
|
|
# we copy the ciphertext into the plaintext variable and we pass the latter to decrypt
|
2022-04-04 17:46:51 +02:00
|
|
|
var plaintext = ciphertext.data
|
2022-04-06 14:37:02 +02:00
|
|
|
# ChaChaPoly.decrypt takes as input: the key (k), the nonce (nonce), a data structure for storing the computed authorization tag (tag),
|
|
|
|
# the ciphertext (overwritten to plaintext) (data), the associated data (ad)
|
2022-04-04 17:46:51 +02:00
|
|
|
ChaChaPoly.decrypt(state.k, state.nonce, tagOut, plaintext, state.ad)
|
2022-03-30 23:15:15 +02:00
|
|
|
#TODO: add unpadding
|
|
|
|
trace "decrypt", tagIn = tagIn.shortLog, tagOut = tagOut.shortLog, nonce = state.nonce
|
2022-04-06 14:37:02 +02:00
|
|
|
# We check if the authorization tag computed while decrypting is the same as the input tag
|
2022-03-30 23:15:15 +02:00
|
|
|
if tagIn != tagOut:
|
2022-04-04 17:46:51 +02:00
|
|
|
debug "decrypt failed", plaintext = shortLog(plaintext)
|
2022-03-30 23:15:15 +02:00
|
|
|
raise newException(NoiseDecryptTagError, "decrypt tag authentication failed.")
|
2022-04-04 17:46:51 +02:00
|
|
|
return plaintext
|
2022-03-30 23:15:15 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Generates a random ChaChaPoly Cipher State for testing encryption/decryption
|
2022-03-30 23:15:15 +02:00
|
|
|
proc randomChaChaPolyCipherState*(rng: var BrHmacDrbgContext): ChaChaPolyCipherState =
|
2022-04-04 17:46:51 +02:00
|
|
|
var randomCipherState: ChaChaPolyCipherState
|
|
|
|
brHmacDrbgGenerate(rng, randomCipherState.k)
|
|
|
|
brHmacDrbgGenerate(rng, randomCipherState.nonce)
|
|
|
|
randomCipherState.ad = newSeq[byte](32)
|
|
|
|
brHmacDrbgGenerate(rng, randomCipherState.ad)
|
|
|
|
return randomCipherState
|
2022-03-30 23:30:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Noise Public keys
|
2022-03-30 23:30:33 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Checks equality between two Noise public keys
|
2022-03-30 23:30:33 +02:00
|
|
|
proc `==`(k1, k2: NoisePublicKey): bool =
|
2022-04-06 14:37:02 +02:00
|
|
|
return (k1.flag == k2.flag) and (k1.pk == k2.pk)
|
2022-03-30 23:30:33 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Converts a (public, private) Elliptic Curve keypair to an unencrypted Noise public key (only public part)
|
2022-03-30 23:30:33 +02:00
|
|
|
proc keyPairToNoisePublicKey*(keyPair: KeyPair): NoisePublicKey =
|
2022-04-06 14:37:02 +02:00
|
|
|
var noisePublicKey: NoisePublicKey
|
|
|
|
noisePublicKey.flag = 0
|
|
|
|
noisePublicKey.pk = getBytes(keyPair.publicKey)
|
|
|
|
return noisePublicKey
|
2022-03-30 23:30:33 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Generates a random Noise public key
|
2022-03-30 23:30:33 +02:00
|
|
|
proc genNoisePublicKey*(rng: var BrHmacDrbgContext): NoisePublicKey =
|
2022-04-06 14:37:02 +02:00
|
|
|
var noisePublicKey: NoisePublicKey
|
|
|
|
# We generate a random key pair
|
2022-03-30 23:30:33 +02:00
|
|
|
let keyPair: KeyPair = genKeyPair(rng)
|
2022-04-06 14:37:02 +02:00
|
|
|
# Since it is unencrypted, flag is 0
|
|
|
|
noisePublicKey.flag = 0
|
|
|
|
# We copy the public X coordinate of the key pair to the output Noise public key
|
|
|
|
noisePublicKey.pk = getBytes(keyPair.publicKey)
|
|
|
|
return noisePublicKey
|
|
|
|
|
|
|
|
# Converts a Noise public key to a stream of bytes as in
|
|
|
|
# https://rfc.vac.dev/spec/35/#public-keys-serialization
|
2022-03-30 23:30:33 +02:00
|
|
|
proc serializeNoisePublicKey*(noisePublicKey: NoisePublicKey): seq[byte] =
|
2022-04-06 14:37:02 +02:00
|
|
|
var serializedNoisePublicKey: seq[byte]
|
|
|
|
# Public key is serialized as (flag || pk)
|
|
|
|
# Note that pk contains the X coordinate of the public key if unencrypted
|
|
|
|
# or the encryption concatenated with the authorization tag if encrypted
|
|
|
|
serializedNoisePublicKey.add noisePublicKey.flag
|
|
|
|
serializedNoisePublicKey.add noisePublicKey.pk
|
|
|
|
return serializedNoisePublicKey
|
|
|
|
|
|
|
|
# Converts a serialized Noise public key to a NoisePublicKey object as in
|
|
|
|
# https://rfc.vac.dev/spec/35/#public-keys-serialization
|
|
|
|
proc intoNoisePublicKey*(serializedNoisePublicKey: seq[byte]): NoisePublicKey
|
|
|
|
{.raises: [Defect, NoisePublicKeyError].} =
|
|
|
|
var noisePublicKey: NoisePublicKey
|
|
|
|
# We retrieve the encryption flag
|
|
|
|
noisePublicKey.flag = serializedNoisePublicKey[0]
|
|
|
|
# If not 0 or 1 we raise a new exception
|
|
|
|
if not (noisePublicKey.flag == 0 or noisePublicKey.flag == 1):
|
|
|
|
raise newException(NoisePublicKeyError, "Invalid flag in serialized public key")
|
|
|
|
# We set the remaining sequence to the pk value (this may be an encrypted or not encrypted X coordinate)
|
|
|
|
noisePublicKey.pk = serializedNoisePublicKey[1..<serializedNoisePublicKey.len]
|
|
|
|
return noisePublicKey
|
|
|
|
|
|
|
|
# Encrypts a Noise public key using a ChaChaPoly Cipher State
|
2022-03-30 23:30:33 +02:00
|
|
|
proc encryptNoisePublicKey*(cs: ChaChaPolyCipherState, noisePublicKey: NoisePublicKey): NoisePublicKey
|
2022-04-06 14:37:02 +02:00
|
|
|
{.raises: [Defect, NoiseEmptyChaChaPolyInput, NoiseNonceMaxError].} =
|
|
|
|
var encryptedNoisePublicKey: NoisePublicKey
|
|
|
|
# We proceed with encryption only if
|
|
|
|
# - a key is set in the cipher state
|
|
|
|
# - the public key is unencrypted
|
2022-03-30 23:30:33 +02:00
|
|
|
if cs.k != EmptyKey and noisePublicKey.flag == 0:
|
2022-04-06 14:37:02 +02:00
|
|
|
let encPk = encrypt(cs, noisePublicKey.pk)
|
|
|
|
# We set the flag to 1, since encrypted
|
|
|
|
encryptedNoisePublicKey.flag = 1
|
|
|
|
# Authorization tag is appendend to the ciphertext
|
|
|
|
encryptedNoisePublicKey.pk = encPk.data
|
|
|
|
encryptedNoisePublicKey.pk.add encPk.tag
|
|
|
|
# Otherwise we return the public key as it is
|
2022-03-30 23:30:33 +02:00
|
|
|
else:
|
2022-04-06 14:37:02 +02:00
|
|
|
encryptedNoisePublicKey = noisePublicKey
|
|
|
|
return encryptedNoisePublicKey
|
2022-03-30 23:30:33 +02:00
|
|
|
|
2022-04-06 14:37:02 +02:00
|
|
|
# Decrypts a Noise public key using a ChaChaPoly Cipher State
|
2022-03-30 23:30:33 +02:00
|
|
|
proc decryptNoisePublicKey*(cs: ChaChaPolyCipherState, noisePublicKey: NoisePublicKey): NoisePublicKey
|
2022-04-06 14:37:02 +02:00
|
|
|
{.raises: [Defect, NoiseEmptyChaChaPolyInput, NoiseDecryptTagError].} =
|
|
|
|
var decryptedNoisePublicKey: NoisePublicKey
|
|
|
|
# We proceed with decryption only if
|
|
|
|
# - a key is set in the cipher state
|
|
|
|
# - the public key is encrypted
|
2022-03-30 23:30:33 +02:00
|
|
|
if cs.k != EmptyKey and noisePublicKey.flag == 1:
|
2022-04-06 14:37:02 +02:00
|
|
|
# Since the pk field would contain an encryption + tag, we retrieve the ciphertext length
|
|
|
|
let pkLen = noisePublicKey.pk.len - ChaChaPolyTag.len
|
|
|
|
# We isolate the ciphertext and the authorization tag
|
|
|
|
let pk = noisePublicKey.pk[0..<pkLen]
|
|
|
|
let pkAuth = intoChaChaPolyTag(noisePublicKey.pk[pkLen..<pkLen+ChaChaPolyTag.len])
|
|
|
|
# We convert it to a ChaChaPolyCiphertext
|
|
|
|
let ciphertext = ChaChaPolyCiphertext(data: pk, tag: pkAuth)
|
|
|
|
# We run decryption and store its value to a non-encrypted Noise public key (flag = 0)
|
|
|
|
decryptedNoisePublicKey.pk = decrypt(cs, ciphertext)
|
|
|
|
decryptedNoisePublicKey.flag = 0
|
|
|
|
# Otherwise we return the public key as it is
|
2022-03-30 23:30:33 +02:00
|
|
|
else:
|
2022-04-06 14:37:02 +02:00
|
|
|
decryptedNoisePublicKey = noisePublicKey
|
2022-04-06 14:54:27 +02:00
|
|
|
return decryptedNoisePublicKey
|
2022-03-30 23:51:36 +02:00
|
|
|
|
|
|
|
|
2022-04-06 14:54:27 +02:00
|
|
|
#################################################################
|
2022-03-30 23:51:36 +02:00
|
|
|
|
|
|
|
# Payload functions
|
2022-04-06 14:54:27 +02:00
|
|
|
|
2022-03-30 23:51:36 +02:00
|
|
|
type
|
|
|
|
PayloadV2* = object
|
|
|
|
protocol_id: uint8
|
|
|
|
handshake_message: seq[NoisePublicKey]
|
|
|
|
transport_message: seq[byte]
|
|
|
|
|
|
|
|
|
|
|
|
proc `==`(p1, p2: PayloadV2): bool =
|
|
|
|
result = (p1.protocol_id == p2.protocol_id) and
|
|
|
|
(p1.handshake_message == p2.handshake_message) and
|
|
|
|
(p1.transport_message == p2.transport_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc randomPayloadV2*(rng: var BrHmacDrbgContext): PayloadV2 =
|
|
|
|
var protocol_id = newSeq[byte](1)
|
|
|
|
brHmacDrbgGenerate(rng, protocol_id)
|
|
|
|
result.protocol_id = protocol_id[0].uint8
|
|
|
|
result.handshake_message = @[genNoisePublicKey(rng), genNoisePublicKey(rng), genNoisePublicKey(rng)]
|
|
|
|
result.transport_message = newSeq[byte](128)
|
|
|
|
brHmacDrbgGenerate(rng, result.transport_message)
|
|
|
|
|
|
|
|
|
|
|
|
proc encodeV2*(self: PayloadV2): Option[seq[byte]] =
|
|
|
|
|
|
|
|
#We collect public keys contained in the handshake message
|
|
|
|
var
|
|
|
|
ser_handshake_message_len: int = 0
|
|
|
|
ser_handshake_message = newSeqOfCap[byte](256)
|
|
|
|
ser_pk: seq[byte]
|
|
|
|
for pk in self.handshake_message:
|
|
|
|
ser_pk = serializeNoisePublicKey(pk)
|
|
|
|
ser_handshake_message_len += ser_pk.len
|
|
|
|
ser_handshake_message.add ser_pk
|
|
|
|
|
|
|
|
|
|
|
|
#RFC: handshake-message-len is 1 byte
|
|
|
|
if ser_handshake_message_len > 256:
|
|
|
|
debug "Payload malformed: too many public keys contained in the handshake message"
|
|
|
|
return none(seq[byte])
|
|
|
|
|
|
|
|
let transport_message_len = self.transport_message.len
|
|
|
|
#let transport_message_len_len = ceil(log(transport_message_len, 8)).int
|
|
|
|
|
|
|
|
var payload = newSeqOfCap[byte](1 + #self.protocol_id.len +
|
|
|
|
1 + #ser_handshake_message_len
|
|
|
|
ser_handshake_message_len +
|
|
|
|
8 + #transport_message_len
|
|
|
|
transport_message_len #self.transport_message
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
payload.add self.protocol_id.byte
|
|
|
|
payload.add ser_handshake_message_len.byte
|
|
|
|
payload.add ser_handshake_message
|
|
|
|
payload.add toBytesLE(transport_message_len.uint64)
|
|
|
|
payload.add self.transport_message
|
|
|
|
|
|
|
|
return some(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Decode Noise handshake payload
|
|
|
|
proc decodeV2*(payload: seq[byte]): Option[PayloadV2] =
|
|
|
|
var res: PayloadV2
|
|
|
|
|
|
|
|
var i: uint64 = 0
|
|
|
|
res.protocol_id = payload[i].uint8
|
|
|
|
i+=1
|
|
|
|
|
|
|
|
var handshake_message_len = payload[i].uint64
|
|
|
|
i+=1
|
|
|
|
|
|
|
|
var handshake_message: seq[NoisePublicKey]
|
|
|
|
|
|
|
|
var
|
|
|
|
flag: byte
|
|
|
|
pk_len: uint64
|
|
|
|
written: uint64 = 0
|
|
|
|
|
|
|
|
while written != handshake_message_len:
|
|
|
|
#Note that flag can be used to add support to multiple Elliptic Curve arithmetics..
|
|
|
|
flag = payload[i]
|
|
|
|
if flag == 0:
|
|
|
|
pk_len = 1 + Curve25519Key.len
|
|
|
|
handshake_message.add intoNoisePublicKey(payload[i..<i+pk_len])
|
|
|
|
i += pk_len
|
|
|
|
written += pk_len
|
|
|
|
if flag == 1:
|
|
|
|
pk_len = 1 + Curve25519Key.len + ChaChaPolyTag.len
|
|
|
|
handshake_message.add intoNoisePublicKey(payload[i..<i+pk_len])
|
|
|
|
i += pk_len
|
|
|
|
written += pk_len
|
|
|
|
|
|
|
|
res.handshake_message = handshake_message
|
|
|
|
|
|
|
|
let transport_message_len = fromBytesLE(uint64, payload[i..(i+8-1)])
|
|
|
|
i+=8
|
|
|
|
|
|
|
|
res.transport_message = payload[i..i+transport_message_len-1]
|
|
|
|
i+=transport_message_len
|
|
|
|
|
2022-04-06 14:54:27 +02:00
|
|
|
return some(res)
|