Skip to content

Commit

Permalink
feat(kyberlib): 🎨 new macros for kem.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 8, 2024
1 parent 446510c commit 122cf72
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ include = [
"/CONTRIBUTING.md",
"/LICENSE-APACHE",
"/LICENSE-MIT",
"/benches/**",
"/build.rs",
"/Cargo.toml",
"/examples/**",
"/README.md",
"/src/**",
"/tests/**",
]
keywords = ["kyber", "kem", "key-exchange", "kex", "post-quantum"]
license = "MIT OR Apache-2.0"
Expand Down
57 changes: 57 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,60 @@ macro_rules! kyberlib_max {
max
}};
}

/// Generates a public and private key pair for CCA-secure Kyber key encapsulation mechanism.
///
/// # Arguments
///
/// * `pk` - Output public key (an already allocated array of CRYPTO_PUBLICKEYBYTES bytes).
/// * `sk` - Output private key (an already allocated array of CRYPTO_SECRETKEYBYTES bytes).
/// * `_rng` - Random number generator implementing RngCore + CryptoRng.
/// * `_seed` - Optional seed for key generation.
///
/// # Errors
///
/// Returns a `KyberLibError` on failure.
#[macro_export]
macro_rules! kyberlib_generate_key_pair {
($pk:expr, $sk:expr, $rng:expr, $seed:expr) => {
kyberlib::kem::generate_key_pair($pk, $sk, $rng, $seed)
};
}

/// Generates cipher text and a shared secret for a given public key.
///
/// # Arguments
///
/// * `ct` - Output cipher text (an already allocated array of CRYPTO_CIPHERTEXTBYTES bytes).
/// * `ss` - Output shared secret (an already allocated array of CRYPTO_BYTES bytes).
/// * `pk` - Input public key (an already allocated array of CRYPTO_PUBLICKEYBYTES bytes).
/// * `_rng` - Random number generator implementing RngCore + CryptoRng.
/// * `_seed` - Optional seed for random number generation.
///
/// # Errors
///
/// Returns a `KyberLibError` on failure.
#[macro_export]
macro_rules! kyberlib_encrypt_message {
($ct:expr, $ss:expr, $pk:expr, $rng:expr, $seed:expr) => {
kyberlib::kem::encrypt_message($ct, $ss, $pk, $rng, $seed)
};
}

/// Generates a shared secret for a given cipher text and private key.
///
/// # Arguments
///
/// * `ss` - Output shared secret (an already allocated array of CRYPTO_BYTES bytes).
/// * `ct` - Input cipher text (an already allocated array of CRYPTO_CIPHERTEXTBYTES bytes).
/// * `sk` - Input private key (an already allocated array of CRYPTO_SECRETKEYBYTES bytes).
///
/// On failure, `ss` will contain a pseudo-random value.
#[macro_export]
macro_rules! kyberlib_decrypt_message {
($ss:expr, $ct:expr, $sk:expr) => {
kyberlib::kem::decrypt_message($ss, $ct, $sk)
};
}


53 changes: 53 additions & 0 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#[cfg(test)]
mod tests {
use kyberlib::{kyberlib_assert, kyberlib_max, kyberlib_min};
use rand_core::OsRng;
use rlg::{log::Log, log_format::LogFormat, log_level::LogLevel};
use kyberlib::{kyberlib_generate_key_pair, kyberlib_encrypt_message, kyberlib_decrypt_message};
use kyberlib::{KYBER_PUBLIC_KEY_BYTES, KYBER_SECRET_KEY_BYTES, KYBER_CIPHERTEXT_BYTES, KYBER_SHARED_SECRET_BYTES};

#[test]
fn test_kyberlib_assert() {
Expand Down Expand Up @@ -82,4 +85,54 @@ mod tests {
assert_eq!(log.description, "Hello world");
assert_eq!(log.format, LogFormat::JSON);
}

#[test]
fn test_generate_key_pair_macro() {
let mut rng = OsRng;
let mut public_key = [0u8; KYBER_PUBLIC_KEY_BYTES];
let mut secret_key = [0u8; KYBER_SECRET_KEY_BYTES];

kyberlib_generate_key_pair!(&mut public_key, &mut secret_key, &mut rng, None).unwrap();

assert_eq!(public_key.len(), KYBER_PUBLIC_KEY_BYTES);
assert_eq!(secret_key.len(), KYBER_SECRET_KEY_BYTES);
}

#[test]
fn test_encrypt_message_macro() {
let mut rng = OsRng;
let mut public_key = [0u8; KYBER_PUBLIC_KEY_BYTES];
let mut secret_key = [0u8; KYBER_SECRET_KEY_BYTES];

kyberlib_generate_key_pair!(&mut public_key, &mut secret_key, &mut rng, None).unwrap();

let mut ciphertext = [0u8; KYBER_CIPHERTEXT_BYTES];
let mut shared_secret1 = [0u8; KYBER_SHARED_SECRET_BYTES];

kyberlib_encrypt_message!(&mut ciphertext, &mut shared_secret1, &public_key, &mut rng, None).unwrap();

let mut shared_secret2 = [0u8; KYBER_SHARED_SECRET_BYTES];
kyberlib_decrypt_message!(&mut shared_secret2, &ciphertext, &secret_key);

assert_eq!(shared_secret1, shared_secret2);
}

#[test]
fn test_decrypt_message_macro() {
let mut rng = OsRng;
let mut public_key = [0u8; KYBER_PUBLIC_KEY_BYTES];
let mut secret_key = [0u8; KYBER_SECRET_KEY_BYTES];

kyberlib_generate_key_pair!(&mut public_key, &mut secret_key, &mut rng, None).unwrap();

let mut ciphertext = [0u8; KYBER_CIPHERTEXT_BYTES];
let mut shared_secret1 = [0u8; KYBER_SHARED_SECRET_BYTES];

kyberlib_encrypt_message!(&mut ciphertext, &mut shared_secret1, &public_key, &mut rng, None).unwrap();

let mut shared_secret2 = [0u8; KYBER_SHARED_SECRET_BYTES];
kyberlib_decrypt_message!(&mut shared_secret2, &ciphertext, &secret_key);

assert_eq!(shared_secret1, shared_secret2);
}
}

0 comments on commit 122cf72

Please sign in to comment.