oqs-safe 0.6.0

Post-Quantum Cryptography (PQC) toolkit in Rust with ML-KEM, ML-DSA, hybrid cryptography (X25519 + ML-KEM), and secure session primitives.
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use crate::{
    hybrid::derive_hybrid_secret,
    kem::{Kem, KemAlgorithm, KemInstance, SecretKey},
    session::SecureSession,
    OqsError,
};

use rand_core::OsRng;
use sha2::{Digest, Sha256};
use x25519_dalek::{PublicKey as X25519PublicKey, StaticSecret};

const HANDSHAKE_CONTEXT: &[u8] = b"oqs-safe-v0.5.0-hybrid-handshake";
const HANDSHAKE_TRANSCRIPT_DOMAIN: &[u8] = b"oqs-safe-v0.6.0-handshake-transcript";

#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Clone, Debug)]
pub struct ClientHello {
    pub client_x25519_public: Vec<u8>,
    pub client_kem_public: Vec<u8>,
}

#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Clone, Debug)]
pub struct ServerHello {
    pub server_x25519_public: Vec<u8>,
    pub kem_ciphertext: Vec<u8>,
}

#[cfg(feature = "serialization")]
impl ClientHello {
    pub fn to_bytes(&self) -> Result<Vec<u8>, HandshakeError> {
        bincode::serialize(self).map_err(|_| HandshakeError::InvalidHandshakeState)
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<Self, HandshakeError> {
        bincode::deserialize(bytes).map_err(|_| HandshakeError::InvalidHandshakeState)
    }
}

#[cfg(feature = "serialization")]
impl ServerHello {
    pub fn to_bytes(&self) -> Result<Vec<u8>, HandshakeError> {
        bincode::serialize(self).map_err(|_| HandshakeError::InvalidHandshakeState)
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<Self, HandshakeError> {
        bincode::deserialize(bytes).map_err(|_| HandshakeError::InvalidHandshakeState)
    }
}

#[derive(Debug, Clone)]
pub struct HandshakeTranscript {
    hasher: Sha256,
}

impl HandshakeTranscript {
    pub fn new() -> Self {
        let mut hasher = Sha256::new();
        hasher.update(HANDSHAKE_TRANSCRIPT_DOMAIN);
        Self { hasher }
    }

    pub fn update_labelled(&mut self, label: &[u8], data: &[u8]) {
        update_len_prefixed(&mut self.hasher, label);
        update_len_prefixed(&mut self.hasher, data);
    }

    pub fn update_algorithm(&mut self, algorithm: KemAlgorithm) {
        self.update_labelled(b"kem_algorithm", format!("{algorithm:?}").as_bytes());
    }

    pub fn update_client_hello(&mut self, client_hello: &ClientHello) {
        self.update_labelled(b"client_x25519_public", &client_hello.client_x25519_public);
        self.update_labelled(b"client_kem_public", &client_hello.client_kem_public);
    }

    pub fn update_server_hello(&mut self, server_hello: &ServerHello) {
        self.update_labelled(b"server_x25519_public", &server_hello.server_x25519_public);
        self.update_labelled(b"kem_ciphertext", &server_hello.kem_ciphertext);
    }

    pub fn finalize(self) -> [u8; 32] {
        let digest = self.hasher.finalize();
        let mut out = [0u8; 32];
        out.copy_from_slice(&digest);
        out
    }
}

impl Default for HandshakeTranscript {
    fn default() -> Self {
        Self::new()
    }
}

fn update_len_prefixed(hasher: &mut Sha256, data: &[u8]) {
    hasher.update((data.len() as u64).to_be_bytes());
    hasher.update(data);
}

fn transcript_bound_context(transcript_hash: &[u8; 32]) -> Vec<u8> {
    let mut context = Vec::with_capacity(HANDSHAKE_CONTEXT.len() + transcript_hash.len());
    context.extend_from_slice(HANDSHAKE_CONTEXT);
    context.extend_from_slice(transcript_hash);
    context
}

#[derive(Debug)]
pub enum HandshakeError {
    MissingClientState,
    MissingServerState,
    InvalidHandshakeState,
    CryptoError(OqsError),
}

impl core::fmt::Display for HandshakeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            HandshakeError::MissingClientState => write!(f, "missing client handshake state"),
            HandshakeError::MissingServerState => write!(f, "missing server handshake state"),
            HandshakeError::InvalidHandshakeState => write!(f, "invalid handshake state"),
            HandshakeError::CryptoError(err) => write!(f, "cryptographic operation failed: {err}"),
        }
    }
}

impl std::error::Error for HandshakeError {}

impl From<OqsError> for HandshakeError {
    fn from(value: OqsError) -> Self {
        HandshakeError::CryptoError(value)
    }
}

pub struct HybridClient {
    kem: KemInstance,
    state: Option<ClientHandshakeState>,
}

struct ClientHandshakeState {
    x25519_secret: StaticSecret,
    kem_secret: SecretKey,
    client_hello: ClientHello,
}

impl HybridClient {
    pub fn new() -> Self {
        Self {
            kem: KemInstance::new(KemAlgorithm::MlKem768),
            state: None,
        }
    }

    pub fn with_algorithm(algorithm: KemAlgorithm) -> Self {
        Self {
            kem: KemInstance::new(algorithm),
            state: None,
        }
    }

    pub fn start_handshake(&mut self) -> Result<ClientHello, HandshakeError> {
        let client_x25519_secret = StaticSecret::random_from_rng(OsRng);
        let client_x25519_public = X25519PublicKey::from(&client_x25519_secret);

        let (client_kem_public, client_kem_secret) = self.kem.keypair()?;

        let client_hello = ClientHello {
            client_x25519_public: client_x25519_public.as_bytes().to_vec(),
            client_kem_public: client_kem_public.as_bytes().to_vec(),
        };

        self.state = Some(ClientHandshakeState {
            x25519_secret: client_x25519_secret,
            kem_secret: client_kem_secret,
            client_hello: client_hello.clone(),
        });

        Ok(client_hello)
    }

    pub fn finish(&mut self, server_hello: ServerHello) -> Result<SecureSession, HandshakeError> {
        let state = self
            .state
            .take()
            .ok_or(HandshakeError::MissingClientState)?;

        if server_hello.server_x25519_public.len() != 32 || server_hello.kem_ciphertext.is_empty() {
            return Err(HandshakeError::InvalidHandshakeState);
        }

        let server_public_bytes: [u8; 32] = server_hello
            .server_x25519_public
            .as_slice()
            .try_into()
            .map_err(|_| HandshakeError::InvalidHandshakeState)?;

        let server_x25519_public = X25519PublicKey::from(server_public_bytes);
        let classical_secret = state.x25519_secret.diffie_hellman(&server_x25519_public);

        let pqc_secret = client_pqc_secret(
            self.kem.algorithm(),
            &server_hello.kem_ciphertext,
            &state.kem_secret,
        )?;

        let mut transcript = HandshakeTranscript::new();
        transcript.update_algorithm(self.kem.algorithm());
        transcript.update_client_hello(&state.client_hello);
        transcript.update_server_hello(&server_hello);
        let transcript_hash = transcript.finalize();

        let context = transcript_bound_context(&transcript_hash);

        let hybrid_secret = derive_hybrid_secret(
            pqc_secret.as_slice(),
            classical_secret.as_bytes(),
            context.as_slice(),
        );

        Ok(SecureSession::new(hybrid_secret.as_bytes().to_vec()))
    }
}

impl Default for HybridClient {
    fn default() -> Self {
        Self::new()
    }
}

pub struct HybridServer {
    kem: KemInstance,
    session: Option<SecureSession>,
}

impl HybridServer {
    pub fn new() -> Self {
        Self {
            kem: KemInstance::new(KemAlgorithm::MlKem768),
            session: None,
        }
    }

    pub fn with_algorithm(algorithm: KemAlgorithm) -> Self {
        Self {
            kem: KemInstance::new(algorithm),
            session: None,
        }
    }

    pub fn respond(&mut self, client_hello: ClientHello) -> Result<ServerHello, HandshakeError> {
        if client_hello.client_x25519_public.len() != 32
            || client_hello.client_kem_public.is_empty()
        {
            return Err(HandshakeError::InvalidHandshakeState);
        }

        let client_public_bytes: [u8; 32] = client_hello
            .client_x25519_public
            .as_slice()
            .try_into()
            .map_err(|_| HandshakeError::InvalidHandshakeState)?;

        let client_x25519_public = X25519PublicKey::from(client_public_bytes);

        let server_x25519_secret = StaticSecret::random_from_rng(OsRng);
        let server_x25519_public = X25519PublicKey::from(&server_x25519_secret);

        let classical_secret = server_x25519_secret.diffie_hellman(&client_x25519_public);

        let (kem_ciphertext, pqc_secret) =
            server_pqc_secret(self.kem.algorithm(), &client_hello.client_kem_public)?;

        let server_hello = ServerHello {
            server_x25519_public: server_x25519_public.as_bytes().to_vec(),
            kem_ciphertext,
        };

        let mut transcript = HandshakeTranscript::new();
        transcript.update_algorithm(self.kem.algorithm());
        transcript.update_client_hello(&client_hello);
        transcript.update_server_hello(&server_hello);
        let transcript_hash = transcript.finalize();

        let context = transcript_bound_context(&transcript_hash);

        let hybrid_secret = derive_hybrid_secret(
            pqc_secret.as_slice(),
            classical_secret.as_bytes(),
            context.as_slice(),
        );

        self.session = Some(SecureSession::new(hybrid_secret.as_bytes().to_vec()));

        Ok(server_hello)
    }

    pub fn session(&self) -> Result<&SecureSession, HandshakeError> {
        self.session
            .as_ref()
            .ok_or(HandshakeError::MissingServerState)
    }
}

impl Default for HybridServer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "liboqs")]
fn server_pqc_secret(
    algorithm: KemAlgorithm,
    client_kem_public: &[u8],
) -> Result<(Vec<u8>, Vec<u8>), HandshakeError> {
    use crate::kem::PublicKey;

    let kem = KemInstance::new(algorithm);
    let client_public_key = PublicKey::new(algorithm, client_kem_public.to_vec());

    let (ciphertext, shared_secret) = kem.encapsulate(&client_public_key)?;

    Ok((
        ciphertext.as_bytes().to_vec(),
        shared_secret.as_bytes().to_vec(),
    ))
}

#[cfg(feature = "liboqs")]
fn client_pqc_secret(
    algorithm: KemAlgorithm,
    kem_ciphertext: &[u8],
    kem_secret: &SecretKey,
) -> Result<Vec<u8>, HandshakeError> {
    use crate::kem::Ciphertext;

    let kem = KemInstance::new(algorithm);
    let ciphertext = Ciphertext::new(algorithm, kem_ciphertext.to_vec());

    let shared_secret = kem.decapsulate(&ciphertext, kem_secret)?;

    Ok(shared_secret.as_bytes().to_vec())
}

#[cfg(not(feature = "liboqs"))]
fn server_pqc_secret(
    algorithm: KemAlgorithm,
    client_kem_public: &[u8],
) -> Result<(Vec<u8>, Vec<u8>), HandshakeError> {
    let ciphertext = mock_ciphertext(algorithm, client_kem_public);
    let shared_secret = mock_shared_secret(algorithm, &ciphertext);

    Ok((ciphertext, shared_secret))
}

#[cfg(not(feature = "liboqs"))]
fn client_pqc_secret(
    algorithm: KemAlgorithm,
    kem_ciphertext: &[u8],
    kem_secret: &SecretKey,
) -> Result<Vec<u8>, HandshakeError> {
    let _ = kem_secret;

    Ok(mock_shared_secret(algorithm, kem_ciphertext))
}

#[cfg(not(feature = "liboqs"))]
fn mock_ciphertext(algorithm: KemAlgorithm, client_kem_public: &[u8]) -> Vec<u8> {
    let mut ciphertext = vec![0u8; algorithm.ciphertext_len()];
    let mut counter = 0u64;
    let mut offset = 0usize;

    while offset < ciphertext.len() {
        let mut hasher = Sha256::new();
        hasher.update(b"oqs-safe-v0.5.0-mock-ciphertext");
        hasher.update(client_kem_public);
        hasher.update(counter.to_le_bytes());

        let block = hasher.finalize();
        let take = core::cmp::min(block.len(), ciphertext.len() - offset);

        ciphertext[offset..offset + take].copy_from_slice(&block[..take]);
        offset += take;
        counter += 1;
    }

    ciphertext
}

#[cfg(not(feature = "liboqs"))]
fn mock_shared_secret(algorithm: KemAlgorithm, kem_ciphertext: &[u8]) -> Vec<u8> {
    let mut hasher = Sha256::new();

    hasher.update(b"oqs-safe-v0.5.0-mock-pqc-secret");
    hasher.update(format!("{algorithm:?}").as_bytes());
    hasher.update(kem_ciphertext);

    hasher.finalize().to_vec()
}

#[cfg(test)]
mod transcript_tests {
    use super::*;

    #[test]
    fn same_transcript_inputs_produce_same_hash() {
        let client_hello = ClientHello {
            client_x25519_public: vec![1; 32],
            client_kem_public: vec![2; 1184],
        };

        let server_hello = ServerHello {
            server_x25519_public: vec![3; 32],
            kem_ciphertext: vec![4; 1088],
        };

        let mut t1 = HandshakeTranscript::new();
        t1.update_algorithm(KemAlgorithm::MlKem768);
        t1.update_client_hello(&client_hello);
        t1.update_server_hello(&server_hello);

        let mut t2 = HandshakeTranscript::new();
        t2.update_algorithm(KemAlgorithm::MlKem768);
        t2.update_client_hello(&client_hello);
        t2.update_server_hello(&server_hello);

        assert_eq!(t1.finalize(), t2.finalize());
    }

    #[test]
    fn transcript_hash_changes_when_client_hello_changes() {
        let client_hello_a = ClientHello {
            client_x25519_public: vec![1; 32],
            client_kem_public: vec![2; 1184],
        };

        let client_hello_b = ClientHello {
            client_x25519_public: vec![9; 32],
            client_kem_public: vec![2; 1184],
        };

        let server_hello = ServerHello {
            server_x25519_public: vec![3; 32],
            kem_ciphertext: vec![4; 1088],
        };

        let mut t1 = HandshakeTranscript::new();
        t1.update_algorithm(KemAlgorithm::MlKem768);
        t1.update_client_hello(&client_hello_a);
        t1.update_server_hello(&server_hello);

        let mut t2 = HandshakeTranscript::new();
        t2.update_algorithm(KemAlgorithm::MlKem768);
        t2.update_client_hello(&client_hello_b);
        t2.update_server_hello(&server_hello);

        assert_ne!(t1.finalize(), t2.finalize());
    }

    #[test]
    fn transcript_hash_changes_when_server_hello_changes() {
        let client_hello = ClientHello {
            client_x25519_public: vec![1; 32],
            client_kem_public: vec![2; 1184],
        };

        let server_hello_a = ServerHello {
            server_x25519_public: vec![3; 32],
            kem_ciphertext: vec![4; 1088],
        };

        let server_hello_b = ServerHello {
            server_x25519_public: vec![3; 32],
            kem_ciphertext: vec![8; 1088],
        };

        let mut t1 = HandshakeTranscript::new();
        t1.update_algorithm(KemAlgorithm::MlKem768);
        t1.update_client_hello(&client_hello);
        t1.update_server_hello(&server_hello_a);

        let mut t2 = HandshakeTranscript::new();
        t2.update_algorithm(KemAlgorithm::MlKem768);
        t2.update_client_hello(&client_hello);
        t2.update_server_hello(&server_hello_b);

        assert_ne!(t1.finalize(), t2.finalize());
    }

    #[test]
    fn transcript_hash_changes_when_algorithm_changes() {
        let client_hello = ClientHello {
            client_x25519_public: vec![1; 32],
            client_kem_public: vec![2; 1184],
        };

        let server_hello = ServerHello {
            server_x25519_public: vec![3; 32],
            kem_ciphertext: vec![4; 1088],
        };

        let mut t1 = HandshakeTranscript::new();
        t1.update_algorithm(KemAlgorithm::MlKem512);
        t1.update_client_hello(&client_hello);
        t1.update_server_hello(&server_hello);

        let mut t2 = HandshakeTranscript::new();
        t2.update_algorithm(KemAlgorithm::MlKem768);
        t2.update_client_hello(&client_hello);
        t2.update_server_hello(&server_hello);

        assert_ne!(t1.finalize(), t2.finalize());
    }

    #[test]
    fn transcript_bound_context_is_deterministic() {
        let transcript_hash = [7u8; 32];

        let context_a = transcript_bound_context(&transcript_hash);
        let context_b = transcript_bound_context(&transcript_hash);

        assert_eq!(context_a, context_b);
        assert!(context_a.starts_with(HANDSHAKE_CONTEXT));
        assert!(context_a.ends_with(&transcript_hash));
    }
}

#[cfg(all(test, feature = "serialization"))]
mod serialization_tests {
    use super::*;

    #[test]
    fn client_hello_roundtrips_through_bytes() {
        let client_hello = ClientHello {
            client_x25519_public: vec![1; 32],
            client_kem_public: vec![2; 1184],
        };

        let encoded = client_hello
            .to_bytes()
            .expect("client hello should serialize");

        let decoded = ClientHello::from_bytes(&encoded).expect("client hello should deserialize");

        assert_eq!(
            decoded.client_x25519_public,
            client_hello.client_x25519_public
        );
        assert_eq!(decoded.client_kem_public, client_hello.client_kem_public);
    }

    #[test]
    fn server_hello_roundtrips_through_bytes() {
        let server_hello = ServerHello {
            server_x25519_public: vec![3; 32],
            kem_ciphertext: vec![4; 1088],
        };

        let encoded = server_hello
            .to_bytes()
            .expect("server hello should serialize");

        let decoded = ServerHello::from_bytes(&encoded).expect("server hello should deserialize");

        assert_eq!(
            decoded.server_x25519_public,
            server_hello.server_x25519_public
        );
        assert_eq!(decoded.kem_ciphertext, server_hello.kem_ciphertext);
    }

    #[test]
    fn invalid_client_hello_bytes_fail_to_deserialize() {
        let invalid = b"not-a-valid-client-hello";

        assert!(ClientHello::from_bytes(invalid).is_err());
    }

    #[test]
    fn invalid_server_hello_bytes_fail_to_deserialize() {
        let invalid = b"not-a-valid-server-hello";

        assert!(ServerHello::from_bytes(invalid).is_err());
    }
}