App-level encryption at rest is available in the Swift and Kotlin SDKs in
version 5.2 and later. Support for other SDKs is planned.
How it works
App-level encryption at rest lets your app encrypt all locally persisted Ditto data, including attachments, with key material that your app supplies. The feature is opt-in. When no encryption key is configured, Ditto creates or opens an unencrypted local store. Ditto uses a 32-byte encryption key directly, so generate high-entropy key material with a cryptographically secure random number generator. Ditto does not store, generate, or manage your encryption key. Your app must provide the same key whenever it opens an encrypted local store, including after each cold start. Store the key outside the Ditto persistence directory, such as in Apple Keychain Services on iOS or the Android Keystore system on Android. Encryption at rest protects locally persisted data only. It does not change Ditto sync behavior, peer discovery, queries, subscriptions, or data transmitted between peers.Encrypt the local store
Pass the key throughDittoConfig.encryptionKey before you open Ditto. Use the
same key every time you reopen the store.
Handle encryption errors
Opening a store can fail with a typed encryption error. Your app can tell a missing key from a corrupted store and respond to each one instead of treating every failure the same way.
In Swift these are cases on the error thrown by
Ditto.open: keyRequired,
keyUnexpected, keyInvalid, unsupportedScheme, and metadataCorrupt. In
Kotlin they are reasons on DittoException.EncryptionException: KeyRequired,
KeyUnexpected, KeyInvalid, UnsupportedScheme, and MetadataCorrupt. The
older passphrase error cases are deprecated.
Opening an existing encrypted store without a key, or with the wrong key, fails
without modifying the store. Opening an existing unencrypted store with a key
also fails.
Switch between encrypted and unencrypted storage
Ditto doesn’t convert a store in place. To change between encrypted and unencrypted local storage, close Ditto, delete the existing local store, and reopen it with the desired configuration.Recover from a lost key
If the encryption key is lost, the local encrypted store cannot be recovered. Delete the local store and let Ditto resync data from other peers. Any changes that hadn’t synced before the key was lost are gone.Related documentation
- Device Storage Management for controlling what the local store holds
- SDK 5.2 release notes for the full list of encryption changes