refactor: removed taglen as parameter as it is defined in spec that it is always`16`

This commit is contained in:
Richard Ramos 2023-11-20 16:56:33 -04:00 committed by richΛrd
parent 3cd683c2f1
commit 1cacb2f548
3 changed files with 6 additions and 16 deletions

View File

@ -9,7 +9,7 @@ import { Handshake, HandshakeStepResult } from "./handshake";
import { MessageNametagBuffer, MessageNametagLength } from "./messagenametag";
import { CipherState, createEmptyKey, SymmetricState } from "./noise";
import { MAX_NONCE, Nonce } from "./nonce";
import { NoiseHandshakePatterns, PayloadV2ProtocolIDs } from "./patterns";
import { NoiseHandshakePatterns } from "./patterns";
import { PayloadV2 } from "./payload";
import { ChaChaPolyCipherState, NoisePublicKey } from "./publickey";
@ -40,11 +40,9 @@ function randomNoisePublicKey(): NoisePublicKey {
function randomPayloadV2(rng: HMACDRBG): PayloadV2 {
const messageNametag = randomBytes(MessageNametagLength, rng);
const protocolId = 14;
const protocolName = Object.keys(PayloadV2ProtocolIDs).find((key) => PayloadV2ProtocolIDs[key] === protocolId);
const handshakePattern = NoiseHandshakePatterns[protocolName!];
const handshakeMessage = [randomNoisePublicKey(), randomNoisePublicKey(), randomNoisePublicKey()];
const transportMessage = randomBytes(128);
return new PayloadV2(messageNametag, protocolId, handshakePattern.tagLen, handshakeMessage, transportMessage);
return new PayloadV2(messageNametag, protocolId, handshakeMessage, transportMessage);
}
describe("js-noise", () => {

View File

@ -1,4 +1,3 @@
import { TAG_LENGTH as ChaChaPolyTagLen } from "@stablelib/chacha20poly1305";
import { Hash } from "@stablelib/hash";
import { SHA256 } from "@stablelib/sha256";
@ -81,7 +80,6 @@ export class HandshakePattern {
public readonly name: string,
dhKeyType: new () => DHKey,
public readonly hash: new () => Hash,
public readonly tagLen: number,
public readonly preMessagePatterns: Array<PreMessagePattern>,
public readonly messagePatterns: Array<MessagePattern>
) {
@ -116,7 +114,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
"Noise_K1K1_25519_ChaChaPoly_SHA256",
DH25519,
SHA256,
ChaChaPolyTagLen,
[
new PreMessagePattern(MessageDirection.r, [NoiseTokens.s]),
new PreMessagePattern(MessageDirection.l, [NoiseTokens.s]),
@ -131,7 +128,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
"Noise_XK1_25519_ChaChaPoly_SHA256",
DH25519,
SHA256,
ChaChaPolyTagLen,
[new PreMessagePattern(MessageDirection.l, [NoiseTokens.s])],
[
new MessagePattern(MessageDirection.r, [NoiseTokens.e]),
@ -143,7 +139,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
"Noise_XX_25519_ChaChaPoly_SHA256",
DH25519,
SHA256,
ChaChaPolyTagLen,
[],
[
new MessagePattern(MessageDirection.r, [NoiseTokens.e]),
@ -155,7 +150,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
"Noise_XXpsk0_25519_ChaChaPoly_SHA256",
DH25519,
SHA256,
ChaChaPolyTagLen,
[],
[
new MessagePattern(MessageDirection.r, [NoiseTokens.psk, NoiseTokens.e]),
@ -167,7 +161,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
"Noise_WakuPairing_25519_ChaChaPoly_SHA256",
DH25519,
SHA256,
ChaChaPolyTagLen,
[new PreMessagePattern(MessageDirection.l, [NoiseTokens.e])],
[
new MessagePattern(MessageDirection.r, [NoiseTokens.e, NoiseTokens.ee]),

View File

@ -7,6 +7,8 @@ import { NoiseHandshakePatterns, PayloadV2ProtocolIDs } from "./patterns.js";
import { NoisePublicKey } from "./publickey.js";
import { readUIntLE, writeUIntLE } from "./utils.js";
const authDataLen = 16;
/**
* PayloadV2 defines an object for Waku payloads with version 2 as in
* https://rfc.vac.dev/spec/35/#public-keys-serialization
@ -17,7 +19,6 @@ export class PayloadV2 {
constructor(
public messageNametag: MessageNametag = new Uint8Array(MessageNametagLength),
public protocolId = 0,
public tagLen = 0,
public handshakeMessage: Array<NoisePublicKey> = [],
public transportMessage: Uint8Array = new Uint8Array()
) {}
@ -31,7 +32,6 @@ export class PayloadV2 {
r.protocolId = this.protocolId;
r.transportMessage = new Uint8Array(this.transportMessage);
r.messageNametag = new Uint8Array(this.messageNametag);
r.tagLen = this.tagLen;
for (let i = 0; i < this.handshakeMessage.length; i++) {
r.handshakeMessage.push(this.handshakeMessage[i].clone());
}
@ -138,7 +138,6 @@ export class PayloadV2 {
}
const pattern = NoiseHandshakePatterns[protocolName];
const tagLen = pattern ? pattern.tagLen : 0;
const keySize = pattern ? pattern.dhKey.DHLen() : 0;
i++;
@ -169,7 +168,7 @@ export class PayloadV2 {
written += pkLen;
// If the key is encrypted, we only read the encrypted X coordinate and the authorization tag, and we deserialize into a Noise Public Key
} else if (flag === 1) {
const pkLen = 1 + keySize + tagLen;
const pkLen = 1 + keySize + authDataLen;
handshakeMessage.push(NoisePublicKey.deserialize(payload.subarray(i, i + pkLen)));
i += pkLen;
written += pkLen;
@ -186,6 +185,6 @@ export class PayloadV2 {
const transportMessage = payload.subarray(i, i + transportMessageLen);
i += transportMessageLen;
return new PayloadV2(messageNametag, protocolId, tagLen, handshakeMessage, transportMessage);
return new PayloadV2(messageNametag, protocolId, handshakeMessage, transportMessage);
}
}