1package com.peachpasswords.vault23import org.bouncycastle.crypto.generators.HKDFBytesGenerator4import org.bouncycastle.crypto.digests.SHA256Digest5import org.bouncycastle.crypto.params.HKDFParameters6import java.math.BigInteger7import java.security.MessageDigest8import javax.crypto.spec.SecretKeySpec910private const val WORD_BITS = 1111private const val TOTAL_WORDS = 121213fun rootKeyToWords(rootKeyBytes: ByteArray): List<String> {14 require(rootKeyBytes.size == 32) { "root key must be 32 bytes" }15 val entropy16 = rootKeyBytes.copyOfRange(0, 16)16 val entropyHash = MessageDigest.getInstance("SHA-256").digest(entropy16)17 val checksum = entropyHash[0].toInt() ushr 41819 var bitsBig = BigInteger.ZERO20 for (i in 0 until 16) {21 bitsBig = bitsBig.shiftLeft(8).or(BigInteger.valueOf((rootKeyBytes[i].toInt() and 0xff).toLong()))22 }23 bitsBig = bitsBig.shiftLeft(4).or(BigInteger.valueOf(checksum.toLong()))2425 val words = ArrayList<String>(TOTAL_WORDS)26 for (i in 0 until TOTAL_WORDS) {27 val shift = (TOTAL_WORDS - 1 - i) * WORD_BITS28 val index = bitsBig.shiftRight(shift).and(BigInteger.valueOf(0x7ffL)).toInt()29 words += WORD_LIST[index]30 }31 return words32}3334fun wordsToRootKeyMaterial(words: List<String>): ByteArray {35 require(words.size == TOTAL_WORDS) { "exactly $TOTAL_WORDS words required" }3637 val indices = IntArray(words.size)38 for (i in words.indices) {39 val word = words[i].trim().lowercase()40 val index = WORD_LIST.indexOf(word)41 require(index >= 0) { "unknown word at position ${i + 1}: \"${words[i]}\"" }42 indices[i] = index43 }4445 var bitsBig = BigInteger.ZERO46 for (index in indices) {47 bitsBig = bitsBig.shiftLeft(WORD_BITS).or(BigInteger.valueOf(index.toLong()))48 }4950 val entropy = ByteArray(16)51 for (i in 0 until 16) {52 val byteShift = 124 - i * 853 entropy[i] = bitsBig.shiftRight(byteShift).and(BigInteger.valueOf(0xffL)).toByte()54 }5556 val storedChecksum = bitsBig.and(BigInteger.valueOf(0xFL)).toInt()57 val entropyHash = MessageDigest.getInstance("SHA-256").digest(entropy)58 val computedChecksum = entropyHash[0].toInt() ushr 459 require(storedChecksum == computedChecksum) { "invalid recovery words — checksum mismatch" }6061 return ByteArray(32).also { it.fill(0); System.arraycopy(entropy, 0, it, 0, entropy.size) }62}6364fun deriveRecoveryKey(wordMaterial: ByteArray, salt: ByteArray): SecretKeySpec {65 val generator = HKDFBytesGenerator(SHA256Digest())66 generator.init(HKDFParameters(wordMaterial, salt, "peach-recovery-v1".encodeToByteArray()))67 val out = ByteArray(32)68 generator.generateBytes(out, 0, out.size)69 return SecretKeySpec(out, "AES")70}7172fun findInvalidWords(words: List<String>): List<Pair<Int, String>> {73 val invalid = ArrayList<Pair<Int, String>>()74 for (i in words.indices) {75 val w = words[i].trim().lowercase()76 if (w.isBlank() || WORD_LIST.indexOf(w) == -1) {77 invalid += i to words[i]78 }79 }80 return invalid81}8283fun getWordSuggestions(prefix: String, maxResults: Int = 5): List<String> {84 val p = prefix.trim().lowercase()85 if (p.length < 2) return emptyList()86 return WORD_LIST.filter { it.startsWith(p) }.take(maxResults)87}88899091929394fun recoveryWordsHash(words: List<String>): String {95 val canonical = words.joinToString(" ") { it.trim().lowercase() }.encodeToByteArray()96 val digest = org.bouncycastle.crypto.digests.Blake3Digest(256)97 digest.update(canonical, 0, canonical.size)98 val out = ByteArray(32)99 digest.doFinal(out, 0)100 return out.joinToString("") { "%02x".format(it) }101}