From 7fd0b8f4078ffaafe25015038bc9e370b42e24a5 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 6 Feb 2023 11:29:45 +0530 Subject: [PATCH] Refactor and start with 256MB as memLimit for lowSpecDevices --- lib/ui/home_page.dart | 1 + lib/utils/crypto_util.dart | 28 +++++++--------------------- lib/utils/device_info.dart | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 lib/utils/device_info.dart diff --git a/lib/ui/home_page.dart b/lib/ui/home_page.dart index 085cce024f..c14c1c6c95 100644 --- a/lib/ui/home_page.dart +++ b/lib/ui/home_page.dart @@ -17,6 +17,7 @@ import 'package:ente_auth/ui/code_widget.dart'; import 'package:ente_auth/ui/common/loading_widget.dart'; import 'package:ente_auth/ui/scanner_page.dart'; import 'package:ente_auth/ui/settings_page.dart'; +import 'package:ente_auth/utils/device_info.dart'; import 'package:flutter/material.dart'; import 'package:flutter_speed_dial/flutter_speed_dial.dart'; import 'package:move_to_background/move_to_background.dart'; diff --git a/lib/utils/crypto_util.dart b/lib/utils/crypto_util.dart index 7fa9139516..6413da9b51 100644 --- a/lib/utils/crypto_util.dart +++ b/lib/utils/crypto_util.dart @@ -1,10 +1,10 @@ import 'dart:io' as io; -import 'dart:io'; import 'dart:typed_data'; import 'package:computer/computer.dart'; import 'package:ente_auth/models/derived_key_result.dart'; import 'package:ente_auth/models/encryption_result.dart'; +import 'package:ente_auth/utils/device_info.dart'; import 'package:flutter_sodium/flutter_sodium.dart'; import 'package:logging/logging.dart'; @@ -283,35 +283,21 @@ class CryptoUtil { return Sodium.cryptoBoxSeal(input, publicKey); } - static bool _isLowSpecDevice() { - try { - if (Platform.isIOS) { - String version = Platform.operatingSystemVersion; - int majorVersion = int.parse(version.split('.').first); - // iPhone 6 or lower - if (majorVersion <= 9) { - return true; - } - } - } catch (e) { - Logger("_isLowSpecDevice").severe("deviceSpec check failed", e); - } - return false; - } - static Future deriveSensitiveKey( Uint8List password, Uint8List salt, ) async { final logger = Logger("pwhash"); + // Default with 1 GB mem and 4 ops limit int memLimit = Sodium.cryptoPwhashMemlimitSensitive; int opsLimit = Sodium.cryptoPwhashOpslimitSensitive; - if (_isLowSpecDevice()) { + if (await isLowSpecDevice()) { + logger.info("low spec device detected"); // When high memLimit is used, on low spec device the OS might kill the - // app with OOM. To avoid that, default to max ops limit - memLimit = Sodium.cryptoPwhashMemlimitMin; - opsLimit = Sodium.cryptoPwhashOpslimitMax; + // app with OOM. To avoid that, start with 256 MB and 16 ops limit + memLimit = Sodium.cryptoPwhashMemlimitModerate; + opsLimit = 16; } Uint8List key; while (memLimit >= Sodium.cryptoPwhashMemlimitMin && diff --git a/lib/utils/device_info.dart b/lib/utils/device_info.dart new file mode 100644 index 0000000000..ee60c5c523 --- /dev/null +++ b/lib/utils/device_info.dart @@ -0,0 +1,31 @@ +import 'dart:io'; + +import 'package:device_info_plus/device_info_plus.dart'; +import 'package:flutter/foundation.dart'; +import 'package:logging/logging.dart'; + +late DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin(); + +// https://gist.github.com/adamawolf/3048717 +late Set iOSLowEndMachineCodes = { + "iPhone5,2", + "iPhone5,3", + "iPhone5,4", + "iPhone6,1", + "iPhone6,2", + "iPhone7,2", + "iPhone7,1", +}; + +Future isLowSpecDevice() async { + try { + if (Platform.isIOS) { + final IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo; + debugPrint("ios utc name ${iosInfo.utsname.machine}"); + return iOSLowEndMachineCodes.contains(iosInfo.utsname.machine); + } + } catch (e) { + Logger("device_info").severe("deviceSpec check failed", e); + } + return false; +}