refactor(utils): moved noise/compat payload encryption code to utils

This commit is contained in:
Lorenzo Delgado 2023-02-07 10:45:25 +01:00 committed by GitHub
parent fd433ce0e2
commit 0826bdad99
13 changed files with 220 additions and 207 deletions

View File

@ -27,17 +27,21 @@ import
../../waku/v2/protocol/waku_lightpush/rpc,
../../waku/v2/protocol/waku_filter,
../../waku/v2/protocol/waku_store,
../../waku/v2/node/[waku_node, waku_payload, waku_metrics],
../../waku/v2/node/waku_node,
../../waku/v2/node/waku_metrics,
../../waku/v2/node/dnsdisc/waku_dnsdisc,
../../waku/v2/node/peer_manager,
../../waku/v2/utils/[peers, time],
../../waku/v2/utils/compat,
../../waku/v2/utils/peers,
../../waku/v2/utils/time,
../../waku/common/utils/nat,
./config_chat2
when defined(rln):
import
libp2p/protocols/pubsub/rpc/messages,
libp2p/protocols/pubsub/pubsub,
libp2p/protocols/pubsub/pubsub
import
../../waku/v2/protocol/waku_rln_relay
const Help = """

View File

@ -4,7 +4,9 @@ import
./v2/test_confutils_envvar,
./v2/test_sqlite_migrations
## Waku archive test suite
## Waku v2
# Waku archive test suite
import
./v2/waku_archive/test_driver_queue_index,
./v2/waku_archive/test_driver_queue_pagination,
@ -15,7 +17,7 @@ import
./v2/waku_archive/test_retention_policy,
./v2/waku_archive/test_waku_archive
## Waku store test suite
# Waku store test suite
import
./v2/waku_store/test_rpc_codec,
./v2/waku_store/test_waku_store,
@ -38,7 +40,6 @@ import
./v2/test_wakunode_filter,
./v2/test_waku_peer_exchange,
./v2/test_peer_store_extended,
./v2/test_waku_payload,
./v2/test_waku_swap,
./v2/test_utils_peers,
./v2/test_message_cache,
@ -62,6 +63,7 @@ import
./v2/test_waku_noise_sessions,
./v2/test_waku_switch,
# Utils
./v2/test_utils_compat,
./v2/test_utils_keyfile
@ -74,7 +76,6 @@ when defined(rln):
./v2/test_waku_rln_relay_onchain
# TODO: Only enable this once swap module is integrated more nicely as a dependency, i.e. as submodule with CI etc
# For PoC execute it manually and run separate module here: https://github.com/vacp2p/swap-contracts-module
# ./v2/test_waku_swap_contracts

View File

@ -32,6 +32,7 @@ import
../../waku/v2/protocol/waku_filter,
../../waku/v2/protocol/waku_filter/rpc,
../../waku/v2/protocol/waku_filter/client,
../../waku/v2/utils/compat,
../../waku/v2/utils/peers,
../../waku/v2/utils/time,
./testlib/common,

View File

@ -1,9 +1,10 @@
{.used.}
import
testutils/unittests,
testutils/unittests
import
../../waku/v2/protocol/waku_message,
../../waku/v2/node/waku_payload,
../../waku/v2/utils/compat,
../../waku/v2/utils/time
procSuite "Waku Payload":
@ -120,28 +121,28 @@ procSuite "Waku Payload":
payload = @[byte 0, 1, 2]
timestamp = Timestamp(10)
msg = WakuMessage(payload: payload, version: version, timestamp: timestamp)
## When
let pb = msg.encode()
let msgDecoded = WakuMessage.decode(pb.buffer)
## Then
check:
msgDecoded.isOk()
let timestampDecoded = msgDecoded.value.timestamp
check:
timestampDecoded == timestamp
test "Encode/Decode waku message without timestamp":
## Test the encoding and decoding of a WakuMessage with an empty timestamp field
## Test the encoding and decoding of a WakuMessage with an empty timestamp field
## Given
let
version = 0'u32
payload = @[byte 0, 1, 2]
msg = WakuMessage(payload: payload, version: version)
## When
let pb = msg.encode()
let msgDecoded = WakuMessage.decode(pb.buffer)
@ -149,7 +150,7 @@ procSuite "Waku Payload":
## Then
check:
msgDecoded.isOk()
let timestampDecoded = msgDecoded.value.timestamp
check:
timestampDecoded == Timestamp(0)
timestampDecoded == Timestamp(0)

View File

@ -18,7 +18,8 @@ import
import
../../waku/v1/protocol/waku_protocol,
../../waku/v2/protocol/waku_message,
../../waku/v2/node/[waku_node, waku_payload],
../../waku/v2/node/waku_node,
../../waku/v2/utils/compat,
../../waku/v2/utils/peers,
../../apps/wakubridge/wakubridge,
../test_helpers

View File

@ -5,7 +5,7 @@ import
std/random,
std/tables,
stew/byteutils,
../../waku/v2/node/waku_payload,
../../waku/v2/utils/noise as waku_message_utils,
../../waku/v2/protocol/waku_noise/noise_types,
../../waku/v2/protocol/waku_noise/noise_utils,
../../waku/v2/protocol/waku_noise/noise,
@ -18,7 +18,7 @@ import
procSuite "Waku Noise":
# We initialize the RNG in test_helpers
let rng = rng()
# We initialize the RNG in std/random
@ -29,7 +29,7 @@ procSuite "Waku Noise":
# We test padding for different message lengths
let maxMessageLength = 3 * NoisePaddingBlockSize
for messageLen in 0..maxMessageLength:
let
message = randomSeqByte(rng[], messageLen)
padded = pkcs7_pad(message, NoisePaddingBlockSize)
@ -50,7 +50,7 @@ procSuite "Waku Noise":
ciphertext: ChaChaPolyCiphertext = encrypt(cipherState, plaintext)
decryptedCiphertext: seq[byte] = decrypt(cipherState, ciphertext)
check:
check:
plaintext == decryptedCiphertext
test "ChaChaPoly Encryption/Decryption: random strings":
@ -66,26 +66,26 @@ procSuite "Waku Noise":
ciphertext: ChaChaPolyCiphertext = encrypt(cipherState, plaintext.toBytes())
decryptedCiphertext: seq[byte] = decrypt(cipherState, ciphertext)
check:
check:
plaintext.toBytes() == decryptedCiphertext
test "Noise public keys: encrypt and decrypt a public key":
let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[])
let
let
cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[])
encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey)
decryptedPk: NoisePublicKey = decryptNoisePublicKey(cs, encryptedPk)
check:
check:
noisePublicKey == decryptedPk
test "Noise public keys: decrypt an unencrypted public key":
let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[])
let
let
cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[])
decryptedPk: NoisePublicKey = decryptNoisePublicKey(cs, noisePublicKey)
@ -100,7 +100,7 @@ procSuite "Waku Noise":
cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[])
encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey)
encryptedPk2: NoisePublicKey = encryptNoisePublicKey(cs, encryptedPk)
check:
encryptedPk == encryptedPk2
@ -114,12 +114,12 @@ procSuite "Waku Noise":
decryptedPk: NoisePublicKey = decryptNoisePublicKey(cs, encryptedPk)
decryptedPk2: NoisePublicKey = decryptNoisePublicKey(cs, decryptedPk)
check:
check:
decryptedPk == decryptedPk2
test "Noise public keys: serialize and deserialize an unencrypted public key":
let
let
noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[])
serializedNoisePublicKey: seq[byte] = serializeNoisePublicKey(noisePublicKey)
deserializedNoisePublicKey: NoisePublicKey = intoNoisePublicKey(serializedNoisePublicKey)
@ -131,7 +131,7 @@ procSuite "Waku Noise":
let noisePublicKey: NoisePublicKey = genNoisePublicKey(rng[])
let
let
cs: ChaChaPolyCipherState = randomChaChaPolyCipherState(rng[])
encryptedPk: NoisePublicKey = encryptNoisePublicKey(cs, noisePublicKey)
serializedNoisePublicKey: seq[byte] = serializeNoisePublicKey(encryptedPk)
@ -159,7 +159,7 @@ procSuite "Waku Noise":
test "PayloadV2: Encode/Decode a Waku Message (version 2) to a PayloadV2":
# We encode to a WakuMessage a random PayloadV2
let
payload2 = randomPayloadV2(rng[])
@ -173,7 +173,7 @@ procSuite "Waku Noise":
# We decode the WakuMessage from the ProtoBuffer
let msgFromPb = WakuMessage.decode(pb.buffer)
check:
msgFromPb.isOk()
@ -202,7 +202,7 @@ procSuite "Waku Noise":
test "Noise State Machine: Cipher State primitives":
# We generate a random Cipher State, associated data ad and plaintext
var
var
cipherState: CipherState = randomCipherState(rng[])
nonce: uint64 = uint64(rand(0 .. int.high))
ad: seq[byte] = randomSeqByte(rng[], rand(1..128))
@ -210,13 +210,13 @@ procSuite "Waku Noise":
# We set the random nonce generated in the cipher state
setNonce(cipherState, nonce)
# We perform encryption
var ciphertext: seq[byte] = encryptWithAd(cipherState, ad, plaintext)
# After any encryption/decryption operation, the Cipher State's nonce increases by 1
check:
getNonce(cipherState) == nonce + 1
getNonce(cipherState) == nonce + 1
# We set the nonce back to its original value for decryption
setNonce(cipherState, nonce)
@ -226,7 +226,7 @@ procSuite "Waku Noise":
# We check if encryption and decryption are correct and that nonce correctly increased after decryption
check:
getNonce(cipherState) == nonce + 1
getNonce(cipherState) == nonce + 1
plaintext == decrypted
# If a Cipher State has no key set, encryptWithAd should return the plaintext without increasing the nonce
@ -254,7 +254,7 @@ procSuite "Waku Noise":
# A Cipher State cannot have a nonce greater or equal 2^64-1
# Note that NonceMax is uint64.high - 1 = 2^64-1-1 and that nonce is increased after each encryption and decryption operation
# We generate a test Cipher State with nonce set to MaxNonce
cipherState = randomCipherState(rng[])
setNonce(cipherState, NonceMax)
@ -264,10 +264,10 @@ procSuite "Waku Noise":
for _ in [1..5]:
expect NoiseNonceMaxError:
ciphertext = encryptWithAd(cipherState, ad, plaintext)
check:
getNonce(cipherState) == NonceMax + 1
# We generate a test Cipher State
# Since nonce is increased after decryption as well, we need to generate a proper ciphertext in order to test MaxNonceError error handling
# We cannot call encryptWithAd to encrypt a plaintext using a nonce equal MaxNonce, since this will trigger a MaxNonceError.
@ -275,7 +275,7 @@ procSuite "Waku Noise":
cipherState = randomCipherState(rng[])
setNonce(cipherState, NonceMax)
plaintext = randomSeqByte(rng[], rand(1..128))
# We perform encryption using the Cipher State key, NonceMax and ad
# By Noise specification the nonce is 8 bytes long out of the 12 bytes supported by ChaChaPoly, thus we copy the Little endian conversion of the nonce to a ChaChaPolyNonce
var
@ -295,14 +295,14 @@ procSuite "Waku Noise":
for _ in [1..5]:
expect NoiseNonceMaxError:
plaintext = decryptWithAd(cipherState, ad, ciphertext)
check:
getNonce(cipherState) == NonceMax + 1
test "Noise State Machine: Symmetric State primitives":
# We select one supported handshake pattern and we initialize a symmetric state
var
var
hsPattern = NoiseHandshakePatterns["XX"]
symmetricState: SymmetricState = SymmetricState.init(hsPattern)
@ -343,7 +343,7 @@ procSuite "Waku Noise":
var inputKeyMaterial = randomSeqByte(rng[], rand(1..128))
mixKey(symmetricState, inputKeyMaterial)
# mixKey changes the Symmetric State's chaining key and encryption key of the embedded Cipher State
# mixKey changes the Symmetric State's chaining key and encryption key of the embedded Cipher State
# It further sets to 0 the nonce of the embedded Cipher State
check:
getKey(cs) != getKey(getCipherState(symmetricState))
@ -399,7 +399,7 @@ procSuite "Waku Noise":
# We restore the symmetric State to its initial value to test decryption
symmetricState = initialSymmetricState
# We execute decryptAndHash over the ciphertext
var decrypted = decryptAndHash(symmetricState, ciphertext)
@ -422,7 +422,7 @@ procSuite "Waku Noise":
check:
getChainingKey(symmetricState) != EmptyKey
# When a Symmetric State's ck is non-empty, we can execute split, which creates two distinct Cipher States cs1 and cs2
# When a Symmetric State's ck is non-empty, we can execute split, which creates two distinct Cipher States cs1 and cs2
# with non-empty encryption keys and nonce set to 0
var (cs1, cs2) = split(symmetricState)
@ -443,11 +443,11 @@ procSuite "Waku Noise":
let bobStaticKey = genKeyPair(rng[])
var bobHS = initialize(hsPattern = hsPattern, staticKey = bobStaticKey)
var
var
sentTransportMessage: seq[byte]
aliceStep, bobStep: HandshakeStepResult
aliceStep, bobStep: HandshakeStepResult
# Here the handshake starts
# Write and read calls alternate between Alice and Bob: the handhshake progresses by alternatively calling stepHandshake for each user
@ -458,20 +458,20 @@ procSuite "Waku Noise":
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# and the (encrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get()
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
###############
# 2nd step
###############
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
@ -480,8 +480,8 @@ procSuite "Waku Noise":
# While Alice reads and returns the (decrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get()
check:
check:
aliceStep.transportMessage == sentTransportMessage
###############
@ -489,36 +489,36 @@ procSuite "Waku Noise":
###############
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
sentTransportMessage = randomSeqByte(rng[], 32)
# Similarly as in first step, Alice writes a Waku2 payload containing the handshake message and the (encrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get()
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
check:
bobStep.transportMessage == sentTransportMessage
# Note that for this handshake pattern, no more message patterns are left for processing
# Note that for this handshake pattern, no more message patterns are left for processing
# Another call to stepHandshake would return an empty HandshakeStepResult
# We test that extra calls to stepHandshake do not affect parties' handshake states
# and that the intermediate HandshakeStepResult are empty
let prevAliceHS = aliceHS
let prevBobHS = bobHS
let bobStep1 = stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get()
let aliceStep1 = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep1.payload2).get()
let aliceStep2 = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get()
let bobStep2 = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep2.payload2).get()
check:
aliceStep1 == default(HandshakeStepResult)
aliceStep2 == default(HandshakeStepResult)
bobStep1 == default(HandshakeStepResult)
bobStep2 == default(HandshakeStepResult)
aliceHS == prevAliceHS
bobHS == prevBobHS
aliceHS == prevAliceHS
bobHS == prevBobHS
#########################
# After Handshake
@ -531,7 +531,7 @@ procSuite "Waku Noise":
bobHSResult = finalizeHandshake(bobHS)
# We test read/write of random messages exchanged between Alice and Bob
var
var
payload2: PayloadV2
message: seq[byte]
readMessage: seq[byte]
@ -543,15 +543,15 @@ procSuite "Waku Noise":
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get()
check:
check:
message == readMessage
# Bob writes to Alice
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get()
check:
message == readMessage
@ -561,18 +561,18 @@ procSuite "Waku Noise":
# We generate a random psk
let psk = randomSeqByte(rng[], 32)
# We initialize Alice's and Bob's Handshake State
let aliceStaticKey = genKeyPair(rng[])
var aliceHS = initialize(hsPattern = hsPattern, staticKey = aliceStaticKey, psk = psk, initiator = true)
let bobStaticKey = genKeyPair(rng[])
var bobHS = initialize(hsPattern = hsPattern, staticKey = bobStaticKey, psk = psk)
var
var
sentTransportMessage: seq[byte]
aliceStep, bobStep: HandshakeStepResult
aliceStep, bobStep: HandshakeStepResult
# Here the handshake starts
# Write and read calls alternate between Alice and Bob: the handhshake progresses by alternatively calling stepHandshake for each user
@ -582,14 +582,14 @@ procSuite "Waku Noise":
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# and the (encrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get()
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
@ -605,8 +605,8 @@ procSuite "Waku Noise":
# While Alice reads and returns the (decrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get()
check:
check:
aliceStep.transportMessage == sentTransportMessage
###############
@ -621,12 +621,12 @@ procSuite "Waku Noise":
# Bob reads Alice's payloads, and returns the (decrypted) transportMessage alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
# Note that for this handshake pattern, no more message patterns are left for processing
# Note that for this handshake pattern, no more message patterns are left for processing
#########################
# After Handshake
#########################
@ -638,7 +638,7 @@ procSuite "Waku Noise":
bobHSResult = finalizeHandshake(bobHS)
# We test read/write of random messages exchanged between Alice and Bob
var
var
payload2: PayloadV2
message: seq[byte]
readMessage: seq[byte]
@ -650,15 +650,15 @@ procSuite "Waku Noise":
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get()
check:
check:
message == readMessage
# Bob writes to Alice
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get()
check:
message == readMessage
@ -679,11 +679,11 @@ procSuite "Waku Noise":
var aliceHS = initialize(hsPattern = hsPattern, staticKey = aliceStaticKey, preMessagePKs = preMessagePKs, initiator = true)
var bobHS = initialize(hsPattern = hsPattern, staticKey = bobStaticKey, preMessagePKs = preMessagePKs)
var
var
sentTransportMessage: seq[byte]
aliceStep, bobStep: HandshakeStepResult
aliceStep, bobStep: HandshakeStepResult
# Here the handshake starts
# Write and read calls alternate between Alice and Bob: the handhshake progresses by alternatively calling stepHandshake for each user
@ -691,16 +691,16 @@ procSuite "Waku Noise":
# 1st step
###############
# We generate a random transport message
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# and the (encrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get()
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
@ -710,13 +710,13 @@ procSuite "Waku Noise":
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
# At this step, Bob writes and returns a payload
bobStep = stepHandshake(rng[], bobHS, transportMessage = sentTransportMessage).get()
# While Alice reads and returns the (decrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get()
check:
aliceStep.transportMessage == sentTransportMessage
@ -732,12 +732,12 @@ procSuite "Waku Noise":
# Bob reads Alice's payloads, and returns the (decrypted) transportMessage alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
# Note that for this handshake pattern, no more message patterns are left for processing
# Note that for this handshake pattern, no more message patterns are left for processing
#########################
# After Handshake
#########################
@ -749,7 +749,7 @@ procSuite "Waku Noise":
bobHSResult = finalizeHandshake(bobHS)
# We test read/write of random messages between Alice and Bob
var
var
payload2: PayloadV2
message: seq[byte]
readMessage: seq[byte]
@ -761,15 +761,15 @@ procSuite "Waku Noise":
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get()
check:
check:
message == readMessage
# Bob writes to Alice
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get()
check:
message == readMessage
@ -790,11 +790,11 @@ procSuite "Waku Noise":
var aliceHS = initialize(hsPattern = hsPattern, staticKey = aliceStaticKey, preMessagePKs = preMessagePKs, initiator = true)
var bobHS = initialize(hsPattern = hsPattern, staticKey = bobStaticKey, preMessagePKs = preMessagePKs)
var
var
sentTransportMessage: seq[byte]
aliceStep, bobStep: HandshakeStepResult
aliceStep, bobStep: HandshakeStepResult
# Here the handshake starts
# Write and read calls alternate between Alice and Bob: the handhshake progresses by alternatively calling stepHandshake for each user
@ -805,13 +805,13 @@ procSuite "Waku Noise":
# We generate a random transport message
sentTransportMessage = randomSeqByte(rng[], 32)
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# and the (encrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage).get()
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
@ -827,7 +827,7 @@ procSuite "Waku Noise":
# While Alice reads and returns the (decrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = bobStep.payload2).get()
check:
aliceStep.transportMessage == sentTransportMessage
@ -843,12 +843,12 @@ procSuite "Waku Noise":
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = aliceStep.payload2).get()
check:
bobStep.transportMessage == sentTransportMessage
# Note that for this handshake pattern, no more message patterns are left for processing
# Note that for this handshake pattern, no more message patterns are left for processing
#########################
# After Handshake
#########################
@ -860,7 +860,7 @@ procSuite "Waku Noise":
bobHSResult = finalizeHandshake(bobHS)
# We test read/write of random messages exchanged between Alice and Bob
var
var
payload2: PayloadV2
message: seq[byte]
readMessage: seq[byte]
@ -872,14 +872,14 @@ procSuite "Waku Noise":
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(aliceHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(bobHSResult, payload2, defaultMessageNametagBuffer).get()
check:
check:
message == readMessage
# Bob writes to Alice
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(bobHSResult, message, defaultMessageNametagBuffer)
readMessage = readMessage(aliceHSResult, payload2, defaultMessageNametagBuffer).get()
check:
message == readMessage

View File

@ -1,12 +1,12 @@
{.used.}
import
std/[random,tables],
stew/byteutils,
std/[random, tables],
stew/[results, byteutils],
testutils/unittests,
libp2p/protobuf/minprotobuf
import
../../waku/v2/node/waku_payload,
../../waku/v2/utils/noise as waku_message_utils,
../../waku/v2/protocol/waku_noise/noise_types,
../../waku/v2/protocol/waku_noise/noise_utils,
../../waku/v2/protocol/waku_noise/noise_handshake_processing,
@ -14,13 +14,13 @@ import
../test_helpers
procSuite "Waku Noise Sessions":
# We initialize the RNG in test_helpers
let rng = rng()
# We initialize the RNG in std/random
randomize()
# This test implements the Device pairing and Secure Transfers with Noise
# This test implements the Device pairing and Secure Transfers with Noise
# detailed in the 43/WAKU2-DEVICE-PAIRING RFC https://rfc.vac.dev/spec/43/
test "Noise Waku Pairing Handhshake and Secure transfer":
@ -65,7 +65,7 @@ procSuite "Waku Noise Sessions":
bobCommittedStaticKey == readCommittedStaticKey
# We set the contentTopic from the content topic parameters exchanged in the QR
let contentTopic: ContentTopic = "/" & applicationName & "/" & applicationVersion & "/wakunoise/1/sessions_shard-" & shardId & "/proto"
let contentTopic: ContentTopic = "/" & applicationName & "/" & applicationVersion & "/wakunoise/1/sessions_shard-" & shardId & "/proto"
###############
# Pre-handshake message
@ -74,7 +74,7 @@ procSuite "Waku Noise Sessions":
###############
let preMessagePKs: seq[NoisePublicKey] = @[toNoisePublicKey(getPublicKey(bobEphemeralKey))]
# We initialize the Handshake states.
# We initialize the Handshake states.
# Note that we pass the whole qr serialization as prologue information
var aliceHS = initialize(hsPattern = hsPattern, ephemeralKey = aliceEphemeralKey, staticKey = aliceStaticKey, prologue = qr.toBytes, preMessagePKs = preMessagePKs, initiator = true)
var bobHS = initialize(hsPattern = hsPattern, ephemeralKey = bobEphemeralKey, staticKey = bobStaticKey, prologue = qr.toBytes, preMessagePKs = preMessagePKs)
@ -82,25 +82,25 @@ procSuite "Waku Noise Sessions":
###############
# Pairing Handshake
###############
var
var
sentTransportMessage: seq[byte]
aliceStep, bobStep: HandshakeStepResult
msgFromPb: ProtoResult[WakuMessage]
wakuMsg: WakuResult[WakuMessage]
wakuMsg: Result[WakuMessage, cstring]
pb: ProtoBuffer
readPayloadV2: PayloadV2
aliceMessageNametag, bobMessageNametag: MessageNametag
# Write and read calls alternate between Alice and Bob: the handhshake progresses by alternatively calling stepHandshake for each user
###############
# 1st step
#
#
# -> eA, eAeB {H(sA||s)} [authcode]
###############
# The messageNametag for the first handshake message is randomly generated and exchanged out-of-band
# The messageNametag for the first handshake message is randomly generated and exchanged out-of-band
# and corresponds to qrMessageNametag
# We set the transport message to be H(sA||s)
@ -110,7 +110,7 @@ procSuite "Waku Noise Sessions":
check:
seqToDigest256(sentTransportMessage) == aliceCommittedStaticKey
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# By being the handshake initiator, Alice writes a Waku2 payload v2 containing her handshake message
# and the (encrypted) transport message
# The message is sent with a messageNametag equal to the one received through the QR code
aliceStep = stepHandshake(rng[], aliceHS, transportMessage = sentTransportMessage, messageNametag = qrMessageNametag).get()
@ -129,7 +129,7 @@ procSuite "Waku Noise Sessions":
# We decode the WakuMessage from the ProtoBuffer
msgFromPb = WakuMessage.decode(pb.buffer)
check:
msgFromPb.isOk()
@ -143,18 +143,18 @@ procSuite "Waku Noise Sessions":
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
# Note that Bob verifies if the received payloadv2 has the expected messageNametag set
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = readPayloadV2, messageNametag = qrMessageNametag).get()
check:
bobStep.transportMessage == sentTransportMessage
# We generate an authorization code using the handshake state
let aliceAuthcode = genAuthcode(aliceHS)
let bobAuthcode = genAuthcode(bobHS)
# We check that they are equal. Note that this check has to be confirmed with a user interaction.
check:
aliceAuthcode == bobAuthcode
###############
# 2nd step
#
@ -186,7 +186,7 @@ procSuite "Waku Noise Sessions":
# We decode the WakuMessage from the ProtoBuffer
msgFromPb = WakuMessage.decode(pb.buffer)
check:
msgFromPb.isOk()
@ -199,7 +199,7 @@ procSuite "Waku Noise Sessions":
# While Alice reads and returns the (decrypted) transport message
aliceStep = stepHandshake(rng[], aliceHS, readPayloadV2 = readPayloadV2, messageNametag = aliceMessageNametag).get()
check:
aliceStep.transportMessage == sentTransportMessage
@ -208,7 +208,7 @@ procSuite "Waku Noise Sessions":
check:
expectedBobCommittedStaticKey == bobCommittedStaticKey
###############
# 3rd step
#
@ -239,7 +239,7 @@ procSuite "Waku Noise Sessions":
# We decode the WakuMessage from the ProtoBuffer
msgFromPb = WakuMessage.decode(pb.buffer)
check:
msgFromPb.isOk()
@ -252,7 +252,7 @@ procSuite "Waku Noise Sessions":
# Bob reads Alice's payloads, and returns the (decrypted) transport message Alice sent to him
bobStep = stepHandshake(rng[], bobHS, readPayloadV2 = readPayloadV2, messageNametag = bobMessageNametag).get()
check:
bobStep.transportMessage == sentTransportMessage
@ -261,7 +261,7 @@ procSuite "Waku Noise Sessions":
check:
expectedAliceCommittedStaticKey == aliceCommittedStaticKey
#########################
# Secure Transfer Phase
#########################
@ -273,7 +273,7 @@ procSuite "Waku Noise Sessions":
bobHSResult = finalizeHandshake(bobHS)
# We test read/write of random messages exchanged between Alice and Bob
var
var
payload2: PayloadV2
message: seq[byte]
readMessage: seq[byte]
@ -286,15 +286,15 @@ procSuite "Waku Noise Sessions":
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(aliceHSResult, message, outboundMessageNametagBuffer = aliceHSResult.nametagsOutbound)
readMessage = readMessage(bobHSResult, payload2, inboundMessageNametagBuffer = bobHSResult.nametagsInbound).get()
check:
check:
message == readMessage
# Bob writes to Alice
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(bobHSResult, message, outboundMessageNametagBuffer = bobHSResult.nametagsOutbound)
readMessage = readMessage(aliceHSResult, payload2, inboundMessageNametagBuffer = aliceHSResult.nametagsInbound).get()
check:
message == readMessage
@ -312,7 +312,7 @@ procSuite "Waku Noise Sessions":
message = randomSeqByte(rng[], 32)
payload2 = writeMessage(bobHSResult, message, outboundMessageNametagBuffer = bobHSResult.nametagsOutbound)
readMessage = readMessage(aliceHSResult, payload2, inboundMessageNametagBuffer = aliceHSResult.nametagsInbound).get()
check:
message == readMessage

View File

@ -7,13 +7,10 @@ import
import
libp2p/protocols/ping,
libp2p/crypto/[crypto, secp],
libp2p/nameresolving/nameresolver,
libp2p/nameresolving/dnsresolver
import
../../waku/v2/node/peer_manager,
../../waku/v2/utils/peers,
../../waku/v2/node/waku_node,
../../waku/v2/node/waku_payload,
../../waku/v2/utils/peers
# protocols and their tag
@ -60,7 +57,7 @@ type
proc parseCmdArg*(T: type chronos.Duration, p: string): T =
try:
result = chronos.seconds(parseInt(p))
except CatchableError as e:
except CatchableError:
raise newException(ConfigurationError, "Invalid timeout value")
proc completeCmdArg*(T: type chronos.Duration, val: string): seq[string] =

View File

@ -9,8 +9,8 @@ import
../../protocol/waku_message,
../../protocol/waku_store,
../../protocol/waku_store/rpc,
../../utils/compat,
../../utils/time,
../waku_payload,
./hexstrings,
./jsonrpc_types

View File

@ -4,17 +4,18 @@ else:
{.push raises: [].}
import
std/[tables,sequtils],
std/[tables, sequtils],
chronicles,
eth/keys,
json_rpc/rpcserver,
nimcrypto/sysrand,
nimcrypto/sysrand
import
../../utils/compat,
../waku_node,
../waku_payload,
./jsonrpc_types,
./jsonrpc_utils
export waku_payload, jsonrpc_types
export compat, jsonrpc_types
logScope:
topics = "waku node jsonrpc private_api"
@ -25,7 +26,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
## Private API version 1 definitions
## Definitions for symmetric cryptography
rpcsrv.rpc("get_waku_v2_private_v1_symmetric_key") do() -> SymKey:
## Generates and returns a symmetric key for message encryption and decryption
debug "get_waku_v2_private_v1_symmetric_key"
@ -42,7 +43,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
let msg = message.toWakuMessage(version = 1,
rng = node.rng,
pubKey = none(waku_payload.PublicKey),
pubKey = none(compat.PublicKey),
symkey = some(symkey.toSymKey()))
if (await node.publish(topic, msg).withTimeout(futTimeout)):
@ -56,7 +57,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
## Returns all WakuMessages received on a PubSub topic since the
## last time this method was called. Decrypts the message payloads
## before returning.
##
##
## @TODO ability to specify a return message limit
debug "get_waku_v2_private_v1_symmetric_messages", topic=topic
@ -65,18 +66,18 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
# Clear cache before next call
topicCache[topic] = @[]
return msgs.mapIt(it.toWakuRelayMessage(symkey = some(symkey.toSymKey()),
privateKey = none(waku_payload.PrivateKey)))
privateKey = none(compat.PrivateKey)))
else:
# Not subscribed to this topic
raise newException(ValueError, "Not subscribed to topic: " & topic)
## Definitions for asymmetric cryptography
rpcsrv.rpc("get_waku_v2_private_v1_asymmetric_keypair") do() -> WakuKeyPair:
## Generates and returns a public/private key pair for asymmetric message encryption and decryption.
debug "get_waku_v2_private_v1_asymmetric_keypair"
let privKey = waku_payload.PrivateKey.random(node.rng[])
let privKey = compat.PrivateKey.random(node.rng[])
return WakuKeyPair(seckey: privKey, pubkey: privKey.toPublicKey())
@ -100,7 +101,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, topicCache: T
## Returns all WakuMessages received on a PubSub topic since the
## last time this method was called. Decrypts the message payloads
## before returning.
##
##
## @TODO ability to specify a return message limit
debug "get_waku_v2_private_v1_asymmetric_messages", topic=topic

View File

@ -2,8 +2,6 @@
##
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-message.md
## for spec.
##
## For payload content and encryption, see waku/v2/node/waku_payload.nim
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}

View File

@ -3,13 +3,14 @@ when (NimMajor, NimMinor) < (1, 4):
else:
{.push raises: [].}
import
std/options,
eth/keys,
stew/results,
eth/keys
import
../../whisper/whisper_types,
../protocol/waku_message,
../protocol/waku_noise/noise_types,
../protocol/waku_noise/noise_utils
../protocol/waku_message
export whisper_types, keys, options
@ -28,9 +29,6 @@ type
of None:
discard
# NOTE: Currently only used here, if we start using it elsewhere pull it out.
WakuResult*[T] = Result[T, cstring]
# TODO:
# - This is using `DecodedPayload` from Waku v1 / Whisper and could be altered
@ -41,7 +39,7 @@ type
# - For now this `KeyInfo` is a bit silly also, but perhaps with v2 or
# adjustments to Waku v1 encoding, it can be better.
proc decodePayload*(message: WakuMessage, keyInfo: KeyInfo):
WakuResult[DecodedPayload] =
Result[DecodedPayload, cstring] =
case message.version
of 0:
return ok(DecodedPayload(payload:message.payload))
@ -69,7 +67,7 @@ proc decodePayload*(message: WakuMessage, keyInfo: KeyInfo):
# TODO: same story as for `decodedPayload`, but then regarding the `Payload`
# object.
proc encode*(payload: Payload, version: uint32, rng: var HmacDrbgContext):
WakuResult[seq[byte]] =
Result[seq[byte], cstring] =
case version
of 0:
# This is rather silly
@ -82,36 +80,3 @@ proc encode*(payload: Payload, version: uint32, rng: var HmacDrbgContext):
return err("Couldn't encode the payload")
else:
return err("Unsupported WakuMessage version")
# Decodes a WakuMessage to a PayloadV2
# Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
proc decodePayloadV2*(message: WakuMessage): WakuResult[PayloadV2]
{.raises: [Defect, NoiseMalformedHandshake, NoisePublicKeyError].} =
# We check message version (only 2 is supported in this proc)
case message.version
of 2:
# We attempt to decode the WakuMessage payload
let deserializedPayload2 = deserializePayloadV2(message.payload)
if deserializedPayload2.isOk():
return ok(deserializedPayload2.get())
else:
return err("Failed to decode WakuMessage")
else:
return err("Wrong message version while decoding payload")
# Encodes a PayloadV2 to a WakuMessage
# Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
proc encodePayloadV2*(payload2: PayloadV2, contentTopic: ContentTopic = default(ContentTopic)): WakuResult[WakuMessage]
{.raises: [Defect, NoiseMalformedHandshake, NoisePublicKeyError].} =
# We attempt to encode the PayloadV2
let serializedPayload2 = serializePayloadV2(payload2)
if not serializedPayload2.isOk():
return err("Failed to encode PayloadV2")
# If successful, we create and return a WakuMessage
let msg = WakuMessage(payload: serializedPayload2.get(), version: 2, contentTopic: contentTopic)
return ok(msg)

44
waku/v2/utils/noise.nim Normal file
View File

@ -0,0 +1,44 @@
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import
stew/results
import
../protocol/waku_message,
../protocol/waku_noise/noise_types,
../protocol/waku_noise/noise_utils
# Decodes a WakuMessage to a PayloadV2
# Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
proc decodePayloadV2*(message: WakuMessage): Result[PayloadV2, cstring]
{.raises: [NoiseMalformedHandshake, NoisePublicKeyError].} =
# We check message version (only 2 is supported in this proc)
case message.version
of 2:
# We attempt to decode the WakuMessage payload
let deserializedPayload2 = deserializePayloadV2(message.payload)
if deserializedPayload2.isOk():
return ok(deserializedPayload2.get())
else:
return err("Failed to decode WakuMessage")
else:
return err("Wrong message version while decoding payload")
# Encodes a PayloadV2 to a WakuMessage
# Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
proc encodePayloadV2*(payload2: PayloadV2, contentTopic: ContentTopic = default(ContentTopic)): Result[WakuMessage, cstring]
{.raises: [NoiseMalformedHandshake, NoisePublicKeyError].} =
# We attempt to encode the PayloadV2
let serializedPayload2 = serializePayloadV2(payload2)
if not serializedPayload2.isOk():
return err("Failed to encode PayloadV2")
# If successful, we create and return a WakuMessage
let msg = WakuMessage(payload: serializedPayload2.get(), version: 2, contentTopic: contentTopic)
return ok(msg)