Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalAttachmentCipherTest.kt
SignalAttachmentCipherTest.kt118 lines · 4.7 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.signal.SignalAttachmentCipher |
| 4 | import java.security.MessageDigest |
| 5 | import kotlin.test.Test |
| 6 | import kotlin.test.assertContentEquals |
| 7 | import kotlin.test.assertEquals |
| 8 | import kotlin.test.assertFailsWith |
| 9 | import kotlin.test.assertFalse |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * Round-trip and tamper vectors for attachment encryption. The blob layout is |
| 14 | * `IV(16) || AES-256-CBC(plaintext) || HMAC-SHA256(IV || ciphertext)(32)`, the key is 32 bytes of AES |
| 15 | * followed by 32 of HMAC, and the digest is SHA-256 over the whole blob. |
| 16 | */ |
| 17 | class SignalAttachmentCipherTest { |
| 18 | private val plaintext = "the quick brown fox jumps over the lazy dog".toByteArray() |
| 19 | |
| 20 | @Test |
| 21 | fun roundTrips() { |
| 22 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 23 | val out = SignalAttachmentCipher.decrypt(enc.blob, enc.key, enc.digest, enc.plaintextSize) |
| 24 | assertContentEquals(plaintext, out) |
| 25 | } |
| 26 | |
| 27 | @Test |
| 28 | fun roundTripsAcrossSizesIncludingBlockBoundaries() { |
| 29 | for (size in intArrayOf(0, 1, 15, 16, 17, 31, 32, 33, 1000)) { |
| 30 | val body = ByteArray(size) { (it % 251).toByte() } |
| 31 | val enc = SignalAttachmentCipher.encrypt(body) |
| 32 | val out = SignalAttachmentCipher.decrypt(enc.blob, enc.key, enc.digest, enc.plaintextSize) |
| 33 | assertContentEquals(body, out, "round-trip failed at size=$size") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | fun keyIsSixtyFourBytesAndDigestIsThirtyTwo() { |
| 39 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 40 | assertEquals(64, enc.key.size) |
| 41 | assertEquals(32, enc.digest.size) |
| 42 | assertEquals(plaintext.size, enc.plaintextSize) |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | fun blobLayoutIsIvThenCiphertextThenMac() { |
| 47 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 48 | // CBC pads to a block, so the ciphertext is at least the plaintext rounded up. |
| 49 | val expectedCiphertext = ((plaintext.size / 16) + 1) * 16 |
| 50 | assertEquals(16 + expectedCiphertext + 32, enc.blob.size) |
| 51 | // The digest covers the entire blob, MAC included. |
| 52 | val digest = MessageDigest.getInstance("SHA-256").digest(enc.blob) |
| 53 | assertContentEquals(digest, enc.digest) |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun tamperedCiphertextFailsTheMac() { |
| 58 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 59 | val tampered = enc.blob.copyOf().also { it[20] = (it[20].toInt() xor 0xFF).toByte() } |
| 60 | assertFailsWith<SignalAttachmentCipher.MacMismatchException> { |
| 61 | // No digest, so the MAC is what has to catch it. |
| 62 | SignalAttachmentCipher.decrypt(tampered, enc.key, digest = null) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | fun tamperedBlobFailsTheDigestBeforeTheMac() { |
| 68 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 69 | val tampered = enc.blob.copyOf().also { it[20] = (it[20].toInt() xor 0xFF).toByte() } |
| 70 | assertFailsWith<SignalAttachmentCipher.DigestMismatchException> { |
| 71 | SignalAttachmentCipher.decrypt(tampered, enc.key, enc.digest) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | fun wrongKeyIsRejected() { |
| 77 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 78 | val wrong = SignalAttachmentCipher.generateKey() |
| 79 | assertFailsWith<SignalAttachmentCipher.MacMismatchException> { |
| 80 | SignalAttachmentCipher.decrypt(enc.blob, wrong, digest = null) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | fun swappingKeyHalvesIsRejected() { |
| 86 | // Catches getting the AES/HMAC halves the wrong way round. |
| 87 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 88 | val swapped = enc.key.copyOfRange(32, 64) + enc.key.copyOfRange(0, 32) |
| 89 | assertFailsWith<SignalAttachmentCipher.MacMismatchException> { |
| 90 | SignalAttachmentCipher.decrypt(enc.blob, swapped, digest = null) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | fun rejectsKeysAndBlobsOfTheWrongSize() { |
| 96 | val enc = SignalAttachmentCipher.encrypt(plaintext) |
| 97 | assertFailsWith<IllegalArgumentException> { |
| 98 | SignalAttachmentCipher.decrypt(enc.blob, ByteArray(32)) |
| 99 | } |
| 100 | assertFailsWith<IllegalArgumentException> { |
| 101 | SignalAttachmentCipher.decrypt(ByteArray(40), enc.key) |
| 102 | } |
| 103 | assertFailsWith<IllegalArgumentException> { |
| 104 | SignalAttachmentCipher.encrypt(plaintext, ByteArray(32)) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | fun eachEncryptionUsesAFreshIv() { |
| 110 | val key = SignalAttachmentCipher.generateKey() |
| 111 | val a = SignalAttachmentCipher.encrypt(plaintext, key) |
| 112 | val b = SignalAttachmentCipher.encrypt(plaintext, key) |
| 113 | assertFalse(a.blob.copyOfRange(0, 16).contentEquals(b.blob.copyOfRange(0, 16))) |
| 114 | // Same key and plaintext must still not produce the same blob. |
| 115 | assertFalse(a.blob.contentEquals(b.blob)) |
| 116 | assertTrue(a.key.contentEquals(b.key)) |
| 117 | } |
| 118 | } |