Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / e2ee-p2p / src / main / java / com / vayunmathur / e2ee / SecurityCode.kt
SecurityCode.kt66 lines · 2.6 KiBRaw
| 1 | package com.vayunmathur.e2ee |
| 2 | |
| 3 | import java.security.MessageDigest |
| 4 | |
| 5 | /** |
| 6 | * Derives a human-comparable "security code" (safety number) from the two participants' public |
| 7 | * keys. Both devices compute the **same** code because the two key fingerprints are combined in a |
| 8 | * canonical (sorted) order before hashing. If the codes match on both devices, the two peers hold |
| 9 | * each other's genuine keys — i.e. no one (not the server) substituted a key to intercept the |
| 10 | * end-to-end-encrypted channel. |
| 11 | * |
| 12 | * Inputs are each side's public key material exactly as it is held and exchanged — [Pqc.securityCode] |
| 13 | * passes the raw public bundles, with no re-encoding. It is the canonical *ordering* of the two |
| 14 | * fingerprints, not any canonical encoding, that makes both devices arrive at the same code. |
| 15 | */ |
| 16 | object SecurityCode { |
| 17 | |
| 18 | /** Iterated hashing slows brute-force search for a colliding short code. */ |
| 19 | private const val ITERATIONS = 4000 |
| 20 | |
| 21 | /** @return a 6-group, 30-digit code like "12345 67890 ...", identical on both devices. */ |
| 22 | fun compute(myKeyDer: ByteArray, theirKeyDer: ByteArray): String { |
| 23 | val a = sha256(myKeyDer) |
| 24 | val b = sha256(theirKeyDer) |
| 25 | // Canonical order so both sides (which hold the two keys in opposite roles) agree. |
| 26 | var h = if (compareLex(a, b) <= 0) a + b else b + a |
| 27 | repeat(ITERATIONS) { h = sha256(h) } |
| 28 | return format(h) |
| 29 | } |
| 30 | |
| 31 | private fun sha256(x: ByteArray): ByteArray = MessageDigest.getInstance("SHA-256").digest(x) |
| 32 | |
| 33 | private fun compareLex(a: ByteArray, b: ByteArray): Int { |
| 34 | val n = minOf(a.size, b.size) |
| 35 | for (i in 0 until n) { |
| 36 | val d = (a[i].toInt() and BYTE_MASK) - (b[i].toInt() and BYTE_MASK) |
| 37 | if (d != 0) return d |
| 38 | } |
| 39 | return a.size - b.size |
| 40 | } |
| 41 | |
| 42 | private const val BYTE_MASK = 0xFF |
| 43 | |
| 44 | private fun format(h: ByteArray): String { |
| 45 | val sb = StringBuilder() |
| 46 | var i = 0 |
| 47 | var group = 0 |
| 48 | while (group < GROUP_COUNT && i + BYTES_PER_GROUP <= h.size) { |
| 49 | var value = 0L |
| 50 | for (j in 0 until BYTES_PER_GROUP) { |
| 51 | value = (value shl BYTE_BITS) or (h[i + j].toInt() and BYTE_MASK).toLong() |
| 52 | } |
| 53 | if (group > 0) sb.append(' ') |
| 54 | sb.append((value % GROUP_MODULUS).toString().padStart(GROUP_DIGITS, '0')) |
| 55 | i += BYTES_PER_GROUP |
| 56 | group++ |
| 57 | } |
| 58 | return sb.toString() |
| 59 | } |
| 60 | |
| 61 | private const val GROUP_COUNT = 6 |
| 62 | private const val BYTES_PER_GROUP = 5 |
| 63 | private const val BYTE_BITS = 8 |
| 64 | private const val GROUP_MODULUS = 100000L |
| 65 | private const val GROUP_DIGITS = 5 |
| 66 | } |