π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Bound Argon derivation costs and widen memory limits
Manav Rathie3b6a5cb00d0
diff --git a/mobile/apps/locker/lib/services/files/links/links_service.dart b/mobile/apps/locker/lib/services/files/links/links_service.dartindex f84917c7a8..bbae32a2fb 100644--- a/mobile/apps/locker/lib/services/files/links/links_service.dart+++ b/mobile/apps/locker/lib/services/files/links/links_service.dart@@ -30,7 +30,7 @@ class LinksService {'encryptedFileKey': secretPayload.encryptedFileKey,'encryptedFileKeyNonce': secretPayload.encryptedFileKeyNonce,'kdfNonce': secretPayload.kdfNonce,- 'kdfMemLimit': secretPayload.kdfMemLimit,+ 'kdfMemLimit': secretPayload.kdfMemLimit.toInt(),'kdfOpsLimit': secretPayload.kdfOpsLimit,'encryptedShareKey': secretPayload.encryptedShareKey,},diff --git a/rust/bindings/frb/lib/src/locker.rs b/rust/bindings/frb/lib/src/locker.rsindex 9fb7d93b92..b894b28544 100644--- a/rust/bindings/frb/lib/src/locker.rs+++ b/rust/bindings/frb/lib/src/locker.rs@@ -22,7 +22,7 @@ pub struct PreparedFileLink {pub encrypted_file_key: String,pub encrypted_file_key_nonce: String,pub kdf_nonce: String,- pub kdf_mem_limit: u32,+ pub kdf_mem_limit: u64,pub kdf_ops_limit: u32,pub encrypted_share_key: String,}diff --git a/rust/bindings/wasm/lib/src/accounts.rs b/rust/bindings/wasm/lib/src/accounts.rsindex 59d19ce8d5..43169a12b5 100644--- a/rust/bindings/wasm/lib/src/accounts.rs+++ b/rust/bindings/wasm/lib/src/accounts.rs@@ -33,7 +33,7 @@ impl From<Error> for JsValue {pub struct GeneratedKek {pub key: String,pub salt: String,- pub mem_limit: u32,+ pub mem_limit: u64,pub ops_limit: u32,}diff --git a/rust/bindings/wasm/lib/src/locker.rs b/rust/bindings/wasm/lib/src/locker.rsindex 70827d8b86..89a34685cf 100644--- a/rust/bindings/wasm/lib/src/locker.rs+++ b/rust/bindings/wasm/lib/src/locker.rs@@ -31,7 +31,7 @@ pub struct PreparedFileLinkPayload {encrypted_file_key: String,encrypted_file_key_nonce: String,kdf_nonce: String,- kdf_mem_limit: u32,+ kdf_mem_limit: u64,kdf_ops_limit: u32,}diff --git a/rust/bindings/wasm/lib/src/prelogin.rs b/rust/bindings/wasm/lib/src/prelogin.rsindex 27fd97ce6b..1870f702ca 100644--- a/rust/bindings/wasm/lib/src/prelogin.rs+++ b/rust/bindings/wasm/lib/src/prelogin.rs@@ -17,7 +17,7 @@ pub struct GeneratedSrpSetup {pub fn auth_derive_kek(password: &str,kek_salt_b64: &str,- mem_limit: u32,+ mem_limit: u64,ops_limit: u32,) -> Result<String, AccountsError> {let kek = auth::derive_kek(password, kek_salt_b64, mem_limit, ops_limit)?;diff --git a/rust/crates/accounts/src/auth/api.rs b/rust/crates/accounts/src/auth/api.rsindex de50a94c7e..259b6b227e 100644--- a/rust/crates/accounts/src/auth/api.rs+++ b/rust/crates/accounts/src/auth/api.rs@@ -41,7 +41,7 @@ impl fmt::Debug for DecryptedSecrets {pub struct GeneratedKek {pub key: SecretVec,pub salt: Vec<u8>,- pub mem_limit: u32,+ pub mem_limit: u64,pub ops_limit: u32,}@@ -97,7 +97,7 @@ pub fn derive_srp_credentials(password: &str, srp_attrs: &SrpAttributes) -> Resupub fn derive_kek(password: &str,kek_salt: &str,- mem_limit: u32,+ mem_limit: u64,ops_limit: u32,) -> Result<SecretVec> {let salt_bytes = b64::decode(kek_salt).map_err(|e| Error::Decode(format!("kek_salt: {e}")))?;diff --git a/rust/crates/accounts/src/auth/types.rs b/rust/crates/accounts/src/auth/types.rsindex dc62bae29f..6db934bd4a 100644--- a/rust/crates/accounts/src/auth/types.rs+++ b/rust/crates/accounts/src/auth/types.rs@@ -19,7 +19,7 @@ pub struct KeyAttributes {pub public_key: String,pub encrypted_secret_key: String,pub secret_key_decryption_nonce: String,- pub mem_limit: u32,+ pub mem_limit: u64,pub ops_limit: u32,#[serde(skip_serializing_if = "Option::is_none")]pub master_key_encrypted_with_recovery_key: Option<String>,@@ -118,7 +118,7 @@ pub struct SrpAttributes {#[serde(rename = "srpUserID")]pub srp_user_id: Uuid,pub srp_salt: String,- pub mem_limit: u32,+ pub mem_limit: u64,pub ops_limit: u32,pub kek_salt: String,#[serde(rename = "isEmailMFAEnabled", default = "default_email_mfa_enabled")]diff --git a/rust/crates/accounts/src/models.rs b/rust/crates/accounts/src/models.rsindex a5e42ef9e3..35a397ba42 100644--- a/rust/crates/accounts/src/models.rs+++ b/rust/crates/accounts/src/models.rs@@ -232,7 +232,7 @@ pub struct UpdatedKeyAttr {pub encrypted_key: String,pub key_decryption_nonce: String,pub ops_limit: u32,- pub mem_limit: u32,+ pub mem_limit: u64,}impl From<&KeyAttributes> for UpdatedKeyAttr {diff --git a/rust/crates/accounts/src/signup/tests.rs b/rust/crates/accounts/src/signup/tests.rsindex e077514cd2..92a1dddf5b 100644--- a/rust/crates/accounts/src/signup/tests.rs+++ b/rust/crates/accounts/src/signup/tests.rs@@ -236,7 +236,7 @@ async fn create_account_uploads_keys_and_completes_srp_setup() {.with_body_from_request(move |request| {let payload: SetUserAttributesPayload = parse_request_body(request);let key_attributes = payload.key_attributes;- let mem_limit = u64::from(key_attributes.mem_limit);+ let mem_limit = key_attributes.mem_limit;let ops_limit = u64::from(key_attributes.ops_limit);assert_eq!(mem_limit * ops_limit, 4_294_967_296);diff --git a/rust/crates/auth/src/backup.rs b/rust/crates/auth/src/backup.rsindex 6a03ead6aa..a0ec172585 100644--- a/rust/crates/auth/src/backup.rs+++ b/rust/crates/auth/src/backup.rs@@ -19,20 +19,11 @@ struct Backup {#[derive(Deserialize, Serialize)]#[serde(rename_all = "camelCase")]struct KdfParams {- mem_limit: u32,+ mem_limit: u64,ops_limit: u32,salt: String,}-fn validate_params(params: argon::Params) -> Result<()> {- params.validate()?;- let strength = u64::from(params.mem_limit) * u64::from(params.ops_limit);- if params.mem_limit > argon::Params::SENSITIVE.mem_limit || strength > 1_u64 << 32 {- return Err(Error::UnsafeKdf);- }- Ok(())-}-pub fn encrypt(plaintext: &[u8], password: &str) -> Result<Vec<u8>> {let derived = argon::derive_sensitive_key(password)?;let encrypted = blob::encrypt(plaintext, &derived.key)?;@@ -58,7 +49,7 @@ pub fn decrypt(input: &[u8], password: &str) -> Result<Zeroizing<Vec<u8>>> {mem_limit: backup.kdf_params.mem_limit,ops_limit: backup.kdf_params.ops_limit,};- validate_params(params)?;+ params.validate()?;let salt = Salt::try_from_slice(&b64::decode(&backup.kdf_params.salt)?)?;let header = Header::try_from_slice(&b64::decode(&backup.encryption_nonce)?)?;let encrypted = b64::decode(&backup.encrypted_data)?;@@ -76,39 +67,6 @@ mod tests {use super::*;use ente_core::crypto::stream::Encryptor;- #[test]- fn admits_existing_producer_profiles_without_deriving_keys() {- let mut memory = 1 << 30;- let mut operations = 4;- while memory >= 8192 {- assert!(- validate_params(argon::Params {- mem_limit: memory,- ops_limit: operations- })- .is_ok()- );- memory /= 2;- operations *= 2;- }- for (memory, operations) in [- (8191, 1),- (8193, 1),- (8192, 0),- (1 << 30, 5),- ((1 << 30) + 1024, 1),- (8192, u32::MAX),- ] {- assert!(- validate_params(argon::Params {- mem_limit: memory,- ops_limit: operations- })- .is_err()- );- }- }-#[test]fn decryption_authenticates_legacy_bytes_without_interpreting_them() {let password = " preserve whitespace ";@@ -129,6 +87,16 @@ mod tests {};let encoded = serde_json::to_vec(&backup).unwrap();assert_eq!(decrypt(&encoded, password).unwrap().as_slice(), bytes);+ backup.kdf_params.mem_limit = 1 << 32;+ backup.kdf_params.ops_limit = 5;+ assert!(matches!(+ decrypt(&serde_json::to_vec(&backup).unwrap(), password),+ Err(Error::Crypto(+ ente_core::crypto::Error::InvalidKeyDerivationParams(_)+ ))+ ));+ backup.kdf_params.mem_limit = 8192;+ backup.kdf_params.ops_limit = 1;assert!(decrypt(&encoded, "wrong").is_err());let mut damaged = encrypted;damaged[0] ^= 1;diff --git a/rust/crates/auth/src/lib.rs b/rust/crates/auth/src/lib.rsindex efa183e698..d9a2a08f3d 100644--- a/rust/crates/auth/src/lib.rs+++ b/rust/crates/auth/src/lib.rs@@ -33,8 +33,6 @@ pub enum Error {InvalidBackup,#[error("unsupported Auth export version")]UnsupportedVersion,- #[error("Auth export key derivation parameters exceed supported bounds")]- UnsafeKdf,}pub type Result<T> = std::result::Result<T, Error>;diff --git a/rust/crates/core/src/crypto/argon.rs b/rust/crates/core/src/crypto/argon.rsindex 4011fe8a76..3c0d38d16e 100644--- a/rust/crates/core/src/crypto/argon.rs+++ b/rust/crates/core/src/crypto/argon.rs@@ -6,7 +6,7 @@ use crate::crypto::{Error, Key, Result, Salt};#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub struct Params {- pub mem_limit: u32,+ pub mem_limit: u64,pub ops_limit: u32,}@@ -53,11 +53,19 @@ impl Params {Self::MIN.ops_limit)));}+ let max_memory = 4 * Self::SENSITIVE.mem_limit;+ let max_work = max_memory * u64::from(Self::SENSITIVE.ops_limit);+ let work = u128::from(self.mem_limit) * u128::from(self.ops_limit);+ if self.mem_limit > max_memory || work > u128::from(max_work) {+ return Err(Error::InvalidKeyDerivationParams(+ "Password derivation exceeds supported resource limits".into(),+ ));+ }Ok(())}}-const MEMLIMIT_SENSITIVE_MIN: u32 = 134_217_728;+const MEMLIMIT_SENSITIVE_MIN: u64 = 134_217_728;pub struct DerivedKey {pub key: Key,@@ -84,7 +92,7 @@ pub fn derive_key(password: &str, salt: &Salt, params: Params) -> Result<Key> {fn derive_key_impl(password: &[u8], salt: &Salt, params: Params) -> Result<Key> {params.validate()?;- let m_cost = params.mem_limit / 1024;+ let m_cost = (params.mem_limit / 1024) as u32;let t_cost = params.ops_limit;let p_cost = 1;@@ -138,9 +146,8 @@ fn derive_sensitive_adaptive(password: &[u8], salt: &Salt) -> Result<DerivedKey>)));}- let desired_strength =- u64::from(Params::SENSITIVE.mem_limit) * u64::from(Params::SENSITIVE.ops_limit);- let factor = Params::SENSITIVE.mem_limit / Params::MODERATE.mem_limit;+ let desired_strength = Params::SENSITIVE.mem_limit * u64::from(Params::SENSITIVE.ops_limit);+ let factor = (Params::SENSITIVE.mem_limit / Params::MODERATE.mem_limit) as u32;let mut mem_limit = Params::MODERATE.mem_limit;let mut ops_limit = Params::SENSITIVE.ops_limit@@ -149,7 +156,7 @@ fn derive_sensitive_adaptive(password: &[u8], salt: &Salt) -> Result<DerivedKey>Error::InvalidKeyDerivationParams("Operations limit overflow".to_string())})?;- if u64::from(mem_limit) * u64::from(ops_limit) != desired_strength {+ if mem_limit * u64::from(ops_limit) != desired_strength {return Err(Error::InvalidKeyDerivationParams(format!("Unexpected mem/ops limits: mem_limit {mem_limit}, ops_limit {ops_limit}")));@@ -196,9 +203,8 @@ mod tests {#[test]fn test_sensitive_policy_matches_existing_client_bounds() {- let desired_strength =- u64::from(Params::SENSITIVE.mem_limit) * u64::from(Params::SENSITIVE.ops_limit);- let factor = Params::SENSITIVE.mem_limit / Params::MODERATE.mem_limit;+ let desired_strength = Params::SENSITIVE.mem_limit * u64::from(Params::SENSITIVE.ops_limit);+ let factor = (Params::SENSITIVE.mem_limit / Params::MODERATE.mem_limit) as u32;let mut mem_limit = Params::MODERATE.mem_limit;let mut ops_limit = Params::SENSITIVE.ops_limit * factor;let mut attempts = Vec::new();@@ -220,7 +226,7 @@ mod tests {assert!(attempts.iter()- .all(|(mem, ops)| u64::from(*mem) * u64::from(*ops) == desired_strength)+ .all(|(mem, ops)| *mem * u64::from(*ops) == desired_strength));}@@ -276,6 +282,34 @@ mod tests {assert!(result.is_err());}+ #[test]+ fn test_params_bound_memory_and_work() {+ for (memory, operations, accepted) in [+ (1 << 30, 4, true),+ (1 << 28, 16, true),+ (1 << 27, 32, true),+ (1 << 32, 4, true),+ (1 << 28, 64, true),+ (8192, 1 << 21, true),+ ((1 << 32) + 1024, 1, false),+ (1 << 32, 5, false),+ (1 << 28, 65, false),+ (8192, u32::MAX, false),+ (u64::MAX - 1023, u32::MAX, false),+ ] {+ assert_eq!(+ Params {+ mem_limit: memory,+ ops_limit: operations,+ }+ .validate()+ .is_ok(),+ accepted,+ "{memory} bytes, {operations} operations"+ );+ }+ }+#[test]fn test_unusual_passwords() {let salt = Salt::generate();diff --git a/rust/crates/legacy/src/client/recovery.rs b/rust/crates/legacy/src/client/recovery.rsindex 9bd7311cc7..7297b91fd8 100644--- a/rust/crates/legacy/src/client/recovery.rs+++ b/rust/crates/legacy/src/client/recovery.rs@@ -326,7 +326,7 @@ struct LegacyUpdatedKeyAttr {kek_salt: String,encrypted_key: String,key_decryption_nonce: String,- mem_limit: u32,+ mem_limit: u64,ops_limit: u32,}diff --git a/rust/crates/legacy/src/kit/recovery.rs b/rust/crates/legacy/src/kit/recovery.rsindex 0e5f6b5fd9..c98f9957e3 100644--- a/rust/crates/legacy/src/kit/recovery.rs+++ b/rust/crates/legacy/src/kit/recovery.rs@@ -378,7 +378,7 @@ struct LegacyKitUpdatedKeyAttr {kek_salt: String,encrypted_key: String,key_decryption_nonce: String,- mem_limit: u32,+ mem_limit: u64,ops_limit: u32,}diff --git a/rust/crates/locker/src/lib.rs b/rust/crates/locker/src/lib.rsindex bba6d6bb7a..09fe7cbda9 100644--- a/rust/crates/locker/src/lib.rs+++ b/rust/crates/locker/src/lib.rs@@ -13,7 +13,7 @@ pub struct FileLinkPayload {pub encrypted_file_key: String,pub encrypted_file_key_nonce: String,pub kdf_nonce: String,- pub kdf_mem_limit: u32,+ pub kdf_mem_limit: u64,pub kdf_ops_limit: u32,}diff --git a/rust/crates/paste/src/lib.rs b/rust/crates/paste/src/lib.rsindex 4a89f91fda..ca90533891 100644--- a/rust/crates/paste/src/lib.rs+++ b/rust/crates/paste/src/lib.rs@@ -200,7 +200,7 @@ pub struct PastePayload {encrypted_paste_key: String,encrypted_paste_key_nonce: String,kdf_nonce: String,- kdf_mem_limit: u32,+ kdf_mem_limit: u64,kdf_ops_limit: u32,}diff --git a/rust/crates/space/src/client/links.rs b/rust/crates/space/src/client/links.rsindex 1a01683654..7809cb24e7 100644--- a/rust/crates/space/src/client/links.rs+++ b/rust/crates/space/src/client/links.rs@@ -482,7 +482,7 @@ fn generate_access_key() -> String {fn derive_link_keys(access_key: &str,encoded_salt: &str,- mem_limit: u32,+ mem_limit: u64,ops_limit: u32,) -> Result<DerivedLinkKeys> {if mem_limit != argon::Params::INTERACTIVE.mem_limitdiff --git a/rust/crates/space/src/transport.rs b/rust/crates/space/src/transport.rsindex bb3b41f6f6..1ab53043b4 100644--- a/rust/crates/space/src/transport.rs+++ b/rust/crates/space/src/transport.rs@@ -392,7 +392,7 @@ pub struct SpaceKeyVersionResponse {pub struct SpaceLinkWriteRequest {pub auth_key: String,pub kdf_salt: String,- pub kdf_mem_limit: u32,+ pub kdf_mem_limit: u64,pub kdf_ops_limit: u32,pub key_version: i32,pub encrypted_space_key: String,@@ -410,7 +410,7 @@ pub struct SpaceLinkStatusResponse {#[serde(default)]pub kdf_salt: String,#[serde(default)]- pub kdf_mem_limit: u32,+ pub kdf_mem_limit: u64,#[serde(default)]pub kdf_ops_limit: u32,#[serde(default)]@@ -423,7 +423,7 @@ pub struct SpaceLinkStatusResponse {#[serde(rename_all = "camelCase")]pub struct SpaceLinkBootstrapResponse {pub kdf_salt: String,- pub kdf_mem_limit: u32,+ pub kdf_mem_limit: u64,pub kdf_ops_limit: u32,}diff --git a/rust/crates/test-support/src/account_fixture.rs b/rust/crates/test-support/src/account_fixture.rsindex 429a319558..68673b4c94 100644--- a/rust/crates/test-support/src/account_fixture.rs+++ b/rust/crates/test-support/src/account_fixture.rs@@ -12,7 +12,7 @@ pub const PASSWORD: &str = "museum-account-fixture-password";// Argon2id(PASSWORD, 16 Γ 0x4d, 256 MiB, 16 ops).pub const KEK: &str = "EwUWye3Qiu3bep2oujaO8oJvUgIdn0DSOk2g0oZ+AWs=";pub const KEK_SALT: &str = "TU1NTU1NTU1NTU1NTU1NTQ==";-pub const MEM_LIMIT: u32 = 268_435_456;+pub const MEM_LIMIT: u64 = 268_435_456;pub const OPS_LIMIT: u32 = 16;#[derive(Debug, Clone)]diff --git a/web/packages/wasm/prelogin/index.ts b/web/packages/wasm/prelogin/index.tsindex ec7f7be13f..f29bb45dce 100644--- a/web/packages/wasm/prelogin/index.ts+++ b/web/packages/wasm/prelogin/index.ts@@ -18,7 +18,8 @@ export const deriveKey = async (saltB64: string,opsLimit: number,memLimit: number,-) => (await wasm()).authDeriveKek(password, saltB64, memLimit, opsLimit);+) =>+ (await wasm()).authDeriveKek(password, saltB64, BigInt(memLimit), opsLimit);export const deriveSensitiveKey = async (password: string) =>(await wasm()).authGenerateSensitiveKek(password);diff --git a/web/packages/wasm/prelogin/tests/auth.test.ts b/web/packages/wasm/prelogin/tests/auth.test.tsindex 24d970f8b8..3d911cc6ef 100644--- a/web/packages/wasm/prelogin/tests/auth.test.ts+++ b/web/packages/wasm/prelogin/tests/auth.test.ts@@ -1,4 +1,5 @@import { expect, test } from "vitest";+import { deriveKey } from "../index";import {authDeriveSrpLoginKey,authGenerateInteractiveKek,@@ -6,7 +7,7 @@ import {cryptoGenerateKey,} from "../pkg/ente_prelogin_wasm.js";-test("generates an interactive kek bundle", () => {+test("generates an interactive kek bundle", async () => {const generated = authGenerateInteractiveKek("correct horse battery staple",);@@ -15,6 +16,17 @@ test("generates an interactive kek bundle", () => {expect(Buffer.from(generated.salt, "base64")).toHaveLength(16);expect(generated.memLimit).toBe(67_108_864);expect(generated.opsLimit).toBe(2);+ await expect(+ deriveKey(+ "correct horse battery staple",+ generated.salt,+ generated.opsLimit,+ generated.memLimit,+ ),+ ).resolves.toBe(generated.key);+ await expect(+ deriveKey("password", generated.salt, 5, 2 ** 32),+ ).rejects.toThrow("Password derivation exceeds supported resource limits");});test("generates SRP setup attributes from a kek", () => {