Skip to content

Commit da04482

Browse files
authored
Clean AES export and move back calculateKeyCheck to secret-storage.ts (#4440)
1 parent 9f40f32 commit da04482

File tree

6 files changed

+22
-44
lines changed

6 files changed

+22
-44
lines changed

spec/unit/secret-storage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Mocked } from "jest-mock";
1818

1919
import {
2020
AccountDataClient,
21+
calculateKeyCheck,
2122
PassphraseInfo,
2223
SecretStorageCallbacks,
2324
SecretStorageKeyDescriptionAesV1,
@@ -26,7 +27,6 @@ import {
2627
trimTrailingEquals,
2728
} from "../../src/secret-storage";
2829
import { randomString } from "../../src/randomstring";
29-
import { calculateKeyCheck } from "../../src/calculateKeyCheck.ts";
3030

3131
describe("ServerSideSecretStorageImpl", function () {
3232
describe(".addKey", function () {

src/calculateKeyCheck.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/crypto/aes.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ limitations under the License.
1616

1717
import encryptAESSecretStorageItem from "../utils/encryptAESSecretStorageItem.ts";
1818
import decryptAESSecretStorageItem from "../utils/decryptAESSecretStorageItem.ts";
19-
import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
2019

2120
// Export for backwards compatibility
22-
export type { AESEncryptedSecretStoragePayload as IEncryptedPayload };
23-
// Export with new names instead of using `as` to not break react-sdk tests
24-
export const encryptAES = encryptAESSecretStorageItem;
25-
export const decryptAES = decryptAESSecretStorageItem;
26-
export { calculateKeyCheck } from "../calculateKeyCheck.ts";
21+
export type { AESEncryptedSecretStoragePayload as IEncryptedPayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
22+
export { encryptAESSecretStorageItem as encryptAES, decryptAESSecretStorageItem as decryptAES };
23+
export { calculateKeyCheck } from "../secret-storage.ts";

src/crypto/backup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { encodeRecoveryKey } from "../crypto-api/index.ts";
4343
import decryptAESSecretStorageItem from "../utils/decryptAESSecretStorageItem.ts";
4444
import encryptAESSecretStorageItem from "../utils/encryptAESSecretStorageItem.ts";
4545
import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
46-
import { calculateKeyCheck } from "../calculateKeyCheck.ts";
46+
import { calculateKeyCheck } from "../secret-storage.ts";
4747

4848
const KEY_BACKUP_KEYS_PER_REQUEST = 200;
4949
const KEY_BACKUP_CHECK_RATE_LIMIT = 5000; // ms

src/crypto/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import { MapWithDefault, recursiveMapToObject } from "../utils.ts";
7575
import {
7676
AccountDataClient,
7777
AddSecretStorageKeyOpts,
78+
calculateKeyCheck,
7879
SECRET_STORAGE_ALGORITHM_V1_AES,
7980
SecretStorageKeyDescription,
8081
SecretStorageKeyObject,
@@ -109,7 +110,6 @@ import { KnownMembership } from "../@types/membership.ts";
109110
import decryptAESSecretStorageItem from "../utils/decryptAESSecretStorageItem.ts";
110111
import encryptAESSecretStorageItem from "../utils/encryptAESSecretStorageItem.ts";
111112
import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
112-
import { calculateKeyCheck } from "../calculateKeyCheck.ts";
113113

114114
/* re-exports for backwards compatibility */
115115
export type {

src/secret-storage.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { logger } from "./logger.ts";
2828
import encryptAESSecretStorageItem from "./utils/encryptAESSecretStorageItem.ts";
2929
import decryptAESSecretStorageItem from "./utils/decryptAESSecretStorageItem.ts";
3030
import { AESEncryptedSecretStoragePayload } from "./@types/AESEncryptedSecretStoragePayload.ts";
31-
import { calculateKeyCheck } from "./crypto/aes.ts";
3231

3332
export const SECRET_STORAGE_ALGORITHM_V1_AES = "m.secret_storage.v1.aes-hmac-sha2";
3433

@@ -676,3 +675,19 @@ export function trimTrailingEquals(input: string): string {
676675
return input;
677676
}
678677
}
678+
679+
// string of zeroes, for calculating the key check
680+
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";
681+
682+
/**
683+
* Calculate the MAC for checking the key.
684+
* See https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2, steps 3 and 4.
685+
*
686+
* @param key - the key to use
687+
* @param iv - The initialization vector as a base64-encoded string.
688+
* If omitted, a random initialization vector will be created.
689+
* @returns An object that contains, `mac` and `iv` properties.
690+
*/
691+
export function calculateKeyCheck(key: Uint8Array, iv?: string): Promise<AESEncryptedSecretStoragePayload> {
692+
return encryptAESSecretStorageItem(ZERO_STR, key, "", iv);
693+
}

0 commit comments

Comments
 (0)