Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile] Translate Dart crypto derivation errors

James Wagar43b5c26cb59e
diff --git a/mobile/packages/ente_crypto_dart_adapter/lib/ente_crypto_dart_adapter.dart b/mobile/packages/ente_crypto_dart_adapter/lib/ente_crypto_dart_adapter.dart
index 9c18f9a8db..8229156cb9 100644
--- a/mobile/packages/ente_crypto_dart_adapter/lib/ente_crypto_dart_adapter.dart
+++ b/mobile/packages/ente_crypto_dart_adapter/lib/ente_crypto_dart_adapter.dart
@@ -3,6 +3,18 @@ import 'dart:typed_data';
import 'package:ente_crypto_api/ente_crypto_api.dart';
import 'package:ente_crypto_dart/ente_crypto_dart.dart' as dart_impl;
+import 'package:meta/meta.dart';
+
+@visibleForTesting
+Future<T> translateDartCryptoErrors<T>(Future<T> Function() operation) async {
+ try {
+ return await operation();
+ } on dart_impl.KeyDerivationError {
+ throw KeyDerivationError();
+ } on dart_impl.LoginKeyDerivationError {
+ throw LoginKeyDerivationError();
+ }
+}
class EnteCryptoDartAdapter implements CryptoApi {
const EnteCryptoDartAdapter();
@@ -122,9 +134,8 @@ class EnteCryptoDartAdapter implements CryptoApi {
Uint8List password,
Uint8List salt,
) async {
- final result = await dart_impl.CryptoUtil.deriveSensitiveKey(
- password,
- salt,
+ final result = await translateDartCryptoErrors(
+ () => dart_impl.CryptoUtil.deriveSensitiveKey(password, salt),
);
return DerivedKeyResult(result.key, result.memLimit, result.opsLimit);
}
@@ -134,9 +145,8 @@ class EnteCryptoDartAdapter implements CryptoApi {
Uint8List password,
Uint8List salt,
) async {
- final result = await dart_impl.CryptoUtil.deriveInteractiveKey(
- password,
- salt,
+ final result = await translateDartCryptoErrors(
+ () => dart_impl.CryptoUtil.deriveInteractiveKey(password, salt),
);
return DerivedKeyResult(result.key, result.memLimit, result.opsLimit);
}
@@ -147,11 +157,13 @@ class EnteCryptoDartAdapter implements CryptoApi {
Uint8List salt,
int memLimit,
int opsLimit,
- ) => dart_impl.CryptoUtil.deriveKey(password, salt, memLimit, opsLimit);
+ ) => translateDartCryptoErrors(
+ () => dart_impl.CryptoUtil.deriveKey(password, salt, memLimit, opsLimit),
+ );
@override
Future<Uint8List> deriveLoginKey(Uint8List key) =>
- dart_impl.CryptoUtil.deriveLoginKey(key);
+ translateDartCryptoErrors(() => dart_impl.CryptoUtil.deriveLoginKey(key));
@override
Uint8List getSaltToDeriveKey() => dart_impl.CryptoUtil.getSaltToDeriveKey();
diff --git a/mobile/packages/ente_crypto_dart_adapter/pubspec.yaml b/mobile/packages/ente_crypto_dart_adapter/pubspec.yaml
index 0d9c1afe04..752c6e312c 100644
--- a/mobile/packages/ente_crypto_dart_adapter/pubspec.yaml
+++ b/mobile/packages/ente_crypto_dart_adapter/pubspec.yaml
@@ -16,3 +16,5 @@ dependencies:
dev_dependencies:
flutter_lints: 6.0.0
+ flutter_test:
+ sdk: flutter
diff --git a/mobile/packages/ente_crypto_dart_adapter/test/ente_crypto_dart_adapter_test.dart b/mobile/packages/ente_crypto_dart_adapter/test/ente_crypto_dart_adapter_test.dart
new file mode 100644
index 0000000000..ff9fc724bb
--- /dev/null
+++ b/mobile/packages/ente_crypto_dart_adapter/test/ente_crypto_dart_adapter_test.dart
@@ -0,0 +1,31 @@
+import 'package:ente_crypto_api/ente_crypto_api.dart';
+import 'package:ente_crypto_dart/ente_crypto_dart.dart' as dart_impl;
+import 'package:ente_crypto_dart_adapter/ente_crypto_dart_adapter.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ group('translateDartCryptoErrors', () {
+ test('translates KeyDerivationError to the API error type', () async {
+ final future = translateDartCryptoErrors<void>(
+ () async => throw dart_impl.KeyDerivationError(),
+ );
+
+ await expectLater(future, throwsA(isA<KeyDerivationError>()));
+ });
+
+ test('translates LoginKeyDerivationError to the API error type', () async {
+ final future = translateDartCryptoErrors<void>(
+ () async => throw dart_impl.LoginKeyDerivationError(),
+ );
+
+ await expectLater(future, throwsA(isA<LoginKeyDerivationError>()));
+ });
+
+ test('preserves unrelated errors', () async {
+ final error = StateError('test');
+ final future = translateDartCryptoErrors<void>(() async => throw error);
+
+ await expectLater(future, throwsA(same(error)));
+ });
+ });
+}