Element-R: pass pickleKey in as raw key for indexeddb encryption (#12543)
* Element-R: pass pickleKey in as raw key for indexeddb encryption Currently, we pass the `pickleKey` to the rust library for use as a passphrase for encrypting its crypto store. The Rust libary then passes that passphrase through 200000 rounds of PBKDF2 to generate an encryption key, which is (deliberately) slow. However, the pickleKey is actually 32 bytes of random data (base64-encoded). By passing the raw key into the rust library, we can therefore save the PBKDF operation. Backwards-compatibility with existing sessions is maintained, because if the rust library discovers that the store was previously encrypted with a key based on a PBKDF, it will re-base64 and PBKDF the key we provide, thus reconstructing the right key. * Update src/Lifecycle.ts Co-authored-by: Florian Duros <florianduros@element.io> * Lifecycle-test: clean up test setup Rely less on the unit under test for setting up the test preconditions -- not least because we don't really want to fire up matrix clients and the like during test setup. * Factor out "encryptPickleKey" method For a start it makes it easier to grok what's going on, but also I went to use this in a test * Improve tests for `Lifecycle.restoreFromLocalStorage` --------- Co-authored-by: Florian Duros <florianduros@element.io>
This commit is contained in:
parent
5004456d82
commit
0a01320fca
6 changed files with 233 additions and 64 deletions
|
@ -20,6 +20,20 @@ limitations under the License.
|
|||
import { encodeUnpaddedBase64 } from "matrix-js-sdk/src/matrix";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
/**
|
||||
* Encrypted format of a pickle key, as stored in IndexedDB.
|
||||
*/
|
||||
export interface EncryptedPickleKey {
|
||||
/** The encrypted payload. */
|
||||
encrypted?: BufferSource;
|
||||
|
||||
/** Initialisation vector for the encryption. */
|
||||
iv?: BufferSource;
|
||||
|
||||
/** The encryption key which was used to encrypt the payload. */
|
||||
cryptoKey?: CryptoKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the `additionalData` for the AES-GCM key used by the pickling processes. This
|
||||
* additional data is *not* encrypted, but *is* authenticated. The additional data is constructed
|
||||
|
@ -46,6 +60,32 @@ export function getPickleAdditionalData(userId: string, deviceId: string): Uint8
|
|||
return additionalData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt the given pickle key, ready for storage in the database.
|
||||
*
|
||||
* @param pickleKey - The key to be encrypted.
|
||||
* @param userId - The user ID the pickle key belongs to.
|
||||
* @param deviceId - The device ID the pickle key belongs to.
|
||||
*
|
||||
* @returns Data object ready for storing in indexeddb.
|
||||
*/
|
||||
export async function encryptPickleKey(
|
||||
pickleKey: Uint8Array,
|
||||
userId: string,
|
||||
deviceId: string,
|
||||
): Promise<EncryptedPickleKey | undefined> {
|
||||
if (!crypto?.subtle) {
|
||||
return undefined;
|
||||
}
|
||||
const cryptoKey = await crypto.subtle.generateKey({ name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]);
|
||||
const iv = new Uint8Array(32);
|
||||
crypto.getRandomValues(iv);
|
||||
|
||||
const additionalData = getPickleAdditionalData(userId, deviceId);
|
||||
const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData }, cryptoKey, pickleKey);
|
||||
return { encrypted, iv, cryptoKey };
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts the provided data into a pickle key and base64-encodes it ready for use elsewhere.
|
||||
*
|
||||
|
@ -59,7 +99,7 @@ export function getPickleAdditionalData(userId: string, deviceId: string): Uint8
|
|||
* @returns A promise that resolves to the encoded pickle key, or undefined if the key cannot be built and encoded.
|
||||
*/
|
||||
export async function buildAndEncodePickleKey(
|
||||
data: { encrypted?: BufferSource; iv?: BufferSource; cryptoKey?: CryptoKey } | undefined,
|
||||
data: EncryptedPickleKey | undefined,
|
||||
userId: string,
|
||||
deviceId: string,
|
||||
): Promise<string | undefined> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue