VaultCrypto.kt

5.0 KB · raw

1package com.peachpasswords.vault23import org.bouncycastle.crypto.generators.Argon2BytesGenerator4import org.bouncycastle.crypto.params.Argon2Parameters5import java.security.SecureRandom6import javax.crypto.Cipher7import javax.crypto.spec.GCMParameterSpec8import javax.crypto.spec.SecretKeySpec910object VaultCrypto {11    const val ARGON2_MEMORY_KB = 6553612    const val ARGON2_ITERATIONS = 313    const val ARGON2_PARALLELISM = 414    const val ARGON2_HASH_LENGTH = 3215    const val SALT_SIZE = 1616    const val NONCE_SIZE = 1217    const val TAG_LENGTH_BITS = 12818    const val KEY_SIZE = 3219    const val PAD_BLOCK = 102420    const val DURESS_SLOT_SIZE = 262_1442122    val PADDING_BUCKETS = intArrayOf(1024, 4096, 16384, 65536, 262144)2324    const val AAD_SLOT_A = "slot_a_v1"25    const val AAD_SLOT_B = "slot_b_v1"26    const val AAD_ROOT_KEY = "pw_root_v1"2728    /**29     * Distinct from [AAD_SLOT_A] so a recovery-escrow blob can never be swapped in30     * as a slot-A blob (GCM auth covers the AAD). Used by Android's whole-payload31     * offline-restore escrow.32     */33    const val AAD_RECOVERY = "recovery_escrow_v1"3435    /**36     * AAD for the browser-byte-compatible root-key escrow. The browser wraps the37     * 32-byte root key under AAD = the single byte 0x01 (`vault.ts:298`); matching38     * it byte-for-byte is what lets the same 12 words unwrap the same root key on39     * either platform. Do NOT change — it is a cross-platform wire constant.40     */41    val AAD_RECOVERY_ROOTKEY = byteArrayOf(1)4243    private val secureRandom = SecureRandom()44    private var _lastTimestamp = 0L4546    fun deriveKey(password: String, salt: ByteArray): ByteArray {47        val params = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)48            .withSalt(salt)49            .withParallelism(ARGON2_PARALLELISM)50            .withMemoryAsKB(ARGON2_MEMORY_KB)51            .withIterations(ARGON2_ITERATIONS)52            .build()53        val generator = Argon2BytesGenerator().apply { init(params) }54        val hash = ByteArray(ARGON2_HASH_LENGTH)55        // Hash explicit UTF-8 bytes, matching the browser's `TextEncoder().encode(password)`56        // (MF-4). BouncyCastle's char[] overload encodes per-UTF-16 unit, which can diverge57        // from UTF-8 for non-BMP characters (emoji, surrogate pairs) and break cross-platform58        // unlock. Zero the transient password bytes immediately after use.59        val passwordBytes = password.toByteArray(Charsets.UTF_8)60        try {61            generator.generateBytes(passwordBytes, hash)62        } finally {63            passwordBytes.fill(0)64        }65        return hash66    }6768    fun encrypt(plaintext: ByteArray, key: ByteArray, aad: String): ByteArray =69        encrypt(plaintext, key, aad.toByteArray(Charsets.UTF_8))7071    fun decrypt(data: ByteArray, key: ByteArray, aad: String): ByteArray =72        decrypt(data, key, aad.toByteArray(Charsets.UTF_8))7374    /**75     * Raw-AAD encrypt. Output is `nonce(12) || ciphertext || tag(16)`. The76     * [aad] bytes are authenticated, not encrypted. Used for the root-key77     * escrow whose AAD is the browser's single 0x01 byte ([AAD_RECOVERY_ROOTKEY]).78     */79    fun encrypt(plaintext: ByteArray, key: ByteArray, aad: ByteArray): ByteArray {80        val nonce = ByteArray(NONCE_SIZE).also(secureRandom::nextBytes)81        val cipher = Cipher.getInstance("AES/GCM/NoPadding")82        cipher.init(83            Cipher.ENCRYPT_MODE,84            SecretKeySpec(key, "AES"),85            GCMParameterSpec(TAG_LENGTH_BITS, nonce),86        )87        cipher.updateAAD(aad)88        return nonce + cipher.doFinal(plaintext)89    }9091    fun decrypt(data: ByteArray, key: ByteArray, aad: ByteArray): ByteArray {92        require(data.size >= NONCE_SIZE) { "Data too short" }93        val nonce = data.copyOfRange(0, NONCE_SIZE)94        val ciphertext = data.copyOfRange(NONCE_SIZE, data.size)95        val cipher = Cipher.getInstance("AES/GCM/NoPadding")96        cipher.init(97            Cipher.DECRYPT_MODE,98            SecretKeySpec(key, "AES"),99            GCMParameterSpec(TAG_LENGTH_BITS, nonce),100        )101        cipher.updateAAD(aad)102        return cipher.doFinal(ciphertext)103    }104105    fun generateSalt(): ByteArray = ByteArray(SALT_SIZE).also(secureRandom::nextBytes)106    fun generateRootKey(): ByteArray = ByteArray(KEY_SIZE).also(secureRandom::nextBytes)107    fun generateNoise(): ByteArray = ByteArray(DURESS_SLOT_SIZE).also(secureRandom::nextBytes)108109    fun padToBucket(data: ByteArray): ByteArray {110        val target = PADDING_BUCKETS.firstOrNull { it >= data.size } ?: data.size111        return data.copyOf(target)112    }113114    fun padToBlock(data: ByteArray): ByteArray {115        val remainder = data.size % PAD_BLOCK116        if (remainder == 0) return data117        return data.copyOf(data.size + (PAD_BLOCK - remainder))118    }119120    @Synchronized121    fun monotonicEntryTimestamp(currentModifiedAt: Long? = null): Long {122        val base = maxOf(System.currentTimeMillis(), (currentModifiedAt ?: 0L) + 1L, _lastTimestamp + 1L)123        _lastTimestamp = base124        return base125    }126127    fun zero(bytes: ByteArray) {128        bytes.fill(0)129    }130}