@unwanted/matrix-sdk-mini 34.12.0-3 → 34.12.0-4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. package/git-revision.txt +1 -1
  2. package/lib/client.d.ts +0 -45
  3. package/lib/client.d.ts.map +1 -1
  4. package/lib/client.js +0 -119
  5. package/lib/client.js.map +1 -1
  6. package/lib/types.d.ts +0 -1
  7. package/lib/types.d.ts.map +1 -1
  8. package/lib/types.js.map +1 -1
  9. package/package.json +1 -1
  10. package/src/client.ts +10 -157
  11. package/src/types.ts +0 -1
  12. package/CHANGELOG.md +0 -5910
  13. package/lib/@types/AESEncryptedSecretStoragePayload.d.ts +0 -14
  14. package/lib/@types/AESEncryptedSecretStoragePayload.d.ts.map +0 -1
  15. package/lib/@types/AESEncryptedSecretStoragePayload.js +0 -1
  16. package/lib/@types/AESEncryptedSecretStoragePayload.js.map +0 -1
  17. package/lib/utils/decryptAESSecretStorageItem.d.ts +0 -12
  18. package/lib/utils/decryptAESSecretStorageItem.d.ts.map +0 -1
  19. package/lib/utils/decryptAESSecretStorageItem.js +0 -50
  20. package/lib/utils/decryptAESSecretStorageItem.js.map +0 -1
  21. package/lib/utils/encryptAESSecretStorageItem.d.ts +0 -16
  22. package/lib/utils/encryptAESSecretStorageItem.d.ts.map +0 -1
  23. package/lib/utils/encryptAESSecretStorageItem.js +0 -68
  24. package/lib/utils/encryptAESSecretStorageItem.js.map +0 -1
  25. package/lib/utils/internal/deriveKeys.d.ts +0 -10
  26. package/lib/utils/internal/deriveKeys.d.ts.map +0 -1
  27. package/lib/utils/internal/deriveKeys.js +0 -60
  28. package/lib/utils/internal/deriveKeys.js.map +0 -1
  29. package/src/@types/AESEncryptedSecretStoragePayload.ts +0 -29
  30. package/src/utils/decryptAESSecretStorageItem.ts +0 -54
  31. package/src/utils/encryptAESSecretStorageItem.ts +0 -73
  32. package/src/utils/internal/deriveKeys.ts +0 -63
@@ -1,14 +0,0 @@
1
- /**
2
- * An AES-encrypted secret storage payload.
3
- * See https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2-1
4
- */
5
- export interface AESEncryptedSecretStoragePayload {
6
- [key: string]: any;
7
- /** the initialization vector in base64 */
8
- iv: string;
9
- /** the ciphertext in base64 */
10
- ciphertext: string;
11
- /** the HMAC in base64 */
12
- mac: string;
13
- }
14
- //# sourceMappingURL=AESEncryptedSecretStoragePayload.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AESEncryptedSecretStoragePayload.d.ts","sourceRoot":"","sources":["../../src/@types/AESEncryptedSecretStoragePayload.ts"],"names":[],"mappings":"AAgBA;;;GAGG;AACH,MAAM,WAAW,gCAAgC;IAC7C,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IACnB,0CAA0C;IAC1C,EAAE,EAAE,MAAM,CAAC;IACX,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,yBAAyB;IACzB,GAAG,EAAE,MAAM,CAAC;CACf"}
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- {"version":3,"file":"AESEncryptedSecretStoragePayload.js","names":[],"sources":["../../src/@types/AESEncryptedSecretStoragePayload.ts"],"sourcesContent":["/*\n * Copyright 2024 The Matrix.org Foundation C.I.C.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * An AES-encrypted secret storage payload.\n * See https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2-1\n */\nexport interface AESEncryptedSecretStoragePayload {\n [key: string]: any; // extensible\n /** the initialization vector in base64 */\n iv: string;\n /** the ciphertext in base64 */\n ciphertext: string;\n /** the HMAC in base64 */\n mac: string;\n}\n"],"mappings":"","ignoreList":[]}
@@ -1,12 +0,0 @@
1
- import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
2
- /**
3
- * Decrypt an AES-encrypted Secret Storage item.
4
- *
5
- * @param data - the encrypted data, returned by {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
6
- * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key. Must
7
- * be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
8
- * @param name - the name of the secret. Also used as an input to the HKDF operation which is used to derive the AES
9
- * key, so again must be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
10
- */
11
- export default function decryptAESSecretStorageItem(data: AESEncryptedSecretStoragePayload, key: Uint8Array, name: string): Promise<string>;
12
- //# sourceMappingURL=decryptAESSecretStorageItem.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"decryptAESSecretStorageItem.d.ts","sourceRoot":"","sources":["../../src/utils/decryptAESSecretStorageItem.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,gCAAgC,EAAE,MAAM,+CAA+C,CAAC;AAEjG;;;;;;;;GAQG;AACH,wBAA8B,2BAA2B,CACrD,IAAI,EAAE,gCAAgC,EACtC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAoBjB"}
@@ -1,50 +0,0 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- /*
3
- * Copyright 2024 The Matrix.org Foundation C.I.C.
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
-
18
- import { decodeBase64 } from "../base64.js";
19
- import { deriveKeys } from "./internal/deriveKeys.js";
20
- /**
21
- * Decrypt an AES-encrypted Secret Storage item.
22
- *
23
- * @param data - the encrypted data, returned by {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
24
- * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key. Must
25
- * be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
26
- * @param name - the name of the secret. Also used as an input to the HKDF operation which is used to derive the AES
27
- * key, so again must be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
28
- */
29
- export default function decryptAESSecretStorageItem(_x, _x2, _x3) {
30
- return _decryptAESSecretStorageItem.apply(this, arguments);
31
- }
32
- function _decryptAESSecretStorageItem() {
33
- _decryptAESSecretStorageItem = _asyncToGenerator(function* (data, key, name) {
34
- var [aesKey, hmacKey] = yield deriveKeys(key, name);
35
- var ciphertext = decodeBase64(data.ciphertext);
36
- if (!(yield globalThis.crypto.subtle.verify({
37
- name: "HMAC"
38
- }, hmacKey, decodeBase64(data.mac), ciphertext))) {
39
- throw new Error("Error decrypting secret ".concat(name, ": bad MAC"));
40
- }
41
- var plaintext = yield globalThis.crypto.subtle.decrypt({
42
- name: "AES-CTR",
43
- counter: decodeBase64(data.iv),
44
- length: 64
45
- }, aesKey, ciphertext);
46
- return new TextDecoder().decode(new Uint8Array(plaintext));
47
- });
48
- return _decryptAESSecretStorageItem.apply(this, arguments);
49
- }
50
- //# sourceMappingURL=decryptAESSecretStorageItem.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"decryptAESSecretStorageItem.js","names":["decodeBase64","deriveKeys","decryptAESSecretStorageItem","_x","_x2","_x3","_decryptAESSecretStorageItem","apply","arguments","_asyncToGenerator","data","key","name","aesKey","hmacKey","ciphertext","globalThis","crypto","subtle","verify","mac","Error","concat","plaintext","decrypt","counter","iv","length","TextDecoder","decode","Uint8Array"],"sources":["../../src/utils/decryptAESSecretStorageItem.ts"],"sourcesContent":["/*\n * Copyright 2024 The Matrix.org Foundation C.I.C.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { decodeBase64 } from \"../base64.ts\";\nimport { deriveKeys } from \"./internal/deriveKeys.ts\";\nimport { AESEncryptedSecretStoragePayload } from \"../@types/AESEncryptedSecretStoragePayload.ts\";\n\n/**\n * Decrypt an AES-encrypted Secret Storage item.\n *\n * @param data - the encrypted data, returned by {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.\n * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key. Must\n * be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.\n * @param name - the name of the secret. Also used as an input to the HKDF operation which is used to derive the AES\n * key, so again must be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.\n */\nexport default async function decryptAESSecretStorageItem(\n data: AESEncryptedSecretStoragePayload,\n key: Uint8Array,\n name: string,\n): Promise<string> {\n const [aesKey, hmacKey] = await deriveKeys(key, name);\n\n const ciphertext = decodeBase64(data.ciphertext);\n\n if (!(await globalThis.crypto.subtle.verify({ name: \"HMAC\" }, hmacKey, decodeBase64(data.mac), ciphertext))) {\n throw new Error(`Error decrypting secret ${name}: bad MAC`);\n }\n\n const plaintext = await globalThis.crypto.subtle.decrypt(\n {\n name: \"AES-CTR\",\n counter: decodeBase64(data.iv),\n length: 64,\n },\n aesKey,\n ciphertext,\n );\n\n return new TextDecoder().decode(new Uint8Array(plaintext));\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,YAAY,QAAQ,cAAc;AAC3C,SAASC,UAAU,QAAQ,0BAA0B;AAGrD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,wBAA8BC,2BAA2BA,CAAAC,EAAA,EAAAC,GAAA,EAAAC,GAAA;EAAA,OAAAC,4BAAA,CAAAC,KAAA,OAAAC,SAAA;AAAA;AAwBxD,SAAAF,6BAAA;EAAAA,4BAAA,GAAAG,iBAAA,CAxBc,WACXC,IAAsC,EACtCC,GAAe,EACfC,IAAY,EACG;IACf,IAAM,CAACC,MAAM,EAAEC,OAAO,CAAC,SAASb,UAAU,CAACU,GAAG,EAAEC,IAAI,CAAC;IAErD,IAAMG,UAAU,GAAGf,YAAY,CAACU,IAAI,CAACK,UAAU,CAAC;IAEhD,IAAI,QAAQC,UAAU,CAACC,MAAM,CAACC,MAAM,CAACC,MAAM,CAAC;MAAEP,IAAI,EAAE;IAAO,CAAC,EAAEE,OAAO,EAAEd,YAAY,CAACU,IAAI,CAACU,GAAG,CAAC,EAAEL,UAAU,CAAC,CAAC,EAAE;MACzG,MAAM,IAAIM,KAAK,4BAAAC,MAAA,CAA4BV,IAAI,cAAW,CAAC;IAC/D;IAEA,IAAMW,SAAS,SAASP,UAAU,CAACC,MAAM,CAACC,MAAM,CAACM,OAAO,CACpD;MACIZ,IAAI,EAAE,SAAS;MACfa,OAAO,EAAEzB,YAAY,CAACU,IAAI,CAACgB,EAAE,CAAC;MAC9BC,MAAM,EAAE;IACZ,CAAC,EACDd,MAAM,EACNE,UACJ,CAAC;IAED,OAAO,IAAIa,WAAW,CAAC,CAAC,CAACC,MAAM,CAAC,IAAIC,UAAU,CAACP,SAAS,CAAC,CAAC;EAC9D,CAAC;EAAA,OAAAjB,4BAAA,CAAAC,KAAA,OAAAC,SAAA;AAAA","ignoreList":[]}
@@ -1,16 +0,0 @@
1
- import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
2
- /**
3
- * Encrypt a string as a secret storage item, using AES-CTR.
4
- *
5
- * @param data - the plaintext to encrypt
6
- * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key for
7
- * encryption. Obviously, the same key must be provided when decrypting.
8
- * @param name - the name of the secret. Used as an input to the HKDF operation which is used to derive the AES key,
9
- * so again the same value must be provided when decrypting.
10
- * @param ivStr - the base64-encoded initialization vector to use. If not supplied, a random one will be generated.
11
- *
12
- * @returns The encrypted result, including the ciphertext itself, the initialization vector (as supplied in `ivStr`,
13
- * or generated), and an HMAC on the ciphertext — all base64-encoded.
14
- */
15
- export default function encryptAESSecretStorageItem(data: string, key: Uint8Array, name: string, ivStr?: string): Promise<AESEncryptedSecretStoragePayload>;
16
- //# sourceMappingURL=encryptAESSecretStorageItem.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"encryptAESSecretStorageItem.d.ts","sourceRoot":"","sources":["../../src/utils/encryptAESSecretStorageItem.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,gCAAgC,EAAE,MAAM,+CAA+C,CAAC;AAEjG;;;;;;;;;;;;GAYG;AACH,wBAA8B,2BAA2B,CACrD,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,MAAM,EACZ,KAAK,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,gCAAgC,CAAC,CAkC3C"}
@@ -1,68 +0,0 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- /*
3
- * Copyright 2024 The Matrix.org Foundation C.I.C.
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
-
18
- import { decodeBase64, encodeBase64 } from "../base64.js";
19
- import { deriveKeys } from "./internal/deriveKeys.js";
20
- /**
21
- * Encrypt a string as a secret storage item, using AES-CTR.
22
- *
23
- * @param data - the plaintext to encrypt
24
- * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key for
25
- * encryption. Obviously, the same key must be provided when decrypting.
26
- * @param name - the name of the secret. Used as an input to the HKDF operation which is used to derive the AES key,
27
- * so again the same value must be provided when decrypting.
28
- * @param ivStr - the base64-encoded initialization vector to use. If not supplied, a random one will be generated.
29
- *
30
- * @returns The encrypted result, including the ciphertext itself, the initialization vector (as supplied in `ivStr`,
31
- * or generated), and an HMAC on the ciphertext — all base64-encoded.
32
- */
33
- export default function encryptAESSecretStorageItem(_x, _x2, _x3, _x4) {
34
- return _encryptAESSecretStorageItem.apply(this, arguments);
35
- }
36
- function _encryptAESSecretStorageItem() {
37
- _encryptAESSecretStorageItem = _asyncToGenerator(function* (data, key, name, ivStr) {
38
- var iv;
39
- if (ivStr) {
40
- iv = decodeBase64(ivStr);
41
- } else {
42
- iv = new Uint8Array(16);
43
- globalThis.crypto.getRandomValues(iv);
44
-
45
- // clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
46
- // (which would mean we wouldn't be able to decrypt on Android). The loss
47
- // of a single bit of iv is a price we have to pay.
48
- iv[8] &= 0x7f;
49
- }
50
- var [aesKey, hmacKey] = yield deriveKeys(key, name);
51
- var encodedData = new TextEncoder().encode(data);
52
- var ciphertext = yield globalThis.crypto.subtle.encrypt({
53
- name: "AES-CTR",
54
- counter: iv,
55
- length: 64
56
- }, aesKey, encodedData);
57
- var hmac = yield globalThis.crypto.subtle.sign({
58
- name: "HMAC"
59
- }, hmacKey, ciphertext);
60
- return {
61
- iv: encodeBase64(iv),
62
- ciphertext: encodeBase64(ciphertext),
63
- mac: encodeBase64(hmac)
64
- };
65
- });
66
- return _encryptAESSecretStorageItem.apply(this, arguments);
67
- }
68
- //# sourceMappingURL=encryptAESSecretStorageItem.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"encryptAESSecretStorageItem.js","names":["decodeBase64","encodeBase64","deriveKeys","encryptAESSecretStorageItem","_x","_x2","_x3","_x4","_encryptAESSecretStorageItem","apply","arguments","_asyncToGenerator","data","key","name","ivStr","iv","Uint8Array","globalThis","crypto","getRandomValues","aesKey","hmacKey","encodedData","TextEncoder","encode","ciphertext","subtle","encrypt","counter","length","hmac","sign","mac"],"sources":["../../src/utils/encryptAESSecretStorageItem.ts"],"sourcesContent":["/*\n * Copyright 2024 The Matrix.org Foundation C.I.C.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { decodeBase64, encodeBase64 } from \"../base64.ts\";\nimport { deriveKeys } from \"./internal/deriveKeys.ts\";\nimport { AESEncryptedSecretStoragePayload } from \"../@types/AESEncryptedSecretStoragePayload.ts\";\n\n/**\n * Encrypt a string as a secret storage item, using AES-CTR.\n *\n * @param data - the plaintext to encrypt\n * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key for\n * encryption. Obviously, the same key must be provided when decrypting.\n * @param name - the name of the secret. Used as an input to the HKDF operation which is used to derive the AES key,\n * so again the same value must be provided when decrypting.\n * @param ivStr - the base64-encoded initialization vector to use. If not supplied, a random one will be generated.\n *\n * @returns The encrypted result, including the ciphertext itself, the initialization vector (as supplied in `ivStr`,\n * or generated), and an HMAC on the ciphertext — all base64-encoded.\n */\nexport default async function encryptAESSecretStorageItem(\n data: string,\n key: Uint8Array,\n name: string,\n ivStr?: string,\n): Promise<AESEncryptedSecretStoragePayload> {\n let iv: Uint8Array;\n if (ivStr) {\n iv = decodeBase64(ivStr);\n } else {\n iv = new Uint8Array(16);\n globalThis.crypto.getRandomValues(iv);\n\n // clear bit 63 of the IV to stop us hitting the 64-bit counter boundary\n // (which would mean we wouldn't be able to decrypt on Android). The loss\n // of a single bit of iv is a price we have to pay.\n iv[8] &= 0x7f;\n }\n\n const [aesKey, hmacKey] = await deriveKeys(key, name);\n const encodedData = new TextEncoder().encode(data);\n\n const ciphertext = await globalThis.crypto.subtle.encrypt(\n {\n name: \"AES-CTR\",\n counter: iv,\n length: 64,\n },\n aesKey,\n encodedData,\n );\n\n const hmac = await globalThis.crypto.subtle.sign({ name: \"HMAC\" }, hmacKey, ciphertext);\n\n return {\n iv: encodeBase64(iv),\n ciphertext: encodeBase64(ciphertext),\n mac: encodeBase64(hmac),\n };\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,YAAY,EAAEC,YAAY,QAAQ,cAAc;AACzD,SAASC,UAAU,QAAQ,0BAA0B;AAGrD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,wBAA8BC,2BAA2BA,CAAAC,EAAA,EAAAC,GAAA,EAAAC,GAAA,EAAAC,GAAA;EAAA,OAAAC,4BAAA,CAAAC,KAAA,OAAAC,SAAA;AAAA;AAuCxD,SAAAF,6BAAA;EAAAA,4BAAA,GAAAG,iBAAA,CAvCc,WACXC,IAAY,EACZC,GAAe,EACfC,IAAY,EACZC,KAAc,EAC2B;IACzC,IAAIC,EAAc;IAClB,IAAID,KAAK,EAAE;MACPC,EAAE,GAAGhB,YAAY,CAACe,KAAK,CAAC;IAC5B,CAAC,MAAM;MACHC,EAAE,GAAG,IAAIC,UAAU,CAAC,EAAE,CAAC;MACvBC,UAAU,CAACC,MAAM,CAACC,eAAe,CAACJ,EAAE,CAAC;;MAErC;MACA;MACA;MACAA,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI;IACjB;IAEA,IAAM,CAACK,MAAM,EAAEC,OAAO,CAAC,SAASpB,UAAU,CAACW,GAAG,EAAEC,IAAI,CAAC;IACrD,IAAMS,WAAW,GAAG,IAAIC,WAAW,CAAC,CAAC,CAACC,MAAM,CAACb,IAAI,CAAC;IAElD,IAAMc,UAAU,SAASR,UAAU,CAACC,MAAM,CAACQ,MAAM,CAACC,OAAO,CACrD;MACId,IAAI,EAAE,SAAS;MACfe,OAAO,EAAEb,EAAE;MACXc,MAAM,EAAE;IACZ,CAAC,EACDT,MAAM,EACNE,WACJ,CAAC;IAED,IAAMQ,IAAI,SAASb,UAAU,CAACC,MAAM,CAACQ,MAAM,CAACK,IAAI,CAAC;MAAElB,IAAI,EAAE;IAAO,CAAC,EAAEQ,OAAO,EAAEI,UAAU,CAAC;IAEvF,OAAO;MACHV,EAAE,EAAEf,YAAY,CAACe,EAAE,CAAC;MACpBU,UAAU,EAAEzB,YAAY,CAACyB,UAAU,CAAC;MACpCO,GAAG,EAAEhC,YAAY,CAAC8B,IAAI;IAC1B,CAAC;EACL,CAAC;EAAA,OAAAvB,4BAAA,CAAAC,KAAA,OAAAC,SAAA;AAAA","ignoreList":[]}
@@ -1,10 +0,0 @@
1
- /**
2
- * Derive AES and HMAC keys from a master key.
3
- *
4
- * This is used for deriving secret storage keys: see https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2 (step 1).
5
- *
6
- * @param key
7
- * @param name
8
- */
9
- export declare function deriveKeys(key: Uint8Array, name: string): Promise<[CryptoKey, CryptoKey]>;
10
- //# sourceMappingURL=deriveKeys.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"deriveKeys.d.ts","sourceRoot":"","sources":["../../../src/utils/internal/deriveKeys.ts"],"names":[],"mappings":"AAmBA;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAmC/F"}
@@ -1,60 +0,0 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- /*
3
- * Copyright 2024 The Matrix.org Foundation C.I.C.
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
-
18
- // salt for HKDF, with 8 bytes of zeros
19
- var zeroSalt = new Uint8Array(8);
20
-
21
- /**
22
- * Derive AES and HMAC keys from a master key.
23
- *
24
- * This is used for deriving secret storage keys: see https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2 (step 1).
25
- *
26
- * @param key
27
- * @param name
28
- */
29
- export function deriveKeys(_x, _x2) {
30
- return _deriveKeys.apply(this, arguments);
31
- }
32
- function _deriveKeys() {
33
- _deriveKeys = _asyncToGenerator(function* (key, name) {
34
- var hkdfkey = yield globalThis.crypto.subtle.importKey("raw", key, {
35
- name: "HKDF"
36
- }, false, ["deriveBits"]);
37
- var keybits = yield globalThis.crypto.subtle.deriveBits({
38
- name: "HKDF",
39
- salt: zeroSalt,
40
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
41
- // @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/879
42
- info: new TextEncoder().encode(name),
43
- hash: "SHA-256"
44
- }, hkdfkey, 512);
45
- var aesKey = keybits.slice(0, 32);
46
- var hmacKey = keybits.slice(32);
47
- var aesProm = globalThis.crypto.subtle.importKey("raw", aesKey, {
48
- name: "AES-CTR"
49
- }, false, ["encrypt", "decrypt"]);
50
- var hmacProm = globalThis.crypto.subtle.importKey("raw", hmacKey, {
51
- name: "HMAC",
52
- hash: {
53
- name: "SHA-256"
54
- }
55
- }, false, ["sign", "verify"]);
56
- return Promise.all([aesProm, hmacProm]);
57
- });
58
- return _deriveKeys.apply(this, arguments);
59
- }
60
- //# sourceMappingURL=deriveKeys.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"deriveKeys.js","names":["zeroSalt","Uint8Array","deriveKeys","_x","_x2","_deriveKeys","apply","arguments","_asyncToGenerator","key","name","hkdfkey","globalThis","crypto","subtle","importKey","keybits","deriveBits","salt","info","TextEncoder","encode","hash","aesKey","slice","hmacKey","aesProm","hmacProm","Promise","all"],"sources":["../../../src/utils/internal/deriveKeys.ts"],"sourcesContent":["/*\n * Copyright 2024 The Matrix.org Foundation C.I.C.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// salt for HKDF, with 8 bytes of zeros\nconst zeroSalt = new Uint8Array(8);\n\n/**\n * Derive AES and HMAC keys from a master key.\n *\n * This is used for deriving secret storage keys: see https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2 (step 1).\n *\n * @param key\n * @param name\n */\nexport async function deriveKeys(key: Uint8Array, name: string): Promise<[CryptoKey, CryptoKey]> {\n const hkdfkey = await globalThis.crypto.subtle.importKey(\"raw\", key, { name: \"HKDF\" }, false, [\"deriveBits\"]);\n const keybits = await globalThis.crypto.subtle.deriveBits(\n {\n name: \"HKDF\",\n salt: zeroSalt,\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/879\n info: new TextEncoder().encode(name),\n hash: \"SHA-256\",\n },\n hkdfkey,\n 512,\n );\n\n const aesKey = keybits.slice(0, 32);\n const hmacKey = keybits.slice(32);\n\n const aesProm = globalThis.crypto.subtle.importKey(\"raw\", aesKey, { name: \"AES-CTR\" }, false, [\n \"encrypt\",\n \"decrypt\",\n ]);\n\n const hmacProm = globalThis.crypto.subtle.importKey(\n \"raw\",\n hmacKey,\n {\n name: \"HMAC\",\n hash: { name: \"SHA-256\" },\n },\n false,\n [\"sign\", \"verify\"],\n );\n\n return Promise.all([aesProm, hmacProm]);\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA,IAAMA,QAAQ,GAAG,IAAIC,UAAU,CAAC,CAAC,CAAC;;AAElC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,gBAAsBC,UAAUA,CAAAC,EAAA,EAAAC,GAAA;EAAA,OAAAC,WAAA,CAAAC,KAAA,OAAAC,SAAA;AAAA;AAmC/B,SAAAF,YAAA;EAAAA,WAAA,GAAAG,iBAAA,CAnCM,WAA0BC,GAAe,EAAEC,IAAY,EAAmC;IAC7F,IAAMC,OAAO,SAASC,UAAU,CAACC,MAAM,CAACC,MAAM,CAACC,SAAS,CAAC,KAAK,EAAEN,GAAG,EAAE;MAAEC,IAAI,EAAE;IAAO,CAAC,EAAE,KAAK,EAAE,CAAC,YAAY,CAAC,CAAC;IAC7G,IAAMM,OAAO,SAASJ,UAAU,CAACC,MAAM,CAACC,MAAM,CAACG,UAAU,CACrD;MACIP,IAAI,EAAE,MAAM;MACZQ,IAAI,EAAElB,QAAQ;MACd;MACA;MACAmB,IAAI,EAAE,IAAIC,WAAW,CAAC,CAAC,CAACC,MAAM,CAACX,IAAI,CAAC;MACpCY,IAAI,EAAE;IACV,CAAC,EACDX,OAAO,EACP,GACJ,CAAC;IAED,IAAMY,MAAM,GAAGP,OAAO,CAACQ,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;IACnC,IAAMC,OAAO,GAAGT,OAAO,CAACQ,KAAK,CAAC,EAAE,CAAC;IAEjC,IAAME,OAAO,GAAGd,UAAU,CAACC,MAAM,CAACC,MAAM,CAACC,SAAS,CAAC,KAAK,EAAEQ,MAAM,EAAE;MAAEb,IAAI,EAAE;IAAU,CAAC,EAAE,KAAK,EAAE,CAC1F,SAAS,EACT,SAAS,CACZ,CAAC;IAEF,IAAMiB,QAAQ,GAAGf,UAAU,CAACC,MAAM,CAACC,MAAM,CAACC,SAAS,CAC/C,KAAK,EACLU,OAAO,EACP;MACIf,IAAI,EAAE,MAAM;MACZY,IAAI,EAAE;QAAEZ,IAAI,EAAE;MAAU;IAC5B,CAAC,EACD,KAAK,EACL,CAAC,MAAM,EAAE,QAAQ,CACrB,CAAC;IAED,OAAOkB,OAAO,CAACC,GAAG,CAAC,CAACH,OAAO,EAAEC,QAAQ,CAAC,CAAC;EAC3C,CAAC;EAAA,OAAAtB,WAAA,CAAAC,KAAA,OAAAC,SAAA;AAAA","ignoreList":[]}
@@ -1,29 +0,0 @@
1
- /*
2
- * Copyright 2024 The Matrix.org Foundation C.I.C.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- /**
18
- * An AES-encrypted secret storage payload.
19
- * See https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2-1
20
- */
21
- export interface AESEncryptedSecretStoragePayload {
22
- [key: string]: any; // extensible
23
- /** the initialization vector in base64 */
24
- iv: string;
25
- /** the ciphertext in base64 */
26
- ciphertext: string;
27
- /** the HMAC in base64 */
28
- mac: string;
29
- }
@@ -1,54 +0,0 @@
1
- /*
2
- * Copyright 2024 The Matrix.org Foundation C.I.C.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import { decodeBase64 } from "../base64.ts";
18
- import { deriveKeys } from "./internal/deriveKeys.ts";
19
- import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
20
-
21
- /**
22
- * Decrypt an AES-encrypted Secret Storage item.
23
- *
24
- * @param data - the encrypted data, returned by {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
25
- * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key. Must
26
- * be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
27
- * @param name - the name of the secret. Also used as an input to the HKDF operation which is used to derive the AES
28
- * key, so again must be the same as provided to {@link utils/encryptAESSecretStorageItem.default | encryptAESSecretStorageItem}.
29
- */
30
- export default async function decryptAESSecretStorageItem(
31
- data: AESEncryptedSecretStoragePayload,
32
- key: Uint8Array,
33
- name: string,
34
- ): Promise<string> {
35
- const [aesKey, hmacKey] = await deriveKeys(key, name);
36
-
37
- const ciphertext = decodeBase64(data.ciphertext);
38
-
39
- if (!(await globalThis.crypto.subtle.verify({ name: "HMAC" }, hmacKey, decodeBase64(data.mac), ciphertext))) {
40
- throw new Error(`Error decrypting secret ${name}: bad MAC`);
41
- }
42
-
43
- const plaintext = await globalThis.crypto.subtle.decrypt(
44
- {
45
- name: "AES-CTR",
46
- counter: decodeBase64(data.iv),
47
- length: 64,
48
- },
49
- aesKey,
50
- ciphertext,
51
- );
52
-
53
- return new TextDecoder().decode(new Uint8Array(plaintext));
54
- }
@@ -1,73 +0,0 @@
1
- /*
2
- * Copyright 2024 The Matrix.org Foundation C.I.C.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import { decodeBase64, encodeBase64 } from "../base64.ts";
18
- import { deriveKeys } from "./internal/deriveKeys.ts";
19
- import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
20
-
21
- /**
22
- * Encrypt a string as a secret storage item, using AES-CTR.
23
- *
24
- * @param data - the plaintext to encrypt
25
- * @param key - the encryption key to use as an input to the HKDF function which is used to derive the AES key for
26
- * encryption. Obviously, the same key must be provided when decrypting.
27
- * @param name - the name of the secret. Used as an input to the HKDF operation which is used to derive the AES key,
28
- * so again the same value must be provided when decrypting.
29
- * @param ivStr - the base64-encoded initialization vector to use. If not supplied, a random one will be generated.
30
- *
31
- * @returns The encrypted result, including the ciphertext itself, the initialization vector (as supplied in `ivStr`,
32
- * or generated), and an HMAC on the ciphertext — all base64-encoded.
33
- */
34
- export default async function encryptAESSecretStorageItem(
35
- data: string,
36
- key: Uint8Array,
37
- name: string,
38
- ivStr?: string,
39
- ): Promise<AESEncryptedSecretStoragePayload> {
40
- let iv: Uint8Array;
41
- if (ivStr) {
42
- iv = decodeBase64(ivStr);
43
- } else {
44
- iv = new Uint8Array(16);
45
- globalThis.crypto.getRandomValues(iv);
46
-
47
- // clear bit 63 of the IV to stop us hitting the 64-bit counter boundary
48
- // (which would mean we wouldn't be able to decrypt on Android). The loss
49
- // of a single bit of iv is a price we have to pay.
50
- iv[8] &= 0x7f;
51
- }
52
-
53
- const [aesKey, hmacKey] = await deriveKeys(key, name);
54
- const encodedData = new TextEncoder().encode(data);
55
-
56
- const ciphertext = await globalThis.crypto.subtle.encrypt(
57
- {
58
- name: "AES-CTR",
59
- counter: iv,
60
- length: 64,
61
- },
62
- aesKey,
63
- encodedData,
64
- );
65
-
66
- const hmac = await globalThis.crypto.subtle.sign({ name: "HMAC" }, hmacKey, ciphertext);
67
-
68
- return {
69
- iv: encodeBase64(iv),
70
- ciphertext: encodeBase64(ciphertext),
71
- mac: encodeBase64(hmac),
72
- };
73
- }
@@ -1,63 +0,0 @@
1
- /*
2
- * Copyright 2024 The Matrix.org Foundation C.I.C.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- // salt for HKDF, with 8 bytes of zeros
18
- const zeroSalt = new Uint8Array(8);
19
-
20
- /**
21
- * Derive AES and HMAC keys from a master key.
22
- *
23
- * This is used for deriving secret storage keys: see https://spec.matrix.org/v1.11/client-server-api/#msecret_storagev1aes-hmac-sha2 (step 1).
24
- *
25
- * @param key
26
- * @param name
27
- */
28
- export async function deriveKeys(key: Uint8Array, name: string): Promise<[CryptoKey, CryptoKey]> {
29
- const hkdfkey = await globalThis.crypto.subtle.importKey("raw", key, { name: "HKDF" }, false, ["deriveBits"]);
30
- const keybits = await globalThis.crypto.subtle.deriveBits(
31
- {
32
- name: "HKDF",
33
- salt: zeroSalt,
34
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
35
- // @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/879
36
- info: new TextEncoder().encode(name),
37
- hash: "SHA-256",
38
- },
39
- hkdfkey,
40
- 512,
41
- );
42
-
43
- const aesKey = keybits.slice(0, 32);
44
- const hmacKey = keybits.slice(32);
45
-
46
- const aesProm = globalThis.crypto.subtle.importKey("raw", aesKey, { name: "AES-CTR" }, false, [
47
- "encrypt",
48
- "decrypt",
49
- ]);
50
-
51
- const hmacProm = globalThis.crypto.subtle.importKey(
52
- "raw",
53
- hmacKey,
54
- {
55
- name: "HMAC",
56
- hash: { name: "SHA-256" },
57
- },
58
- false,
59
- ["sign", "verify"],
60
- );
61
-
62
- return Promise.all([aesProm, hmacProm]);
63
- }