Skip to content

Commit 423eb96

Browse files
fix(kyberlib): 🐛 add new import function
1 parent 1144629 commit 423eb96

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/api.rs

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
params::*,
99
CryptoRng, RngCore,
1010
};
11+
#[cfg(feature = "zeroize")]
12+
use zeroize::{Zeroize, ZeroizeOnDrop};
1113

1214
/// Generate a key pair for Kyber encryption with a provided RNG.
1315
///
@@ -157,6 +159,7 @@ pub fn decapsulate(ct: &[u8], sk: &[u8]) -> Decapsulated {
157159
///
158160
/// Byte lengths of the keys are determined by the security level chosen.
159161
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
162+
#[cfg_attr(feature = "zeroize", derive(Zeroize, ZeroizeOnDrop))]
160163
pub struct Keypair {
161164
/// The public key.
162165
pub public: PublicKey,
@@ -188,6 +191,37 @@ impl Keypair {
188191
pub fn generate<R: CryptoRng + RngCore>(rng: &mut R) -> Result<Keypair, KyberLibError> {
189192
keypair(rng)
190193
}
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+
}
191225
}
192226

193227
struct DummyRng {}

0 commit comments

Comments
 (0)