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

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / WhatsAppCallCryptoTest.kt

WhatsAppCallCryptoTest.kt58 lines · 2.0 KiBRaw
1package com.vayunmathur.communicate.whatsapp
2
3import com.vayunmathur.communicate.data.whatsapp.call.WhatsAppCallCrypto
4import kotlin.test.Test
5import kotlin.test.assertEquals
6import kotlin.test.assertFalse
7import kotlin.test.assertTrue
8
9/**
10 * Derivation vectors for the call-key crypto (Phase D 3b). Pure (java.security only), no Android.
11 */
12class WhatsAppCallCryptoTest {
13
14 private val callKey = ByteArray(32) { (it + 1).toByte() }
15
16 @Test
17 fun callKeyIs32Bytes() {
18 assertEquals(32, WhatsAppCallCrypto.generateCallKey().size)
19 }
20
21 @Test
22 fun mediaKeys_areDeterministicWithCorrectLengths() {
23 val a = WhatsAppCallCrypto.deriveMediaKeys(callKey, "call-123")
24 val b = WhatsAppCallCrypto.deriveMediaKeys(callKey, "call-123")
25 assertEquals(16, a.encKey.size)
26 assertEquals(14, a.salt.size)
27 assertEquals(32, a.authKey.size)
28 assertTrue(a.encKey.contentEquals(b.encKey))
29 assertTrue(a.salt.contentEquals(b.salt))
30 assertTrue(a.authKey.contentEquals(b.authKey))
31 assertEquals(a.ssrc, b.ssrc)
32 }
33
34 @Test
35 fun differentCallId_yieldsDifferentKeys() {
36 val a = WhatsAppCallCrypto.deriveMediaKeys(callKey, "call-A")
37 val b = WhatsAppCallCrypto.deriveMediaKeys(callKey, "call-B")
38 assertFalse(a.encKey.contentEquals(b.encKey))
39 assertTrue(a.ssrc != b.ssrc || !a.salt.contentEquals(b.salt))
40 }
41
42 @Test
43 fun ssrc_isNonNegativeAndDeterministic() {
44 val s1 = WhatsAppCallCrypto.deriveSsrc(callKey, "call-1")
45 val s2 = WhatsAppCallCrypto.deriveSsrc(callKey, "call-1")
46 assertEquals(s1, s2)
47 assertTrue(s1 >= 0)
48 }
49
50 @Test
51 fun rekey_differsFromOriginalAndIs32Bytes() {
52 val rk = WhatsAppCallCrypto.rekey(callKey, "rekey-7")
53 assertEquals(32, rk.size)
54 assertFalse(rk.contentEquals(callKey))
55 // Deterministic for the same rekey id.
56 assertTrue(rk.contentEquals(WhatsAppCallCrypto.rekey(callKey, "rekey-7")))
57 }
58}