VaultCrypto.kt

3.4 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    private val secureRandom = SecureRandom()29    private var _lastTimestamp = 0L3031    fun deriveKey(password: String, salt: ByteArray): ByteArray {32        val params = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)33            .withSalt(salt)34            .withParallelism(ARGON2_PARALLELISM)35            .withMemoryAsKB(ARGON2_MEMORY_KB)36            .withIterations(ARGON2_ITERATIONS)37            .build()38        val generator = Argon2BytesGenerator().apply { init(params) }39        val hash = ByteArray(ARGON2_HASH_LENGTH)40        generator.generateBytes(password.toCharArray(), hash)41        return hash42    }4344    fun encrypt(plaintext: ByteArray, key: ByteArray, aad: String): ByteArray {45        val nonce = ByteArray(NONCE_SIZE).also(secureRandom::nextBytes)46        val cipher = Cipher.getInstance("AES/GCM/NoPadding")47        cipher.init(48            Cipher.ENCRYPT_MODE,49            SecretKeySpec(key, "AES"),50            GCMParameterSpec(TAG_LENGTH_BITS, nonce),51        )52        cipher.updateAAD(aad.toByteArray(Charsets.UTF_8))53        return nonce + cipher.doFinal(plaintext)54    }5556    fun decrypt(data: ByteArray, key: ByteArray, aad: String): ByteArray {57        require(data.size >= NONCE_SIZE) { "Data too short" }58        val nonce = data.copyOfRange(0, NONCE_SIZE)59        val ciphertext = data.copyOfRange(NONCE_SIZE, data.size)60        val cipher = Cipher.getInstance("AES/GCM/NoPadding")61        cipher.init(62            Cipher.DECRYPT_MODE,63            SecretKeySpec(key, "AES"),64            GCMParameterSpec(TAG_LENGTH_BITS, nonce),65        )66        cipher.updateAAD(aad.toByteArray(Charsets.UTF_8))67        return cipher.doFinal(ciphertext)68    }6970    fun generateSalt(): ByteArray = ByteArray(SALT_SIZE).also(secureRandom::nextBytes)71    fun generateRootKey(): ByteArray = ByteArray(KEY_SIZE).also(secureRandom::nextBytes)72    fun generateNoise(): ByteArray = ByteArray(DURESS_SLOT_SIZE).also(secureRandom::nextBytes)7374    fun padToBucket(data: ByteArray): ByteArray {75        val target = PADDING_BUCKETS.firstOrNull { it >= data.size } ?: data.size76        return data.copyOf(target)77    }7879    fun padToBlock(data: ByteArray): ByteArray {80        val remainder = data.size % PAD_BLOCK81        if (remainder == 0) return data82        return data.copyOf(data.size + (PAD_BLOCK - remainder))83    }8485    @Synchronized86    fun monotonicEntryTimestamp(currentModifiedAt: Long? = null): Long {87        val base = maxOf(System.currentTimeMillis(), (currentModifiedAt ?: 0L) + 1L, _lastTimestamp + 1L)88        _lastTimestamp = base89        return base90    }9192    fun zero(bytes: ByteArray) {93        bytes.fill(0)94    }95}