1use dcrypt_api::{Kem, Result};
4use rand::{CryptoRng, RngCore};
5use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
6
7pub 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
22impl SaberPublicKey {
24 pub fn new(bytes: Vec<u8>) -> Self {
26 Self(bytes)
27 }
28
29 pub fn len(&self) -> usize {
31 self.0.len()
32 }
33
34 pub fn is_empty(&self) -> bool {
36 self.0.is_empty()
37 }
38
39 pub fn to_bytes(&self) -> Vec<u8> {
41 self.0.clone()
42 }
43
44 pub fn as_bytes(&self) -> &[u8] {
46 &self.0
47 }
48
49 pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
51 Ok(Self(bytes.to_vec()))
52 }
53}
54
55impl SaberSecretKey {
57 pub fn new(bytes: Vec<u8>) -> Self {
59 Self(bytes)
60 }
61
62 pub fn len(&self) -> usize {
64 self.0.len()
65 }
66
67 pub fn is_empty(&self) -> bool {
69 self.0.is_empty()
70 }
71
72 pub fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>> {
74 Zeroizing::new(self.0.clone())
75 }
76
77 pub(crate) fn as_bytes(&self) -> &[u8] {
79 &self.0
80 }
81
82 pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
84 Ok(Self(bytes.to_vec()))
85 }
86}
87
88impl SaberSharedSecret {
90 pub fn new(bytes: Vec<u8>) -> Self {
92 Self(bytes)
93 }
94
95 pub fn len(&self) -> usize {
97 self.0.len()
98 }
99
100 pub fn is_empty(&self) -> bool {
102 self.0.is_empty()
103 }
104
105 pub fn to_bytes_zeroizing(&self) -> Zeroizing<Vec<u8>> {
107 Zeroizing::new(self.0.clone())
108 }
109
110 pub(crate) fn as_bytes(&self) -> &[u8] {
112 &self.0
113 }
114}
115
116impl SaberCiphertext {
118 pub fn new(bytes: Vec<u8>) -> Self {
120 Self(bytes)
121 }
122
123 pub fn len(&self) -> usize {
125 self.0.len()
126 }
127
128 pub fn is_empty(&self) -> bool {
130 self.0.is_empty()
131 }
132
133 pub fn to_bytes(&self) -> Vec<u8> {
135 self.0.clone()
136 }
137
138 pub fn as_bytes(&self) -> &[u8] {
140 &self.0
141 }
142
143 pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
145 Ok(Self(bytes.to_vec()))
146 }
147}
148
149impl 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 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 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 Ok(SaberSharedSecret(vec![0u8; 32]))
196 }
197}
198
199pub 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 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 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 Ok(SaberSharedSecret(vec![0u8; 32]))
247 }
248}
249
250pub 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 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 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 Ok(SaberSharedSecret(vec![0u8; 32]))
298 }
299}