From 062315f01b292b96edddffa30b5fa3b0e28cd128 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 15 Nov 2023 11:31:25 -0600 Subject: [PATCH] Secure storage overwrite fix (#1161) * secure storage overwrite fix * add comment * bump encrypt package, use more explicit iv source --------- Co-authored-by: fossephate --- lib/core/auth_service.dart | 10 +++++----- lib/entities/encrypt.dart | 8 ++++---- pubspec_base.yaml | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/core/auth_service.dart b/lib/core/auth_service.dart index 854640015..321b5b94a 100644 --- a/lib/core/auth_service.dart +++ b/lib/core/auth_service.dart @@ -38,6 +38,9 @@ class AuthService with Store { Future setPassword(String password) async { final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword); final encodedPassword = encodedPinCode(pin: password); + // secure storage has a weird bug on macOS, where overwriting a key doesn't work, unless + // we delete what's there first: + await secureStorage.delete(key: key); await secureStorage.write(key: key, value: encodedPassword); } @@ -104,9 +107,8 @@ class AuthService with Store { } return; } -} + } - Navigator.of(context).pushNamed(Routes.auth, arguments: (bool isAuthenticatedSuccessfully, AuthPageState auth) async { if (!isAuthenticatedSuccessfully) { @@ -140,8 +142,6 @@ class AuthService with Store { } } } - - }); - + }); } } diff --git a/lib/entities/encrypt.dart b/lib/entities/encrypt.dart index 3e644a2b7..891f7a92d 100644 --- a/lib/entities/encrypt.dart +++ b/lib/entities/encrypt.dart @@ -2,18 +2,18 @@ import 'package:encrypt/encrypt.dart'; // import 'package:password/password.dart'; import 'package:cake_wallet/.secrets.g.dart' as secrets; -String encrypt({required String source, required String key, int keyLength = 16}) { +String encrypt({required String source, required String key}) { final _key = Key.fromUtf8(key); - final iv = IV.fromLength(keyLength); + final iv = IV.allZerosOfLength(16); final encrypter = Encrypter(AES(_key)); final encrypted = encrypter.encrypt(source, iv: iv); return encrypted.base64; } -String decrypt({required String source, required String key, int keyLength = 16}) { +String decrypt({required String source, required String key}) { final _key = Key.fromUtf8(key); - final iv = IV.fromLength(keyLength); + final iv = IV.allZerosOfLength(16); final encrypter = Encrypter(AES(_key)); final decrypted = encrypter.decrypt64(source, iv: iv); diff --git a/pubspec_base.yaml b/pubspec_base.yaml index 82cb697f5..573f8c671 100644 --- a/pubspec_base.yaml +++ b/pubspec_base.yaml @@ -49,7 +49,7 @@ dependencies: lottie: ^1.3.0 animate_do: ^2.1.0 cupertino_icons: ^1.0.5 - encrypt: 5.0.1 + encrypt: 5.0.2 crypto: ^3.0.2 # password: ^1.0.0 basic_utils: ^5.6.1