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
Changes from 1 commit
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
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),
};
}