π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Make secretbox encrypt_with_nonce a private helper
Manav Rathi4b7181431aac
diff --git a/rust/bindings/wasm/core/src/crypto.rs b/rust/bindings/wasm/core/src/crypto.rsindex 66c738e995..420361a062 100644--- a/rust/bindings/wasm/core/src/crypto.rs+++ b/rust/bindings/wasm/core/src/crypto.rs@@ -57,19 +57,6 @@ pub fn secretbox_encrypt(data: &[u8], key: &[u8]) -> Result<EncryptedBox, Crypto})}-#[wasm_bindgen(js_name = secretboxEncryptWithNonce)]-pub fn secretbox_encrypt_with_nonce(- data: &[u8],- nonce: &[u8],- key: &[u8],-) -> Result<Vec<u8>, CryptoError> {- Ok(core_crypto::secretbox::encrypt_with_nonce(- data,- &core_crypto::Nonce::try_from_slice(nonce)?,- &core_crypto::Key::try_from_slice(key)?,- ))-}-#[wasm_bindgen(js_name = secretboxDecrypt)]pub fn secretbox_decrypt(data: &[u8], nonce: &[u8], key: &[u8]) -> Result<Vec<u8>, CryptoError> {Ok(core_crypto::secretbox::decrypt(diff --git a/rust/crates/core/src/auth/key_gen.rs b/rust/crates/core/src/auth/key_gen.rsindex 868f21740a..f2b00b55c5 100644--- a/rust/crates/core/src/auth/key_gen.rs+++ b/rust/crates/core/src/auth/key_gen.rs@@ -1,6 +1,6 @@//! Key generation for new account sign-up.-use crate::crypto::{self, Key, Nonce, SecretString, SecretVec, argon, kdf, secretbox};+use crate::crypto::{self, Key, SecretString, SecretVec, argon, kdf, secretbox};use super::{KeyAttributes, KeyGenResult, PrivateKeyAttributes, Result};@@ -17,11 +17,10 @@ pub enum KeyDerivationStrength {/// Encrypt data and return (encrypted_data, nonce) as base64 strings./// The encrypted_data is MAC || ciphertext format (compatible with Dart).fn encrypt_to_b64(plaintext: &[u8], key: &Key) -> Result<(String, String)> {- let nonce = Nonce::generate();- let encrypted = secretbox::encrypt_with_nonce(plaintext, &nonce, key);+ let encrypted = secretbox::encrypt(plaintext, key);Ok((- crypto::encode_b64(&encrypted),- crypto::encode_b64(nonce.as_bytes()),+ crypto::encode_b64(&encrypted.encrypted_data),+ crypto::encode_b64(encrypted.nonce.as_bytes()),))}@@ -176,6 +175,7 @@ pub fn create_new_recovery_key(#[cfg(test)]mod tests {use super::*;+ use crate::crypto::Nonce;// Typed-decrypt helper for byte-slice test material.fn decrypt_raw(data: &[u8], nonce: &[u8], key: &[u8]) -> Vec<u8> {diff --git a/rust/crates/core/src/crypto/secretbox.rs b/rust/crates/core/src/crypto/secretbox.rsindex f763adea2c..d62546c8e6 100644--- a/rust/crates/core/src/crypto/secretbox.rs+++ b/rust/crates/core/src/crypto/secretbox.rs@@ -11,7 +11,7 @@//! (recorded per function below).//!//! Every message takes a 192-bit nonce. The nonce is not secret, but it must-//! never be reused with the same key (see [`encrypt_with_nonce`]). The+//! never be reused with the same key. The//! [`encrypt`] and [`encrypt_combined`] functions generate a fresh random nonce//! for you, which is the safe default.//!@@ -80,22 +80,10 @@ pub fn encrypt(data: &[u8], key: &Key) -> EncryptedBox {}}-/// Encrypt `data` under `key` with a caller-supplied `nonce`.-///-/// Prefer [`encrypt`], which generates the nonce for you. Reach for this only-/// when the nonce is fixed by something else, for example re-creating a-/// ciphertext that must match bytes produced earlier.-///-/// # Security-///-/// The nonce must be unique for every message encrypted under a given key.-/// Reusing a (key, nonce) pair is catastrophic: it reveals the XOR of the two-/// plaintexts and makes the Poly1305 tag forgeable, breaking both-/// confidentiality and authenticity. The nonce itself need not be secret.-///-/// Returns `MAC (16 bytes) β ciphertext`, wire-compatible with libsodium's-/// `crypto_secretbox_easy`.-pub fn encrypt_with_nonce(data: &[u8], nonce: &Nonce, key: &Key) -> Vec<u8> {+/// Shared back end for [`encrypt`] and [`encrypt_combined`]: encrypts `data`+/// under `key` with the given `nonce`, returning `MAC β ciphertext`. Both+/// callers generate a fresh nonce, so a (key, nonce) pair is never reused.+fn encrypt_with_nonce(data: &[u8], nonce: &Nonce, key: &Key) -> Vec<u8> {let cipher = XSalsa20Poly1305::new(GenericArray::from_slice(key.as_bytes()));let nonce_ga = GenericArray::from_slice(nonce.as_bytes());@@ -106,7 +94,7 @@ pub fn encrypt_with_nonce(data: &[u8], nonce: &Nonce, key: &Key) -> Vec<u8> {.expect("XSalsa20-Poly1305 encryption cannot fail for in-memory plaintexts")}-/// Decrypt a ciphertext produced by [`encrypt`] or [`encrypt_with_nonce`].+/// Decrypt a ciphertext produced by [`encrypt`].////// Pass the same `nonce` and `key` that encrypted it. `data` is/// `MAC (16 bytes) β ciphertext`. The Poly1305 tag is verified before anydiff --git a/web/packages/wasm-core/secretbox.ts b/web/packages/wasm-core/secretbox.tsindex 0caa0b6ab5..73e5ac8f30 100644--- a/web/packages/wasm-core/secretbox.ts+++ b/web/packages/wasm-core/secretbox.ts@@ -12,9 +12,9 @@* (recorded per function below).** Every message takes a 192-bit nonce. The nonce is not secret, but it must- * never be reused with the same key (see {@link secretboxEncryptWithNonce}). The- * {@link secretboxEncrypt} and {@link secretboxEncryptCombined} functions- * generate a fresh random nonce for you, which is the safe default.+ * never be reused with the same key. The {@link secretboxEncrypt} and+ * {@link secretboxEncryptCombined} functions generate a fresh random nonce for+ * you, which is the safe default.** Two payload shapes are offered, differing only in how the nonce travels:*@@ -77,30 +77,6 @@ export const secretboxEncrypt = async (}};-/**- * Encrypt `data` under `key` with a caller-supplied `nonce`.- *- * Prefer {@link secretboxEncrypt}, which generates the nonce for you. Reach for- * this only when the nonce is fixed by something else, for example re-creating a- * ciphertext that must match bytes produced earlier.- *- * The nonce must be unique for every message encrypted under a given key.- * Reusing a (key, nonce) pair is catastrophic: it reveals the XOR of the two- * plaintexts and makes the Poly1305 tag forgeable, breaking both- * confidentiality and authenticity. The nonce itself need not be secret.- *- * Returns `MAC (16 bytes) β ciphertext`, wire-compatible with libsodium's- * `crypto_secretbox_easy`.- */-export const secretboxEncryptWithNonce = async (- data: Uint8Array,- nonce: Nonce,- key: Key,-): Promise<Uint8Array> => {- const wasm = await loadWasmCore();- return wasm.secretboxEncryptWithNonce(data, nonce.bytes, key.bytes);-};-/*** Decrypt a ciphertext produced by {@link secretboxEncrypt}.*