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

Feature / Keystore Extra Entropy #1295

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
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
26 changes: 8 additions & 18 deletions src/controllers/keystore/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ import {
encryptWithPublicKey,
publicKeyByPrivateKey
} from 'eth-crypto'
import {
concat,
getBytes,
hexlify,
keccak256,
Mnemonic,
randomBytes,
toUtf8Bytes,
Wallet
} from 'ethers'
import { concat, getBytes, hexlify, keccak256, Mnemonic, toUtf8Bytes, Wallet } from 'ethers'
import scrypt from 'scrypt-js'

import EmittableError from '../../classes/EmittableError'
Expand All @@ -38,6 +29,7 @@ import {
} from '../../interfaces/keystore'
import { Storage } from '../../interfaces/storage'
import { WindowManager } from '../../interfaces/window'
import { EntropyGenerator } from '../../libs/entropyGenerator/entropyGenerator'
import {
getDefaultKeyLabel,
getShouldMigrateKeyMetaNullToKeyMetaCreatedAt,
Expand Down Expand Up @@ -301,16 +293,14 @@ export class KeystoreController extends EventEmitter {
})

let mainKey: MainKey | null = this.#mainKey
const entropyGenerator = new EntropyGenerator()

// We are not unlocked
if (!mainKey) {
if (!this.#keystoreSecrets.length) {
const key = getBytes(keccak256(concat([randomBytes(32), toUtf8Bytes(extraEntropy)]))).slice(
0,
16
)
mainKey = {
key,
iv: randomBytes(16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do we take the extra entropy from?

key: entropyGenerator.generateRandomBytes(16, extraEntropy),
iv: entropyGenerator.generateRandomBytes(16, extraEntropy)
}
} else
throw new EmittableError({
Expand All @@ -324,7 +314,7 @@ export class KeystoreController extends EventEmitter {
}
}

const salt = randomBytes(32)
const salt = entropyGenerator.generateRandomBytes(32, extraEntropy)
const key = await scrypt.scrypt(
getBytesForSecret(secret),
salt,
Expand All @@ -334,7 +324,7 @@ export class KeystoreController extends EventEmitter {
scryptDefaults.dkLen,
() => {}
)
const iv = randomBytes(16)
const iv = entropyGenerator.generateRandomBytes(16, extraEntropy)
const derivedKey = key.slice(0, 16)
const macPrefix = key.slice(16, 32)
const counter = new aes.Counter(iv)
Expand Down
54 changes: 54 additions & 0 deletions src/libs/entropyGenerator/entropyGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable no-bitwise */
import { getBytes, keccak256, randomBytes } from 'ethers'

export class EntropyGenerator {
#entropyPool: Uint8Array = new Uint8Array(0)

generateRandomBytes(length: number, extraEntropy: string): Uint8Array {
this.#resetEntropyPool()
this.#collectSystemNoiseEntropy()
this.#collectTimeEntropy()

if (extraEntropy) {
const encoder = new TextEncoder()
const uint8Array = encoder.encode(extraEntropy)
this.addEntropy(uint8Array)
}

if (this.#entropyPool.length === 0) throw new Error('Entropy pool is empty')

const hash = getBytes(keccak256(this.#entropyPool))
const randomBytesGenerated = randomBytes(length)
// ensures non-deterministic final output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is incorrect - not sure what you meant but it is deterministic - the same input will always generate the same output

for (let i = 0; i < length; i++) {
randomBytesGenerated[i] ^= hash[i % hash.length]
}

return randomBytesGenerated
}

#collectTimeEntropy(): void {
const now = performance.now()

if (!now) return

const timeEntropy = new Uint8Array(new Float64Array([now]).buffer)
this.addEntropy(timeEntropy)
}

#collectSystemNoiseEntropy(): void {
const systemNoise = randomBytes(16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not really system noise though

this.addEntropy(systemNoise)
}

addEntropy(newEntropy: Uint8Array): void {
const combined = new Uint8Array(this.#entropyPool.length + newEntropy.length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a better way to concat?

combined.set(this.#entropyPool)
combined.set(newEntropy, this.#entropyPool.length)
this.#entropyPool = combined
}

#resetEntropyPool() {
this.#entropyPool = new Uint8Array(0)
}
}
Loading