π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Dec
Manav Rathi13f31a7d092b
diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.tsindex 3566862d9a..d29422fc17 100644--- a/web/packages/base/crypto/ente.ts+++ b/web/packages/base/crypto/ente.ts@@ -22,6 +22,8 @@ import * as libsodium from "@ente/shared/crypto/internal/libsodium";/*** Encrypt arbitrary data associated with a file using the file's key.*+ * Use {@link decryptFileAssociatedData} to decrypt the result.+ ** See {@link encryptChaChaOneShot} for the implementation details.** @param data The data (bytes) to encrypt.@@ -34,6 +36,34 @@ import * as libsodium from "@ente/shared/crypto/internal/libsodium";export const encryptFileAssociatedData = (data: Uint8Array, keyB64: string) =>libsodium.encryptChaChaOneShot(data, keyB64);+/**+ * Decrypt arbitrary data associated with a file using the file's key.+ *+ * This is the sibling of {@link encryptFileAssociatedData}.+ *+ * @param encryptedData A {@link Base64 encoded string containing the encrypted data.+ *+ * @param headerB64 Base64 encoded string containing the decryption header+ * produced during encryption.+ *+ * @param keyB64 Base64 encoded string containing the encryption key. This is+ * expected to be the key of the file with which {@link encryptedDataB64} is+ * associated.+ *+ * @returns The decrypted metadata bytes.+ */+export const decryptFileAssociatedDataFromB64 = async (+ encryptedDataB64: string,+ decryptionHeaderB64: string,+ keyB64: string,+) =>+ libsodium.decryptChaChaOneShot(+ await libsodium.fromB64(encryptedDataB64),+ await libsodium.fromB64(decryptionHeaderB64),+ keyB64,+ );++/*** A variant of {@link encryptFileAssociatedData} that returns the Base64* encoded encrypted data instead of its raw bytes.diff --git a/web/packages/shared/crypto/internal/libsodium.ts b/web/packages/shared/crypto/internal/libsodium.tsindex d553d8bbba..b1896c6cb4 100644--- a/web/packages/shared/crypto/internal/libsodium.ts+++ b/web/packages/shared/crypto/internal/libsodium.ts@@ -113,15 +113,27 @@ export async function fromHex(input: string) {/*** Encrypt the given {@link data} using the given (Base64 encoded) key.*- * 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.+ * 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. As such, this is only meant to be used for relatively- * small amounts of data.+ * streaming encryption. This is only meant to be used for relatively small+ * amounts of data (few MBs).*- * Ref: https://doc.libsodium.org/secret-key_cryptography/secretstream+ * 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.+ *+ * See: https://doc.libsodium.org/secret-key_cryptography/secretbox+ *+ * The difference here is that this function is meant to used for data+ * associated with a file. There is no technical reason to do it that way, just+ * that all data associated with a file, including its actual contents, use the+ * same underlying (streaming) libsodium APIs. While other independent free+ * standing encryption needs are covered using the secretbox APIs.** @param data A {@link Uint8Array} containing the bytes that we want to* encrypt.