Skip to content

Commit 8fce564

Browse files
authored
frodo-kem: remove ct_eq_bytes method (#261)
1 parent 1f9bd5f commit 8fce564

2 files changed

Lines changed: 3 additions & 31 deletions

File tree

frodo-kem/src/hazmat/traits.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::hazmat::{
77
use alloc::{string::String, vec::Vec};
88
use rand_core::CryptoRng;
99
use sha3::digest::{ExtendableOutput, ExtendableOutputReset, Update};
10-
use subtle::{Choice, ConditionallySelectable};
10+
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
1111
use zeroize::Zeroize;
1212

1313
/// Trait for implementing the FrodoKEM sampling algorithm
@@ -403,8 +403,7 @@ pub trait Kem: Params + Expanded + Sample {
403403
// Needs to avoid branching on secret data as per:
404404
// Qian Guo, Thomas Johansson, Alexander Nilsson. A key-recovery timing attack on post-quantum
405405
// primitives using the Fujisaki-Okamoto transformation and its application on FrodoKEM. In CRYPTO 2020.
406-
let choice =
407-
self.ct_verify(&matrix_bp, &matrix_bpp) & self.ct_verify(&matrix_c, &matrix_cc);
406+
let choice = matrix_bp.ct_eq(&matrix_bpp) & matrix_c.ct_eq(&matrix_cc);
408407

409408
let mut fin_k = vec![0u8; Self::SHARED_SECRET_LENGTH];
410409
// Take k if choice == 0, otherwise take s
@@ -769,19 +768,6 @@ pub trait Kem: Params + Expanded + Sample {
769768
}
770769
}
771770

772-
/// Constant time verify for a u16 array
773-
fn ct_verify(&self, a: &[u16], b: &[u16]) -> Choice {
774-
let mut choice = 0;
775-
776-
for i in 0..a.len() {
777-
choice |= a[i] ^ b[i];
778-
}
779-
780-
let mut choice = choice as i16;
781-
choice = ((choice | choice.wrapping_neg()) >> 15) + 1;
782-
Choice::from(choice as u8)
783-
}
784-
785771
/// Constant time select for a u16 array
786772
fn ct_select(&self, choice: Choice, a: &[u8], b: &[u8], out: &mut [u8]) {
787773
for i in 0..a.len() {

frodo-kem/src/lib.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ macro_rules! ct_eq_imp {
237237
($name:ident) => {
238238
impl ConstantTimeEq for $name {
239239
fn ct_eq(&self, other: &Self) -> Choice {
240-
self.algorithm.ct_eq(&other.algorithm) & ct_eq_bytes(&self.value, &other.value)
240+
self.algorithm.ct_eq(&other.algorithm) & self.value.ct_eq(&other.value)
241241
}
242242
}
243243

@@ -1645,20 +1645,6 @@ pub struct AlgorithmParams {
16451645
pub ciphertext_length: usize,
16461646
}
16471647

1648-
fn ct_eq_bytes(lhs: &[u8], rhs: &[u8]) -> Choice {
1649-
if lhs.len() != rhs.len() {
1650-
return 0u8.into();
1651-
}
1652-
1653-
let mut eq = 0u8;
1654-
for i in 0..lhs.len() {
1655-
eq |= lhs[i] ^ rhs[i];
1656-
}
1657-
1658-
let eq = ((eq | eq.wrapping_neg()) >> 7).wrapping_add(1);
1659-
Choice::from(eq)
1660-
}
1661-
16621648
#[cfg(test)]
16631649
#[allow(clippy::unwrap_used)]
16641650
mod tests {

0 commit comments

Comments
 (0)