js-libp2p-noise/src/utils.ts

135 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-01-07 17:01:52 +01:00
import { x25519, HKDF, SHA256 } from 'bcrypto';
2019-11-20 21:38:14 +01:00
import protobuf from "protobufjs";
2019-11-22 12:52:59 +01:00
import { Buffer } from "buffer";
2019-12-02 12:53:00 +01:00
import PeerId from "peer-id";
2019-12-03 13:39:33 +01:00
import * as crypto from 'libp2p-crypto';
2019-11-20 13:23:36 +01:00
import { KeyPair } from "./@types/libp2p";
2019-12-24 21:15:38 +01:00
import {bytes, bytes32} from "./@types/basic";
2020-02-10 13:38:35 +01:00
import {Hkdf, INoisePayload} from "./@types/handshake";
import payloadProto from "./proto/payload.json";
2019-11-20 21:38:14 +01:00
2020-02-10 13:53:10 +01:00
async function loadPayloadProto () {
const payloadProtoBuf = await protobuf.Root.fromJSON(payloadProto);
return payloadProtoBuf.lookupType("NoiseHandshakePayload");
2019-11-20 21:38:14 +01:00
}
2019-11-11 21:58:04 +01:00
2019-11-28 17:53:27 +01:00
export function generateKeypair(): KeyPair {
2019-11-11 21:58:04 +01:00
const privateKey = x25519.privateKeyGenerate();
const publicKey = x25519.publicKeyCreate(privateKey);
return {
publicKey,
privateKey,
}
}
2020-01-07 16:59:41 +01:00
export async function getPayload(
localPeer: PeerId,
staticPublicKey: bytes,
earlyData?: bytes,
): Promise<bytes> {
const signedPayload = await signPayload(localPeer, getHandshakePayload(staticPublicKey));
const earlyDataPayload = earlyData || Buffer.alloc(0);
2020-01-07 16:59:41 +01:00
return await createHandshakePayload(
localPeer.marshalPubKey(),
signedPayload,
earlyDataPayload
2020-01-07 16:59:41 +01:00
);
}
2019-11-20 21:38:14 +01:00
export async function createHandshakePayload(
2019-11-21 13:38:39 +01:00
libp2pPublicKey: bytes,
2019-11-20 21:38:14 +01:00
signedPayload: bytes,
earlyData?: bytes,
2019-11-28 17:53:27 +01:00
): Promise<bytes> {
2019-11-20 21:38:14 +01:00
const NoiseHandshakePayload = await loadPayloadProto();
const earlyDataPayload = earlyData ?
2019-11-28 17:32:46 +01:00
{
data: earlyData,
2019-11-28 17:32:46 +01:00
} : {};
2019-11-20 21:38:14 +01:00
const payloadInit = NoiseHandshakePayload.create({
2020-02-05 22:10:51 +01:00
identityKey: libp2pPublicKey,
identitySig: signedPayload,
2019-11-28 17:32:46 +01:00
...earlyDataPayload,
2019-11-20 21:38:14 +01:00
});
return Buffer.from(NoiseHandshakePayload.encode(payloadInit).finish());
}
2020-01-07 16:59:41 +01:00
export async function signPayload(peerId: PeerId, payload: bytes): Promise<bytes> {
return peerId.privKey.sign(payload);
2019-11-11 21:58:04 +01:00
}
2019-11-20 21:38:14 +01:00
export async function getPeerIdFromPayload(payload: INoisePayload): Promise<PeerId> {
return await PeerId.createFromPubKey(Buffer.from(payload.identityKey));
2020-02-07 20:21:27 +01:00
}
export async function decodePayload(payload: bytes): Promise<INoisePayload> {
2020-02-07 20:21:27 +01:00
const NoiseHandshakePayload = await loadPayloadProto();
return NoiseHandshakePayload.toObject(
NoiseHandshakePayload.decode(payload)
2020-02-10 13:38:35 +01:00
) as INoisePayload;
2020-02-07 20:21:27 +01:00
}
2019-11-28 17:32:46 +01:00
export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.from("noise-libp2p-static-key:"), publicKey]);
2019-12-03 13:39:33 +01:00
async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) {
const generatedPeerId = await PeerId.createFromPubKey(publicKeyProtobuf);
return generatedPeerId.id.equals(peerId);
2019-12-02 12:53:00 +01:00
}
2020-02-08 12:23:35 +01:00
/**
* Verifies signed payload, throws on any irregularities.
2020-02-08 12:23:35 +01:00
* @param {bytes} noiseStaticKey - owner's noise static key
* @param {bytes} payload - decoded payload
* @param {PeerId} remotePeer - owner's libp2p peer ID
2020-02-08 12:23:35 +01:00
* @returns {Promise<PeerId>} - peer ID of payload owner
*/
export async function verifySignedPayload(
noiseStaticKey: bytes,
payload: INoisePayload,
remotePeer: PeerId
2020-02-08 12:23:35 +01:00
): Promise<PeerId> {
2020-01-15 17:27:32 +01:00
try {
//temporary fix until protobufsjs conversion options starts working
//by default it ends up as Uint8Array
payload.identityKey = Buffer.from(payload.identityKey);
payload.identitySig = Buffer.from(payload.identitySig);
2020-01-15 17:27:32 +01:00
} catch (e) {
throw new Error("Failed to decode received payload. Reason: " + e.message);
2020-01-15 17:27:32 +01:00
}
2019-12-02 12:53:00 +01:00
if (!(await isValidPeerId(remotePeer.id, payload.identityKey)) ) {
2019-12-03 13:39:33 +01:00
throw new Error("Peer ID doesn't match libp2p public key.");
}
const generatedPayload = getHandshakePayload(noiseStaticKey);
2020-01-07 17:08:08 +01:00
// Unmarshaling from PublicKey protobuf
const publicKey = crypto.keys.unmarshalPublicKey(payload.identityKey);
if (!publicKey.verify(generatedPayload, payload.identitySig)) {
throw new Error("Static key doesn't match to peer that signed payload!");
2019-12-02 12:53:00 +01:00
}
2020-02-08 12:23:35 +01:00
return remotePeer;
2019-12-02 12:53:00 +01:00
}
2019-12-24 21:15:38 +01:00
export function getHkdf(ck: bytes32, ikm: bytes): Hkdf {
const info = Buffer.alloc(0);
const prk = HKDF.extract(SHA256, ikm, ck);
const okm = HKDF.expand(SHA256, prk, info, 96);
const k1 = okm.slice(0, 32);
const k2 = okm.slice(32, 64);
const k3 = okm.slice(64, 96);
return [ k1, k2, k3 ];
}
2019-12-29 18:23:43 +01:00
export function isValidPublicKey(pk: bytes): boolean {
return x25519.publicKeyVerify(pk);
}