Skip to content

Commit

Permalink
Use tweetnacl randomBytes instead of node crypto's
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Akhterov <akhterovd@gmail.com>
  • Loading branch information
janaakhterov committed Mar 19, 2020
1 parent b2c06fd commit 684e5c6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
4 changes: 2 additions & 2 deletions __tests__/unit/Keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ describe("Ed25519PrivateKey", () => {

it("derive() produces correct value", async() => {
const iosKey = await Ed25519PrivateKey.fromMnemonic(iosWalletMnemonic, "");
const iosChildKey = await iosKey.derive(0);
const iosChildKey = await iosKey.derive2(0);

expect(iosChildKey.toBytes()).toStrictEqual(iosWalletPrivKeyBytes);
expect(iosChildKey.publicKey.toBytes()).toStrictEqual(iosWalletPubKeyBytes);

const androidKey = await Ed25519PrivateKey.fromMnemonic(androidWalletMnemonic, "");
const androidChildKey = await androidKey.derive(0);
const androidChildKey = await androidKey.derive2(0);

expect(androidChildKey.toBytes()).toStrictEqual(androidWalletPrivKeyBytes);
expect(androidChildKey.publicKey.toBytes()).toStrictEqual(androidWalletPubKeyBytes);
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/Ed25519PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
arraysEqual,
deriveChildKey,
deriveChildKey2,
ed25519PrivKeyPrefix,
randomBytes
ed25519PrivKeyPrefix
} from "./util";
import { RawKeyPair } from "./RawKeyPair";
import { createKeystore, loadKeystore } from "./Keystore";
Expand Down Expand Up @@ -168,8 +167,9 @@ export class Ed25519PrivateKey {
*
* This key will _not_ support child key derivation.
*/
// eslint-disable-next-line require-await
public static async generate(): Promise<Ed25519PrivateKey> {
return this.fromBytes(await randomBytes(32));
return this.fromBytes(nacl.randomBytes(32));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/crypto/Keystore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as crypto from "crypto";
import * as nacl from "tweetnacl";
import { randomBytes } from "./util";
import { RawKeyPair } from "./RawKeyPair";
import { KeyMismatchError } from "./KeyMismatchError";
import * as hex from "@stablelib/hex";
Expand Down Expand Up @@ -45,11 +44,11 @@ export async function createKeystore(
const dkLen = 32;
const c = 262144;
const saltLen = 32;
const salt = await randomBytes(saltLen);
const salt = nacl.randomBytes(saltLen);

const key = await Pbkdf2.deriveKey(HashAlgorithm.Sha256, passphrase, salt, c, dkLen);

const iv = await randomBytes(16);
const iv = nacl.randomBytes(16);

// AES-128-CTR with the first half of the derived key and a random IV
const cipher = crypto.createCipheriv(AES_128_CTR, key.slice(0, 16), iv);
Expand Down
3 changes: 0 additions & 3 deletions src/crypto/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as crypto from "crypto";
import { promisify } from "util";
import { Hmac, HashAlgorithm } from "./Hmac";

export const randomBytes = promisify(crypto.randomBytes);

// we could go through the whole BS of producing a DER-encoded structure but it's quite simple
// for Ed25519 keys and we don't have to shell out to a potentially broken lib
// https://github.com/PeculiarVentures/pvutils/issues/8
Expand Down

0 comments on commit 684e5c6

Please sign in to comment.