Skip to content

armgit / ente

Public Imported from GitHub
0

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

Rename and prune

Manav Rathi9bce3bba7c4c
diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.ts
index ff7b5a1de6..a7275c596b 100644
--- a/web/packages/base/crypto/ente.ts
+++ b/web/packages/base/crypto/ente.ts
@@ -23,10 +23,11 @@ export const encryptFileMetadata = async (
metadata: Uint8Array,
keyB64: string,
) => {
- const file = await libsodium.encryptChaChaOneShot(metadata, keyB64);
+ const { encryptedData, decryptionHeaderB64 } =
+ await libsodium.encryptChaChaOneShot(metadata, keyB64);
return {
- encryptedMetadataB64: await libsodium.toB64(file.encryptedData),
- decryptionHeaderB64: file.decryptionHeader,
+ encryptedMetadataB64: await libsodium.toB64(encryptedData),
+ decryptionHeaderB64,
};
};
diff --git a/web/packages/shared/crypto/internal/crypto.worker.ts b/web/packages/shared/crypto/internal/crypto.worker.ts
index 4366625b22..bada1d6958 100644
--- a/web/packages/shared/crypto/internal/crypto.worker.ts
+++ b/web/packages/shared/crypto/internal/crypto.worker.ts
@@ -49,40 +49,34 @@ export class DedicatedCryptoWorker {
async encryptMetadata(metadata: Object, key: string) {
const encodedMetadata = textEncoder.encode(JSON.stringify(metadata));
- const encryptedMetadata = await libsodium.encryptChaChaOneShot(
- encodedMetadata,
- key,
- );
- const { encryptedData, ...other } = encryptedMetadata;
+ const { encryptedData, decryptionHeaderB64 } =
+ await libsodium.encryptChaChaOneShot(encodedMetadata, key);
return {
file: {
encryptedData: await libsodium.toB64(encryptedData),
- ...other,
+ // TODO:
+ decryptionHeader: decryptionHeaderB64,
},
key,
};
}
- async encryptThumbnail(fileData: Uint8Array, key: string) {
- return libsodium.encryptChaChaOneShot(fileData, key);
- }
-
- async encryptEmbedding(embedding: Float32Array, key: string) {
- const encodedEmbedding = textEncoder.encode(
- JSON.stringify(Array.from(embedding)),
- );
- const encryptEmbedding = await libsodium.encryptChaChaOneShot(
- encodedEmbedding,
- key,
- );
- const { encryptedData, ...other } = encryptEmbedding;
- return {
- file: {
- encryptedData: await libsodium.toB64(encryptedData),
- ...other,
- },
- key,
- };
+ /**
+ * Encrypt the thumbnail associated with a file.
+ *
+ * This defers to {@link encryptChaChaOneShot}.
+ *
+ * @param data The thumbnail's data.
+ *
+ * @param keyB64 The key associated with the file whose thumbnail this is.
+ *
+ * @returns The encrypted thumbnail, and the associated decryption header
+ * (Base64 encoded).
+ */
+ async encryptThumbnail(data: Uint8Array, keyB64: string) {
+ const { encryptedData, decryptionHeaderB64: decryptionHeader } =
+ await libsodium.encryptChaChaOneShot(data, keyB64);
+ return { encryptedData, decryptionHeader };
}
async encryptFile(fileData: Uint8Array) {
diff --git a/web/packages/shared/crypto/internal/libsodium.ts b/web/packages/shared/crypto/internal/libsodium.ts
index 4f1b1c2497..d553d8bbba 100644
--- a/web/packages/shared/crypto/internal/libsodium.ts
+++ b/web/packages/shared/crypto/internal/libsodium.ts
@@ -115,8 +115,11 @@ export async function fromHex(input: string) {
*
* This uses the same stream encryption algorithm pair (XChaCha20 stream cipher
* with Poly1305 MAC authentication) that we use for encrypting, well, other
- * streams, like the file's contents. The difference here is that this function
- * does a one shot instead of a streaming encryption.
+ * streams, like the file's contents.
+ *
+ * The difference here is that this function does a one shot instead of a
+ * streaming encryption. As such, this is only meant to be used for relatively
+ * small amounts of data.
*
* Ref: https://doc.libsodium.org/secret-key_cryptography/secretstream
*
@@ -126,13 +129,17 @@ export async function fromHex(input: string) {
* @param keyB64 Base64 encoded string containing the encryption key. This is
* usually be the key associated with a file to which {@link data} relates to.
*
- * @returns The encrypted data and decryption header pair. Both these values are
- * needed to decrypt the data. The header does not need to be secret.
+ * @returns The encrypted data (bytes) and decryption header pair (Base64
+ * encoded string). Both these values are needed to decrypt the data. The header
+ * does not need to be secret.
*/
-export async function encryptChaChaOneShot(data: Uint8Array, key: string) {
+export const encryptChaChaOneShot = async (
+ data: Uint8Array,
+ keyB64: string,
+) => {
await sodium.ready;
- const uintkey: Uint8Array = await fromB64(key);
+ const uintkey: Uint8Array = await fromB64(keyB64);
const initPushResult =
sodium.crypto_secretstream_xchacha20poly1305_init_push(uintkey);
const [pushState, header] = [initPushResult.state, initPushResult.header];
@@ -145,9 +152,9 @@ export async function encryptChaChaOneShot(data: Uint8Array, key: string) {
);
return {
encryptedData: pushResult,
- decryptionHeader: await toB64(header),
+ decryptionHeaderB64: await toB64(header),
};
-}
+};
export const ENCRYPTION_CHUNK_SIZE = 4 * 1024 * 1024;