diff --git a/src/api.rs b/src/api.rs index 3a29494..b416d0a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -19,6 +19,11 @@ pub enum SignError { Verify, } +pub enum RestoreError { + PublicKeyLen, + SecretKeyLen, +} + impl Keypair { /// Explicitly expose secret key /// ``` @@ -46,6 +51,30 @@ impl Keypair { crypto_sign_keypair(&mut public, &mut secret, None); Keypair { public, secret } } + + /// Restores a keypair from public and secret key + /// + /// Example: + /// ``` + /// # use pqc_dilithium::*; + /// let (public, secret) = get_keys_from_db(&mut conn).await?; + /// let keys = Keypair::restore(public, secret)?; + /// ``` + pub fn restore(public: &[u8], secret: &[u8]) -> Result { + if public.len() != PUBLICKEYBYTES { + return Err(RestoreError::PublicKeyLen) + } + if secret.len() != SECRETKEYBYTES { + return Err(RestoreError::SecretKeyLen) + } + + let mut _public = [0u8; PUBLICKEYBYTES]; + let mut _secret = [0u8; SECRETKEYBYTES]; + _public[..PUBLICKEYBYTES].copy_from_slice(public); + _secret[..SECRETKEYBYTES].copy_from_slice(secret); + + Ok(Keypair { public: _public, secret: _secret }) + } /// Generates a signature for the given message using a keypair ///