Skip to content

armgit / ente

Public Imported from GitHub
0

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

Forward

Manav Rathie36aad9f7c19
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index 82b5e41152..a04c0fb0ca 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -83,3 +83,9 @@ export const _decryptMetadataJSON = async (r: {
},
r.keyB64,
);
+
+export const _generateKeyPair = libsodium.generateKeyPair;
+
+export const _boxSeal = libsodium.boxSeal;
+
+export const _boxSealOpen = libsodium.boxSealOpen;
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index f3f69e4565..8ad977b882 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -1,6 +1,9 @@
/**
* @file Higher level functions that use the ontology of Ente's requirements.
*
+ * For more detailed documentation of specific functions, see the corresponding
+ * function in `libsodium.ts`.
+ *
* [Note: Crypto code hierarchy]
*
* 1. @/base/crypto (Crypto API for our code)
@@ -172,8 +175,6 @@ export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) =>
/**
* Encrypt the given data using chunked streaming encryption, but process all
* the chunks in one go.
- *
- * For more details, see {@link encryptStreamBytes} in `libsodium.ts`.
*/
export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) =>
inWorker()
@@ -182,8 +183,6 @@ export const encryptStreamBytes = async (data: Uint8Array, key: BytesOrB64) =>
/**
* Prepare for chunked streaming encryption using {@link encryptStreamChunk}.
- *
- * For more details, see {@link initChunkEncryption} in `libsodium.ts`.
*/
export const initChunkEncryption = async (key: BytesOrB64) =>
inWorker()
@@ -192,8 +191,6 @@ export const initChunkEncryption = async (key: BytesOrB64) =>
/**
* Encrypt a chunk as part of a chunked streaming encryption.
- *
- * For more details, see {@link encryptStreamChunk} in `libsodium.ts`.
*/
export const encryptStreamChunk = async (
data: Uint8Array,
@@ -346,3 +343,33 @@ export const decryptMetadataJSON = (r: {
inWorker()
? ei._decryptMetadataJSON(r)
: sharedCryptoWorker().then((w) => w.decryptMetadataJSON(r));
+
+/**
+ * Generate a new public/private keypair for use with the boxSeal* functions.
+ */
+export const generateKeyPair = async () =>
+ inWorker()
+ ? ei._generateKeyPair()
+ : sharedCryptoWorker().then((w) => w.generateKeyPair());
+
+/**
+ * Public key encryption.
+ */
+export const boxSeal = async (data: string, publicKey: string) =>
+ inWorker()
+ ? ei._boxSeal(data, publicKey)
+ : sharedCryptoWorker().then((w) => w.boxSeal(data, publicKey));
+
+/**
+ * Decrypt the result of {@link boxSeal}.
+ */
+export const boxSealOpen = async (
+ encryptedData: string,
+ publicKey: string,
+ secretKey: string,
+) =>
+ inWorker()
+ ? ei._boxSealOpen(encryptedData, publicKey, secretKey)
+ : sharedCryptoWorker().then((w) =>
+ w.boxSealOpen(encryptedData, publicKey, secretKey),
+ );
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index de70f4e99a..d10bd10a78 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -707,14 +707,14 @@ export const boxSeal = async (data: string, publicKey: string) => {
* underlying data.
*/
export const boxSealOpen = async (
- input: string,
+ encryptedData: string,
publicKey: string,
secretKey: string,
) => {
await sodium.ready;
return toB64(
sodium.crypto_box_seal_open(
- await fromB64(input),
+ await fromB64(encryptedData),
await fromB64(publicKey),
await fromB64(secretKey),
),
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index 265dbe1c68..a5a16fc767 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -33,6 +33,9 @@ export class CryptoWorker {
decryptStreamChunk = ei._decryptStreamChunk;
decryptMetadataJSON_New = ei._decryptMetadataJSON_New;
decryptMetadataJSON = ei._decryptMetadataJSON;
+ generateKeyPair = ei._generateKeyPair;
+ boxSeal = ei._boxSeal;
+ boxSealOpen = ei._boxSealOpen;
// TODO: -- AUDIT BELOW --
@@ -93,18 +96,6 @@ export class CryptoWorker {
return libsodium.generateSaltToDeriveKey();
}
- async generateKeyPair() {
- return libsodium.generateKeyPair();
- }
-
- async boxSealOpen(input: string, publicKey: string, secretKey: string) {
- return libsodium.boxSealOpen(input, publicKey, secretKey);
- }
-
- async boxSeal(input: string, publicKey: string) {
- return libsodium.boxSeal(input, publicKey);
- }
-
async generateSubKey(
key: string,
subKeyLength: number,