Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use browser (subtle) implementation for all env #622

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
"**/*.spec.js"
]
},
"browser": {
"./lib/crypto_subtle.js": "./lib/crypto_subtle_browser.js"
},
"size-limit": [
{
"path": "build/esm/index.js",
Expand Down
24 changes: 1 addition & 23 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
import nodeCrypto from "crypto";

// IE 11
declare global {
interface Window {
msCrypto?: Crypto;
}

interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}

const crypto =
(typeof window !== "undefined" &&
(window as Window) &&
(window.crypto || window.msCrypto)) ||
(nodeCrypto.webcrypto as unknown as Crypto);
const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;

if (subtle === undefined) {
throw new Error("crypto and/or subtle api unavailable");
}
import { crypto, subtle } from "./crypto_subtle";

export { crypto, subtle };

Expand Down
15 changes: 15 additions & 0 deletions src/lib/crypto_subtle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import nodeCrypto from "crypto";

// Types do not seem up-to-date
const crypto: Crypto = nodeCrypto.webcrypto as unknown as Crypto;
if (crypto === undefined) {
throw new Error("node crypto api unavailable");
}

const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;

if (subtle === undefined) {
throw new Error("node subtle api unavailable");
}

export { crypto, subtle };
22 changes: 22 additions & 0 deletions src/lib/crypto_subtle_browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare global {
interface Window {
msCrypto?: Crypto;
}

interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}

const crypto = window.crypto || window.msCrypto;
if (crypto === undefined) {
throw new Error("browser crypto api unavailable");
}

const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;

if (subtle === undefined) {
throw new Error("browser subtle api unavailable");
}

export { crypto, subtle };
16 changes: 3 additions & 13 deletions src/lib/enr/keypair/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import crypto from "crypto";

import * as secp256k1 from "secp256k1";
import { concat } from "uint8arrays/concat";

import { randomBytes } from "../../crypto";

import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types";

export function secp256k1PublicKeyToCompressed(
Expand Down Expand Up @@ -41,7 +41,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
}

static async generate(): Promise<Secp256k1Keypair> {
const privateKey = await randomBytes(32);
const privateKey = randomBytes(32);
const publicKey = secp256k1.publicKeyCreate(privateKey);
return new Secp256k1Keypair(privateKey, publicKey);
}
Expand Down Expand Up @@ -69,13 +69,3 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
return secp256k1.ecdsaVerify(sig, msg, this.publicKey);
}
};

function randomBytes(length: number): Uint8Array {
if (typeof window !== "undefined" && window && window.crypto) {
const array = new Uint8Array(length);
window.crypto.getRandomValues(array);
return array;
} else {
return crypto.randomBytes(length);
}
}
20 changes: 5 additions & 15 deletions src/lib/enr/v4.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import crypto from "crypto";

import { keccak256 } from "js-sha3";
import * as secp256k1 from "secp256k1";

import { randomBytes } from "../crypto";

import { createNodeId } from "./create";
import { NodeId } from "./types";

export function hash(input: Uint8Array): Uint8Array {
return new Uint8Array(keccak256.arrayBuffer(input));
}

export async function createPrivateKey(): Promise<Uint8Array> {
export function createPrivateKey(): Uint8Array {
return randomBytes(32);
}

Expand Down Expand Up @@ -45,13 +45,13 @@ export class ENRKeyPair {
public readonly publicKey: Uint8Array
) {}

public static async create(privateKey?: Uint8Array): Promise<ENRKeyPair> {
public static create(privateKey?: Uint8Array): ENRKeyPair {
if (privateKey) {
if (!secp256k1.privateKeyVerify(privateKey)) {
throw new Error("Invalid private key");
}
}
const _privateKey = privateKey || (await createPrivateKey());
const _privateKey = privateKey || createPrivateKey();
const _publicKey = publicKey(_privateKey);
const _nodeId = nodeId(_publicKey);

Expand All @@ -66,13 +66,3 @@ export class ENRKeyPair {
return verify(this.publicKey, msg, sig);
}
}

function randomBytes(length: number): Uint8Array {
if (typeof window !== "undefined" && window && window.crypto) {
const array = new Uint8Array(length);
window.crypto.getRandomValues(array);
return array;
} else {
return crypto.randomBytes(length);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { IvSize } from "./index";
import { randomBytes, subtle } from "../crypto";

declare global {
interface Window {
msCrypto?: Crypto;
}
interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}

const crypto = window.crypto || window.msCrypto;
const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;
export const KeySize = 32;
export const IvSize = 12;
export const TagSize = 16;

const Algorithm = { name: "AES-GCM", length: 128 };

if (subtle === undefined) {
throw new Error("Failed to load Subtle CryptoAPI");
}

export async function encrypt(
iv: Buffer | Uint8Array,
key: Buffer,
Expand Down Expand Up @@ -45,7 +33,5 @@ export async function decrypt(
}

export function generateIv(): Uint8Array {
const iv = new Uint8Array(IvSize);
crypto.getRandomValues(iv);
return iv;
return randomBytes(IvSize);
}
38 changes: 0 additions & 38 deletions src/lib/waku_message/symmetric/index.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/lib/waku_message/symmetric/node.ts

This file was deleted.

18 changes: 4 additions & 14 deletions src/lib/waku_message/version_1.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Buffer } from "buffer";
import * as crypto from "crypto";

import { keccak256 } from "js-sha3";
import * as secp256k1 from "secp256k1";

import { randomBytes } from "../crypto";
import { hexToBytes } from "../utils";

import * as ecies from "./ecies";
import { IvSize, symmetric, SymmetricKeySize } from "./symmetric";
import * as symmetric from "./symmetric";

const FlagsLength = 1;
const FlagMask = 3; // 0011
Expand Down Expand Up @@ -170,7 +170,7 @@ export async function decryptSymmetric(
key: Uint8Array | Buffer | string
): Promise<Uint8Array> {
const data = Buffer.from(payload);
const ivStart = data.length - IvSize;
const ivStart = data.length - symmetric.IvSize;
const cipher = data.slice(0, ivStart);
const iv = data.slice(ivStart);

Expand All @@ -190,7 +190,7 @@ export function generatePrivateKey(): Uint8Array {
* Generate a new symmetric key to be used for symmetric encryption.
*/
export function generateSymmetricKey(): Uint8Array {
return randomBytes(SymmetricKeySize);
return randomBytes(symmetric.KeySize);
}

/**
Expand Down Expand Up @@ -258,13 +258,3 @@ function ecRecoverPubKey(messageHash: string, signature: Buffer): Uint8Array {
false
);
}

function randomBytes(length: number): Uint8Array {
if (typeof window !== "undefined" && window && window.crypto) {
const array = new Uint8Array(length);
window.crypto.getRandomValues(array);
return array;
} else {
return crypto.randomBytes(length);
}
}