Repository navigation
chore: sync from GitLab (v1.8.7) #196
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
deddf73
Merge branch 'docs/remove-fossa-badge' into 'main'
SlavaSereb 7ebc9ec
fix: strict PKCS#7 and UTF-8 passphrase for mobile share decryption
cshooker-tech 02f70fc
fix: report unknown algorithm instead of passphrase error and keep JS…
cshooker-tech f39f1cd
fix: bump version to 1.8.7 for recovery-utility
SlavaSereb 9e78535
Merge branch 'fix/mobile-share-strict-padding-utf8' into 'main'
SlavaSereb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@fireblocks/extended-key-recovery': patch | ||
| --- | ||
|
|
||
| Mobile key share decryption now uses strict PKCS#7 padding and validates the decrypted share, so a wrong passphrase reports a passphrase error instead of failing the metadata check; the passphrase is encoded as UTF-8 (with a legacy raw-char-code fallback) so non-ASCII passphrases work |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| import { createCipheriv, pbkdf2Sync, randomBytes } from 'crypto'; | ||
| import { decryptMobilePrivateKey } from '../src/decrypt'; | ||
| import { recoverMobileKeyShare } from '../src/mobileKey'; | ||
| import { DecryptMobileKeyError, UnknownAlgorithmError } from '../src/types'; | ||
|
|
||
| const KEY_ID = '00000000-0000-4000-8000-000000000001'; | ||
| const USER_ID = '00000000-0000-4000-8000-000000000002'; | ||
| const DEVICE_ID = '00000000-0000-4000-8000-000000000003'; | ||
| const ALGO_CMP_ECDSA = 0; | ||
|
|
||
| const signingKeys: any = { [KEY_ID]: { algo: 'MPC_CMP_ECDSA_SECP256K1', keysetId: 1 } }; | ||
|
|
||
| // Mirrors how the mobile apps wrap a V1 share: PBKDF2-HMAC-SHA1(10k) over the UTF-8 passphrase, zero IV, AES-256-CBC. | ||
| const encryptV1 = (plaintext: Buffer, pass: string): Buffer => { | ||
| const key = pbkdf2Sync(Buffer.from(pass, 'utf8'), USER_ID, 10000, 32, 'sha1'); | ||
| const cipher = createCipheriv('aes-256-cbc', key, Buffer.alloc(16)); | ||
| return Buffer.concat([cipher.update(plaintext), cipher.final()]); | ||
| }; | ||
|
|
||
| const buildShare = (plaintext: Buffer, pass: string): string => | ||
| JSON.stringify({ | ||
| keyId: KEY_ID, | ||
| userId: USER_ID, | ||
| deviceId: DEVICE_ID, | ||
| encryptedKey: encryptV1(plaintext, pass).toString('hex'), | ||
| }); | ||
|
|
||
| const rawShare = () => randomBytes(32); | ||
| const prefixedShare = () => Buffer.concat([Buffer.from([ALGO_CMP_ECDSA, 0, 0, 0]), randomBytes(32)]); | ||
| const jsonShare = () => Buffer.from(JSON.stringify({ key: randomBytes(32).toString('hex'), pad: 'x'.repeat(200) })); | ||
|
|
||
| const wrongPassphrases = (n: number): string[] => Array.from({ length: n }, (_, i) => `wrong-passphrase-${i}-Aa1!`); | ||
|
|
||
| const tryRecover = (share: string, pass: string): 'ok' | 'rejected' => { | ||
| try { | ||
| recoverMobileKeyShare(signingKeys, share, pass); | ||
| return 'ok'; | ||
| } catch (e) { | ||
| if (e instanceof DecryptMobileKeyError) return 'rejected'; | ||
| throw e; | ||
| } | ||
| }; | ||
|
|
||
| describe('V1 mobile share decryption', () => { | ||
| const PASS = 'Correct-Horse-1!'; | ||
|
|
||
| it.each([ | ||
| ['raw 32 byte share', rawShare], | ||
| ['36 byte algo-prefixed share', prefixedShare], | ||
| ['JSON share', jsonShare], | ||
| ])('recovers a %s with the right passphrase', (_name, make) => { | ||
| expect(tryRecover(buildShare(make(), PASS), PASS)).toBe('ok'); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['raw 32 byte share', rawShare], | ||
| ['36 byte algo-prefixed share', prefixedShare], | ||
| ['JSON share', jsonShare], | ||
| ])('never returns key material for a wrong passphrase (%s)', (_name, make) => { | ||
| const share = buildShare(make(), PASS); | ||
| const accepted = wrongPassphrases(300).filter((p) => tryRecover(share, p) === 'ok'); | ||
| expect(accepted).toEqual([]); | ||
| }); | ||
|
|
||
| it('reports an unknown algorithm, not a passphrase error, when the passphrase is correct', () => { | ||
| const unknownAlgo = Buffer.concat([Buffer.from([2, 0, 0, 0]), randomBytes(32)]); | ||
| expect(() => recoverMobileKeyShare(signingKeys, buildShare(unknownAlgo, PASS), PASS)).toThrow(UnknownAlgorithmError); | ||
| }); | ||
|
|
||
| it('rejects data whose padding bytes are inconsistent', () => { | ||
| const key = pbkdf2Sync(Buffer.from(PASS, 'utf8'), USER_ID, 10000, 32, 'sha1'); | ||
| const cipher = createCipheriv('aes-256-cbc', key, Buffer.alloc(16)); | ||
| cipher.setAutoPadding(false); | ||
| // 48 bytes ending in 0x05 but with non-matching preceding bytes | ||
| const bad = Buffer.concat([randomBytes(43), Buffer.from([1, 2, 3, 4, 5])]); | ||
| const enc = Buffer.concat([cipher.update(bad), cipher.final()]); | ||
| expect(() => decryptMobilePrivateKey(PASS, USER_ID, enc)).toThrow(); | ||
| }); | ||
|
|
||
| it.each(['é', 'Pässwörd1!', '£€’pass1A', 'пароль-Aa1'])('recovers a share protected by the non-ASCII passphrase %s', (pass) => { | ||
| expect(tryRecover(buildShare(rawShare(), pass), pass)).toBe('ok'); | ||
| }); | ||
|
|
||
| it('still recovers a share wrapped with the legacy raw-char-code encoding', () => { | ||
| const pass = 'Pässwörd1!'; | ||
| const key = pbkdf2Sync(Buffer.from(pass, 'latin1'), USER_ID, 10000, 32, 'sha1'); | ||
| const cipher = createCipheriv('aes-256-cbc', key, Buffer.alloc(16)); | ||
| const enc = Buffer.concat([cipher.update(rawShare()), cipher.final()]).toString('hex'); | ||
| const share = JSON.stringify({ keyId: KEY_ID, userId: USER_ID, deviceId: DEVICE_ID, encryptedKey: enc }); | ||
| expect(tryRecover(share, pass)).toBe('ok'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.