mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-28 23:52:25 +00:00
Address some PR comments
This commit is contained in:
parent
37215d7a3c
commit
e4b5c068db
29
src/xx.ts
29
src/xx.ts
@ -3,7 +3,7 @@ import { Buffer } from 'buffer';
|
|||||||
import * as crypto from 'libp2p-crypto';
|
import * as crypto from 'libp2p-crypto';
|
||||||
import AEAD from 'bcrypto/aead-browser';
|
import AEAD from 'bcrypto/aead-browser';
|
||||||
|
|
||||||
type KeyPair = {
|
interface KeyPair {
|
||||||
publicKey: bytes32,
|
publicKey: bytes32,
|
||||||
privateKey: bytes32,
|
privateKey: bytes32,
|
||||||
}
|
}
|
||||||
@ -37,13 +37,16 @@ type NoiseSession = {
|
|||||||
i: boolean,
|
i: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
const emptyKey = Buffer.alloc(32) as bytes32;
|
|
||||||
const minNonce = 0;
|
const minNonce = 0;
|
||||||
|
|
||||||
class XXHandshake {
|
class XXHandshake {
|
||||||
|
private createEmptyKey() : bytes32 {
|
||||||
|
return Buffer.alloc(32);
|
||||||
|
}
|
||||||
|
|
||||||
private async initializeInitiator(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32) : Promise<HandshakeState> {
|
private async initializeInitiator(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32) : Promise<HandshakeState> {
|
||||||
const e: KeyPair;
|
let e: KeyPair;
|
||||||
const re: bytes32;
|
let re: bytes32;
|
||||||
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
||||||
const ss = await this.initializeSymmetric(name);
|
const ss = await this.initializeSymmetric(name);
|
||||||
await this.mixHash(ss, prologue);
|
await this.mixHash(ss, prologue);
|
||||||
@ -52,8 +55,8 @@ class XXHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async initializeResponder(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32) : Promise<HandshakeState> {
|
private async initializeResponder(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32) : Promise<HandshakeState> {
|
||||||
const e: KeyPair;
|
let e: KeyPair;
|
||||||
const re: bytes32;
|
let re: bytes32;
|
||||||
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
||||||
const ss = await this.initializeSymmetric(name);
|
const ss = await this.initializeSymmetric(name);
|
||||||
await this.mixHash(ss, prologue);
|
await this.mixHash(ss, prologue);
|
||||||
@ -95,22 +98,24 @@ class XXHandshake {
|
|||||||
// Symmetric state related
|
// Symmetric state related
|
||||||
|
|
||||||
private async initializeSymmetric(protocolName: string) : Promise<SymmetricState> {
|
private async initializeSymmetric(protocolName: string) : Promise<SymmetricState> {
|
||||||
const h = await this.hashProtocolName(protocolName);
|
const protocolNameBytes: bytes = Buffer.from(protocolName, 'utf-8');
|
||||||
|
const h = await this.hashProtocolName(protocolNameBytes);
|
||||||
const ck = h;
|
const ck = h;
|
||||||
const cs = this.initializeKey(emptyKey);
|
const key = this.createEmptyKey();
|
||||||
|
const cs = this.initializeKey(key);
|
||||||
|
|
||||||
return { cs, ck, h };
|
return { cs, ck, h };
|
||||||
}
|
}
|
||||||
|
|
||||||
private async hashProtocolName(protocolName: string) : Promise<bytes32> {
|
private async hashProtocolName(protocolName: bytes) : Promise<bytes32> {
|
||||||
if (protocolName.length <= 32) {
|
if (protocolName.length <= 32) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const h = Buffer.alloc(32);
|
const h = Buffer.alloc(32);
|
||||||
h.write(protocolName);
|
protocolName.copy(h);
|
||||||
resolve(h)
|
resolve(h)
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return await this.getHash(Buffer.from(protocolName, 'utf-8'), Buffer.from([]));
|
return await this.getHash(protocolName, Buffer.from([]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +129,7 @@ class XXHandshake {
|
|||||||
|
|
||||||
public async initSession(initiator: boolean, prologue: bytes32[], s: KeyPair, rs: bytes32) : Promise<NoiseSession> {
|
public async initSession(initiator: boolean, prologue: bytes32[], s: KeyPair, rs: bytes32) : Promise<NoiseSession> {
|
||||||
let session: NoiseSession;
|
let session: NoiseSession;
|
||||||
const psk = emptyKey;
|
const psk = this.createEmptyKey();
|
||||||
|
|
||||||
if (initiator) {
|
if (initiator) {
|
||||||
session.hs = await this.initializeInitiator(prologue, s, rs, psk);
|
session.hs = await this.initializeInitiator(prologue, s, rs, psk);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user