This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: deriveKey in webkit linux (workaround) (#313)
The subtlecrypto implementation in WebKit on Linux doesn't like to derive a key from an empty imported key. This works on all other browsers, so it seems like the WebKit implementation is doing the wrong thing. Maybe worth opening a bug and writing a test for them. In the mean time here's a workaround. This unblocks webkit testing in the interop tester (which runs on linux). This also lets folks use js-libp2p from webkit based linux browsers, although it doesn't seem like anyone else ran into this issue. --------- Co-authored-by: Alex Potsides <[email protected]>
- Loading branch information
1 parent
e7bb8b2
commit 4905944
Showing
6 changed files
with
117 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint max-nested-callbacks: ["error", 8] */ | ||
/* eslint-env mocha */ | ||
import { expect } from 'aegir/chai' | ||
|
||
import { importer } from '../../src/keys/importer.js' | ||
import { exporter } from '../../src/keys/exporter.js' | ||
|
||
describe('libp2p-crypto importer/exporter', function () { | ||
it('roundtrips', async () => { | ||
for (const password of ['', 'password']) { | ||
const secret = new Uint8Array(32) | ||
for (let i = 0; i < secret.length; i++) { | ||
secret[i] = i | ||
} | ||
|
||
const exported = await exporter(secret, password) | ||
const imported = await importer(exported, password) | ||
expect(imported).to.deep.equal(secret) | ||
} | ||
}) | ||
}) |
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,26 @@ | ||
|
||
/* eslint-env mocha */ | ||
import { isWebkitLinux, derivedEmptyPasswordKey } from '../src/ciphers/aes-gcm.browser.js' | ||
import { expect } from 'aegir/chai' | ||
|
||
describe('Constant derived key is generated correctly', () => { | ||
it('Generates correctly', async () => { | ||
if (isWebkitLinux() || typeof crypto === 'undefined') { | ||
// WebKit Linux can't generate this. Hence the workaround. | ||
return | ||
} | ||
|
||
const generatedKey = await crypto.subtle.exportKey('jwk', | ||
await crypto.subtle.deriveKey( | ||
{ name: 'PBKDF2', salt: new Uint8Array(16), iterations: 32767, hash: { name: 'SHA-256' } }, | ||
await crypto.subtle.importKey('raw', new Uint8Array(0), { name: 'PBKDF2' }, false, ['deriveKey']), | ||
{ name: 'AES-GCM', length: 128 }, true, ['encrypt', 'decrypt']) | ||
) | ||
|
||
// Webkit macos flips these. Sort them so they match. | ||
derivedEmptyPasswordKey.key_ops.sort() | ||
generatedKey?.key_ops?.sort() | ||
|
||
expect(generatedKey).to.eql(derivedEmptyPasswordKey) | ||
}) | ||
}) |