π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Reuse byte views during upload verification
ashil-pilot016641cdf8b1
diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dartindex 3b747b353a..bef9dde8c2 100644--- a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart@@ -7,6 +7,7 @@ import "package:crypto/crypto.dart";import "package:ente_crypto/src/models/derived_key_result.dart";import "package:ente_crypto/src/models/encryption_result.dart";import "package:ente_crypto/src/models/errors.dart";+import "package:ente_crypto/src/uint8_list_equals.dart";import "package:flutter/foundation.dart";import "package:flutter_sodium/flutter_sodium.dart";import "package:logging/logging.dart";@@ -206,7 +207,7 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(null,);- if (!_uint8listEquals(pullResult.m, buffer)) {+ if (!uint8ListEquals(pullResult.m, buffer)) {throw Exception("$kBitFlipErrorTag Data corruption detected at chunk $chunkIndex",);@@ -314,34 +315,6 @@ Future<FileEncryptResult> chachaEncryptFileWithVerification(}}-bool _uint8listEquals(Uint8List a, Uint8List b) {- if (identical(a, b)) return true;- if (a.length != b.length) return false;-- final len = a.length;- int i = 0;-- while (i + 7 < len) {- final va = a.buffer.asByteData().getUint64(- a.offsetInBytes + i,- Endian.little,- );- final vb = b.buffer.asByteData().getUint64(- b.offsetInBytes + i,- Endian.little,- );- if (va != vb) return false;- i += 8;- }-- while (i < len) {- if (a[i] != b[i]) return false;- i++;- }-- return true;-}-Future<void> chachaDecryptFile(Map<String, dynamic> args) async {final logger = Logger("ChaChaDecrypt");final decryptionStartTime = DateTime.now().millisecondsSinceEpoch;diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/uint8_list_equals.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/uint8_list_equals.dartnew file mode 100644index 0000000000..591cb114c4--- /dev/null+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/uint8_list_equals.dart@@ -0,0 +1,25 @@+import "dart:typed_data";++bool uint8ListEquals(Uint8List a, Uint8List b) {+ if (identical(a, b)) return true;+ if (a.length != b.length) return false;++ final len = a.length;+ final aView = ByteData.sublistView(a);+ final bView = ByteData.sublistView(b);+ int i = 0;++ while (i + 7 < len) {+ final va = aView.getUint64(i, Endian.little);+ final vb = bView.getUint64(i, Endian.little);+ if (va != vb) return false;+ i += 8;+ }++ while (i < len) {+ if (a[i] != b[i]) return false;+ i++;+ }++ return true;+}diff --git a/mobile/apps/photos/plugins/ente_crypto/pubspec.yaml b/mobile/apps/photos/plugins/ente_crypto/pubspec.yamlindex 12ab97748e..91fa844ff5 100644--- a/mobile/apps/photos/plugins/ente_crypto/pubspec.yaml+++ b/mobile/apps/photos/plugins/ente_crypto/pubspec.yaml@@ -23,5 +23,7 @@ dependencies:dev_dependencies:flutter_lints: 6.0.0+ flutter_test:+ sdk: flutterflutter:diff --git a/mobile/apps/photos/plugins/ente_crypto/test/uint8_list_equals_test.dart b/mobile/apps/photos/plugins/ente_crypto/test/uint8_list_equals_test.dartnew file mode 100644index 0000000000..624b2adb6b--- /dev/null+++ b/mobile/apps/photos/plugins/ente_crypto/test/uint8_list_equals_test.dart@@ -0,0 +1,53 @@+import "dart:typed_data";++import "package:ente_crypto/src/uint8_list_equals.dart";+import "package:flutter_test/flutter_test.dart";++void main() {+ test("compares empty, byte-tail and complete-word lengths", () {+ for (final length in [0, 1, 7, 8, 9, 15, 16, 17]) {+ final a = Uint8List.fromList(+ List.generate(length, (index) => (index * 131 + 19) & 255),+ );+ final b = Uint8List.fromList(a);++ expect(uint8ListEquals(a, a), isTrue);+ expect(uint8ListEquals(a, b), isTrue);+ expect(uint8ListEquals(a, Uint8List(length + 1)), isFalse);++ for (var index = 0; index < length; index++) {+ b[index] ^= 0x80;+ expect(+ uint8ListEquals(a, b),+ isFalse,+ reason: "Mismatch at byte $index of $length",+ );+ b[index] ^= 0x80;+ }+ }+ });++ test(+ "respects unaligned nested view offsets and excludes surrounding bytes",+ () {+ final aBacking = Uint8List(40)..fillRange(0, 40, 0x55);+ final bBacking = Uint8List(48)..fillRange(0, 48, 0xAA);+ final a = Uint8List.sublistView(+ Uint8List.sublistView(aBacking, 1),+ 2,+ 19,+ );+ final b = Uint8List.sublistView(bBacking, 11, 28);+ for (var i = 0; i < a.length; i++) {+ a[i] = b[i] = (i * 17) & 255;+ }++ expect(uint8ListEquals(a, b), isTrue);+ for (final index in [0, 7, 8, 15, 16]) {+ b[index] ^= 1;+ expect(uint8ListEquals(a, b), isFalse, reason: "View byte $index");+ b[index] ^= 1;+ }+ },+ );+}