Skip to content

Encrypting Private Data ​

skapi-kotlin can encrypt the data of private records, and the files attached to them, in your app before they are uploaded. The keys come from the user's password and are never sent to the server. Records and files encrypted by skapi-kotlin open in skapi-js and skapi-py, and the other way around.

A lost password means lost data

If a user loses their password, their encrypted private data is lost. Resetting the password gets them back into their account, but it cannot open their encrypted records. Nobody can recover them: not you, and not Skapi.

The only way back is the user's encryption recovery code, and only if they saved it while they still had access to their account: the code shown once at their first login with encryption on (in getConnectionInfo()["encryption_recovery"]), or a new one from regenerateEncryptionRecoveryCode(). Once the password is lost, a new code can be made only on a device where the user is still signed in with encryption unlocked.

Before you turn encryption on, make sure your app shows the code to your users and tells them to keep it, and warn them in your password reset flow. See When the recovery code is needed.

It is off by default, and it covers less than "everything is encrypted". In particular:

  • Only data and the contents of attached files are covered. Index values, tags, file names and the record's other fields stay readable to the server, because queries run on them.
  • The strength is capped by the user's password.
  • A forgotten password with no saved encryption recovery code means the data is gone.

What it needs ​

Nothing extra. Encryption uses the platform's javax.crypto, on the JVM and on Android alike.

What the server sees ​

Readable by the serverEncrypted
record_id, unique_id, table, access_groupdata of private records
index, tags, referencecontents of files on private records
file names, sizes, upload times
timestamps, record sizes
the users a record is shared with

Records outside the "private" group are never encrypted.

The security claim, exactly ​

For a user's own private records, not shared:

A service provider who dumps the database, the storage bucket, and every request log cannot recover the record's data without a successful offline guess against the user's password.

For records shared with other users:

The provider cannot recover the record's data by reading its own database. It can obtain that record's data key by serving a forged public key at the moment the user shares, which the user can detect by comparing key fingerprints, and which trustPolicy = "strict" prevents outright.

Both are bounded by the password. For an OpenID user, that is the password Skapi makes for the account and hands to the SDK at each OpenID sign-in; the user never types it. The key is derived with PBKDF2 (600,000 rounds), so a short password falls to an offline attack. Encryption works with any valid account password, so it is exactly as strong as the password the user chose. When you use client-side encryption, encourage your users to use longer passwords, for example with a hint in your signup and password-change forms.

Turning it on ​

Users who log in with OpenID

Encryption works for OpenID users (openIdLogin) with nothing extra to set up. See OpenID users.

kotlin
import com.skapi.*
import kotlinx.serialization.json.*

val skapi = Skapi("<Project ID>", encryption = EncryptionOptions())
java
import com.skapi.MemoryStorage;
import com.skapi.java.EncryptionOptionsBuilder;
import com.skapi.java.SkapiJava;

// storage, timeoutSeconds and targetCdn at their defaults
SkapiJava skapi = new SkapiJava("<Project ID>", new MemoryStorage(), 120, null, new EncryptionOptionsBuilder().build());

With options:

kotlin
val skapi = Skapi(
    "<Project ID>",
    encryption = EncryptionOptions(
        iterations = 600000,  // PBKDF2 rounds. Default 600000, minimum 100000.
        recovery = "code",    // issue an encryption recovery code. "code" (default) or "none".
        trustPolicy = "tofu", // "tofu" (default) or "strict". See Sharing.
        withheld = "null",    // "null" (default) or "sentinel". See below.
    ),
)
java
SkapiJava skapi = new SkapiJava(
    "<Project ID>",
    new MemoryStorage(),
    120,
    null,
    new EncryptionOptionsBuilder()
        .iterations(600000)   // PBKDF2 rounds. Default 600000, minimum 100000.
        .recovery("code")     // issue an encryption recovery code. "code" (default) or "none".
        .trustPolicy("tofu")  // "tofu" (default) or "strict". See Sharing.
        .withheld("null")     // "null" (default) or "sentinel". See below.
        .build()
);

The option only applies to the object it is given to. An object made without it saves every record in the clear, and turning it on later does not encrypt what was already saved.

Then use records as usual:

kotlin
skapi.login("user@example.com", "password")

skapi.postRecord(
    jsonOf("diagnosis" to "confidential"),
    jsonOf("table" to jsonOf("name" to "notes", "access_group" to "private")),
)

val rec = skapi.getRecords(jsonOf("table" to jsonOf("name" to "notes", "access_group" to "private")))["list"]!!.jsonArray[0].jsonObject
rec["data"]      // {"diagnosis": "confidential"}
rec["encrypted"] // {"status": "encrypted", "recipients": ["<your user id>"]}
java
skapi.login("user@example.com", "password").get();

skapi.postRecord(
    SkapiJson.obj("diagnosis", "confidential"),
    SkapiJson.obj("table", SkapiJson.obj("name", "notes", "access_group", "private"))
).get();

Map<?, ?> rec = (Map<?, ?>) ((List<?>) skapi.getRecords(SkapiJson.obj("table", SkapiJson.obj("name", "notes", "access_group", "private"))).get().get("list")).get(0);
rec.get("data");      // {"diagnosis": "confidential"}
rec.get("encrypted"); // {"status": "encrypted", "recipients": ["<your user id>"]}

Unlocking ​

Signing in with a password (login, signup with login, or verifyMfa) unlocks encryption, and so does openIdLogin. The first time, it also sets up the user's keyring: a random master key wrapped under the password, and a key pair for sharing. If two devices set it up for the same user at the same moment, the first keyring kept wins: the other device uses it and removes its own keyring and public key. An account left with a stray public key on top (by an older SDK, or a removal that failed) is repaired when it unlocks: the user's real public key is published again and the stray one is removed. A user who pinned the stray fingerprint gets ENCRYPTION_PEER_KEY_CHANGED and must pin the new one.

A session that did not sign in with a password stays locked: one restored from storage, or from loginWithToken. skapi-kotlin keeps the master key only in memory, unless you give it a device key store. Unlock it with the password (an OpenID user signs in again instead, see OpenID users):

kotlin
skapi.getEncryptionStatus()
// {"status": "locked", "reason": "NO_SESSION_KEY", ...}

skapi.unlockEncryption("password")
// {"status": "unlocked"}
java
skapi.getEncryptionStatus().get();
// {"status": "locked", "reason": "NO_SESSION_KEY", ...}

skapi.unlockEncryption("password").get();
// {"status": "unlocked"}

unlockEncryption() only opens encryption that is already set up. It is set up at the first login with the password, or for an OpenID account at its first openIdLogin() with encryption on, so an account that has never signed in either way gets NOT_EXISTS.

While locked, private records read as "data": null with "encrypted": {"status": "failed", "reason": "NO_SESSION_KEY"}, and writing one throws ENCRYPTION_LOCKED. lockEncryption and logout forget the keys. A lock keeps an encryption recovery code that was not collected yet, so the next getConnectionInfo() still returns it, once; a logout drops it.

OpenID users ​

Encryption works for OpenID users with nothing extra to do. With encryption on, openIdLogin signs in with the password Skapi makes for the account, as it did before 2.8.0, and the SDK sets up and unlocks the keyring with that password, the same way as a password login. The first OpenID sign-in creates the keyring and the encryption recovery code, so show the code to OpenID users too. See Record encryption. A normal OpenID sign-in never needs the code; the cases that do are in When the recovery code is needed.

The app never sees that password, so it cannot pass it to unlockEncryption(). A session restored with a device key store opens on its own; otherwise, to unlock a restored session of an OpenID user, call openIdLogin() again.

Remember this device ​

This is an addition of skapi-kotlin, the counterpart of skapi-js keeping the key in the browser. Give EncryptionOptions a deviceKeyStore, and the user's master key is kept there whenever encryption unlocks (at a password login, with unlockEncryption(), or with an encryption recovery code). A session restored from storage the next time the app starts then opens the user's encrypted data without asking for the password again: the store is read before the first encrypted read or write.

On Android, use AndroidKeystoreDeviceKeyStore from skapi-android. It seals the key with an AES key that never leaves the Android Keystore. See Android.

kotlin
import com.skapi.*
import com.skapi.android.AndroidKeystoreDeviceKeyStore
import com.skapi.android.SharedPreferencesStorage

val skapi = Skapi(
    "<Project ID>",
    storage = SharedPreferencesStorage(context),
    encryption = EncryptionOptions(deviceKeyStore = AndroidKeystoreDeviceKeyStore(context)),
)
java
import com.skapi.android.AndroidKeystoreDeviceKeyStore;
import com.skapi.android.SharedPreferencesStorage;
import com.skapi.java.EncryptionOptionsBuilder;
import com.skapi.java.SkapiJava;

SkapiJava skapi = new SkapiJava(
    "<Project ID>",
    new SharedPreferencesStorage(context),
    120,
    null,
    new EncryptionOptionsBuilder().deviceKeyStore(new AndroidKeystoreDeviceKeyStore(context)).build()
);

The core library has no store of its own on purpose: a plain file would put the key on disk in the clear. On other platforms, implement DeviceKeyStore (save, load, delete) with your platform's secure storage. Entries are named "<service>/<owner>#<user_id>".

the stored key
logout()is removed, so the next person on the device cannot reach this user's keys by restoring their session
lockEncryption()is kept: the next restored session unlocks again
lockEncryption(forgetDevice = true)is removed: the next restored session stays locked until unlockEncryption()
persistDevice = falseis never saved

A stored key that no longer opens the keyring (the password was changed on another device, or the keyring was replaced) leaves encryption locked, as without a store.

Reading what you cannot decrypt ​

A private record that cannot be decrypted comes back with "data": null and a reason:

reason
NO_SESSION_KEYencryption is locked
NOT_A_RECIPIENTthe record was not shared with you; encrypted["recipients"] lists who it was
ENCRYPTION_DISABLEDthis Skapi object was made without encryption
BAD_KEY / CORRUPT / BINDING_MISMATCHthe stored key or data does not open
DATA_UNAVAILABLEthe stored data could not be downloaded
UNSUPPORTED_VERSIONmade by a newer SDK

With withheld = "sentinel", data is {"__skapi_no_access__": true, "reason": ...} instead of null; isWithheld tells it apart. Writing such a value back throws ENCRYPTION_CANNOT_REWRITE_WITHHELD.

Changing a password ​

changePassword adds a wrap under the new password before the password changes. Encryption must be unlocked, or it throws ENCRYPTION_LOCKED and the password stays as it was.

A reset (forgotPassword and resetPassword) proves the e-mail address, not the old password, so it cannot open the keyring. After a reset, the user's encrypted data opens with their encryption recovery code, once, or on a device still signed in with encryption unlocked (or one that unlocks from its device key store), which can also make a new code. See When the recovery code is needed.

Changes from two devices at once ​

A password change (and the cleanup of the old password's wrap after it), an encryption recovery code used with unlockEncryptionWithRecoveryCode(), and a new one from regenerateEncryptionRecoveryCode() each change the user's keyring. Each reads the latest keyring again right before it saves, keeps what another device added in the meantime, and applies only its own changes: the wraps it adds and the wraps it removes. A wrap another device just added, for example the new password's wrap from a changePassword() there, stays, and a wrap this device removed is not written back. With one device, nothing is different.

When the recovery code is needed ​

Why it exists. With record encryption, the key to a user's encrypted records is locked with their password, on their device. Skapi never sees that key, so Skapi cannot open your users' encrypted data. A password reset proves the user controls their e-mail, not that they know the old password, so after a reset only the encryption recovery code can open the key, apart from a device still signed in with encryption unlocked (or one that unlocks from its device key store), which can also make a new code. That is on purpose.

Skapi never stores the code. Your app gets it once: in getConnectionInfo()["encryption_recovery"] after the user's first sign-in with encryption on, and as "encryptionRecoveryCode" in the result of every unlockEncryptionWithRecoveryCode() and regenerateEncryptionRecoveryCode(). Show it to the user and tell them to keep it. Every use issues a new code, and the code just used stops working.

The rule is the same for password accounts and for OpenID accounts.

The code is needed once, in these cases:

  • After a forgotten-password reset (forgotPassword, then resetPassword()). The user signs in with the new password, then unlocks with the code and the new password: unlockEncryptionWithRecoveryCode(code, newPassword).
  • After a password account is merged with OpenID, in an app with encryption on (openIdLogin() with merge), when the account already had encryption set up (it had signed in with its password while encryption was on). Otherwise the merge sign-in sets encryption up and no code is needed. The user signs in with OpenID, then unlocks with the code: unlockEncryptionWithRecoveryCode(code). A user who still remembers the old password can call unlockEncryption(oldPassword) instead. From then on, OpenID sign-ins on any device open the records.
  • OpenID accounts, rare: after the project's OpenID logger is deleted and registered again, and once after each change of the server key Skapi uses for OpenID sign-in. The user signs in with OpenID, then unlocks with unlockEncryptionWithRecoveryCode(code). From then on, OpenID sign-ins on any device open the records.

In each case the sign-in itself succeeds, and encryption stays locked: right after login() or openIdLogin(), getEncryptionStatus() returns "status": "locked". That is when your app asks the user for the code.

The code is not needed:

  • A normal sign-in, with a password or with OpenID, on any device.
  • changePassword() while signed in. The SDK moves the key to the new password by itself (see Changing a password).
  • Turning encryption off and on again in your app.

After a password reset:

kotlin
skapi.login("user@example.com", "new password") // signs in; encryption stays locked
val res = skapi.unlockEncryptionWithRecoveryCode(code, "new password")
res["encryptionRecoveryCode"] // a NEW code. Show it: the one just used no longer works.
java
skapi.login("user@example.com", "new password").get(); // signs in; encryption stays locked
Map<String, Object> res = skapi.unlockEncryptionWithRecoveryCode(code, "new password").get();
res.get("encryptionRecoveryCode"); // a NEW code. Show it: the one just used no longer works.

The records open, the key is locked under the new password ("repaired": true; the old password no longer opens it), and every later sign-in unlocks normally.

After a merge, or after the OpenID logger was registered again:

kotlin
skapi.openIdLogin(token, "google") // signs in; encryption stays locked
if (skapi.getEncryptionStatus()["status"]?.jsonPrimitive?.content == "locked") {
    val res = skapi.unlockEncryptionWithRecoveryCode(code) // the code only
    res["encryptionRecoveryCode"] // a NEW code. Show it: the one just used no longer works.
}
java
skapi.openIdLogin(token, "google").get(); // signs in; encryption stays locked
if ("locked".equals(skapi.getEncryptionStatus().get().get("status"))) {
    Map<String, Object> res = skapi.unlockEncryptionWithRecoveryCode(code).get(); // the code only
    res.get("encryptionRecoveryCode"); // a NEW code. Show it: the one just used no longer works.
}

Pass the code only: the app never has the password an OpenID sign-in uses, and a password given in this session is not checked. Call it in the session openIdLogin() just started: there it also sets the key up for OpenID sign-in, so every later openIdLogin(), on this device or another, opens the records by itself. In a session restored after the app restarts, it opens the records but the next openIdLogin() asks for the code again. A session restored with a device key store opens on its own.

An OpenID account that goes through a forgotten-password reset gets a password of its own. In an app with encryption on, the user signs in with login() and that password from then on (there openIdLogin() usually throws ACCOUNT_EXISTS, and calling again with merge does not help; it throws INVALID_REQUEST with This account has its own password. Update the app to the latest Skapi SDK to sign in with OpenID, or sign in with the password. only when the account had signed in with OpenID through a 2.8.0 app with encryption off and the reset code went to the e-mail the provider shares), and unlocks once with the code and the new password, as after any reset. In an app with encryption off, from SDK 2.8.0, both the password and OpenID sign-in work when the reset code went to the e-mail the OpenID provider shares; after a reset whose code went to another e-mail, the next OpenID sign-in takes the account over again and replaces that password. See A password of their own.

If the code is lost. While the user is still signed in with encryption unlocked, regenerateEncryptionRecoveryCode() gives a new code (the old one stops working): show it to them. After a reset with no code, and no device where the user is still signed in with encryption unlocked (for example through a device key store), the encrypted records cannot be opened again. With recovery = "none" (see Turning it on) there is no code at all.

Encryption recovery codes ​

When a user forgets their password and resets it, they lose access to their encrypted private records, unless they saved their encryption recovery code. The code is the only way back, apart from a device that is still signed in and unlocked (it can also make a new code). It is not the code forgotPassword() e-mails: that one resets the account password, this one unlocks the encrypted data.

There are two ways to get one, and both work only while the user still has access:

  1. Shown once, at setup. The first login with encryption on, with a password or with openIdLogin, creates the code. The next getConnectionInfo returns it in "encryption_recovery", one time only:
kotlin
skapi.login("user@example.com", "password")
val recovery = skapi.getConnectionInfo()["encryption_recovery"]
// first time: {"code": "XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX",
//              "message": "This is your encryption recovery code. Save it now: it is shown only this once. ..."}
// after that: {"code": null,
//              "message": "Your encryption recovery code was shown once and cannot be shown again. Call regenerateEncryptionRecoveryCode() to get a new one."}
java
skapi.login("user@example.com", "password").get();
Object recovery = skapi.getConnectionInfo().get().get("encryption_recovery");
// first time: {"code": "XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX",
//              "message": "This is your encryption recovery code. Save it now: it is shown only this once. ..."}
// after that: {"code": null,
//              "message": "Your encryption recovery code was shown once and cannot be shown again. Call regenerateEncryptionRecoveryCode() to get a new one."}
  1. A new one, any time, with regenerateEncryptionRecoveryCode. It needs the user signed in and unlocked, and the old code stops working.

After a password reset, sign in with the new password and unlock with the saved code, giving the new password so every later login unlocks normally. This is needed once (see When the recovery code is needed):

kotlin
skapi.login("user@example.com", "new password") // encryption stays locked: the keyring is sealed under the old password
val res = skapi.unlockEncryptionWithRecoveryCode("XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX", "new password")
// {"status": "unlocked", "repaired": true, "encryptionRecoveryCode": "<a new code>"}
java
skapi.login("user@example.com", "new password").get(); // encryption stays locked: the keyring is sealed under the old password
Map<String, Object> res = skapi.unlockEncryptionWithRecoveryCode("XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX", "new password").get();
// {"status": "unlocked", "repaired": true, "encryptionRecoveryCode": "<a new code>"}

password is checked first. Before anything is changed, the SDK asks the sign-in service whether it is the account's current password. When it is not, the call throws INCORRECT_USERNAME_OR_PASSWORD (The password is not this account's current password.) and nothing changes: the keyring, the saved encryption recovery code and the session stay as they were, so a typo cannot leave the data sealed under a password nobody knows. Too many tries in a row throw REQUEST_EXCEED. The check does not sign the user in again: the session, its stored tokens and your login listeners are left alone. A session that openIdLogin signed in is not checked this way, because the app never saw the account's password: there, call it with the code only (see When the recovery code is needed).

Using a code replaces it: save the new one. With recovery = "none" no code is ever made.

Sharing a record ​

The owner shares an encrypted record with the usual grant:

kotlin
skapi.grantPrivateRecordAccess(recordId, listOf("<user id>"))
java
skapi.grantPrivateRecordAccess(recordId, Arrays.asList("<user id>")).get();

skapi-kotlin fetches the other user's public key, wraps the record's key to it and writes that onto the record, then grants access. The other user reads the record with their own session; the owner does not need to be online.

Removing access also replaces the record's key and re-encrypts its data and its encrypted files, so the removed user cannot read later versions. It cannot take back what they already read. The files are encrypted under the new key and uploaded first, then one write puts the new key on the record and deletes the old copies, and only then is the access removed. If any step fails, the new copies are deleted and the record, its files and its access stay as they were.

  • Only the owner can share or remove access on an encrypted record.
  • The owner can do this from any device. Before sharing, removing access, updating or attaching files, skapi-kotlin checks that the record's key it holds is still the one on the record, and reads the record again when another session changed it.
  • An update that does not state the table reads the record's access group before it writes data, so a record another device made non-private is not encrypted again.
  • The other user must have signed in once with encryption on, so that they have a key. Otherwise the grant throws ENCRYPTION_RECIPIENT_HAS_NO_KEY and changes nothing.
  • The first key seen for a user is remembered (pinned). If it later changes, sharing with them throws ENCRYPTION_PEER_KEY_CHANGED: confirm the new fingerprint with that user, then pinPeerKey.
  • With trustPolicy = "strict", no key is trusted until you pin it, which closes the forged-key risk in the claim. Users compare fingerprints out of band: getEncryptionStatus()["fingerprint"].

Changing the access group ​

Moving a record out of "private" writes its data back in the clear, and its encrypted files are uploaded again in the clear. Moving it into "private" encrypts the data, and the files already on it are encrypted as well. Either way, every grant on the record is removed, as for any private record.

Files ​

Files uploaded to an encrypted private record are encrypted too. getFile, getFileText and downloadFile decrypt them; read the record first in the session, so its key is known. In the record's bin they are marked "encrypted": true, with the plaintext size and the stored size in stored_size. A link from getFileUrl downloads the encrypted bytes.

Limits ​

  • Size. Encryption adds about a third, so a single record holds about 1.5 MB of data instead of 2 MB; more throws ENCRYPTED_DATA_TOO_LARGE. An update over 256 KB once encrypted is stored as a file next to the record; postRecords refuses such updates, save them with postRecord. A record holding more than one such file (left by older SDKs) is read from the newest, and the extra files are removed on its next update.
  • Whole updates only. data is one sealed value, so changing one field means read, change, write. Updating a record without data (only its index, tags, and so on) is fine.
  • Reserved names. __skapi_enc__ in record data, __skenc__ in a file field name, and the table __skapi__keyring (where each user's keyring lives).
  • One project. Encrypted records cannot be written to another project through service/owner; use a separate Skapi object.
  • Server-side reads. Nothing on the server can read encrypted data: no search on it, and the project owner sees data withheld.

skapi-kotlin 2.7.0