mirror of
https://github.com/logos-messaging/js-noise.git
synced 2026-01-04 06:33:08 +00:00
refactor: removed taglen as parameter as it is defined in spec that it is always16
This commit is contained in:
parent
3cd683c2f1
commit
1cacb2f548
@ -9,7 +9,7 @@ import { Handshake, HandshakeStepResult } from "./handshake";
|
|||||||
import { MessageNametagBuffer, MessageNametagLength } from "./messagenametag";
|
import { MessageNametagBuffer, MessageNametagLength } from "./messagenametag";
|
||||||
import { CipherState, createEmptyKey, SymmetricState } from "./noise";
|
import { CipherState, createEmptyKey, SymmetricState } from "./noise";
|
||||||
import { MAX_NONCE, Nonce } from "./nonce";
|
import { MAX_NONCE, Nonce } from "./nonce";
|
||||||
import { NoiseHandshakePatterns, PayloadV2ProtocolIDs } from "./patterns";
|
import { NoiseHandshakePatterns } from "./patterns";
|
||||||
import { PayloadV2 } from "./payload";
|
import { PayloadV2 } from "./payload";
|
||||||
import { ChaChaPolyCipherState, NoisePublicKey } from "./publickey";
|
import { ChaChaPolyCipherState, NoisePublicKey } from "./publickey";
|
||||||
|
|
||||||
@ -40,11 +40,9 @@ function randomNoisePublicKey(): NoisePublicKey {
|
|||||||
function randomPayloadV2(rng: HMACDRBG): PayloadV2 {
|
function randomPayloadV2(rng: HMACDRBG): PayloadV2 {
|
||||||
const messageNametag = randomBytes(MessageNametagLength, rng);
|
const messageNametag = randomBytes(MessageNametagLength, rng);
|
||||||
const protocolId = 14;
|
const protocolId = 14;
|
||||||
const protocolName = Object.keys(PayloadV2ProtocolIDs).find((key) => PayloadV2ProtocolIDs[key] === protocolId);
|
|
||||||
const handshakePattern = NoiseHandshakePatterns[protocolName!];
|
|
||||||
const handshakeMessage = [randomNoisePublicKey(), randomNoisePublicKey(), randomNoisePublicKey()];
|
const handshakeMessage = [randomNoisePublicKey(), randomNoisePublicKey(), randomNoisePublicKey()];
|
||||||
const transportMessage = randomBytes(128);
|
const transportMessage = randomBytes(128);
|
||||||
return new PayloadV2(messageNametag, protocolId, handshakePattern.tagLen, handshakeMessage, transportMessage);
|
return new PayloadV2(messageNametag, protocolId, handshakeMessage, transportMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("js-noise", () => {
|
describe("js-noise", () => {
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import { TAG_LENGTH as ChaChaPolyTagLen } from "@stablelib/chacha20poly1305";
|
|
||||||
import { Hash } from "@stablelib/hash";
|
import { Hash } from "@stablelib/hash";
|
||||||
import { SHA256 } from "@stablelib/sha256";
|
import { SHA256 } from "@stablelib/sha256";
|
||||||
|
|
||||||
@ -81,7 +80,6 @@ export class HandshakePattern {
|
|||||||
public readonly name: string,
|
public readonly name: string,
|
||||||
dhKeyType: new () => DHKey,
|
dhKeyType: new () => DHKey,
|
||||||
public readonly hash: new () => Hash,
|
public readonly hash: new () => Hash,
|
||||||
public readonly tagLen: number,
|
|
||||||
public readonly preMessagePatterns: Array<PreMessagePattern>,
|
public readonly preMessagePatterns: Array<PreMessagePattern>,
|
||||||
public readonly messagePatterns: Array<MessagePattern>
|
public readonly messagePatterns: Array<MessagePattern>
|
||||||
) {
|
) {
|
||||||
@ -116,7 +114,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
|
|||||||
"Noise_K1K1_25519_ChaChaPoly_SHA256",
|
"Noise_K1K1_25519_ChaChaPoly_SHA256",
|
||||||
DH25519,
|
DH25519,
|
||||||
SHA256,
|
SHA256,
|
||||||
ChaChaPolyTagLen,
|
|
||||||
[
|
[
|
||||||
new PreMessagePattern(MessageDirection.r, [NoiseTokens.s]),
|
new PreMessagePattern(MessageDirection.r, [NoiseTokens.s]),
|
||||||
new PreMessagePattern(MessageDirection.l, [NoiseTokens.s]),
|
new PreMessagePattern(MessageDirection.l, [NoiseTokens.s]),
|
||||||
@ -131,7 +128,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
|
|||||||
"Noise_XK1_25519_ChaChaPoly_SHA256",
|
"Noise_XK1_25519_ChaChaPoly_SHA256",
|
||||||
DH25519,
|
DH25519,
|
||||||
SHA256,
|
SHA256,
|
||||||
ChaChaPolyTagLen,
|
|
||||||
[new PreMessagePattern(MessageDirection.l, [NoiseTokens.s])],
|
[new PreMessagePattern(MessageDirection.l, [NoiseTokens.s])],
|
||||||
[
|
[
|
||||||
new MessagePattern(MessageDirection.r, [NoiseTokens.e]),
|
new MessagePattern(MessageDirection.r, [NoiseTokens.e]),
|
||||||
@ -143,7 +139,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
|
|||||||
"Noise_XX_25519_ChaChaPoly_SHA256",
|
"Noise_XX_25519_ChaChaPoly_SHA256",
|
||||||
DH25519,
|
DH25519,
|
||||||
SHA256,
|
SHA256,
|
||||||
ChaChaPolyTagLen,
|
|
||||||
[],
|
[],
|
||||||
[
|
[
|
||||||
new MessagePattern(MessageDirection.r, [NoiseTokens.e]),
|
new MessagePattern(MessageDirection.r, [NoiseTokens.e]),
|
||||||
@ -155,7 +150,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
|
|||||||
"Noise_XXpsk0_25519_ChaChaPoly_SHA256",
|
"Noise_XXpsk0_25519_ChaChaPoly_SHA256",
|
||||||
DH25519,
|
DH25519,
|
||||||
SHA256,
|
SHA256,
|
||||||
ChaChaPolyTagLen,
|
|
||||||
[],
|
[],
|
||||||
[
|
[
|
||||||
new MessagePattern(MessageDirection.r, [NoiseTokens.psk, NoiseTokens.e]),
|
new MessagePattern(MessageDirection.r, [NoiseTokens.psk, NoiseTokens.e]),
|
||||||
@ -167,7 +161,6 @@ export const NoiseHandshakePatterns: Record<string, HandshakePattern> = {
|
|||||||
"Noise_WakuPairing_25519_ChaChaPoly_SHA256",
|
"Noise_WakuPairing_25519_ChaChaPoly_SHA256",
|
||||||
DH25519,
|
DH25519,
|
||||||
SHA256,
|
SHA256,
|
||||||
ChaChaPolyTagLen,
|
|
||||||
[new PreMessagePattern(MessageDirection.l, [NoiseTokens.e])],
|
[new PreMessagePattern(MessageDirection.l, [NoiseTokens.e])],
|
||||||
[
|
[
|
||||||
new MessagePattern(MessageDirection.r, [NoiseTokens.e, NoiseTokens.ee]),
|
new MessagePattern(MessageDirection.r, [NoiseTokens.e, NoiseTokens.ee]),
|
||||||
|
|||||||
@ -7,6 +7,8 @@ import { NoiseHandshakePatterns, PayloadV2ProtocolIDs } from "./patterns.js";
|
|||||||
import { NoisePublicKey } from "./publickey.js";
|
import { NoisePublicKey } from "./publickey.js";
|
||||||
import { readUIntLE, writeUIntLE } from "./utils.js";
|
import { readUIntLE, writeUIntLE } from "./utils.js";
|
||||||
|
|
||||||
|
const authDataLen = 16;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PayloadV2 defines an object for Waku payloads with version 2 as in
|
* PayloadV2 defines an object for Waku payloads with version 2 as in
|
||||||
* https://rfc.vac.dev/spec/35/#public-keys-serialization
|
* https://rfc.vac.dev/spec/35/#public-keys-serialization
|
||||||
@ -17,7 +19,6 @@ export class PayloadV2 {
|
|||||||
constructor(
|
constructor(
|
||||||
public messageNametag: MessageNametag = new Uint8Array(MessageNametagLength),
|
public messageNametag: MessageNametag = new Uint8Array(MessageNametagLength),
|
||||||
public protocolId = 0,
|
public protocolId = 0,
|
||||||
public tagLen = 0,
|
|
||||||
public handshakeMessage: Array<NoisePublicKey> = [],
|
public handshakeMessage: Array<NoisePublicKey> = [],
|
||||||
public transportMessage: Uint8Array = new Uint8Array()
|
public transportMessage: Uint8Array = new Uint8Array()
|
||||||
) {}
|
) {}
|
||||||
@ -31,7 +32,6 @@ export class PayloadV2 {
|
|||||||
r.protocolId = this.protocolId;
|
r.protocolId = this.protocolId;
|
||||||
r.transportMessage = new Uint8Array(this.transportMessage);
|
r.transportMessage = new Uint8Array(this.transportMessage);
|
||||||
r.messageNametag = new Uint8Array(this.messageNametag);
|
r.messageNametag = new Uint8Array(this.messageNametag);
|
||||||
r.tagLen = this.tagLen;
|
|
||||||
for (let i = 0; i < this.handshakeMessage.length; i++) {
|
for (let i = 0; i < this.handshakeMessage.length; i++) {
|
||||||
r.handshakeMessage.push(this.handshakeMessage[i].clone());
|
r.handshakeMessage.push(this.handshakeMessage[i].clone());
|
||||||
}
|
}
|
||||||
@ -138,7 +138,6 @@ export class PayloadV2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pattern = NoiseHandshakePatterns[protocolName];
|
const pattern = NoiseHandshakePatterns[protocolName];
|
||||||
const tagLen = pattern ? pattern.tagLen : 0;
|
|
||||||
const keySize = pattern ? pattern.dhKey.DHLen() : 0;
|
const keySize = pattern ? pattern.dhKey.DHLen() : 0;
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
@ -169,7 +168,7 @@ export class PayloadV2 {
|
|||||||
written += pkLen;
|
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
|
// 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) {
|
} else if (flag === 1) {
|
||||||
const pkLen = 1 + keySize + tagLen;
|
const pkLen = 1 + keySize + authDataLen;
|
||||||
handshakeMessage.push(NoisePublicKey.deserialize(payload.subarray(i, i + pkLen)));
|
handshakeMessage.push(NoisePublicKey.deserialize(payload.subarray(i, i + pkLen)));
|
||||||
i += pkLen;
|
i += pkLen;
|
||||||
written += pkLen;
|
written += pkLen;
|
||||||
@ -186,6 +185,6 @@ export class PayloadV2 {
|
|||||||
const transportMessage = payload.subarray(i, i + transportMessageLen);
|
const transportMessage = payload.subarray(i, i + transportMessageLen);
|
||||||
i += transportMessageLen;
|
i += transportMessageLen;
|
||||||
|
|
||||||
return new PayloadV2(messageNametag, protocolId, tagLen, handshakeMessage, transportMessage);
|
return new PayloadV2(messageNametag, protocolId, handshakeMessage, transportMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user