@@ -8,6 +8,8 @@ use crate::{
8
8
params:: * ,
9
9
CryptoRng , RngCore ,
10
10
} ;
11
+ #[ cfg( feature = "zeroize" ) ]
12
+ use zeroize:: { Zeroize , ZeroizeOnDrop } ;
11
13
12
14
/// Generate a key pair for Kyber encryption with a provided RNG.
13
15
///
@@ -157,6 +159,7 @@ pub fn decapsulate(ct: &[u8], sk: &[u8]) -> Decapsulated {
157
159
///
158
160
/// Byte lengths of the keys are determined by the security level chosen.
159
161
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
162
+ #[ cfg_attr( feature = "zeroize" , derive( Zeroize , ZeroizeOnDrop ) ) ]
160
163
pub struct Keypair {
161
164
/// The public key.
162
165
pub public : PublicKey ,
@@ -188,6 +191,37 @@ impl Keypair {
188
191
pub fn generate < R : CryptoRng + RngCore > ( rng : & mut R ) -> Result < Keypair , KyberLibError > {
189
192
keypair ( rng)
190
193
}
194
+
195
+ /// Imports a keypair from existing public and secret key arrays.
196
+ ///
197
+ /// This function imports a keypair from existing public and secret key arrays and returns it as a `Keypair` struct.
198
+ ///
199
+ /// # Arguments
200
+ ///
201
+ /// * `public` - A mutable reference to a `[u8; KYBER_PUBLIC_KEY_BYTES]` array representing the public key.
202
+ /// * `secret` - A mutable reference to a `[u8; KYBER_SECRET_KEY_BYTES]` array representing the secret key.
203
+ /// * `rng` - The random number generator implementing the `RngCore` and `CryptoRng` traits.
204
+ ///
205
+ /// # Example
206
+ ///
207
+ /// ```
208
+ /// # use kyberlib::*;
209
+ /// # fn main() -> Result<(), KyberLibError> {
210
+ /// let mut rng = rand::thread_rng();
211
+ /// let keys = keypair(&mut rng)?;
212
+ /// let mut public_key = keys.public;
213
+ /// let mut secret_key = keys.secret;
214
+ /// let keys = Keypair::import(&mut public_key, &mut secret_key, &mut rng)?;
215
+ /// let _ = Keypair::import(&mut public_key, &mut secret_key, &mut rng)?;
216
+ /// # Ok(()) }
217
+ /// ```
218
+ pub fn import < R : CryptoRng + RngCore > (
219
+ public : & mut [ u8 ; KYBER_PUBLIC_KEY_BYTES ] ,
220
+ secret : & mut [ u8 ; KYBER_SECRET_KEY_BYTES ] ,
221
+ rng : & mut R ,
222
+ ) -> Result < Keypair , KyberLibError > {
223
+ keypairfrom ( public, secret, rng)
224
+ }
191
225
}
192
226
193
227
struct DummyRng { }
0 commit comments