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 +}