mnemo-engine 0.12.1__tar.gz → 0.12.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/PKG-INFO +1 -1
  2. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/Cargo.lock +3 -3
  3. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/Cargo.toml +1 -1
  4. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/README.md +9 -9
  5. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/README.md +56 -16
  6. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/gradle.properties +1 -1
  7. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/release-check/settings.gradle.kts +1 -1
  8. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/androidTest/kotlin/io/github/toarchkumar/mnemo/AndroidTests.kt +1 -1
  9. mnemo_engine-0.12.3/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/MnemoKeystore.kt +344 -0
  10. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/MnemoStore.kt +37 -19
  11. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/MnemoStoreConfiguration.kt +4 -1
  12. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/MnemoStoreException.kt +12 -5
  13. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/test/kotlin/io/github/toarchkumar/mnemo/EmbedderLockTests.kt +41 -0
  14. mnemo_engine-0.12.3/mnemo/bindings/kotlin/src/test/kotlin/io/github/toarchkumar/mnemo/KeystoreErrorTests.kt +126 -0
  15. mnemo_engine-0.12.3/mnemo/bindings/kotlin/src/test/kotlin/io/github/toarchkumar/mnemo/StoreHardeningTests.kt +108 -0
  16. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/test/kotlin/io/github/toarchkumar/mnemo/StoreTests.kt +23 -0
  17. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/uniffi.toml +5 -0
  18. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/README.md +83 -24
  19. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Sources/Mnemo/Lifecycle.swift +28 -6
  20. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Sources/Mnemo/MnemoKeychain.swift +16 -0
  21. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Sources/Mnemo/MnemoStore.swift +149 -31
  22. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Sources/Mnemo/MnemoStoreError.swift +23 -0
  23. mnemo_engine-0.12.3/mnemo/bindings/swift/Tests/MnemoTests/EmbedQueueTests.swift +120 -0
  24. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Tests/MnemoTests/EmbedderLockTests.swift +8 -3
  25. mnemo_engine-0.12.3/mnemo/bindings/swift/Tests/MnemoTests/StoreHardeningTests.swift +146 -0
  26. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Tests/MnemoTests/StoreTests.swift +24 -0
  27. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Tests/MnemoTests/iOSTests.swift +2 -0
  28. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/device_budget.rs +52 -1
  29. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/bin/mnemo.rs +5 -4
  30. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/lib.rs +4 -4
  31. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/pager.rs +44 -3
  32. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/crud.rs +52 -0
  33. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/essence.rs +1 -1
  34. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/flush.rs +40 -0
  35. mnemo_engine-0.12.3/mnemo/src/store/header_graft_tests.rs +180 -0
  36. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/keyed.rs +30 -3
  37. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/load.rs +6 -2
  38. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/maintenance.rs +47 -10
  39. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/mod.rs +74 -1
  40. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/open.rs +42 -0
  41. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/records.rs +5 -2
  42. mnemo_engine-0.12.3/mnemo/tests/engine_safety.rs +277 -0
  43. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fmt9b_column.rs +13 -4
  44. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fmt9d_bm25.rs +13 -6
  45. mnemo_engine-0.12.3/mnemo/tests/lock_release.rs +115 -0
  46. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/rekey_export.rs +5 -1
  47. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo-python/Cargo.lock +2 -2
  48. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo-python/Cargo.toml +1 -1
  49. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo-python/test_mnemo.py +8 -0
  50. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/pyproject.toml +1 -1
  51. mnemo_engine-0.12.1/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/MnemoKeystore.kt +0 -213
  52. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/README.md +0 -0
  53. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/.gitignore +0 -0
  54. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/cache.rs +0 -0
  55. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/corpus.rs +0 -0
  56. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/embed_overhead.rs +0 -0
  57. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/open.rs +0 -0
  58. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/recall.rs +0 -0
  59. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/thread.rs +0 -0
  60. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/benches/write.rs +0 -0
  61. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/.gitignore +0 -0
  62. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/NOTICE +0 -0
  63. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/build.gradle.kts +0 -0
  64. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/consumer-rules.pro +0 -0
  65. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/gradle/wrapper/gradle-wrapper.jar +0 -0
  66. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/gradle/wrapper/gradle-wrapper.properties +0 -0
  67. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/gradlew +0 -0
  68. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/gradlew.bat +0 -0
  69. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/release-check/build.gradle.kts +0 -0
  70. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/release-check/src/main/AndroidManifest.xml +0 -0
  71. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/release-check/src/test/kotlin/io/github/toarchkumar/mnemo/check/ConsumerTest.kt +0 -0
  72. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/scripts/build-native.sh +0 -0
  73. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/scripts/central-dry-run.sh +0 -0
  74. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/scripts/check-elf.py +0 -0
  75. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/scripts/check-package.py +0 -0
  76. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/scripts/emulator-tests.sh +0 -0
  77. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/settings.gradle.kts +0 -0
  78. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/androidTest/AndroidManifest.xml +0 -0
  79. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/androidTest/kotlin/io/github/toarchkumar/mnemo/TestActivity.kt +0 -0
  80. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/AndroidManifest.xml +0 -0
  81. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/FlushPolicy.kt +0 -0
  82. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/KeyId.kt +0 -0
  83. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/MnemoKeyStorage.kt +0 -0
  84. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/main/kotlin/io/github/toarchkumar/mnemo/ProcessStopObserver.kt +0 -0
  85. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/sharedTest/kotlin/io/github/toarchkumar/mnemo/Support.kt +0 -0
  86. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/test/kotlin/io/github/toarchkumar/mnemo/FlushPolicyTests.kt +0 -0
  87. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/kotlin/src/test/kotlin/io/github/toarchkumar/mnemo/KeystoreFileTests.kt +0 -0
  88. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/.gitignore +0 -0
  89. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Package.swift +0 -0
  90. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Sources/Mnemo/FilePolicy.swift +0 -0
  91. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Sources/Mnemo/FlushPolicy.swift +0 -0
  92. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/TestHost/App/MnemoTestHostApp.swift +0 -0
  93. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/TestHost/project.yml +0 -0
  94. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Tests/MnemoTests/FlushPolicyTests.swift +0 -0
  95. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/Tests/MnemoTests/Support.swift +0 -0
  96. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/scripts/build-xcframework.sh +0 -0
  97. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/scripts/check-published-package.sh +0 -0
  98. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/scripts/check-release-package.sh +0 -0
  99. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/scripts/kill-test.sh +0 -0
  100. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/bindings/swift/scripts/make-release-package.sh +0 -0
  101. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/deny.toml +0 -0
  102. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/ann_defaults_v0_6_0.rs +0 -0
  103. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/ann_diagnose.rs +0 -0
  104. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/ann_recall_grid.rs +0 -0
  105. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/ann_recall_grid_v2.rs +0 -0
  106. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/embedder_server.py +0 -0
  107. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/file_size_breakdown.rs +0 -0
  108. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/fmt9_proto.rs +0 -0
  109. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/open_rss.rs +0 -0
  110. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/quickstart.rs +0 -0
  111. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/ranking_v2.rs +0 -0
  112. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/session.rs +0 -0
  113. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/examples/thread_profile.rs +0 -0
  114. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/cache.rs +0 -0
  115. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/crypto.rs +0 -0
  116. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/embed.rs +0 -0
  117. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/embed_cmd.rs +0 -0
  118. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/error.rs +0 -0
  119. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/format.rs +0 -0
  120. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/freelist.rs +0 -0
  121. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/index.rs +0 -0
  122. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/keyslot.rs +0 -0
  123. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/mcp.rs +0 -0
  124. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/memory.rs +0 -0
  125. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/result_cache.rs +0 -0
  126. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/session.rs +0 -0
  127. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/ann.rs +0 -0
  128. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/cache_api.rs +0 -0
  129. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/continuation.rs +0 -0
  130. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/hooks.rs +0 -0
  131. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/recall.rs +0 -0
  132. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/snapshots.rs +0 -0
  133. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/store/threads.rs +0 -0
  134. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/text.rs +0 -0
  135. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/transcript.rs +0 -0
  136. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/veccol.rs +0 -0
  137. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/src/wal.rs +0 -0
  138. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/cli_smoke.rs +0 -0
  139. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/dur1_torn_header.rs +0 -0
  140. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fix1_correctness_sweep.rs +0 -0
  141. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fixtures/v0_10_0_memories.jsonl +0 -0
  142. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fmt9_skeleton.rs +0 -0
  143. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fmt9c_commit.rs +0 -0
  144. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/fmt9e_zstd.rs +0 -0
  145. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/integration.rs +0 -0
  146. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/proptest_store.proptest-regressions +0 -0
  147. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/proptest_store.rs +0 -0
  148. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/sec1_nonce_uniqueness.rs +0 -0
  149. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/suites/dur1_torn_header.rs +0 -0
  150. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/suites/fix1_correctness_sweep.rs +0 -0
  151. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/suites/integration.rs +0 -0
  152. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/suites/sec1_nonce_uniqueness.rs +0 -0
  153. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/v0_10_fixture.rs +0 -0
  154. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo/tests/wal_recover_alloc.rs +0 -0
  155. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo-python/.gitignore +0 -0
  156. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo-python/README.md +0 -0
  157. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/mnemo-python/src/lib.rs +0 -0
  158. {mnemo_engine-0.12.1 → mnemo_engine-0.12.3}/python/mnemo_engine/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mnemo-engine
3
- Version: 0.12.1
3
+ Version: 0.12.3
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: License :: OSI Approved :: Apache Software License
@@ -821,7 +821,7 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
821
821
 
822
822
  [[package]]
823
823
  name = "mnemo-engine"
824
- version = "0.12.1"
824
+ version = "0.12.3"
825
825
  dependencies = [
826
826
  "aes-gcm",
827
827
  "argon2",
@@ -849,7 +849,7 @@ dependencies = [
849
849
 
850
850
  [[package]]
851
851
  name = "mnemo-node"
852
- version = "0.12.1"
852
+ version = "0.12.3"
853
853
  dependencies = [
854
854
  "mnemo-engine",
855
855
  "napi",
@@ -859,7 +859,7 @@ dependencies = [
859
859
 
860
860
  [[package]]
861
861
  name = "mnemo-uniffi"
862
- version = "0.12.1"
862
+ version = "0.12.3"
863
863
  dependencies = [
864
864
  "mnemo-engine",
865
865
  "serde_json",
@@ -10,7 +10,7 @@
10
10
  # The CLI binary name stays `mnemo`, so users still invoke `mnemo` on
11
11
  # the command line after `cargo install mnemo-engine`.
12
12
  name = "mnemo-engine"
13
- version = "0.12.1"
13
+ version = "0.12.3"
14
14
  edition = "2021"
15
15
  rust-version = "1.75"
16
16
  # `benches/corpus.rs` is a shared helper module pulled into real benches
@@ -309,15 +309,15 @@ Argon2id; the KEK wraps a random data-encryption key (DEK); the DEK encrypts
309
309
  every data page with AES-256-GCM. Changing the passphrase (`rekey`) only
310
310
  re-wraps the DEK — the bulk pages are never rewritten.
311
311
 
312
- That makes `rekey` cheap, and it also means a rekey changes who can open
313
- the file *from now on, provided nobody kept the old secret*: the DEK is the
314
- same, so a copy of the file taken earlier still opens with the old
315
- passphrase, and so does the live file's data for anyone holding that copy's
316
- header. To revoke an old passphrase (after a suspected leak, say), rotate
317
- the DEK too: `mnemo rekey --rotate`, or `rekey` followed by
318
- `Mnemo::compact_file`, which rewrites the file under a fresh DEK.
319
- `Mnemo::export_rekeyed` writes a copy under a fresh DEK and a new secret,
320
- sharing no key material with the source.
312
+ That makes `rekey` cheap, and it means **a plain `rekey` changes the
313
+ secret that opens this file from now on; anyone who had the file and the
314
+ old secret can still read it and later writes.** The DEK is the same, so an
315
+ earlier copy of the file, or of just its header, still unlocks it with the
316
+ old passphrase. **Use `--rotate` to revoke** (after a suspected leak, say):
317
+ `mnemo rekey --rotate`, or `rekey` followed by `Mnemo::compact_file`,
318
+ rewrites the file under a fresh DEK. `Mnemo::export_rekeyed` writes a copy
319
+ under a fresh DEK and a new secret, sharing no key material with the
320
+ source. Rotation can't erase old ciphertext the storage hardware still holds: flash keeps rewritten blocks until it reuses them, and that's out of the engine's hands.
321
321
 
322
322
  The file is a sequence of fixed **8 KiB pages**:
323
323
 
@@ -19,7 +19,7 @@ From Maven Central (`mavenCentral()` in your repositories):
19
19
 
20
20
  ```kotlin
21
21
  dependencies {
22
- implementation("io.github.toarchkumar:mnemo-android:0.12.1")
22
+ implementation("io.github.toarchkumar:mnemo-android:0.12.3")
23
23
  }
24
24
  ```
25
25
 
@@ -101,6 +101,11 @@ val byWord = store.recall(emptyList(), RecallOptions(keywords = "UA 931"))
101
101
  - **Keyed facts:** `RememberOptions(key = ...)` writes a new version and
102
102
  supersedes the old one. `history(key)` lists the versions, newest first.
103
103
  - **The result cache:** `cachePut` and `cacheGet`, by exact key.
104
+ - **Access counts:** `RecallOptions.trackAccess` is off by default here
105
+ (v0.12.2). With it on, every recall marks its hits as read, which dirties
106
+ the file, so a recall made in the background ends in a full commit, and
107
+ ranking doesn't need it. Pass `RecallOptions(topK = 5u, trackAccess = true)` on the calls whose
108
+ access counts the app reads.
104
109
  - **Batches:** `withSession { session -> ... }` runs many calls on one
105
110
  open handle and commits once at the end (see Durability).
106
111
  - **Also:** `get`, `update`, `delete`, `about`, `stats`, `count`, `flush`,
@@ -114,17 +119,26 @@ It stays in memory while the file's **live facts** fit the configuration's
114
119
  superseded versions don't count. Past the budget, recall reads the vectors
115
120
  from the file instead, which is slower, and slower still as the file grows.
116
121
 
117
- | Dimensions | Facts that fit 16 MiB |
118
- |---|---|
119
- | 384 | 21,732 |
120
- | 512 | 16,320 |
121
- | 768 | 10,894 |
122
+ | Dimensions | Bytes per fact | Facts that fit 16 MiB | Budget to stay resident at 50,000 facts |
123
+ |---|---|---|---|
124
+ | 384 | 772 | 21,732 | 38.6 MB |
125
+ | 512 | 1,028 | 16,320 | 51.4 MB |
126
+ | 768 | 1,540 | 10,894 | 77.0 MB |
122
127
 
123
- ``store.stats()`` says which path recall takes now, in `recallMode`:
128
+ `store.stats()` says which path recall takes now, in `recallMode`:
124
129
  `RecallMode.EXACT_RESIDENT` (in memory, the fast path),
125
- `RecallMode.EXACT_PAGED` (from the file) or `RecallMode.INDEXED`. Past the budget, raise `vectorCacheBudgetBytes` if the app can
126
- spend the memory. Past 20,000 facts, `buildIndex()` builds the IVF+PQ
127
- index instead.
130
+ `RecallMode.EXACT_PAGED` (from the file) or `RecallMode.INDEXED`, and how many bytes the column needs to stay in memory, in
131
+ `vectorColumnBytes` (`facts × (2 × dimensions + 4)`).
132
+
133
+ **At the cliff, raise the budget first.** Setting `vectorCacheBudgetBytes` to at least
134
+ `vectorColumnBytes` costs that much memory and gives the fast path back at
135
+ once (at 22,000 facts × 384, warm recall went from 23.6 ms paged to 4.9 ms
136
+ with the budget raised). Build an index (`buildIndex()`) only when the
137
+ column no longer fits the memory the app can spare, past 20,000 facts, and
138
+ only in a maintenance window: the build holds the store and takes long (49.8
139
+ s at 22,000 facts on an M1 Pro; 19 s at 10,000 and 179 s at 100,000 in the
140
+ perf file's `build_secs`), and a raised `cachePages` doesn't help a large
141
+ file.
128
142
 
129
143
  ### History
130
144
 
@@ -134,6 +148,9 @@ never lets them pin more than a quarter of the file. Under the default
134
148
  flush policy a busy app commits about once a second, so the history
135
149
  reaches back about a day, not further. A v8 file keeps the newest 256.
136
150
  `compact()` drops all of them.
151
+ `setMaxSnapshots(_:)` (Kotlin: `setMaxSnapshots(count)`) keeps a different
152
+ number instead, as a hard cap; the store applies it every time it opens the
153
+ file.
137
154
 
138
155
  ### Durability
139
156
 
@@ -265,15 +282,25 @@ transfer. So neither the file nor its key reaches a new phone, and the app
265
282
  starts empty there unless it imports an export.
266
283
 
267
284
  **Recovering from `MnemoStoreException.KeyMissing`.** The file exists, but
268
- its key isn't available. This happens in three ways:
285
+ its key is gone. This happens in four ways:
269
286
 
270
287
  - the key file was deleted;
271
288
  - the file was copied from another device;
272
- - the Keystore key that wraps it is gone or invalidated, which is rare.
289
+ - the key file is damaged, or doesn't authenticate;
290
+ - the Keystore key that wraps it is missing or permanently invalidated,
291
+ which is rare.
273
292
 
274
293
  The data can't be read without the key. The app can import a passphrase
275
294
  export (below), or delete the file and start over.
276
295
 
296
+ **`MnemoStoreException.Keystore` is different: never delete anything for
297
+ it.** It means the Keystore, or the key file behind it, failed for a reason
298
+ that can pass: the Keystore service busy or restarting, a binder error, an
299
+ I/O error, or a wrapping-key entry that exists but couldn't be read. The key
300
+ is probably still there. Retry later (on the next launch, say), and treat
301
+ only `KeyMissing` as a lost key. The store never replaces a wrapping key
302
+ whose alias exists, since that would orphan every key it wraps.
303
+
277
304
  ### Moving to another device: passphrase export and import
278
305
 
279
306
  ```kotlin
@@ -292,8 +319,9 @@ val store = MnemoStore.importWithPassphrase(
292
319
  - The destination must not exist yet, and the source is never modified.
293
320
  - Both write a compacted copy (live data, no history) under a **fresh data
294
321
  key**. The export shares no key material with the device's file, and the
295
- imported file shares none with the export: the transfer passphrase never
322
+ import is re-protected under a new data key: the transfer passphrase never
296
323
  opens the imported file, even with a leftover copy of the export.
324
+ - Rotation can't erase old ciphertext the storage hardware still holds: flash keeps rewritten blocks until it reuses them, and that's out of the engine's hands.
297
325
  - Have the user choose the passphrase. Never store it next to the export,
298
326
  and delete the export once it's imported.
299
327
 
@@ -323,10 +351,22 @@ name. Nothing else is needed.
323
351
 
324
352
  - **`MnemoException`** comes from the engine: `Locked`, `WrongPassphrase`,
325
353
  `HeaderTampered`, `UnsupportedVersion`, `NotFound`, `ReadOnly`,
326
- `DimensionMismatch`, `Invalid`, `Io`, `Corrupt`, `Closed` and `Embedder`.
327
- `MnemoStore` never surfaces `Closed`.
354
+ `DimensionMismatch`, `Invalid`, `Io`, `Corrupt`, `Closed` and `Embedder`,
355
+ and since v0.12.2 `NotAMnemoFile`, `NeedsWriteOpen`, `Kdf` and `Crypto`.
356
+ `MnemoStore` never surfaces `Closed`: it reopens the file and retries once,
357
+ and a handle a panic poisoned is treated the same way.
328
358
  - **`MnemoStoreException`** comes from this layer: `KeyMissing(keyId,
329
- path)`, `Keystore(operation)` and `File`.
359
+ path)` (the key is gone), `Keystore(operation)` (retryable; see above)
360
+ and `File`.
361
+
362
+ **What to do about an error, in three classes** (`e.isRetryable()` tells the first
363
+ apart for `MnemoException`):
364
+
365
+ | Class | Errors | What to do |
366
+ |---|---|---|
367
+ | Retry later | `Locked`, `Io`, `Kdf` (the KDF couldn't get its memory), `Closed`; `MnemoStoreException.Keystore` | Try again later, on the next launch, say. The data is fine. |
368
+ | Fix the call | `Invalid`, `DimensionMismatch`, `ReadOnly`, `NeedsWriteOpen`, `NotFound`, `WrongPassphrase`, `Embedder` | Change what's passed (or open read-write once, for `NeedsWriteOpen`). |
369
+ | The data is unreadable | `Corrupt` (integrity failures only), `HeaderTampered`, `Crypto`, `NotAMnemoFile`, `UnsupportedVersion`; `MnemoStoreException.KeyMissing` | Restore from a backup or an export. Only here is deleting the file ever the answer. |
330
370
 
331
371
  ## Tests
332
372
 
@@ -3,4 +3,4 @@ org.gradle.caching=true
3
3
  android.useAndroidX=true
4
4
  # The published coordinates (io.github.toarchkumar:mnemo-android).
5
5
  GROUP=io.github.toarchkumar
6
- VERSION_NAME=0.12.1
6
+ VERSION_NAME=0.12.3
@@ -2,7 +2,7 @@
2
2
  // mnemo-android artifact from mavenLocal() (run the library's signed
3
3
  // publishToMavenLocal first) and runs a JVM smoke test against it.
4
4
  //
5
- // ../gradlew -p release-check test -PmnemoVersion=0.12.1 \
5
+ // ../gradlew -p release-check test -PmnemoVersion=0.12.3 \
6
6
  // -Pjna.library.path=$PWD/generated/host
7
7
  pluginManagement {
8
8
  repositories {
@@ -49,7 +49,7 @@ class AndroidTests {
49
49
  val alias = "mnemo-test-${UUID.randomUUID()}"
50
50
  aliases += alias
51
51
  val dir = File(context.cacheDir, "keys-${UUID.randomUUID()}").also { cleanup += it }
52
- return MnemoKeystore(dir, AndroidKeystoreWrapper(alias)) to alias
52
+ return MnemoKeystore(dir, AndroidKeystoreWrapper(alias, AndroidKeystoreKeys, File(dir, MnemoKeystore.LOCK_FILE))) to alias
53
53
  }
54
54
 
55
55
  @Test fun theKeystoreWrapsKeysWithANonExportableKey() {
@@ -0,0 +1,344 @@
1
+ package io.github.toarchkumar.mnemo
2
+
3
+ import android.content.Context
4
+ import android.security.keystore.KeyGenParameterSpec
5
+ import android.security.keystore.KeyPermanentlyInvalidatedException
6
+ import android.security.keystore.KeyProperties
7
+ import android.system.Os
8
+ import android.system.OsConstants
9
+ import java.io.File
10
+ import java.io.FileNotFoundException
11
+ import java.io.FileOutputStream
12
+ import java.io.IOException
13
+ import java.io.RandomAccessFile
14
+ import java.security.KeyStore
15
+ import java.util.UUID
16
+ import javax.crypto.AEADBadTagException
17
+ import javax.crypto.Cipher
18
+ import javax.crypto.KeyGenerator
19
+ import javax.crypto.SecretKey
20
+ import javax.crypto.spec.GCMParameterSpec
21
+
22
+ /**
23
+ * Keys for [MnemoStore] on Android, wrapped by the Android Keystore.
24
+ *
25
+ * - **The wrapping key** is one AES-256-GCM key in `AndroidKeyStore` under
26
+ * [alias]: generated on first use, non-exportable (it never leaves the
27
+ * Keystore, in hardware where the device has it), and usable without user
28
+ * authentication, so stores work in the background and after a lock-screen
29
+ * change.
30
+ * - **Each MNemo key** (32 random bytes per file) is encrypted by it and
31
+ * written to `noBackupFilesDir/mnemo/keys/<id>.key`, with the key id as
32
+ * associated data, so a key file can't be passed off under another id.
33
+ * - **Device-only.** Keystore keys never leave the device, and
34
+ * `noBackupFilesDir` is excluded from backups and device transfers. A file
35
+ * restored or copied elsewhere fails with [MnemoStoreException.KeyMissing];
36
+ * move data with [MnemoStore.exportWithPassphrase] instead.
37
+ * - **Errors.** [MnemoStoreException.KeyMissing] means the key is gone: its
38
+ * file is missing or damaged, the wrapping key is missing or permanently
39
+ * invalidated, or the file doesn't authenticate. Every other failure (the
40
+ * Keystore service busy or restarting, a binder error, an I/O error) is
41
+ * [MnemoStoreException.Keystore], which is retryable and never a reason to
42
+ * delete anything.
43
+ * - **Key files are durable:** each is synced to disk before it's renamed
44
+ * into place, and the directory is synced after.
45
+ *
46
+ * No `androidx.security:security-crypto`: it's deprecated, and this is the
47
+ * part of it that's needed.
48
+ */
49
+ class MnemoKeystore internal constructor(
50
+ private val directory: File,
51
+ private val wrapper: KeyWrapper,
52
+ ) : MnemoKeyStorage {
53
+ /**
54
+ * @param alias the `AndroidKeyStore` alias of the wrapping key. Keep the
55
+ * default unless two independent key sets must not share one.
56
+ */
57
+ @JvmOverloads
58
+ constructor(context: Context, alias: String = DEFAULT_ALIAS) :
59
+ this(
60
+ defaultDirectory(context),
61
+ AndroidKeystoreWrapper(alias, AndroidKeystoreKeys, File(defaultDirectory(context), LOCK_FILE)),
62
+ )
63
+
64
+ override fun storeKey(id: String, key: ByteArray) {
65
+ require(key.size == KEY_BYTES) { "a MNemo key is $KEY_BYTES bytes" }
66
+ val (iv, sealed) = wrapper.wrap(key, aad(id))
67
+ val bytes = byteArrayOf(*MAGIC, iv.size.toByte()) + iv + sealed
68
+ val target = fileFor(id)
69
+ ensureDirectory()
70
+ val tmp = File(directory, "${target.name}.${UUID.randomUUID()}.tmp")
71
+ try {
72
+ // Synced before the rename, so a power loss right after a create
73
+ // or an import never leaves the database without its key.
74
+ FileOutputStream(tmp).use { out ->
75
+ out.write(bytes)
76
+ out.fd.sync()
77
+ }
78
+ if (!tmp.renameTo(target)) throw IOException("rename to $target failed")
79
+ } catch (e: IOException) {
80
+ tmp.delete()
81
+ throw MnemoStoreException.File("can't write the key file $target", e)
82
+ }
83
+ syncDirectory(directory)
84
+ }
85
+
86
+ override fun key(id: String): ByteArray {
87
+ val file = fileFor(id)
88
+ // Decided by existence, not by the exception: the JVM reports a
89
+ // permission error or a directory as FileNotFoundException too.
90
+ if (!file.exists()) {
91
+ throw MnemoStoreException.KeyMissing(id, null, FileNotFoundException(file.path))
92
+ }
93
+ val bytes = try {
94
+ file.readBytes()
95
+ } catch (e: IOException) {
96
+ // The file is there but couldn't be read now: retryable.
97
+ throw MnemoStoreException.Keystore("read the key file", e)
98
+ }
99
+ val header = MAGIC.size + 1
100
+ if (bytes.size <= header || !bytes.copyOfRange(0, MAGIC.size).contentEquals(MAGIC)) {
101
+ throw MnemoStoreException.KeyMissing(id, null, IOException("$file isn't a MNemo key file"))
102
+ }
103
+ val ivLength = bytes[MAGIC.size].toInt()
104
+ if (ivLength <= 0 || bytes.size <= header + ivLength) {
105
+ throw MnemoStoreException.KeyMissing(id, null, IOException("$file is truncated"))
106
+ }
107
+ val iv = bytes.copyOfRange(header, header + ivLength)
108
+ val sealed = bytes.copyOfRange(header + ivLength, bytes.size)
109
+ val key = try {
110
+ wrapper.unwrap(iv, sealed, aad(id))
111
+ } catch (e: MnemoStoreException) {
112
+ throw e
113
+ } catch (e: Exception) {
114
+ if (meansKeyMissing(e)) throw MnemoStoreException.KeyMissing(id, null, e)
115
+ // Anything else may pass (the Keystore busy or restarting): the
116
+ // key may well still be there.
117
+ throw MnemoStoreException.Keystore("unwrap", e)
118
+ }
119
+ if (key.size != KEY_BYTES) {
120
+ key.zeroize()
121
+ throw MnemoStoreException.KeyMissing(id, null, IOException("$file holds no 32-byte key"))
122
+ }
123
+ return key
124
+ }
125
+
126
+ override fun keyIds(): List<String> =
127
+ directory.listFiles { f -> f.isFile && f.name.endsWith(SUFFIX) }
128
+ ?.map { it.name.removeSuffix(SUFFIX) }
129
+ ?.filter { ID.matches(it) }
130
+ ?.sorted()
131
+ ?: emptyList()
132
+
133
+ override fun deleteKey(id: String) {
134
+ val file = fileFor(id)
135
+ if (file.exists() && !file.delete()) {
136
+ throw MnemoStoreException.File("can't delete the key file $file")
137
+ }
138
+ }
139
+
140
+ /** The key file for [id] (tests). */
141
+ internal fun fileFor(id: String): File {
142
+ require(ID.matches(id)) { "invalid key id \"$id\"" }
143
+ return File(directory, "$id$SUFFIX")
144
+ }
145
+
146
+ private fun ensureDirectory() {
147
+ if (!directory.isDirectory && !directory.mkdirs() && !directory.isDirectory) {
148
+ throw MnemoStoreException.File("can't create $directory")
149
+ }
150
+ }
151
+
152
+ private fun aad(id: String) = "mnemo-key:$id".toByteArray(Charsets.UTF_8)
153
+
154
+ /**
155
+ * The failures that mean the key is gone for good: the file doesn't
156
+ * authenticate (tampered, or wrapped by a key that's been replaced), or
157
+ * the wrapping key is missing or permanently invalidated.
158
+ */
159
+ private fun meansKeyMissing(e: Exception): Boolean =
160
+ e is AEADBadTagException || e is WrappingKeyMissing || isKeyPermanentlyInvalidated(e)
161
+
162
+ companion object {
163
+ /** The default `AndroidKeyStore` alias of the wrapping key. */
164
+ const val DEFAULT_ALIAS = "io.github.toarchkumar.mnemo.wrap"
165
+
166
+ /** `noBackupFilesDir/mnemo/keys/`. */
167
+ @JvmStatic
168
+ fun defaultDirectory(context: Context): File =
169
+ File(MnemoStoreConfiguration.defaultDirectory(context), "keys")
170
+
171
+ internal const val KEY_BYTES = 32
172
+ internal const val LOCK_FILE = ".wrapping-key.lock"
173
+ private const val SUFFIX = ".key"
174
+ private val MAGIC = byteArrayOf('M'.code.toByte(), 'N'.code.toByte(), 'K'.code.toByte(), 1)
175
+ private val ID = Regex("[A-Za-z0-9-]{1,64}")
176
+ }
177
+ }
178
+
179
+ /** The wrapping key's alias isn't in the Keystore: the key is gone, not busy. */
180
+ internal class WrappingKeyMissing(alias: String) : Exception("no wrapping key \"$alias\" in AndroidKeyStore")
181
+
182
+ /** `KeyPermanentlyInvalidatedException`, by class, so JVM tests needn't load it. */
183
+ private fun isKeyPermanentlyInvalidated(e: Exception): Boolean = try {
184
+ e is KeyPermanentlyInvalidatedException
185
+ } catch (e: LinkageError) {
186
+ false
187
+ }
188
+
189
+ /**
190
+ * Syncs a directory so a rename in it is durable. Best effort: Android allows
191
+ * it through `Os`, and where it doesn't (a JVM test, an old file system) the
192
+ * file itself is already synced.
193
+ */
194
+ internal fun syncDirectory(dir: File) {
195
+ runCatching {
196
+ val fd = Os.open(dir.path, OsConstants.O_RDONLY, 0)
197
+ try {
198
+ Os.fsync(fd)
199
+ } finally {
200
+ Os.close(fd)
201
+ }
202
+ }
203
+ }
204
+
205
+ /** Encrypts and decrypts key material (the Keystore on Android; software in tests). */
206
+ internal interface KeyWrapper {
207
+ /** Returns the IV and the ciphertext with its tag. */
208
+ fun wrap(plain: ByteArray, aad: ByteArray): Pair<ByteArray, ByteArray>
209
+
210
+ fun unwrap(iv: ByteArray, sealed: ByteArray, aad: ByteArray): ByteArray
211
+ }
212
+
213
+ /** Where the wrapping key lives: `AndroidKeyStore` on a device, a fake in JVM tests. */
214
+ internal interface WrappingKeys {
215
+ /** The key under [alias], or `null` if the entry can't be read as a key. */
216
+ fun getKey(alias: String): SecretKey?
217
+
218
+ fun containsAlias(alias: String): Boolean
219
+
220
+ /** Generates a new key under [alias]. Replaces any key there. */
221
+ fun generate(alias: String): SecretKey
222
+ }
223
+
224
+ /** The real `AndroidKeyStore`. */
225
+ internal object AndroidKeystoreKeys : WrappingKeys {
226
+ private const val PROVIDER = "AndroidKeyStore"
227
+
228
+ private fun store() = KeyStore.getInstance(PROVIDER).apply { load(null) }
229
+
230
+ override fun getKey(alias: String): SecretKey? = store().getKey(alias, null) as? SecretKey
231
+
232
+ override fun containsAlias(alias: String): Boolean = store().containsAlias(alias)
233
+
234
+ override fun generate(alias: String): SecretKey =
235
+ KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, PROVIDER).run {
236
+ init(
237
+ KeyGenParameterSpec.Builder(
238
+ alias,
239
+ KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT,
240
+ )
241
+ .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
242
+ .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
243
+ .setKeySize(256)
244
+ .setRandomizedEncryptionRequired(true)
245
+ .setUserAuthenticationRequired(false)
246
+ .build(),
247
+ )
248
+ generateKey()
249
+ }
250
+ }
251
+
252
+ /**
253
+ * AES-256-GCM with a non-exportable key in `AndroidKeyStore`.
254
+ *
255
+ * The wrapping key is created only when its alias doesn't exist. If the
256
+ * alias exists but the key can't be read, that's a retryable
257
+ * [MnemoStoreException.Keystore], never a new key: generating over a live
258
+ * alias would orphan every key wrapped by the old one. Creation holds a file
259
+ * lock in the keys directory, so two processes of one app can't both create.
260
+ */
261
+ internal class AndroidKeystoreWrapper(
262
+ private val alias: String,
263
+ private val keys: WrappingKeys,
264
+ private val lockFile: File,
265
+ ) : KeyWrapper {
266
+ override fun wrap(plain: ByteArray, aad: ByteArray): Pair<ByteArray, ByteArray> {
267
+ val cipher = try {
268
+ Cipher.getInstance(TRANSFORMATION).apply {
269
+ // The Keystore picks a fresh random IV (randomized encryption).
270
+ init(Cipher.ENCRYPT_MODE, wrappingKey(create = true))
271
+ updateAAD(aad)
272
+ }
273
+ } catch (e: MnemoStoreException) {
274
+ throw e
275
+ } catch (e: Exception) {
276
+ throw MnemoStoreException.Keystore("wrap", e)
277
+ }
278
+ return try {
279
+ cipher.iv to cipher.doFinal(plain)
280
+ } catch (e: Exception) {
281
+ throw MnemoStoreException.Keystore("wrap", e)
282
+ }
283
+ }
284
+
285
+ override fun unwrap(iv: ByteArray, sealed: ByteArray, aad: ByteArray): ByteArray {
286
+ val key = wrappingKey(create = false) ?: throw WrappingKeyMissing(alias)
287
+ return Cipher.getInstance(TRANSFORMATION).run {
288
+ init(Cipher.DECRYPT_MODE, key, GCMParameterSpec(TAG_BITS, iv))
289
+ updateAAD(aad)
290
+ doFinal(sealed)
291
+ }
292
+ }
293
+
294
+ /**
295
+ * The wrapping key. `null` only when its alias doesn't exist and
296
+ * [create] is false. An alias that exists but gives no key is the
297
+ * retryable [MnemoStoreException.Keystore], whatever [create] says.
298
+ */
299
+ private fun wrappingKey(create: Boolean): SecretKey? = synchronized(LOCK) {
300
+ existingKey()?.let { return it }
301
+ if (!create) return null
302
+ // Another process may be creating it: take the file lock, then look
303
+ // again before generating.
304
+ lockFile.parentFile?.mkdirs()
305
+ RandomAccessFile(lockFile, "rw").use { raf ->
306
+ raf.channel.lock().use {
307
+ existingKey()?.let { return it }
308
+ try {
309
+ keys.generate(alias)
310
+ } catch (e: Exception) {
311
+ throw MnemoStoreException.Keystore("generate the wrapping key", e)
312
+ }
313
+ }
314
+ }
315
+ }
316
+
317
+ /** The key, or `null` when the alias doesn't exist; throws if it exists unreadable. */
318
+ private fun existingKey(): SecretKey? {
319
+ val key = try {
320
+ keys.getKey(alias)
321
+ } catch (e: Exception) {
322
+ throw MnemoStoreException.Keystore("load the wrapping key", e)
323
+ }
324
+ if (key != null) return key
325
+ val present = try {
326
+ keys.containsAlias(alias)
327
+ } catch (e: Exception) {
328
+ throw MnemoStoreException.Keystore("look up the wrapping key", e)
329
+ }
330
+ if (present) {
331
+ throw MnemoStoreException.Keystore(
332
+ "load the wrapping key",
333
+ IllegalStateException("the alias \"$alias\" exists but returned no key; not replacing it"),
334
+ )
335
+ }
336
+ return null
337
+ }
338
+
339
+ private companion object {
340
+ const val TRANSFORMATION = "AES/GCM/NoPadding"
341
+ const val TAG_BITS = 128
342
+ val LOCK = Any()
343
+ }
344
+ }
@@ -113,6 +113,9 @@ class MnemoStore private constructor(
113
113
  private var commitTimer: Job? = null
114
114
  private var timerGeneration = 0
115
115
 
116
+ /** Set by [setMaxSnapshots]; applied on every open. Guarded by [mutex]. */
117
+ private var maxSnapshots: UInt? = null
118
+
116
119
  /** The last error a flush the store ran on its own hit (`ON_STOP`, a coalesced commit), if any. */
117
120
  @Volatile var lastBackgroundError: Throwable? = null
118
121
  private set
@@ -287,9 +290,25 @@ class MnemoStore private constructor(
287
290
  /** Makes every write so far durable. A no-op while the file is closed. */
288
291
  suspend fun flush() = locked { commitLocked() }
289
292
 
290
- /** Flushes and closes the file, releasing its lock. The next call reopens it. */
293
+ /**
294
+ * Flushes and closes the file, releasing its lock. The next call reopens
295
+ * it. If the final flush fails (a full disk, say), the file stays open
296
+ * with every unflushed write and the error is thrown: free space and
297
+ * call [flush] or `close()` again (v0.12.2).
298
+ */
291
299
  suspend fun close() = locked { closeLocked() }
292
300
 
301
+ /**
302
+ * How many past snapshots (restore points) the file keeps, replacing the
303
+ * default (the newest 16 plus any from the last 24 hours). Applies now
304
+ * and every time the store reopens the file (v0.12.2).
305
+ */
306
+ suspend fun setMaxSnapshots(count: UInt) = locked {
307
+ maxSnapshots = count
308
+ db?.setMaxSnapshots(count)
309
+ Unit
310
+ }
311
+
293
312
  /**
294
313
  * Runs [body] on one open handle with no other call in between, then
295
314
  * commits once (unless the policy is [FlushPolicy.Manual]). Use it for a
@@ -508,14 +527,13 @@ class MnemoStore private constructor(
508
527
  private fun closeLocked() {
509
528
  val open = db ?: return
510
529
  cancelPendingCommitLocked()
530
+ // The binding's close flushes first; if that fails it keeps its
531
+ // handle (and the writes) and throws, and so does this, with the
532
+ // store still holding the open handle for a retry.
533
+ open.closeFile()
511
534
  db = null
512
- try {
513
- open.flush()
514
- commits += 1
515
- open.closeFile()
516
- } finally {
517
- open.close() // frees the Kotlin handle
518
- }
535
+ commits += 1
536
+ open.close() // frees the Kotlin handle
519
537
  }
520
538
 
521
539
  private fun dropHandle() {
@@ -550,6 +568,7 @@ class MnemoStore private constructor(
550
568
  if (configuration.cachePages != MnemoStoreConfiguration.DEFAULT_CACHE_PAGES) {
551
569
  opened.setCacheCapacity(configuration.cachePages)
552
570
  }
571
+ maxSnapshots?.let { opened.setMaxSnapshots(it) }
553
572
  } catch (e: Exception) {
554
573
  runCatching { opened.closeFile() }
555
574
  opened.close()
@@ -603,21 +622,20 @@ class MnemoStore private constructor(
603
622
  } catch (e: MnemoStoreException.KeyMissing) {
604
623
  continue
605
624
  }
606
- try {
607
- // Opening with a raw key costs no KDF, so a wrong key is cheap.
608
- val probe = MnemoDb.openReadOnlyWithKey(file.path, key)
609
- try {
610
- probe.closeFile()
611
- } finally {
612
- probe.close()
613
- }
614
- } catch (e: MnemoException.WrongPassphrase) {
615
- key.zeroize()
616
- continue
625
+ // From the header alone (no KDF for a raw key, no catalog load),
626
+ // and it answers even when the file has a WAL to recover, where a
627
+ // read-only open refuses (v0.12.2, RB-001.23). Only `false` means
628
+ // "not this key"; any error is the file's, not the key's.
629
+ val opens = try {
630
+ keyOpens(file.path, key)
617
631
  } catch (e: Exception) {
618
632
  key.zeroize()
619
633
  throw e
620
634
  }
635
+ if (!opens) {
636
+ key.zeroize()
637
+ continue
638
+ }
621
639
  KeyId.write(id, file)
622
640
  return id to key
623
641
  }