-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(encryption): extract standalone encrypter/decrypter (#1945)
- Loading branch information
Showing
4 changed files
with
178 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { _decrypt, _encrypt, ENCRYPTION_KEY_BYTES, getKeyDigest, loadKey } from './utils'; | ||
|
||
/** | ||
* Default encrypter | ||
*/ | ||
export class Encrypter { | ||
private key: CryptoKey | undefined; | ||
private keyDigest: string | undefined; | ||
|
||
constructor(private readonly encryptionKey: Uint8Array) { | ||
if (encryptionKey.length !== ENCRYPTION_KEY_BYTES) { | ||
throw new Error(`Encryption key must be ${ENCRYPTION_KEY_BYTES} bytes`); | ||
} | ||
} | ||
|
||
/** | ||
* Encrypts the given data | ||
*/ | ||
async encrypt(data: string): Promise<string> { | ||
if (!this.key) { | ||
this.key = await loadKey(this.encryptionKey, ['encrypt']); | ||
} | ||
|
||
if (!this.keyDigest) { | ||
this.keyDigest = await getKeyDigest(this.encryptionKey); | ||
} | ||
|
||
return _encrypt(data, this.key, this.keyDigest); | ||
} | ||
} | ||
|
||
/** | ||
* Default decrypter | ||
*/ | ||
export class Decrypter { | ||
private keys: Array<{ key: CryptoKey; digest: string }> = []; | ||
|
||
constructor(private readonly decryptionKeys: Uint8Array[]) { | ||
if (decryptionKeys.length === 0) { | ||
throw new Error('At least one decryption key must be provided'); | ||
} | ||
|
||
for (const key of decryptionKeys) { | ||
if (key.length !== ENCRYPTION_KEY_BYTES) { | ||
throw new Error(`Decryption key must be ${ENCRYPTION_KEY_BYTES} bytes`); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Decrypts the given data | ||
*/ | ||
async decrypt(data: string): Promise<string> { | ||
if (this.keys.length === 0) { | ||
this.keys = await Promise.all( | ||
this.decryptionKeys.map(async (key) => ({ | ||
key: await loadKey(key, ['decrypt']), | ||
digest: await getKeyDigest(key), | ||
})) | ||
); | ||
} | ||
|
||
return _decrypt(data, async (digest) => | ||
this.keys.filter((entry) => entry.digest === digest).map((entry) => entry.key) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { z } from 'zod'; | ||
|
||
export const ENCRYPTER_VERSION = 1; | ||
export const ENCRYPTION_KEY_BYTES = 32; | ||
export const IV_BYTES = 12; | ||
export const ALGORITHM = 'AES-GCM'; | ||
export const KEY_DIGEST_BYTES = 8; | ||
|
||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
|
||
const encryptionMetaSchema = z.object({ | ||
// version | ||
v: z.number(), | ||
// algorithm | ||
a: z.string(), | ||
// key digest | ||
k: z.string(), | ||
}); | ||
|
||
export async function loadKey(key: Uint8Array, keyUsages: KeyUsage[]): Promise<CryptoKey> { | ||
return crypto.subtle.importKey('raw', key, ALGORITHM, false, keyUsages); | ||
} | ||
|
||
export async function getKeyDigest(key: Uint8Array) { | ||
const rawDigest = await crypto.subtle.digest('SHA-256', key); | ||
return new Uint8Array(rawDigest.slice(0, KEY_DIGEST_BYTES)).reduce( | ||
(acc, byte) => acc + byte.toString(16).padStart(2, '0'), | ||
'' | ||
); | ||
} | ||
|
||
export async function _encrypt(data: string, key: CryptoKey, keyDigest: string): Promise<string> { | ||
const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES)); | ||
const encrypted = await crypto.subtle.encrypt( | ||
{ | ||
name: ALGORITHM, | ||
iv, | ||
}, | ||
key, | ||
encoder.encode(data) | ||
); | ||
|
||
// combine IV and encrypted data into a single array of bytes | ||
const cipherBytes = [...iv, ...new Uint8Array(encrypted)]; | ||
|
||
// encryption metadata | ||
const meta = { v: ENCRYPTER_VERSION, a: ALGORITHM, k: keyDigest }; | ||
|
||
// convert concatenated result to base64 string | ||
return `${btoa(JSON.stringify(meta))}.${btoa(String.fromCharCode(...cipherBytes))}`; | ||
} | ||
|
||
export async function _decrypt(data: string, findKey: (digest: string) => Promise<CryptoKey[]>): Promise<string> { | ||
const [metaText, cipherText] = data.split('.'); | ||
if (!metaText || !cipherText) { | ||
throw new Error('Malformed encrypted data'); | ||
} | ||
|
||
let metaObj: unknown; | ||
try { | ||
metaObj = JSON.parse(atob(metaText)); | ||
} catch (error) { | ||
throw new Error('Malformed metadata'); | ||
} | ||
|
||
// parse meta | ||
const { a: algorithm, k: keyDigest } = encryptionMetaSchema.parse(metaObj); | ||
|
||
// find a matching decryption key | ||
const keys = await findKey(keyDigest); | ||
if (keys.length === 0) { | ||
throw new Error('No matching decryption key found'); | ||
} | ||
|
||
// convert base64 back to bytes | ||
const bytes = Uint8Array.from(atob(cipherText), (c) => c.charCodeAt(0)); | ||
|
||
// extract IV from the head | ||
const iv = bytes.slice(0, IV_BYTES); | ||
const cipher = bytes.slice(IV_BYTES); | ||
let lastError: unknown; | ||
|
||
for (const key of keys) { | ||
let decrypted: ArrayBuffer; | ||
try { | ||
decrypted = await crypto.subtle.decrypt({ name: algorithm, iv }, key, cipher); | ||
} catch (err) { | ||
lastError = err; | ||
continue; | ||
} | ||
return decoder.decode(decrypted); | ||
} | ||
|
||
throw lastError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters