dcrypt_kem/saber/
mod.rs

1// File: dcrypt-kem/src/saber/mod.rs
2
3use dcrypt_api::{Kem, Result};
4use rand::{CryptoRng, RngCore};
5use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
6
7/// LightSaber KEM
8pub struct LightSaber;
9
10#[derive(Clone, Zeroize)]
11pub struct SaberPublicKey(pub Vec<u8>);
12
13#[derive(Clone, Zeroize, ZeroizeOnDrop)]
14pub struct SaberSecretKey(pub Vec<u8>);
15
16#[derive(Clone, Zeroize, ZeroizeOnDrop)]
17pub struct SaberSharedSecret(pub Vec<u8>);
18
19#[derive(Clone)]
20pub struct SaberCiphertext(pub Vec<u8>);
21
22// SaberPublicKey methods
23impl SaberPublicKey {
24    /// Create a new public key from bytes
25    pub fn new(bytes: Vec<u8>) -> Self {
26        Self(bytes)
27    }
28
29    /// Get the length of the public key
30    pub fn len(&self) -> usize {
31        self.0.len()
32    }
33
34    /// Check if the public key is empty
35    pub fn is_empty(&self) -> bool {
36        self.0.is_empty()
37    }
38
39    /// Export the public key to bytes
40    pub fn to_bytes(&self) -> Vec<u8> {
41        self.0.clone()
42    }
43
44    /// Get a reference to the inner bytes
45    pub fn as_bytes(&self) -> &[u8] {
46        &self.0
47    }
48
49    /// Create from a byte slice
50    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
51        Ok(Self(bytes.to_vec()))
52    }
53}
54
55// SaberSecretKey methods
56impl SaberSecretKey {
57    /// Create a new secret key from bytes
58    pub fn new(bytes: Vec<u8>) -> Self {
59        Self(bytes)
60    }
61
62    /// Get the length of the secret key
63    pub fn len(&self) -> usize {
64        self.0.len()
65    }
66
67    /// Check if the secret key is empty
68    pub fn is_empty(&self) -> bool {
69        self.0.is_empty()
70    }
71
72    /// Export the secret key to bytes with zeroization
73    pub fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>> {
74        Zeroizing::new(self.0.clone())
75    }
76
77    /// Get a reference to the inner bytes (internal use only)
78    pub(crate) fn as_bytes(&self) -> &[u8] {
79        &self.0
80    }
81
82    /// Create from a byte slice
83    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
84        Ok(Self(bytes.to_vec()))
85    }
86}
87
88// SaberSharedSecret methods
89impl SaberSharedSecret {
90    /// Create a new shared secret from bytes
91    pub fn new(bytes: Vec<u8>) -> Self {
92        Self(bytes)
93    }
94
95    /// Get the length of the shared secret
96    pub fn len(&self) -> usize {
97        self.0.len()
98    }
99
100    /// Check if the shared secret is empty
101    pub fn is_empty(&self) -> bool {
102        self.0.is_empty()
103    }
104
105    /// Export the shared secret to bytes with zeroization
106    pub fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>> {
107        Zeroizing::new(self.0.clone())
108    }
109
110    /// Get a reference to the inner bytes (internal use only)
111    pub(crate) fn as_bytes(&self) -> &[u8] {
112        &self.0
113    }
114}
115
116// SaberCiphertext methods
117impl SaberCiphertext {
118    /// Create a new ciphertext from bytes
119    pub fn new(bytes: Vec<u8>) -> Self {
120        Self(bytes)
121    }
122
123    /// Get the length of the ciphertext
124    pub fn len(&self) -> usize {
125        self.0.len()
126    }
127
128    /// Check if the ciphertext is empty
129    pub fn is_empty(&self) -> bool {
130        self.0.is_empty()
131    }
132
133    /// Export the ciphertext to bytes
134    pub fn to_bytes(&self) -> Vec<u8> {
135        self.0.clone()
136    }
137
138    /// Get a reference to the inner bytes
139    pub fn as_bytes(&self) -> &[u8] {
140        &self.0
141    }
142
143    /// Create from a byte slice
144    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
145        Ok(Self(bytes.to_vec()))
146    }
147}
148
149// NO AsRef or AsMut implementations - this prevents direct byte access
150
151impl Kem for LightSaber {
152    type PublicKey = SaberPublicKey;
153    type SecretKey = SaberSecretKey;
154    type SharedSecret = SaberSharedSecret;
155    type Ciphertext = SaberCiphertext;
156    type KeyPair = (Self::PublicKey, Self::SecretKey);
157
158    fn name() -> &'static str {
159        "LightSaber"
160    }
161
162    fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<(Self::PublicKey, Self::SecretKey)> {
163        // Placeholder implementation
164        let mut public_key = vec![0u8; 672];
165        let mut secret_key = vec![0u8; 1568];
166        rng.fill_bytes(&mut public_key);
167        rng.fill_bytes(&mut secret_key);
168        Ok((SaberPublicKey(public_key), SaberSecretKey(secret_key)))
169    }
170
171    fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey {
172        keypair.0.clone()
173    }
174
175    fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey {
176        keypair.1.clone()
177    }
178
179    fn encapsulate<R: CryptoRng + RngCore>(
180        _rng: &mut R,
181        _public_key: &Self::PublicKey,
182    ) -> Result<(Self::Ciphertext, Self::SharedSecret)> {
183        // Placeholder implementation
184        Ok((
185            SaberCiphertext(vec![0u8; 736]),
186            SaberSharedSecret(vec![0u8; 32]),
187        ))
188    }
189
190    fn decapsulate(
191        _secret_key: &Self::SecretKey,
192        _ciphertext: &Self::Ciphertext,
193    ) -> Result<Self::SharedSecret> {
194        // Placeholder implementation
195        Ok(SaberSharedSecret(vec![0u8; 32]))
196    }
197}
198
199/// Saber KEM
200pub struct Saber;
201
202impl Kem for Saber {
203    type PublicKey = SaberPublicKey;
204    type SecretKey = SaberSecretKey;
205    type SharedSecret = SaberSharedSecret;
206    type Ciphertext = SaberCiphertext;
207    type KeyPair = (Self::PublicKey, Self::SecretKey);
208
209    fn name() -> &'static str {
210        "Saber"
211    }
212
213    fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<(Self::PublicKey, Self::SecretKey)> {
214        // Placeholder implementation
215        let mut public_key = vec![0u8; 992];
216        let mut secret_key = vec![0u8; 2304];
217        rng.fill_bytes(&mut public_key);
218        rng.fill_bytes(&mut secret_key);
219        Ok((SaberPublicKey(public_key), SaberSecretKey(secret_key)))
220    }
221
222    fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey {
223        keypair.0.clone()
224    }
225
226    fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey {
227        keypair.1.clone()
228    }
229
230    fn encapsulate<R: CryptoRng + RngCore>(
231        _rng: &mut R,
232        _public_key: &Self::PublicKey,
233    ) -> Result<(Self::Ciphertext, Self::SharedSecret)> {
234        // Placeholder implementation
235        Ok((
236            SaberCiphertext(vec![0u8; 1088]),
237            SaberSharedSecret(vec![0u8; 32]),
238        ))
239    }
240
241    fn decapsulate(
242        _secret_key: &Self::SecretKey,
243        _ciphertext: &Self::Ciphertext,
244    ) -> Result<Self::SharedSecret> {
245        // Placeholder implementation
246        Ok(SaberSharedSecret(vec![0u8; 32]))
247    }
248}
249
250/// FireSaber KEM
251pub struct FireSaber;
252
253impl Kem for FireSaber {
254    type PublicKey = SaberPublicKey;
255    type SecretKey = SaberSecretKey;
256    type SharedSecret = SaberSharedSecret;
257    type Ciphertext = SaberCiphertext;
258    type KeyPair = (Self::PublicKey, Self::SecretKey);
259
260    fn name() -> &'static str {
261        "FireSaber"
262    }
263
264    fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<(Self::PublicKey, Self::SecretKey)> {
265        // Placeholder implementation
266        let mut public_key = vec![0u8; 1312];
267        let mut secret_key = vec![0u8; 3040];
268        rng.fill_bytes(&mut public_key);
269        rng.fill_bytes(&mut secret_key);
270        Ok((SaberPublicKey(public_key), SaberSecretKey(secret_key)))
271    }
272
273    fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey {
274        keypair.0.clone()
275    }
276
277    fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey {
278        keypair.1.clone()
279    }
280
281    fn encapsulate<R: CryptoRng + RngCore>(
282        _rng: &mut R,
283        _public_key: &Self::PublicKey,
284    ) -> Result<(Self::Ciphertext, Self::SharedSecret)> {
285        // Placeholder implementation
286        Ok((
287            SaberCiphertext(vec![0u8; 1472]),
288            SaberSharedSecret(vec![0u8; 32]),
289        ))
290    }
291
292    fn decapsulate(
293        _secret_key: &Self::SecretKey,
294        _ciphertext: &Self::Ciphertext,
295    ) -> Result<Self::SharedSecret> {
296        // Placeholder implementation
297        Ok(SaberSharedSecret(vec![0u8; 32]))
298    }
299}