π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Integrate
Manav Rathi4e49a352be0c
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.tsindex e8580ae008..7d23f01f60 100644--- a/web/packages/base/crypto/ente-impl.ts+++ b/web/packages/base/crypto/ente-impl.ts@@ -1,30 +1,25 @@/** Careful when adding add other imports! */import * as libsodium from "./libsodium";+import type { EncryptBytes, EncryptJSON } from "./types";-export const encryptAssociatedDataI = libsodium.encryptChaChaOneShot;+export const _encryptAssociatedData = libsodium.encryptChaChaOneShot;-export const encryptThumbnailI = encryptAssociatedDataI;+export const _encryptThumbnail = _encryptAssociatedData;-export const encryptFileEmbeddingI = async (- data: Uint8Array,- keyB64: string,-) => {- const { encryptedData, decryptionHeaderB64 } = await encryptAssociatedDataI(- data,- keyB64,- );+export const _encryptFileEmbedding = async (r: EncryptBytes) => {+ const { encryptedData, decryptionHeaderB64 } =+ await _encryptAssociatedData(r);return {encryptedDataB64: await libsodium.toB64(encryptedData),decryptionHeaderB64,};};-export const encryptMetadataI = async (metadata: unknown, keyB64: string) => {- const encodedMetadata = new TextEncoder().encode(JSON.stringify(metadata));+export const _encryptMetadata = async ({ jsonValue, keyB64 }: EncryptJSON) => {+ const data = new TextEncoder().encode(JSON.stringify(jsonValue));- const { encryptedData, decryptionHeaderB64 } = await encryptAssociatedDataI(- encodedMetadata,- keyB64,+ const { encryptedData, decryptionHeaderB64 } = await _encryptAssociatedData(+ { data, keyB64 },);return {encryptedDataB64: await libsodium.toB64(encryptedData),@@ -32,7 +27,7 @@ export const encryptMetadataI = async (metadata: unknown, keyB64: string) => {};};-export const decryptAssociatedDataI = libsodium.decryptChaChaOneShot;+export const _decryptAssociatedData = libsodium.decryptChaChaOneShot;export const decryptThumbnailI = decryptAssociatedDataI;diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.tsindex 98d768ba39..7897288cbb 100644--- a/web/packages/base/crypto/ente.ts+++ b/web/packages/base/crypto/ente.ts@@ -17,7 +17,7 @@* sodium.ready has been called before accessing libsodium's APIs, thus all the* functions it exposes are async.*- * The final layer is this file, `crypto/ente.ts`. These are usually thin+ * The highest layer is this file, `crypto/ente.ts`. These are usually simple* compositions of functionality exposed by `crypto/libsodium.ts`, but the* difference is that the functions in ente.ts don't talk in terms of the crypto* algorithms, but rather in terms the higher-level Ente specific goal we are@@ -30,7 +30,7 @@* to another worker is wasteful.** To handle both these scenario, each function in this file is split into the- * external API, and the underlying implementation (denoted by an "I" suffix).+ * external API, and the underlying implementation (denoted by an "_" prefix).* The external API functions check to see if we're already in a web worker, and* if so directly invoke the implementation. Otherwise the call the sibling* function in a shared "crypto" web worker (which then invokes the@@ -51,12 +51,16 @@ import { assertionFailed } from "../assert";import { inWorker } from "../env";import * as ei from "./ente-impl";import * as libsodium from "./libsodium";+import type { EncryptBytes } from "./types";import { sharedCryptoWorker } from "./worker";/**- * Some functions we haven't yet needed to use on the main thread. These don't- * have a corresponding sharedCryptoWorker interface. This assertion will let us- * know when we need to implement them (in production it'll just log a warning).+ * Some of these functions have not yet been needed on the main thread, and for+ * these we don't have a corresponding sharedCryptoWorker method.+ *+ * This assertion will let us know when we need to implement them (this'll+ * gracefully degrade in production: the functionality will work, just that the+ * crypto will happen on the main thread itself).*/const assertInWorker = <T>(x: T): T => {if (!inWorker()) assertionFailed("Currently only usable in a web worker");@@ -70,32 +74,17 @@ const assertInWorker = <T>(x: T): T => {* Use {@link decryptAssociatedData} to decrypt the result.** See {@link encryptChaChaOneShot} for the implementation details.- *- * @param data A {@link Uint8Array} containing the bytes to encrypt.- *- * @param keyB64 base64 string containing the encryption key. This is expected- * to the key of the object with which {@link data} is associated. For example,- * if this is data associated with a file, then this will be the file's key.- *- * @returns The encrypted data and the (base64 encoded) decryption header.*/-export const encryptAssociatedData = (data: Uint8Array, keyB64: string) =>- assertInWorker(ei.encryptAssociatedDataI(data, keyB64));+export const encryptAssociatedData = (r: EncryptBytes) =>+ assertInWorker(ei._encryptAssociatedData(r));/*** Encrypt the thumbnail for a file.** This is just an alias for {@link encryptAssociatedData}.- *- * @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).*/-export const encryptThumbnail = (data: Uint8Array, keyB64: string) =>- assertInWorker(ei.encryptThumbnailI(data, keyB64));+export const encryptThumbnail = (r: EncryptBytes) =>+ assertInWorker(ei._encryptThumbnail(r));/*** Encrypted the embedding associated with a file using the file's key.@@ -152,7 +141,8 @@ export const encryptMetadata = async (metadata: unknown, keyB64: string) =>** @returns The decrypted bytes.*/-export const decryptAssociatedData = ( encryptedData: Uint8Array,+export const decryptAssociatedData = (+ encryptedData: Uint8Array,headerB64: string,keyB64: string,) => libsodium.decryptChaChaOneShot;diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.tsindex cf30256a86..f8be7c147c 100644--- a/web/packages/base/crypto/libsodium.ts+++ b/web/packages/base/crypto/libsodium.ts@@ -11,6 +11,7 @@import { mergeUint8Arrays } from "@/utils/array";import { CustomError } from "@ente/shared/error";import sodium, { type StateAddress } from "libsodium-wrappers";+import type { DecryptBytes, EncryptBytes, EncryptedBytes } from "./types";/*** Convert bytes ({@link Uint8Array}) to a base64 string.@@ -113,7 +114,7 @@ export async function fromHex(input: string) {}/**- * Encrypt the given {@link data} using the given (base64 encoded) key.+ * Encrypt the given data using the given (base64 encoded) key.** Use {@link decryptChaChaOneShot} to decrypt the result.*@@ -138,7 +139,7 @@ export async function fromHex(input: string) {** See: https://doc.libsodium.org/secret-key_cryptography/secretbox*- * The difference here is that this function is meant to used for data+ * The difference to those is that this function is meant to used for data* associated with a file (or some other Ente object, like a collection or an* entity). There is no technical reason to do it that way, just this way all* data associated with a file, including its actual contents, use the same@@ -155,10 +156,10 @@ export async function fromHex(input: string) {* encoded string). Both these values are needed to decrypt the data. The header* does not need to be secret.*/-export const encryptChaChaOneShot = async (- data: Uint8Array,- keyB64: string,-) => {+export const encryptChaChaOneShot = async ({+ data,+ keyB64,+}: EncryptBytes): Promise<EncryptedBytes> => {await sodium.ready;const uintkey: Uint8Array = await fromB64(keyB64);@@ -254,27 +255,15 @@ export async function encryptFileChunk(/*** Decrypt the result of {@link encryptChaChaOneShot}.- *- * @param encryptedData A {@link Uint8Array} containing the bytes to decrypt.- *- * @param header A base64 string containing the bytes of the decryption header- * that was produced during encryption.- *- * @param keyB64 The base64 string containing the key that was used to encrypt- * the data.- *- * @returns The decrypted bytes.- *- * @returns The decrypted metadata bytes.*/-export const decryptChaChaOneShot = async (- encryptedData: Uint8Array,- headerB64: string,- keyB64: string,-) => {+export const decryptChaChaOneShot = async ({+ encryptedData,+ decryptionHeaderB64,+ keyB64,+}: DecryptBytes): Promise<Uint8Array> => {await sodium.ready;const pullState = sodium.crypto_secretstream_xchacha20poly1305_init_pull(- await fromB64(headerB64),+ await fromB64(decryptionHeaderB64),await fromB64(keyB64),);const pullResult = sodium.crypto_secretstream_xchacha20poly1305_pull(diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.tsindex 1411259c4d..a1719ff0c7 100644--- a/web/packages/base/crypto/types.ts+++ b/web/packages/base/crypto/types.ts@@ -29,6 +29,45 @@ export interface EncryptJSON {keyB64: string;}+/**+ * The result of encryption using the stream APIs used in one-shot mode.+ *+ * 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 interface EncryptedBytes {+ /**+ * A {@link Uint8Array} containing the encrypted data.+ */+ encryptedData: Uint8Array;+ /**+ * A base64 string containing the decryption header.+ *+ * The header contains a random nonce and other libsodium specific metadata.+ * It does not need to be secret, but it is required to decrypt the data.+ */+ decryptionHeaderB64: string;+}++/**+ * The result of encryption using the stream APIs used in one-shot mode, with+ * the encrypted data encoded as a base64 string.+ */+export interface EncryptedB64 {+ /**+ * A base64 string containing the encrypted data.+ */+ encryptedDataB64: string;+ /**+ * A base64 string containing the decryption header.+ *+ * The header contains a random nonce and other libsodium specific metadata.+ * It does not need to be secret, but it is required to decrypt the data.+ */+ decryptionHeaderB64: string;+}+/*** A decryption request with the encrypted data as a base64 encoded string.*/