Fix a couple of minor review comments
This commit is contained in:
parent
e37bf6b7be
commit
09ce74cc76
2 changed files with 16 additions and 16 deletions
|
@ -60,12 +60,12 @@ export function decryptMegolmKeyFile(data, password) {
|
||||||
const hmac = body.subarray(-32);
|
const hmac = body.subarray(-32);
|
||||||
|
|
||||||
return deriveKeys(salt, iterations, password).then((keys) => {
|
return deriveKeys(salt, iterations, password).then((keys) => {
|
||||||
const [aes_key, sha_key] = keys;
|
const [aes_key, hmac_key] = keys;
|
||||||
|
|
||||||
const toVerify = body.subarray(0, -32);
|
const toVerify = body.subarray(0, -32);
|
||||||
return subtleCrypto.verify(
|
return subtleCrypto.verify(
|
||||||
{name: 'HMAC'},
|
{name: 'HMAC'},
|
||||||
sha_key,
|
hmac_key,
|
||||||
hmac,
|
hmac,
|
||||||
toVerify,
|
toVerify,
|
||||||
).then((isValid) => {
|
).then((isValid) => {
|
||||||
|
@ -109,7 +109,7 @@ export function encryptMegolmKeyFile(data, password, options) {
|
||||||
window.crypto.getRandomValues(iv);
|
window.crypto.getRandomValues(iv);
|
||||||
|
|
||||||
return deriveKeys(salt, kdf_rounds, password).then((keys) => {
|
return deriveKeys(salt, kdf_rounds, password).then((keys) => {
|
||||||
const [aes_key, sha_key] = keys;
|
const [aes_key, hmac_key] = keys;
|
||||||
|
|
||||||
return subtleCrypto.encrypt(
|
return subtleCrypto.encrypt(
|
||||||
{
|
{
|
||||||
|
@ -137,7 +137,7 @@ export function encryptMegolmKeyFile(data, password, options) {
|
||||||
|
|
||||||
return subtleCrypto.sign(
|
return subtleCrypto.sign(
|
||||||
{name: 'HMAC'},
|
{name: 'HMAC'},
|
||||||
sha_key,
|
hmac_key,
|
||||||
toSign,
|
toSign,
|
||||||
).then((hmac) => {
|
).then((hmac) => {
|
||||||
hmac = new Uint8Array(hmac);
|
hmac = new Uint8Array(hmac);
|
||||||
|
@ -149,12 +149,12 @@ export function encryptMegolmKeyFile(data, password, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derive the AES and SHA keys for the file
|
* Derive the AES and HMAC-SHA-256 keys for the file
|
||||||
*
|
*
|
||||||
* @param {Unit8Array} salt salt for pbkdf
|
* @param {Unit8Array} salt salt for pbkdf
|
||||||
* @param {Number} iterations number of pbkdf iterations
|
* @param {Number} iterations number of pbkdf iterations
|
||||||
* @param {String} password password
|
* @param {String} password password
|
||||||
* @return {Promise<[CryptoKey, CryptoKey]>} promise for [aes key, sha key]
|
* @return {Promise<[CryptoKey, CryptoKey]>} promise for [aes key, hmac key]
|
||||||
*/
|
*/
|
||||||
function deriveKeys(salt, iterations, password) {
|
function deriveKeys(salt, iterations, password) {
|
||||||
return subtleCrypto.importKey(
|
return subtleCrypto.importKey(
|
||||||
|
@ -176,7 +176,7 @@ function deriveKeys(salt, iterations, password) {
|
||||||
);
|
);
|
||||||
}).then((keybits) => {
|
}).then((keybits) => {
|
||||||
const aes_key = keybits.slice(0, 32);
|
const aes_key = keybits.slice(0, 32);
|
||||||
const sha_key = keybits.slice(32);
|
const hmac_key = keybits.slice(32);
|
||||||
|
|
||||||
const aes_prom = subtleCrypto.importKey(
|
const aes_prom = subtleCrypto.importKey(
|
||||||
'raw',
|
'raw',
|
||||||
|
@ -185,9 +185,9 @@ function deriveKeys(salt, iterations, password) {
|
||||||
false,
|
false,
|
||||||
['encrypt', 'decrypt']
|
['encrypt', 'decrypt']
|
||||||
);
|
);
|
||||||
const sha_prom = subtleCrypto.importKey(
|
const hmac_prom = subtleCrypto.importKey(
|
||||||
'raw',
|
'raw',
|
||||||
sha_key,
|
hmac_key,
|
||||||
{
|
{
|
||||||
name: 'HMAC',
|
name: 'HMAC',
|
||||||
hash: {name: 'SHA-256'},
|
hash: {name: 'SHA-256'},
|
||||||
|
@ -195,7 +195,7 @@ function deriveKeys(salt, iterations, password) {
|
||||||
false,
|
false,
|
||||||
['sign', 'verify']
|
['sign', 'verify']
|
||||||
);
|
);
|
||||||
return Promise.all([aes_prom, sha_prom]);
|
return Promise.all([aes_prom, hmac_prom]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,16 +61,16 @@ export default class TextEncoder {
|
||||||
outU8Array[outIdx++] = u;
|
outU8Array[outIdx++] = u;
|
||||||
} else if (u <= 0x7FF) {
|
} else if (u <= 0x7FF) {
|
||||||
outU8Array[outIdx++] = 0xC0 | (u >> 6);
|
outU8Array[outIdx++] = 0xC0 | (u >> 6);
|
||||||
outU8Array[outIdx++] = 0x80 | (u & 63);
|
outU8Array[outIdx++] = 0x80 | (u & 0x3F);
|
||||||
} else if (u <= 0xFFFF) {
|
} else if (u <= 0xFFFF) {
|
||||||
outU8Array[outIdx++] = 0xE0 | (u >> 12);
|
outU8Array[outIdx++] = 0xE0 | (u >> 12);
|
||||||
outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
|
outU8Array[outIdx++] = 0x80 | ((u >> 6) & 0x3F);
|
||||||
outU8Array[outIdx++] = 0x80 | (u & 63);
|
outU8Array[outIdx++] = 0x80 | (u & 0x3F);
|
||||||
} else {
|
} else {
|
||||||
outU8Array[outIdx++] = 0xF0 | (u >> 18);
|
outU8Array[outIdx++] = 0xF0 | (u >> 18);
|
||||||
outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63);
|
outU8Array[outIdx++] = 0x80 | ((u >> 12) & 0x3F);
|
||||||
outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63);
|
outU8Array[outIdx++] = 0x80 | ((u >> 6) & 0x3F);
|
||||||
outU8Array[outIdx++] = 0x80 | (u & 63);
|
outU8Array[outIdx++] = 0x80 | (u & 0x3F);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return outU8Array;
|
return outU8Array;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue