Skip to content

fix(deps): support android 16kb page size + dep updates#68

Merged
mhamann merged 8 commits into
mainfrom
fix-android-16kb
Nov 18, 2025
Merged

fix(deps): support android 16kb page size + dep updates#68
mhamann merged 8 commits into
mainfrom
fix-android-16kb

Conversation

@mhamann
Copy link
Copy Markdown
Contributor

@mhamann mhamann commented Nov 18, 2025

Fixes #67

Summary by Sourcery

Replace EncryptedFile-based key persistence with custom AES-GCM encryption using AndroidKeyStore to support 16KB page-size devices and update various project dependencies

Bug Fixes:

  • Fix key storage mechanism on devices with 16KB SQLite page size

Enhancements:

  • Use AndroidKeyStore to generate and manage AES/GCM secret keys with manual IV handling
  • Encrypt and decrypt key files via CipherOutputStream/CipherInputStream and delete corrupt key files on load failures

Build:

  • Bump Dagger to 2.57.2
  • Upgrade AndroidX Datastore Preferences to 1.1.7
  • Update lazysodium-android to 5.2.0 and JNA to 5.18.1
  • Upgrade Google Play Services Auth to 21.4.0

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Nov 18, 2025

Reviewer's Guide

This PR replaces the previous EncryptedFile-based key storage with a custom AndroidKeyStore-backed AES-GCM encryption approach (storing IV alongside ciphertext) and updates several core dependencies to newer versions.

Class diagram for updated Encryption key storage logic

classDiagram
    class Encryption {
        <<object>>
        +doesKeyExist(keyId: String): Boolean
        +storeKey(key: Key, keyId: String)
        +loadKey(keyId: String): Key?
        -keyName(keyId: String?): String
        -getOrCreateSecretKey(): SecretKey
        -keyStore: KeyStore
        -ANDROID_KEYSTORE: String
        -KEY_ALIAS: String
        -ENCRYPTION_ALGORITHM: String
        -ENCRYPTION_BLOCK_MODE: String
        -ENCRYPTION_PADDING: String
        -TRANSFORMATION_STRING: String
        -GCM_IV_LENGTH: Int
        -AES_KEY_SIZE: Int
    }
    Encryption --> KeyStore
    Encryption --> SecretKey
    Encryption --> Key
    Encryption --> Cipher
    Encryption --> File
    Encryption --> CipherInputStream
    Encryption --> CipherOutputStream
Loading

Flow diagram for new key storage and retrieval process

flowchart TD
    A["storeKey(key, keyId)"] --> B["Get or create SecretKey from AndroidKeyStore"]
    B --> C["Initialize AES-GCM Cipher (ENCRYPT_MODE)"]
    C --> D["Write IV to start of file"]
    D --> E["Encrypt key bytes and write to file using CipherOutputStream"]

    F["loadKey(keyId)"] --> G["Read IV from start of file"]
    G --> H["Get or create SecretKey from AndroidKeyStore"]
    H --> I["Initialize AES-GCM Cipher (DECRYPT_MODE) with IV"]
    I --> J["Decrypt key bytes using CipherInputStream"]
    J --> K["Return Key object"]
Loading

File-Level Changes

Change Details Files
Replace EncryptedFile-based key storage with AndroidKeyStore-backed AES-GCM encryption
  • Define keystore constants and key alias for AndroidKeyStore
  • Implement getOrCreateSecretKey with KeyGenParameterSpec and KeyGenerator
  • Encrypt keys via CipherOutputStream writing IV then ciphertext
  • Decrypt keys via CipherInputStream reading IV, apply GCMParameterSpec
  • Remove androidx.security.crypto EncryptedFile/MasterKeys usage
  • Improve error logging and delete corrupt key files on failure
android/src/main/java/io/rownd/android/util/Encryption.kt
Update project dependencies to latest stable versions
  • Bump Dagger from 2.56 to 2.57.2
  • Upgrade Datastore Preferences from 1.1.3 to 1.1.7
  • Update LazySodium to 5.2.0 and JNA to 5.18.1
  • Increment Play Services Auth from 21.3.0 to 21.4.0
android/build.gradle

Assessment against linked issues

Issue Objective Addressed Explanation
#67 Reduce or eliminate transitive dependencies that prevent compliance with the 16kb page size requirement for Google Play.
#67 Update the dependencies 'net.java.dev.jna:jna' and 'com.goterl:lazysodium-android' to versions that support the 16kb page size requirement.

Possibly linked issues

  • 16kb page size #67: The PR updates lazysodium-android and jna dependencies and refactors encryption to support Android 16kb page size requirement.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `android/src/main/java/io/rownd/android/util/Encryption.kt:103` </location>
<code_context>
+            FileInputStream(file).use { fileIn ->
+                // Read the IV from the start of the file
+                val iv = ByteArray(GCM_IV_LENGTH)
+                fileIn.read(iv)
+
+                val secretKey = getOrCreateSecretKey()
</code_context>

<issue_to_address>
**issue (bug_risk):** No check for incomplete IV read from file.

If fileIn.read(iv) returns fewer bytes than GCM_IV_LENGTH, cryptographic operations may fail. Please verify the number of bytes read and handle incomplete reads appropriately.
</issue_to_address>

### Comment 2
<location> `android/src/main/java/io/rownd/android/util/Encryption.kt:119-120` </location>
<code_context>
             return null
         } catch (error: Exception) {
-            Log.e("Rownd", "Failed to load encryption key: ${error.message}")
+            Log.e("Rownd", "Failed to load encryption key: ${error.message}", error)
+            // It's possible the key is corrupt or something changed, delete it
+            file.delete()
             return null
</code_context>

<issue_to_address>
**issue (bug_risk):** Automatically deleting key file on any exception may be risky.

Restrict key file deletion to cases where corruption or decryption failure is confirmed, rather than on all exceptions.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread android/src/main/java/io/rownd/android/util/Encryption.kt Outdated
Comment thread android/src/main/java/io/rownd/android/util/Encryption.kt Outdated
@mhamann mhamann merged commit 6a7cef9 into main Nov 18, 2025
7 checks passed
@mhamann mhamann deleted the fix-android-16kb branch November 18, 2025 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

16kb page size

2 participants