package com.vayunmathur.communicate.data.whatsapp.call import java.nio.ByteBuffer import java.security.SecureRandom import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec /** * WhatsApp call-key crypto (Phase D 3b, whatsapp-documentation.md:373 `CryptoCallback`). * * The 32-byte **call key** is generated by the caller and fanned out per-device via the same Signal * `` path as messages (see [WhatsAppCallSignaling] / `WhatsAppClient.encryptForDevices`). From * it, both sides derive symmetric **media keys** via HKDF-SHA256 + HMAC-SHA256 and a **secure SSRC**, * matching the `CryptoCallback` contract, and support `call_rekey` (a new call key derived from the * previous one for a rekey message id). * * All derivations are pure (java.security only) and deterministic, so the vectors are unit-testable. * * NB: our client-to-client media path uses WebRTC's own DTLS-SRTP; these keys model WhatsApp's * native SRTP key schedule and are exchanged/verified alongside the SDP so the derivation is * exercised and asserted, even though byte-level interop with WhatsApp's native relay isn't possible. */ object WhatsAppCallCrypto { const val CALL_KEY_LEN = 32 private const val SRTP_KEY_LEN = 16 // AES-128 private const val SRTP_SALT_LEN = 14 // SRTP session salt private const val AUTH_KEY_LEN = 32 // HMAC-SHA256 auth key private const val INFO_MEDIA = "WhatsApp Call Media Keys" private const val INFO_REKEY = "WhatsApp Call Rekey" private const val INFO_SSRC = "WhatsApp Call SSRC" private const val INT_POSITIVE_MASK = 0x7FFFFFFF private val rng = SecureRandom() /** Derived symmetric media material for one direction of a call. */ data class MediaKeys( val encKey: ByteArray, // 16B SRTP AES-128 key val salt: ByteArray, // 14B SRTP session salt val authKey: ByteArray, // 32B HMAC-SHA256 auth key val ssrc: Int, // secure (non-negative) synchronization source id ) /** Generate a fresh 32-byte call key. */ fun generateCallKey(): ByteArray = ByteArray(CALL_KEY_LEN).also { rng.nextBytes(it) } /** * Derive the media keys + secure SSRC for [callId] from [callKey]. Deterministic: the same * (callKey, callId) always yields the same keys, so both peers agree. */ fun deriveMediaKeys(callKey: ByteArray, callId: String): MediaKeys { val salt = callId.toByteArray(Charsets.UTF_8) val out = hkdfSha256(callKey, salt, INFO_MEDIA.toByteArray(Charsets.UTF_8), SRTP_KEY_LEN + SRTP_SALT_LEN) val encKey = out.copyOfRange(0, SRTP_KEY_LEN) val srtpSalt = out.copyOfRange(SRTP_KEY_LEN, SRTP_KEY_LEN + SRTP_SALT_LEN) val authKey = hmacSha256(callKey, encKey + srtpSalt).copyOf(AUTH_KEY_LEN) val ssrc = deriveSsrc(callKey, callId) return MediaKeys(encKey, srtpSalt, authKey, ssrc) } /** Secure SSRC: top 31 bits of HMAC-SHA256(callKey, "…SSRC"||callId), always non-negative. */ fun deriveSsrc(callKey: ByteArray, callId: String): Int { val mac = hmacSha256(callKey, (INFO_SSRC + callId).toByteArray(Charsets.UTF_8)) val raw = ByteBuffer.wrap(mac, 0, 4).int return raw and INT_POSITIVE_MASK } /** * Derive a new call key for a `call_rekey` identified by [rekeyId], chained from the previous * [prevCallKey]. Distinct from the original key so a compromised prior key can't decrypt after * rekey. Ref whatsapp-documentation.md `call_rekey`. */ fun rekey(prevCallKey: ByteArray, rekeyId: String): ByteArray = hkdfSha256( prevCallKey, rekeyId.toByteArray(Charsets.UTF_8), INFO_REKEY.toByteArray(Charsets.UTF_8), CALL_KEY_LEN) // -- primitives -- fun hmacSha256(key: ByteArray, data: ByteArray): ByteArray { val mac = Mac.getInstance("HmacSHA256") mac.init(SecretKeySpec(key, "HmacSHA256")) return mac.doFinal(data) } /** HKDF-SHA256 (RFC 5869). null/empty salt = 32 zero bytes. */ fun hkdfSha256(ikm: ByteArray, salt: ByteArray?, info: ByteArray, length: Int): ByteArray { val actualSalt = if (salt == null || salt.isEmpty()) ByteArray(32) else salt val prk = Mac.getInstance("HmacSHA256").apply { init(SecretKeySpec(actualSalt, "HmacSHA256")) }.doFinal(ikm) val out = ByteArray(length) var t = ByteArray(0) var pos = 0 var counter = 1 while (pos < length) { val mac = Mac.getInstance("HmacSHA256").apply { init(SecretKeySpec(prk, "HmacSHA256")) } mac.update(t); mac.update(info); mac.update(counter.toByte()) t = mac.doFinal() val n = minOf(t.size, length - pos) System.arraycopy(t, 0, out, pos, n) pos += n; counter++ } return out } }