Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1ProtocolCryptoTest.kt
W5Slice1ProtocolCryptoTest.kt190 lines · 7.7 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 4 | import com.vayunmathur.communicate.data.whatsapp.buildPollVoteMessage |
| 5 | import com.vayunmathur.communicate.data.whatsapp.decryptAppStateValue |
| 6 | import com.vayunmathur.communicate.data.whatsapp.decryptMedia |
| 7 | import com.vayunmathur.communicate.data.whatsapp.decryptPollVote |
| 8 | import com.vayunmathur.communicate.data.whatsapp.encryptMedia |
| 9 | import com.vayunmathur.communicate.data.whatsapp.expandAppStateKeys |
| 10 | import com.vayunmathur.communicate.data.whatsapp.generateMessageId |
| 11 | import com.vayunmathur.communicate.data.whatsapp.getMediaKeys |
| 12 | import com.vayunmathur.communicate.data.whatsapp.padMessage |
| 13 | import com.vayunmathur.communicate.data.whatsapp.proto.WhatsAppE2EProto |
| 14 | import com.vayunmathur.communicate.data.whatsapp.unpadMessage |
| 15 | import kotlin.test.Test |
| 16 | import kotlin.test.assertEquals |
| 17 | import kotlin.test.assertFailsWith |
| 18 | import kotlin.test.assertNotEquals |
| 19 | import kotlin.test.assertTrue |
| 20 | |
| 21 | /** |
| 22 | * Slice 1 (w5): crypto helpers in WhatsAppProtocolCrypto.kt. |
| 23 | * Pure JVM (java.security only). The poll-vote path here mirrors the wire derivation |
| 24 | * (HKDF info "Poll Vote", AES-GCM) so build→decrypt must round-trip. |
| 25 | */ |
| 26 | class W5Slice1ProtocolCryptoTest { |
| 27 | |
| 28 | // ---- pad / unpad ---- |
| 29 | |
| 30 | @Test |
| 31 | fun pad_addsBetween1And15BytesWithPadCountValue() { |
| 32 | val plain = "hello".toByteArray() |
| 33 | val padded = WhatsAppProtocol.padMessage(plain) |
| 34 | val padSize = padded.size - plain.size |
| 35 | assertTrue(padSize in 1..15, "pad=$padSize") |
| 36 | assertTrue(padded.take(plain.size).toByteArray().contentEquals(plain)) |
| 37 | assertTrue(padded.drop(plain.size).all { it == padSize.toByte() }) |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | fun unpad_recoversPlaintext() { |
| 42 | val plain = "round trip payload".toByteArray() |
| 43 | assertTrue(WhatsAppProtocol.unpadMessage(WhatsAppProtocol.padMessage(plain)).contentEquals(plain)) |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | fun unpad_edgeCases_returnInputUnchanged() { |
| 48 | assertEquals(0, WhatsAppProtocol.unpadMessage(ByteArray(0)).size) |
| 49 | // padSize 0 or larger than input: returned as-is |
| 50 | val zeroPad = byteArrayOf(1, 2, 0) |
| 51 | assertTrue(WhatsAppProtocol.unpadMessage(zeroPad).contentEquals(zeroPad)) |
| 52 | val huge = byteArrayOf(1, 2, 3, 100) |
| 53 | assertTrue(WhatsAppProtocol.unpadMessage(huge).contentEquals(huge)) |
| 54 | } |
| 55 | |
| 56 | // ---- media keys + encrypt/decrypt ---- |
| 57 | |
| 58 | @Test |
| 59 | fun mediaKeys_haveExpectedLengthsAndDeterminism() { |
| 60 | val key = ByteArray(32) { it.toByte() } |
| 61 | val a = WhatsAppProtocol.getMediaKeys(key, WhatsAppProtocol.MEDIA_KEY_IMAGE) |
| 62 | val b = WhatsAppProtocol.getMediaKeys(key, WhatsAppProtocol.MEDIA_KEY_IMAGE) |
| 63 | assertEquals(16, a.iv.size) |
| 64 | assertEquals(32, a.cipherKey.size) |
| 65 | assertEquals(32, a.macKey.size) |
| 66 | assertEquals(32, a.refKey.size) |
| 67 | assertTrue(a.cipherKey.contentEquals(b.cipherKey)) |
| 68 | // different media type -> different keys |
| 69 | val video = WhatsAppProtocol.getMediaKeys(key, WhatsAppProtocol.MEDIA_KEY_VIDEO) |
| 70 | assertTrue(!a.cipherKey.contentEquals(video.cipherKey)) |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun media_encryptDecrypt_roundTrips() { |
| 75 | val plain = "fake image bytes 1234567890".toByteArray() |
| 76 | val enc = WhatsAppProtocol.encryptMedia(plain, WhatsAppProtocol.MEDIA_KEY_IMAGE) |
| 77 | assertEquals(plain.size.toLong(), enc.fileLength) |
| 78 | assertEquals(32, enc.mediaKey.size) |
| 79 | assertEquals(32, enc.fileSha256.size) |
| 80 | assertEquals(32, enc.fileEncSha256.size) |
| 81 | val dec = WhatsAppProtocol.decryptMedia(enc.encryptedData, enc.mediaKey, WhatsAppProtocol.MEDIA_KEY_IMAGE) |
| 82 | assertTrue(dec.contentEquals(plain)) |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | fun media_decrypt_tamperedCiphertext_throws() { |
| 87 | val plain = "tamper me".toByteArray() |
| 88 | val enc = WhatsAppProtocol.encryptMedia(plain, WhatsAppProtocol.MEDIA_KEY_AUDIO) |
| 89 | val tampered = enc.encryptedData.copyOf().also { it[0] = (it[0] + 1).toByte() } |
| 90 | assertFailsWith<Exception> { |
| 91 | WhatsAppProtocol.decryptMedia(tampered, enc.mediaKey, WhatsAppProtocol.MEDIA_KEY_AUDIO) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | fun media_decrypt_wrongKey_throws() { |
| 97 | val enc = WhatsAppProtocol.encryptMedia("x".toByteArray(), WhatsAppProtocol.MEDIA_KEY_DOCUMENT) |
| 98 | assertFailsWith<Exception> { |
| 99 | WhatsAppProtocol.decryptMedia(enc.encryptedData, ByteArray(32) { 7 }, WhatsAppProtocol.MEDIA_KEY_DOCUMENT) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // ---- app-state keys ---- |
| 104 | |
| 105 | @Test |
| 106 | fun expandAppStateKeys_returnsFive32ByteKeys() { |
| 107 | val keys = WhatsAppProtocol.expandAppStateKeys(ByteArray(32) { (it * 3).toByte() }) |
| 108 | assertEquals(5, keys.size) |
| 109 | keys.forEach { assertEquals(32, it.size) } |
| 110 | // distinct sub-keys |
| 111 | assertTrue(!keys[0].contentEquals(keys[1])) |
| 112 | assertTrue(!keys[3].contentEquals(keys[4])) |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | fun decryptAppStateValue_tooShort_returnsNull() { |
| 117 | assertEquals(null, WhatsAppProtocol.decryptAppStateValue(ByteArray(10), ByteArray(32))) |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | fun decryptAppStateValue_badKey_returnsNull() { |
| 122 | val blob = ByteArray(16 + 16 + 32) { it.toByte() } |
| 123 | assertEquals(null, WhatsAppProtocol.decryptAppStateValue(blob, ByteArray(16))) |
| 124 | } |
| 125 | |
| 126 | // ---- message IDs ---- |
| 127 | |
| 128 | @Test |
| 129 | fun generateMessageId_hasPrefixAndHexShape() { |
| 130 | val id = WhatsAppProtocol.generateMessageId("[email protected]") |
| 131 | assertTrue(id.startsWith("3EB0"), id) |
| 132 | assertEquals(4 + 18, id.length) |
| 133 | assertTrue(id.drop(4).all { it in '0'..'9' || it in 'A'..'F' }, id) |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | fun generateMessageId_nullJid_stillWellFormed() { |
| 138 | val id = WhatsAppProtocol.generateMessageId(null) |
| 139 | assertTrue(id.startsWith("3EB0"), id) |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | fun generateMessageId_isUnique() { |
| 144 | val a = WhatsAppProtocol.generateMessageId("[email protected]") |
| 145 | val b = WhatsAppProtocol.generateMessageId("[email protected]") |
| 146 | assertNotEquals(a, b) |
| 147 | } |
| 148 | |
| 149 | // ---- primitives ---- |
| 150 | |
| 151 | @Test |
| 152 | fun primitives_knownVectors() { |
| 153 | // SHA-256("abc") |
| 154 | val sha = WhatsAppProtocol.sha256("abc".toByteArray()) |
| 155 | assertEquals( |
| 156 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", |
| 157 | sha.joinToString("") { "%02x".format(it) }, |
| 158 | ) |
| 159 | // HKDF is deterministic and length-correct (RFC 5869 shape, not full vector) |
| 160 | val k1 = WhatsAppProtocol.hkdfSha256("ikm".toByteArray(), null, "info".toByteArray(), 42) |
| 161 | val k2 = WhatsAppProtocol.hkdfSha256("ikm".toByteArray(), null, "info".toByteArray(), 42) |
| 162 | assertEquals(42, k1.size) |
| 163 | assertTrue(k1.contentEquals(k2)) |
| 164 | // HMAC differs by key |
| 165 | val m1 = WhatsAppProtocol.hmacSha256("k1".toByteArray(), "d".toByteArray()) |
| 166 | val m2 = WhatsAppProtocol.hmacSha256("k2".toByteArray(), "d".toByteArray()) |
| 167 | assertTrue(!m1.contentEquals(m2)) |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | fun noiseHandshake_encryptDecryptRoundTrips() { |
| 172 | val hs = WhatsAppProtocol.NoiseHandshake() |
| 173 | hs.start(WhatsAppProtocol.NOISE_START_PATTERN, WhatsAppProtocol.WA_CONN_HEADER) |
| 174 | val ct = hs.encrypt("handshake probe".toByteArray()) |
| 175 | // A fresh handshake with the same start state decrypts its own ciphertext only if |
| 176 | // counters line up: encrypt then decrypt on separate instances both at counter 0. |
| 177 | val hs2 = WhatsAppProtocol.NoiseHandshake() |
| 178 | hs2.start(WhatsAppProtocol.NOISE_START_PATTERN, WhatsAppProtocol.WA_CONN_HEADER) |
| 179 | assertEquals("handshake probe", String(hs2.decrypt(ct))) |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | fun noiseHandshake_finish_returnsKeyPair() { |
| 184 | val hs = WhatsAppProtocol.NoiseHandshake() |
| 185 | hs.start(WhatsAppProtocol.NOISE_START_PATTERN, WhatsAppProtocol.WA_CONN_HEADER) |
| 186 | val (w, r) = hs.finish() |
| 187 | assertEquals(32, w.encoded.size) |
| 188 | assertEquals(32, r.encoded.size) |
| 189 | } |
| 190 | } |