Skip to content
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

Move out crypto/aes #4431

Merged
merged 19 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
eae6bb4
Move `SecretEncryptedPayload` in `src/utils/@types`
florianduros Sep 25, 2024
91327a4
Move `encryptAES` to a dedicated file. Moved in a utils folder.
florianduros Sep 25, 2024
9d7a074
Move `deriveKeys` to a dedicated file in order to share it
florianduros Sep 25, 2024
854f38b
Move `decryptAES` to a dedicated file. Moved in a utils folder.
florianduros Sep 25, 2024
3216a7d
Move `calculateKeyCheck` to a dedicated file. Moved in a utils folder.
florianduros Sep 25, 2024
48ceb73
Remove AES functions in `aes.ts` and export new ones for backward com…
florianduros Sep 25, 2024
ca95c3d
Update import to use new functions
florianduros Sep 25, 2024
83bcbb1
Add `src/utils` entrypoint in `README.md`
florianduros Sep 25, 2024
f29304d
Merge branch 'refs/heads/develop' into florianduros/rip-out-legacy-cr…
florianduros Sep 27, 2024
073af16
- Rename `SecretEncryptedPayload` to `AESEncryptedSecretStoragePayload`.
florianduros Sep 27, 2024
e06c0b7
Move `calculateKeyCheck` into `secret-storage.ts`.
florianduros Sep 27, 2024
6e7b7ba
Move `deriveKeys` into `src/utils/internal` folder.
florianduros Sep 27, 2024
8b1dd09
- Rename `encryptAES` on `encryptAESSecretStorageItem`
florianduros Sep 27, 2024
c99cf95
- Rename `decryptAES` on `decryptAESSecretStorageItem`
florianduros Sep 27, 2024
aeafe25
Update documentation
florianduros Sep 27, 2024
28f60be
Update `decryptAESSecretStorageItem` doc
florianduros Sep 27, 2024
6fe09cf
Add lnk to spec for `calculateKeyCheck`
florianduros Sep 30, 2024
442366c
Merge branch 'develop' into florianduros/rip-out-legacy-crypto/aes
florianduros Oct 1, 2024
25a34c1
Fix downstream tests
florianduros Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 4 additions & 150 deletions src/crypto/aes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,153 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { decodeBase64, encodeBase64 } from "../base64.ts";

// salt for HKDF, with 8 bytes of zeros
const zeroSalt = new Uint8Array(8);

export interface IEncryptedPayload {
[key: string]: any; // extensible
/** the initialization vector in base64 */
iv: string;
/** the ciphertext in base64 */
ciphertext: string;
/** the HMAC in base64 */
mac: string;
}

/**
* Encrypt a string using AES-CTR.
*
* @param data - the plaintext to encrypt
* @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key for
* encryption. Obviously, the same key must be provided when decrypting.
* @param name - the name of the secret. Used as an input to the HKDF operation which is used to derive the AES key,
* so again the same value must be provided when decrypting.
* @param ivStr - the base64-encoded initialization vector to use. If not supplied, a random one will be generated.
*
* @returns The encrypted result, including the ciphertext itself, the initialization vector (as supplied in `ivStr`,
* or generated), and an HMAC on the ciphertext — all base64-encoded.
*/
export async function encryptAES(
data: string,
key: Uint8Array,
name: string,
ivStr?: string,
): Promise<IEncryptedPayload> {
let iv: Uint8Array;
if (ivStr) {
iv = decodeBase64(ivStr);
} else {
iv = new Uint8Array(16);
globalThis.crypto.getRandomValues(iv);

// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[8] &= 0x7f;
}

const [aesKey, hmacKey] = await deriveKeys(key, name);
const encodedData = new TextEncoder().encode(data);

const ciphertext = await globalThis.crypto.subtle.encrypt(
{
name: "AES-CTR",
counter: iv,
length: 64,
},
aesKey,
encodedData,
);

const hmac = await globalThis.crypto.subtle.sign({ name: "HMAC" }, hmacKey, ciphertext);

return {
iv: encodeBase64(iv),
ciphertext: encodeBase64(ciphertext),
mac: encodeBase64(hmac),
};
}

/**
* Decrypt an AES-encrypted string.
*
* @param data - the encrypted data, returned by {@link encryptAES}.
* @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key. Must
* be the same as provided to {@link encryptAES}.
* @param name - the name of the secret. Also used as an input to the HKDF operation which is used to derive the AES
* key, so again must be the same as provided to {@link encryptAES}.
*/
export async function decryptAES(data: IEncryptedPayload, key: Uint8Array, name: string): Promise<string> {
const [aesKey, hmacKey] = await deriveKeys(key, name);

const ciphertext = decodeBase64(data.ciphertext);

if (!(await globalThis.crypto.subtle.verify({ name: "HMAC" }, hmacKey, decodeBase64(data.mac), ciphertext))) {
throw new Error(`Error decrypting secret ${name}: bad MAC`);
}

const plaintext = await globalThis.crypto.subtle.decrypt(
{
name: "AES-CTR",
counter: decodeBase64(data.iv),
length: 64,
},
aesKey,
ciphertext,
);

return new TextDecoder().decode(new Uint8Array(plaintext));
}

async function deriveKeys(key: Uint8Array, name: string): Promise<[CryptoKey, CryptoKey]> {
const hkdfkey = await globalThis.crypto.subtle.importKey("raw", key, { name: "HKDF" }, false, ["deriveBits"]);
const keybits = await globalThis.crypto.subtle.deriveBits(
{
name: "HKDF",
salt: zeroSalt,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/879
info: new TextEncoder().encode(name),
hash: "SHA-256",
},
hkdfkey,
512,
);

const aesKey = keybits.slice(0, 32);
const hmacKey = keybits.slice(32);

const aesProm = globalThis.crypto.subtle.importKey("raw", aesKey, { name: "AES-CTR" }, false, [
"encrypt",
"decrypt",
]);

const hmacProm = globalThis.crypto.subtle.importKey(
"raw",
hmacKey,
{
name: "HMAC",
hash: { name: "SHA-256" },
},
false,
["sign", "verify"],
);

return Promise.all([aesProm, hmacProm]);
}

// string of zeroes, for calculating the key check
const ZERO_STR = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

/** Calculate the MAC for checking the key.
*
* @param key - the key to use
* @param iv - The initialization vector as a base64-encoded string.
* If omitted, a random initialization vector will be created.
* @returns An object that contains, `mac` and `iv` properties.
*/
export function calculateKeyCheck(key: Uint8Array, iv?: string): Promise<IEncryptedPayload> {
return encryptAES(ZERO_STR, key, "", iv);
}
// Export for backwards compatibility
export type { SecretEncryptedPayload as IEncryptedPayload } from "../utils/@types/SecretEncryptedPayload.ts";
export { encryptAES } from "../utils/encryptAES.ts";
export { decryptAES } from "../utils/decryptAES.ts";
60 changes: 60 additions & 0 deletions src/rust-crypto/deriveKeys.ts
florianduros marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
*/

// salt for HKDF, with 8 bytes of zeros
const zeroSalt = new Uint8Array(8);

/**
* Derive AES and HMAC keys from a master key.
* @param key
* @param name
*/
florianduros marked this conversation as resolved.
Show resolved Hide resolved
export async function deriveKeys(key: Uint8Array, name: string): Promise<[CryptoKey, CryptoKey]> {
const hkdfkey = await globalThis.crypto.subtle.importKey("raw", key, { name: "HKDF" }, false, ["deriveBits"]);
const keybits = await globalThis.crypto.subtle.deriveBits(
{
name: "HKDF",
salt: zeroSalt,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/879
info: new TextEncoder().encode(name),
hash: "SHA-256",
},
hkdfkey,
512,
);

const aesKey = keybits.slice(0, 32);
const hmacKey = keybits.slice(32);

const aesProm = globalThis.crypto.subtle.importKey("raw", aesKey, { name: "AES-CTR" }, false, [
"encrypt",
"decrypt",
]);

const hmacProm = globalThis.crypto.subtle.importKey(
"raw",
hmacKey,
{
name: "HMAC",
hash: { name: "SHA-256" },
},
false,
["sign", "verify"],
);

return Promise.all([aesProm, hmacProm]);
}
32 changes: 32 additions & 0 deletions src/utils/calculateKeyCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 { encryptAES } from "./encryptAES.ts";
import { SecretEncryptedPayload } from "./@types/SecretEncryptedPayload.ts";

// string of zeroes, for calculating the key check
const ZERO_STR = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

/** Calculate the MAC for checking the key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Calculate the MAC for checking the key.
/**
* Calculate the MAC for checking a secret storage key.
*
* See https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2, steps 3 and 4.

*
* @param key - the key to use
* @param iv - The initialization vector as a base64-encoded string.
* If omitted, a random initialization vector will be created.
* @returns An object that contains, `mac` and `iv` properties.
*/
export function calculateKeyCheck(key: Uint8Array, iv?: string): Promise<SecretEncryptedPayload> {
return encryptAES(ZERO_STR, key, "", iv);
}
florianduros marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions src/utils/decryptAES.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 { decodeBase64 } from "../base64.ts";
import { deriveKeys } from "../rust-crypto/deriveKeys.ts";
import { SecretEncryptedPayload } from "./@types/SecretEncryptedPayload.ts";

/**
* Decrypt an AES-encrypted string.
florianduros marked this conversation as resolved.
Show resolved Hide resolved
*
* @param data - the encrypted data, returned by {@link encryptAES}.
florianduros marked this conversation as resolved.
Show resolved Hide resolved
* @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key. Must
* be the same as provided to {@link encryptAES}.
* @param name - the name of the secret. Also used as an input to the HKDF operation which is used to derive the AES
* key, so again must be the same as provided to {@link encryptAES}.
*/
export async function decryptAES(data: SecretEncryptedPayload, key: Uint8Array, name: string): Promise<string> {
florianduros marked this conversation as resolved.
Show resolved Hide resolved
const [aesKey, hmacKey] = await deriveKeys(key, name);

const ciphertext = decodeBase64(data.ciphertext);

if (!(await globalThis.crypto.subtle.verify({ name: "HMAC" }, hmacKey, decodeBase64(data.mac), ciphertext))) {
throw new Error(`Error decrypting secret ${name}: bad MAC`);
}

const plaintext = await globalThis.crypto.subtle.decrypt(
{
name: "AES-CTR",
counter: decodeBase64(data.iv),
length: 64,
},
aesKey,
ciphertext,
);

return new TextDecoder().decode(new Uint8Array(plaintext));
}
73 changes: 73 additions & 0 deletions src/utils/encryptAES.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 { decodeBase64, encodeBase64 } from "../base64.ts";
import { deriveKeys } from "../rust-crypto/deriveKeys.ts";
import { SecretEncryptedPayload } from "./@types/SecretEncryptedPayload.ts";

/**
* Encrypt a string using AES-CTR.
florianduros marked this conversation as resolved.
Show resolved Hide resolved
*
* @param data - the plaintext to encrypt
* @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key for
* encryption. Obviously, the same key must be provided when decrypting.
* @param name - the name of the secret. Used as an input to the HKDF operation which is used to derive the AES key,
* so again the same value must be provided when decrypting.
* @param ivStr - the base64-encoded initialization vector to use. If not supplied, a random one will be generated.
*
* @returns The encrypted result, including the ciphertext itself, the initialization vector (as supplied in `ivStr`,
* or generated), and an HMAC on the ciphertext — all base64-encoded.
*/
export async function encryptAES(
florianduros marked this conversation as resolved.
Show resolved Hide resolved
florianduros marked this conversation as resolved.
Show resolved Hide resolved
data: string,
key: Uint8Array,
name: string,
ivStr?: string,
): Promise<SecretEncryptedPayload> {
let iv: Uint8Array;
if (ivStr) {
iv = decodeBase64(ivStr);
} else {
iv = new Uint8Array(16);
globalThis.crypto.getRandomValues(iv);

// clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
// (which would mean we wouldn't be able to decrypt on Android). The loss
// of a single bit of iv is a price we have to pay.
iv[8] &= 0x7f;
}

const [aesKey, hmacKey] = await deriveKeys(key, name);
const encodedData = new TextEncoder().encode(data);

const ciphertext = await globalThis.crypto.subtle.encrypt(
{
name: "AES-CTR",
counter: iv,
length: 64,
},
aesKey,
encodedData,
);

const hmac = await globalThis.crypto.subtle.sign({ name: "HMAC" }, hmacKey, ciphertext);

return {
iv: encodeBase64(iv),
ciphertext: encodeBase64(ciphertext),
mac: encodeBase64(hmac),
};
}