This commit is contained in:
morrigan 2019-11-04 22:37:43 +01:00
parent fdfadfa794
commit 75dbfeff8c

View File

@ -30,7 +30,7 @@ type HandshakeState = {
s: KeyPair, s: KeyPair,
e?: KeyPair, e?: KeyPair,
rs: bytes32, rs: bytes32,
re?: bytes32, re: bytes32,
psk: bytes32, psk: bytes32,
} }
@ -54,16 +54,18 @@ export class XXHandshake {
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);
const re = Buffer.alloc(32);
return { ss, s, rs, psk }; return { ss, s, rs, psk, re };
} }
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 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);
const re = Buffer.alloc(32);
return { ss, s, rs, psk }; return { ss, s, rs, psk, re };
} }
private incrementNonce(n: uint32) : uint32 { private incrementNonce(n: uint32) : uint32 {
@ -154,11 +156,9 @@ export class XXHandshake {
private async hashProtocolName(protocolName: bytes) : Promise<bytes32> { private async hashProtocolName(protocolName: bytes) : Promise<bytes32> {
if (protocolName.length <= 32) { if (protocolName.length <= 32) {
return new Promise(resolve => {
const h = Buffer.alloc(32); const h = Buffer.alloc(32);
protocolName.copy(h); protocolName.copy(h);
resolve(h) return Promise.resolve(h)
});
} else { } else {
return await this.getHash(protocolName, Buffer.from([])); return await this.getHash(protocolName, Buffer.from([]));
} }
@ -280,8 +280,16 @@ export class XXHandshake {
session.cs2 = cs2; session.cs2 = cs2;
} else if (session.mc > 2) { } else if (session.mc > 2) {
if (session.i) { if (session.i) {
if (!session.cs1) {
throw new Error("CS1 (cipher state) is not defined")
}
messageBuffer = await this.writeMessageRegular(session.cs1, message); messageBuffer = await this.writeMessageRegular(session.cs1, message);
} else { } else {
if (!session.cs2) {
throw new Error("CS2 (cipher state) is not defined")
}
messageBuffer = await this.writeMessageRegular(session.cs2, message); messageBuffer = await this.writeMessageRegular(session.cs2, message);
} }
} else { } else {