2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-04 01:25:19 +00:00
|
|
|
import pProps from 'p-props';
|
2020-11-13 19:57:55 +00:00
|
|
|
import { chunk } from 'lodash';
|
2020-09-04 01:25:19 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export function typedArrayToArrayBuffer(typedArray: Uint8Array): ArrayBuffer {
|
2019-05-08 20:14:52 +00:00
|
|
|
const { buffer, byteOffset, byteLength } = typedArray;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2020-09-04 01:25:19 +00:00
|
|
|
return buffer.slice(byteOffset, byteLength + byteOffset) as typeof typedArray;
|
2019-05-08 20:14:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function arrayBufferToBase64(arrayBuffer: ArrayBuffer): string {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64');
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
2020-04-15 23:12:28 +00:00
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function arrayBufferToHex(arrayBuffer: ArrayBuffer): string {
|
2020-04-15 23:12:28 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('hex');
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function base64ToArrayBuffer(base64string: string): ArrayBuffer {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(base64string, 'base64').toArrayBuffer();
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function hexToArrayBuffer(hexString: string): ArrayBuffer {
|
2020-04-15 23:12:28 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(hexString, 'hex').toArrayBuffer();
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function fromEncodedBinaryToArrayBuffer(key: string): ArrayBuffer {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(key, 'binary').toArrayBuffer();
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function bytesFromString(string: string): ArrayBuffer {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(string, 'utf8').toArrayBuffer();
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
export function stringFromBytes(buffer: ArrayBuffer): string {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(buffer).toString('utf8');
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
2020-09-11 19:37:01 +00:00
|
|
|
export function hexFromBytes(buffer: ArrayBuffer): string {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(buffer).toString('hex');
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
2020-09-29 22:07:03 +00:00
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function bytesFromHexString(string: string): ArrayBuffer {
|
2020-03-31 20:03:38 +00:00
|
|
|
return window.dcodeIO.ByteBuffer.wrap(string, 'hex').toArrayBuffer();
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function deriveStickerPackKey(
|
|
|
|
packKey: ArrayBuffer
|
|
|
|
): Promise<ArrayBuffer> {
|
2019-05-16 22:32:11 +00:00
|
|
|
const salt = getZeroes(32);
|
|
|
|
const info = bytesFromString('Sticker Pack');
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const [part1, part2] = await window.libsignal.HKDF.deriveSecrets(
|
2019-05-16 22:32:11 +00:00
|
|
|
packKey,
|
|
|
|
salt,
|
|
|
|
info
|
|
|
|
);
|
|
|
|
|
|
|
|
return concatenateBytes(part1, part2);
|
|
|
|
}
|
2018-12-13 21:41:42 +00:00
|
|
|
|
2020-11-20 17:30:45 +00:00
|
|
|
export async function deriveMasterKeyFromGroupV1(
|
|
|
|
groupV1Id: ArrayBuffer
|
|
|
|
): Promise<ArrayBuffer> {
|
|
|
|
const salt = getZeroes(32);
|
|
|
|
const info = bytesFromString('GV2 Migration');
|
|
|
|
|
|
|
|
const [part1] = await window.libsignal.HKDF.deriveSecrets(
|
|
|
|
groupV1Id,
|
|
|
|
salt,
|
|
|
|
info
|
|
|
|
);
|
|
|
|
|
|
|
|
return part1;
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:25:05 +00:00
|
|
|
export async function computeHash(data: ArrayBuffer): Promise<string> {
|
|
|
|
const hash = await crypto.subtle.digest({ name: 'SHA-512' }, data);
|
|
|
|
return arrayBufferToBase64(hash);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// High-level Operations
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function encryptDeviceName(
|
|
|
|
deviceName: string,
|
|
|
|
identityPublic: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<Record<string, ArrayBuffer>> {
|
2018-12-13 19:12:33 +00:00
|
|
|
const plaintext = bytesFromString(deviceName);
|
2020-03-31 20:03:38 +00:00
|
|
|
const ephemeralKeyPair = await window.libsignal.KeyHelper.generateIdentityKeyPair();
|
|
|
|
const masterSecret = await window.libsignal.Curve.async.calculateAgreement(
|
2018-12-13 19:12:33 +00:00
|
|
|
identityPublic,
|
|
|
|
ephemeralKeyPair.privKey
|
|
|
|
);
|
|
|
|
|
|
|
|
const key1 = await hmacSha256(masterSecret, bytesFromString('auth'));
|
2019-05-08 20:14:52 +00:00
|
|
|
const syntheticIv = getFirstBytes(await hmacSha256(key1, plaintext), 16);
|
2018-12-13 19:12:33 +00:00
|
|
|
|
|
|
|
const key2 = await hmacSha256(masterSecret, bytesFromString('cipher'));
|
|
|
|
const cipherKey = await hmacSha256(key2, syntheticIv);
|
|
|
|
|
|
|
|
const counter = getZeroes(16);
|
|
|
|
const ciphertext = await encryptAesCtr(cipherKey, plaintext, counter);
|
|
|
|
|
|
|
|
return {
|
|
|
|
ephemeralPublic: ephemeralKeyPair.pubKey,
|
|
|
|
syntheticIv,
|
|
|
|
ciphertext,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function decryptDeviceName(
|
|
|
|
{
|
|
|
|
ephemeralPublic,
|
|
|
|
syntheticIv,
|
|
|
|
ciphertext,
|
|
|
|
}: {
|
|
|
|
ephemeralPublic: ArrayBuffer;
|
|
|
|
syntheticIv: ArrayBuffer;
|
|
|
|
ciphertext: ArrayBuffer;
|
|
|
|
},
|
|
|
|
identityPrivate: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<string> {
|
2020-03-31 20:03:38 +00:00
|
|
|
const masterSecret = await window.libsignal.Curve.async.calculateAgreement(
|
2018-12-13 19:12:33 +00:00
|
|
|
ephemeralPublic,
|
|
|
|
identityPrivate
|
|
|
|
);
|
|
|
|
|
|
|
|
const key2 = await hmacSha256(masterSecret, bytesFromString('cipher'));
|
|
|
|
const cipherKey = await hmacSha256(key2, syntheticIv);
|
|
|
|
|
|
|
|
const counter = getZeroes(16);
|
|
|
|
const plaintext = await decryptAesCtr(cipherKey, ciphertext, counter);
|
|
|
|
|
|
|
|
const key1 = await hmacSha256(masterSecret, bytesFromString('auth'));
|
2019-05-08 20:14:52 +00:00
|
|
|
const ourSyntheticIv = getFirstBytes(await hmacSha256(key1, plaintext), 16);
|
2018-12-13 19:12:33 +00:00
|
|
|
|
|
|
|
if (!constantTimeEqual(ourSyntheticIv, syntheticIv)) {
|
|
|
|
throw new Error('decryptDeviceName: synthetic IV did not match');
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringFromBytes(plaintext);
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
// Path structure: 'fa/facdf99c22945b1c9393345599a276f4b36ad7ccdc8c2467f5441b742c2d11fa'
|
2020-09-11 19:37:01 +00:00
|
|
|
export function getAttachmentLabel(path: string): ArrayBuffer {
|
2018-12-13 21:41:42 +00:00
|
|
|
const filename = path.slice(3);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
return base64ToArrayBuffer(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PUB_KEY_LENGTH = 32;
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function encryptAttachment(
|
|
|
|
staticPublicKey: ArrayBuffer,
|
|
|
|
path: string,
|
|
|
|
plaintext: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-12-13 21:41:42 +00:00
|
|
|
const uniqueId = getAttachmentLabel(path);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
return encryptFile(staticPublicKey, uniqueId, plaintext);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function decryptAttachment(
|
|
|
|
staticPrivateKey: ArrayBuffer,
|
|
|
|
path: string,
|
|
|
|
data: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-12-13 21:41:42 +00:00
|
|
|
const uniqueId = getAttachmentLabel(path);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
return decryptFile(staticPrivateKey, uniqueId, data);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function encryptFile(
|
|
|
|
staticPublicKey: ArrayBuffer,
|
|
|
|
uniqueId: ArrayBuffer,
|
|
|
|
plaintext: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2020-03-31 20:03:38 +00:00
|
|
|
const ephemeralKeyPair = await window.libsignal.KeyHelper.generateIdentityKeyPair();
|
|
|
|
const agreement = await window.libsignal.Curve.async.calculateAgreement(
|
2018-12-13 21:41:42 +00:00
|
|
|
staticPublicKey,
|
|
|
|
ephemeralKeyPair.privKey
|
|
|
|
);
|
|
|
|
const key = await hmacSha256(agreement, uniqueId);
|
|
|
|
|
|
|
|
const prefix = ephemeralKeyPair.pubKey.slice(1);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
return concatenateBytes(prefix, await encryptSymmetric(key, plaintext));
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function decryptFile(
|
|
|
|
staticPrivateKey: ArrayBuffer,
|
|
|
|
uniqueId: ArrayBuffer,
|
|
|
|
data: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2019-05-08 20:14:52 +00:00
|
|
|
const ephemeralPublicKey = getFirstBytes(data, PUB_KEY_LENGTH);
|
2020-09-04 01:25:19 +00:00
|
|
|
const ciphertext = getBytes(data, PUB_KEY_LENGTH, data.byteLength);
|
2020-03-31 20:03:38 +00:00
|
|
|
const agreement = await window.libsignal.Curve.async.calculateAgreement(
|
2018-12-13 21:41:42 +00:00
|
|
|
ephemeralPublicKey,
|
|
|
|
staticPrivateKey
|
|
|
|
);
|
|
|
|
|
|
|
|
const key = await hmacSha256(agreement, uniqueId);
|
|
|
|
|
|
|
|
return decryptSymmetric(key, ciphertext);
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:56:56 +00:00
|
|
|
export async function deriveStorageManifestKey(
|
|
|
|
storageServiceKey: ArrayBuffer,
|
|
|
|
version: number
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2020-07-07 00:56:56 +00:00
|
|
|
return hmacSha256(storageServiceKey, bytesFromString(`Manifest_${version}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deriveStorageItemKey(
|
|
|
|
storageServiceKey: ArrayBuffer,
|
|
|
|
itemID: string
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2020-07-07 00:56:56 +00:00
|
|
|
return hmacSha256(storageServiceKey, bytesFromString(`Item_${itemID}`));
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function deriveAccessKey(
|
|
|
|
profileKey: ArrayBuffer
|
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const iv = getZeroes(12);
|
|
|
|
const plaintext = getZeroes(16);
|
2020-09-04 01:25:19 +00:00
|
|
|
const accessKey = await encryptAesGcm(profileKey, iv, plaintext);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2019-05-08 20:14:52 +00:00
|
|
|
return getFirstBytes(accessKey, 16);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function getAccessKeyVerifier(
|
|
|
|
accessKey: ArrayBuffer
|
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const plaintext = getZeroes(32);
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
return hmacSha256(accessKey, plaintext);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function verifyAccessKey(
|
|
|
|
accessKey: ArrayBuffer,
|
|
|
|
theirVerifier: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<boolean> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const ourVerifier = await getAccessKeyVerifier(accessKey);
|
|
|
|
|
|
|
|
if (constantTimeEqual(ourVerifier, theirVerifier)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
const IV_LENGTH = 16;
|
|
|
|
const MAC_LENGTH = 16;
|
|
|
|
const NONCE_LENGTH = 16;
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function encryptSymmetric(
|
|
|
|
key: ArrayBuffer,
|
|
|
|
plaintext: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const iv = getZeroes(IV_LENGTH);
|
|
|
|
const nonce = getRandomBytes(NONCE_LENGTH);
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const cipherKey = await hmacSha256(key, nonce);
|
|
|
|
const macKey = await hmacSha256(key, cipherKey);
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
const cipherText = await _encryptAes256CbcPkcsPadding(
|
2018-04-27 21:25:04 +00:00
|
|
|
cipherKey,
|
|
|
|
iv,
|
|
|
|
plaintext
|
|
|
|
);
|
2019-05-08 20:14:52 +00:00
|
|
|
const mac = getFirstBytes(await hmacSha256(macKey, cipherText), MAC_LENGTH);
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return concatenateBytes(nonce, cipherText, mac);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function decryptSymmetric(
|
|
|
|
key: ArrayBuffer,
|
|
|
|
data: ArrayBuffer
|
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const iv = getZeroes(IV_LENGTH);
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2019-05-08 20:14:52 +00:00
|
|
|
const nonce = getFirstBytes(data, NONCE_LENGTH);
|
2020-09-04 01:25:19 +00:00
|
|
|
const cipherText = getBytes(
|
2018-03-19 19:42:12 +00:00
|
|
|
data,
|
|
|
|
NONCE_LENGTH,
|
|
|
|
data.byteLength - NONCE_LENGTH - MAC_LENGTH
|
|
|
|
);
|
2020-09-04 01:25:19 +00:00
|
|
|
const theirMac = getBytes(data, data.byteLength - MAC_LENGTH, MAC_LENGTH);
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const cipherKey = await hmacSha256(key, nonce);
|
|
|
|
const macKey = await hmacSha256(key, cipherKey);
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2019-05-08 20:14:52 +00:00
|
|
|
const ourMac = getFirstBytes(
|
2018-10-18 01:01:21 +00:00
|
|
|
await hmacSha256(macKey, cipherText),
|
2018-04-27 21:25:04 +00:00
|
|
|
MAC_LENGTH
|
|
|
|
);
|
2018-03-19 19:42:12 +00:00
|
|
|
if (!constantTimeEqual(theirMac, ourMac)) {
|
2018-04-27 21:25:04 +00:00
|
|
|
throw new Error(
|
|
|
|
'decryptSymmetric: Failed to decrypt; MAC verification failed'
|
|
|
|
);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
return _decryptAes256CbcPkcsPadding(cipherKey, iv, cipherText);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function constantTimeEqual(
|
|
|
|
left: ArrayBuffer,
|
|
|
|
right: ArrayBuffer
|
|
|
|
): boolean {
|
2018-03-19 19:42:12 +00:00
|
|
|
if (left.byteLength !== right.byteLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let result = 0;
|
|
|
|
const ta1 = new Uint8Array(left);
|
|
|
|
const ta2 = new Uint8Array(right);
|
2020-03-31 20:03:38 +00:00
|
|
|
const max = left.byteLength;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
2018-03-19 19:42:12 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
result |= ta1[i] ^ ta2[i];
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Encryption
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function hmacSha256(
|
|
|
|
key: ArrayBuffer,
|
|
|
|
plaintext: ArrayBuffer
|
|
|
|
): Promise<ArrayBuffer> {
|
|
|
|
const algorithm: HmacImportParams = {
|
2018-10-18 01:01:21 +00:00
|
|
|
name: 'HMAC',
|
|
|
|
hash: 'SHA-256',
|
|
|
|
};
|
2018-03-19 19:42:12 +00:00
|
|
|
const extractable = false;
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
const cryptoKey = await window.crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
algorithm,
|
2018-03-19 19:42:12 +00:00
|
|
|
extractable,
|
|
|
|
['sign']
|
|
|
|
);
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return window.crypto.subtle.sign(algorithm, cryptoKey, plaintext);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function _encryptAes256CbcPkcsPadding(
|
2020-03-31 20:03:38 +00:00
|
|
|
key: ArrayBuffer,
|
|
|
|
iv: ArrayBuffer,
|
|
|
|
plaintext: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const algorithm = {
|
|
|
|
name: 'AES-CBC',
|
|
|
|
iv,
|
|
|
|
};
|
2018-03-19 19:42:12 +00:00
|
|
|
const extractable = false;
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
const cryptoKey = await window.crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
// `algorithm` appears to be an instance of AesCbcParams,
|
|
|
|
// which is not in the param's types, so we need to pass as `any`.
|
|
|
|
// TODO: just pass the string "AES-CBC", per the docs?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-03-31 20:03:38 +00:00
|
|
|
algorithm as any,
|
2018-03-19 19:42:12 +00:00
|
|
|
extractable,
|
|
|
|
['encrypt']
|
|
|
|
);
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return window.crypto.subtle.encrypt(algorithm, cryptoKey, plaintext);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function _decryptAes256CbcPkcsPadding(
|
2020-03-31 20:03:38 +00:00
|
|
|
key: ArrayBuffer,
|
|
|
|
iv: ArrayBuffer,
|
|
|
|
plaintext: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const algorithm = {
|
|
|
|
name: 'AES-CBC',
|
|
|
|
iv,
|
|
|
|
};
|
2018-03-19 19:42:12 +00:00
|
|
|
const extractable = false;
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
const cryptoKey = await window.crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
// `algorithm` appears to be an instance of AesCbcParams,
|
|
|
|
// which is not in the param's types, so we need to pass as `any`.
|
|
|
|
// TODO: just pass the string "AES-CBC", per the docs?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-03-31 20:03:38 +00:00
|
|
|
algorithm as any,
|
2018-03-19 19:42:12 +00:00
|
|
|
extractable,
|
|
|
|
['decrypt']
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return window.crypto.subtle.decrypt(algorithm, cryptoKey, plaintext);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function encryptAesCtr(
|
|
|
|
key: ArrayBuffer,
|
|
|
|
plaintext: ArrayBuffer,
|
|
|
|
counter: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const extractable = false;
|
|
|
|
const algorithm = {
|
|
|
|
name: 'AES-CTR',
|
|
|
|
counter: new Uint8Array(counter),
|
|
|
|
length: 128,
|
|
|
|
};
|
|
|
|
|
|
|
|
const cryptoKey = await crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
// `algorithm` appears to be an instance of AesCtrParams,
|
|
|
|
// which is not in the param's types, so we need to pass as `any`.
|
|
|
|
// TODO: just pass the string "AES-CTR", per the docs?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-03-31 20:03:38 +00:00
|
|
|
algorithm as any,
|
2018-10-18 01:01:21 +00:00
|
|
|
extractable,
|
|
|
|
['encrypt']
|
|
|
|
);
|
|
|
|
|
|
|
|
const ciphertext = await crypto.subtle.encrypt(
|
|
|
|
algorithm,
|
|
|
|
cryptoKey,
|
|
|
|
plaintext
|
|
|
|
);
|
|
|
|
|
|
|
|
return ciphertext;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export async function decryptAesCtr(
|
|
|
|
key: ArrayBuffer,
|
|
|
|
ciphertext: ArrayBuffer,
|
|
|
|
counter: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const extractable = false;
|
|
|
|
const algorithm = {
|
|
|
|
name: 'AES-CTR',
|
|
|
|
counter: new Uint8Array(counter),
|
|
|
|
length: 128,
|
|
|
|
};
|
|
|
|
|
|
|
|
const cryptoKey = await crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
// `algorithm` appears to be an instance of AesCtrParams,
|
|
|
|
// which is not in the param's types, so we need to pass as `any`.
|
|
|
|
// TODO: just pass the string "AES-CTR", per the docs?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-03-31 20:03:38 +00:00
|
|
|
algorithm as any,
|
2018-10-18 01:01:21 +00:00
|
|
|
extractable,
|
|
|
|
['decrypt']
|
|
|
|
);
|
|
|
|
const plaintext = await crypto.subtle.decrypt(
|
|
|
|
algorithm,
|
|
|
|
cryptoKey,
|
|
|
|
ciphertext
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return plaintext;
|
|
|
|
}
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2020-09-04 01:25:19 +00:00
|
|
|
export async function encryptAesGcm(
|
2020-03-31 20:03:38 +00:00
|
|
|
key: ArrayBuffer,
|
|
|
|
iv: ArrayBuffer,
|
2020-09-04 01:25:19 +00:00
|
|
|
plaintext: ArrayBuffer,
|
|
|
|
additionalData?: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const algorithm = {
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv,
|
2020-09-04 01:25:19 +00:00
|
|
|
...(additionalData ? { additionalData } : {}),
|
2018-10-18 01:01:21 +00:00
|
|
|
};
|
2020-09-04 01:25:19 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const extractable = false;
|
|
|
|
|
|
|
|
const cryptoKey = await crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
// `algorithm` appears to be an instance of AesGcmParams,
|
|
|
|
// which is not in the param's types, so we need to pass as `any`.
|
|
|
|
// TODO: just pass the string "AES-GCM", per the docs?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-03-31 20:03:38 +00:00
|
|
|
algorithm as any,
|
2018-10-18 01:01:21 +00:00
|
|
|
extractable,
|
|
|
|
['encrypt']
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return crypto.subtle.encrypt(algorithm, cryptoKey, plaintext);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 01:25:19 +00:00
|
|
|
export async function decryptAesGcm(
|
|
|
|
key: ArrayBuffer,
|
|
|
|
iv: ArrayBuffer,
|
|
|
|
ciphertext: ArrayBuffer,
|
|
|
|
additionalData?: ArrayBuffer
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<ArrayBuffer> {
|
2020-09-04 01:25:19 +00:00
|
|
|
const algorithm = {
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv,
|
|
|
|
...(additionalData ? { additionalData } : {}),
|
|
|
|
tagLength: 128,
|
|
|
|
};
|
|
|
|
|
|
|
|
const extractable = false;
|
|
|
|
const cryptoKey = await crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
key,
|
2020-09-11 19:37:01 +00:00
|
|
|
// `algorithm` appears to be an instance of AesGcmParams,
|
|
|
|
// which is not in the param's types, so we need to pass as `any`.
|
|
|
|
// TODO: just pass the string "AES-GCM", per the docs?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-09-04 01:25:19 +00:00
|
|
|
algorithm as any,
|
|
|
|
extractable,
|
|
|
|
['decrypt']
|
|
|
|
);
|
|
|
|
|
|
|
|
return crypto.subtle.decrypt(algorithm, cryptoKey, ciphertext);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashing
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export async function sha256(data: ArrayBuffer): Promise<ArrayBuffer> {
|
2020-09-04 01:25:19 +00:00
|
|
|
return crypto.subtle.digest('SHA-256', data);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Utility
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function getRandomBytes(n: number): ArrayBuffer {
|
2018-03-19 19:42:12 +00:00
|
|
|
const bytes = new Uint8Array(n);
|
|
|
|
window.crypto.getRandomValues(bytes);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return typedArrayToArrayBuffer(bytes);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export function getRandomValue(low: number, high: number): number {
|
2019-05-16 22:32:11 +00:00
|
|
|
const diff = high - low;
|
|
|
|
const bytes = new Uint32Array(1);
|
|
|
|
window.crypto.getRandomValues(bytes);
|
|
|
|
|
|
|
|
// Because high and low are inclusive
|
|
|
|
const mod = diff + 1;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
return (bytes[0] % mod) + low;
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function getZeroes(n: number): ArrayBuffer {
|
2018-03-19 19:42:12 +00:00
|
|
|
const result = new Uint8Array(n);
|
|
|
|
|
|
|
|
const value = 0;
|
|
|
|
const startIndex = 0;
|
|
|
|
const endExclusive = n;
|
|
|
|
result.fill(value, startIndex, endExclusive);
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
return typedArrayToArrayBuffer(result);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export function highBitsToInt(byte: number): number {
|
2020-09-11 19:37:01 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
2018-10-18 01:01:21 +00:00
|
|
|
return (byte & 0xff) >> 4;
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export function intsToByteHighAndLow(
|
|
|
|
highValue: number,
|
|
|
|
lowValue: number
|
|
|
|
): number {
|
2020-09-11 19:37:01 +00:00
|
|
|
// eslint-disable-next-line no-bitwise
|
2018-10-18 01:01:21 +00:00
|
|
|
return ((highValue << 4) | lowValue) & 0xff;
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function trimBytes(buffer: ArrayBuffer, length: number): ArrayBuffer {
|
2019-05-08 20:14:52 +00:00
|
|
|
return getFirstBytes(buffer, length);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export function getViewOfArrayBuffer(
|
|
|
|
buffer: ArrayBuffer,
|
|
|
|
start: number,
|
|
|
|
finish: number
|
2020-09-11 19:37:01 +00:00
|
|
|
): ArrayBuffer | SharedArrayBuffer {
|
2018-10-18 01:01:21 +00:00
|
|
|
const source = new Uint8Array(buffer);
|
|
|
|
const result = source.slice(start, finish);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return result.buffer;
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function concatenateBytes(
|
|
|
|
...elements: Array<ArrayBuffer | Uint8Array>
|
|
|
|
): ArrayBuffer {
|
2018-03-19 19:42:12 +00:00
|
|
|
const length = elements.reduce(
|
|
|
|
(total, element) => total + element.byteLength,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
const result = new Uint8Array(length);
|
|
|
|
let position = 0;
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const max = elements.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
2018-03-19 19:42:12 +00:00
|
|
|
const element = new Uint8Array(elements[i]);
|
|
|
|
result.set(element, position);
|
|
|
|
position += element.byteLength;
|
|
|
|
}
|
|
|
|
if (position !== result.length) {
|
|
|
|
throw new Error('problem concatenating!');
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
return typedArrayToArrayBuffer(result);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
export function splitBytes(
|
|
|
|
buffer: ArrayBuffer,
|
|
|
|
...lengths: Array<number>
|
|
|
|
): Array<ArrayBuffer> {
|
2018-10-18 01:01:21 +00:00
|
|
|
const total = lengths.reduce((acc, length) => acc + length, 0);
|
|
|
|
|
|
|
|
if (total !== buffer.byteLength) {
|
|
|
|
throw new Error(
|
2020-01-08 17:44:54 +00:00
|
|
|
`Requested lengths total ${total} does not match source total ${buffer.byteLength}`
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const source = new Uint8Array(buffer);
|
|
|
|
const results = [];
|
|
|
|
let position = 0;
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const max = lengths.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const length = lengths[i];
|
|
|
|
const result = new Uint8Array(length);
|
|
|
|
const section = source.slice(position, position + length);
|
|
|
|
result.set(section);
|
|
|
|
position += result.byteLength;
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
results.push(typedArrayToArrayBuffer(result));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function getFirstBytes(data: ArrayBuffer, n: number): ArrayBuffer {
|
2018-10-18 01:01:21 +00:00
|
|
|
const source = new Uint8Array(data);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return typedArrayToArrayBuffer(source.subarray(0, n));
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 01:25:19 +00:00
|
|
|
export function getBytes(
|
2020-03-31 20:03:38 +00:00
|
|
|
data: ArrayBuffer | Uint8Array,
|
|
|
|
start: number,
|
|
|
|
n: number
|
2020-09-11 19:37:01 +00:00
|
|
|
): ArrayBuffer {
|
2018-10-18 01:01:21 +00:00
|
|
|
const source = new Uint8Array(data);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return typedArrayToArrayBuffer(source.subarray(start, start + n));
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
2020-09-04 01:25:19 +00:00
|
|
|
|
|
|
|
function _getMacAndData(ciphertext: ArrayBuffer) {
|
|
|
|
const dataLength = ciphertext.byteLength - MAC_LENGTH;
|
|
|
|
const data = getBytes(ciphertext, 0, dataLength);
|
|
|
|
const mac = getBytes(ciphertext, dataLength, MAC_LENGTH);
|
|
|
|
|
|
|
|
return { data, mac };
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function encryptCdsDiscoveryRequest(
|
|
|
|
attestations: {
|
|
|
|
[key: string]: { clientKey: ArrayBuffer; requestId: ArrayBuffer };
|
|
|
|
},
|
|
|
|
phoneNumbers: ReadonlyArray<string>
|
2020-09-11 19:37:01 +00:00
|
|
|
): Promise<Record<string, unknown>> {
|
2020-09-04 01:25:19 +00:00
|
|
|
const nonce = getRandomBytes(32);
|
|
|
|
const numbersArray = new window.dcodeIO.ByteBuffer(
|
|
|
|
phoneNumbers.length * 8,
|
|
|
|
window.dcodeIO.ByteBuffer.BIG_ENDIAN
|
|
|
|
);
|
|
|
|
phoneNumbers.forEach(number => {
|
|
|
|
// Long.fromString handles numbers with or without a leading '+'
|
|
|
|
numbersArray.writeLong(window.dcodeIO.ByteBuffer.Long.fromString(number));
|
|
|
|
});
|
|
|
|
const queryDataPlaintext = concatenateBytes(nonce, numbersArray.buffer);
|
|
|
|
const queryDataKey = getRandomBytes(32);
|
|
|
|
const commitment = await sha256(queryDataPlaintext);
|
|
|
|
const iv = getRandomBytes(12);
|
|
|
|
const queryDataCiphertext = await encryptAesGcm(
|
|
|
|
queryDataKey,
|
|
|
|
iv,
|
|
|
|
queryDataPlaintext
|
|
|
|
);
|
|
|
|
const {
|
|
|
|
data: queryDataCiphertextData,
|
|
|
|
mac: queryDataCiphertextMac,
|
|
|
|
} = _getMacAndData(queryDataCiphertext);
|
|
|
|
|
|
|
|
const envelopes = await pProps(
|
|
|
|
attestations,
|
|
|
|
async ({ clientKey, requestId }) => {
|
|
|
|
const envelopeIv = getRandomBytes(12);
|
|
|
|
const ciphertext = await encryptAesGcm(
|
|
|
|
clientKey,
|
|
|
|
envelopeIv,
|
|
|
|
queryDataKey,
|
|
|
|
requestId
|
|
|
|
);
|
|
|
|
const { data, mac } = _getMacAndData(ciphertext);
|
|
|
|
|
|
|
|
return {
|
|
|
|
requestId: arrayBufferToBase64(requestId),
|
|
|
|
data: arrayBufferToBase64(data),
|
|
|
|
iv: arrayBufferToBase64(envelopeIv),
|
|
|
|
mac: arrayBufferToBase64(mac),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
addressCount: phoneNumbers.length,
|
|
|
|
commitment: arrayBufferToBase64(commitment),
|
|
|
|
data: arrayBufferToBase64(queryDataCiphertextData),
|
|
|
|
iv: arrayBufferToBase64(iv),
|
|
|
|
mac: arrayBufferToBase64(queryDataCiphertextMac),
|
|
|
|
envelopes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:57:55 +00:00
|
|
|
export function uuidToArrayBuffer(uuid: string): ArrayBuffer {
|
|
|
|
if (uuid.length !== 36) {
|
|
|
|
window.log.warn(
|
|
|
|
'uuidToArrayBuffer: received a string of invalid length. Returning an empty ArrayBuffer'
|
|
|
|
);
|
|
|
|
return new ArrayBuffer(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Uint8Array.from(
|
|
|
|
chunk(uuid.replace(/-/g, ''), 2).map(pair => parseInt(pair.join(''), 16))
|
|
|
|
).buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function arrayBufferToUuid(
|
|
|
|
arrayBuffer: ArrayBuffer
|
|
|
|
): undefined | string {
|
|
|
|
if (arrayBuffer.byteLength !== 16) {
|
|
|
|
window.log.warn(
|
|
|
|
'arrayBufferToUuid: received an ArrayBuffer of invalid length. Returning undefined'
|
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uuids = splitUuids(arrayBuffer);
|
|
|
|
if (uuids.length === 1) {
|
|
|
|
return uuids[0] || undefined;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:37:01 +00:00
|
|
|
export function splitUuids(arrayBuffer: ArrayBuffer): Array<string | null> {
|
2020-09-04 01:25:19 +00:00
|
|
|
const uuids = [];
|
|
|
|
for (let i = 0; i < arrayBuffer.byteLength; i += 16) {
|
|
|
|
const bytes = getBytes(arrayBuffer, i, 16);
|
|
|
|
const hex = arrayBufferToHex(bytes);
|
|
|
|
const chunks = [
|
|
|
|
hex.substring(0, 8),
|
|
|
|
hex.substring(8, 12),
|
|
|
|
hex.substring(12, 16),
|
|
|
|
hex.substring(16, 20),
|
|
|
|
hex.substring(20),
|
|
|
|
];
|
|
|
|
const uuid = chunks.join('-');
|
|
|
|
if (uuid !== '00000000-0000-0000-0000-000000000000') {
|
|
|
|
uuids.push(uuid);
|
|
|
|
} else {
|
|
|
|
uuids.push(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uuids;
|
|
|
|
}
|