Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1Crypt15Test.kt
W5Slice1Crypt15Test.kt159 lines · 5.6 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 4 | import com.vayunmathur.communicate.data.whatsapp.backup.Crypt15Decryptor |
| 5 | import org.junit.runner.RunWith |
| 6 | import java.io.ByteArrayOutputStream |
| 7 | import java.nio.file.Files |
| 8 | import java.security.MessageDigest |
| 9 | import java.util.zip.GZIPOutputStream |
| 10 | import javax.crypto.Cipher |
| 11 | import javax.crypto.Mac |
| 12 | import javax.crypto.spec.GCMParameterSpec |
| 13 | import javax.crypto.spec.SecretKeySpec |
| 14 | import kotlin.test.Test |
| 15 | import kotlin.test.assertEquals |
| 16 | import kotlin.test.assertFailsWith |
| 17 | import kotlin.test.assertTrue |
| 18 | |
| 19 | /** |
| 20 | * Slice 1 (w5): Crypt15Decryptor — build a crypt15 blob with the documented |
| 21 | * HKDF(info="backup encryption") derivation, decrypt, and assert the gunzipped |
| 22 | * SQLite bytes round-trip. Plus malformed-input error paths. |
| 23 | * |
| 24 | * Robolectric: the success path logs via Android Log, so the class runs under |
| 25 | * Robolectric (behavior-identical crypto on JVM). |
| 26 | */ |
| 27 | @RunWith(AndroidJUnit4::class) |
| 28 | class W5Slice1Crypt15Test { |
| 29 | |
| 30 | private fun hmac(key: ByteArray, msg: ByteArray): ByteArray { |
| 31 | val mac = Mac.getInstance("HmacSHA256") |
| 32 | mac.init(SecretKeySpec(key, "HmacSHA256")) |
| 33 | return mac.doFinal(msg) |
| 34 | } |
| 35 | |
| 36 | private fun hkdf(ikm: ByteArray, salt: ByteArray, info: ByteArray, len: Int): ByteArray { |
| 37 | val prk = hmac(salt, ikm) |
| 38 | val out = ByteArray(len) |
| 39 | var t = ByteArray(0) |
| 40 | var pos = 0 |
| 41 | var c = 1 |
| 42 | while (pos < len) { |
| 43 | val mac = Mac.getInstance("HmacSHA256") |
| 44 | mac.init(SecretKeySpec(prk, "HmacSHA256")) |
| 45 | mac.update(t) |
| 46 | mac.update(info) |
| 47 | mac.update(c.toByte()) |
| 48 | t = mac.doFinal() |
| 49 | val n = minOf(t.size, len - pos) |
| 50 | System.arraycopy(t, 0, out, pos, n) |
| 51 | pos += n |
| 52 | c++ |
| 53 | } |
| 54 | return out |
| 55 | } |
| 56 | |
| 57 | private fun gzip(data: ByteArray): ByteArray { |
| 58 | val bos = ByteArrayOutputStream() |
| 59 | GZIPOutputStream(bos).use { it.write(data) } |
| 60 | return bos.toByteArray() |
| 61 | } |
| 62 | |
| 63 | private fun varint(v: Long): ByteArray { |
| 64 | val out = ByteArrayOutputStream() |
| 65 | var x = v |
| 66 | while (true) { |
| 67 | var b = (x and 0x7F).toInt() |
| 68 | x = x ushr 7 |
| 69 | if (x != 0L) b = b or 0x80 |
| 70 | out.write(b) |
| 71 | if (x == 0L) break |
| 72 | } |
| 73 | return out.toByteArray() |
| 74 | } |
| 75 | |
| 76 | /** Minimal header: [varint len][field1 = length-delimited 16-byte IV]. */ |
| 77 | private fun headerWithIv(iv: ByteArray): ByteArray { |
| 78 | val inner = ByteArrayOutputStream() |
| 79 | inner.write(0x0A) // field 1, wire type 2 |
| 80 | inner.write(varint(16)) |
| 81 | inner.write(iv) |
| 82 | val innerBytes = inner.toByteArray() |
| 83 | val outer = ByteArrayOutputStream() |
| 84 | outer.write(varint(innerBytes.size.toLong())) |
| 85 | outer.write(innerBytes) |
| 86 | return outer.toByteArray() |
| 87 | } |
| 88 | |
| 89 | private fun encryptBlob(plainDb: ByteArray, backupKey: ByteArray, iv: ByteArray): ByteArray { |
| 90 | val aesKey = hkdf(backupKey, ByteArray(32), "backup encryption".toByteArray(Charsets.UTF_8), 32) |
| 91 | val cipher = Cipher.getInstance("AES/GCM/NoPadding") |
| 92 | cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(aesKey, "AES"), GCMParameterSpec(128, iv)) |
| 93 | val ct = cipher.doFinal(gzip(plainDb)) |
| 94 | return headerWithIv(iv) + ct |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | fun decrypt_roundTripsGzippedPayload() { |
| 99 | val backupKey = ByteArray(32) { (it * 7 + 1).toByte() } |
| 100 | val iv = ByteArray(16) { (it + 3).toByte() } |
| 101 | val fakeDb = "SQLite format 3\u0000fake-db-bytes-1234567890".toByteArray(Charsets.ISO_8859_1) |
| 102 | val blob = encryptBlob(fakeDb, backupKey, iv) |
| 103 | |
| 104 | val cacheDir = Files.createTempDirectory("crypt15test").toFile() |
| 105 | try { |
| 106 | val out = Crypt15Decryptor.decryptToFile(blob, backupKey, cacheDir) |
| 107 | assertTrue(out.exists()) |
| 108 | assertTrue(out.readBytes().contentEquals(fakeDb)) |
| 109 | } finally { |
| 110 | cacheDir.deleteRecursively() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | fun decrypt_rejectsShortKey() { |
| 116 | val ex = assertFailsWith<Crypt15Decryptor.DecryptException> { |
| 117 | Crypt15Decryptor.decryptToFile(ByteArray(64), ByteArray(16), Files.createTempDirectory("c15").toFile()) |
| 118 | } |
| 119 | assertTrue(ex.message!!.contains("32 bytes"), ex.message) |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | fun decrypt_truncatedFile_throws() { |
| 124 | assertFailsWith<Crypt15Decryptor.DecryptException> { |
| 125 | Crypt15Decryptor.decryptToFile( |
| 126 | byteArrayOf(0x40, 0x0A), // claims 64-byte header, has 1 |
| 127 | ByteArray(32), |
| 128 | Files.createTempDirectory("c15").toFile(), |
| 129 | ) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | @Test |
| 134 | fun decrypt_headerWithoutIv_throws() { |
| 135 | // header with only a varint field, no 16-byte blob |
| 136 | val inner = byteArrayOf(0x08, 0x01) // field 1 varint = 1 |
| 137 | val blob = varint(inner.size.toLong()) + inner + ByteArray(32) |
| 138 | val ex = assertFailsWith<Crypt15Decryptor.DecryptException> { |
| 139 | Crypt15Decryptor.decryptToFile(blob, ByteArray(32), Files.createTempDirectory("c15").toFile()) |
| 140 | } |
| 141 | assertTrue(ex.message!!.contains("IV"), ex.message) |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | fun decrypt_wrongKey_throwsDecryptException() { |
| 146 | val rightKey = ByteArray(32) { 11 } |
| 147 | val iv = ByteArray(16) { 5 } |
| 148 | val blob = encryptBlob("db-bytes".toByteArray(), rightKey, iv) |
| 149 | assertFailsWith<Crypt15Decryptor.DecryptException> { |
| 150 | Crypt15Decryptor.decryptToFile(blob, ByteArray(32) { 12 }, Files.createTempDirectory("c15").toFile()) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | fun sha256_sanity() { |
| 156 | val digest = MessageDigest.getInstance("SHA-256").digest("abc".toByteArray()) |
| 157 | assertEquals(32, digest.size) |
| 158 | } |
| 159 | } |