Skip to content

Commit

Permalink
Use synchronous crypto for sealed sender
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal authored and josh-signal committed Apr 5, 2021
1 parent d81aaf6 commit 6cfb3c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 73 deletions.
83 changes: 14 additions & 69 deletions ts/Crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
import pProps from 'p-props';
import { chunk } from 'lodash';

import {
CipherType,
encrypt,
decrypt,
hash,
sign,
} from './util/synchronousCrypto';

export function typedArrayToArrayBuffer(typedArray: Uint8Array): ArrayBuffer {
const ab = new ArrayBuffer(typedArray.length);
// Create a new Uint8Array backed by the ArrayBuffer and copy all values from
Expand Down Expand Up @@ -79,8 +87,8 @@ export async function deriveMasterKeyFromGroupV1(
}

export async function computeHash(data: ArrayBuffer): Promise<string> {
const hash = await crypto.subtle.digest({ name: 'SHA-512' }, data);
return arrayBufferToBase64(hash);
const digest = await crypto.subtle.digest({ name: 'SHA-512' }, data);
return arrayBufferToBase64(digest);
}

// High-level Operations
Expand Down Expand Up @@ -331,21 +339,7 @@ export async function hmacSha256(
key: ArrayBuffer,
plaintext: ArrayBuffer
): Promise<ArrayBuffer> {
const algorithm: HmacImportParams = {
name: 'HMAC',
hash: 'SHA-256',
};
const extractable = false;

const cryptoKey = await window.crypto.subtle.importKey(
'raw',
key,
algorithm,
extractable,
['sign']
);

return window.crypto.subtle.sign(algorithm, cryptoKey, plaintext);
return sign(key, plaintext);
}

export async function _encryptAes256CbcPkcsPadding(
Expand Down Expand Up @@ -405,64 +399,15 @@ export async function encryptAesCtr(
plaintext: ArrayBuffer,
counter: ArrayBuffer
): Promise<ArrayBuffer> {
const extractable = false;
const algorithm = {
name: 'AES-CTR',
counter: new Uint8Array(counter),
length: 128,
};

const cryptoKey = await crypto.subtle.importKey(
'raw',
key,
// `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
algorithm as any,
extractable,
['encrypt']
);

const ciphertext = await crypto.subtle.encrypt(
algorithm,
cryptoKey,
plaintext
);

return ciphertext;
return encrypt(key, plaintext, counter, CipherType.AES256CTR);
}

export async function decryptAesCtr(
key: ArrayBuffer,
ciphertext: ArrayBuffer,
counter: ArrayBuffer
): Promise<ArrayBuffer> {
const extractable = false;
const algorithm = {
name: 'AES-CTR',
counter: new Uint8Array(counter),
length: 128,
};

const cryptoKey = await crypto.subtle.importKey(
'raw',
key,
// `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
algorithm as any,
extractable,
['decrypt']
);
const plaintext = await crypto.subtle.decrypt(
algorithm,
cryptoKey,
ciphertext
);

return plaintext;
return decrypt(key, ciphertext, counter, CipherType.AES256CTR);
}

export async function encryptAesGcm(
Expand Down Expand Up @@ -526,7 +471,7 @@ export async function decryptAesGcm(
// Hashing

export async function sha256(data: ArrayBuffer): Promise<ArrayBuffer> {
return crypto.subtle.digest('SHA-256', data);
return hash(data);
}

// Utility
Expand Down
15 changes: 11 additions & 4 deletions ts/util/synchronousCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export function hash(data: ArrayBuffer): ArrayBuffer {
);
}

export enum CipherType {
AES256CBC = 'aes-256-cbc',
AES256CTR = 'aes-256-ctr',
}

export function encrypt(
key: ArrayBuffer,
data: ArrayBuffer,
iv: ArrayBuffer
iv: ArrayBuffer,
cipherType: CipherType = CipherType.AES256CBC
): ArrayBuffer {
const cipher = crypto.createCipheriv(
'aes-256-cbc',
cipherType,
Buffer.from(key),
Buffer.from(iv)
);
Expand All @@ -41,10 +47,11 @@ export function encrypt(
export function decrypt(
key: ArrayBuffer,
data: ArrayBuffer,
iv: ArrayBuffer
iv: ArrayBuffer,
cipherType: CipherType = CipherType.AES256CBC
): ArrayBuffer {
const cipher = crypto.createDecipheriv(
'aes-256-cbc',
cipherType,
Buffer.from(key),
Buffer.from(iv)
);
Expand Down

0 comments on commit 6cfb3c9

Please sign in to comment.