Add types and encryption methods

This commit is contained in:
morrigan 2019-11-12 14:02:59 +01:00
parent d2c844d598
commit 26b112f712
6 changed files with 49 additions and 20 deletions

View File

@ -52,6 +52,7 @@
"bcrypto": "^4.2.3", "bcrypto": "^4.2.3",
"bn.js": "^5.0.0", "bn.js": "^5.0.0",
"buffer": "^5.4.3", "buffer": "^5.4.3",
"it-pair": "^1.0.0",
"protobufjs": "~6.8.8" "protobufjs": "~6.8.8"
} }
} }

13
src/crypto.ts Normal file
View File

@ -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<void> {
}
// Decrypt received payload from the stream and pipe to user
export async function decryptStreams(streams: Duplex, session: NoiseSession) : Promise<void> {
}

View File

@ -6,10 +6,11 @@ import { InsecureConnection, NoiseConnection, PeerId, SecureConnection, KeyPair
import { Handshake } from "./handshake"; import { Handshake } from "./handshake";
import { generateKeypair, signPayload } from "./utils"; import { generateKeypair, signPayload } from "./utils";
import {encryptStream} from "./crypto";
export class Noise implements NoiseConnection { export class Noise implements NoiseConnection {
private readonly privateKey: bytes; private readonly privateKey: bytes;
private staticKeys?: KeyPair; private staticKeys: KeyPair;
private earlyData?: bytes; private earlyData?: bytes;
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) { constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
@ -22,6 +23,8 @@ export class Noise implements NoiseConnection {
privateKey: staticNoiseKey, privateKey: staticNoiseKey,
publicKey, publicKey,
} }
} else {
// todo: generate new static key
} }
} }
@ -43,14 +46,6 @@ export class Noise implements NoiseConnection {
public async secureInbound(connection: InsecureConnection) : Promise<SecureConnection> { public async secureInbound(connection: InsecureConnection) : Promise<SecureConnection> {
} }
private async read(ciphertext: bytes) {
}
private async write(plaintext: bytes) {
}
private async createSecureConnection( private async createSecureConnection(
connection: InsecureConnection, connection: InsecureConnection,
remotePublicKey: bytes, remotePublicKey: bytes,
@ -69,13 +64,13 @@ export class Noise implements NoiseConnection {
const prologue = Buffer.from(this.protocol()); const prologue = Buffer.from(this.protocol());
const session = await Handshake.runXX(isInitiator, remotePublicKey, prologue, signedPayload, this.staticKeys); const session = await Handshake.runXX(isInitiator, remotePublicKey, prologue, signedPayload, this.staticKeys);
await encryptStream(connection.streams, session);
return { return {
insecure: connection, ...connection,
initiator: isInitiator, initiator: isInitiator,
prologue, prologue,
// localKey: get public key, // localKey: get public key,
localPeer: connection.localPeer,
remotePeer: connection.remotePeer,
local: { local: {
noiseKey: this.staticKeys.publicKey, noiseKey: this.staticKeys.publicKey,
// libp2pKey: // libp2pKey:

View File

@ -22,16 +22,23 @@ type ConnectionStats = {
encryption: string, encryption: string,
} }
type Stream = {
sink(), // Also seen as Pair
export type Stream = {
sink(source: Iterable<any>),
source: Object, source: Object,
} }
export type Duplex = [Stream, Stream];
export interface InsecureConnection { export interface InsecureConnection {
localPeer: PeerId, localPeer: PeerId,
remotePeer: PeerId, remotePeer: PeerId,
local: PeerInfo,
remote: PeerInfo,
stats: ConnectionStats, stats: ConnectionStats,
streams(): [Stream],
streams(): Duplex,
addStream(muxedStream: any) : Stream, addStream(muxedStream: any) : Stream,
} }
@ -42,14 +49,9 @@ export interface NoiseConnection {
} }
export interface SecureConnection { export interface SecureConnection {
insecure: InsecureConnection,
initiator: boolean, initiator: boolean,
prologue: bytes32, prologue: bytes32,
localKey: bytes, localKey: bytes,
localPeer: PeerId,
remotePeer: PeerId,
local: PeerInfo,
remote: PeerInfo,
xxNoiseSession: NoiseSession, xxNoiseSession: NoiseSession,
xxComplete: boolean, xxComplete: boolean,

View File

@ -227,6 +227,9 @@ export class XXHandshake {
private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> { private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
let ns = Buffer.alloc(0); let ns = Buffer.alloc(0);
hs.e = await generateKeypair(); hs.e = await generateKeypair();
if (!hs.e) {
throw new Error("Handshake state has keypair missing.");
}
const ne = hs.e.publicKey; const ne = hs.e.publicKey;
this.mixHash(hs.ss, ne); this.mixHash(hs.ss, ne);
@ -237,6 +240,9 @@ export class XXHandshake {
private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> { private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
hs.e = await generateKeypair(); hs.e = await generateKeypair();
if (!hs.e) {
throw new Error("Handshake state has keypair missing.");
}
const ne = hs.e.publicKey; const ne = hs.e.publicKey;
this.mixHash(hs.ss, ne); this.mixHash(hs.ss, ne);

View File

@ -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" resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE= 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: get-value@^2.0.3, get-value@^2.0.6:
version "2.0.6" version "2.0.6"
resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28" 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" resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8= 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: js-levenshtein@^1.1.3:
version "1.1.6" version "1.1.6"
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d" resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"