@@ -7,7 +7,7 @@ use crate::hazmat::{
77use alloc:: { string:: String , vec:: Vec } ;
88use rand_core:: CryptoRng ;
99use sha3:: digest:: { ExtendableOutput , ExtendableOutputReset , Update } ;
10- use subtle:: { Choice , ConditionallySelectable } ;
10+ use subtle:: { Choice , ConditionallySelectable , ConstantTimeEq } ;
1111use 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 ( ) {
0 commit comments