add kyber512 from libcrux

This commit is contained in:
Jan Winkelmann (keks)
2025-02-27 16:58:31 +01:00
parent 075d9ffff3
commit 253243a8c8
5 changed files with 251 additions and 17 deletions

View File

@@ -13,6 +13,7 @@ readme = "readme.md"
experiment_libcrux = [
"dep:libcrux",
"dep:libcrux-chacha20poly1305",
"dep:libcrux-ml-kem",
]
[dependencies]
@@ -28,7 +29,10 @@ zeroize = { workspace = true }
chacha20poly1305 = { workspace = true }
blake2 = { workspace = true }
libcrux = { workspace = true, optional = true }
sha3 = {workspace = true}
libcrux-chacha20poly1305 = { workspace = true, optional = true }
libcrux-ml-kem = { workspace = true, optional = true, features = ["kyber"] }
sha3 = { workspace = true }
rand = { workspace = true }
[dev-dependencies]
rand = { workspace = true }

View File

@@ -0,0 +1,62 @@
use libcrux_ml_kem::kyber512;
use rand::RngCore;
use rosenpass_cipher_traits::algorithms::kem_kyber512::*;
use rosenpass_cipher_traits::primitives::{Kem, KemError};
pub struct Kyber512;
impl Kem<SK_LEN, PK_LEN, CT_LEN, SHK_LEN> for Kyber512 {
fn keygen(&self, sk: &mut [u8; SK_LEN], pk: &mut [u8; PK_LEN]) -> Result<(), KemError> {
let mut randomness = [0u8; libcrux_ml_kem::KEY_GENERATION_SEED_SIZE];
rand::thread_rng().fill_bytes(&mut randomness);
let key_pair = kyber512::generate_key_pair(randomness);
let new_sk: &[u8; SK_LEN] = key_pair.sk();
let new_pk: &[u8; PK_LEN] = key_pair.pk();
sk.clone_from_slice(new_sk);
pk.clone_from_slice(new_pk);
Ok(())
}
fn encaps(
&self,
shk: &mut [u8; SHK_LEN],
ct: &mut [u8; CT_LEN],
pk: &[u8; PK_LEN],
) -> Result<(), KemError> {
let mut randomness = [0u8; libcrux_ml_kem::SHARED_SECRET_SIZE];
rand::thread_rng().fill_bytes(&mut randomness);
let (new_ct, new_shk) = kyber512::encapsulate(&pk.into(), randomness);
let new_ct: &[u8; CT_LEN] = new_ct.as_slice();
shk.clone_from_slice(&new_shk);
ct.clone_from_slice(new_ct);
Ok(())
}
fn decaps(
&self,
shk: &mut [u8; SHK_LEN],
sk: &[u8; SK_LEN],
ct: &[u8; CT_LEN],
) -> Result<(), KemError> {
let new_shk: [u8; SHK_LEN] = kyber512::decapsulate(&sk.into(), &ct.into());
shk.clone_from(&new_shk);
Ok(())
}
}
impl Default for Kyber512 {
fn default() -> Self {
Self
}
}
impl KemKyber512 for Kyber512 {}

View File

@@ -1,3 +1,4 @@
//! Implementations backed by libcrux, a verified crypto library
pub mod chacha20poly1305_ietf;
pub mod kyber512;