π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Implement hashing and salting for storing custom PIN/password in secure storage
Aman Raj Singh Mourya3fe0faff6ad6
diff --git a/mobile/lib/core/configuration.dart b/mobile/lib/core/configuration.dartindex 1a3ca413c8..2be3b0fb53 100644--- a/mobile/lib/core/configuration.dart+++ b/mobile/lib/core/configuration.dart@@ -5,6 +5,7 @@ import "dart:io";import 'package:bip39/bip39.dart' as bip39;import "package:flutter/services.dart";import 'package:flutter_secure_storage/flutter_secure_storage.dart';+import "package:flutter_sodium/flutter_sodium.dart";import 'package:logging/logging.dart';import 'package:path_provider/path_provider.dart';import 'package:photos/core/constants.dart';@@ -74,6 +75,7 @@ class Configuration {static const endPointKey = "endpoint";static const password = "user_pass";static const pin = "user_pin";+ static const saltKey = "user_salt";static final _logger = Logger("Configuration");String? _cachedToken;@@ -152,35 +154,78 @@ class Configuration {}}+ static Uint8List generateSalt() {+ return Sodium.randombytesBuf(Sodium.cryptoPwhashSaltbytes);+ }+Future<void> setPin(String userPin) async {- await _preferences.setString(pin, userPin);- await _preferences.remove(password);+ await _secureStorage.delete(key: saltKey);++ final salt = generateSalt();+ final hash = cryptoPwHash({+ "password": utf8.encode(userPin),+ "salt": salt,+ "opsLimit": Sodium.cryptoPwhashOpslimitInteractive,+ "memLimit": Sodium.cryptoPwhashMemlimitInteractive,+ });++ final String saltPin = base64Encode(salt);+ final String hashedPin = base64Encode(hash);++ await _secureStorage.write(key: saltKey, value: saltPin);+ await _secureStorage.write(key: pin, value: hashedPin);+ await _secureStorage.delete(key: password);++ return;+ }++ Future<Uint8List?> getSalt() async {+ final String? salt = await _secureStorage.read(key: saltKey);+ if (salt == null) return null;+ return base64Decode(salt);}Future<String?> getPin() async {- return _preferences.getString(pin);+ return _secureStorage.read(key: pin);}Future<void> setPassword(String pass) async {- await _preferences.setString(password, pass);- await _preferences.remove(pin);+ await _secureStorage.delete(key: saltKey);++ final salt = generateSalt();+ final hash = cryptoPwHash({+ "password": utf8.encode(pass),+ "salt": salt,+ "opsLimit": Sodium.cryptoPwhashOpslimitInteractive,+ "memLimit": Sodium.cryptoPwhashMemlimitInteractive,+ });++ final String saltPassword = base64Encode(salt);+ final String hashPassword = base64Encode(hash);++ await _secureStorage.write(key: saltKey, value: saltPassword);+ await _secureStorage.write(key: password, value: hashPassword);+ await _secureStorage.delete(key: pin);++ return;}Future<String?> getPassword() async {- return _preferences.getString(password);+ return _secureStorage.read(key: password);}Future<void> removePinAndPassword() async {- await _preferences.remove(pin);- await _preferences.remove(password);+ await _secureStorage.delete(key: saltKey);+ await _secureStorage.delete(key: pin);+ await _secureStorage.delete(key: password);}- bool isPinSet() {- return _preferences.containsKey(pin);+ Future<bool> isPinSet() async {+ return await _secureStorage.containsKey(key: pin);}- bool isPasswordSet() {- return _preferences.containsKey(password);+ Future<bool> isPasswordSet() async {+ return await _secureStorage.containsKey(key: password);}// _cleanUpStaleFiles deletes all files in the temp directory that are olderdiff --git a/mobile/lib/ui/settings/TEMP/lock_screen_option.dart b/mobile/lib/ui/settings/TEMP/lock_screen_option.dartindex 7ae211ce14..fa6827956b 100644--- a/mobile/lib/ui/settings/TEMP/lock_screen_option.dart+++ b/mobile/lib/ui/settings/TEMP/lock_screen_option.dart@@ -27,21 +27,26 @@ class _LockScreenOptionState extends State<LockScreenOption> {@overridevoid initState() {super.initState();- isPasswordEnabled = _configuration.isPasswordSet();- isPinEnabled = _configuration.isPinSet();+ _initializeSettings();appLock = isPinEnabled ||isPasswordEnabled ||_configuration.shouldShowLockScreen();}- Future<void> _deviceLock() async {- await _configuration.removePinAndPassword();+ Future<void> _initializeSettings() async {+ final bool passwordEnabled = await _configuration.isPasswordSet();+ final bool pinEnabled = await _configuration.isPinSet();setState(() {- isPasswordEnabled = _configuration.isPasswordSet();- isPinEnabled = _configuration.isPinSet();+ isPasswordEnabled = passwordEnabled;+ isPinEnabled = pinEnabled;});}+ Future<void> _deviceLock() async {+ await _configuration.removePinAndPassword();+ await _initializeSettings();+ }+Future<void> _pinLock() async {final bool result = await Navigator.of(context).push(MaterialPageRoute(@@ -51,8 +56,7 @@ class _LockScreenOptionState extends State<LockScreenOption> {),);setState(() {- isPasswordEnabled = _configuration.isPasswordSet();- isPinEnabled = _configuration.isPinSet();+ _initializeSettings();if (result == false) {appLock = appLock;} else {@@ -72,12 +76,13 @@ class _LockScreenOptionState extends State<LockScreenOption> {),);setState(() {- isPasswordEnabled = _configuration.isPasswordSet();- isPinEnabled = _configuration.isPinSet();+ _initializeSettings();if (result == false) {appLock = appLock;} else {- appLock = isPinEnabled || isPasswordEnabled;+ appLock = isPinEnabled ||+ isPasswordEnabled ||+ _configuration.shouldShowLockScreen();}});}@@ -87,8 +92,7 @@ class _LockScreenOptionState extends State<LockScreenOption> {await Configuration.instance.setShouldShowLockScreen(!appLock);await _configuration.removePinAndPassword();setState(() {- isPasswordEnabled = _configuration.isPasswordSet();- isPinEnabled = _configuration.isPinSet();+ _initializeSettings();appLock = !appLock;});}diff --git a/mobile/lib/ui/settings/TEMP/lock_screen_option_confirm_pin.dart b/mobile/lib/ui/settings/TEMP/lock_screen_option_confirm_pin.dartindex 38be79bb83..149cb54d66 100644--- a/mobile/lib/ui/settings/TEMP/lock_screen_option_confirm_pin.dart+++ b/mobile/lib/ui/settings/TEMP/lock_screen_option_confirm_pin.dart@@ -144,6 +144,7 @@ class _LockScreenOptionConfirmPinStateconst Padding(padding: EdgeInsets.all(12)),Pinput(length: 4,+ pinputAutovalidateMode: PinputAutovalidateMode.onSubmit,controller: _confirmPinController,defaultPinTheme: _pinPutDecoration,submittedPinTheme: _pinPutDecoration.copyWith(@@ -180,10 +181,8 @@ class _LockScreenOptionConfirmPinStatevalidator: (value) {if (value == widget.pin) {return null;- } else {- value = null;- return 'PIN does not match';}+ return 'PIN does not match';},onCompleted: (value) async {await Future.delayed(const Duration(milliseconds: 250));diff --git a/mobile/lib/ui/settings/TEMP/lock_screen_option_password.dart b/mobile/lib/ui/settings/TEMP/lock_screen_option_password.dartindex cd86f75101..d4c1c59cfb 100644--- a/mobile/lib/ui/settings/TEMP/lock_screen_option_password.dart+++ b/mobile/lib/ui/settings/TEMP/lock_screen_option_password.dart@@ -1,11 +1,16 @@+import "dart:convert";+import "package:flutter/material.dart";import "package:flutter/services.dart";+import "package:flutter_sodium/flutter_sodium.dart";+import "package:photos/core/configuration.dart";import "package:photos/generated/l10n.dart";import "package:photos/theme/ente_theme.dart";import "package:photos/ui/common/dynamic_fab.dart";import "package:photos/ui/components/buttons/icon_button_widget.dart";import "package:photos/ui/components/text_input_widget.dart";import "package:photos/ui/settings/TEMP/lock_screen_option_confirm_password.dart";+import "package:photos/utils/crypto_util.dart";class LockScreenOptionPassword extends StatefulWidget {const LockScreenOptionPassword({@@ -26,6 +31,8 @@ class _LockScreenOptionPasswordState extends State<LockScreenOptionPassword> {final _focusNode = FocusNode();final _isFormValid = ValueNotifier<bool>(false);final _submitNotifier = ValueNotifier(false);+ Configuration configuration = Configuration.instance;+ late String hashedPassword;@overridevoid initState() {super.initState();@@ -43,7 +50,16 @@ class _LockScreenOptionPasswordState extends State<LockScreenOptionPassword> {}Future<bool> confirmPasswordAuth(String code) async {- if (widget.authPass == code) {+ final Uint8List? salt = await configuration.getSalt();+ final hash = cryptoPwHash({+ "password": utf8.encode(code),+ "salt": salt,+ "opsLimit": Sodium.cryptoPwhashOpslimitInteractive,+ "memLimit": Sodium.cryptoPwhashMemlimitInteractive,+ });++ hashedPassword = base64Encode(hash);+ if (widget.authPass == hashedPassword) {Navigator.of(context).pop(true);return true;} else {diff --git a/mobile/lib/ui/settings/TEMP/lock_screen_option_pin.dart b/mobile/lib/ui/settings/TEMP/lock_screen_option_pin.dartindex f7956b7efc..bbc467f721 100644--- a/mobile/lib/ui/settings/TEMP/lock_screen_option_pin.dart+++ b/mobile/lib/ui/settings/TEMP/lock_screen_option_pin.dart@@ -1,10 +1,15 @@+import "dart:convert";+import "package:flutter/material.dart";import "package:flutter/services.dart";+import "package:flutter_sodium/flutter_sodium.dart";+import "package:photos/core/configuration.dart";import "package:photos/theme/colors.dart";import "package:photos/theme/ente_theme.dart";import "package:photos/theme/text_style.dart";import "package:photos/ui/components/buttons/icon_button_widget.dart";import "package:photos/ui/settings/TEMP/lock_screen_option_confirm_pin.dart";+import "package:photos/utils/crypto_util.dart";import 'package:pinput/pinput.dart';class LockScreenOptionPin extends StatefulWidget {@@ -22,7 +27,8 @@ class LockScreenOptionPin extends StatefulWidget {class _LockScreenOptionPinState extends State<LockScreenOptionPin> {final _pinController = TextEditingController(text: null);-+ Configuration configuration = Configuration.instance;+ late String hashedPin;@overridevoid dispose() {super.dispose();@@ -43,10 +49,20 @@ class _LockScreenOptionPinState extends State<LockScreenOptionPin> {}Future<bool> confirmPinAuth(String code) async {- if (widget.authPin == code) {+ final Uint8List? salt = await configuration.getSalt();+ final hash = cryptoPwHash({+ "password": utf8.encode(code),+ "salt": salt,+ "opsLimit": Sodium.cryptoPwhashOpslimitInteractive,+ "memLimit": Sodium.cryptoPwhashMemlimitInteractive,+ });++ hashedPin = base64Encode(hash);+ if (widget.authPin == hashedPin) {Navigator.of(context).pop(true);return true;}+_pinController.clear();await HapticFeedback.vibrate();return false;@@ -196,14 +212,14 @@ class _LockScreenOptionPinState extends State<LockScreenOptionPin> {textStyle:textTheme.h3Bold.copyWith(color: colorTheme.warning400),),- validator: widget.isAuthenticating- ? (value) {- if (widget.authPin == value) {- return null;- }- return 'Invalid PIN';- }- : null,+ // validator: widget.isAuthenticating+ // ? (value) {+ // if (widget.authPin == hashedPin) {+ // return null;+ // }+ // return 'Invalid PIN';+ // }+ // : null,errorText: '',obscureText: true,obscuringCharacter: '*',