r/FlutterDev • • 6d 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.

  1. 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

  1. 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

  1. 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

  1. 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

15 Upvotes

10 comments sorted by

1

u/laplace_apps 1d ago

Really useful notes, especially the cryptography_flutter part. I'd have assumed enable() sped up X25519 too, not just AES-GCM. Did you end up moving key exchange to a native plugin, or was the pure Dart speed acceptable in practice?

1

u/FrozenCocomo 20h ago

yea i moved it to native, pure dart wasnt really acceptable for me, i didn't write a plugin myself, i used the sodium package over dart:ffi ... you need the sumo build if u want raw crypto_scalarmult, which i needed bcs my handshake is X3DH style and works on the raw DH outputs, only Ed25519 and X25519 go through libsodium, AES-GCM and HKDF stay on the cryptography package since cryptography_flutter does speed up AES-GCM natively

and swapping the crypto was comfortable to me bcs of the known answer test vectors i already had, server and client both have to produce the exact same bytes for handshakes, signatures and hashes, so if libsodium had done anything differently the tests would have failed right away

1

u/EION_dev 6d ago

This matches my experience a lot. One more trap on the same side, which I hit in my own Flutter messenger (X25519 + AES-GCM): on some Android skins (MIUI in my case) flutter_secure_storage accepts a write and an immediate read-back returns it, but if the process gets killed right after (swiping the app away from recents is a SIGKILL), the fresh value is gone on the next start. For me it was a newly generated private key: the app read "nothing" as "no key yet", created a new one, and everything encrypted to the old key became unreadable.

Lesson: never treat an empty read of an identity key as a first run and silently create a new one. And set allowBackup="false" so the key doesn't end up in a cloud backup either.

1

u/FrozenCocomo 6d ago

One thing that saved me there is the server just wont take a new identity once an acc already has one, so even if the phone loses everything and makes fresh keys, they get rejected instead of just replacing the old ones

1

u/EION_dev 6d ago

Nice, that's a cleaner guard. How do you handle a legit reinstall or a new phone then? Is there a key backup/transfer, or does the user approve the new device from an old one? We went a different way: every device has its own key and the sender encrypts for all devices of the account, so a lost key only affects that one device, and contacts get notified about the new key.

1

u/FrozenCocomo 6d ago

honestly right now i don't, there's no backup or transfer yet, so a new phone or a reinstall can't open that account's albums

its my next feature so i ll think about it

1

u/EION_dev 6d ago

Makes sense, that's the hardest part. When you get to it: what worked best for us is transfer from the old device. The new phone shows a QR code, the old one scans it and sends the history encrypted to the new device's key, so the server never sees anything. The harder case is when the old device is gone; the usual answer there is an encrypted backup with a passphrase only the user knows. Good luck with it!

1

u/FrozenCocomo 6d ago

I ll keep that in my mind. Thanks

2

u/queerlonda 3d ago

These kind of posts we actually want instead of the slop

1

u/repoonse 6d ago

The fail-closed metadata point is the real lesson here. "Upload the original if decoding fails" is exactly how GPS leaks ship to production.