Arunアルン
← all posts2026-11-25

Zeroising and UniFFI

So right now in the secrets module of our Rust crate we have a struct that holds the key material:

pub struct KeyBundle {
    signing_key: Vec<u8>,
    encryption_key: Vec<u8>,
    mac_key: Vec<u8>,
    seed: Vec<u8>,
}

Now when we call expose_backup_json(&self), it returns these values as a Zeroizing<String>. And this is fine as long as these values always remain in a Rust process. Even then it is only kind of safe, because true zeroization is not actually guaranteed. Article Link

Now when this is compiled with UniFFI so that a Swift target can access this function, what UniFFI basically does is serialise these vectors into a transport buffer. Then Swift deserialises this data into:

public struct KeyBundle {
    public let signingKey: Data
    public let encryptionKey: Data
    public let macKey: Data
    public let seed: Data
}

So the memory flow basically looks like:

Rust                            FFI                 Swift

KeyBundle
---------

[signing_key bytes]    ─copy─▶  RustBuffer ─copy─▶  Data(signingKey)
[encryption_key bytes] ─copy─▶  RustBuffer ─copy─▶  Data(encryptionKey)
[mac_key bytes]        ─copy─▶  RustBuffer ─copy─▶  Data(macKey)
[seed bytes]           ─copy─▶  RustBuffer ─copy─▶  Data(seed)

After the call is done, the temporary KeyBundle on the Rust side is zeroised and we move forward to the Swift side. Now Swift has copies of all four values in the KeyBundle struct, and the transport buffer may also have held copies. Passing the values back into Rust serialises them again, so now there are more copies, and this keeps happening every time something has to be done with the keys.

Better Approach

Now there is a better approach to do this, called the Opaque Object approach, where the actual key material always remains in Rust-owned memory.

Rust heap                                Swift heap

┌──────────────────────────────┐         ┌────────────────────┐
│ KeyBundleHandle              │         │ KeyBundleHandle    │
│                              │         │ Swift wrapper      │
│ keys:                        │         │                    │
│   signing_key:    [32 bytes] │◀────────│ Rust object handle │
│   encryption_key: [32 bytes] │ pointer │                    │
│   mac_key:        [32 bytes] │         └────────────────────┘
│   seed:           [32 bytes] │
└──────────────────────────────┘

The Swift side here only receives a pointer to the Rust-owned memory. In order to define this we implement a handle:

#[derive(uniffi::Object)]
pub struct KeyBundleHandle {
    keys: KeyBundle,
}

So now Swift can't directly access the fields:

let keys: KeyBundleHandle

keys.signingKey // this will fail and not compile

So we define the operations in the Rust implementation itself:

#[uniffi::export]
impl KeyBundleHandle {
    pub fn sign(&self, message: Vec<u8>) -> Result<Vec<u8>, KeyBundleError> {
        // Uses self.keys.signing_key inside Rust.
    }

    pub fn rotate(&self) -> Result<Arc<KeyBundleHandle>, KeyBundleError> {
        // Constructs the next key bundle inside Rust.
    }
}

So our flow becomes much more secure than before:

  1. Swift passes the handle + public input
  2. Rust operates directly on the handle passed
  3. Only the result we want to send back to Swift is forwarded (no secret exposed)

UniFFI usually exports objects wrapped in an Arc, so we get something like Arc<KeyBundleHandle>.

So in Swift too we get pointer/reference behaviour when copying things:

let keys1 = keys       // *ptr --> Rust memory [Arc<KeyBundleHandle>]
let keys1Copy = keys1  // *ptr

Now what happens when Swift releases its last copy of the handle:

  1. The generated Swift wrapper's deinit calls UniFFI's Rust release function.
  2. Rust decreases the object's reference count.
  3. When the last reference disappears, KeyBundleHandle is dropped.
  4. Its KeyBundle field is dropped.
  5. KeyBundle::drop() zeroizes all four fields.
  6. The Rust allocation is then deallocated.

This approach helps us reduce the unnecessary memory copies while performing operations. So now we have better memory hygiene; it doesn't solve all of the handling of secrets, but it improves things a lot.