Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub enum SignError {
Verify,
}

pub enum RestoreError {
PublicKeyLen,
SecretKeyLen,
}

impl Keypair {
/// Explicitly expose secret key
/// ```
Expand Down Expand Up @@ -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<Keypair, RestoreError> {
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
///
Expand Down