π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
rust/core: harden auth and align tests
Prateek Sunal0ba7c2f73222
diff --git a/rust/core/src/auth/srp.rs b/rust/core/src/auth/srp.rsindex cf020288fb..40cd19dd93 100644--- a/rust/core/src/auth/srp.rs+++ b/rust/core/src/auth/srp.rs@@ -7,6 +7,7 @@use sha2::Sha256;use srp::client::{SrpClient as SrpClientInner, SrpClientVerifier};use srp::groups::G_4096;+use zeroize::Zeroize;use super::{AuthError, Result};@@ -93,6 +94,10 @@ impl SrpSession {let proof = verifier.proof().to_vec();self.verifier = Some(verifier);+ // No longer needed after we have a verifier+ self.login_key.zeroize();+ self.a_private.zeroize();+Ok(proof)}@@ -112,6 +117,13 @@ impl SrpSession {}}+impl Drop for SrpSession {+ fn drop(&mut self) {+ self.login_key.zeroize();+ self.a_private.zeroize();+ }+}+#[cfg(test)]mod tests {use super::*;@@ -133,6 +145,32 @@ mod tests {assert!(a.len() > 100); // 4096-bit group produces large values}+ #[test]+ fn test_sensitive_buffers_zeroized_after_compute_m1() {+ crypto::init().unwrap();++ use rand_core::RngCore;+ use srp::server::SrpServer;++ let srp_user_id = "test-user-id";+ let srp_salt = [0u8; 16];+ let login_key = [0x11u8; 16];++ let client = SrpClientInner::<Sha256>::new(&G_4096);+ let verifier = client.compute_verifier(srp_user_id.as_bytes(), &login_key, &srp_salt);++ let server = SrpServer::<Sha256>::new(&G_4096);+ let mut b = [0u8; 64];+ rand_core::OsRng.fill_bytes(&mut b);+ let b_pub = server.compute_public_ephemeral(&b, &verifier);++ let mut session = SrpSession::new(srp_user_id, &srp_salt, &login_key).unwrap();+ session.compute_m1(&b_pub).unwrap();++ assert!(session.login_key.iter().all(|&b| b == 0));+ assert!(session.a_private.iter().all(|&b| b == 0));+ }+#[test]fn test_srp_session_invalid_login_key() {let srp_user_id = "test-user-id";diff --git a/rust/core/src/auth/types.rs b/rust/core/src/auth/types.rsindex 49b968841a..2d631445be 100644--- a/rust/core/src/auth/types.rs+++ b/rust/core/src/auth/types.rs@@ -73,11 +73,16 @@ pub struct LoginResult {pub key_encryption_key: Vec<u8>,}+fn default_email_mfa_enabled() -> bool {+ true+}+/// SRP attributes received from server.#[derive(Debug, Clone, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub struct SrpAttributes {/// SRP user ID (UUID)+ #[serde(rename = "srpUserID")]pub srp_user_id: String,/// SRP salt (base64)pub srp_salt: String,@@ -88,6 +93,7 @@ pub struct SrpAttributes {/// KEK salt (base64) - same as in KeyAttributespub kek_salt: String,/// Whether email MFA is enabled (use email OTT instead of SRP)+ #[serde(rename = "isEmailMFAEnabled", default = "default_email_mfa_enabled")]pub is_email_mfa_enabled: bool,}diff --git a/rust/core/tests/auth_integration.rs b/rust/core/tests/auth_integration.rsindex 96ef92f7c3..527c89b5a6 100644--- a/rust/core/tests/auth_integration.rs+++ b/rust/core/tests/auth_integration.rs@@ -305,9 +305,15 @@ mod serialization {};let json = serde_json::to_string(&attrs).unwrap();- let parsed: SrpAttributes = serde_json::from_str(&json).unwrap();+ assert!(json.contains("\"srpUserID\""));+ assert!(json.contains("\"isEmailMFAEnabled\""));+ let parsed: SrpAttributes = serde_json::from_str(&json).unwrap();assert_eq!(parsed.srp_user_id, attrs.srp_user_id);assert_eq!(parsed.mem_limit, attrs.mem_limit);++ let json_missing_flag = r#"{"srpUserID":"user123","srpSalt":"c2FsdA==","memLimit":67108864,"opsLimit":2,"kekSalt":"a2VrU2FsdA=="}"#;+ let parsed_missing: SrpAttributes = serde_json::from_str(json_missing_flag).unwrap();+ assert!(parsed_missing.is_email_mfa_enabled);}}diff --git a/rust/core/tests/crypto_interop.rs b/rust/core/tests/crypto_interop.rsindex a2a197a03f..ec81cab8e8 100644--- a/rust/core/tests/crypto_interop.rs+++ b/rust/core/tests/crypto_interop.rs@@ -819,10 +819,10 @@ mod public_api_coverage {// Small data: single FINAL chunkassert_eq!(crypto::stream::estimate_encrypted_size(100), 100 + abytes);- // Exact chunk size: one MESSAGE chunk + empty FINAL chunk+ // Exact chunk size: one FINAL chunkassert_eq!(crypto::stream::estimate_encrypted_size(chunk_size),- dec_chunk_size + abytes+ dec_chunk_size);// Chunk size + extra: one MESSAGE chunk + non-empty FINAL chunk@@ -847,15 +847,15 @@ mod public_api_coverage {// Valid casesassert!(crypto::stream::validate_sizes(0, abytes)); // empty plaintextassert!(crypto::stream::validate_sizes(100, 117)); // small data- assert!(crypto::stream::validate_sizes(- chunk_size,- dec_chunk_size + abytes- )); // exact multiple+ assert!(crypto::stream::validate_sizes(chunk_size, dec_chunk_size)); // exact chunk// Invalid casesassert!(!crypto::stream::validate_sizes(100, 100)); // missing ABYTESassert!(!crypto::stream::validate_sizes(0, 0)); // ciphertext can't be 0- assert!(!crypto::stream::validate_sizes(chunk_size, dec_chunk_size)); // missing final ABYTES+ assert!(!crypto::stream::validate_sizes(+ chunk_size,+ dec_chunk_size + abytes+ )); // extra empty chunk}#[test]