Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

enc

Manav Rathi6eab85b7e105
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index e990c86d93..dca6ac112b 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -13,17 +13,18 @@ export const _fromHex = libsodium.fromHex;
export const _generateKey = libsodium.generateKey;
export const _generateBlobOrStreamKey = libsodium.generateBlobOrStreamKey;
export const _encryptBox = libsodium.encryptBox;
-export const _encryptBlobBytes = libsodium.encryptBlobBytes;
+export const _encryptBoxUTF8 = libsodium.encryptBoxUTF8;
export const _encryptBlob = libsodium.encryptBlob;
+export const _encryptBlobBytes = libsodium.encryptBlobBytes;
export const _encryptMetadataJSON = libsodium.encryptMetadataJSON;
export const _encryptStreamBytes = libsodium.encryptStreamBytes;
export const _initChunkEncryption = libsodium.initChunkEncryption;
export const _encryptStreamChunk = libsodium.encryptStreamChunk;
+export const _decryptBox = libsodium.decryptBox;
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;
+export const _decryptBlobBytes = libsodium.decryptBlobBytes;
export const _decryptMetadataJSON = libsodium.decryptMetadataJSON;
export const _decryptStreamBytes = libsodium.decryptStreamBytes;
export const _initChunkDecryption = libsodium.initChunkDecryption;
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index e9c24cce94..606dd1d4cb 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -63,9 +63,12 @@
* 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.
+ * representations of the underlying data. Usually, the unqualified "base"
+ * function deals with Base64 strings, since they also are the data type in
+ * which we usually send the encryted data etc to remote.
*
- * 2. Raw bytes. Uint8Arrays are byte arrays.
+ * 2. Raw bytes. Uint8Arrays are byte arrays. The functions that deal with bytes
+ * are indicated by a *Bytes suffix in their name.
*
* Where possible and useful, functions also accept a union of these two - a
* {@link BytesOrB64} where the implementation will automatically convert
@@ -196,6 +199,15 @@ export const encryptBox = (data: BytesOrB64, key: BytesOrB64) =>
? ei._encryptBox(data, key)
: sharedWorker().then((w) => w.encryptBox(data, key));
+/**
+ * A variant of {@link encryptBox} that first UTF-8 encodes the input string to
+ * obtain bytes, which it then encrypts.
+ */
+export const encryptBoxUTF8 = (data: string, key: BytesOrB64) =>
+ inWorker()
+ ? ei._encryptBoxUTF8(data, key)
+ : sharedWorker().then((w) => w.encryptBoxUTF8(data, key));
+
/**
* Encrypt the given data, returning a blob containing the encrypted data and a
* decryption header as base64 strings.
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index 116ce65ca2..ab83b1e10d 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -283,6 +283,18 @@ export const encryptBox = async (
};
};
+/**
+ * A variant of {@link encryptBox} that first converts the input string into
+ * bytes using a UTF-8 encoding, and then encrypts those bytes.
+ */
+export const encryptBoxUTF8 = async (
+ data: string,
+ key: BytesOrB64,
+): Promise<EncryptedBoxB64> => {
+ await sodium.ready;
+ return encryptBox(sodium.from_string(data), key);
+};
+
/**
* Encrypt the given data using libsodium's secretstream APIs without chunking.
*
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index 74a0da24cd..b0bf092b40 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -21,17 +21,18 @@ export class CryptoWorker {
generateKey = ei._generateKey;
generateBlobOrStreamKey = ei._generateBlobOrStreamKey;
encryptBox = ei._encryptBox;
- encryptBlobBytes = ei._encryptBlobBytes;
+ encryptBoxUTF8 = ei._encryptBoxUTF8;
encryptBlob = ei._encryptBlob;
+ encryptBlobBytes = ei._encryptBlobBytes;
encryptMetadataJSON = ei._encryptMetadataJSON;
encryptStreamBytes = ei._encryptStreamBytes;
initChunkEncryption = ei._initChunkEncryption;
encryptStreamChunk = ei._encryptStreamChunk;
+ decryptBox = ei._decryptBox;
decryptBoxBytes = ei._decryptBoxBytes;
decryptBoxUTF8 = ei._decryptBoxUTF8;
- decryptBox = ei._decryptBox;
- decryptBlobBytes = ei._decryptBlobBytes;
decryptBlob = ei._decryptBlob;
+ decryptBlobBytes = ei._decryptBlobBytes;
decryptMetadataJSON = ei._decryptMetadataJSON;
decryptStreamBytes = ei._decryptStreamBytes;
initChunkDecryption = ei._initChunkDecryption;