From c3ab986d3da086098da061b5ad359d85936d2670 Mon Sep 17 00:00:00 2001 From: Belma Gutlic Date: Tue, 7 Jan 2020 13:34:45 +0100 Subject: [PATCH] Address PR comments --- .eslintrc | 3 ++- src/@types/handshake-interface.ts | 2 +- src/crypto.ts | 6 +++--- src/handshake-ik.ts | 14 +++++++------- src/handshake-xx-fallback.ts | 12 ++++++------ src/handshake-xx.ts | 16 ++++++++-------- src/noise.ts | 24 ++++++++++++------------ test/noise.test.ts | 4 ++-- test/xx-fallback-handshake.test.ts | 6 +++--- test/xx-handshake.test.ts | 14 +++++++------- 10 files changed, 51 insertions(+), 50 deletions(-) diff --git a/.eslintrc b/.eslintrc index a621e68..46054ea 100644 --- a/.eslintrc +++ b/.eslintrc @@ -15,6 +15,7 @@ "@typescript-eslint/indent": ["error", 2], "@typescript-eslint/no-use-before-define": "off", "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/interface-name-prefix": "off", "no-console": "warn" } -} \ No newline at end of file +} diff --git a/src/@types/handshake-interface.ts b/src/@types/handshake-interface.ts index 5c152e5..d92b9ff 100644 --- a/src/@types/handshake-interface.ts +++ b/src/@types/handshake-interface.ts @@ -1,7 +1,7 @@ import {bytes} from "./basic"; import {NoiseSession} from "./handshake"; -export interface HandshakeInterface { +export interface IHandshake { session: NoiseSession; encrypt(plaintext: bytes, session: NoiseSession): bytes; decrypt(ciphertext: bytes, session: NoiseSession): bytes; diff --git a/src/crypto.ts b/src/crypto.ts index eff1494..1c63b6d 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -1,5 +1,5 @@ import { Buffer } from "buffer"; -import {HandshakeInterface} from "./@types/handshake-interface"; +import {IHandshake} from "./@types/handshake-interface"; interface ReturnEncryptionWrapper { (source: Iterable): AsyncIterableIterator; @@ -8,7 +8,7 @@ interface ReturnEncryptionWrapper { const maxPlaintextLength = 65519; // Returns generator that encrypts payload from the user -export function encryptStream(handshake: HandshakeInterface): ReturnEncryptionWrapper { +export function encryptStream(handshake: IHandshake): ReturnEncryptionWrapper { return async function * (source) { for await (const chunk of source) { const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length); @@ -28,7 +28,7 @@ export function encryptStream(handshake: HandshakeInterface): ReturnEncryptionWr // Decrypt received payload to the user -export function decryptStream(handshake: HandshakeInterface): ReturnEncryptionWrapper { +export function decryptStream(handshake: IHandshake): ReturnEncryptionWrapper { return async function * (source) { for await (const chunk of source) { const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length); diff --git a/src/handshake-ik.ts b/src/handshake-ik.ts index 5f173f2..30afa1a 100644 --- a/src/handshake-ik.ts +++ b/src/handshake-ik.ts @@ -3,17 +3,17 @@ import {IK} from "./handshakes/ik"; import {NoiseSession} from "./@types/handshake"; import {bytes, bytes32} from "./@types/basic"; import {KeyPair, PeerId} from "./@types/libp2p"; -import {HandshakeInterface} from "./@types/handshake-interface"; +import {IHandshake} from "./@types/handshake-interface"; import {Buffer} from "buffer"; -export class Handshake implements HandshakeInterface { +export class IKHandshake implements IHandshake { public isInitiator: boolean; public session: NoiseSession; private libp2pPrivateKey: bytes; private libp2pPublicKey: bytes; private prologue: bytes32; - private staticKeys: KeyPair; + private staticKeypair: KeyPair; private connection: WrappedConnection; private remotePeer: PeerId; private ik: IK; @@ -23,7 +23,7 @@ export class Handshake implements HandshakeInterface { libp2pPrivateKey: bytes, libp2pPublicKey: bytes, prologue: bytes32, - staticKeys: KeyPair, + staticKeypair: KeyPair, connection: WrappedConnection, remotePeer: PeerId, handshake?: IK, @@ -32,7 +32,7 @@ export class Handshake implements HandshakeInterface { this.libp2pPrivateKey = libp2pPrivateKey; this.libp2pPublicKey = libp2pPublicKey; this.prologue = prologue; - this.staticKeys = staticKeys; + this.staticKeypair = staticKeypair; this.connection = connection; this.remotePeer = remotePeer; @@ -40,8 +40,8 @@ export class Handshake implements HandshakeInterface { // Dummy data // TODO: Load remote static keys if found - const remoteStaticKeys = this.staticKeys; - this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeys, remoteStaticKeys.publicKey); + const remoteStaticKeys = this.staticKeypair; + this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeypair, remoteStaticKeys.publicKey); } public decrypt(ciphertext: Buffer, session: NoiseSession): Buffer { diff --git a/src/handshake-xx-fallback.ts b/src/handshake-xx-fallback.ts index a7c0788..8f9a7d6 100644 --- a/src/handshake-xx-fallback.ts +++ b/src/handshake-xx-fallback.ts @@ -1,6 +1,6 @@ import { Buffer } from "buffer"; -import { Handshake as XXHandshake } from "./handshake-xx"; +import { XXHandshake } from "./handshake-xx"; import { XX } from "./handshakes/xx"; import { KeyPair, PeerId } from "./@types/libp2p"; import { bytes, bytes32 } from "./@types/basic"; @@ -15,7 +15,7 @@ import { logger } from "./logger"; import { WrappedConnection } from "./noise"; import {decode0, decode1, encode1} from "./encoder"; -export class Handshake extends XXHandshake { +export class XXFallbackHandshake extends XXHandshake { private ephemeralKeys?: KeyPair; private initialMsg: bytes; @@ -24,14 +24,14 @@ export class Handshake extends XXHandshake { libp2pPrivateKey: bytes, libp2pPublicKey: bytes, prologue: bytes32, - staticKeys: KeyPair, + staticKeypair: KeyPair, connection: WrappedConnection, remotePeer: PeerId, initialMsg: bytes, ephemeralKeys?: KeyPair, handshake?: XX, ) { - super(isInitiator, libp2pPrivateKey, libp2pPublicKey, prologue, staticKeys, connection, remotePeer, handshake); + super(isInitiator, libp2pPrivateKey, libp2pPublicKey, prologue, staticKeypair, connection, remotePeer, handshake); if (ephemeralKeys) { this.ephemeralKeys = ephemeralKeys; } @@ -59,7 +59,7 @@ export class Handshake extends XXHandshake { public async exchange(): Promise { if (this.isInitiator) { logger('XX Fallback Stage 1 - Initiator waiting to receive first message from responder...'); - const receivedMessageBuffer = decode1((await this.connection.readLP()).slice()); + const receivedMessageBuffer = decode1((await this.connection.readLP())); // const receivedMessageBuffer = decode1(this.initialMsg); logger("Initiator receivedMessageBuffer in stage 1", receivedMessageBuffer); const plaintext = this.xx.recvMessage(this.session, receivedMessageBuffer); @@ -74,7 +74,7 @@ export class Handshake extends XXHandshake { logger("All good with the signature!"); } else { logger('XX Fallback Stage 1 - Responder sending out first message with signed payload and static key.'); - const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey)); + const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeypair.publicKey)); const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, Buffer.alloc(0)); const handshakePayload = await createHandshakePayload( this.libp2pPublicKey, diff --git a/src/handshake-xx.ts b/src/handshake-xx.ts index f5fd353..9dd8788 100644 --- a/src/handshake-xx.ts +++ b/src/handshake-xx.ts @@ -4,7 +4,7 @@ import { XX } from "./handshakes/xx"; import { KeyPair, PeerId } from "./@types/libp2p"; import { bytes, bytes32 } from "./@types/basic"; import { NoiseSession } from "./@types/handshake"; -import {HandshakeInterface} from "./@types/handshake-interface"; +import {IHandshake} from "./@types/handshake-interface"; import { createHandshakePayload, getHandshakePayload, @@ -16,7 +16,7 @@ import { logger } from "./logger"; import { decode0, decode1, encode0, encode1 } from "./encoder"; import { WrappedConnection } from "./noise"; -export class Handshake implements HandshakeInterface { +export class XXHandshake implements IHandshake { public isInitiator: boolean; public session: NoiseSession; @@ -24,7 +24,7 @@ export class Handshake implements HandshakeInterface { protected xx: XX; protected libp2pPrivateKey: bytes; protected libp2pPublicKey: bytes; - protected staticKeys: KeyPair; + protected staticKeypair: KeyPair; protected remotePeer: PeerId; private prologue: bytes32; @@ -34,7 +34,7 @@ export class Handshake implements HandshakeInterface { libp2pPrivateKey: bytes, libp2pPublicKey: bytes, prologue: bytes32, - staticKeys: KeyPair, + staticKeypair: KeyPair, connection: WrappedConnection, remotePeer: PeerId, handshake?: XX, @@ -43,12 +43,12 @@ export class Handshake implements HandshakeInterface { this.libp2pPrivateKey = libp2pPrivateKey; this.libp2pPublicKey = libp2pPublicKey; this.prologue = prologue; - this.staticKeys = staticKeys; + this.staticKeypair = staticKeypair; this.connection = connection; this.remotePeer = remotePeer; this.xx = handshake || new XX(); - this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys); + this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeypair); } // stage 0 @@ -83,7 +83,7 @@ export class Handshake implements HandshakeInterface { logger("All good with the signature!"); } else { logger('Stage 1 - Responder sending out first message with signed payload and static key.'); - const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey)); + const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeypair.publicKey)); const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, Buffer.alloc(0)); const handshakePayload = await createHandshakePayload( this.libp2pPublicKey, @@ -102,7 +102,7 @@ export class Handshake implements HandshakeInterface { public async finish(earlyData?: bytes): Promise { if (this.isInitiator) { logger('Stage 2 - Initiator sending third handshake message.'); - const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey)); + const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeypair.publicKey)); const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0)); const handshakePayload = await createHandshakePayload( this.libp2pPublicKey, diff --git a/src/noise.ts b/src/noise.ts index 249e76d..0b9a3ef 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -6,16 +6,16 @@ import ensureBuffer from 'it-buffer'; import pipe from 'it-pipe'; import lp from 'it-length-prefixed'; -import { Handshake as XX } from "./handshake-xx"; -import { Handshake as IK } from "./handshake-ik"; -import { Handshake as XXFallback } from "./handshake-xx-fallback"; +import { XXHandshake } from "./handshake-xx"; +import { IKHandshake } from "./handshake-ik"; +import { XXFallbackHandshake } from "./handshake-xx-fallback"; import { generateKeypair } from "./utils"; import { uint16BEDecode, uint16BEEncode } from "./encoder"; import { decryptStream, encryptStream } from "./crypto"; import { bytes } from "./@types/basic"; import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p"; import { Duplex } from "./@types/it-pair"; -import {HandshakeInterface} from "./@types/handshake-interface"; +import {IHandshake} from "./@types/handshake-interface"; export type WrappedConnection = ReturnType; @@ -105,7 +105,7 @@ export class Noise implements NoiseConnection { * @param libp2pPublicKey * @param remotePeer */ - private async performHandshake(params: HandshakeParams): Promise { + private async performHandshake(params: HandshakeParams): Promise { // TODO: Implement noise pipes if (false) { @@ -127,10 +127,10 @@ export class Noise implements NoiseConnection { params: HandshakeParams, ephemeralKeys: KeyPair, initialMsg: bytes, - ): Promise { + ): Promise { const { isInitiator, libp2pPublicKey, remotePeer, connection } = params; const handshake = - new XXFallback(isInitiator, this.privateKey, libp2pPublicKey, this.prologue, this.staticKeys, connection, remotePeer, initialMsg, ephemeralKeys); + new XXFallbackHandshake(isInitiator, this.privateKey, libp2pPublicKey, this.prologue, this.staticKeys, connection, remotePeer, initialMsg, ephemeralKeys); try { await handshake.propose(); @@ -145,9 +145,9 @@ export class Noise implements NoiseConnection { private async performXXHandshake( params: HandshakeParams, - ): Promise { + ): Promise { const { isInitiator, libp2pPublicKey, remotePeer, connection } = params; - const handshake = new XX(isInitiator, this.privateKey, libp2pPublicKey, this.prologue, this.staticKeys, connection, remotePeer); + const handshake = new XXHandshake(isInitiator, this.privateKey, libp2pPublicKey, this.prologue, this.staticKeys, connection, remotePeer); try { await handshake.propose(); @@ -162,9 +162,9 @@ export class Noise implements NoiseConnection { private async performIKHandshake( params: HandshakeParams, - ): Promise { + ): Promise { const { isInitiator, libp2pPublicKey, remotePeer, connection } = params; - const handshake = new IK(isInitiator, this.privateKey, libp2pPublicKey, this.prologue, this.staticKeys, connection, remotePeer); + const handshake = new IKHandshake(isInitiator, this.privateKey, libp2pPublicKey, this.prologue, this.staticKeys, connection, remotePeer); // TODO @@ -173,7 +173,7 @@ export class Noise implements NoiseConnection { private async createSecureConnection( connection: WrappedConnection, - handshake: HandshakeInterface, + handshake: IHandshake, ): Promise { // Create encryption box/unbox wrapper const [secure, user] = DuplexPair(); diff --git a/test/noise.test.ts b/test/noise.test.ts index 00297a0..34617c1 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -5,7 +5,7 @@ import { Noise } from "../src"; import {createPeerIdsFromFixtures} from "./fixtures/peer"; import Wrap from "it-pb-rpc"; import { random } from "bcrypto"; -import {Handshake} from "../src/handshake-xx"; +import {XXHandshake} from "../src/handshake-xx"; import { createHandshakePayload, generateKeypair, @@ -61,7 +61,7 @@ describe("Noise", () => { const xx = new XX(); const { privateKey: libp2pPrivKey, publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer); - const handshake = new Handshake(false, libp2pPrivKey, libp2pPubKey, prologue, staticKeys, wrapped, localPeer, xx); + const handshake = new XXHandshake(false, libp2pPrivKey, libp2pPubKey, prologue, staticKeys, wrapped, localPeer, xx); let receivedMessageBuffer = decode0((await wrapped.readLP()).slice()); // The first handshake message contains the initiator's ephemeral public key diff --git a/test/xx-fallback-handshake.test.ts b/test/xx-fallback-handshake.test.ts index 7eb7e28..78bea5f 100644 --- a/test/xx-fallback-handshake.test.ts +++ b/test/xx-fallback-handshake.test.ts @@ -9,7 +9,7 @@ import { signPayload } from "../src/utils"; import {generateEd25519Keys, getKeyPairFromPeerId} from "./utils"; -import {Handshake} from "../src/handshake-xx-fallback"; +import {XXFallbackHandshake} from "../src/handshake-xx-fallback"; import {createPeerIdsFromFixtures} from "./fixtures/peer"; import {assert} from "chai"; import {encode0, encode1} from "../src/encoder"; @@ -48,10 +48,10 @@ describe("XX Fallback Handshake", () => { }); const handshakeInit = - new Handshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, peerB, initialMsg, ephemeralKeys); + new XXFallbackHandshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, peerB, initialMsg, ephemeralKeys); const handshakeResp = - new Handshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, peerA, initialMsg); + new XXFallbackHandshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, peerA, initialMsg); await handshakeInit.propose(); diff --git a/test/xx-handshake.test.ts b/test/xx-handshake.test.ts index e1f2cb2..3b651f8 100644 --- a/test/xx-handshake.test.ts +++ b/test/xx-handshake.test.ts @@ -3,7 +3,7 @@ import Duplex from 'it-pair/duplex'; import {Buffer} from "buffer"; import Wrap from "it-pb-rpc"; -import {Handshake} from "../src/handshake-xx"; +import {XXHandshake} from "../src/handshake-xx"; import {generateKeypair} from "../src/utils"; import {createPeerIdsFromFixtures} from "./fixtures/peer"; import {getKeyPairFromPeerId} from "./utils"; @@ -27,10 +27,10 @@ describe("XX Handshake", () => { const staticKeysResponder = generateKeypair(); const { privateKey: initiatorPrivKey, publicKey: initiatorPubKey } = getKeyPairFromPeerId(peerA); - const handshakeInitator = new Handshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, peerB); + const handshakeInitator = new XXHandshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, peerB); const { privateKey: responderPrivKey, publicKey: responderPubKey } = getKeyPairFromPeerId(peerB); - const handshakeResponder = new Handshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, peerA); + const handshakeResponder = new XXHandshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, peerA); await handshakeInitator.propose(); await handshakeResponder.propose(); @@ -72,10 +72,10 @@ describe("XX Handshake", () => { const staticKeysResponder = generateKeypair(); const { privateKey: initiatorPrivKey, publicKey: initiatorPubKey } = getKeyPairFromPeerId(peerA); - const handshakeInitator = new Handshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, fakePeer); + const handshakeInitator = new XXHandshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, fakePeer); const { privateKey: responderPrivKey, publicKey: responderPubKey } = getKeyPairFromPeerId(peerB); - const handshakeResponder = new Handshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, peerA); + const handshakeResponder = new XXHandshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, peerA); await handshakeInitator.propose(); await handshakeResponder.propose(); @@ -100,10 +100,10 @@ describe("XX Handshake", () => { const staticKeysResponder = generateKeypair(); const { privateKey: initiatorPrivKey, publicKey: initiatorPubKey } = getKeyPairFromPeerId(peerA); - const handshakeInitator = new Handshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, peerB); + const handshakeInitator = new XXHandshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom, peerB); const { privateKey: responderPrivKey, publicKey: responderPubKey } = getKeyPairFromPeerId(peerB); - const handshakeResponder = new Handshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, fakePeer); + const handshakeResponder = new XXHandshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo, fakePeer); await handshakeInitator.propose(); await handshakeResponder.propose();