From 26b112f712d2adbd6b91866f34f627e7a69fb45c Mon Sep 17 00:00:00 2001 From: morrigan Date: Tue, 12 Nov 2019 14:02:59 +0100 Subject: [PATCH] Add types and encryption methods --- package.json | 1 + src/crypto.ts | 13 +++++++++++++ src/noise.ts | 19 +++++++------------ src/types/libp2p.ts | 18 ++++++++++-------- src/xx.ts | 6 ++++++ yarn.lock | 12 ++++++++++++ 6 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 src/crypto.ts diff --git a/package.json b/package.json index 5e7e1f0..5a57171 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "bcrypto": "^4.2.3", "bn.js": "^5.0.0", "buffer": "^5.4.3", + "it-pair": "^1.0.0", "protobufjs": "~6.8.8" } } diff --git a/src/crypto.ts b/src/crypto.ts new file mode 100644 index 0000000..8d205a9 --- /dev/null +++ b/src/crypto.ts @@ -0,0 +1,13 @@ +import { Duplex } from "./types/libp2p"; +import { NoiseSession } from "./xx"; + +// Send encrypted payload from the user to stream +export async function encryptStreams(streams: Duplex, session: NoiseSession) : Promise { + +} + + +// Decrypt received payload from the stream and pipe to user +export async function decryptStreams(streams: Duplex, session: NoiseSession) : Promise { + +} diff --git a/src/noise.ts b/src/noise.ts index 3af12e9..085e5f6 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -6,10 +6,11 @@ import { InsecureConnection, NoiseConnection, PeerId, SecureConnection, KeyPair import { Handshake } from "./handshake"; import { generateKeypair, signPayload } from "./utils"; +import {encryptStream} from "./crypto"; export class Noise implements NoiseConnection { private readonly privateKey: bytes; - private staticKeys?: KeyPair; + private staticKeys: KeyPair; private earlyData?: bytes; constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) { @@ -22,6 +23,8 @@ export class Noise implements NoiseConnection { privateKey: staticNoiseKey, publicKey, } + } else { + // todo: generate new static key } } @@ -43,14 +46,6 @@ export class Noise implements NoiseConnection { public async secureInbound(connection: InsecureConnection) : Promise { } - private async read(ciphertext: bytes) { - - } - - private async write(plaintext: bytes) { - - } - private async createSecureConnection( connection: InsecureConnection, remotePublicKey: bytes, @@ -69,13 +64,13 @@ export class Noise implements NoiseConnection { const prologue = Buffer.from(this.protocol()); const session = await Handshake.runXX(isInitiator, remotePublicKey, prologue, signedPayload, this.staticKeys); + await encryptStream(connection.streams, session); + return { - insecure: connection, + ...connection, initiator: isInitiator, prologue, // localKey: get public key, - localPeer: connection.localPeer, - remotePeer: connection.remotePeer, local: { noiseKey: this.staticKeys.publicKey, // libp2pKey: diff --git a/src/types/libp2p.ts b/src/types/libp2p.ts index 2cd1fc3..e2a5a8a 100644 --- a/src/types/libp2p.ts +++ b/src/types/libp2p.ts @@ -22,16 +22,23 @@ type ConnectionStats = { encryption: string, } -type Stream = { - sink(), + +// Also seen as Pair +export type Stream = { + sink(source: Iterable), source: Object, } +export type Duplex = [Stream, Stream]; + export interface InsecureConnection { localPeer: PeerId, remotePeer: PeerId, + local: PeerInfo, + remote: PeerInfo, stats: ConnectionStats, - streams(): [Stream], + + streams(): Duplex, addStream(muxedStream: any) : Stream, } @@ -42,14 +49,9 @@ export interface NoiseConnection { } export interface SecureConnection { - insecure: InsecureConnection, initiator: boolean, prologue: bytes32, localKey: bytes, - localPeer: PeerId, - remotePeer: PeerId, - local: PeerInfo, - remote: PeerInfo, xxNoiseSession: NoiseSession, xxComplete: boolean, diff --git a/src/xx.ts b/src/xx.ts index 1c51a51..3acde91 100644 --- a/src/xx.ts +++ b/src/xx.ts @@ -227,6 +227,9 @@ export class XXHandshake { private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise { let ns = Buffer.alloc(0); hs.e = await generateKeypair(); + if (!hs.e) { + throw new Error("Handshake state has keypair missing."); + } const ne = hs.e.publicKey; this.mixHash(hs.ss, ne); @@ -237,6 +240,9 @@ export class XXHandshake { private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise { hs.e = await generateKeypair(); + if (!hs.e) { + throw new Error("Handshake state has keypair missing."); + } const ne = hs.e.publicKey; this.mixHash(hs.ss, ne); diff --git a/yarn.lock b/yarn.lock index 2ef30f1..3482f1f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1882,6 +1882,11 @@ get-func-name@^2.0.0: resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41" integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE= +get-iterator@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/get-iterator/-/get-iterator-1.0.2.tgz#cd747c02b4c084461fac14f48f6b45a80ed25c82" + integrity sha512-v+dm9bNVfOYsY1OrhaCrmyOcYoSeVvbt+hHZ0Au+T+p1y+0Uyj9aMaGIeUTT6xdpRbWzDeYKvfOslPhggQMcsg== + get-value@^2.0.3, get-value@^2.0.6: version "2.0.6" resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28" @@ -2304,6 +2309,13 @@ isobject@^3.0.0, isobject@^3.0.1: resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df" integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8= +it-pair@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/it-pair/-/it-pair-1.0.0.tgz#b1add81f49af16a10b2939dbef7b1974fae87d6a" + integrity sha512-9raOiDu5OAuDOahtMtapKQDrQTxBfzlzrNcB6o7JARHkt+7Bb1dMkW/TpYdAjBJE77KH3e2zGzwpGUP9tXbLww== + dependencies: + get-iterator "^1.0.2" + js-levenshtein@^1.1.3: version "1.1.6" resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"