Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / registration / SignalPqPreKey.kt
SignalPqPreKey.kt63 lines · 3.2 KiBRaw
| 1 | package com.vayunmathur.communicate.data.signal.registration |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.signal.e2e.RustSignalCrypto |
| 4 | import org.signal.libsignal.protocol.ecc.ECPrivateKey |
| 5 | import org.signal.libsignal.protocol.kem.KEMKeyPair |
| 6 | import org.signal.libsignal.protocol.kem.KEMKeyType |
| 7 | |
| 8 | /** |
| 9 | * Post-quantum (Kyber1024 / ML-KEM 1024) last-resort prekey for Signal registration. |
| 10 | * |
| 11 | * Wire correctness (from libsignal/rust/protocol/src/kem.rs + state/kyber_prekey.rs): |
| 12 | * - KEMKeyPair.generate(KYBER_1024) -> publicKey.serialize() is 0x08 || 1568 bytes = 1569B |
| 13 | * (includes 0x08 KeyType byte; see kem::KeyType::Kyber1024 = 0x08) |
| 14 | * - Signature is XEdDSA over the FULL serialized public key INCLUDING the 0x08 type byte: |
| 15 | * identityPrivate.calculateSignature(pub.serialize()) |
| 16 | * (PreKeyUtil.java:157, libsignal KyberPreKeyRecord::generate) |
| 17 | * - Server validates signature over that exact 1569B buffer; stripping the tag breaks verification. |
| 18 | * - Secret key serialize() is 0x08 || 3168B = 3169B? Actually 3168 raw + 1 tag = 3169. |
| 19 | * Stored as Base64 for upload; the raw 1569B public is what goes in KyberPreKeyEntity. |
| 20 | */ |
| 21 | object SignalPqPreKey { |
| 22 | |
| 23 | private const val KYBER1024_PUB_SIZE = 1569 |
| 24 | |
| 25 | /** A generated post-quantum (Kyber1024) pre-key for Signal registration. */ |
| 26 | data class Generated( |
| 27 | val keyId: Int, |
| 28 | val publicKey: ByteArray, // 1569B = 0x08 || 1568 raw Kyber1024 pub (tag INTACT for wire) |
| 29 | val secretKey: ByteArray, // KEMSecretKey.serialize() (includes 0x08 tag) |
| 30 | val signature: ByteArray, // XEdDSA signature over publicKey (full 1569B) |
| 31 | ) |
| 32 | |
| 33 | /** |
| 34 | * Signing input is the FULL serialized public key (1569B including 0x08 tag). |
| 35 | * Do NOT strip the tag - libsignal signs pub.serialize() verbatim. |
| 36 | */ |
| 37 | fun signingInput(serializedPublicKey1569: ByteArray): ByteArray = serializedPublicKey1569 |
| 38 | |
| 39 | fun generate(identityPrivate32: ByteArray, keyId: Int): Generated { |
| 40 | val kp = KEMKeyPair.generate(KEMKeyType.KYBER_1024) |
| 41 | // KEEP the 0x08 tag - serialize() is already 0x08 || 1568 = 1569B |
| 42 | val pub = kp.publicKey.serialize() // 1569B wire-correct |
| 43 | require(pub.size == KYBER1024_PUB_SIZE) { "Kyber1024 pub serialize must be 1569B, got ${pub.size}" } |
| 44 | val secret = kp.secretKey.serialize() |
| 45 | // Real libsignal: identityPrivate.calculateSignature(pub.serialize()) |
| 46 | // Use ECPrivateKey bridge for XEdDSA (same curve as identity) |
| 47 | val identityEcPriv = ECPrivateKey(identityPrivate32) |
| 48 | val signature = identityEcPriv.calculateSignature(signingInput(pub)) |
| 49 | return Generated(keyId = keyId, publicKey = pub, secretKey = secret, signature = signature) |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Variant that takes an ECPrivateKey directly (avoids re-wrapping). |
| 54 | */ |
| 55 | fun generateWithECPrivate(identityPrivate: ECPrivateKey, keyId: Int): Generated { |
| 56 | val kp = KEMKeyPair.generate(KEMKeyType.KYBER_1024) |
| 57 | val pub = kp.publicKey.serialize() |
| 58 | require(pub.size == KYBER1024_PUB_SIZE) { "Kyber1024 pub serialize must be 1569B, got ${pub.size}" } |
| 59 | val secret = kp.secretKey.serialize() |
| 60 | val signature = identityPrivate.calculateSignature(pub) |
| 61 | return Generated(keyId = keyId, publicKey = pub, secretKey = secret, signature = signature) |
| 62 | } |
| 63 | } |