Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / registration / SignalRegistrationKeys.kt

SignalRegistrationKeys.kt158 lines · 8.1 KiBRaw
1package com.vayunmathur.communicate.data.signal.registration
2
3import android.util.Base64
4import com.vayunmathur.communicate.data.signal.SignalAuthData
5import java.security.SecureRandom
6import org.signal.libsignal.protocol.IdentityKeyPair
7import org.signal.libsignal.protocol.ecc.ECKeyPair
8
9/**
10 * Generates the client key material for a fresh Signal registration.
11 *
12 * Real Signal (PushServiceSocket REGISTRATION_PATH, RegistrationSessionRequestBody,
13 * AccountAttributes, PreKeyUtil.java:157, libsignal state/kyber_prekey.rs):
14 * - Two IdentityKeyPairs (ACI + PNI) via IdentityKeyPair.generate()
15 * - Two registrationIds 1..16383 (KeyHelper.generateRegistrationId)
16 * - Two signed EC prekeys (ECKeyPair.generate() + identityPriv.calculateSignature(pub.serialize()))
17 * - Two Kyber-1024 last-resort prekeys (KEMKeyPair KYBER_1024, pub.serialize() 1569B = 0x08||1568, sig over full 1569B)
18 * - Account password (random for Basic e164:password) and UAK 32B
19 *
20 * Serialization contract (coordinate with crypto teammate who owns SignalPqPreKey):
21 * - aci/pniIdentityKey wire = Base64-nopad(IdentityKey.serialize()) 33B (0x05||32) via
22 * RegistrationSessionRequestBody.aciIdentityKey
23 * - SignedPreKeyEntity wire = {keyId, publicKey: Base64-nopad(ECPublicKey.serialize() 33B), signature}
24 * - KyberPreKeyEntity wire = {keyId, publicKey: Base64-nopad(KEMPublicKey.serialize() 1569B with 0x08 tag), signature}
25 * Signature for Kyber is over the FULL 1569B serialize (including 0x08) — stripping tag breaks server verification.
26 * Secret key (3168B raw + tag) is persisted locally only, never uploaded.
27 */
28class SignalRegistrationKeys private constructor(val authScaffold: SignalAuthData) {
29 /** Registration-id bounds and factories for [SignalRegistrationKeys]. */
30 companion object {
31 private const val MAX_REGISTRATION_ID = 0x3FFF
32
33 fun generate(phoneNumber: String): SignalRegistrationKeys {
34 val rng = SecureRandom()
35 fun regId() = rng.nextInt(MAX_REGISTRATION_ID) + 1
36
37 val aciIdentity = IdentityKeyPair.generate()
38 val pniIdentity = IdentityKeyPair.generate()
39 val aciRegId = regId()
40 val pniRegId = regId()
41
42 val aciSignedKp = ECKeyPair.generate()
43 val pniSignedKp = ECKeyPair.generate()
44 val aciSignedId = 1
45 val pniSignedId = 1
46 // libsignal: identityPriv.calculateSignature(pub.serialize()) — EC pub serialize is 33B (0x05||32)
47 val aciSignedSig = aciIdentity.privateKey.calculateSignature(aciSignedKp.publicKey.serialize())
48 val pniSignedSig = pniIdentity.privateKey.calculateSignature(pniSignedKp.publicKey.serialize())
49
50 // Kyber-1024 last-resort — use SignalPqPreKey which keeps 0x08 tag intact (coordinate with crypto)
51 val aciPq = SignalPqPreKey.generateWithECPrivate(aciIdentity.privateKey, 1)
52 val pniPq = SignalPqPreKey.generateWithECPrivate(pniIdentity.privateKey, 1)
53
54 // Account password for Basic e164:password (POST /v1/registration + WS Authorization)
55 val password = generatePassword(rng)
56 // unidentifiedAccessKey is EXACTLY 16 bytes (UnidentifiedAccess.deriveAccessKeyFrom trims to 16);
57 // the server enforces a 16-byte length, so a 32-byte value is rejected with HTTP 422.
58 val uak = ByteArray(16).also { rng.nextBytes(it) }
59
60 val scaffold = buildAuthScaffold(
61 phoneNumber, aciIdentity, pniIdentity, aciRegId, pniRegId,
62 aciSignedKp, pniSignedKp, aciSignedId, pniSignedId, aciSignedSig, pniSignedSig,
63 aciPq, pniPq, password, uak,
64 )
65 return SignalRegistrationKeys(scaffold)
66 }
67
68 /** Build the auth scaffold from generated key material. */
69 @Suppress("LongParameterList")
70 private fun buildAuthScaffold(
71 phoneNumber: String,
72 aciIdentity: IdentityKeyPair,
73 pniIdentity: IdentityKeyPair,
74 aciRegId: Int,
75 pniRegId: Int,
76 aciSignedKp: ECKeyPair,
77 pniSignedKp: ECKeyPair,
78 aciSignedId: Int,
79 pniSignedId: Int,
80 aciSignedSig: ByteArray,
81 pniSignedSig: ByteArray,
82 aciPq: SignalPqPreKey.Generated,
83 pniPq: SignalPqPreKey.Generated,
84 password: String,
85 uak: ByteArray,
86 ): SignalAuthData {
87 // Identity serialization: IdentityKey.serialize() = 33B (0x05||32), private = 32B raw EC
88 val aciPubB64 = b64(aciIdentity.publicKey.serialize())
89 val pniPubB64 = b64(pniIdentity.publicKey.serialize())
90 // Private key bytes for storage — ECPrivateKey.serialize() is 32B
91 val aciPrivB64 = b64(aciIdentity.privateKey.serialize())
92 val pniPrivB64 = b64(pniIdentity.privateKey.serialize())
93
94 return SignalAuthData(
95 phoneNumber = phoneNumber,
96 aci = "",
97 pni = "",
98 deviceId = 1,
99 // Legacy single-identity mirror (ACI) for SignalE2E/RustSignalCrypto compat
100 identityPrivateKey = aciPrivB64,
101 identityPublicKey = aciPubB64,
102 registrationId = aciRegId,
103 signedPreKeyId = aciSignedId,
104 signedPreKeyPublic = b64(aciSignedKp.publicKey.serialize()),
105 signedPreKeyPrivate = b64(aciSignedKp.privateKey.serialize()),
106 signedPreKeySignature = b64(aciSignedSig),
107 pqLastResortKeyId = aciPq.keyId,
108 pqLastResortPublic = b64(aciPq.publicKey),
109 pqLastResortSecret = b64(aciPq.secretKey),
110 pqLastResortSignature = b64(aciPq.signature),
111 kyberPreKeyId = aciPq.keyId,
112 kyberPreKeyPublic = b64(aciPq.publicKey),
113 kyberPreKeySecret = b64(aciPq.secretKey),
114 kyberPreKeySignature = b64(aciPq.signature),
115 // Dual-identity (preferred)
116 aciIdentityPrivateKey = aciPrivB64,
117 aciIdentityPublicKey = aciPubB64,
118 pniIdentityPrivateKey = pniPrivB64,
119 pniIdentityPublicKey = pniPubB64,
120 aciRegistrationId = aciRegId,
121 pniRegistrationId = pniRegId,
122 aciSignedPreKeyId = aciSignedId,
123 aciSignedPreKeyPublic = b64(aciSignedKp.publicKey.serialize()),
124 aciSignedPreKeyPrivate = b64(aciSignedKp.privateKey.serialize()),
125 aciSignedPreKeySignature = b64(aciSignedSig),
126 pniSignedPreKeyId = pniSignedId,
127 pniSignedPreKeyPublic = b64(pniSignedKp.publicKey.serialize()),
128 pniSignedPreKeyPrivate = b64(pniSignedKp.privateKey.serialize()),
129 pniSignedPreKeySignature = b64(pniSignedSig),
130 aciPqLastResortKeyId = aciPq.keyId,
131 aciPqLastResortPublic = b64(aciPq.publicKey),
132 aciPqLastResortSecret = b64(aciPq.secretKey),
133 aciPqLastResortSignature = b64(aciPq.signature),
134 pniPqLastResortKeyId = pniPq.keyId,
135 pniPqLastResortPublic = b64(pniPq.publicKey),
136 pniPqLastResortSecret = b64(pniPq.secretKey),
137 pniPqLastResortSignature = b64(pniPq.signature),
138 password = password,
139 unidentifiedAccessKey = b64(uak),
140 // live-only SVR2 derivation (MasterKey.deriveRegistrationLock);
141 // keep wire-correct null for offline
142 registrationLock = null,
143 verificationSessionId = null,
144 registered = false,
145 )
146 }
147
148 private fun generatePassword(rng: SecureRandom): String {
149 // Signal password is a random client secret for Basic auth; 16 bytes base64 is sufficient and
150 // server-agnostic.
151 val b = ByteArray(16).also { rng.nextBytes(it) }
152 // Use URL-safe-ish alphanumeric: base64 without padding, strip '=' — server accepts any non-empty string.
153 return Base64.encodeToString(b, Base64.NO_WRAP or Base64.NO_PADDING)
154 }
155
156 private fun b64(b: ByteArray): String = Base64.encodeToString(b, Base64.NO_WRAP)
157 }
158}