Skip to content

Commit ea293fa

Browse files
committed
ml-kem: harden equality testing; expand cargo checkct driver
Removes secret-dependent short circuiting by comparing `z` with `CtEq`. Additionally expands `cargo checkct` to cover this functionality.
1 parent 675f93f commit ea293fa

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎ml-kem/checkct/driver/src/driver.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,46 @@ macro_rules! check_decapsulation {
7272
check_decapsulation!(decapsulate_512, MlKem512, 768, "fixtures/ek512.bin");
7373
check_decapsulation!(decapsulate_768, MlKem768, 1152, "fixtures/ek768.bin");
7474
check_decapsulation!(decapsulate_1024, MlKem1024, 1536, "fixtures/ek1024.bin");
75+
76+
macro_rules! check_key_equality {
77+
($name:ident, $kem:ident, $sk_len:expr, $fixture:literal) => {
78+
#[checkct]
79+
#[allow(deprecated)]
80+
pub fn $name() {
81+
use ml_kem::{DecapsulationKey, ExpandedDecapsulationKey};
82+
83+
black_box(PublicRng.next_u32());
84+
85+
// Fix the public key and H(ek), allowing both equal and unequal
86+
// secret components without symbolic public-key expansion.
87+
let public = include_bytes!($fixture);
88+
let mut expanded = ExpandedDecapsulationKey::<ml_kem::$kem>::default();
89+
expanded[$sk_len..$sk_len + public.len()].copy_from_slice(public);
90+
let reference = DecapsulationKey::<ml_kem::$kem>::from_expanded(&expanded).unwrap();
91+
92+
PrivateRng.fill_bytes(&mut expanded[..$sk_len]);
93+
PrivateRng.fill_bytes(&mut expanded[$sk_len + public.len()..]);
94+
let secret = DecapsulationKey::<ml_kem::$kem>::from_expanded(&expanded).unwrap();
95+
black_box(reference == secret);
96+
}
97+
};
98+
}
99+
100+
check_key_equality!(
101+
decapsulation_key_eq_512,
102+
MlKem512,
103+
768,
104+
"fixtures/ek512.bin"
105+
);
106+
check_key_equality!(
107+
decapsulation_key_eq_768,
108+
MlKem768,
109+
1152,
110+
"fixtures/ek768.bin"
111+
);
112+
check_key_equality!(
113+
decapsulation_key_eq_1024,
114+
MlKem1024,
115+
1536,
116+
"fixtures/ek1024.bin"
117+
);

‎ml-kem/src/decapsulation_key.rs‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use kem::{
1515
};
1616
use module_lattice::{
1717
MaybeBox,
18-
ctutils::{CtEq, CtSelect},
18+
ctutils::{Choice, CtEq, CtSelect},
1919
};
2020
use rand_core::{TryCryptoRng, TryRng};
2121

@@ -148,7 +148,9 @@ where
148148
P: KemParams,
149149
{
150150
fn eq(&self, other: &Self) -> bool {
151-
self.dk_pke.ct_eq(&other.dk_pke).into() && self.ek.eq(&other.ek) && self.z.eq(&other.z)
151+
// Only the public encapsulation key may be compared in variable time.
152+
let ek_eq = Choice::from_u8_lsb(u8::from(self.ek.eq(&other.ek)));
153+
(self.dk_pke.ct_eq(&other.dk_pke) & ek_eq & self.z.ct_eq(&other.z)).into()
152154
}
153155
}
154156

@@ -292,3 +294,41 @@ where
292294
P::concat_dk(dk_pke, ek, self.ek.h(), *self.z)
293295
}
294296
}
297+
298+
#[cfg(test)]
299+
mod tests {
300+
use super::*;
301+
use crate::{MlKem512, MlKem768, MlKem1024};
302+
303+
fn key_equality<P: KemParams>() {
304+
let key = DecapsulationKey::<P>::from_seed([0; 64].into());
305+
let other = DecapsulationKey::<P>::from_seed([1; 64].into());
306+
307+
// Changing any one compared component must make the keys unequal.
308+
let mut changed = key.clone();
309+
changed.dk_pke = other.dk_pke.clone();
310+
assert_ne!(key, changed);
311+
312+
let mut changed = key.clone();
313+
changed.ek = other.ek.clone();
314+
assert_ne!(key, changed);
315+
316+
for i in [0, 31] {
317+
let mut changed = key.clone();
318+
changed.z[i] ^= 1;
319+
assert_ne!(key, changed);
320+
}
321+
322+
// Seed provenance is deliberately omitted from equality.
323+
let mut imported = key.clone();
324+
imported.d = None;
325+
assert_eq!(key, imported);
326+
}
327+
328+
#[test]
329+
fn equality_compares_all_key_components() {
330+
key_equality::<MlKem512>();
331+
key_equality::<MlKem768>();
332+
key_equality::<MlKem1024>();
333+
}
334+
}

0 commit comments

Comments
 (0)