π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move recovery key mnemonic conversion to shared Rust
r4khula8eb9d522c60
diff --git a/mobile/apps/auth/lib/core/configuration.dart b/mobile/apps/auth/lib/core/configuration.dartindex ea00ef6042..23948ba8b2 100644--- a/mobile/apps/auth/lib/core/configuration.dart+++ b/mobile/apps/auth/lib/core/configuration.dart@@ -5,6 +5,8 @@ import 'package:ente_account_deletion/account_deletion.dart';import 'package:ente_auth/services/authenticated_session.dart';import 'package:ente_auth/src/rust/third_party/ente_frb_lib/account_deletion.dart'as account_deletion;+import 'package:ente_auth/src/rust/third_party/ente_frb_lib/recovery.dart'+ as recovery;import 'package:ente_base/models/database.dart';import 'package:ente_configuration/base_configuration.dart';import 'package:ente_crypto_api/ente_crypto_api.dart';@@ -106,6 +108,16 @@ class Configuration extends BaseConfigurationfeedback: feedback,);+ @override+ String recoveryKeyToMnemonic(String recoveryKeyHex) =>+ recovery.recoveryKeyToMnemonic(recoveryKeyHex: recoveryKeyHex);++ @override+ String recoveryKeyFromMnemonicOrHex(String recoveryKeyMnemonicOrHex) =>+ recovery.recoveryKeyFromMnemonicOrHex(+ recoveryKeyMnemonicOrHex: recoveryKeyMnemonicOrHex,+ );+@overrideFuture<void> logout({bool autoLogout = false}) async {clearAuthenticatedSession();diff --git a/mobile/apps/auth/lib/core/constants.dart b/mobile/apps/auth/lib/core/constants.dartindex e800ca50bc..edadf30fde 100644--- a/mobile/apps/auth/lib/core/constants.dart+++ b/mobile/apps/auth/lib/core/constants.dart@@ -20,10 +20,6 @@ const String livePhotoToastCounterKey = "show_live_photo_toast";const thumbnailDiskLoadDeferDuration = Duration(milliseconds: 40);const thumbnailServerLoadDeferDuration = Duration(milliseconds: 80);-// 256 bit key maps to 24 words-// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#Generating_the_mnemonic-const mnemonicKeyWordCount = 24;-class FFDefault {static const bool enableStripe = true;static const bool disableCFWorker = false;diff --git a/mobile/apps/auth/lib/ui/components/recovery_key_sheet.dart b/mobile/apps/auth/lib/ui/components/recovery_key_sheet.dartindex 8d6b7fcbfb..b7b4040310 100644--- a/mobile/apps/auth/lib/ui/components/recovery_key_sheet.dart+++ b/mobile/apps/auth/lib/ui/components/recovery_key_sheet.dart@@ -1,9 +1,7 @@import 'dart:io' as io;-import 'package:bip39/bip39.dart' as bip39;import 'package:ente_auth/core/configuration.dart';import 'package:ente_configuration/base_configuration.dart';-import 'package:ente_configuration/constants.dart';import 'package:ente_strings/ente_strings.dart';import "package:ente_ui/components/base_bottom_sheet.dart";import 'package:ente_ui/components/buttons/gradient_button.dart';@@ -49,16 +47,10 @@ class _RecoveryKeySheetState extends State<RecoveryKeySheet> {final colorScheme = getEnteColorScheme(context);final textTheme = getEnteTextTheme(context);- final String recoveryKeyMnemonic = bip39.entropyToMnemonic(+ final String recoveryKeyMnemonic = _config.recoveryKeyToMnemonic(widget.recoveryKey,);- if (recoveryKeyMnemonic.split(' ').length != mnemonicKeyWordCount) {- throw AssertionError(- 'Recovery code should have $mnemonicKeyWordCount words',- );- }-return Column(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.start,diff --git a/mobile/apps/auth/pubspec.yaml b/mobile/apps/auth/pubspec.yamlindex 5fd85aa469..6da8814cea 100644--- a/mobile/apps/auth/pubspec.yaml+++ b/mobile/apps/auth/pubspec.yaml@@ -14,7 +14,6 @@ dependencies:backup_exclusion:path: ../../packages/backup_exclusionbase32: 2.1.3- bip39: 1.0.6clipboard: 0.1.3collection: 1.19.1confetti: 0.8.0diff --git a/mobile/apps/locker/lib/core/constants.dart b/mobile/apps/locker/lib/core/constants.dartindex 4311e05b91..c2ebff1e65 100644--- a/mobile/apps/locker/lib/core/constants.dart+++ b/mobile/apps/locker/lib/core/constants.dart@@ -2,10 +2,6 @@ import 'package:flutter/foundation.dart';const int microSecondsInDay = 86400000000;-// 256 bit key maps to 24 words-// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#Generating_the_mnemonic-const mnemonicKeyWordCount = 24;-const kDefaultProductionEndpoint = 'https://api.ente.com';const String githubDiscussionsUrl = "https://github.com/ente/ente/discussions";diff --git a/mobile/apps/locker/lib/services/configuration.dart b/mobile/apps/locker/lib/services/configuration.dartindex 5023270160..dc06906409 100644--- a/mobile/apps/locker/lib/services/configuration.dart+++ b/mobile/apps/locker/lib/services/configuration.dart@@ -9,6 +9,8 @@ import 'package:locker/services/favorites_service.dart';import 'package:locker/services/files/offline/offline_file_storage.dart';import 'package:locker/src/rust/third_party/ente_frb_lib/account_deletion.dart'as account_deletion;+import 'package:locker/src/rust/third_party/ente_frb_lib/recovery.dart'+ as recovery;import 'package:logging/logging.dart';import 'package:shared_preferences/shared_preferences.dart';@@ -68,6 +70,16 @@ class Configuration extends BaseConfigurationfeedback: feedback,);+ @override+ String recoveryKeyToMnemonic(String recoveryKeyHex) =>+ recovery.recoveryKeyToMnemonic(recoveryKeyHex: recoveryKeyHex);++ @override+ String recoveryKeyFromMnemonicOrHex(String recoveryKeyMnemonicOrHex) =>+ recovery.recoveryKeyFromMnemonicOrHex(+ recoveryKeyMnemonicOrHex: recoveryKeyMnemonicOrHex,+ );+@overrideFuture<void> logout({bool autoLogout = false}) async {CollectionService.instance.clearCache();diff --git a/mobile/apps/locker/lib/ui/components/recovery_key_sheet.dart b/mobile/apps/locker/lib/ui/components/recovery_key_sheet.dartindex ca4b5940a4..2df4ee790a 100644--- a/mobile/apps/locker/lib/ui/components/recovery_key_sheet.dart+++ b/mobile/apps/locker/lib/ui/components/recovery_key_sheet.dart@@ -1,9 +1,7 @@import 'dart:io' as io;-import 'package:bip39/bip39.dart' as bip39;import 'package:ente_components/ente_components.dart';import 'package:ente_configuration/base_configuration.dart';-import 'package:ente_configuration/constants.dart';import 'package:ente_strings/ente_strings.dart';import 'package:ente_ui/utils/toast_util.dart';import 'package:flutter/material.dart';@@ -46,16 +44,10 @@ class _RecoveryKeySheetState extends State<RecoveryKeySheet> {Widget build(BuildContext context) {final colors = context.componentColors;- final String recoveryKeyMnemonic = bip39.entropyToMnemonic(+ final String recoveryKeyMnemonic = _config.recoveryKeyToMnemonic(widget.recoveryKey,);- if (recoveryKeyMnemonic.split(' ').length != mnemonicKeyWordCount) {- throw AssertionError(- 'Recovery code should have $mnemonicKeyWordCount words',- );- }-return Column(mainAxisSize: MainAxisSize.min,crossAxisAlignment: CrossAxisAlignment.start,diff --git a/mobile/apps/locker/pubspec.yaml b/mobile/apps/locker/pubspec.yamlindex f5b90c4d8c..ad602a82e9 100644--- a/mobile/apps/locker/pubspec.yaml+++ b/mobile/apps/locker/pubspec.yaml@@ -10,7 +10,6 @@ environment:dependencies:adaptive_theme: 3.7.2app_links: 6.4.1- bip39: 1.0.6camera: 0.12.1collection: 1.19.1dio: 5.9.2diff --git a/mobile/apps/photos/lib/core/configuration.dart b/mobile/apps/photos/lib/core/configuration.dartindex b8d490ed81..941298cbcc 100644--- a/mobile/apps/photos/lib/core/configuration.dart+++ b/mobile/apps/photos/lib/core/configuration.dart@@ -3,7 +3,6 @@ import 'dart:convert';import "dart:io";import 'package:backup_exclusion/backup_exclusion.dart';-import 'package:bip39/bip39.dart' as bip39;import 'package:ente_account_deletion/account_deletion.dart';import 'package:ente_contacts/contacts.dart';import "package:ente_crypto/ente_crypto.dart";@@ -58,6 +57,8 @@ import 'package:photos/services/sync/sync_service.dart';import 'package:photos/services/video_preview_service.dart';import 'package:photos/src/rust/third_party/ente_frb_lib/account_deletion.dart'as account_deletion;+import 'package:photos/src/rust/third_party/ente_frb_lib/recovery.dart'+ as recovery;import 'package:photos/utils/validator_util.dart';import 'package:shared_preferences/shared_preferences.dart';import "package:tuple/tuple.dart";@@ -490,16 +491,9 @@ class Configuration implements LockScreenHost, AccountDeletionHost {}Future<void> recover(String recoveryKey) async {- // Legacy users will have recoveryKey in the form of a hex string, while- // newer users will have it as a mnemonic code- if (recoveryKey.contains(' ')) {- if (recoveryKey.split(' ').length != mnemonicKeyWordCount) {- throw AssertionError(- 'recovery code should have $mnemonicKeyWordCount words',- );- }- recoveryKey = bip39.mnemonicToEntropy(recoveryKey);- }+ recoveryKey = recovery.recoveryKeyFromMnemonicOrHex(+ recoveryKeyMnemonicOrHex: recoveryKey,+ );final attributes = getKeyAttributes();Uint8List masterKey;try {diff --git a/mobile/apps/photos/lib/core/constants.dart b/mobile/apps/photos/lib/core/constants.dartindex abdcc396b2..581cb1c488 100644--- a/mobile/apps/photos/lib/core/constants.dart+++ b/mobile/apps/photos/lib/core/constants.dart@@ -31,10 +31,6 @@ const galleryThumbnailServerLoadDeferDuration = Duration(milliseconds: 80);const mediaManagementHintDismissDuration = Duration(days: 7);const mediaManagementHintDeleteAttemptThreshold = 3;-// 256 bit key maps to 24 words-// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#Generating_the_mnemonic-const mnemonicKeyWordCount = 24;-const dragSensitivity = 8;const supportEmail = '[email protected]';diff --git a/mobile/apps/photos/lib/ui/account/recovery_key_page.dart b/mobile/apps/photos/lib/ui/account/recovery_key_page.dartindex bd45d5d855..c8ef77ae14 100644--- a/mobile/apps/photos/lib/ui/account/recovery_key_page.dart+++ b/mobile/apps/photos/lib/ui/account/recovery_key_page.dart@@ -1,14 +1,14 @@import 'dart:async';import 'dart:io';-import 'package:bip39/bip39.dart' as bip39;import 'package:ente_components/ente_components.dart';import "package:ente_strings/ente_strings.dart";import 'package:flutter/material.dart';import 'package:flutter/services.dart';import 'package:hugeicons/hugeicons.dart';import 'package:photos/core/configuration.dart';-import 'package:photos/core/constants.dart';+import 'package:photos/src/rust/third_party/ente_frb_lib/recovery.dart'+ as recovery;import "package:photos/ui/account/onboarding_page_scaffold.dart";import 'package:photos/ui/notification/toast.dart';import 'package:photos/utils/share_util.dart';@@ -47,12 +47,9 @@ class _RecoveryKeyPageState extends State<RecoveryKeyPage> {Widget build(BuildContext context) {final colors = context.componentColors;- final String recoveryKey = bip39.entropyToMnemonic(widget.recoveryKey);- if (recoveryKey.split(' ').length != mnemonicKeyWordCount) {- throw AssertionError(- 'recovery code should have $mnemonicKeyWordCount words',- );- }+ final String recoveryKey = recovery.recoveryKeyToMnemonic(+ recoveryKeyHex: widget.recoveryKey,+ );if (widget.isOnboarding) {return OnboardingPageScaffold(diff --git a/mobile/apps/photos/lib/ui/account/verify_recovery_page.dart b/mobile/apps/photos/lib/ui/account/verify_recovery_page.dartindex f885d18150..f5bbaffce3 100644--- a/mobile/apps/photos/lib/ui/account/verify_recovery_page.dart+++ b/mobile/apps/photos/lib/ui/account/verify_recovery_page.dart@@ -1,4 +1,3 @@-import 'package:bip39/bip39.dart' as bip39;import 'package:dio/dio.dart';import "package:ente_components/ente_components.dart";import 'package:ente_crypto/ente_crypto.dart';@@ -11,6 +10,8 @@ import 'package:photos/core/event_bus.dart';import 'package:photos/events/notification_event.dart';import "package:photos/service_locator.dart";import 'package:photos/services/account/user_service.dart';+import 'package:photos/src/rust/third_party/ente_frb_lib/recovery.dart'+ as recovery;import 'package:photos/ui/account/recovery_key_page.dart';import "package:photos/ui/components/alert_bottom_sheet.dart";import 'package:photos/ui/components/buttons/button_widget.dart';@@ -45,7 +46,9 @@ class _VerifyRecoveryPageState extends State<VerifyRecoveryPage> {final String recoveryKey = CryptoUtil.bin2hex(await UserService.instance.getOrCreateRecoveryKey(context),);- final String recoveryKeyWords = bip39.entropyToMnemonic(recoveryKey);+ final String recoveryKeyWords = recovery.recoveryKeyToMnemonic(+ recoveryKeyHex: recoveryKey,+ );if (inputKey == recoveryKey || inputKey == recoveryKeyWords) {try {await flagService.setRecoveryKeyVerified(true);diff --git a/mobile/packages/accounts/lib/pages/recovery_key_page.dart b/mobile/packages/accounts/lib/pages/recovery_key_page.dartindex 9fa52cc81e..ab6af66ada 100644--- a/mobile/packages/accounts/lib/pages/recovery_key_page.dart+++ b/mobile/packages/accounts/lib/pages/recovery_key_page.dart@@ -1,12 +1,10 @@import 'dart:convert';import 'dart:io' as io;-import 'package:bip39/bip39.dart' as bip39;import 'package:dots_indicator/dots_indicator.dart';import 'package:ente_accounts/widgets/account_app_bar_logo.dart';import 'package:ente_components/ente_components.dart';import 'package:ente_configuration/base_configuration.dart';-import 'package:ente_configuration/constants.dart';import 'package:ente_pure_utils/ente_pure_utils.dart';import 'package:ente_strings/ente_strings.dart';import 'package:ente_ui/theme/ente_theme.dart';@@ -62,12 +60,9 @@ class _RecoveryKeyPageState extends State<RecoveryKeyPage> {@overrideWidget build(BuildContext context) {- final String recoveryKey = bip39.entropyToMnemonic(widget.recoveryKey);- if (recoveryKey.split(' ').length != mnemonicKeyWordCount) {- throw AssertionError(- 'recovery code should have $mnemonicKeyWordCount words',- );- }+ final String recoveryKey = widget.config.recoveryKeyToMnemonic(+ widget.recoveryKey,+ );final colorScheme = getEnteColorScheme(context);final textTheme = getEnteTextTheme(context);diff --git a/mobile/packages/accounts/pubspec.yaml b/mobile/packages/accounts/pubspec.yamlindex 9e3ea0d8fd..9ce099eb7d 100644--- a/mobile/packages/accounts/pubspec.yaml+++ b/mobile/packages/accounts/pubspec.yaml@@ -8,7 +8,6 @@ environment:dependencies:app_links: 6.4.1- bip39: 1.0.6collection: 1.19.1dio: 5.9.2dots_indicator: 4.0.1diff --git a/mobile/packages/configuration/lib/base_configuration.dart b/mobile/packages/configuration/lib/base_configuration.dartindex 45b66636a4..063ddd0e06 100644--- a/mobile/packages/configuration/lib/base_configuration.dart+++ b/mobile/packages/configuration/lib/base_configuration.dart@@ -1,7 +1,6 @@import 'dart:convert';import 'dart:io' as io;-import 'package:bip39/bip39.dart' as bip39;import 'package:ente_base/models/database.dart';import 'package:ente_base/models/key_attributes.dart';import 'package:ente_base/models/key_gen_result.dart';@@ -58,6 +57,10 @@ abstract class BaseConfiguration {List<String> get secureStorageKeys;+ String recoveryKeyToMnemonic(String recoveryKeyHex);++ String recoveryKeyFromMnemonicOrHex(String recoveryKeyMnemonicOrHex);+List<String> get lockScreenSecureStorageKeys => const [];Future<void> init(List<EnteBaseDatabase> dbs) async {@@ -238,25 +241,7 @@ abstract class BaseConfiguration {}Future<void> recover(String recoveryKey) async {- if (recoveryKey.contains(' ')) {- final split = recoveryKey.split(' ');- if (split.length != mnemonicKeyWordCount) {- String wordThatIsFollowedByEmptySpaceInSplit = '';- for (int i = 0; i < split.length; i++) {- final String word = split[i];- if (word.isEmpty) {- wordThatIsFollowedByEmptySpaceInSplit =- '\n\nExtra space after word at position $i';- break;- }- }- throw AssertionError(- '\nRecovery code should have $mnemonicKeyWordCount words, '- 'found ${split.length} words instead.$wordThatIsFollowedByEmptySpaceInSplit',- );- }- recoveryKey = bip39.mnemonicToEntropy(recoveryKey);- }+ recoveryKey = recoveryKeyFromMnemonicOrHex(recoveryKey);final attributes = getKeyAttributes();Uint8List masterKey;try {diff --git a/mobile/packages/configuration/lib/constants.dart b/mobile/packages/configuration/lib/constants.dartindex f0333e9bef..ea783497c2 100644--- a/mobile/packages/configuration/lib/constants.dart+++ b/mobile/packages/configuration/lib/constants.dart@@ -1,6 +1,2 @@const kDefaultProductionEndpoint = 'https://api.ente.com';const kLegacyProductionEndpoint = 'https://api.ente.io';--// 256 bit key maps to 24 words-// https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#Generating_the_mnemonic-const mnemonicKeyWordCount = 24;diff --git a/mobile/packages/configuration/pubspec.yaml b/mobile/packages/configuration/pubspec.yamlindex 2fcc49f400..4305ca8d83 100644--- a/mobile/packages/configuration/pubspec.yaml+++ b/mobile/packages/configuration/pubspec.yaml@@ -7,7 +7,6 @@ environment:flutter: ">=1.17.0"dependencies:- bip39: 1.0.6ente_base:path: ../../packages/baseente_crypto_api:diff --git a/mobile/packages/sharing/test/user_avatar_widget_test.dart b/mobile/packages/sharing/test/user_avatar_widget_test.dartindex 12830278ef..35d6bc18d1 100644--- a/mobile/packages/sharing/test/user_avatar_widget_test.dart+++ b/mobile/packages/sharing/test/user_avatar_widget_test.dart@@ -104,6 +104,14 @@ class _TestConfiguration extends BaseConfiguration {List<String> get secureStorageKeys =>BaseConfiguration.accountSecureStorageKeys;+ @override+ String recoveryKeyToMnemonic(String recoveryKeyHex) =>+ throw UnimplementedError();++ @override+ String recoveryKeyFromMnemonicOrHex(String recoveryKeyMnemonicOrHex) =>+ throw UnimplementedError();+@overrideString? getEmail() => '[email protected]';diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex a10103296f..749be274cc 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2709,6 +2709,7 @@ dependencies = ["ente-legacy","ente-locker","flutter_rust_bridge",+ "hex",][[package]]diff --git a/rust/Cargo.toml b/rust/Cargo.tomlindex 28203d1357..ababe23be2 100644--- a/rust/Cargo.toml+++ b/rust/Cargo.toml@@ -93,6 +93,7 @@ flutter_rust_bridge = "=2.13.0"flutter_rust_bridge_codegen = "=2.13.0"futures-util = "0.3.32"getrandom = { version = "0.4.0", features = ["wasm_js"] }+hex = "0.4.0"hkdf = "0.13.0"hmac = "0.13.0"icu_casemap = "2.2.0"diff --git a/rust/bindings/frb/lib/Cargo.toml b/rust/bindings/frb/lib/Cargo.tomlindex 175f3fe801..8a72b43e6b 100644--- a/rust/bindings/frb/lib/Cargo.toml+++ b/rust/bindings/frb/lib/Cargo.toml@@ -14,6 +14,7 @@ ente-core.workspace = trueente-legacy = { workspace = true, optional = true }ente-locker = { workspace = true, optional = true }flutter_rust_bridge.workspace = true+hex.workspace = true[lints]workspace = truediff --git a/rust/bindings/frb/lib/src/lib.rs b/rust/bindings/frb/lib/src/lib.rsindex 324dac6119..858cd7e8a5 100644--- a/rust/bindings/frb/lib/src/lib.rs+++ b/rust/bindings/frb/lib/src/lib.rs@@ -6,4 +6,5 @@ pub mod contacts;pub mod legacy;#[cfg(feature = "locker")]pub mod locker;+pub mod recovery;pub mod session;diff --git a/rust/bindings/frb/lib/src/recovery.rs b/rust/bindings/frb/lib/src/recovery.rsnew file mode 100644index 0000000000..bb7c0ae414--- /dev/null+++ b/rust/bindings/frb/lib/src/recovery.rs@@ -0,0 +1,30 @@+use ente_accounts::auth;+use flutter_rust_bridge::frb;++#[frb(non_opaque)]+pub enum RecoveryError {+ Other { message: String },+}++impl From<ente_accounts::Error> for RecoveryError {+ fn from(error: ente_accounts::Error) -> Self {+ Self::Other {+ message: ente_core::error::chain(&error),+ }+ }+}++#[frb(sync)]+pub fn recovery_key_to_mnemonic(recovery_key_hex: String) -> Result<String, RecoveryError> {+ let recovery_key = hex::decode(&recovery_key_hex)+ .map_err(|e| ente_accounts::Error::Decode(format!("recovery_key: {e}")))?;+ auth::recovery_key_to_mnemonic(&recovery_key).map_err(Into::into)+}++#[frb(sync)]+pub fn recovery_key_from_mnemonic_or_hex(+ recovery_key_mnemonic_or_hex: String,+) -> Result<String, RecoveryError> {+ let recovery_key = auth::recovery_key_from_mnemonic_or_hex(&recovery_key_mnemonic_or_hex)?;+ Ok(hex::encode(&recovery_key))+}diff --git a/rust/crates/accounts/Cargo.toml b/rust/crates/accounts/Cargo.tomlindex 8f8ed8613b..105563e149 100644--- a/rust/crates/accounts/Cargo.toml+++ b/rust/crates/accounts/Cargo.toml@@ -9,7 +9,7 @@ museum = []bip39 = { version = "3.0.0", default-features = false, features = ["std"] }ente-core.workspace = truegetrandom.workspace = true-hex = "0.4.0"+hex.workspace = trueserde.workspace = trueserde_json.workspace = truesha2.workspace = true