-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from cryspen/franziskus/mlkem_pk_validation
mlkem pk validation
- Loading branch information
Showing
44 changed files
with
1,014 additions
and
886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use libcrux::digest::shake128; | ||
use rand::CryptoRng; | ||
|
||
use rand; | ||
|
||
use rand::RngCore; | ||
|
||
pub(crate) struct FuzzRng { | ||
pub(crate) data: Vec<u8>, | ||
} | ||
|
||
impl FuzzRng { | ||
/// Create a new rng for fuzzing with 1024 bytes, using shake128. | ||
pub(crate) fn new(e: &[u8]) -> Self { | ||
let data: [u8; 1024] = shake128(e); | ||
Self { | ||
data: data.to_vec(), | ||
} | ||
} | ||
} | ||
|
||
impl RngCore for FuzzRng { | ||
fn next_u32(&mut self) -> u32 { | ||
let mut bytes: [u8; 4] = [0; 4]; | ||
self.fill_bytes(&mut bytes); | ||
u32::from_be_bytes(bytes) | ||
} | ||
|
||
fn next_u64(&mut self) -> u64 { | ||
todo!() | ||
} | ||
|
||
fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
dest.copy_from_slice(&self.data[0..dest.len()]); | ||
self.data = self.data.drain(dest.len()..).collect(); | ||
} | ||
|
||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { | ||
self.fill_bytes(dest); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl CryptoRng for FuzzRng {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use libcrux::digest::shake256; | ||
|
||
use libcrux::kem::PrivateKey; | ||
|
||
use libcrux::kem::Ct; | ||
|
||
use rand::Rng; | ||
|
||
use rand::CryptoRng; | ||
|
||
use libcrux::kem::Algorithm; | ||
|
||
pub(crate) fn modify_ciphertext( | ||
alg: Algorithm, | ||
rng: &mut (impl CryptoRng + Rng), | ||
ciphertext: Ct, | ||
) -> Ct { | ||
let mut raw_ciphertext = ciphertext.encode(); | ||
|
||
let mut random_u32: usize = rng.next_u32().try_into().unwrap(); | ||
|
||
let mut random_byte: u8 = (random_u32 & 0xFF) as u8; | ||
if random_byte == 0 { | ||
random_byte += 1; | ||
} | ||
random_u32 >>= 8; | ||
|
||
let position = random_u32 % raw_ciphertext.len(); | ||
raw_ciphertext[position] ^= random_byte; | ||
|
||
Ct::decode(alg, &raw_ciphertext).unwrap() | ||
} | ||
|
||
pub(crate) const SHARED_SECRET_SIZE: usize = 32; | ||
|
||
pub(crate) fn modify_secret_key( | ||
alg: Algorithm, | ||
rng: &mut (impl CryptoRng + Rng), | ||
secret_key: PrivateKey, | ||
modify_implicit_rejection_value: bool, | ||
) -> PrivateKey { | ||
let mut raw_secret_key = secret_key.encode(); | ||
|
||
let mut random_u32: usize = rng.next_u32().try_into().unwrap(); | ||
|
||
let mut random_byte: u8 = (random_u32 & 0xFF) as u8; | ||
if random_byte == 0 { | ||
random_byte += 1; | ||
} | ||
random_u32 >>= 8; | ||
|
||
let position = if modify_implicit_rejection_value { | ||
(raw_secret_key.len() - SHARED_SECRET_SIZE) + (random_u32 % SHARED_SECRET_SIZE) | ||
} else { | ||
random_u32 % (raw_secret_key.len() - SHARED_SECRET_SIZE) | ||
}; | ||
|
||
raw_secret_key[position] ^= random_byte; | ||
|
||
PrivateKey::decode(alg, &raw_secret_key).unwrap() | ||
} | ||
|
||
pub(crate) fn compute_implicit_rejection_shared_secret( | ||
ciphertext: Ct, | ||
secret_key: PrivateKey, | ||
) -> [u8; SHARED_SECRET_SIZE] { | ||
let raw_secret_key = secret_key.encode(); | ||
|
||
let mut to_hash = raw_secret_key[raw_secret_key.len() - SHARED_SECRET_SIZE..].to_vec(); | ||
to_hash.extend_from_slice(&ciphertext.encode()); | ||
|
||
shake256(&to_hash) | ||
} |
Oops, something went wrong.