use crate::errors::{BottleError, Result};
use crate::keychain::SignerKey;
use crate::signing::{Sign, Verify};
use ed25519_dalek::{
Signature, SigningKey as Ed25519SigningKey, VerifyingKey as Ed25519VerifyingKey,
};
use p256::ecdsa::{SigningKey as P256SigningKey, VerifyingKey as P256VerifyingKey};
use rand::{CryptoRng, RngCore};
use rsa::{Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey};
use sha2::{Digest, Sha256};
#[cfg(feature = "ml-kem")]
use ml_kem::{kem::Kem, EncodedSizeUser, KemCore, MlKem1024Params, MlKem768Params};
#[cfg(feature = "post-quantum")]
use pqcrypto_dilithium;
#[cfg(feature = "post-quantum")]
use pqcrypto_sphincsplus;
#[cfg(feature = "post-quantum")]
use pqcrypto_traits::sign::{
DetachedSignature as PqcDetachedSignature, PublicKey as PqcPublicKey, SecretKey as PqcSecretKey,
};
pub struct EcdsaP256Key {
signing_key: P256SigningKey,
verifying_key: P256VerifyingKey,
}
impl EcdsaP256Key {
pub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let signing_key = P256SigningKey::random(rng);
let verifying_key = *signing_key.verifying_key();
Self {
signing_key,
verifying_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
self.verifying_key.to_sec1_bytes().to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
self.signing_key.to_bytes().to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let signing_key =
P256SigningKey::from_bytes(bytes.into()).map_err(|_| BottleError::InvalidKeyType)?;
let verifying_key = *signing_key.verifying_key();
Ok(Self {
signing_key,
verifying_key,
})
}
}
impl Sign for EcdsaP256Key {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
use ecdsa::signature::Signer;
use sha2::Digest;
let digest = sha2::Sha256::digest(message);
let signature: ecdsa::Signature<p256::NistP256> = self.signing_key.sign(&digest);
Ok(signature.to_bytes().to_vec())
}
}
impl Verify for EcdsaP256Key {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
use ecdsa::signature::Verifier;
use sha2::Digest;
let digest = sha2::Sha256::digest(message);
let sig = ecdsa::Signature::from_bytes(signature.into())
.map_err(|_| BottleError::VerifyFailed)?;
self.verifying_key
.verify(&digest, &sig)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
impl SignerKey for EcdsaP256Key {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[derive(Clone)]
pub struct Ed25519Key {
signing_key: Ed25519SigningKey,
verifying_key: Ed25519VerifyingKey,
}
impl Ed25519Key {
pub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let signing_key = Ed25519SigningKey::generate(rng);
let verifying_key = signing_key.verifying_key();
Self {
signing_key,
verifying_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
self.verifying_key.to_bytes().to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
self.signing_key.to_bytes().to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let signing_key = Ed25519SigningKey::from_bytes(
bytes.try_into().map_err(|_| BottleError::InvalidKeyType)?,
);
let verifying_key = signing_key.verifying_key();
Ok(Self {
signing_key,
verifying_key,
})
}
}
impl Sign for Ed25519Key {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
use ed25519_dalek::Signer;
let signature = self.signing_key.sign(message);
Ok(signature.to_bytes().to_vec())
}
}
impl Verify for Ed25519Key {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
use ed25519_dalek::Verifier;
let sig = Signature::from_bytes(
signature
.try_into()
.map_err(|_| BottleError::VerifyFailed)?,
);
self.verifying_key
.verify(message, &sig)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
impl SignerKey for Ed25519Key {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
pub struct X25519Key {
secret: [u8; 32], public: x25519_dalek::PublicKey,
}
impl X25519Key {
pub fn generate<R: RngCore>(rng: &mut R) -> Self {
use x25519_dalek::StaticSecret;
let mut secret_bytes = [0u8; 32];
rng.fill_bytes(&mut secret_bytes);
let secret = StaticSecret::from(secret_bytes);
let public = x25519_dalek::PublicKey::from(&secret);
Self {
secret: secret_bytes,
public,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
self.public.as_bytes().to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
self.secret.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
use x25519_dalek::StaticSecret;
let secret_bytes: [u8; 32] = bytes.try_into().map_err(|_| BottleError::InvalidKeyType)?;
let secret = StaticSecret::from(secret_bytes);
let public = x25519_dalek::PublicKey::from(&secret);
Ok(Self {
secret: secret_bytes,
public,
})
}
}
pub struct RsaKey {
private_key: RsaPrivateKey,
public_key: RsaPublicKey,
key_size: usize,
}
impl RsaKey {
pub fn generate<R: RngCore + CryptoRng>(rng: &mut R, bits: usize) -> Result<Self> {
if bits < 512 || !bits.is_multiple_of(8) {
return Err(BottleError::InvalidKeyType);
}
let private_key = RsaPrivateKey::new(rng, bits).map_err(|_| BottleError::InvalidKeyType)?;
let public_key = RsaPublicKey::from(&private_key);
Ok(Self {
private_key,
public_key,
key_size: bits / 8,
})
}
pub fn public_key_bytes(&self) -> Vec<u8> {
vec![]
}
pub fn private_key_bytes(&self) -> Vec<u8> {
vec![]
}
pub fn from_private_key_bytes(_bytes: &[u8]) -> Result<Self> {
Err(BottleError::InvalidKeyType)
}
pub fn encrypt<R: RngCore + CryptoRng>(&self, rng: &mut R, data: &[u8]) -> Result<Vec<u8>> {
use rsa::Oaep;
let padding = Oaep::new::<Sha256>();
self.public_key
.encrypt(rng, padding, data)
.map_err(|e| BottleError::Encryption(format!("RSA encryption failed: {}", e)))
}
pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
use rsa::Oaep;
let padding = Oaep::new::<Sha256>();
self.private_key
.decrypt(padding, ciphertext)
.map_err(|e| BottleError::Decryption(format!("RSA decryption failed: {}", e)))
}
pub fn public_key(&self) -> &RsaPublicKey {
&self.public_key
}
pub fn private_key(&self) -> &RsaPrivateKey {
&self.private_key
}
pub fn key_size(&self) -> usize {
self.key_size
}
}
impl Sign for RsaKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let mut hasher = Sha256::new();
hasher.update(message);
let hashed = hasher.finalize();
self.private_key
.sign(Pkcs1v15Sign::new::<Sha256>(), &hashed)
.map_err(|_| BottleError::VerifyFailed)
}
}
impl Verify for RsaKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let mut hasher = Sha256::new();
hasher.update(message);
let hashed = hasher.finalize();
self.public_key
.verify(Pkcs1v15Sign::new::<Sha256>(), &hashed, signature)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
impl SignerKey for RsaKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "ml-kem")]
pub struct MlKem768Key {
decaps_key: <Kem<MlKem768Params> as KemCore>::DecapsulationKey,
encaps_key: <Kem<MlKem768Params> as KemCore>::EncapsulationKey,
}
#[cfg(feature = "ml-kem")]
impl MlKem768Key {
pub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
use rand_core_09::{CryptoRng as CryptoRng09, RngCore as RngCore09};
struct RngAdapter<'a, R: RngCore + CryptoRng>(&'a mut R);
impl<'a, R: RngCore + CryptoRng> RngCore09 for RngAdapter<'a, R> {
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}
}
impl<'a, R: RngCore + CryptoRng> CryptoRng09 for RngAdapter<'a, R> {}
let mut adapter = RngAdapter(rng);
let (dk, ek) = <Kem<MlKem768Params> as KemCore>::generate(&mut adapter);
Self {
decaps_key: dk,
encaps_key: ek,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
self.encaps_key.as_bytes().to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
let mut result = self.decaps_key.as_bytes().to_vec();
result.extend_from_slice(&self.encaps_key.as_bytes());
result
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
const DK_SIZE: usize = 2400;
const PK_SIZE: usize = 1184;
const TOTAL_SIZE: usize = DK_SIZE + PK_SIZE;
if bytes.len() != TOTAL_SIZE {
return Err(BottleError::InvalidKeyType);
}
let decaps_key_bytes: [u8; DK_SIZE] = bytes[..DK_SIZE]
.try_into()
.map_err(|_| BottleError::InvalidKeyType)?;
let decaps_key = <Kem<MlKem768Params> as KemCore>::DecapsulationKey::from_bytes(
(&decaps_key_bytes).into(),
);
let encaps_key_bytes: [u8; PK_SIZE] = bytes[DK_SIZE..]
.try_into()
.map_err(|_| BottleError::InvalidKeyType)?;
let encaps_key = <Kem<MlKem768Params> as KemCore>::EncapsulationKey::from_bytes(
(&encaps_key_bytes).into(),
);
Ok(Self {
decaps_key,
encaps_key,
})
}
pub fn encapsulation_key(&self) -> &<Kem<MlKem768Params> as KemCore>::EncapsulationKey {
&self.encaps_key
}
pub fn decapsulation_key(&self) -> &<Kem<MlKem768Params> as KemCore>::DecapsulationKey {
&self.decaps_key
}
}
#[cfg(feature = "ml-kem")]
pub struct MlKem1024Key {
decaps_key: <Kem<MlKem1024Params> as KemCore>::DecapsulationKey,
encaps_key: <Kem<MlKem1024Params> as KemCore>::EncapsulationKey,
}
#[cfg(feature = "ml-kem")]
impl MlKem1024Key {
pub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
use rand_core_09::{CryptoRng as CryptoRng09, RngCore as RngCore09};
struct RngAdapter<'a, R: RngCore + CryptoRng>(&'a mut R);
impl<'a, R: RngCore + CryptoRng> RngCore09 for RngAdapter<'a, R> {
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}
}
impl<'a, R: RngCore + CryptoRng> CryptoRng09 for RngAdapter<'a, R> {}
let mut adapter = RngAdapter(rng);
let (dk, ek) = <Kem<MlKem1024Params> as KemCore>::generate(&mut adapter);
Self {
decaps_key: dk,
encaps_key: ek,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
self.encaps_key.as_bytes().to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
let mut result = self.decaps_key.as_bytes().to_vec();
result.extend_from_slice(&self.encaps_key.as_bytes());
result
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
const DK_SIZE: usize = 3168;
const PK_SIZE: usize = 1568;
const TOTAL_SIZE: usize = DK_SIZE + PK_SIZE;
if bytes.len() != TOTAL_SIZE {
return Err(BottleError::InvalidKeyType);
}
let decaps_key_bytes: [u8; DK_SIZE] = bytes[..DK_SIZE]
.try_into()
.map_err(|_| BottleError::InvalidKeyType)?;
let decaps_key = <Kem<MlKem1024Params> as KemCore>::DecapsulationKey::from_bytes(
(&decaps_key_bytes).into(),
);
let encaps_key_bytes: [u8; PK_SIZE] = bytes[DK_SIZE..]
.try_into()
.map_err(|_| BottleError::InvalidKeyType)?;
let encaps_key = <Kem<MlKem1024Params> as KemCore>::EncapsulationKey::from_bytes(
(&encaps_key_bytes).into(),
);
Ok(Self {
decaps_key,
encaps_key,
})
}
pub fn encapsulation_key(&self) -> &<Kem<MlKem1024Params> as KemCore>::EncapsulationKey {
&self.encaps_key
}
pub fn decapsulation_key(&self) -> &<Kem<MlKem1024Params> as KemCore>::DecapsulationKey {
&self.decaps_key
}
}
#[cfg(feature = "post-quantum")]
pub struct MlDsa44Key {
public_key: pqcrypto_dilithium::dilithium2::PublicKey,
secret_key: pqcrypto_dilithium::dilithium2::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl MlDsa44Key {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_dilithium::dilithium2::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
self.public_key.as_bytes().to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
self.secret_key.as_bytes().to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = pqcrypto_dilithium::dilithium2::SecretKey::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
let (_public_key, _) = pqcrypto_dilithium::dilithium2::keypair();
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for MlDsa44Key {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_dilithium::dilithium2::detached_sign(message, &self.secret_key);
Ok(
<pqcrypto_dilithium::dilithium2::DetachedSignature as PqcDetachedSignature>::as_bytes(
&detached_sig,
)
.to_vec(),
)
}
}
#[cfg(feature = "post-quantum")]
impl Verify for MlDsa44Key {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_dilithium::dilithium2::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_dilithium::dilithium2::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for MlDsa44Key {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct MlDsa65Key {
public_key: pqcrypto_dilithium::dilithium3::PublicKey,
secret_key: pqcrypto_dilithium::dilithium3::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl MlDsa65Key {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_dilithium::dilithium3::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_dilithium::dilithium3::PublicKey as PqcPublicKey>::as_bytes(&self.public_key)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_dilithium::dilithium3::SecretKey as PqcSecretKey>::as_bytes(&self.secret_key)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key =
<pqcrypto_dilithium::dilithium3::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for MlDsa65Key {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_dilithium::dilithium3::detached_sign(message, &self.secret_key);
Ok(
<pqcrypto_dilithium::dilithium3::DetachedSignature as PqcDetachedSignature>::as_bytes(
&detached_sig,
)
.to_vec(),
)
}
}
#[cfg(feature = "post-quantum")]
impl Verify for MlDsa65Key {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_dilithium::dilithium3::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_dilithium::dilithium3::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for MlDsa65Key {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct MlDsa87Key {
public_key: pqcrypto_dilithium::dilithium5::PublicKey,
secret_key: pqcrypto_dilithium::dilithium5::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl MlDsa87Key {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_dilithium::dilithium5::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_dilithium::dilithium5::PublicKey as PqcPublicKey>::as_bytes(&self.public_key)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_dilithium::dilithium5::SecretKey as PqcSecretKey>::as_bytes(&self.secret_key)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key =
<pqcrypto_dilithium::dilithium5::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for MlDsa87Key {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_dilithium::dilithium5::detached_sign(message, &self.secret_key);
Ok(
<pqcrypto_dilithium::dilithium5::DetachedSignature as PqcDetachedSignature>::as_bytes(
&detached_sig,
)
.to_vec(),
)
}
}
#[cfg(feature = "post-quantum")]
impl Verify for MlDsa87Key {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_dilithium::dilithium5::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_dilithium::dilithium5::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for MlDsa87Key {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsa128sKey {
public_key: pqcrypto_sphincsplus::sphincsshake256128srobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincsshake256128srobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsa128sKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincsshake256128srobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256128srobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256128srobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincsshake256128srobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsa128sKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincsshake256128srobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincsshake256128srobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsa128sKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincsshake256128srobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincsshake256128srobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsa128sKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsa192sKey {
public_key: pqcrypto_sphincsplus::sphincsshake256192srobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincsshake256192srobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsa192sKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincsshake256192srobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256192srobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256192srobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincsshake256192srobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsa192sKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincsshake256192srobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincsshake256192srobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsa192sKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincsshake256192srobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincsshake256192srobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsa192sKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsa256sKey {
public_key: pqcrypto_sphincsplus::sphincsshake256256srobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincsshake256256srobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsa256sKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincsshake256256srobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256256srobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256256srobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincsshake256256srobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsa256sKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincsshake256256srobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincsshake256256srobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsa256sKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincsshake256256srobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincsshake256256srobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsa256sKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsa128fKey {
public_key: pqcrypto_sphincsplus::sphincsshake256128frobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincsshake256128frobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsa128fKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincsshake256128frobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256128frobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256128frobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincsshake256128frobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsa128fKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincsshake256128frobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincsshake256128frobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsa128fKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincsshake256128frobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincsshake256128frobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsa128fKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsa192fKey {
public_key: pqcrypto_sphincsplus::sphincsshake256192frobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincsshake256192frobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsa192fKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincsshake256192frobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256192frobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256192frobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincsshake256192frobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsa192fKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincsshake256192frobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincsshake256192frobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsa192fKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincsshake256192frobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincsshake256192frobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsa192fKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsa256fKey {
public_key: pqcrypto_sphincsplus::sphincsshake256256frobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincsshake256256frobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsa256fKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincsshake256256frobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256256frobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincsshake256256frobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincsshake256256frobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsa256fKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincsshake256256frobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincsshake256256frobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsa256fKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincsshake256256frobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincsshake256256frobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsa256fKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsaSha2_128sKey {
public_key: pqcrypto_sphincsplus::sphincssha256128srobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincssha256128srobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsaSha2_128sKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincssha256128srobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256128srobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256128srobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincssha256128srobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsaSha2_128sKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincssha256128srobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincssha256128srobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsaSha2_128sKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincssha256128srobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincssha256128srobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsaSha2_128sKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsaSha2_128fKey {
public_key: pqcrypto_sphincsplus::sphincssha256128frobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincssha256128frobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsaSha2_128fKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincssha256128frobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256128frobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256128frobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincssha256128frobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsaSha2_128fKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincssha256128frobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincssha256128frobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsaSha2_128fKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincssha256128frobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincssha256128frobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsaSha2_128fKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsaSha2_192sKey {
public_key: pqcrypto_sphincsplus::sphincssha256192srobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincssha256192srobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsaSha2_192sKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincssha256192srobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256192srobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256192srobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincssha256192srobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsaSha2_192sKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincssha256192srobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincssha256192srobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsaSha2_192sKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincssha256192srobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincssha256192srobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsaSha2_192sKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsaSha2_192fKey {
public_key: pqcrypto_sphincsplus::sphincssha256192frobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincssha256192frobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsaSha2_192fKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincssha256192frobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256192frobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256192frobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincssha256192frobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsaSha2_192fKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincssha256192frobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincssha256192frobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsaSha2_192fKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincssha256192frobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincssha256192frobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsaSha2_192fKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsaSha2_256sKey {
public_key: pqcrypto_sphincsplus::sphincssha256256srobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincssha256256srobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsaSha2_256sKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincssha256256srobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256256srobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256256srobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincssha256256srobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsaSha2_256sKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincssha256256srobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincssha256256srobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsaSha2_256sKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincssha256256srobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincssha256256srobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsaSha2_256sKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}
#[cfg(feature = "post-quantum")]
pub struct SlhDsaSha2_256fKey {
public_key: pqcrypto_sphincsplus::sphincssha256256frobust::PublicKey,
secret_key: pqcrypto_sphincsplus::sphincssha256256frobust::SecretKey,
}
#[cfg(feature = "post-quantum")]
impl SlhDsaSha2_256fKey {
pub fn generate<R: RngCore + CryptoRng>(_rng: &mut R) -> Self {
let (public_key, secret_key) = pqcrypto_sphincsplus::sphincssha256256frobust::keypair();
Self {
public_key,
secret_key,
}
}
pub fn public_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256256frobust::PublicKey as PqcPublicKey>::as_bytes(
&self.public_key,
)
.to_vec()
}
pub fn private_key_bytes(&self) -> Vec<u8> {
<pqcrypto_sphincsplus::sphincssha256256frobust::SecretKey as PqcSecretKey>::as_bytes(
&self.secret_key,
)
.to_vec()
}
pub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self> {
let _secret_key = <pqcrypto_sphincsplus::sphincssha256256frobust::SecretKey as PqcSecretKey>::from_bytes(bytes)
.map_err(|_| BottleError::InvalidKeyType)?;
Err(BottleError::InvalidKeyType)
}
}
#[cfg(feature = "post-quantum")]
impl Sign for SlhDsaSha2_256fKey {
fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>> {
let detached_sig = pqcrypto_sphincsplus::sphincssha256256frobust::detached_sign(
message,
&self.secret_key,
);
Ok(<pqcrypto_sphincsplus::sphincssha256256frobust::DetachedSignature as PqcDetachedSignature>::as_bytes(&detached_sig).to_vec())
}
}
#[cfg(feature = "post-quantum")]
impl Verify for SlhDsaSha2_256fKey {
fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()> {
let detached_sig = <pqcrypto_sphincsplus::sphincssha256256frobust::DetachedSignature as PqcDetachedSignature>::from_bytes(signature)
.map_err(|_| BottleError::VerifyFailed)?;
pqcrypto_sphincsplus::sphincssha256256frobust::verify_detached_signature(
&detached_sig,
message,
&self.public_key,
)
.map_err(|_| BottleError::VerifyFailed)?;
Ok(())
}
}
#[cfg(feature = "post-quantum")]
impl SignerKey for SlhDsaSha2_256fKey {
fn fingerprint(&self) -> Vec<u8> {
crate::hash::sha256(&self.public_key_bytes())
}
fn public_key(&self) -> Vec<u8> {
self.public_key_bytes()
}
}