diff --git a/waku/v2/protocol/waku_noise/noise_handshake_processing.nim b/waku/v2/protocol/waku_noise/noise_handshake_processing.nim index d99b3bb63..8d72a50d9 100644 --- a/waku/v2/protocol/waku_noise/noise_handshake_processing.nim +++ b/waku/v2/protocol/waku_noise/noise_handshake_processing.nim @@ -355,7 +355,7 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt trace "noise write s" # If the local static key is not set (the handshake state was not properly initialized), we raise an error - if hs.s == default(KeyPair): + if isDefault(hs.s): raise newException(NoisePublicKeyError, "Static key not set") # We encrypt the public part of the static key in case a key is set in the Cipher State @@ -386,7 +386,7 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt trace "noise dh ee" # If local and/or remote ephemeral keys are not set, we raise an error - if hs.e == default(KeyPair) or hs.re == default(Curve25519Key): + if isDefault(hs.e) or isDefault(hs.re): raise newException(NoisePublicKeyError, "Local or remote ephemeral key not set") # Calls MixKey(DH(e, re)). @@ -401,11 +401,11 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt # We check if keys are correctly set. # If both present, we call MixKey(DH(e, rs)) if initiator, MixKey(DH(s, re)) if responder. if hs.initiator: - if hs.e == default(KeyPair) or hs.rs == default(Curve25519Key): + if isDefault(hs.e) or isDefault(hs.rs): raise newException(NoisePublicKeyError, "Local or remote ephemeral/static key not set") hs.ss.mixKey(dh(hs.e.privateKey, hs.rs)) else: - if hs.re == default(Curve25519Key) or hs.s == default(KeyPair): + if isDefault(hs.re) or isDefault(hs.s): raise newException(NoisePublicKeyError, "Local or remote ephemeral/static key not set") hs.ss.mixKey(dh(hs.s.privateKey, hs.re)) @@ -418,11 +418,11 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt # We check if keys are correctly set. # If both present, call MixKey(DH(s, re)) if initiator, MixKey(DH(e, rs)) if responder. if hs.initiator: - if hs.s == default(KeyPair) or hs.re == default(Curve25519Key): + if isDefault(hs.s) or isDefault(hs.re): raise newException(NoiseMalformedHandshake, "Local or remote ephemeral/static key not set") hs.ss.mixKey(dh(hs.s.privateKey, hs.re)) else: - if hs.rs == default(Curve25519Key) or hs.e == default(KeyPair): + if isDefault(hs.rs) or isDefault(hs.e): raise newException(NoiseMalformedHandshake, "Local or remote ephemeral/static key not set") hs.ss.mixKey(dh(hs.e.privateKey, hs.rs)) @@ -433,7 +433,7 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt trace "noise dh ss" # If local and/or remote static keys are not set, we raise an error - if hs.s == default(KeyPair) or hs.rs == default(Curve25519Key): + if isDefault(hs.s) or isDefault(hs.rs): raise newException(NoiseMalformedHandshake, "Local or remote static key not set") # Calls MixKey(DH(s, rs)). diff --git a/waku/v2/protocol/waku_noise/noise_utils.nim b/waku/v2/protocol/waku_noise/noise_utils.nim index 9fd30d86f..e58bcbdfa 100644 --- a/waku/v2/protocol/waku_noise/noise_utils.nim +++ b/waku/v2/protocol/waku_noise/noise_utils.nim @@ -57,6 +57,11 @@ proc pkcs7_unpad*(payload: seq[byte], paddingSize: int): seq[byte] = let unpadded = payload[0..payload.high-k.int] return unpadded +# Simple utility that checks if the given variable is "default", +# Therefore, it has not been initialized +proc isDefault*[T](value: T): bool = + value == static(default(T)) + ################################################################# ################################# @@ -406,4 +411,4 @@ proc deserializePayloadV2*(payload: seq[byte]): Result[PayloadV2, cstring] payload2.transportMessage = payload[i..i+transportMessageLen-1] i += transportMessageLen - return ok(payload2) \ No newline at end of file + return ok(payload2)