Skip to content

armgit / ente

Public Imported from GitHub
0

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

Enc

Manav Rathicf6508be4ad5
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index 156ee30094..d6b395f758 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -3,7 +3,6 @@ import * as libsodium from "./libsodium";
import type {
BytesOrB64,
DecryptBlobB64,
- EncryptB64,
EncryptBytes,
EncryptedBlobB64,
EncryptedBlobBytes,
@@ -11,14 +10,6 @@ import type {
EncryptJSON,
} from "./types";
-const EncryptB64ToBytes = async ({
- dataB64,
- keyB64,
-}: EncryptB64): Promise<EncryptBytes> => ({
- data: await libsodium.fromB64(dataB64),
- keyB64,
-});
-
const EncryptedBlobBytesToB64 = async ({
encryptedData,
decryptionHeaderB64,
@@ -27,8 +18,7 @@ const EncryptedBlobBytesToB64 = async ({
decryptionHeaderB64,
});
-export const _encryptBoxB64 = (r: EncryptB64) =>
- EncryptB64ToBytes(r).then((rb) => libsodium.encryptBox(rb));
+export const _encryptBoxB64 = libsodium.encryptBoxB64;
export const _encryptAssociatedData = libsodium.encryptBlob;
diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.ts
index d2657bb19f..db560d7bbd 100644
--- a/web/packages/base/crypto/ente.ts
+++ b/web/packages/base/crypto/ente.ts
@@ -55,7 +55,6 @@ import type {
BytesOrB64,
DecryptBlobB64,
DecryptBlobBytes,
- EncryptB64,
EncryptBytes,
EncryptedBox2,
EncryptJSON,
@@ -75,7 +74,8 @@ const assertInWorker = <T>(x: T): T => {
};
/**
- * Encrypt arbitrary data using the given key and a randomly generated nonce.
+ * Encrypt the given data, returning a box containing encrypted data and a
+ * randomly generated nonce.
*
* Use {@link decryptBoxB64} to decrypt the result.
*
@@ -86,10 +86,10 @@ const assertInWorker = <T>(x: T): T => {
* >
* > See: [Note: 3 forms of encryption (Box | Blob | Stream)]
*/
-export const encryptBoxB64 = (r: EncryptB64) =>
+export const encryptBoxB64 = (data: BytesOrB64, key: BytesOrB64) =>
inWorker()
- ? ei._encryptBoxB64(r)
- : sharedCryptoWorker().then((w) => w.encryptBoxB64(r));
+ ? ei._encryptBoxB64(data, key)
+ : sharedCryptoWorker().then((w) => w.encryptBoxB64(data, key));
/**
* Encrypt arbitrary data associated with an Ente object (file, collection,
@@ -153,10 +153,10 @@ export const decryptBox = (b: EncryptedBox2, k: BytesOrB64) =>
* Variant of {@link decryptBox} that returns the decrypted data as a base64
* string.
*/
-export const decryptBoxB64 = (b: EncryptedBox2, k: BytesOrB64) =>
+export const decryptBoxB64 = (box: EncryptedBox2, key: BytesOrB64) =>
inWorker()
- ? ei._decryptBoxB64(b, k)
- : sharedCryptoWorker().then((w) => w.decryptBoxB64(b, k));
+ ? ei._decryptBoxB64(box, key)
+ : sharedCryptoWorker().then((w) => w.decryptBoxB64(box, key));
/**
* Decrypt arbitrary data associated with an Ente object (file, collection or
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index a21359b181..5b7d6aa25e 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -18,6 +18,7 @@ import type {
EncryptBytes,
EncryptedBlobBytes,
EncryptedBox2,
+ EncryptedBoxB64,
EncryptedBoxBytes,
} from "./types";
@@ -122,7 +123,22 @@ export async function fromHex(input: string) {
}
/**
- * Encrypt the given data using the provided base64 encoded key.
+ * If the provided {@link bob} ("Bytes or B64 string") is already a
+ * {@link Uint8Array}, return it unchanged, otherwise convert the base64 string
+ * into bytes and return those.
+ */
+const bytes = async (bob: BytesOrB64) =>
+ typeof bob == "string" ? fromB64(bob) : bob;
+
+/**
+ * Encrypt the given data using libsodium's secretbox APIs, using a randomly
+ * generated nonce.
+ *
+ * Use {@link decryptBox} to decrypt the result.
+ *
+ * @param data The data to encrypt.
+ *
+ * @param key The key to use for encryption.
*
* [Note: 3 forms of encryption (Box | Blob | Stream)]
*
@@ -207,6 +223,23 @@ export async function fromHex(input: string) {
*
* 3. Box returns a "nonce", while Blob returns a "header".
*/
+export const encryptBoxB64 = async (
+ data: BytesOrB64,
+ key: BytesOrB64,
+): Promise<EncryptedBoxB64> => {
+ await sodium.ready;
+ const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
+ const encryptedData = sodium.crypto_secretbox_easy(
+ data,
+ nonce,
+ await bytes(key),
+ );
+ return {
+ encryptedData: await toB64(encryptedData),
+ nonce: await toB64(nonce),
+ };
+};
+
export const encryptBox = async ({
data,
keyB64,
@@ -344,14 +377,6 @@ export const decryptBox = async ({
);
};
-/**
- * If the provided {@link bob} ("Bytes or B64 string") is already a
- * {@link Uint8Array}, return it unchanged, otherwise convert the base64 string
- * into bytes and return those.
- */
-const bytes = async (bob: BytesOrB64) =>
- typeof bob == "string" ? fromB64(bob) : bob;
-
/**
* Decrypt the result of {@link encryptBox}.
*/
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index 9fde2fd388..0824fa7286 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -3,6 +3,20 @@
*/
export type BytesOrB64 = Uint8Array | string;
+/**
+ * An encryption request
+ */
+export interface EncryptBytesOrB64 {
+ /**
+ * The data to encrypt.
+ */
+ data: BytesOrB64;
+ /**
+ * The key to use for encryption.
+ */
+ key: BytesOrB64;
+}
+
/**
* An encryption request with the data to encrypt provided as bytes.
*/
@@ -71,6 +85,20 @@ export interface EncryptedBox2 {
nonce: BytesOrB64;
}
+export interface EncryptedBoxB64 {
+ /**
+ * The encrypted data as a base64 string.
+ */
+ encryptedData: string;
+ /**
+ * The nonce that was used during encryption, as a base64 string.
+ *
+ * The nonce is required to decrypt the data, but it does not need to be
+ * kept secret.
+ */
+ nonce: string;
+}
+
/**
* The result of encryption using the secretbox APIs.
*