mirror of https://github.com/waku-org/nwaku.git
chore(noise): minor refactor of default checks of keypairs (#1104)
* chore(noise): readable default checks for keypairs * chore(noise): fix naming of arg in exists proc * chore(noise): fix naming to make it clearer * chore(noise): inline and remove result usage * chore(noise): move the isDefault function up, replace defaultyVar with value * chore(noise): inline style * chore(noise): static the default check, remove inline pragma
This commit is contained in:
parent
58e6e28dd5
commit
ea62e2b7ce
|
@ -355,7 +355,7 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt
|
||||||
trace "noise write s"
|
trace "noise write s"
|
||||||
|
|
||||||
# If the local static key is not set (the handshake state was not properly initialized), we raise an error
|
# 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")
|
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
|
# 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"
|
trace "noise dh ee"
|
||||||
|
|
||||||
# If local and/or remote ephemeral keys are not set, we raise an error
|
# 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")
|
raise newException(NoisePublicKeyError, "Local or remote ephemeral key not set")
|
||||||
|
|
||||||
# Calls MixKey(DH(e, re)).
|
# Calls MixKey(DH(e, re)).
|
||||||
|
@ -401,11 +401,11 @@ proc processMessagePatternTokens(rng: var BrHmacDrbgContext, hs: var HandshakeSt
|
||||||
# We check if keys are correctly set.
|
# 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 both present, we call MixKey(DH(e, rs)) if initiator, MixKey(DH(s, re)) if responder.
|
||||||
if hs.initiator:
|
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")
|
raise newException(NoisePublicKeyError, "Local or remote ephemeral/static key not set")
|
||||||
hs.ss.mixKey(dh(hs.e.privateKey, hs.rs))
|
hs.ss.mixKey(dh(hs.e.privateKey, hs.rs))
|
||||||
else:
|
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")
|
raise newException(NoisePublicKeyError, "Local or remote ephemeral/static key not set")
|
||||||
hs.ss.mixKey(dh(hs.s.privateKey, hs.re))
|
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.
|
# We check if keys are correctly set.
|
||||||
# If both present, call MixKey(DH(s, re)) if initiator, MixKey(DH(e, rs)) if responder.
|
# If both present, call MixKey(DH(s, re)) if initiator, MixKey(DH(e, rs)) if responder.
|
||||||
if hs.initiator:
|
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")
|
raise newException(NoiseMalformedHandshake, "Local or remote ephemeral/static key not set")
|
||||||
hs.ss.mixKey(dh(hs.s.privateKey, hs.re))
|
hs.ss.mixKey(dh(hs.s.privateKey, hs.re))
|
||||||
else:
|
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")
|
raise newException(NoiseMalformedHandshake, "Local or remote ephemeral/static key not set")
|
||||||
hs.ss.mixKey(dh(hs.e.privateKey, hs.rs))
|
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"
|
trace "noise dh ss"
|
||||||
|
|
||||||
# If local and/or remote static keys are not set, we raise an error
|
# 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")
|
raise newException(NoiseMalformedHandshake, "Local or remote static key not set")
|
||||||
|
|
||||||
# Calls MixKey(DH(s, rs)).
|
# Calls MixKey(DH(s, rs)).
|
||||||
|
|
|
@ -57,6 +57,11 @@ proc pkcs7_unpad*(payload: seq[byte], paddingSize: int): seq[byte] =
|
||||||
let unpadded = payload[0..payload.high-k.int]
|
let unpadded = payload[0..payload.high-k.int]
|
||||||
return unpadded
|
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]
|
payload2.transportMessage = payload[i..i+transportMessageLen-1]
|
||||||
i += transportMessageLen
|
i += transportMessageLen
|
||||||
|
|
||||||
return ok(payload2)
|
return ok(payload2)
|
||||||
|
|
Loading…
Reference in New Issue