← Back to the blog

Rust • 2026-11-29 • 3 min read

One Rust Core for iOS, Android and the Backend (UniFFI)

How we share cryptographic validation, the offline SQLite cache and the sync protocol between the mobile apps and the Rust API.

One Rust Core for iOS, Android and the Backend (UniFFI) — Rust

Security logic written twice is security logic that disagrees with itself. A signature check implemented in Swift and again in Kotlin will drift — not immediately, but on the third bug fix that only one of them gets. We compile one Rust crate instead and bind it into both apps with UniFFI.

What belongs in the core

The rule we use is simple: anything that would be a correctness or security bug if the two platforms disagreed goes into Rust. In practice that is four things.

  • Request signing and signature verification, so a payload accepted on Android is accepted on iOS
  • The offline cache — schema, migrations and queries — on rusqlite
  • The sync protocol: conflict resolution, retry policy, the shape of every envelope
  • Validation of anything arriving from the network, before it reaches UI code

What stays native is everything the platform does better: views, navigation, biometrics, notifications, the keychain and the keystore.

UniFFI in one paragraph

UniFFI reads the exported surface of your crate and generates the Swift and Kotlin bindings, plus the C ABI glue underneath. You annotate, it generates. There is no hand-written JNI and no @objc bridging header to keep in step, which is the part that rots.

// rust
#[derive(uniffi::Error, Debug, thiserror::Error)]
pub enum WidentError {
    #[error("key {0} is not in the keystore")]
    UnknownKey(String),
    #[error("payload failed authentication")]
    Tampered,
}

#[uniffi::export]
pub fn verify_and_decrypt(payload: &[u8], key_id: String) -> Result<Vec<u8>, WidentError> {
    let key = keystore::get_ephemeral_key(&key_id)?;
    crypto::chacha20_open(&key, payload)
}

The error enum matters more than the function. UniFFI maps it to a Swift Error and a Kotlin sealed class, so a failure crosses the boundary as a typed value rather than as a null and a log line.

The parts that cost time

Three things are worth knowing before you commit to this.

  1. Async across the boundary. UniFFI supports it, but the ergonomics are better if the core exposes blocking calls and each app wraps them in its own concurrency model. We run the core on a dedicated dispatch queue on iOS and a Dispatchers.IO context on Android.
  2. Build times. Two extra architectures per platform, built for debug and release, is real minutes. Cache the artefacts in CI and build the simulator slice separately.
  3. Panics. A panic unwinds into generated code that cannot do anything useful with it. We set panic = "abort" for release and treat any panic found in testing as a bug of the same severity as a crash, because that is what it is.

What it bought us

One implementation of the sync protocol, one test suite for it, and a class of bug — "it works on Android" — that no longer occurs in that layer. The same crate signs the requests that reach the Security API, so the server and both clients agree on what a valid request looks like by construction rather than by review.

Read next

COOKIES // MEASUREMENT

We count page views through Google Tag Manager. Say yes and it may store a cookie in your browser; say no and it still counts the visit, without storing anything and without an identifier. Nothing here is used for advertising.

Cookie Policy — you can change this at any time from the footer of any page.