π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Terminology
Manav Rathi7e03462891b9
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.tsindex 6339a306ae..2b65831fb9 100644--- a/web/packages/base/crypto/ente-impl.ts+++ b/web/packages/base/crypto/ente-impl.ts@@ -1,27 +1,27 @@/** Careful when adding add other imports! */import * as libsodium from "./libsodium";import type {- DecryptB64,+ DecryptBlobB64,EncryptBytes,- EncryptedB64,- EncryptedBytes,+ EncryptedBlobB64,+ EncryptedBlobBytes,EncryptJSON,} from "./types";-const EncryptedBytesToB64 = async ({+const EncryptedBlobBytesToB64 = async ({encryptedData,decryptionHeaderB64,-}: EncryptedBytes): Promise<EncryptedB64> => ({+}: EncryptedBlobBytes): Promise<EncryptedBlobB64> => ({encryptedDataB64: await libsodium.toB64(encryptedData),decryptionHeaderB64,});-export const _encryptAssociatedData = libsodium.encryptChaChaOneShot;+export const _encryptAssociatedData = libsodium.encryptBlob;export const _encryptThumbnail = _encryptAssociatedData;export const _encryptAssociatedB64Data = (r: EncryptBytes) =>- _encryptAssociatedData(r).then(EncryptedBytesToB64);+ _encryptAssociatedData(r).then(EncryptedBlobBytesToB64);export const _encryptMetadataJSON = ({ jsonValue, keyB64 }: EncryptJSON) =>_encryptAssociatedB64Data({@@ -29,7 +29,7 @@ export const _encryptMetadataJSON = ({ jsonValue, keyB64 }: EncryptJSON) =>keyB64,});-export const _decryptAssociatedData = libsodium.decryptChaChaOneShot;+export const _decryptAssociatedData = libsodium.decryptBlob;export const _decryptThumbnail = _decryptAssociatedData;@@ -37,14 +37,14 @@ export const _decryptAssociatedB64Data = async ({encryptedDataB64,decryptionHeaderB64,keyB64,-}: DecryptB64) =>+}: DecryptBlobB64) =>await _decryptAssociatedData({encryptedData: await libsodium.fromB64(encryptedDataB64),decryptionHeaderB64,keyB64,});-export const _decryptMetadataJSON = async (r: DecryptB64) =>+export const _decryptMetadataJSON = async (r: DecryptBlobB64) =>JSON.parse(new TextDecoder().decode(await _decryptAssociatedB64Data(r)),) as unknown;diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.tsindex b5e7fa0c1e..18035ed85f 100644--- a/web/packages/base/crypto/ente.ts+++ b/web/packages/base/crypto/ente.ts@@ -52,8 +52,8 @@ import { assertionFailed } from "../assert";import { inWorker } from "../env";import * as ei from "./ente-impl";import type {- DecryptB64,- DecryptBytes,+ DecryptBlobB64,+ DecryptBlobBytes,EncryptBytes,EncryptJSON,} from "./types";@@ -77,7 +77,7 @@ const assertInWorker = <T>(x: T): T => {** Use {@link decryptAssociatedData} to decrypt the result.*- * See {@link encryptChaChaOneShot} for the implementation details.+ * See {@link encryptBlob} for the implementation details.*/export const encryptAssociatedData = (r: EncryptBytes) =>assertInWorker(ei._encryptAssociatedData(r));@@ -127,9 +127,9 @@ export const encryptMetadataJSON = async (r: EncryptJSON) =>** This is the sibling of {@link encryptAssociatedData}.*- * See {@link decryptChaChaOneShot} for the implementation details.+ * See {@link decryptBlob} for the implementation details.*/-export const decryptAssociatedData = (r: DecryptBytes) =>+export const decryptAssociatedData = (r: DecryptBlobBytes) =>assertInWorker(ei._decryptAssociatedData(r));/**@@ -137,7 +137,7 @@ export const decryptAssociatedData = (r: DecryptBytes) =>** This is the sibling of {@link encryptThumbnail}.*/-export const decryptThumbnail = (r: DecryptBytes) =>+export const decryptThumbnail = (r: DecryptBlobBytes) =>assertInWorker(ei._decryptThumbnail(r));/**@@ -146,7 +146,7 @@ export const decryptThumbnail = (r: DecryptBytes) =>** This is the sibling of {@link decryptAssociatedB64Data}.*/-export const decryptAssociatedB64Data = (r: DecryptB64) =>+export const decryptAssociatedB64Data = (r: DecryptBlobB64) =>inWorker()? ei._decryptAssociatedB64Data(r): sharedCryptoWorker().then((w) => w.decryptAssociatedB64Data(r));@@ -158,7 +158,7 @@ export const decryptAssociatedB64Data = (r: DecryptB64) =>* @returns The decrypted JSON value. Since TypeScript does not have a native* JSON type, we need to return it as an `unknown`.*/-export const decryptMetadataJSON = (r: DecryptB64) =>+export const decryptMetadataJSON = (r: DecryptBlobB64) =>inWorker()? ei._decryptMetadataJSON(r): sharedCryptoWorker().then((w) => w.decryptMetadataJSON(r));diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.tsindex 95e05f66a5..2c8403a2ec 100644--- a/web/packages/base/crypto/libsodium.ts+++ b/web/packages/base/crypto/libsodium.ts@@ -11,7 +11,11 @@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";+import type {+ DecryptBlobBytes,+ EncryptBytes,+ EncryptedBlobBytes,+} from "./types";/*** Convert bytes ({@link Uint8Array}) to a base64 string.@@ -116,7 +120,7 @@ export async function fromHex(input: string) {/*** Encrypt the given data using the provided base64 encoded key.*- * [Note: 3 forms of encryption]+ * [Note: 3 forms of encryption (Box | Blob | Stream)]** libsodium provides two "high level" encryption patterns:*@@ -201,38 +205,14 @@ export async function fromHex(input: string) {*//**- * Encrypt the given file data using the given (base64 encoded) key.+ * Encrypt the given data using stream APIs in one-shot mode, using the given+ * base64 encoded key.*- * Use {@link decryptChaChaOneShot} to decrypt the result.+ * Use {@link decryptBlob} to decrypt the result.*- * [Note: Salsa and ChaCha]+ * - See: [Note: 3 forms of encryption (Box | Blob | Stream)].*- * This uses the same stream encryption algorithm (XChaCha20 stream cipher with- * Poly1305 MAC authentication) that we use for encrypting other streams, in- * particular the actual file's contents.- *- * The difference here is that this function does a one shot instead of a- * streaming encryption. This is only meant to be used for relatively small- * amounts of data (few MBs).- *- * See: https://doc.libsodium.org/secret-key_cryptography/secretstream- *- * Libsodium also provides the `crypto_secretbox_easy` APIs for one shot- * encryption, which we do use in other places where we need to one shot- * encryption of independent bits of data.- *- * These secretbox APIs use XSalsa20 with Poly1305. XSalsa20 is a minor variant- * (predecessor in fact) of XChaCha20.- *- * See: https://doc.libsodium.org/secret-key_cryptography/secretbox- *- * 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- * underlying (streaming) libsodium APIs. In other cases, where we have free- * standing independent data, we continue using the secretbox APIs for one shot- * encryption and decryption.+ * - See: https://doc.libsodium.org/secret-key_cryptography/secretstream** @param data A {@link Uint8Array} containing the bytes that we want to* encrypt.@@ -243,10 +223,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 ({+export const encryptBlob = async ({data,keyB64,-}: EncryptBytes): Promise<EncryptedBytes> => {+}: EncryptBytes): Promise<EncryptedBlobBytes> => {await sodium.ready;const uintkey: Uint8Array = await fromB64(keyB64);@@ -341,13 +321,13 @@ export async function encryptFileChunk(}/**- * Decrypt the result of {@link encryptChaChaOneShot}.+ * Decrypt the result of {@link encryptBlob}.*/-export const decryptChaChaOneShot = async ({+export const decryptBlob = async ({encryptedData,decryptionHeaderB64,keyB64,-}: DecryptBytes): Promise<Uint8Array> => {+}: DecryptBlobBytes): Promise<Uint8Array> => {await sodium.ready;const pullState = sodium.crypto_secretstream_xchacha20poly1305_init_pull(await fromB64(decryptionHeaderB64),