Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / SignalAttachmentCipher.kt
SignalAttachmentCipher.kt117 lines · 4.7 KiBRaw
| 1 | package com.vayunmathur.communicate.data.signal |
| 2 | |
| 3 | import java.security.MessageDigest |
| 4 | import java.security.SecureRandom |
| 5 | import javax.crypto.Cipher |
| 6 | import javax.crypto.Mac |
| 7 | import javax.crypto.spec.IvParameterSpec |
| 8 | import javax.crypto.spec.SecretKeySpec |
| 9 | |
| 10 | /** |
| 11 | * Signal's attachment encryption. |
| 12 | * |
| 13 | * The blob on the CDN is `IV || AES-256-CBC(plaintext) || HMAC-SHA256(IV || ciphertext)`, and the |
| 14 | * `AttachmentPointer` carries the 64-byte combined key (32 AES then 32 HMAC) plus a SHA-256 digest over |
| 15 | * the whole blob. The server never sees the key, so an attachment uploaded without this is both readable |
| 16 | * by the CDN and undecryptable by any real peer. |
| 17 | */ |
| 18 | object SignalAttachmentCipher { |
| 19 | private const val KEY_SIZE = 64 |
| 20 | private const val AES_KEY_SIZE = 32 |
| 21 | private const val IV_SIZE = 16 |
| 22 | private const val MAC_SIZE = 32 |
| 23 | |
| 24 | /** Thrown when an attachment blob MAC does not verify (tampered or wrong key). */ |
| 25 | class MacMismatchException : Exception("attachment MAC did not verify") |
| 26 | /** Thrown when an attachment blob digest does not match its pointer. */ |
| 27 | class DigestMismatchException : Exception("attachment digest did not verify") |
| 28 | |
| 29 | /** An encrypted attachment blob plus the key and digest for its AttachmentPointer. */ |
| 30 | data class Encrypted( |
| 31 | /** The 64-byte combined key for `AttachmentPointer.key`. */ |
| 32 | val key: ByteArray, |
| 33 | /** SHA-256 over [blob], for `AttachmentPointer.digest`. */ |
| 34 | val digest: ByteArray, |
| 35 | /** What gets uploaded. */ |
| 36 | val blob: ByteArray, |
| 37 | /** Plaintext length, for `AttachmentPointer.size`. */ |
| 38 | val plaintextSize: Int, |
| 39 | ) |
| 40 | |
| 41 | fun generateKey(): ByteArray = ByteArray(KEY_SIZE).also { SecureRandom().nextBytes(it) } |
| 42 | |
| 43 | fun encrypt(plaintext: ByteArray, key: ByteArray = generateKey()): Encrypted { |
| 44 | require(key.size == KEY_SIZE) { "attachment key must be $KEY_SIZE bytes, was ${key.size}" } |
| 45 | val aesKey = key.copyOfRange(0, AES_KEY_SIZE) |
| 46 | val macKey = key.copyOfRange(AES_KEY_SIZE, KEY_SIZE) |
| 47 | |
| 48 | val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") |
| 49 | cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(aesKey, "AES")) |
| 50 | val iv = cipher.iv |
| 51 | val ciphertext = cipher.doFinal(plaintext) |
| 52 | |
| 53 | val mac = Mac.getInstance("HmacSHA256") |
| 54 | mac.init(SecretKeySpec(macKey, "HmacSHA256")) |
| 55 | mac.update(iv) |
| 56 | val auth = mac.doFinal(ciphertext) |
| 57 | |
| 58 | val blob = ByteArray(iv.size + ciphertext.size + auth.size) |
| 59 | iv.copyInto(blob) |
| 60 | ciphertext.copyInto(blob, iv.size) |
| 61 | auth.copyInto(blob, iv.size + ciphertext.size) |
| 62 | |
| 63 | return Encrypted( |
| 64 | key = key, |
| 65 | digest = MessageDigest.getInstance("SHA-256").digest(blob), |
| 66 | blob = blob, |
| 67 | plaintextSize = plaintext.size, |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Reverse of [encrypt]. The MAC is checked before decrypting, and [digest] before that when supplied — |
| 73 | * a blob that fails either is discarded rather than decrypted, since it did not come from the sender. |
| 74 | * |
| 75 | * [plaintextSize] trims CBC padding to the length the pointer declared; omit it to keep whatever |
| 76 | * unpadding yields. |
| 77 | */ |
| 78 | fun decrypt( |
| 79 | blob: ByteArray, |
| 80 | key: ByteArray, |
| 81 | digest: ByteArray? = null, |
| 82 | plaintextSize: Int? = null, |
| 83 | ): ByteArray { |
| 84 | require(key.size == KEY_SIZE) { "attachment key must be $KEY_SIZE bytes, was ${key.size}" } |
| 85 | require(blob.size > IV_SIZE + MAC_SIZE) { "attachment blob is too short: ${blob.size}" } |
| 86 | |
| 87 | if (digest != null) { |
| 88 | val actual = MessageDigest.getInstance("SHA-256").digest(blob) |
| 89 | if (!MessageDigest.isEqual(actual, digest)) throw DigestMismatchException() |
| 90 | } |
| 91 | |
| 92 | val aesKey = key.copyOfRange(0, AES_KEY_SIZE) |
| 93 | val macKey = key.copyOfRange(AES_KEY_SIZE, KEY_SIZE) |
| 94 | |
| 95 | val macOffset = blob.size - MAC_SIZE |
| 96 | val mac = Mac.getInstance("HmacSHA256") |
| 97 | mac.init(SecretKeySpec(macKey, "HmacSHA256")) |
| 98 | mac.update(blob, 0, macOffset) |
| 99 | // Constant-time compare; a byte-by-byte early exit would leak the expected MAC. |
| 100 | if (!MessageDigest.isEqual(mac.doFinal(), blob.copyOfRange(macOffset, blob.size))) { |
| 101 | throw MacMismatchException() |
| 102 | } |
| 103 | |
| 104 | val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") |
| 105 | cipher.init( |
| 106 | Cipher.DECRYPT_MODE, |
| 107 | SecretKeySpec(aesKey, "AES"), |
| 108 | IvParameterSpec(blob.copyOfRange(0, IV_SIZE)), |
| 109 | ) |
| 110 | val plaintext = cipher.doFinal(blob, IV_SIZE, macOffset - IV_SIZE) |
| 111 | return if (plaintextSize != null && plaintextSize in 0..plaintext.size) { |
| 112 | plaintext.copyOfRange(0, plaintextSize) |
| 113 | } else { |
| 114 | plaintext |
| 115 | } |
| 116 | } |
| 117 | } |