aboutsummaryrefslogtreecommitdiff
path: root/crates/chat-crypto/src/lib.rs
blob: 62319e2fad73a66af8767030ce2b0e09a2054f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (C) 2026 Doan Luu Duc Tai <doan@luuduct.ai>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#![doc = "Experimental Sequoia post-quantum browser compatibility probe."]

use anyhow::{Context, Result, ensure};
use pgp::Profile;
use pgp::cert::prelude::*;
use pgp::crypto::{Password, SessionKey};
use pgp::parse::{
    Parse,
    stream::{
        DecryptionHelper, DecryptorBuilder, MessageLayer, MessageStructure, VerificationHelper,
    },
};
use pgp::policy::StandardPolicy;
use pgp::serialize::{
    Serialize,
    stream::{Encryptor, LiteralWriter, Message, Signer},
};
use pgp::types::{KeyFlags, SymmetricAlgorithm};
use sequoia_openpgp as pgp;
use std::io::Write;

const PASSWORD: &str = "compatibility-test-only-not-a-real-secret";
const TEXT: &[u8] = "stack-chat/v1: UTF-8 message — doan ↔ visitor".as_bytes();

#[doc = "Exercises v6 PQ generation, protected-key import, signing and two-recipient encryption."]
#[doc = "# Errors"]
#[doc = "Returns a diagnostic if a required operation or integrity check fails."]
pub fn compatibility_probe() -> std::result::Result<String, String> {
    probe().map_err(|error| format!("{error:#}"))
}

fn identity() -> Result<Cert> {
    let (cert, _revocation) = CertBuilder::new()
        .set_profile(Profile::RFC9580)?
        .set_cipher_suite(CipherSuite::MLDSA87_Ed448)
        .set_primary_key_flags(KeyFlags::empty().set_certification().set_signing())
        .add_transport_encryption_subkey()
        .set_password(Some(Password::from(PASSWORD)))
        .generate()?;
    let mut encoded = Vec::new();
    cert.as_tsk().serialize(&mut encoded)?;
    Cert::from_bytes(&encoded)
}

fn encrypt(sender: &Cert, other: &Cert) -> Result<Vec<u8>> {
    let policy = StandardPolicy::new();
    let recipients = [sender, other].into_iter().flat_map(|cert| {
        cert.keys()
            .with_policy(&policy, None)
            .supported()
            .alive()
            .revoked(false)
            .for_transport_encryption()
    });
    let signer = sender
        .primary_key()
        .key()
        .parts_as_secret()?
        .clone()
        .decrypt_secret(&Password::from(PASSWORD))?
        .into_keypair()?;
    let mut ciphertext = Vec::new();
    let message = Message::new(&mut ciphertext);
    let encrypted = Encryptor::for_recipients(message, recipients)
        .symmetric_algo(SymmetricAlgorithm::AES256)
        .build()?;
    let authenticated_stream = Signer::new(encrypted, signer)?.build()?;
    let mut literal = LiteralWriter::new(authenticated_stream).build()?;
    literal.write_all(TEXT)?;
    literal.finalize()?;
    Ok(ciphertext)
}

struct Helper<'a> {
    recipient: &'a Cert,
    sender: &'a Cert,
    password: &'a str,
}

impl VerificationHelper for Helper<'_> {
    fn get_certs(&mut self, _ids: &[pgp::KeyHandle]) -> Result<Vec<Cert>> {
        Ok(vec![self.sender.clone().strip_secret_key_material()])
    }
    fn check(&mut self, structure: MessageStructure<'_>) -> Result<()> {
        let mut signed = false;
        for layer in structure {
            if let MessageLayer::SignatureGroup { results } = layer {
                ensure!(!results.is_empty(), "Missing signature");
                for result in results {
                    result.map_err(|error| {
                        anyhow::anyhow!("Signature verification failed: {error}")
                    })?;
                }
                signed = true;
            }
        }
        ensure!(signed, "Unsigned message rejected");
        Ok(())
    }
}

impl DecryptionHelper for Helper<'_> {
    fn decrypt(
        &mut self,
        pkesks: &[pgp::packet::PKESK],
        _skesks: &[pgp::packet::SKESK],
        algorithm: Option<SymmetricAlgorithm>,
        decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool,
    ) -> Result<Option<Cert>> {
        let policy = StandardPolicy::new();
        let key = self
            .recipient
            .keys()
            .secret()
            .with_policy(&policy, None)
            .for_transport_encryption()
            .next()
            .context("Missing encryption subkey")?
            .key()
            .clone();
        let mut pair = key
            .decrypt_secret(&Password::from(self.password))?
            .into_keypair()?;
        for packet in pkesks {
            if packet
                .decrypt(&mut pair, algorithm)
                .is_some_and(|(cipher, key)| decrypt(cipher, &key))
            {
                return Ok(Some(self.recipient.clone().strip_secret_key_material()));
            }
        }
        anyhow::bail!("No decryptable recipient packet")
    }
}

fn decrypt(ciphertext: &[u8], recipient: &Cert, sender: &Cert, password: &str) -> Result<Vec<u8>> {
    let policy = StandardPolicy::new();
    let helper = Helper {
        recipient,
        sender,
        password,
    };
    let mut reader =
        DecryptorBuilder::from_bytes(ciphertext)?.with_policy(&policy, None, helper)?;
    let mut plaintext = Vec::new();
    std::io::copy(&mut reader, &mut plaintext)?;
    Ok(plaintext)
}

fn probe() -> Result<String> {
    let backend = pgp::crypto::backend();
    ensure!(
        backend.starts_with("OpenSSL "),
        "Expected OpenSSL backend, got {backend}"
    );
    let sender = identity()?;
    let visitor = identity()?;
    ensure!(
        sender.primary_key().key().pk_algo() == pgp::types::PublicKeyAlgorithm::MLDSA87_Ed448,
        "Unexpected signing algorithm"
    );
    ensure!(
        sender.primary_key().key().version() == 6,
        "Expected v6 identity"
    );
    ensure!(
        sender
            .keys()
            .any(|key| key.key().pk_algo() == pgp::types::PublicKeyAlgorithm::MLKEM1024_X448),
        "Missing ML-KEM-1024+X448"
    );
    let ciphertext = encrypt(&sender, &visitor)?;
    ensure!(
        decrypt(&ciphertext, &sender, &sender, PASSWORD)? == TEXT,
        "Sender round-trip mismatch"
    );
    ensure!(
        decrypt(&ciphertext, &visitor, &sender, PASSWORD)? == TEXT,
        "Visitor round-trip mismatch"
    );
    ensure!(
        decrypt(&ciphertext, &visitor, &sender, "wrong-password").is_err(),
        "Wrong password accepted"
    );
    ensure!(
        decrypt(&ciphertext, &visitor, &visitor, PASSWORD).is_err(),
        "Wrong signer accepted"
    );
    let mut altered = ciphertext;
    let byte = altered.last_mut().context("Empty ciphertext")?;
    *byte ^= 1;
    ensure!(
        decrypt(&altered, &visitor, &sender, PASSWORD).is_err(),
        "Tampered ciphertext accepted"
    );
    Ok(format!(
        "PASS: backend={backend}; RFC 9980 ML-KEM-1024+X448, ML-DSA-87+Ed448, v6 keys, protected key import, both recipients, wrong password, wrong signer, tampering"
    ))
}

#[cfg(test)]
mod tests {
    #[test]
    fn required_crypto_round_trip() -> anyhow::Result<()> {
        super::probe()?;
        Ok(())
    }
}