-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) * Move `crypto/key_passphrase.ts` to `crypto-api/key-passphrase.ts` * Re-export `crypto-api/key-passphrase` into `crypto/key_passphrase.ts` * Add doc * Deprecate `MatrixClient.keyBackupKeyFromPassword` * Move `keyFromAuthData` to `common-crypto/key-passphrase.ts` * Fix faulty import * Keep `keyFromPassphrase` in old crypto * - Rename `deriveKey` into `deriveRecoveryKeyFromPassphrase` - Call `deriveRecoveryKeyFromPassphrase` into `RustCrypto.createRecoveryKeyFromPassphrase` instead of using `keyFromPassphrase` * Remove alternative in `keyBackupKeyFromPassword` deprecation. * Add tests for `keyFromAuthData` * Deprecate `keyFromAuthData` * Review changes
- Loading branch information
1 parent
53b599f
commit fab9cab
Showing
7 changed files
with
170 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2024 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { keyFromAuthData } from "../../../src/common-crypto/key-passphrase.ts"; | ||
|
||
describe("key-passphrase", () => { | ||
describe("keyFromAuthData", () => { | ||
it("should throw an error if salt or iterations are missing", async () => { | ||
// missing salt | ||
expect(() => keyFromAuthData({ private_key_iterations: 5 }, "passphrase")).toThrow( | ||
"Salt and/or iterations not found: this backup cannot be restored with a passphrase", | ||
); | ||
|
||
// missing iterations | ||
expect(() => keyFromAuthData({ private_key_salt: "salt" }, "passphrase")).toThrow( | ||
"Salt and/or iterations not found: this backup cannot be restored with a passphrase", | ||
); | ||
}); | ||
|
||
it("should derive key from auth data", async () => { | ||
const key = await keyFromAuthData({ private_key_salt: "salt", private_key_iterations: 5 }, "passphrase"); | ||
expect(key).toBeDefined(); | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
/* | ||
* Copyright 2024 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { deriveRecoveryKeyFromPassphrase } from "../crypto-api/index.ts"; | ||
|
||
/* eslint-disable camelcase */ | ||
interface IAuthData { | ||
private_key_salt?: string; | ||
private_key_iterations?: number; | ||
private_key_bits?: number; | ||
} | ||
|
||
/** | ||
* Derive a backup key from a passphrase using the salt and iterations from the auth data. | ||
* @param authData - The auth data containing the salt and iterations | ||
* @param passphrase - The passphrase to derive the key from | ||
* @deprecated Deriving a backup key from a passphrase is not part of the matrix spec. Instead, a random key is generated and stored/shared via 4S. | ||
*/ | ||
export function keyFromAuthData(authData: IAuthData, passphrase: string): Promise<Uint8Array> { | ||
if (!authData.private_key_salt || !authData.private_key_iterations) { | ||
throw new Error("Salt and/or iterations not found: " + "this backup cannot be restored with a passphrase"); | ||
} | ||
|
||
return deriveRecoveryKeyFromPassphrase( | ||
passphrase, | ||
authData.private_key_salt, | ||
authData.private_key_iterations, | ||
authData.private_key_bits, | ||
); | ||
} |
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,58 @@ | ||
/* | ||
* Copyright 2024 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const DEFAULT_BIT_SIZE = 256; | ||
|
||
/** | ||
* Derive a recovery key from a passphrase and salt using PBKDF2. | ||
* @see https://spec.matrix.org/v1.11/client-server-api/#deriving-keys-from-passphrases | ||
* | ||
* @param passphrase - The passphrase to derive the key from | ||
* @param salt - The salt to use in the derivation | ||
* @param iterations - The number of iterations to use in the derivation | ||
* @param numBits - The number of bits to derive | ||
*/ | ||
export async function deriveRecoveryKeyFromPassphrase( | ||
passphrase: string, | ||
salt: string, | ||
iterations: number, | ||
numBits = DEFAULT_BIT_SIZE, | ||
): Promise<Uint8Array> { | ||
if (!globalThis.crypto.subtle || !TextEncoder) { | ||
throw new Error("Password-based backup is not available on this platform"); | ||
} | ||
|
||
const key = await globalThis.crypto.subtle.importKey( | ||
"raw", | ||
new TextEncoder().encode(passphrase), | ||
{ name: "PBKDF2" }, | ||
false, | ||
["deriveBits"], | ||
); | ||
|
||
const keybits = await globalThis.crypto.subtle.deriveBits( | ||
{ | ||
name: "PBKDF2", | ||
salt: new TextEncoder().encode(salt), | ||
iterations: iterations, | ||
hash: "SHA-512", | ||
}, | ||
key, | ||
numBits, | ||
); | ||
|
||
return new Uint8Array(keybits); | ||
} |
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