feat(phase-1/m3.2): FieldKeyProvider seam — AesGcmFieldCipher + EncryptionVerifier shared

Milestone M3 task 2 (DESKTOP_PHASE_PLAN §5): the cipher the desktop
build needs. AesGcmFieldCipher never touched Android APIs — only its
key SOURCE (ObjectBoxKeyProvider) did. New fun interface
FieldKeyProvider { getOrCreateKey(): ByteArray } in org.incido.data.crypto
is the seam; the class moves into the shared sources compiled by BOTH
:core:data (Android) and :core:data-jvm (desktop).

EncryptionVerifier (pure coroutines over NoteCryptoService) moves with
it — M3's desktop container needs the same trial-decrypt badge logic,
and the plan names the CipherHolder/NoteCryptoService/EncryptionVerifier
wiring as the thing to preserve.

Android diff is mechanical: AppContainer + SettingsViewModel pass
objectBoxKeyProvider::getOrCreateKey (SAM conversion) instead of the
provider object; NoteListViewModel's FQN import follows the package.
Behavior unchanged — same class, same bytes on the wire.

Justification for widening the seam (rules of engagement §9): the
interface replaces the concrete-class constructor parameter exactly as
planned; no other API moved.

Verified: ./gradlew assembleDebug (green) + ./gradlew :desktopApp:run
--args="--data-dir /tmp/incido-desktop-smoke --self-test --exit-after 6"
(green — :core:data-jvm compiles the shared crypto). One unrelated
GTK/XWayland window-startup wedge observed on a background run (process
never reached first composition; not reproducible on re-run, not a data
regression) — recorded for honesty.
This commit is contained in:
Incido Developer committed 2026-10-06 06:31:18 +13:00
1 parent 8db835640e
commit 40e2b9ff52
6 files changed
+46 -16

No files matched your search

@@ -7,14 +7,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import org.incido.data.AndroidImageOrientationNormalizer
import org.incido.data.ObjectBoxStore
import org.incido.data.crypto.AesGcmFieldCipher
import org.incido.data.crypto.CipherHolder
import org.incido.data.crypto.EncryptionVerifier
import org.incido.data.crypto.NoteCryptoService
import org.incido.data.repository.NoteRepositoryImpl
import org.incido.domain.repository.NoteRepository
import org.incido.compose.data.backup.BackupManager
import org.incido.compose.data.backup.CarnetImporter
import org.incido.compose.data.security.AesGcmFieldCipher
import org.incido.compose.data.security.EncryptionVerifier
import org.incido.compose.data.security.ObjectBoxKeyProvider
import org.incido.compose.data.security.SecurityManager
import org.incido.compose.data.settings.SettingsRepository
@@ -77,7 +77,9 @@ class AppContainer(context: Context) {
init {
val s = settingsRepository.snapshot()
if (s.isEncryptionActive && objectBoxKeyProvider.hasKeyBlocking()) {
cipherHolder.cipher = AesGcmFieldCipher(objectBoxKeyProvider)
// M3 task 2 seam: the cipher only needs the key bytes; the
// Keystore-wrapped provider is passed as a plain function.
cipherHolder.cipher = AesGcmFieldCipher(objectBoxKeyProvider::getOrCreateKey)
}
}
@@ -53,7 +53,7 @@ data class NoteListUiState(
class NoteListViewModel(
private val repository: NoteRepository,
/** v0.3.5.0 — source of the verified-encryption badge set. */
private val encryptionVerifier: org.incido.compose.data.security.EncryptionVerifier? = null,
private val encryptionVerifier: org.incido.data.crypto.EncryptionVerifier? = null,
) : ViewModel() {
private val filterMode = MutableStateFlow<NoteFilter>(NoteFilter.Home)
@@ -15,11 +15,11 @@ import kotlinx.coroutines.launch
import org.incido.compose.IncidoApplication
import org.incido.compose.data.backup.BackupManager
import org.incido.compose.data.backup.CarnetImporter
import org.incido.compose.data.security.AesGcmFieldCipher
import org.incido.compose.data.security.EncryptionVerifier
import org.incido.compose.data.security.ObjectBoxKeyProvider
import org.incido.compose.data.security.SecurityManager
import org.incido.data.crypto.AesGcmFieldCipher
import org.incido.data.crypto.CipherHolder
import org.incido.data.crypto.EncryptionVerifier
import org.incido.data.crypto.NoteCryptoService
import org.incido.compose.data.settings.AutoLockTimeout
import org.incido.compose.data.settings.SettingsRepository
@@ -141,7 +141,8 @@ class SettingsViewModel(
if (enabled) {
// Key first (idempotent), cipher into the holder, sweep.
objectBoxKeyProvider.getOrCreateKey()
cipherHolder.cipher = AesGcmFieldCipher(objectBoxKeyProvider)
cipherHolder.cipher =
AesGcmFieldCipher(objectBoxKeyProvider::getOrCreateKey)
noteCryptoService.encryptAllInPlace { done, total ->
_migrationState.value = MigrationState.Running(
if (total == 0) 1f else done.toFloat() / total,
@@ -320,7 +321,8 @@ class SettingsViewModel(
// Swap the live cipher to point at the freshly-installed key.
// We build a new wrapper that re-reads the (just-persisted)
// wrapped blob — no need to re-implement the unwrap here.
cipherHolder.cipher = AesGcmFieldCipher(objectBoxKeyProvider)
cipherHolder.cipher =
AesGcmFieldCipher(objectBoxKeyProvider::getOrCreateKey)
encryptionVerifier.checkNow()
_snackbar.value = if (overwroteExisting) {
"Recovery key installed. Previously-encrypted notes will appear as unverified."
@@ -1,14 +1,13 @@
package org.incido.compose.data.security
package org.incido.data.crypto
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.SecretKeySpec
import org.incido.data.crypto.FieldCipher
/**
* v0.3.5.0: AES-256-GCM implementation of [FieldCipher] backed by the
* Keystore-wrapped 32-byte key owned by [ObjectBoxKeyProvider].
* 32-byte key served by a [FieldKeyProvider].
*
* This is what actually makes "Encrypt in place" work: note content
* and attachment blobs are stored as
@@ -23,9 +22,14 @@ import org.incido.data.crypto.FieldCipher
* primitives (public-key exchange / signatures) are not used here.
*
* The key bytes are cached for the process lifetime (they already live
* in memory whenever [ObjectBoxKeyProvider] unwraps them at startup).
* in memory whenever the provider serves them at startup — Keystore
* unwrap on Android, key-file read on desktop).
*
* Phase 1 (M3 task 2): moved from :composeApp into the shared data layer
* behind the [FieldKeyProvider] seam — the class itself never touched
* Android APIs, only the key source did. Compiled for BOTH platforms.
*/
class AesGcmFieldCipher(keyProvider: ObjectBoxKeyProvider) : FieldCipher {
class AesGcmFieldCipher(keyProvider: FieldKeyProvider) : FieldCipher {
private val keyBytes: ByteArray = keyProvider.getOrCreateKey()
private val secretKey = SecretKeySpec(keyBytes, "AES")
@@ -1,4 +1,4 @@
package org.incido.compose.data.security
package org.incido.data.crypto
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -8,8 +8,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.incido.data.crypto.EncryptionVerification
import org.incido.data.crypto.NoteCryptoService
/**
* v0.3.5.0: runs the note-encryption integrity check and exposes the
@@ -0,0 +1,24 @@
package org.incido.data.crypto
/**
* Phase 1 (M3 task 2): the platform seam the field cipher is built on.
*
* [AesGcmFieldCipher] only ever needs "the 32-byte field key, get it or
* make one" — everything else about where the key lives is platform
* territory:
*
* - Android: `ObjectBoxKeyProvider` (Keystore-wrapped key persisted in
* DataStore) is passed as `objectBoxKeyProvider::getOrCreateKey`.
* - Desktop: a JVM key provider (M3 task 3) reads/generates a `0600`
* key file, or installs the key imported through an
* [org.incido.compose.data.backup.EncryptedKeyCodec] recovery
* envelope.
*
* Declared `fun interface` so both platforms can pass a plain function
* reference / lambda instead of a class.
*/
fun interface FieldKeyProvider {
/** Return the 32-byte field key, creating and persisting one if none
* exists yet. Idempotent; blocking by design (startup call site). */
fun getOrCreateKey(): ByteArray
}