Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / registration / WhatsAppPqPreKey.kt
WhatsAppPqPreKey.kt63 lines · 2.9 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.registration |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto |
| 4 | import org.signal.libsignal.protocol.kem.KEMKeyPair |
| 5 | import org.signal.libsignal.protocol.kem.KEMKeyType |
| 6 | |
| 7 | /** |
| 8 | * Post-quantum (Kyber1024 / ML-KEM) last-resort prekey for the registration `e_pq_*` bundle |
| 9 | * (w2.md §2.1, Phase B 2d). |
| 10 | * |
| 11 | * Uses the bundled libsignal KEM API (`org.signal.libsignal.protocol.kem`) — already a dependency |
| 12 | * (verified: `libsignal-client` 0.86.5 ships `KEMKeyPair`/`KEMKeyType.KYBER_1024`). The signature |
| 13 | * is XEdDSA over `0x08 || pqPublicKey` using the curve25519 identity private key, mirroring the |
| 14 | * classic signed-prekey convention (`WhatsAppE2E.signSignedPreKey` signs `0x05 || skeyPub`). |
| 15 | * |
| 16 | * `KEMPublicKey.serialize()` prepends a 1-byte type tag (`0x08` for Kyber1024); the wire |
| 17 | * `e_pq_last_resort_val` is the raw 1568-byte public key, so the tag byte is stripped. |
| 18 | */ |
| 19 | object WhatsAppPqPreKey { |
| 20 | |
| 21 | /** A generated post-quantum (Kyber1024) pre-key for WhatsApp registration. */ |
| 22 | data class Generated( |
| 23 | val keyId: Int, |
| 24 | /** Raw Kyber1024 public key, 1568 bytes (no type tag). */ |
| 25 | val publicKey: ByteArray, |
| 26 | /** Serialized KEM secret key (opaque; persisted for later use). */ |
| 27 | val secretKey: ByteArray, |
| 28 | /** XEdDSA signature over `0x08 || publicKey`, 64 bytes. */ |
| 29 | val signature: ByteArray, |
| 30 | ) |
| 31 | |
| 32 | /** |
| 33 | * The bytes signed to produce `e_pq_last_resort_sig`: `0x08 || pqPublicKey`. Pure/JVM-testable |
| 34 | * (no native/libsignal calls) so the signing input framing can be asserted in unit tests. |
| 35 | */ |
| 36 | fun signingInput(pqPublicKey: ByteArray): ByteArray { |
| 37 | val out = ByteArray(1 + pqPublicKey.size) |
| 38 | out[0] = WhatsAppRegistrationConstants.KEY_TYPE_KYBER |
| 39 | System.arraycopy(pqPublicKey, 0, out, 1, pqPublicKey.size) |
| 40 | return out |
| 41 | } |
| 42 | |
| 43 | /** Strip the leading libsignal type tag from a serialized [KEMPublicKey] blob. */ |
| 44 | private fun stripTypeTag(serialized: ByteArray): ByteArray = |
| 45 | if (serialized.size == WhatsAppRegistrationConstants.PQ_KYBER1024_PUBLIC_LEN) { |
| 46 | serialized |
| 47 | } else { |
| 48 | serialized.copyOfRange(1, serialized.size) |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Generate a Kyber1024 last-resort prekey and sign it with the curve25519 [identityPrivate32]. |
| 53 | * @throws RuntimeException if native signing fails. |
| 54 | */ |
| 55 | fun generate(identityPrivate32: ByteArray, keyId: Int): Generated { |
| 56 | val kp = KEMKeyPair.generate(KEMKeyType.KYBER_1024) |
| 57 | val pub = stripTypeTag(kp.publicKey.serialize()) |
| 58 | val secret = kp.secretKey.serialize() |
| 59 | val signature = RustWhatsAppCrypto.sign(identityPrivate32, signingInput(pub)) |
| 60 | ?: throw IllegalStateException("Rust sign returned null for PQ prekey") |
| 61 | return Generated(keyId = keyId, publicKey = pub, secretKey = secret, signature = signature) |
| 62 | } |
| 63 | } |