Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add lock-screen verify with a legacy-hash fallback for photos

Manav Rathi34fd2eb4a968
diff --git a/mobile/packages/lock_screen/lib/lock_screen_settings.dart b/mobile/packages/lock_screen/lib/lock_screen_settings.dart
index 9b92bcf016..c0ea5976ae 100644
--- a/mobile/packages/lock_screen/lib/lock_screen_settings.dart
+++ b/mobile/packages/lock_screen/lib/lock_screen_settings.dart
@@ -194,6 +194,66 @@ class LockScreenSettings {
return _secureStorage.read(key: password);
}
+ /// Verifies that the hash of [text] matches [storedHash].
+ Future<bool> verify({
+ required String text,
+ required String? storedHash,
+ }) async {
+ if (storedHash == null) return false;
+ final Uint8List? salt = await getSalt();
+ if (salt == null) return false;
+ final hash = base64Encode(
+ CryptoUtil.cryptoPwHash(
+ utf8.encode(text),
+ salt,
+ CryptoUtil.pwhashMemLimitInteractive,
+ CryptoUtil.pwhashOpsLimitSensitive,
+ ),
+ );
+ return hash == storedHash;
+ }
+
+ /// Like [verify], but for secrets created by photos' lock screen.
+ ///
+ /// On a miss it retries with photos' legacy (Interactive ops) parameters and,
+ /// on a hit, upgrades the stored hash to the current parameters under
+ /// [storageKey]. The re-store is best-effort, so a correct secret is never
+ /// rejected.
+ Future<bool> verifyWithLegacyFallback({
+ required String text,
+ required String? storedHash,
+ required String storageKey,
+ }) async {
+ if (await verify(text: text, storedHash: storedHash)) return true;
+ if (storedHash == null) return false;
+ final Uint8List? salt = await getSalt();
+ if (salt == null) return false;
+ final secret = utf8.encode(text);
+
+ final legacy = base64Encode(
+ CryptoUtil.cryptoPwHash(
+ secret,
+ salt,
+ CryptoUtil.pwhashMemLimitInteractive,
+ CryptoUtil.pwhashOpsLimitInteractive,
+ ),
+ );
+ if (legacy != storedHash) return false;
+
+ final upgraded = base64Encode(
+ CryptoUtil.cryptoPwHash(
+ secret,
+ salt,
+ CryptoUtil.pwhashMemLimitInteractive,
+ CryptoUtil.pwhashOpsLimitSensitive,
+ ),
+ );
+ try {
+ await _secureStorage.write(key: storageKey, value: upgraded);
+ } catch (_) {}
+ return true;
+ }
+
Future<void> removePinAndPassword() async {
await _secureStorage.delete(key: saltKey);
await _secureStorage.delete(key: pin);
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart
index 9e728142f7..c07f36c4f9 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart
@@ -1,6 +1,3 @@
-import "dart:convert";
-
-import "package:ente_crypto_api/ente_crypto_api.dart";
import "package:ente_lock_screen/lock_screen_settings.dart";
import "package:ente_lock_screen/ui/lock_screen_confirm_password.dart";
import "package:ente_lock_screen/ui/lock_screen_options.dart";
@@ -167,14 +164,11 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
}
Future<bool> _confirmPasswordAuth(String inputtedPassword) async {
- final Uint8List? salt = await _lockscreenSetting.getSalt();
- final hash = CryptoUtil.cryptoPwHash(
- utf8.encode(inputtedPassword),
- salt!,
- CryptoUtil.pwhashMemLimitInteractive,
- CryptoUtil.pwhashOpsLimitSensitive,
+ final matched = await _lockscreenSetting.verify(
+ text: inputtedPassword,
+ storedHash: widget.authPass,
);
- if (widget.authPass == base64Encode(hash)) {
+ if (matched) {
await _lockscreenSetting.setInvalidAttemptCount(0);
widget.isAuthenticatingOnAppLaunch ||
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart
index f2b91839ee..d82e857a90 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart
@@ -1,7 +1,5 @@
-import "dart:convert";
import "dart:io";
-import "package:ente_crypto_api/ente_crypto_api.dart";
import "package:ente_lock_screen/lock_screen_settings.dart";
import "package:ente_lock_screen/ui/custom_pin_keypad.dart";
import "package:ente_lock_screen/ui/lock_screen_confirm_pin.dart";
@@ -65,14 +63,11 @@ class _LockScreenPinState extends State<LockScreenPin> {
}
Future<bool> confirmPinAuth(String inputtedPin) async {
- final Uint8List? salt = await _lockscreenSetting.getSalt();
- final hash = CryptoUtil.cryptoPwHash(
- utf8.encode(inputtedPin),
- salt!,
- CryptoUtil.pwhashMemLimitInteractive,
- CryptoUtil.pwhashOpsLimitSensitive,
+ final matched = await _lockscreenSetting.verify(
+ text: inputtedPin,
+ storedHash: widget.authPin,
);
- if (widget.authPin == base64Encode(hash)) {
+ if (matched) {
invalidAttemptsCount = 0;
await _lockscreenSetting.setInvalidAttemptCount(0);
widget.isAuthenticatingOnAppLaunch ||