Skip to content

Commit

Permalink
aus
Browse files Browse the repository at this point in the history
  • Loading branch information
eschorn1 committed Oct 12, 2023
0 parents commit d11bf15
Show file tree
Hide file tree
Showing 11 changed files with 1,084 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "ml-kem-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]
zeroize = {version = "1.6.0", features = ["zeroize_derive"]}
#typenum = {version = "1.17.0", features = ["const-generics"]}
#generic-array = {version = "1.0.0", features= ["zeroize"]}
sha3 = "0.10.8"
rand = "0.8.5"


[dev-dependencies]
rand_chacha = "0.3.1"
36 changes: 36 additions & 0 deletions src/issue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@


#[cfg(test)]
mod tests {
use generic_array::{ArrayLength, GenericArray};
use typenum::{U123, U1024};

pub struct MySmallArray<N: ArrayLength>(GenericArray<u8, N>);
pub struct MyBigArray<N: ArrayLength>(GenericArray<u8, N>);

#[test]
fn test_array_sizes() {
use generic_array::{typenum::*, ArrayLength, GenericArray};

struct Foo<T, N: ArrayLength> {
data: GenericArray<T, N>,
}

// https://docs.rs/typenum/latest/typenum/operator_aliases/type.Prod.html
type U12345 = Prod<U15, U823>;

let foo = Foo::<i32, U12345> {
data: GenericArray::default(),
};

let mut dest = [0i32; 12345];
dest.copy_from_slice(&foo.data);

assert_eq!(foo.data.len(), dest.len());
// let big = MyBigArray::Sum::<U123, U1024>(GenericArray.default());
// let mut big_bytes = [0u8; 1047];
// big_bytes.copy_from_slice(&big.0);
}
}


117 changes: 117 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#![deny(clippy::pedantic)]
#![deny(warnings)]
use zeroize::{Zeroize, ZeroizeOnDrop};
mod ml_kem;

pub const N: u32 = 11;
pub const Q: u32 = 12;
pub const SSK_LEN: usize = 32;

#[derive(Default, PartialEq, Debug, Zeroize, ZeroizeOnDrop)]
pub struct SharedSecretKey([u8; SSK_LEN]);

macro_rules! functionality {
() => {
use zeroize::{Zeroize, ZeroizeOnDrop};

#[derive(Zeroize, ZeroizeOnDrop)]
pub struct EncapsKey([u8; EK_LEN]);

#[derive(Zeroize, ZeroizeOnDrop)]
pub struct DecapsKey([u8; DK_LEN]);

#[derive(Zeroize, ZeroizeOnDrop)]
pub struct CipherText([u8; CT_LEN]);

pub fn key_gen() -> (EncapsKey, DecapsKey) {
let (mut ek, mut dk) = (EncapsKey::default(), DecapsKey::default());
ml_kem::key_gen(K, ETA1, &mut ek.0, &mut dk.0);
(ek, dk)
}

pub fn new_ek(bytes: [u8; EK_LEN]) -> EncapsKey {
EncapsKey(bytes)
}

pub fn new_ct(bytes: [u8; CT_LEN]) -> CipherText {
CipherText(bytes)
}

impl EncapsKey {
fn default() -> Self {
EncapsKey([0u8; EK_LEN])
}
pub fn encaps(&self) -> (SharedSecretKey, CipherText) {
let (ek, mut ct) = (EncapsKey::default(), CipherText::default());
let ssk = ml_kem::encaps(K, ETA1, ETA2, DU, DV, &ek.0, &mut ct.0);
(ssk, ct)
}
pub fn to_bytes(&self) -> [u8; EK_LEN] {
self.0.clone()
}
}

impl DecapsKey {
fn default() -> Self {
DecapsKey([0u8; DK_LEN])
}
pub fn decaps(&self, ct: &CipherText) -> SharedSecretKey {
ml_kem::decaps(K, DU, DV, &self.0, &ct.0)
}
}

impl CipherText {
fn default() -> Self {
CipherText([0u8; CT_LEN])
}
pub fn to_bytes(&self) -> [u8; CT_LEN] {
self.0.clone()
}
}
};
}

pub mod ml_kem_512 {
use crate::{ml_kem, SharedSecretKey};

const K: u32 = 2;
const ETA1: u32 = 3;
const ETA2: u32 = 2;
const DU: u32 = 10;
const DV: u32 = 4;
const EK_LEN: usize = 800;
const DK_LEN: usize = 1632;
const CT_LEN: usize = 768;

functionality!();
}

pub mod ml_kem_768 {
use crate::{ml_kem, SharedSecretKey};

const K: u32 = 3;
const ETA1: u32 = 2;
const ETA2: u32 = 2;
const DU: u32 = 10;
const DV: u32 = 4;
const EK_LEN: usize = 1184;
const DK_LEN: usize = 2400;
const CT_LEN: usize = 1088;

functionality!();
}

pub mod ml_kem_1024 {
use crate::{ml_kem, SharedSecretKey};

const K: u32 = 4;
const ETA1: u32 = 2;
const ETA2: u32 = 2;
const DU: u32 = 11;
const DV: u32 = 5;
const EK_LEN: usize = 1568;
const DK_LEN: usize = 3168;
const CT_LEN: usize = 1568;

functionality!();
}
29 changes: 29 additions & 0 deletions src/ml_kem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{SharedSecretKey, SSK_LEN};

pub(crate) fn key_gen(_k: u32, _eta1: u32, ek: &mut [u8], dk: &mut [u8]) {
for item in ek.iter_mut() {
*item = 11
}
for item in dk.iter_mut() {
*item = 22
}
}

pub(crate) fn encaps(
_k: u32,
_eta1: u32,
_eta2: u32,
_du: u32,
_dv: u32,
_ek: &[u8],
ct: &mut [u8],
) -> SharedSecretKey {
for item in ct.iter_mut() {
*item = 33
}
SharedSecretKey([44u8; SSK_LEN])
}

pub(crate) fn decaps(_k: u32, _du: u32, _dv: u32, _dk: &[u8], _ct: &[u8]) -> SharedSecretKey {
SharedSecretKey([55u8; SSK_LEN])
}
Loading

0 comments on commit d11bf15

Please sign in to comment.