-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connect):
resetDevice
with entropy check
- Loading branch information
1 parent
548017e
commit dc41db0
Showing
9 changed files
with
307 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/connect/src/api/firmware/__tests__/verifyEntropy.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { verifyEntropy } from '../verifyEntropy'; | ||
|
||
describe('firmware/verifyEntropy', () => { | ||
it('bip39 success', async () => { | ||
const response = await verifyEntropy({ | ||
strength: 256, | ||
hostEntropy: 'e14806194511f95f2e6b7c5267fcb824469a478007d97339da08abb379244553', | ||
commitment: '09c7dff5c85814852fb9cb12feecd11183f7af1c10296a5075f276c5eca9fb44', | ||
trezorEntropy: 'ffa4581852ee93e789b9e83554f58dd1a3e09765a64d91d8cd08f6b5c813745d', | ||
xpubs: { | ||
"m/84'/0'/0'": | ||
'xpub6CCMQserNP7QkjspvUVWfCdjK1FcgFdmka3ZgzVgZKqkkCL5bfQoscxZ9UzTLLLedPGwkhQobEGE84gWvZY1tXaHJsVLMHA6cXNUmXnrj3s', | ||
"m/44'/60'/0'": | ||
'xpub6Cdh8AtW8tSXTDYYGirGkmTCgYe4SCCAuqJLmqkhDFYVCkHLngQ7JmNSVuCQuQARqx5tDJJ9my1JCgaUHHizioZZoXRWw6LR95uQUkbKJi3', | ||
}, | ||
}); | ||
expect(response.success).toEqual(true); | ||
}); | ||
|
||
it('slip39 success', async () => { | ||
const response = await verifyEntropy({ | ||
type: 1, | ||
strength: 256, | ||
hostEntropy: '20e1524b5ea128b581c8882ddd5b030dd54e3bf49c8e7063768be13e0add4420', | ||
commitment: '0ea37a3ae4e765ac59f6d721548920c92667bb9cc09b53ebf81404d3c07794a1', | ||
trezorEntropy: '00610ab95fe09b3d32320662e96ba195344461b00322a09b86df68432db1e745', | ||
xpubs: { | ||
"m/84'/0'/0'": | ||
'xpub6CxDGHMZekeQtFmny74NHcf1cA8opN8yWHLdmXhwhin7WrjCWKgypDyG5SoCR7ae57JqPT8ZWd2st56yzgC8bzzpHRDurXxZxkaZfXeF1bW', | ||
"m/44'/60'/0'": | ||
'xpub6CV17nmnkijMua6ZpyRU7MNnZjHoByRGoWf2nPxJmKF1EriH92awnhV7KS2X1mB6ke1fuRerGir3kvZr6uRcQqn2Pnv48gmhtsyaHcLALVG', | ||
}, | ||
}); | ||
expect(response.success).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { entropyToMnemonic, mnemonicToSeed } from 'bip39'; | ||
import { hmac } from '@noble/hashes/hmac'; | ||
import { crypto } from '@noble/hashes/crypto'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { randomBytes } from '@noble/hashes/utils'; | ||
|
||
import { bip32 } from '@trezor/utxo-lib'; | ||
|
||
import { PROTO } from '../../constants'; | ||
|
||
export const generateEntropy = (len: number) => { | ||
try { | ||
return Buffer.from(randomBytes(len)); | ||
} catch { | ||
throw new Error('generateEntropy: Environment does not support crypto random'); | ||
} | ||
}; | ||
|
||
// https://github.com/trezor/python-shamir-mnemonic/blob/master/shamir_mnemonic/cipher.py | ||
const BASE_ITERATION_COUNT = 10000; | ||
const ROUND_COUNT = 4; | ||
|
||
// https://github.com/trezor/python-shamir-mnemonic/blob/master/shamir_mnemonic/cipher.py | ||
const roundFunction = async (i: number, passphrase: Buffer, e: number, salt: Buffer, r: Buffer) => { | ||
const data = Buffer.concat([Buffer.from([i]), passphrase]); | ||
const iterations = Math.floor((BASE_ITERATION_COUNT << e) / ROUND_COUNT); | ||
|
||
// '@noble/hashes/pbkdf2' takes ~ 8sec. in the web build | ||
// const result = pbkdf2(sha256, data, Buffer.concat([salt, r]), { | ||
// c: iterations, | ||
// dkLen: r.length, | ||
// }); | ||
|
||
// Nodejs only | ||
// return crypto.pbkdf2Sync(data, Buffer.concat([salt, r]), iterations, r.length, 'sha256'); | ||
|
||
// Nodejs + WebCrypto equivalent | ||
const { subtle } = crypto as Crypto; | ||
const key = await subtle.importKey('raw', data, 'PBKDF2', false, ['deriveBits']); | ||
const bits = await subtle.deriveBits( | ||
{ | ||
name: 'PBKDF2', | ||
hash: 'SHA-256', | ||
salt: Buffer.concat([salt, r]), | ||
iterations, | ||
}, | ||
key, | ||
r.length * 8, | ||
); | ||
|
||
return Buffer.from(bits); | ||
}; | ||
|
||
// https://github.com/trezor/python-shamir-mnemonic/blob/master/shamir_mnemonic/cipher.py | ||
const xor = (a: Buffer, b: Buffer) => { | ||
if (a.length !== b.length) { | ||
throw new Error('Buffers must be of equal length to XOR.'); | ||
} | ||
const result = Buffer.alloc(a.length); | ||
for (let i = 0; i < a.length; i++) { | ||
result[i] = a[i] ^ b[i]; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// https://github.com/trezor/python-shamir-mnemonic/blob/master/shamir_mnemonic/cipher.py | ||
// simplified "decrypt" function | ||
const entropyToSeedSlip39 = async (encryptedSecret: Buffer) => { | ||
const iterationExponent = 1; | ||
// const identifier = 0; | ||
// const extendable = true, | ||
const passphrase = Buffer.from('', 'utf-8'); // empty passphrase | ||
const salt = Buffer.alloc(0); // extendable: True => no salt | ||
|
||
const half = Math.floor(encryptedSecret.length / 2); | ||
let l = encryptedSecret.subarray(0, half); | ||
let r = encryptedSecret.subarray(half); | ||
for (let round = ROUND_COUNT - 1; round >= 0; round--) { | ||
const f = await roundFunction(round, passphrase, iterationExponent, salt, r); | ||
const rr = xor(l, f); | ||
l = r; | ||
r = rr; | ||
} | ||
|
||
return Buffer.concat([r, l]); | ||
}; | ||
|
||
const getEntropy = (trezorEntropy: string, hostEntropy: string, strength: number) => { | ||
const data = Buffer.concat([ | ||
Buffer.from(trezorEntropy, 'hex'), | ||
Buffer.from(hostEntropy, 'hex'), | ||
]); | ||
const entropy = sha256(data); | ||
|
||
return Buffer.from(entropy.subarray(0, Math.floor(strength / 8))); | ||
}; | ||
|
||
const computeSeed = (type: VerifyEntropyOptions['type'], secret: Buffer) => { | ||
const BackupType = PROTO.Enum_BackupType; | ||
if ( | ||
type && | ||
[ | ||
BackupType.Slip39_Basic, | ||
BackupType.Slip39_Advanced, | ||
BackupType.Slip39_Single_Extendable, | ||
BackupType.Slip39_Basic_Extendable, | ||
BackupType.Slip39_Advanced_Extendable, | ||
].includes(type) | ||
) { | ||
// use slip39 | ||
return entropyToSeedSlip39(secret); | ||
} | ||
|
||
// use bip39 | ||
return mnemonicToSeed(entropyToMnemonic(secret)); | ||
}; | ||
|
||
const verifyCommitment = (entropy: string, commitment: string) => { | ||
const hmacDigest = hmac(sha256, Buffer.from(entropy, 'hex'), Buffer.alloc(0)); | ||
if (!Buffer.from(hmacDigest).equals(Buffer.from(commitment, 'hex'))) { | ||
throw new Error('Invalid entropy commitment'); | ||
} | ||
}; | ||
|
||
type VerifyEntropyOptions = { | ||
type?: PROTO.Enum_BackupType; // ResetDevice.backup_type | ||
strength?: number; // ResetDevice.strength | ||
commitment?: string; // entropy_commitment received from previous EntropyRequest | ||
hostEntropy: string; // host_entropy used in previous EntropyAck | ||
trezorEntropy?: string; // prev_entropy received from current EntropyRequest, after ResetDeviceContinue | ||
xpubs: Record<string, string>; // <address_n, xpub> | ||
}; | ||
|
||
export const verifyEntropy = async ({ | ||
type, | ||
strength, | ||
trezorEntropy, | ||
hostEntropy, | ||
commitment, | ||
xpubs, | ||
}: VerifyEntropyOptions) => { | ||
try { | ||
if (!trezorEntropy || !commitment || !strength || Object.keys(xpubs).length < 1) { | ||
throw new Error('Missing verifyEntropy data'); | ||
} | ||
|
||
verifyCommitment(trezorEntropy, commitment); | ||
// compute seed | ||
const secret = getEntropy(trezorEntropy, hostEntropy, strength); | ||
const seed = await computeSeed(type, secret); | ||
|
||
// derive xpubs and compare with FW results | ||
const node = bip32.fromSeed(seed); | ||
Object.keys(xpubs).forEach(path => { | ||
const pubKey = node.derivePath(path); | ||
const xpub = pubKey.neutered().toBase58(); | ||
if (xpub !== xpubs[path]) { | ||
throw new Error('verifyEntropy xpub mismatch'); | ||
} | ||
}); | ||
|
||
return { success: true as const }; | ||
} catch (error) { | ||
return { success: false as const, error: error.message }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.