-
-
Notifications
You must be signed in to change notification settings - Fork 619
Move out crypto/aes
#4431
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
Merged
Merged
Move out crypto/aes
#4431
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 91327a4
Move `encryptAES` to a dedicated file. Moved in a utils folder.
florianduros 9d7a074
Move `deriveKeys` to a dedicated file in order to share it
florianduros 854f38b
Move `decryptAES` to a dedicated file. Moved in a utils folder.
florianduros 3216a7d
Move `calculateKeyCheck` to a dedicated file. Moved in a utils folder.
florianduros 48ceb73
Remove AES functions in `aes.ts` and export new ones for backward com…
florianduros ca95c3d
Update import to use new functions
florianduros 83bcbb1
Add `src/utils` entrypoint in `README.md`
florianduros f29304d
Merge branch 'refs/heads/develop' into florianduros/rip-out-legacy-cr…
florianduros 073af16
- Rename `SecretEncryptedPayload` to `AESEncryptedSecretStoragePayload`.
florianduros e06c0b7
Move `calculateKeyCheck` into `secret-storage.ts`.
florianduros 6e7b7ba
Move `deriveKeys` into `src/utils/internal` folder.
florianduros 8b1dd09
- Rename `encryptAES` on `encryptAESSecretStorageItem`
florianduros c99cf95
- Rename `decryptAES` on `decryptAESSecretStorageItem`
florianduros aeafe25
Update documentation
florianduros 28f60be
Update `decryptAESSecretStorageItem` doc
florianduros 6fe09cf
Add lnk to spec for `calculateKeyCheck`
florianduros 442366c
Merge branch 'develop' into florianduros/rip-out-legacy-crypto/aes
florianduros 25a34c1
Fix downstream tests
florianduros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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. | ||
* | ||
* @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), | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.