Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / e2ee-openpgp / src / main / java / com / vayunmathur / e2ee / Pgp.kt
Pgp.kt128 lines · 5.2 KiBRaw
| 1 | package com.vayunmathur.e2ee |
| 2 | |
| 3 | /** |
| 4 | * OpenPGP (RFC 9580) primitives for SecureMail, backed by native Rust (rPGP) |
| 5 | * instead of BouncyCastle. |
| 6 | * |
| 7 | * Suite: Ed25519 signing primary + X25519 encryption subkey, AES-256 session |
| 8 | * keys, SHA-512 digests — interoperable with ProtonMail and any OpenPGP |
| 9 | * client. All key handling speaks ASCII armor; the relay wire carries raw |
| 10 | * bytes and converts at the boundary only, mirroring the FindFamily relay |
| 11 | * contract. |
| 12 | */ |
| 13 | object Pgp { |
| 14 | |
| 15 | /** A generated identity: armored secret ring (passphrase-wrapped) + armored public ring. */ |
| 16 | data class Identity( |
| 17 | val armoredSecret: String, |
| 18 | val armoredPublic: String, |
| 19 | val fingerprintHex: String, |
| 20 | ) |
| 21 | |
| 22 | /** `nativePgpGenerate` returns exactly (secret, public, fingerprint). */ |
| 23 | private const val GENERATED_PART_COUNT = 3 |
| 24 | |
| 25 | private fun String.toUtf8(): ByteArray = toByteArray(Charsets.UTF_8) |
| 26 | private fun ByteArray.toUtf8String(): String = toString(Charsets.UTF_8) |
| 27 | |
| 28 | /** |
| 29 | * Generates an Ed25519 primary + X25519 encryption subkey for [userId] |
| 30 | * (e.g. "alice <[email protected]>"). The secret key material is wrapped |
| 31 | * with [passphrase] via S2K so the stored blob alone is useless. |
| 32 | */ |
| 33 | fun generateIdentity(userId: String, passphrase: String): Identity { |
| 34 | val parts = PgpNative.nativePgpGenerate(userId.toUtf8(), passphrase.toUtf8()) |
| 35 | ?: error("OpenPGP key generation failed") |
| 36 | require(parts.size == GENERATED_PART_COUNT) { "OpenPGP key generation returned ${parts.size} parts" } |
| 37 | return Identity( |
| 38 | armoredSecret = parts[0].toUtf8String(), |
| 39 | armoredPublic = parts[1].toUtf8String(), |
| 40 | fingerprintHex = parts[2].toUtf8String(), |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | /** Fingerprint hex (uppercase, no spaces) of an armored public key. */ |
| 45 | fun fingerprintHex(armoredPublic: String): String = |
| 46 | PgpNative.nativePgpFingerprint(armoredPublic.toUtf8())?.toUtf8String() |
| 47 | ?: error("Invalid OpenPGP public key") |
| 48 | |
| 49 | /** |
| 50 | * Normalizes a WKD record (binary key or armor) to canonical armor text. |
| 51 | * Throws when the input is not a parseable OpenPGP public key. |
| 52 | */ |
| 53 | fun normalizeKey(record: ByteArray): String = |
| 54 | PgpNative.nativePgpNormalizeKey(record)?.toUtf8String() |
| 55 | ?: error("Invalid OpenPGP public key") |
| 56 | |
| 57 | /** |
| 58 | * Encrypts [plaintext] (raw message bytes, e.g. a PGP/MIME body) to the |
| 59 | * holder of [armoredPublic]. Returns raw (unarmored) PGP bytes for the |
| 60 | * relay wire; armor via [armorMessage] for SMTP federation. |
| 61 | */ |
| 62 | fun encrypt(armoredPublic: String, plaintext: ByteArray): ByteArray = |
| 63 | PgpNative.nativePgpEncrypt(armoredPublic.toUtf8(), plaintext) |
| 64 | ?: error("OpenPGP encryption failed") |
| 65 | |
| 66 | /** |
| 67 | * Encrypts + signs: as [encrypt], additionally signing with the sender's |
| 68 | * secret key. Empty [senderSecret] disables signing (same as [encrypt]). |
| 69 | */ |
| 70 | fun encryptSigned( |
| 71 | armoredPublic: String, |
| 72 | senderSecret: String, |
| 73 | senderPassphrase: String, |
| 74 | plaintext: ByteArray, |
| 75 | ): ByteArray = |
| 76 | PgpNative.nativePgpEncryptSigned( |
| 77 | armoredPublic.toUtf8(), |
| 78 | senderSecret.toUtf8(), |
| 79 | senderPassphrase.toUtf8(), |
| 80 | plaintext, |
| 81 | ) ?: error("OpenPGP signed encryption failed") |
| 82 | |
| 83 | /** |
| 84 | * Decrypts raw PGP bytes with [armoredSecret] + [passphrase]. Returns the |
| 85 | * literal payload. |
| 86 | */ |
| 87 | fun decrypt(armoredSecret: String, passphrase: String, ciphertext: ByteArray): ByteArray = |
| 88 | PgpNative.nativePgpDecrypt(armoredSecret.toUtf8(), passphrase.toUtf8(), ciphertext) |
| 89 | ?: error("OpenPGP decryption failed") |
| 90 | |
| 91 | /** Result of [decryptVerify]: payload plus signer fingerprint (empty when unsigned). */ |
| 92 | data class Verified(val payload: ByteArray, val signerFingerprintHex: String) |
| 93 | |
| 94 | /** |
| 95 | * Decrypts + verifies: as [decrypt], additionally verifying an embedded |
| 96 | * signature against [verifierPublic] (empty = skip verification). |
| 97 | * Unsigned messages verify cleanly with an empty fingerprint (accepted); |
| 98 | * tampered signatures throw. |
| 99 | */ |
| 100 | fun decryptVerify( |
| 101 | armoredSecret: String, |
| 102 | passphrase: String, |
| 103 | ciphertext: ByteArray, |
| 104 | verifierPublic: String, |
| 105 | ): Verified { |
| 106 | val parts = PgpNative.nativePgpDecryptVerify( |
| 107 | armoredSecret.toUtf8(), |
| 108 | passphrase.toUtf8(), |
| 109 | ciphertext, |
| 110 | verifierPublic.toUtf8(), |
| 111 | ) ?: error("OpenPGP decrypt/verify failed") |
| 112 | require(parts.size == 2) { "decryptVerify returned ${parts.size} parts" } |
| 113 | return Verified( |
| 114 | payload = parts[0], |
| 115 | signerFingerprintHex = parts[1].toUtf8String(), |
| 116 | ) |
| 117 | } |
| 118 | |
| 119 | /** Armors raw PGP bytes as a `PGP MESSAGE` block (for SMTP federation). */ |
| 120 | fun armorMessage(raw: ByteArray): String = |
| 121 | PgpNative.nativePgpArmorMessage(raw)?.toUtf8String() |
| 122 | ?: error("OpenPGP armor failed") |
| 123 | |
| 124 | /** Raw binary detached signature over [data] with the Ed25519 primary. */ |
| 125 | fun signDetached(armoredSecret: String, passphrase: String, data: ByteArray): ByteArray = |
| 126 | PgpNative.nativePgpSignDetached(armoredSecret.toUtf8(), passphrase.toUtf8(), data) |
| 127 | ?: error("OpenPGP detached signing failed") |
| 128 | } |