Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalSealedSenderTest.kt
SignalSealedSenderTest.kt88 lines · 3.4 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.signal.SignalSealedSender |
| 4 | import com.vayunmathur.communicate.data.signal.e2e.SignalE2E |
| 5 | import com.vayunmathur.communicate.data.signal.transport.SignalPayload |
| 6 | import javax.crypto.Cipher |
| 7 | import javax.crypto.spec.GCMParameterSpec |
| 8 | import javax.crypto.spec.SecretKeySpec |
| 9 | import kotlin.test.Test |
| 10 | import kotlin.test.assertEquals |
| 11 | import kotlin.test.assertFalse |
| 12 | import kotlin.test.assertNull |
| 13 | import kotlin.test.assertTrue |
| 14 | |
| 15 | /** |
| 16 | * Vectors for sealed sender. The access-key derivation is deliberately not an HKDF, and the truncation |
| 17 | * takes the GCM *ciphertext* rather than the tag — both are easy to get subtly wrong. |
| 18 | */ |
| 19 | class SignalSealedSenderTest { |
| 20 | private val profileKey = ByteArray(32) { (it + 1).toByte() } |
| 21 | |
| 22 | @Test |
| 23 | fun accessKeyIsSixteenBytesAndDeterministic() { |
| 24 | val a = SignalSealedSender.deriveAccessKey(profileKey)!! |
| 25 | val b = SignalSealedSender.deriveAccessKey(profileKey)!! |
| 26 | assertEquals(16, a.size) |
| 27 | assertTrue(a.contentEquals(b)) |
| 28 | } |
| 29 | |
| 30 | @Test |
| 31 | fun accessKeyIsTheCiphertextNotTheTag() { |
| 32 | // AES-256-GCM over 16 zero bytes returns 32: 16 ciphertext then 16 tag. Taking the tag instead |
| 33 | // would still be 16 bytes and would still look plausible, so pin the half that is used. |
| 34 | val cipher = Cipher.getInstance("AES/GCM/NoPadding") |
| 35 | cipher.init( |
| 36 | Cipher.ENCRYPT_MODE, |
| 37 | SecretKeySpec(profileKey, "AES"), |
| 38 | GCMParameterSpec(128, ByteArray(12)), |
| 39 | ) |
| 40 | val full = cipher.doFinal(ByteArray(16)) |
| 41 | assertEquals(32, full.size) |
| 42 | |
| 43 | val derived = SignalSealedSender.deriveAccessKey(profileKey)!! |
| 44 | assertTrue(derived.contentEquals(full.copyOfRange(0, 16))) |
| 45 | assertFalse(derived.contentEquals(full.copyOfRange(16, 32))) |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | fun differentProfileKeysGiveDifferentAccessKeys() { |
| 50 | val other = ByteArray(32) { (it + 2).toByte() } |
| 51 | val a = SignalSealedSender.deriveAccessKey(profileKey)!! |
| 52 | val b = SignalSealedSender.deriveAccessKey(other)!! |
| 53 | assertFalse(a.contentEquals(b)) |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun rejectsProfileKeysOfTheWrongLength() { |
| 58 | assertNull(SignalSealedSender.deriveAccessKey(ByteArray(0))) |
| 59 | assertNull(SignalSealedSender.deriveAccessKey(ByteArray(16))) |
| 60 | assertNull(SignalSealedSender.deriveAccessKey(ByteArray(31))) |
| 61 | assertNull(SignalSealedSender.deriveAccessKey(ByteArray(33))) |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | fun accessKeyHeaderUsesTheOfficialNameAndPaddedBase64() { |
| 66 | val header = SignalSealedSender.accessKeyHeader(ByteArray(16)) |
| 67 | assertEquals("Unidentified-Access-Key:AAAAAAAAAAAAAAAAAAAAAA==", header) |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | fun certificateResponseWithoutTheFieldIsRejected() { |
| 72 | assertNull(SignalSealedSender.parseCertificate("""{}""", warn = {})) |
| 73 | assertNull(SignalSealedSender.parseCertificate("""{"certificate":""}""", warn = {})) |
| 74 | assertNull(SignalSealedSender.parseCertificate("not json", warn = {})) |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun malformedCertificateBytesAreRejected() { |
| 79 | // A well-formed response carrying garbage must not yield a certificate. |
| 80 | assertNull(SignalSealedSender.parseCertificate("""{"certificate":"AQIDBA=="}""", warn = {})) |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | fun sealedSenderMapsOntoItsOwnEnvelopeType() { |
| 85 | // Envelope.UNIDENTIFIED_SENDER = 6, distinct from the inner ciphertext types. |
| 86 | assertEquals(6, SignalPayload.envelopeTypeFor(SignalE2E.SEALED_SENDER_TYPE)) |
| 87 | } |
| 88 | } |