diff --git a/src/codec.ts b/src/codec.ts index bba6998..5c94809 100644 --- a/src/codec.ts +++ b/src/codec.ts @@ -6,7 +6,7 @@ import { MessageV0 } from "js-waku/lib/waku_message/version_0"; import { HandshakeResult, HandshakeStepResult } from "./handshake.js"; import { PayloadV2 } from "./payload.js"; -const log = debug("waku:message:noise-encoder"); +const log = debug("waku:message:noise-codec"); const OneMillion = BigInt(1_000_000); diff --git a/src/handshake_state.ts b/src/handshake_state.ts index f6fdaeb..44d5292 100644 --- a/src/handshake_state.ts +++ b/src/handshake_state.ts @@ -1,3 +1,4 @@ +import debug from "debug"; import * as pkcs7 from "pkcs7-padding"; import { equals as uint8ArrayEquals } from "uint8arrays/equals"; @@ -9,6 +10,8 @@ import { SymmetricState } from "./noise.js"; import { EmptyPreMessage, HandshakePattern, MessageDirection, NoiseTokens, PreMessagePattern } from "./patterns.js"; import { NoisePublicKey } from "./publickey.js"; +const log = debug("waku:noise:handshake-state"); + // The padding blocksize of a transport message export const NoisePaddingBlockSize = 248; @@ -208,7 +211,7 @@ export class HandshakeState { // If user is reading the "e" token if (reading) { - console.debug("noise pre-message read e"); + log("noise pre-message read e"); // We check if current key is encrypted or not. We assume pre-message public keys are all unencrypted on users' end if (currPK.flag == 0) { @@ -220,7 +223,7 @@ export class HandshakeState { } // If user is writing the "e" token } else if (writing) { - console.debug("noise pre-message write e"); + log("noise pre-message write e"); // When writing, the user is sending a public key, // We check that the public part corresponds to the set local key and we call MixHash(e.public_key). @@ -252,7 +255,7 @@ export class HandshakeState { // If user is reading the "s" token if (reading) { - console.debug("noise pre-message read s"); + log("noise pre-message read s"); // We check if current key is encrypted or not. We assume pre-message public keys are all unencrypted on users' end if (currPK.flag == 0) { @@ -265,7 +268,7 @@ export class HandshakeState { // If user is writing the "s" token } else if (writing) { - console.debug("noise pre-message write s"); + log("noise pre-message write s"); // If writing, it means that the user is sending a public key, // We check that the public part corresponds to the set local key and we call MixHash(s.public_key). @@ -345,7 +348,7 @@ export class HandshakeState { case NoiseTokens.e: // If user is reading the "s" token if (reading) { - console.debug("noise read e"); + log("noise read e"); // We expect an ephemeral key, so we attempt to read it (next PK to process will always be at index 0 of preMessagePKs) if (inHandshakeMessage.length > 0) { @@ -384,7 +387,7 @@ export class HandshakeState { // If user is writing the "e" token } else if (writing) { - console.debug("noise write e"); + log("noise write e"); // We generate a new ephemeral keypair this.e = generateX25519KeyPair(); @@ -408,7 +411,7 @@ export class HandshakeState { case NoiseTokens.s: // If user is reading the "s" token if (reading) { - console.debug("noise read s"); + log("noise read s"); // We expect a static key, so we attempt to read it (next PK to process will always be at index 0 of preMessagePKs) if (inHandshakeMessage.length > 0) { @@ -436,7 +439,7 @@ export class HandshakeState { // If user is writing the "s" token } else if (writing) { - console.debug("noise write s"); + log("noise write s"); // If the local static key is not set (the handshake state was not properly initialized), we raise an error if (!this.s) { @@ -462,7 +465,7 @@ export class HandshakeState { case NoiseTokens.psk: // If user is reading the "psk" token - console.debug("noise psk"); + log("noise psk"); // Calls MixKeyAndHash(psk) this.ss.mixKeyAndHash(this.psk); @@ -471,7 +474,7 @@ export class HandshakeState { case NoiseTokens.ee: // If user is reading the "ee" token - console.debug("noise dh ee"); + log("noise dh ee"); // If local and/or remote ephemeral keys are not set, we raise an error if (!this.e || !this.re) { @@ -485,7 +488,7 @@ export class HandshakeState { case NoiseTokens.es: // If user is reading the "es" token - console.debug("noise dh es"); + log("noise dh es"); // We check if keys are correctly set. // If both present, we call MixKey(DH(e, rs)) if initiator, MixKey(DH(s, re)) if responder. @@ -507,7 +510,7 @@ export class HandshakeState { case NoiseTokens.se: // If user is reading the "se" token - console.debug("noise dh se"); + log("noise dh se"); // We check if keys are correctly set. // If both present, call MixKey(DH(s, re)) if initiator, MixKey(DH(e, rs)) if responder. @@ -529,7 +532,7 @@ export class HandshakeState { case NoiseTokens.ss: // If user is reading the "ss" token - console.debug("noise dh ss"); + log("noise dh ss"); // If local and/or remote static keys are not set, we raise an error if (!this.s || !this.rs) { diff --git a/src/noise.ts b/src/noise.ts index b5c7e35..a9aa2f7 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -1,3 +1,4 @@ +import debug from "debug"; import { fromString as uint8ArrayFromString } from "uint8arrays"; import { concat as uint8ArrayConcat } from "uint8arrays/concat"; import { equals as uint8ArrayEquals } from "uint8arrays/equals"; @@ -7,6 +8,8 @@ import { chaCha20Poly1305Decrypt, chaCha20Poly1305Encrypt, getHKDF, hashSHA256 } import { Nonce } from "./nonce.js"; import { HandshakePattern } from "./patterns.js"; +const log = debug("waku:noise:handshake-state"); + // Waku Noise Protocols for Waku Payload Encryption // Noise module implementing the Noise State Objects and ChaChaPoly encryption/decryption primitives // See spec for more details: @@ -83,11 +86,11 @@ export class CipherState { this.n.increment(); this.n.assertValue(); - console.debug("encryptWithAd", ciphertext, this.n.getUint64() - 1); + log("encryptWithAd", ciphertext, this.n.getUint64() - 1); } else { // Otherwise we return the input plaintext according to specification http://www.noiseprotocol.org/noise.html#the-cipherstate-object ciphertext = plaintext; - console.debug("encryptWithAd called with no encryption key set. Returning plaintext."); + log("encryptWithAd called with no encryption key set. Returning plaintext."); } return ciphertext; @@ -111,7 +114,7 @@ export class CipherState { } else { // Otherwise we return the input ciphertext according to specification // http://www.noiseprotocol.org/noise.html#the-cipherstate-object - console.debug("decryptWithAd called with no encryption key set. Returning ciphertext."); + log("decryptWithAd called with no encryption key set. Returning ciphertext."); return ciphertext; } } @@ -191,7 +194,7 @@ export class SymmetricState { // We update ck and the Cipher state's key k using the output of HDKF this.cs = new CipherState(tempK); this.ck = ck; - console.debug("mixKey", this.ck, this.cs.k); + log("mixKey", this.ck, this.cs.k); } // MixHash as per Noise specification http://www.noiseprotocol.org/noise.html#the-symmetricstate-object @@ -199,7 +202,7 @@ export class SymmetricState { mixHash(data: Uint8Array): void { // We hash the previous handshake hash and input data and store the result in the Symmetric State's handshake hash value this.h = hashSHA256(uint8ArrayConcat([this.h, data])); - console.debug("mixHash", this.h); + log("mixHash", this.h); } // mixKeyAndHash as per Noise specification http://www.noiseprotocol.org/noise.html#the-symmetricstate-object diff --git a/src/pairing.ts b/src/pairing.ts index cd1c529..c1da631 100644 --- a/src/pairing.ts +++ b/src/pairing.ts @@ -1,5 +1,6 @@ import { HMACDRBG } from "@stablelib/hmac-drbg"; import { randomBytes } from "@stablelib/random"; +import debug from "debug"; import { EventEmitter } from "eventemitter3"; import { Decoder, Encoder, Message } from "js-waku/lib/interfaces"; import { pEvent } from "p-event"; @@ -20,6 +21,8 @@ import { MessageNametagLength } from "./payload.js"; import { NoisePublicKey } from "./publickey.js"; import { QR } from "./qr.js"; +const log = debug("waku:noise:pairing"); + export interface Sender { publish(encoder: Encoder, msg: Message): Promise; } @@ -171,7 +174,7 @@ export class WakuPairing { return step; } catch (err) { if (err instanceof MessageNametagError) { - console.debug("Unexpected message nametag", err.expectedNametag, err.actualNametag); + log("Unexpected message nametag", err.expectedNametag, err.actualNametag); } else { throw err; }