π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
utf-8
Manav Rathi40e7d58c0b4e
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.tsindex 2b007391c7..e990c86d93 100644--- a/web/packages/base/crypto/ente-impl.ts+++ b/web/packages/base/crypto/ente-impl.ts@@ -20,6 +20,7 @@ export const _encryptStreamBytes = libsodium.encryptStreamBytes;export const _initChunkEncryption = libsodium.initChunkEncryption;export const _encryptStreamChunk = libsodium.encryptStreamChunk;export const _decryptBoxBytes = libsodium.decryptBoxBytes;+export const _decryptBoxUTF8 = libsodium.decryptBoxUTF8;export const _decryptBox = libsodium.decryptBox;export const _decryptBlobBytes = libsodium.decryptBlobBytes;export const _decryptBlob = libsodium.decryptBlob;diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.tsindex 5368a270c3..e9c24cce94 100644--- a/web/packages/base/crypto/index.ts+++ b/web/packages/base/crypto/index.ts@@ -27,6 +27,8 @@* to functions exposed by `crypto/libsodium.ts`, but they automatically defer* to a worker thread if we're not already running on one.*+ * ---+ ** [Note: Using libsodium in worker thread]** `crypto/ente-impl.ts` and `crypto/worker.ts` are logic-less internal files@@ -53,6 +55,34 @@* Also, some code (e.g. the uploader) creates it own crypto worker instances,* and thus directly calls the functions in the web worker that it created* instead of going through this file.+ *+ * ---+ *+ * [Note: Crypto layer API data types]+ *+ * There are two primary types used when exchanging data with these functions:+ *+ * 1. Base64 strings. Unqualified strings are taken as base64 encoded+ * representations of the underlying data.+ *+ * 2. Raw bytes. Uint8Arrays are byte arrays.+ *+ * Where possible and useful, functions also accept a union of these two - a+ * {@link BytesOrB64} where the implementation will automatically convert+ * to/from base64 to bytes if needed, thus saving on unnecessary conversions at+ * the caller side.+ *+ * Apart from these two, there are other secondary and one off types.+ *+ * 1. "Regular" JavaScript strings. These are indicated by the *UTF8 suffix on+ * the function that deals with them. These strings will be obtained by utf-8+ * encoding (or decoding) the underlying bytes.+ *+ * 2. Hex representations of the bytes. These are indicated by the *Hex suffix+ * on the functions dealing with them.+ *+ * 2. JSON values. These are indicated by the *JSON suffix on the functions+ * dealing with them.*/import { ComlinkWorker } from "ente-base/worker/comlink-worker";import { type StateAddress } from "libsodium-wrappers-sumo";@@ -270,6 +300,16 @@ export const decryptBoxBytes = (box: EncryptedBox, key: BytesOrB64) =>? ei._decryptBoxBytes(box, key): sharedWorker().then((w) => w.decryptBoxBytes(box, key));+/**+ * Variant of {@link decryptBoxBytes} that returns the decrypted bytes as a+ * "JavaScript string", specifically a UTF-8 string. That is, after decryption+ * we obtain raw bytes, which we interpret as a UTF-8 string.+ */+export const decryptBoxUTF8 = (box: EncryptedBox, key: BytesOrB64) =>+ inWorker()+ ? ei._decryptBoxUTF8(box, key)+ : sharedWorker().then((w) => w.decryptBoxUTF8(box, key));+/*** Decrypt a blob encrypted using either {@link encryptBlobBytes} or* {@link encryptBlob} and return it as a base64 encoded string.diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.tsindex 8db4d29864..116ce65ca2 100644--- a/web/packages/base/crypto/libsodium.ts+++ b/web/packages/base/crypto/libsodium.ts@@ -501,6 +501,18 @@ export const decryptBox = (key: BytesOrB64,): Promise<string> => decryptBoxBytes(box, key).then(toB64);+/**+ * Variant of {@link decryptBoxBytes} that returns the data after decoding the+ * decrypted bytes as a utf-8 string.+ */+export const decryptBoxUTF8 = async (+ box: EncryptedBox,+ key: BytesOrB64,+): Promise<string> => {+ await sodium.ready;+ return sodium.to_string(await decryptBoxBytes(box, key));+};+/*** Decrypt the result of {@link encryptBlobBytes} or {@link encryptBlob}.*/diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.tsindex 7595ce46a5..74a0da24cd 100644--- a/web/packages/base/crypto/worker.ts+++ b/web/packages/base/crypto/worker.ts@@ -28,6 +28,7 @@ export class CryptoWorker {initChunkEncryption = ei._initChunkEncryption;encryptStreamChunk = ei._encryptStreamChunk;decryptBoxBytes = ei._decryptBoxBytes;+ decryptBoxUTF8 = ei._decryptBoxUTF8;decryptBox = ei._decryptBox;decryptBlobBytes = ei._decryptBlobBytes;decryptBlob = ei._decryptBlob;