π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Rework decryption
Manav Rathi105f750c9562
diff --git a/web/apps/cast/src/services/render.ts b/web/apps/cast/src/services/render.tsindex b9f9c589d0..7fcb020444 100644--- a/web/apps/cast/src/services/render.ts+++ b/web/apps/cast/src/services/render.ts@@ -343,13 +343,13 @@ const downloadFile = async ();const cryptoWorker = await sharedCryptoWorker();- const decrypted = await cryptoWorker.decryptFile(- new Uint8Array(await res.arrayBuffer()),- await cryptoWorker.fromB64(- shouldUseThumbnail+ const decrypted = await cryptoWorker.decryptStreamBytes(+ {+ encryptedData: new Uint8Array(await res.arrayBuffer()),+ decryptionHeader: shouldUseThumbnail? file.thumbnail.decryptionHeader: file.file.decryptionHeader,- ),+ },file.key,);return new Response(decrypted).blob();diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.tsindex d5369eb07f..82b5e41152 100644--- a/web/packages/base/crypto/ente-impl.ts+++ b/web/packages/base/crypto/ente-impl.ts@@ -57,6 +57,12 @@ export const _decryptBlobB64 = libsodium.decryptBlobB64;export const _decryptThumbnail = _decryptBlob;+export const _decryptStreamBytes = libsodium.decryptStreamBytes;++export const _initChunkDecryption = libsodium.initChunkDecryption;++export const _decryptStreamChunk = libsodium.decryptStreamChunk;+export const _decryptMetadataJSON_New = async (blob: EncryptedBlob,key: BytesOrB64,diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.tsindex 40636c5b21..177a28ce33 100644--- a/web/packages/base/crypto/libsodium.ts+++ b/web/packages/base/crypto/libsodium.ts@@ -503,16 +503,17 @@ export const decryptBlobB64 = (key: BytesOrB64,): Promise<string> => decryptBlob(blob, key).then(toB64);-/** Decrypt Stream, but merge the results. */-export const decryptChaCha = async (- data: Uint8Array,- header: Uint8Array,- key: string,+/**+ * Decrypt the result of {@link encryptStreamBytes}.+ */+export const decryptStreamBytes = async (+ { encryptedData, decryptionHeader }: EncryptedFile,+ key: BytesOrB64,) => {await sodium.ready;const pullState = sodium.crypto_secretstream_xchacha20poly1305_init_pull(- header,- await fromB64(key),+ await fromB64(decryptionHeader),+ await bytes(key),);const decryptionChunkSize =streamEncryptionChunkSize +@@ -522,19 +523,14 @@ export const decryptChaCha = async (let tag = sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;while (tag !== sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) {let chunkSize = decryptionChunkSize;- if (bytesRead + chunkSize > data.length) {- chunkSize = data.length - bytesRead;+ if (bytesRead + chunkSize > encryptedData.length) {+ chunkSize = encryptedData.length - bytesRead;}- const buffer = data.slice(bytesRead, bytesRead + chunkSize);+ const buffer = encryptedData.slice(bytesRead, bytesRead + chunkSize);const pullResult = sodium.crypto_secretstream_xchacha20poly1305_pull(pullState,buffer,);- // TODO:- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (!pullResult.message) {- throw new Error("processing failed");- }decryptedChunks.push(pullResult.message);tag = pullResult.tag;bytesRead += chunkSize;@@ -542,36 +538,55 @@ export const decryptChaCha = async (return mergeUint8Arrays(decryptedChunks);};-export async function initChunkDecryption(header: Uint8Array, key: Uint8Array) {+/**+ * Prepare to decrypt the result of {@link initChunkEncryption} and+ * {@link encryptStreamChunk}.+ *+ * @param decryptionHeader The header (as a base64 string) that was produced+ * during encryption by {@link initChunkEncryption}.+ *+ * @param key The encryption key.+ *+ * @returns The pull state, which should be treated as opaque libsodium specific+ * state that should be passed along to each subsequent call to+ * {@link decryptStreamChunk}, and the size of each (decrypted) chunk that will+ * be produced by subsequent calls to {@link decryptStreamChunk}.+ */+export const initChunkDecryption = async (+ decryptionHeader: string,+ key: BytesOrB64,+) => {await sodium.ready;const pullState = sodium.crypto_secretstream_xchacha20poly1305_init_pull(- header,- key,+ await fromB64(decryptionHeader),+ await bytes(key),);const decryptionChunkSize =streamEncryptionChunkSize +sodium.crypto_secretstream_xchacha20poly1305_ABYTES;- const tag = sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;- return { pullState, decryptionChunkSize, tag };-}+ return { pullState, decryptionChunkSize };+};-export async function decryptFileChunk(+/**+ * Decrypt an individual chunk of the data encrypted using+ * {@link initChunkEncryption} and {@link encryptStreamChunk}.+ *+ * This is meant to be used in tandem with {@link initChunkDecryption}. During+ * each invocation, it should be passed the encrypted chunk, and the+ * {@link pullState} returned by {@link initChunkDecryption}. It will then+ * return the corresponding decrypted chunk's bytes.+ */+export const decryptStreamChunk = async (data: Uint8Array,pullState: StateAddress,-) {+) => {await sodium.ready;const pullResult = sodium.crypto_secretstream_xchacha20poly1305_pull(pullState,data,);- // TODO:- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (!pullResult.message) {- throw new Error("processing failed");- }- const newTag = pullResult.tag;- return { decryptedData: pullResult.message, newTag };-}+ return pullResult.message;+};export interface B64EncryptionResult {encryptedData: string;diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.tsindex ffe0503dde..a9a9d84bcb 100644--- a/web/packages/base/crypto/worker.ts+++ b/web/packages/base/crypto/worker.ts@@ -28,23 +28,14 @@ export class CryptoWorker {decryptBlob = ei._decryptBlob;decryptBlobB64 = ei._decryptBlobB64;decryptThumbnail = ei._decryptThumbnail;+ decryptStreamBytes = ei._decryptStreamBytes;+ initChunkDecryption = ei._initChunkDecryption;+ decryptStreamChunk = ei._decryptStreamChunk;decryptMetadataJSON_New = ei._decryptMetadataJSON_New;decryptMetadataJSON = ei._decryptMetadataJSON;// TODO: -- AUDIT BELOW --- async decryptFile(fileData: Uint8Array, header: Uint8Array, key: string) {- return libsodium.decryptChaCha(fileData, header, key);- }-- async initChunkDecryption(header: Uint8Array, key: Uint8Array) {- return libsodium.initChunkDecryption(header, key);- }-- async decryptFileChunk(fileData: Uint8Array, pullState: StateAddress) {- return libsodium.decryptFileChunk(fileData, pullState);- }-async initChunkHashing() {return libsodium.initChunkHashing();}diff --git a/web/packages/new/photos/services/download.ts b/web/packages/new/photos/services/download.tsindex 90d36c684a..5e716e641a 100644--- a/web/packages/new/photos/services/download.ts+++ b/web/packages/new/photos/services/download.ts@@ -301,9 +301,11 @@ class DownloadManagerImpl {}this.clearDownloadProgress(file.id);- const decrypted = await cryptoWorker.decryptFile(- new Uint8Array(encryptedArrayBuffer),- await cryptoWorker.fromB64(file.file.decryptionHeader),+ const decrypted = await cryptoWorker.decryptStreamBytes(+ {+ encryptedData: new Uint8Array(encryptedArrayBuffer),+ decryptionHeader: file.file.decryptionHeader,+ },file.key,);return new Response(decrypted).body;@@ -329,12 +331,11 @@ class DownloadManagerImpl {parseInt(res.headers.get("Content-Length") ?? "") || 0;let downloadedBytes = 0;- const decryptionHeader = await cryptoWorker.fromB64(- file.file.decryptionHeader,- );- const fileKey = await cryptoWorker.fromB64(file.key);const { pullState, decryptionChunkSize } =- await cryptoWorker.initChunkDecryption(decryptionHeader, fileKey);+ await cryptoWorker.initChunkDecryption(+ file.file.decryptionHeader,+ file.key,+ );let leftoverBytes = new Uint8Array();@@ -367,8 +368,8 @@ class DownloadManagerImpl {// data.length might be a multiple of decryptionChunkSize,// and we might need multiple iterations to drain it all.while (data.length >= decryptionChunkSize) {- const { decryptedData } =- await cryptoWorker.decryptFileChunk(+ const decryptedData =+ await cryptoWorker.decryptStreamChunk(data.slice(0, decryptionChunkSize),pullState,);@@ -381,8 +382,8 @@ class DownloadManagerImpl {// Send off the remaining bytes without waiting for a// full chunk, no more bytes are going to come.if (data.length) {- const { decryptedData } =- await cryptoWorker.decryptFileChunk(+ const decryptedData =+ await cryptoWorker.decryptStreamChunk(data,pullState,);