dcrypt-kem 1.2.3

Key Encapsulation Mechanisms for the dcrypt library
Documentation
// File: dcrypt-kem/src/saber/mod.rs

use dcrypt_api::{Kem, Result};
use rand::{CryptoRng, RngCore};
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

/// LightSaber KEM
pub struct LightSaber;

#[derive(Clone, Zeroize)]
pub struct SaberPublicKey(pub Vec<u8>);

#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct SaberSecretKey(pub Vec<u8>);

#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct SaberSharedSecret(pub Vec<u8>);

#[derive(Clone)]
pub struct SaberCiphertext(pub Vec<u8>);

// SaberPublicKey methods
impl SaberPublicKey {
    /// Create a new public key from bytes
    pub fn new(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    /// Get the length of the public key
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the public key is empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Export the public key to bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        self.0.clone()
    }

    /// Get a reference to the inner bytes
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Create from a byte slice
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        Ok(Self(bytes.to_vec()))
    }
}

// SaberSecretKey methods
impl SaberSecretKey {
    /// Create a new secret key from bytes
    pub fn new(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    /// Get the length of the secret key
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the secret key is empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Export the secret key to bytes with zeroization
    pub fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>> {
        Zeroizing::new(self.0.clone())
    }

    /// Get a reference to the inner bytes (internal use only)
    pub(crate) fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Create from a byte slice
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        Ok(Self(bytes.to_vec()))
    }
}

// SaberSharedSecret methods
impl SaberSharedSecret {
    /// Create a new shared secret from bytes
    pub fn new(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    /// Get the length of the shared secret
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the shared secret is empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Export the shared secret to bytes with zeroization
    pub fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>> {
        Zeroizing::new(self.0.clone())
    }

    /// Get a reference to the inner bytes (internal use only)
    pub(crate) fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

// SaberCiphertext methods
impl SaberCiphertext {
    /// Create a new ciphertext from bytes
    pub fn new(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    /// Get the length of the ciphertext
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if the ciphertext is empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Export the ciphertext to bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        self.0.clone()
    }

    /// Get a reference to the inner bytes
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Create from a byte slice
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        Ok(Self(bytes.to_vec()))
    }
}

// NO AsRef or AsMut implementations - this prevents direct byte access

impl Kem for LightSaber {
    type PublicKey = SaberPublicKey;
    type SecretKey = SaberSecretKey;
    type SharedSecret = SaberSharedSecret;
    type Ciphertext = SaberCiphertext;
    type KeyPair = (Self::PublicKey, Self::SecretKey);

    fn name() -> &'static str {
        "LightSaber"
    }

    fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<(Self::PublicKey, Self::SecretKey)> {
        // Placeholder implementation
        let mut public_key = vec![0u8; 672];
        let mut secret_key = vec![0u8; 1568];
        rng.fill_bytes(&mut public_key);
        rng.fill_bytes(&mut secret_key);
        Ok((SaberPublicKey(public_key), SaberSecretKey(secret_key)))
    }

    fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey {
        keypair.0.clone()
    }

    fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey {
        keypair.1.clone()
    }

    fn encapsulate<R: CryptoRng + RngCore>(
        _rng: &mut R,
        _public_key: &Self::PublicKey,
    ) -> Result<(Self::Ciphertext, Self::SharedSecret)> {
        // Placeholder implementation
        Ok((
            SaberCiphertext(vec![0u8; 736]),
            SaberSharedSecret(vec![0u8; 32]),
        ))
    }

    fn decapsulate(
        _secret_key: &Self::SecretKey,
        _ciphertext: &Self::Ciphertext,
    ) -> Result<Self::SharedSecret> {
        // Placeholder implementation
        Ok(SaberSharedSecret(vec![0u8; 32]))
    }
}

/// Saber KEM
pub struct Saber;

impl Kem for Saber {
    type PublicKey = SaberPublicKey;
    type SecretKey = SaberSecretKey;
    type SharedSecret = SaberSharedSecret;
    type Ciphertext = SaberCiphertext;
    type KeyPair = (Self::PublicKey, Self::SecretKey);

    fn name() -> &'static str {
        "Saber"
    }

    fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<(Self::PublicKey, Self::SecretKey)> {
        // Placeholder implementation
        let mut public_key = vec![0u8; 992];
        let mut secret_key = vec![0u8; 2304];
        rng.fill_bytes(&mut public_key);
        rng.fill_bytes(&mut secret_key);
        Ok((SaberPublicKey(public_key), SaberSecretKey(secret_key)))
    }

    fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey {
        keypair.0.clone()
    }

    fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey {
        keypair.1.clone()
    }

    fn encapsulate<R: CryptoRng + RngCore>(
        _rng: &mut R,
        _public_key: &Self::PublicKey,
    ) -> Result<(Self::Ciphertext, Self::SharedSecret)> {
        // Placeholder implementation
        Ok((
            SaberCiphertext(vec![0u8; 1088]),
            SaberSharedSecret(vec![0u8; 32]),
        ))
    }

    fn decapsulate(
        _secret_key: &Self::SecretKey,
        _ciphertext: &Self::Ciphertext,
    ) -> Result<Self::SharedSecret> {
        // Placeholder implementation
        Ok(SaberSharedSecret(vec![0u8; 32]))
    }
}

/// FireSaber KEM
pub struct FireSaber;

impl Kem for FireSaber {
    type PublicKey = SaberPublicKey;
    type SecretKey = SaberSecretKey;
    type SharedSecret = SaberSharedSecret;
    type Ciphertext = SaberCiphertext;
    type KeyPair = (Self::PublicKey, Self::SecretKey);

    fn name() -> &'static str {
        "FireSaber"
    }

    fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<(Self::PublicKey, Self::SecretKey)> {
        // Placeholder implementation
        let mut public_key = vec![0u8; 1312];
        let mut secret_key = vec![0u8; 3040];
        rng.fill_bytes(&mut public_key);
        rng.fill_bytes(&mut secret_key);
        Ok((SaberPublicKey(public_key), SaberSecretKey(secret_key)))
    }

    fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey {
        keypair.0.clone()
    }

    fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey {
        keypair.1.clone()
    }

    fn encapsulate<R: CryptoRng + RngCore>(
        _rng: &mut R,
        _public_key: &Self::PublicKey,
    ) -> Result<(Self::Ciphertext, Self::SharedSecret)> {
        // Placeholder implementation
        Ok((
            SaberCiphertext(vec![0u8; 1472]),
            SaberSharedSecret(vec![0u8; 32]),
        ))
    }

    fn decapsulate(
        _secret_key: &Self::SecretKey,
        _ciphertext: &Self::Ciphertext,
    ) -> Result<Self::SharedSecret> {
        // Placeholder implementation
        Ok(SaberSharedSecret(vec![0u8; 32]))
    }
}