-
-
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
fef3825
commit f54ba20
Showing
7 changed files
with
243 additions
and
20 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
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
import { verifyEntropy } from '../verifyEntropy'; | ||
|
||
describe('firmware/verifyEntropy', () => { | ||
it('bip39 success', async () => { | ||
const response = await verifyEntropy({ | ||
type: undefined, | ||
strength: 256, | ||
hostEntropy: '16180a5ae8e1b3976367cb9afa79856b4e7fea76c8751d37debc0b89f4942a73', | ||
trezorEntropy: '3b3bc4625e77c23825770c4240be26319f1c74bf72c14204293208f174d5d6a3', | ||
xpubs: { | ||
"m/84'/0'/0'": | ||
'xpub6CnUiQ2hMiKiwjYZ467qtSQVNmUertqUDYTdSLkZzTr6Y7b66WhCiPeA1RXTfC1ZWgqsiqH1uY7tgGW7xPEN1361vw7QsEr9zAiibayh7rg', | ||
"m/44'/60'/0'": | ||
'xpub6DLfqKZGZAzXAdEL8dA6u84An3mh1QuSHaxhhvswB9BKzfzEUes31pZZ1LzV7e8iDRfKhwo2xTwoizqSBaHLZJbHbKnHAseZJneLefbXwce', | ||
}, | ||
}); | ||
expect(response.success).toEqual(true); | ||
}); | ||
|
||
it('slip39 success', async () => { | ||
const response = await verifyEntropy({ | ||
type: 1, | ||
strength: 256, | ||
hostEntropy: '0f675453428a5c0075f75a43bf0bacc6b46053d85ac96a1923c52f8c8a73cfb3', | ||
trezorEntropy: '3ca3d7ec6b25481fce7b7439d550cba730ecc3a8cf112defe2540814d94ebfdc', | ||
xpubs: { | ||
"m/84'/0'/0'": | ||
'xpub6CToV2Azz3zvtb9J25ge7oPux9SWHk61DHk3Y9H4wXpBwKTd7BHYEPMTm6SvmiNaZDecfk5qN1mtXPx5kJwy7kyYTqhEUpJU4NFSpXwm4fR', | ||
"m/44'/60'/0'": | ||
'xpub6CHPgi7CWuYVXyB98q9cW6qPbxxAB9WWTZqCofsSKgasrapwDDWeUxx9D3p3r6UKBdVCndP7AFjj6QG7tdTSaunPo1DFETSaHiKe6Ds6tsr', | ||
}, | ||
}); | ||
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,129 @@ | ||
import { entropyToMnemonic, mnemonicToSeed } from 'bip39'; | ||
import { pbkdf2 } from '@noble/hashes/pbkdf2'; | ||
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 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 = (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); | ||
|
||
const result = pbkdf2(sha256, data, Buffer.concat([salt, r]), { | ||
c: iterations, | ||
dkLen: r.length, | ||
}); | ||
|
||
return Buffer.from(result); | ||
}; | ||
|
||
// 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 = (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 = roundFunction(round, passphrase, iterationExponent, salt, r); | ||
const rr = xor(l, f); | ||
l = r; | ||
r = rr; | ||
} | ||
|
||
return Buffer.concat([r, l]); | ||
}; | ||
|
||
const getEntropy = (options: Options) => { | ||
const data = Buffer.concat([ | ||
Buffer.from(options.trezorEntropy, 'hex'), | ||
Buffer.from(options.hostEntropy, 'hex'), | ||
]); | ||
const entropy = sha256(data); | ||
const strength = Math.floor(options.strength / 8); | ||
|
||
return Buffer.from(entropy.subarray(0, strength)); | ||
}; | ||
|
||
const computeSeed = (options: Options) => { | ||
const secret = getEntropy(options); | ||
const BackupType = PROTO.Enum_BackupType; | ||
if ( | ||
options.type && | ||
[ | ||
BackupType.Slip39_Basic, | ||
BackupType.Slip39_Advanced, | ||
BackupType.Slip39_Single_Extendable, | ||
BackupType.Slip39_Basic_Extendable, | ||
BackupType.Slip39_Advanced_Extendable, | ||
].includes(options.type) | ||
) { | ||
// use slip39 | ||
return entropyToSeedSlip39(secret); | ||
} | ||
|
||
// use bip39 | ||
return mnemonicToSeed(entropyToMnemonic(secret)); | ||
}; | ||
|
||
type Options = { | ||
type?: PROTO.Enum_BackupType; | ||
strength: number; | ||
hostEntropy: string; | ||
trezorEntropy: string; | ||
xpubs: Record<string, string>; | ||
}; | ||
|
||
export const verifyEntropy = async (options: Options) => { | ||
try { | ||
// compute seed | ||
const seed = await computeSeed(options); | ||
|
||
// derive xpubs and compare with FW results | ||
const node = bip32.fromSeed(seed); | ||
Object.keys(options.xpubs).forEach(path => { | ||
const pubKey = node.derivePath(path); | ||
const xpub = pubKey.neutered().toBase58(); | ||
if (xpub !== options.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
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