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 2930313233 const val AAD_RECOVERY = "recovery_escrow_v1"3435 363738394041 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 56 57 58 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 7576777879 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}