From 40e2b9ff52f5272532657cb68a6bd98d2700cb3c Mon Sep 17 00:00:00 2001 From: Incido Developer Date: Tue, 6 Oct 2026 06:31:18 +1300 Subject: [PATCH] =?UTF-8?q?feat(phase-1/m3.2):=20FieldKeyProvider=20seam?= =?UTF-8?q?=20=E2=80=94=20AesGcmFieldCipher=20+=20EncryptionVerifier=20sha?= =?UTF-8?q?red?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../org/incido/compose/di/AppContainer.kt | 8 ++++--- .../compose/ui/notelist/NoteListViewModel.kt | 2 +- .../compose/ui/settings/SettingsViewModel.kt | 10 ++++---- .../incido/data/crypto}/AesGcmFieldCipher.kt | 14 +++++++---- .../incido/data/crypto}/EncryptionVerifier.kt | 4 +--- .../incido/data/crypto/FieldKeyProvider.kt | 24 +++++++++++++++++++ 6 files changed, 46 insertions(+), 16 deletions(-) rename {composeApp/src/main/kotlin/org/incido/compose/data/security => core/data/src/main/kotlin/org/incido/data/crypto}/AesGcmFieldCipher.kt (83%) rename {composeApp/src/main/kotlin/org/incido/compose/data/security => core/data/src/main/kotlin/org/incido/data/crypto}/EncryptionVerifier.kt (96%) create mode 100644 core/data/src/main/kotlin/org/incido/data/crypto/FieldKeyProvider.kt diff --git a/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt b/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt index 89f463d..8b35710 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt +++ b/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt @@ -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) } } diff --git a/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt b/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt index fb95b94..181e6db 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt +++ b/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt @@ -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.Home) diff --git a/composeApp/src/main/kotlin/org/incido/compose/ui/settings/SettingsViewModel.kt b/composeApp/src/main/kotlin/org/incido/compose/ui/settings/SettingsViewModel.kt index dfeac62..aa366c2 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/ui/settings/SettingsViewModel.kt +++ b/composeApp/src/main/kotlin/org/incido/compose/ui/settings/SettingsViewModel.kt @@ -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." diff --git a/composeApp/src/main/kotlin/org/incido/compose/data/security/AesGcmFieldCipher.kt b/core/data/src/main/kotlin/org/incido/data/crypto/AesGcmFieldCipher.kt similarity index 83% rename from composeApp/src/main/kotlin/org/incido/compose/data/security/AesGcmFieldCipher.kt rename to core/data/src/main/kotlin/org/incido/data/crypto/AesGcmFieldCipher.kt index 266054f..f2603a0 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/data/security/AesGcmFieldCipher.kt +++ b/core/data/src/main/kotlin/org/incido/data/crypto/AesGcmFieldCipher.kt @@ -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") diff --git a/composeApp/src/main/kotlin/org/incido/compose/data/security/EncryptionVerifier.kt b/core/data/src/main/kotlin/org/incido/data/crypto/EncryptionVerifier.kt similarity index 96% rename from composeApp/src/main/kotlin/org/incido/compose/data/security/EncryptionVerifier.kt rename to core/data/src/main/kotlin/org/incido/data/crypto/EncryptionVerifier.kt index b85fd1b..5b9bce0 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/data/security/EncryptionVerifier.kt +++ b/core/data/src/main/kotlin/org/incido/data/crypto/EncryptionVerifier.kt @@ -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 diff --git a/core/data/src/main/kotlin/org/incido/data/crypto/FieldKeyProvider.kt b/core/data/src/main/kotlin/org/incido/data/crypto/FieldKeyProvider.kt new file mode 100644 index 0000000..406ed78 --- /dev/null +++ b/core/data/src/main/kotlin/org/incido/data/crypto/FieldKeyProvider.kt @@ -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 +}