Skip to content

Commit cb6b8c8

Browse files
authored
ml-kem: redact secret key material from Debug output (#394)
`DecapsulationKey` and `DecryptionKey` derived `Debug`, which could expose secret key material when a key was printed or logged. This changes `DecapsulationKey` to show only the public `ek`, following the approach used in `x-wing`. `DecryptionKey` now prints as `DecryptionKey { .. }`. No changes to the algorithms or public API. Added a regression test covering ML-KEM-512, ML-KEM-768, and ML-KEM-1024.
1 parent 593d288 commit cb6b8c8

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎ml-kem/src/decapsulation_key.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use array::{
88
Array, ArraySize,
99
sizes::{U32, U64},
1010
};
11+
use core::fmt;
1112
use kem::{
1213
Ciphertext, Decapsulate, Decapsulator, Generate, InvalidKey, Kem, KeyExport, KeyInit,
1314
KeySizeUser,
@@ -23,7 +24,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
2324

2425
/// A `DecapsulationKey` provides the ability to generate a new key pair, and decapsulate an
2526
/// encapsulated shared key.
26-
#[derive(Clone, Debug)]
27+
#[derive(Clone)]
2728
pub struct DecapsulationKey<P>
2829
where
2930
P: KemParams,
@@ -128,6 +129,18 @@ where
128129
}
129130
}
130131

132+
// Only show the public encapsulation key so secrets don't end up in logs
133+
impl<P> fmt::Debug for DecapsulationKey<P>
134+
where
135+
P: KemParams,
136+
{
137+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138+
f.debug_struct("DecapsulationKey")
139+
.field("ek", &self.ek)
140+
.finish_non_exhaustive()
141+
}
142+
}
143+
131144
// Handwritten to omit `d` in the comparisons, so keys initialized from seeds compare equally to
132145
// keys initialized from the expanded form
133146
impl<P> PartialEq for DecapsulationKey<P>

‎ml-kem/src/pke.rs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::compress::Compress;
77
use crate::crypto::{G, PRF};
88
use crate::param::{EncodedDecryptionKey, EncodedEncryptionKey, PkeParams};
99
use array::typenum::{U1, Unsigned};
10+
use core::fmt;
1011
use kem::{Ciphertext, InvalidKey};
1112
use module_lattice::{
1213
Encode,
@@ -18,14 +19,24 @@ use zeroize::Zeroize;
1819

1920
/// A `DecryptionKey` provides the ability to generate a new key pair, and decrypt an
2021
/// encrypted value.
21-
#[derive(Clone, Default, Debug)]
22+
#[derive(Clone, Default)]
2223
pub(crate) struct DecryptionKey<P>
2324
where
2425
P: PkeParams,
2526
{
2627
s_hat: NttVector<P::K>,
2728
}
2829

30+
// Don't print the secret vector `s_hat`
31+
impl<P> fmt::Debug for DecryptionKey<P>
32+
where
33+
P: PkeParams,
34+
{
35+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36+
f.debug_struct("DecryptionKey").finish_non_exhaustive()
37+
}
38+
}
39+
2940
impl<P> CtEq for DecryptionKey<P>
3041
where
3142
P: PkeParams,

‎ml-kem/tests/debug.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! `Debug` output tests.
2+
3+
use core::fmt::Debug;
4+
use ml_kem::{Kem, MlKem512, MlKem768, MlKem1024, Seed, kem::Decapsulator};
5+
6+
fn debug_redaction_test<K>()
7+
where
8+
K: Kem,
9+
K::DecapsulationKey: Debug + From<Seed>,
10+
{
11+
let dk = K::DecapsulationKey::from(Seed::from([0x42; 64]));
12+
let expected = format!(
13+
"DecapsulationKey {{ ek: {:?}, .. }}",
14+
dk.encapsulation_key()
15+
);
16+
17+
// Not `assert_eq!`, which would print the secret key if this fails
18+
assert!(
19+
format!("{dk:?}") == expected,
20+
"`Debug` output should only contain the encapsulation key"
21+
);
22+
}
23+
24+
#[test]
25+
fn debug_redaction() {
26+
debug_redaction_test::<MlKem512>();
27+
debug_redaction_test::<MlKem768>();
28+
debug_redaction_test::<MlKem1024>();
29+
}

0 commit comments

Comments
 (0)