Address PR comments

This commit is contained in:
Belma Gutlic 2020-01-07 13:34:45 +01:00
parent 28bf51c492
commit c3ab986d3d
10 changed files with 51 additions and 50 deletions

View File

@ -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"
}
}
}

View File

@ -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;

View File

@ -1,5 +1,5 @@
import { Buffer } from "buffer";
import {HandshakeInterface} from "./@types/handshake-interface";
import {IHandshake} from "./@types/handshake-interface";
interface ReturnEncryptionWrapper {
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>;
@ -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);

View File

@ -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 {

View File

@ -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<void> {
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,

View File

@ -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<void> {
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,

View File

@ -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<typeof Wrap>;
@ -105,7 +105,7 @@ export class Noise implements NoiseConnection {
* @param libp2pPublicKey
* @param remotePeer
*/
private async performHandshake(params: HandshakeParams): Promise<HandshakeInterface> {
private async performHandshake(params: HandshakeParams): Promise<IHandshake> {
// TODO: Implement noise pipes
if (false) {
@ -127,10 +127,10 @@ export class Noise implements NoiseConnection {
params: HandshakeParams,
ephemeralKeys: KeyPair,
initialMsg: bytes,
): Promise<XXFallback> {
): Promise<XXFallbackHandshake> {
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<XX> {
): Promise<XXHandshake> {
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<IK> {
): Promise<IKHandshake> {
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<Duplex> {
// Create encryption box/unbox wrapper
const [secure, user] = DuplexPair();

View File

@ -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

View File

@ -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();

View File

@ -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();