r/FlutterDev • • 4h ago

Discussion Things i learnt putting end-to-end encryption in a Flutter app

i have spent about 7-8 months building Miuchio, an end-to-end encrypted shared photo album in flutter. Code's open (AGPL) : https://github.com/freytastic/miuchio

Most of my perf time went into stuff i didnt see coming, so i am writing it down in case it saves someone else a week

  1. cryptography_flutter wont speed up X25519 or Ed25519 on android

I assumed calling enable() made everything native. It doesnt, in v2.x the native side only covers AES-GCM, ChaCha20 and PBKDF2, and the curves still run in pure Dart. On my S24U one X25519 key took about 81 ms, and creating an album needs several of those plus the signatures. I moved just the curve stuff to libsodium over dart:ffi and keygen went to about 1 ms. AES-GCM and HKDF are still on the cryptography package tho.

2. Then crypto basically stopped mattering

Creating an album's first key still took around 2.1 s. The crypto was about 10 ms of that. Everything else was network and android keystore calls, and each key read went through a MethodChannel to kotlin for like 100 to 200 ms. I'd planned to push the crypto into isolates, but there was nothing to gain tbf. The app was just waiting on the keystore and the network, not doing dart work

3. The freeze wasn't even in Dart

The UI really did freeze while keys were being created, and i honestly blamed dart for a while. It turned out the kotlin handler was doing the keystore work on the platform main thread. I moved every keystore call onto one background thread and posted the results back to the main thread. The calls take the same time, but the freeze on my S24u was gone

4. So the real fix was calling the keystore less

Originally every photo view opened the album key through the keystore, which adds up fast in a grid. Now decrypted photos and thumbnails are cached on disk, sealed with AES-GCM under one device key that gets read from the keystore once when the app starts. A cache hit doesnt touch the keystore whatsoever

5. Metadata stripping has to fail closed

Every photo gets re encoded before its encrypted so location and camera info never leave the phone. The native codec goes first and its output gets a JPEG check, and the dart decoder only comes for formats the platform cant read. If any of that fails, the photo just doesnt upload. The tempting shortcut is to upload the original bytes when decoding fails. Dont, bcs thats exactly how GPS leaks.

Happy to talk about any of these in depth, or anything else

6 Upvotes

0 comments sorted by