Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use newer pattern in more places

Manav Rathi360113c3aca2
diff --git a/web/apps/photos/src/services/upload/uploadService.ts b/web/apps/photos/src/services/upload/uploadService.ts
index 42c2706105..fca23c01b0 100644
--- a/web/apps/photos/src/services/upload/uploadService.ts
+++ b/web/apps/photos/src/services/upload/uploadService.ts
@@ -1119,11 +1119,8 @@ const encryptFile = async (
const {
encryptedData: thumbEncryptedData,
- decryptionHeaderB64: thumbDecryptionHeader,
- } = await worker.encryptThumbnail({
- data: file.thumbnail,
- keyB64: fileKey,
- });
+ decryptionHeader: thumbDecryptionHeader,
+ } = await worker.encryptThumbnail(file.thumbnail, fileKey);
const encryptedThumbnail = {
encryptedData: thumbEncryptedData,
decryptionHeader: thumbDecryptionHeader,
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index 305cee31e7..d928d3a8e1 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -1,35 +1,36 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
-import type {
- DecryptBlobB64,
- EncryptBytes,
- EncryptedBlobB64,
- EncryptedBlobBytes,
- EncryptJSON,
-} from "./types";
-
-const EncryptedBlobBytesToB64 = async ({
- encryptedData,
- decryptionHeaderB64,
-}: EncryptedBlobBytes): Promise<EncryptedBlobB64> => ({
- encryptedDataB64: await libsodium.toB64(encryptedData),
- decryptionHeaderB64,
-});
+import type { BytesOrB64, DecryptBlobB64, EncryptJSON } from "./types";
export const _encryptBoxB64 = libsodium.encryptBoxB64;
-export const _encryptAssociatedData = libsodium.encryptBlob;
+export const _encryptBlob = libsodium.encryptBlob;
-export const _encryptThumbnail = _encryptAssociatedData;
+export const _encryptBlobB64 = libsodium.encryptBlobB64;
-export const _encryptAssociatedDataB64 = (r: EncryptBytes) =>
- _encryptAssociatedData(r).then(EncryptedBlobBytesToB64);
+export const _encryptThumbnail = async (data: BytesOrB64, key: BytesOrB64) => {
+ const { encryptedData, decryptionHeader } = await _encryptBlob(data, key);
+ return {
+ encryptedData,
+ decryptionHeader: await libsodium.toB64(decryptionHeader),
+ };
+};
-export const _encryptMetadataJSON = ({ jsonValue, keyB64 }: EncryptJSON) =>
- _encryptAssociatedDataB64({
- data: new TextEncoder().encode(JSON.stringify(jsonValue)),
+export const _encryptMetadataJSON_New = ({ jsonValue, keyB64 }: EncryptJSON) =>
+ _encryptBlobB64(
+ new TextEncoder().encode(JSON.stringify(jsonValue)),
keyB64,
- });
+ );
+
+export const _encryptMetadataJSON = async (r: EncryptJSON) => {
+ // Deprecated. Keep the old API for now.
+ const { encryptedData, decryptionHeader } =
+ await _encryptMetadataJSON_New(r);
+ return {
+ encryptedDataB64: encryptedData,
+ decryptionHeaderB64: decryptionHeader,
+ };
+};
export const _decryptBox = libsodium.decryptBox2;
diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.ts
index 01682c2ec5..9265f615a9 100644
--- a/web/packages/base/crypto/ente.ts
+++ b/web/packages/base/crypto/ente.ts
@@ -55,7 +55,6 @@ import type {
BytesOrB64,
DecryptBlobB64,
DecryptBlobBytes,
- EncryptBytes,
EncryptedBox2,
EncryptJSON,
} from "./types";
@@ -92,34 +91,40 @@ export const encryptBoxB64 = (data: BytesOrB64, key: BytesOrB64) =>
: sharedCryptoWorker().then((w) => w.encryptBoxB64(data, key));
/**
- * Encrypt arbitrary data associated with an Ente object (file, collection,
- * entity) using the object's key.
+ * Encrypt the given data, returning a blob containing the encrypted data and a
+ * decryption header.
+ *
+ * This function is usually used to encrypt data associated with an Ente object
+ * (file, collection, entity) using the object's key.
*
- * Use {@link decryptAssociatedData} to decrypt the result.
+ * Use {@link decryptBlobB64} to decrypt the result.
*
- * See {@link encryptBlob} for the implementation details.
+ * > The suffix "Blob" comes from our convention of naming functions that use
+ * > the secretstream APIs in one-shot mode.
+ * >
+ * > See: [Note: 3 forms of encryption (Box | Blob | Stream)]
*/
-export const encryptAssociatedData = (r: EncryptBytes) =>
- assertInWorker(ei._encryptAssociatedData(r));
+export const encryptBlob = (data: BytesOrB64, key: BytesOrB64) =>
+ assertInWorker(ei._encryptBlob(data, key));
/**
- * Encrypt the thumbnail for a file.
- *
- * This is just an alias for {@link encryptAssociatedData}.
- *
- * Use {@link decryptThumbnail} to decrypt the result.
+ * A variant of {@link encryptBlob} that returns the result components as base64
+ * strings.
*/
-export const encryptThumbnail = (r: EncryptBytes) =>
- assertInWorker(ei._encryptThumbnail(r));
+export const encryptBlobB64 = (data: BytesOrB64, key: BytesOrB64) =>
+ assertInWorker(ei._encryptBlobB64(data, key));
/**
- * A variant of {@link encryptAssociatedData} that returns the encrypted data as
- * a base64 string instead of returning its bytes.
+ * Encrypt the thumbnail for a file.
*
- * Use {@link decryptAssociatedDataB64} to decrypt the result.
+ * This is midway variant of {@link encryptBlob} and {@link encryptBlobB64} that
+ * returns the decryption header as a base64 string, but leaves the data
+ * unchanged.
+ *
+ * Use {@link decryptThumbnail} to decrypt the result.
*/
-export const encryptAssociatedDataB64 = (r: EncryptBytes) =>
- assertInWorker(ei._encryptAssociatedDataB64(r));
+export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) =>
+ assertInWorker(ei._encryptThumbnail(data, key));
/**
* Encrypt the JSON metadata 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 bb19f53f6e..73e96a6078 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -16,7 +16,8 @@ import type {
DecryptBlobBytes,
DecryptBoxBytes,
EncryptBytes,
- EncryptedBlobBytes,
+ EncryptedBlob_2,
+ EncryptedBlobB64_2,
EncryptedBox2,
EncryptedBoxB64,
EncryptedBoxBytes,
@@ -140,6 +141,8 @@ const bytes = async (bob: BytesOrB64) =>
*
* @param key The key to use for encryption.
*
+ * @returns The encrypted data and the generated nonce, both as base64 strings.
+ *
* [Note: 3 forms of encryption (Box | Blob | Stream)]
*
* libsodium provides two "high level" encryption patterns:
@@ -240,7 +243,8 @@ export const encryptBoxB64 = async (
};
};
-export const encryptBox = async ({
+/** deprecated + needs rename */
+const encryptBox = async ({
data,
keyB64,
}: EncryptBytes): Promise<EncryptedBoxBytes> => {
@@ -255,22 +259,27 @@ export const encryptBox = async ({
};
/**
- * Encrypt the given data using secretstream APIs in one-shot mode, using the
- * given base64 encoded key.
+ * Encrypt the given data using libsodium's secretstream APIs in one-shot mode.
*
* Use {@link decryptBlob} to decrypt the result.
*
- * See: [Note: 3 forms of encryption (Box | Blob | Stream)].
+ * @param data The data to encrypt.
+ *
+ * @param key The key to use for encryption.
+ *
+ * @returns The encrypted data and the decryption header as {@link Uint8Array}s.
*
- * See: https://doc.libsodium.org/secret-key_cryptography/secretstream
+ * - See: [Note: 3 forms of encryption (Box | Blob | Stream)].
+ *
+ * - See: https://doc.libsodium.org/secret-key_cryptography/secretstream
*/
-export const encryptBlob = async ({
- data,
- keyB64,
-}: EncryptBytes): Promise<EncryptedBlobBytes> => {
+export const encryptBlob = async (
+ data: BytesOrB64,
+ key: BytesOrB64,
+): Promise<EncryptedBlob_2> => {
await sodium.ready;
- const uintkey: Uint8Array = await fromB64(keyB64);
+ const uintkey = await bytes(key);
const initPushResult =
sodium.crypto_secretstream_xchacha20poly1305_init_push(uintkey);
const [pushState, header] = [initPushResult.state, initPushResult.header];
@@ -283,10 +292,26 @@ export const encryptBlob = async ({
);
return {
encryptedData: pushResult,
- decryptionHeaderB64: await toB64(header),
+ decryptionHeader: header,
};
};
+/**
+ * A variant of {@link encryptBlob} that returns the both the encrypted data and
+ * decryption header as base64 strings.
+ */
+export const encryptBlobB64 = async (
+ data: BytesOrB64,
+ key: BytesOrB64,
+): Promise<EncryptedBlobB64_2> => {
+ const { encryptedData, decryptionHeader} = await encryptBlob(data, key);
+ return {
+ encryptedData: await toB64(encryptedData),
+ decryptionHeader: await toB64(decryptionHeader),
+ };
+};
+
+
export const ENCRYPTION_CHUNK_SIZE = 4 * 1024 * 1024;
export const encryptChaCha = async (data: Uint8Array) => {
diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts
index 8795267338..19e4e1e7eb 100644
--- a/web/packages/base/crypto/types.ts
+++ b/web/packages/base/crypto/types.ts
@@ -100,6 +100,48 @@ export interface EncryptedBoxB64 {
nonce: string;
}
+/**
+ * The result of encryption using the secretstream APIs in one-shot mode.
+ *
+ * It contains an encrypted data and a header that should be provided during
+ * decryption. The header does not need to be secret.
+ *
+ * See: [Note: 3 forms of encryption (Box | Blob | Stream)].
+ */
+export interface EncryptedBlob_2 {
+ /**
+ * The encrypted data.
+ */
+ encryptedData: Uint8Array;
+ /**
+ * The decryption header.
+ *
+ * While the exact contents of the header are libsodium's internal details,
+ * it effectively contains a random nonce generated by libsodium. It does
+ * not need to be secret, but it is required to decrypt the data.
+ */
+ decryptionHeader: Uint8Array;
+}
+
+/**
+ * A variant of {@link EncryptedBlob_2} that has the encrypted data and header
+ * as base64 strings.
+ */
+export interface EncryptedBlobB64_2 {
+ /**
+ * The encrypted data as a base64 string.
+ */
+ encryptedData: string;
+ /**
+ * A base64 string containing the decryption header.
+ *
+ * While the exact contents of the header are libsodium's internal details,
+ * it effectively contains a random nonce generated by libsodium. It does
+ * not need to be secret, but it is required to decrypt the data.
+ */
+ decryptionHeader: string;
+}
+
/**
* The result of encryption using the secretbox APIs.
*
diff --git a/web/packages/new/photos/services/file-data.ts b/web/packages/new/photos/services/file-data.ts
index 0b3f1ef0b8..df4ddbf9f4 100644
--- a/web/packages/new/photos/services/file-data.ts
+++ b/web/packages/new/photos/services/file-data.ts
@@ -1,4 +1,4 @@
-import { encryptAssociatedDataB64 } from "@/base/crypto/ente";
+import { encryptBlobB64 } from "@/base/crypto/ente";
import { authenticatedRequestHeaders, ensureOk } from "@/base/http";
import { apiURL } from "@/base/origins";
import type { EnteFile } from "@/new/photos/types/file";
@@ -94,8 +94,10 @@ export const putFileData = async (
type: FileDataType,
data: Uint8Array,
) => {
- const { encryptedDataB64, decryptionHeaderB64 } =
- await encryptAssociatedDataB64({ data: data, keyB64: enteFile.key });
+ const { encryptedData, decryptionHeader } = await encryptBlobB64(
+ data,
+ enteFile.key,
+ );
const res = await fetch(await apiURL("/files/data"), {
method: "PUT",
@@ -103,8 +105,8 @@ export const putFileData = async (
body: JSON.stringify({
fileID: enteFile.id,
type,
- encryptedData: encryptedDataB64,
- decryptionHeader: decryptionHeaderB64,
+ encryptedData,
+ decryptionHeader,
}),
});
ensureOk(res);