From 73af752f5285ff29bc59dc97ce6c67bc3fede1bc Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:08:56 +0530 Subject: [PATCH] [mob] LowerMem & increase ops limit for key derivation --- mobile/lib/utils/crypto_util.dart | 40 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/mobile/lib/utils/crypto_util.dart b/mobile/lib/utils/crypto_util.dart index 2005e7882f..e48ca1587d 100644 --- a/mobile/lib/utils/crypto_util.dart +++ b/mobile/lib/utils/crypto_util.dart @@ -18,6 +18,8 @@ const int loginSubKeyLen = 32; const int loginSubKeyId = 1; const String loginSubKeyContext = "loginctx"; +const int _keyDerivcationStregth = 1073741824 * 4; + Uint8List cryptoSecretboxEasy(Map args) { return Sodium.cryptoSecretboxEasy(args["source"], args["nonce"], args["key"]); } @@ -392,24 +394,28 @@ class CryptoUtil { Uint8List salt, ) async { final logger = Logger("pwhash"); - int memLimit = Sodium.cryptoPwhashMemlimitSensitive; - int opsLimit = Sodium.cryptoPwhashOpslimitSensitive; - if (await isLowSpecDevice()) { - logger.info("low spec device detected"); - // When sensitive memLimit (1 GB) is used, on low spec device the OS might - // kill the app with OOM. To avoid that, start with 256 MB and - // corresponding ops limit (16). - // This ensures that the product of these two variables - // (the area under the graph that determines the amount of work required) - // stays the same - // SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE: 1073741824 - // SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE: 268435456 - // SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE: 4 - memLimit = Sodium.cryptoPwhashMemlimitModerate; - final factor = Sodium.cryptoPwhashMemlimitSensitive ~/ - Sodium.cryptoPwhashMemlimitModerate; // = 4 - opsLimit = opsLimit * factor; // = 16 + final int desiredStrength = Sodium.cryptoPwhashMemlimitSensitive * + Sodium.cryptoPwhashOpslimitSensitive; + // When sensitive memLimit (1 GB) is used, on low spec device the OS might + // kill the app with OOM. To avoid that, start with 256 MB and + // corresponding ops limit (16). + // This ensures that the product of these two variables + // (the area under the graph that determines the amount of work required) + // stays the same + // SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE: 1073741824 + // SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE: 268435456 + // SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE: 4 + int memLimit = Sodium.cryptoPwhashMemlimitModerate; + final factor = Sodium.cryptoPwhashMemlimitSensitive ~/ + Sodium.cryptoPwhashMemlimitModerate; // = 4 + int opsLimit = Sodium.cryptoPwhashOpslimitSensitive * factor; // = 16 + if (memLimit * opsLimit != desiredStrength || + desiredStrength != _keyDerivcationStregth) { + throw UnsupportedError( + "unexpcted values for memLimit $memLimit and opsLimit: $opsLimit or desiredStrength: $desiredStrength", + ); } + Uint8List key; while (memLimit >= Sodium.cryptoPwhashMemlimitMin && opsLimit <= Sodium.cryptoPwhashOpslimitMax) {