π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
rust/cli: remove internal crypto module
Prateek Sunal76dc39a370dc
diff --git a/rust/cli/src/crypto/argon.rs b/rust/cli/src/crypto/argon.rsdeleted file mode 100644index 0acdeebbfe..0000000000--- a/rust/cli/src/crypto/argon.rs+++ /dev/null@@ -1,53 +0,0 @@-use crate::{Error, Result};-use libsodium_sys as sodium;--/// Derive a key using Argon2id algorithm-/// This matches the Go implementation using libsodium-pub fn derive_argon_key(- password: &str,- salt: &str,- mem_limit: u32,- ops_limit: u32,-) -> Result<Vec<u8>> {- if mem_limit < 1024 || ops_limit < 1 {- return Err(Error::InvalidInput(- "Invalid memory or operation limits".into(),- ));- }-- // Decode salt from base64- let salt_bytes = super::decode_base64(salt)?;-- // libsodium requires salt to be exactly crypto_pwhash_SALTBYTES- if salt_bytes.len() != sodium::crypto_pwhash_SALTBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid salt length: expected {}, got {}",- sodium::crypto_pwhash_SALTBYTES,- salt_bytes.len()- )));- }-- let mut key = vec![0u8; sodium::crypto_secretbox_KEYBYTES as usize]; // 32 bytes output-- // Convert password to bytes (matching JS sodium.from_string)- let password_bytes = password.as_bytes();-- let result = unsafe {- sodium::crypto_pwhash(- key.as_mut_ptr(),- key.len() as u64,- password_bytes.as_ptr() as *const std::ffi::c_char,- password_bytes.len() as u64,- salt_bytes.as_ptr(),- ops_limit as u64,- mem_limit as usize, // API sends bytes, libsodium-sys expects bytes- sodium::crypto_pwhash_ALG_ARGON2ID13 as i32,- )- };-- if result != 0 {- return Err(Error::Crypto("Failed to derive key with Argon2id".into()));- }-- Ok(key)-}diff --git a/rust/cli/src/crypto/chacha.rs b/rust/cli/src/crypto/chacha.rsdeleted file mode 100644index 8f60dcc75b..0000000000--- a/rust/cli/src/crypto/chacha.rs+++ /dev/null@@ -1,209 +0,0 @@-use crate::{Error, Result};-use libsodium_sys as sodium;--/// Decrypt data encrypted with ChaCha20-Poly1305-pub fn decrypt_chacha(ciphertext: &[u8], nonce: &[u8], key: &[u8]) -> Result<Vec<u8>> {- if nonce.len() != sodium::crypto_aead_xchacha20poly1305_ietf_NPUBBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid nonce length: expected {}, got {}",- sodium::crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,- nonce.len()- )));- }-- if key.len() != sodium::crypto_aead_xchacha20poly1305_ietf_KEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid key length: expected {}, got {}",- sodium::crypto_aead_xchacha20poly1305_ietf_KEYBYTES,- key.len()- )));- }-- let mut plaintext =- vec![0u8; ciphertext.len() - sodium::crypto_aead_xchacha20poly1305_ietf_ABYTES as usize];- let mut plaintext_len: u64 = 0;-- let result = unsafe {- sodium::crypto_aead_xchacha20poly1305_ietf_decrypt(- plaintext.as_mut_ptr(),- &mut plaintext_len,- std::ptr::null_mut(),- ciphertext.as_ptr(),- ciphertext.len() as u64,- std::ptr::null(),- 0,- nonce.as_ptr(),- key.as_ptr(),- )- };-- if result != 0 {- return Err(Error::Crypto(- "Failed to decrypt with ChaCha20-Poly1305".into(),- ));- }-- plaintext.truncate(plaintext_len as usize);- Ok(plaintext)-}--/// Encrypt data with ChaCha20-Poly1305-pub fn encrypt_chacha(plaintext: &[u8], nonce: &[u8], key: &[u8]) -> Result<Vec<u8>> {- if nonce.len() != sodium::crypto_aead_xchacha20poly1305_ietf_NPUBBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid nonce length: expected {}, got {}",- sodium::crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,- nonce.len()- )));- }-- if key.len() != sodium::crypto_aead_xchacha20poly1305_ietf_KEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid key length: expected {}, got {}",- sodium::crypto_aead_xchacha20poly1305_ietf_KEYBYTES,- key.len()- )));- }-- let mut ciphertext =- vec![0u8; plaintext.len() + sodium::crypto_aead_xchacha20poly1305_ietf_ABYTES as usize];- let mut ciphertext_len: u64 = 0;-- let result = unsafe {- sodium::crypto_aead_xchacha20poly1305_ietf_encrypt(- ciphertext.as_mut_ptr(),- &mut ciphertext_len,- plaintext.as_ptr(),- plaintext.len() as u64,- std::ptr::null(),- 0,- std::ptr::null(),- nonce.as_ptr(),- key.as_ptr(),- )- };-- if result != 0 {- return Err(Error::Crypto(- "Failed to encrypt with ChaCha20-Poly1305".into(),- ));- }-- ciphertext.truncate(ciphertext_len as usize);- Ok(ciphertext)-}--/// Open a sealed box (decrypt with public key crypto)-pub fn sealed_box_open(ciphertext: &[u8], public_key: &[u8], secret_key: &[u8]) -> Result<Vec<u8>> {- if public_key.len() != sodium::crypto_box_PUBLICKEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid public key length: expected {}, got {}",- sodium::crypto_box_PUBLICKEYBYTES,- public_key.len()- )));- }-- if secret_key.len() != sodium::crypto_box_SECRETKEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid secret key length: expected {}, got {}",- sodium::crypto_box_SECRETKEYBYTES,- secret_key.len()- )));- }-- if ciphertext.len() < sodium::crypto_box_SEALBYTES as usize {- return Err(Error::Crypto("Ciphertext too short".into()));- }-- let mut plaintext = vec![0u8; ciphertext.len() - sodium::crypto_box_SEALBYTES as usize];-- let result = unsafe {- sodium::crypto_box_seal_open(- plaintext.as_mut_ptr(),- ciphertext.as_ptr(),- ciphertext.len() as u64,- public_key.as_ptr(),- secret_key.as_ptr(),- )- };-- if result != 0 {- return Err(Error::Crypto("Failed to open sealed box".into()));- }-- Ok(plaintext)-}--/// Open a secret box (decrypt with XSalsa20-Poly1305)-pub fn secret_box_open(ciphertext: &[u8], nonce: &[u8], key: &[u8]) -> Result<Vec<u8>> {- if nonce.len() != sodium::crypto_secretbox_NONCEBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid nonce length: expected {}, got {}",- sodium::crypto_secretbox_NONCEBYTES,- nonce.len()- )));- }-- if key.len() != sodium::crypto_secretbox_KEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid key length: expected {}, got {}",- sodium::crypto_secretbox_KEYBYTES,- key.len()- )));- }-- let mut plaintext = vec![0u8; ciphertext.len() - sodium::crypto_secretbox_MACBYTES as usize];-- let result = unsafe {- sodium::crypto_secretbox_open_easy(- plaintext.as_mut_ptr(),- ciphertext.as_ptr(),- ciphertext.len() as u64,- nonce.as_ptr(),- key.as_ptr(),- )- };-- if result != 0 {- return Err(Error::Crypto("Failed to open secret box".into()));- }-- Ok(plaintext)-}--/// Seal a secret box (encrypt with XSalsa20-Poly1305)-pub fn secret_box_seal(plaintext: &[u8], nonce: &[u8], key: &[u8]) -> Result<Vec<u8>> {- if nonce.len() != sodium::crypto_secretbox_NONCEBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid nonce length: expected {}, got {}",- sodium::crypto_secretbox_NONCEBYTES,- nonce.len()- )));- }-- if key.len() != sodium::crypto_secretbox_KEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid key length: expected {}, got {}",- sodium::crypto_secretbox_KEYBYTES,- key.len()- )));- }-- let mut ciphertext = vec![0u8; plaintext.len() + sodium::crypto_secretbox_MACBYTES as usize];-- let result = unsafe {- sodium::crypto_secretbox_easy(- ciphertext.as_mut_ptr(),- plaintext.as_ptr(),- plaintext.len() as u64,- nonce.as_ptr(),- key.as_ptr(),- )- };-- if result != 0 {- return Err(Error::Crypto("Failed to seal secret box".into()));- }-- Ok(ciphertext)-}diff --git a/rust/cli/src/crypto/kdf.rs b/rust/cli/src/crypto/kdf.rsdeleted file mode 100644index 2cafd5bd0c..0000000000--- a/rust/cli/src/crypto/kdf.rs+++ /dev/null@@ -1,35 +0,0 @@-use crate::{Error, Result};-use libsodium_sys as sodium;--const LOGIN_SUB_KEY_LEN: usize = 32;-const LOGIN_SUB_KEY_ID: u64 = 1;-const LOGIN_SUB_KEY_CONTEXT: &[u8] = b"loginctx";--/// Derive login key from key encryption key-/// This matches the web implementation's deriveSRPLoginSubKey function-pub fn derive_login_key(key_enc_key: &[u8]) -> Result<Vec<u8>> {- // Derive 32 bytes using crypto_kdf_derive_from_key- let mut sub_key = vec![0u8; LOGIN_SUB_KEY_LEN];-- // Ensure context is exactly 8 bytes (crypto_kdf_CONTEXTBYTES)- let mut context = [0u8; sodium::crypto_kdf_CONTEXTBYTES as usize];- let context_len = LOGIN_SUB_KEY_CONTEXT.len().min(context.len());- context[..context_len].copy_from_slice(&LOGIN_SUB_KEY_CONTEXT[..context_len]);-- let result = unsafe {- sodium::crypto_kdf_derive_from_key(- sub_key.as_mut_ptr(),- LOGIN_SUB_KEY_LEN,- LOGIN_SUB_KEY_ID,- context.as_ptr() as *const std::ffi::c_char,- key_enc_key.as_ptr(),- )- };-- if result != 0 {- return Err(Error::Crypto("Failed to derive login subkey".into()));- }-- // Return the first 16 bytes of the derived key (matching web implementation)- Ok(sub_key[..16].to_vec())-}diff --git a/rust/cli/src/crypto/mod.rs b/rust/cli/src/crypto/mod.rsdeleted file mode 100644index 619c7bb7a2..0000000000--- a/rust/cli/src/crypto/mod.rs+++ /dev/null@@ -1,38 +0,0 @@-use crate::Result;-use base64::{Engine, engine::general_purpose::STANDARD as BASE64};-use libsodium_sys as sodium;-use std::sync::Once;--mod argon;-mod chacha;-mod kdf;-mod stream;--pub use argon::derive_argon_key;-pub use chacha::{- decrypt_chacha, encrypt_chacha, sealed_box_open, secret_box_open, secret_box_seal,-};-pub use kdf::derive_login_key;-pub use stream::{StreamDecryptor, decrypt_file_data, decrypt_stream};--static INIT: Once = Once::new();--/// Initialize libsodium. Must be called before any crypto operations.-pub fn init() -> Result<()> {- INIT.call_once(|| unsafe {- if sodium::sodium_init() < 0 {- panic!("Failed to initialize libsodium");- }- });- Ok(())-}--/// Decode base64 string to bytes-pub fn decode_base64(input: &str) -> Result<Vec<u8>> {- Ok(BASE64.decode(input)?)-}--/// Encode bytes to base64 string-pub fn encode_base64(input: &[u8]) -> String {- BASE64.encode(input)-}diff --git a/rust/cli/src/crypto/stream.rs b/rust/cli/src/crypto/stream.rsdeleted file mode 100644index b5f9686328..0000000000--- a/rust/cli/src/crypto/stream.rs+++ /dev/null@@ -1,133 +0,0 @@-use crate::{Error, Result};-use libsodium_sys as sodium;--/// Tag indicating this is the final message in the stream-#[allow(dead_code)]-pub const TAG_FINAL: u8 = 0x03;-/// Tag for regular messages-#[allow(dead_code)]-pub const TAG_MESSAGE: u8 = 0x00;--/// XChaCha20-Poly1305 streaming decryptor-pub struct StreamDecryptor {- state: Box<[u8]>,-}--impl StreamDecryptor {- fn state_bytes() -> usize {- unsafe { sodium::crypto_secretstream_xchacha20poly1305_statebytes() }- }-- /// Create a new stream decryptor from key and header- pub fn new(key: &[u8], header: &[u8]) -> Result<Self> {- if key.len() != sodium::crypto_secretstream_xchacha20poly1305_KEYBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid key length: expected {}, got {}",- sodium::crypto_secretstream_xchacha20poly1305_KEYBYTES,- key.len()- )));- }-- if header.len() != sodium::crypto_secretstream_xchacha20poly1305_HEADERBYTES as usize {- return Err(Error::Crypto(format!(- "Invalid header length: expected {}, got {}",- sodium::crypto_secretstream_xchacha20poly1305_HEADERBYTES,- header.len()- )));- }-- let mut state = vec![0u8; Self::state_bytes()].into_boxed_slice();-- unsafe {- let ret = sodium::crypto_secretstream_xchacha20poly1305_init_pull(- state.as_mut_ptr() as *mut sodium::crypto_secretstream_xchacha20poly1305_state,- header.as_ptr(),- key.as_ptr(),- );-- if ret != 0 {- return Err(Error::Crypto(- "Failed to initialize stream decryptor".into(),- ));- }- }-- Ok(StreamDecryptor { state })- }-- /// Pull (decrypt) a message from the stream- pub fn pull(&mut self, ciphertext: &[u8]) -> Result<(Vec<u8>, u8)> {- if ciphertext.len() < sodium::crypto_secretstream_xchacha20poly1305_ABYTES as usize {- return Err(Error::Crypto("Ciphertext too short".into()));- }-- let mut plaintext = vec![- 0u8;- ciphertext.len()- - sodium::crypto_secretstream_xchacha20poly1305_ABYTES as usize- ];- let mut plaintext_len: u64 = 0;- let mut tag: u8 = 0;-- unsafe {- let ret = sodium::crypto_secretstream_xchacha20poly1305_pull(- self.state.as_mut_ptr() as *mut sodium::crypto_secretstream_xchacha20poly1305_state,- plaintext.as_mut_ptr(),- &mut plaintext_len,- &mut tag,- ciphertext.as_ptr(),- ciphertext.len() as u64,- std::ptr::null(),- 0,- );-- if ret != 0 {- return Err(Error::Crypto("Failed to decrypt stream chunk".into()));- }- }-- plaintext.truncate(plaintext_len as usize);- Ok((plaintext, tag))- }-- /// Decrypt an entire message at once (non-streaming)- pub fn decrypt_all(key: &[u8], header: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> {- let mut decryptor = Self::new(key, header)?;- let (plaintext, _tag) = decryptor.pull(ciphertext)?;- Ok(plaintext)- }-}--/// Decrypt data using streaming XChaCha20-Poly1305-/// This is for single-chunk decryption (most common case for files)-pub fn decrypt_stream(ciphertext: &[u8], header: &[u8], key: &[u8]) -> Result<Vec<u8>> {- StreamDecryptor::decrypt_all(key, header, ciphertext)-}--/// Decrypt file data from memory using streaming cipher with chunking for large files-pub fn decrypt_file_data(encrypted_data: &[u8], header: &[u8], key: &[u8]) -> Result<Vec<u8>> {- // Buffer size matching Go implementation: 4MB + overhead- const CHUNK_SIZE: usize =- 4 * 1024 * 1024 + sodium::crypto_secretstream_xchacha20poly1305_ABYTES as usize;-- let mut decryptor = StreamDecryptor::new(key, header)?;- let mut result = Vec::with_capacity(encrypted_data.len());-- let mut offset = 0;- while offset < encrypted_data.len() {- let chunk_end = std::cmp::min(offset + CHUNK_SIZE, encrypted_data.len());- let chunk = &encrypted_data[offset..chunk_end];-- let (plaintext, tag) = decryptor.pull(chunk)?;- result.extend_from_slice(&plaintext);-- offset = chunk_end;-- // Check if this was the final chunk- if tag == TAG_FINAL {- break;- }- }-- Ok(result)-}