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
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));
}