tholos-pq 0.3.0

Pure-Rust post-quantum multi-recipient encryption: ML-KEM-1024 + ML-DSA-65 + XChaCha20-Poly1305
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Cryptographic operations for encryption, decryption, and key generation.

use std::time::{SystemTime, UNIX_EPOCH};

use chacha20poly1305::aead::{Aead, KeyInit};
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use dilithium::{DilithiumKeyPair, DilithiumSignature, ML_DSA_65};
use hkdf::Hkdf;
use ml_kem::kem::{Decapsulate, Encapsulate};
use ml_kem::{Ciphertext, EncodedSizeUser, KemCore, MlKem1024};
use rand::rngs::OsRng;
use rand::{CryptoRng, RngCore};
use sha2::Sha256;
use zeroize::{Zeroize, Zeroizing};

use crate::errors::TholosError;
use crate::types::*;

/// Recipient private key material.
///
/// The ML-KEM decapsulation key is zeroized on drop (`ml-kem` `zeroize` feature).
pub struct RecipientPriv {
    /// Recipient identifier.
    pub kid: String,
    /// ML-KEM-1024 decapsulation (secret) key.
    pub sk_kyber: <MlKem1024 as KemCore>::DecapsulationKey,
}

/// Sender keypair for signing messages.
///
/// Private key material is held inside [`DilithiumKeyPair`], which zeroizes on drop.
pub struct SenderKeypair {
    /// Sender identifier.
    pub sid: String,
    keypair: DilithiumKeyPair,
}

impl Drop for SenderKeypair {
    fn drop(&mut self) {
        self.sid.zeroize();
    }
}

impl SenderKeypair {
    /// ML-DSA-65 public key bytes.
    pub fn public_key_bytes(&self) -> &[u8] {
        self.keypair.public_key()
    }

    /// ML-DSA-65 private key bytes (for tests / vectors).
    pub fn private_key_bytes(&self) -> &[u8] {
        self.keypair.private_key()
    }
}

/// Options for deterministic / injectable encryption parameters.
pub struct EncryptOptions<'a, R: RngCore + CryptoRng> {
    /// RNG used for CEK, nonces, ML-KEM encapsulation, and signing randomness.
    pub rng: &'a mut R,
    /// Message ID written into the header (normally a UUID).
    pub msg_id: String,
    /// Unix timestamp written into the header.
    pub timestamp_unix: u64,
}

/// Generate a new recipient keypair.
pub fn gen_recipient_keypair(kid: &str) -> (RecipientPub, RecipientPriv) {
    let mut rng = OsRng;
    gen_recipient_keypair_with(kid, &mut rng)
}

/// Generate a recipient keypair using the provided RNG (for tests / vectors).
pub fn gen_recipient_keypair_with<R: RngCore + CryptoRng>(
    kid: &str,
    rng: &mut R,
) -> (RecipientPub, RecipientPriv) {
    let (sk, pk) = MlKem1024::generate(rng);
    let pub_bytes = pk.as_bytes().to_vec();
    (
        RecipientPub {
            kid: kid.to_string(),
            pk_kyber: pub_bytes,
        },
        RecipientPriv {
            kid: kid.to_string(),
            sk_kyber: sk,
        },
    )
}

/// Generate a new sender keypair (ML-DSA-65).
pub fn gen_sender_keypair(sid: &str) -> SenderKeypair {
    #[allow(clippy::expect_used)]
    let keypair = DilithiumKeyPair::generate(ML_DSA_65).expect("ML-DSA-65 key generation failed");
    SenderKeypair {
        sid: sid.to_string(),
        keypair,
    }
}

/// Generate a sender keypair deterministically from a 32-byte seed (tests / vectors).
pub fn gen_sender_keypair_deterministic(sid: &str, seed: &[u8; 32]) -> SenderKeypair {
    SenderKeypair {
        sid: sid.to_string(),
        keypair: DilithiumKeyPair::generate_deterministic(ML_DSA_65, seed),
    }
}

/// Reconstruct a sender keypair from raw ML-DSA-65 key bytes (for tests / vectors).
pub fn sender_keypair_from_bytes(
    sid: &str,
    pk_bytes: &[u8],
    sk_bytes: &[u8],
) -> Result<SenderKeypair, TholosError> {
    let keypair = DilithiumKeyPair::from_keys(sk_bytes, pk_bytes, ML_DSA_65)
        .map_err(|_| TholosError::Malformed("ml-dsa keys"))?;
    Ok(SenderKeypair {
        sid: sid.to_string(),
        keypair,
    })
}

/// Extract the public key information from a sender keypair.
pub fn sender_pub(sender: &SenderKeypair) -> SenderPub {
    SenderPub {
        sid: sender.sid.clone(),
        pk_dilithium: sender.public_key_bytes().to_vec(),
    }
}

fn unix_timestamp_secs() -> Result<u64, TholosError> {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| TholosError::Malformed("system clock before unix epoch"))
        .map(|d| d.as_secs())
}

fn hkdf32(shared: &[u8], kid: &str, header_cbor: &[u8]) -> Zeroizing<[u8; 32]> {
    let hk = Hkdf::<Sha256>::new(Some(kid.as_bytes()), shared);
    let mut okm = Zeroizing::new([0u8; 32]);
    #[allow(clippy::expect_used)]
    hk.expand(header_cbor, okm.as_mut())
        .expect("HKDF expand failed - this should never happen with 32-byte output");
    okm
}

fn aead_enc(
    key: &[u8; 32],
    nonce24: &[u8; 24],
    aad: &[u8],
    pt: &[u8],
) -> Result<Vec<u8>, TholosError> {
    let cipher = XChaCha20Poly1305::new(key.into());
    let nonce = XNonce::from(*nonce24);
    cipher
        .encrypt(&nonce, chacha20poly1305::aead::Payload { msg: pt, aad })
        .map_err(|_| TholosError::Aead)
}

fn aead_dec(
    key: &[u8; 32],
    nonce24: &[u8; 24],
    aad: &[u8],
    ct: &[u8],
) -> Result<Vec<u8>, TholosError> {
    let cipher = XChaCha20Poly1305::new(key.into());
    let nonce = XNonce::from(*nonce24);
    cipher
        .decrypt(&nonce, chacha20poly1305::aead::Payload { msg: ct, aad })
        .map_err(|_| TholosError::Aead)
}

fn expect_self_describe_tag(wire_cbor: &[u8]) -> Result<(), TholosError> {
    if !has_self_describe_tag(wire_cbor) {
        return Err(TholosError::Malformed("wire self-describe tag"));
    }
    Ok(())
}

fn sign_inner<R: RngCore>(
    inner_cbor: &[u8],
    sender: &SenderKeypair,
    rng: &mut R,
) -> Result<Vec<u8>, TholosError> {
    let mut rnd = [0u8; 32];
    rng.fill_bytes(&mut rnd);
    let sig = sender
        .keypair
        .sign_deterministic(inner_cbor, b"", &rnd)
        .map_err(|_| TholosError::Malformed("ml-dsa sign"))?;
    rnd.zeroize();
    Ok(sig.as_bytes().to_vec())
}

fn validate_inner(inner: &BundleUnsigned) -> Result<(), TholosError> {
    if inner.header.v != 1 || inner.header.suite != SUITE_V1 {
        return Err(TholosError::UnsupportedSuite {
            v: inner.header.v,
            suite: inner.header.suite.clone(),
        });
    }

    if inner.header.recipients.len() != inner.recipients.len() {
        return Err(TholosError::Malformed("recipient list mismatch"));
    }

    let mut seen = std::collections::HashSet::new();
    for (header_kid, env) in inner.header.recipients.iter().zip(&inner.recipients) {
        if header_kid != &env.kid {
            return Err(TholosError::Malformed("recipient order mismatch"));
        }
        if !seen.insert(env.kid.clone()) {
            return Err(TholosError::Malformed("duplicate recipient kid"));
        }
    }

    Ok(())
}

fn verify_signed_bundle(
    bundle: &BundleSigned,
    allowed_senders: &[(String, Vec<u8>)],
) -> Result<BundleUnsigned, TholosError> {
    if bundle.inner.is_empty() {
        return Err(TholosError::Malformed("empty inner bundle"));
    }

    let inner: BundleUnsigned = from_cbor(&bundle.inner)?;
    validate_inner(&inner)?;

    let sender_sid = &inner.header.sender;
    let Some((_, pk_bytes)) = allowed_senders.iter().find(|(sid, _)| sid == sender_sid) else {
        return Err(TholosError::BadSignature);
    };

    if pk_bytes.len() != DILITHIUM3_PK_LEN {
        return Err(TholosError::Malformed("dilithium pk"));
    }
    if bundle.sig_dilithium.len() != DILITHIUM3_SIG_LEN {
        return Err(TholosError::Malformed("signature"));
    }

    let sig = DilithiumSignature::from_slice(&bundle.sig_dilithium);
    if !DilithiumKeyPair::verify(pk_bytes, &sig, &bundle.inner, b"", ML_DSA_65) {
        return Err(TholosError::BadSignature);
    }

    Ok(inner)
}

/// Verify a wire bundle and return its authenticated header without decrypting.
pub fn verify_header(
    wire_cbor: &[u8],
    allowed_senders: &[(String, Vec<u8>)],
) -> Result<Header, TholosError> {
    expect_self_describe_tag(wire_cbor)?;
    let bundle: BundleSigned = from_cbor(wire_cbor)?;
    let inner = verify_signed_bundle(&bundle, allowed_senders)?;
    Ok(inner.header)
}

/// Encrypt a message for multiple recipients and sign it with the sender's key.
pub fn encrypt(
    plaintext: &[u8],
    sender: &SenderKeypair,
    recipients: &[RecipientPub],
) -> Result<Vec<u8>, TholosError> {
    let mut rng = OsRng;
    encrypt_with(
        plaintext,
        sender,
        recipients,
        EncryptOptions {
            rng: &mut rng,
            msg_id: uuid::Uuid::new_v4().to_string(),
            timestamp_unix: unix_timestamp_secs()?,
        },
    )
}

/// Encrypt with caller-supplied RNG, message ID, and timestamp.
pub fn encrypt_with<R: RngCore + CryptoRng>(
    plaintext: &[u8],
    sender: &SenderKeypair,
    recipients: &[RecipientPub],
    opts: EncryptOptions<'_, R>,
) -> Result<Vec<u8>, TholosError> {
    if recipients.is_empty() {
        return Err(TholosError::NoRecipients);
    }

    let header = Header {
        v: 1,
        suite: SUITE_V1.to_string(),
        sender: sender.sid.clone(),
        recipients: recipients.iter().map(|r| r.kid.clone()).collect(),
        msg_id: opts.msg_id,
        timestamp_unix: opts.timestamp_unix,
    };
    let header_cbor = to_cbor(&header)?;

    let rng = opts.rng;
    let mut cek = Zeroizing::new([0u8; 32]);
    rng.fill_bytes(cek.as_mut());

    let mut pay_nonce = [0u8; 24];
    rng.fill_bytes(&mut pay_nonce);
    let ciphertext = aead_enc(&cek, &pay_nonce, &header_cbor, plaintext)?;

    let mut envs = Vec::with_capacity(recipients.len());
    for r in recipients {
        let pk_bytes: &[u8] = &r.pk_kyber;
        let pk = <MlKem1024 as KemCore>::EncapsulationKey::from_bytes(
            &pk_bytes
                .try_into()
                .map_err(|_| TholosError::Malformed("ml-kem pk"))?,
        );
        let (kem_ct, shared) = pk
            .encapsulate(rng)
            .map_err(|_| TholosError::Malformed("encapsulation"))?;

        let kek = hkdf32(shared.as_slice(), &r.kid, &header_cbor);

        let mut wrap_nonce = [0u8; 24];
        rng.fill_bytes(&mut wrap_nonce);
        let wrapped_cek = aead_enc(&kek, &wrap_nonce, &header_cbor, cek.as_ref())?;

        envs.push(RecipientEnvelope {
            kid: r.kid.clone(),
            kem_ct: kem_ct.as_slice().to_vec(),
            wrap_nonce: wrap_nonce.to_vec(),
            wrapped_cek,
        });
    }

    let inner = BundleUnsigned {
        header,
        pay_nonce: pay_nonce.to_vec(),
        ciphertext,
        recipients: envs,
    };

    let inner_cbor = to_cbor(&inner)?;
    let sig_dilithium = sign_inner(&inner_cbor, sender, rng)?;

    let bundle = BundleSigned {
        inner: inner_cbor,
        sig_dilithium,
    };

    to_cbor(&bundle)
}

/// Decrypt a message as a recipient and verify the sender's signature.
pub fn decrypt(
    wire_cbor: &[u8],
    my_kid: &str,
    my_sk: &<MlKem1024 as KemCore>::DecapsulationKey,
    allowed_senders: &[(String, Vec<u8>)],
) -> Result<Vec<u8>, TholosError> {
    expect_self_describe_tag(wire_cbor)?;
    let bundle: BundleSigned = from_cbor(wire_cbor)?;
    let inner = verify_signed_bundle(&bundle, allowed_senders)?;

    let env = inner
        .recipients
        .iter()
        .find(|e| e.kid == my_kid)
        .ok_or_else(|| TholosError::MissingEnvelope(my_kid.to_string()))?;

    if env.wrap_nonce.len() != 24 {
        return Err(TholosError::Malformed("wrap nonce"));
    }
    let kem_ct_bytes: &[u8] = &env.kem_ct;
    let kem_ct: Ciphertext<MlKem1024> = kem_ct_bytes
        .try_into()
        .map_err(|_| TholosError::Malformed("kem_ct"))?;
    let shared = my_sk
        .decapsulate(&kem_ct)
        .map_err(|_| TholosError::Malformed("decapsulation"))?;

    let header_cbor = to_cbor(&inner.header)?;
    let kek = hkdf32(shared.as_slice(), my_kid, &header_cbor);

    let mut wrap_nonce = [0u8; 24];
    wrap_nonce.copy_from_slice(&env.wrap_nonce);
    let cek = aead_dec(&kek, &wrap_nonce, &header_cbor, &env.wrapped_cek)?;

    if cek.len() != 32 {
        return Err(TholosError::Malformed("cek length"));
    }
    let mut cek_arr = Zeroizing::new([0u8; 32]);
    cek_arr.copy_from_slice(&cek);

    if inner.pay_nonce.len() != 24 {
        return Err(TholosError::Malformed("pay nonce"));
    }
    let mut pay_nonce = [0u8; 24];
    pay_nonce.copy_from_slice(&inner.pay_nonce);

    aead_dec(&cek_arr, &pay_nonce, &header_cbor, &inner.ciphertext)
}

pub(crate) mod test_support {
    use super::*;

    pub fn resign_bundle(
        inner: &BundleUnsigned,
        sender: &SenderKeypair,
    ) -> Result<BundleSigned, TholosError> {
        let inner_cbor = to_cbor(inner)?;
        let mut rng = OsRng;
        let sig_dilithium = sign_inner(&inner_cbor, sender, &mut rng)?;
        Ok(BundleSigned {
            inner: inner_cbor,
            sig_dilithium,
        })
    }

    pub fn encode_bundle(bundle: &BundleSigned) -> Result<Vec<u8>, TholosError> {
        to_cbor(bundle)
    }

    pub fn decode_bundle(wire: &[u8]) -> Result<BundleSigned, TholosError> {
        from_cbor(wire)
    }

    pub fn decode_inner(bytes: &[u8]) -> Result<BundleUnsigned, TholosError> {
        from_cbor(bytes)
    }

    pub fn encode_cbor<T: serde::Serialize>(v: &T) -> Result<Vec<u8>, TholosError> {
        to_cbor(v)
    }

    pub fn decode_cbor<T: serde::de::DeserializeOwned>(data: &[u8]) -> Result<T, TholosError> {
        from_cbor(data)
    }
}