use crate::error::Result;
use async_trait::async_trait;
pub trait SymmetricEncrypt {
const KEY_SIZE: usize;
const NONCE_SIZE: usize;
const TAG_SIZE: usize;
const ALGORITHM: &'static str;
fn encrypt(
key: &[u8],
nonce: &[u8],
plaintext: &[u8],
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>>;
fn decrypt(
key: &[u8],
nonce: &[u8],
ciphertext: &[u8],
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>>;
}
pub trait StreamCipher {
fn apply_keystream(&mut self, data: &mut [u8]);
fn seek(&mut self, position: u64);
fn position(&self) -> u64;
}
pub trait AsymmetricEncrypt {
type PublicKey;
type PrivateKey;
const ALGORITHM: &'static str;
fn encrypt(public_key: &Self::PublicKey, plaintext: &[u8]) -> Result<Vec<u8>>;
fn decrypt(private_key: &Self::PrivateKey, ciphertext: &[u8]) -> Result<Vec<u8>>;
}
pub trait KeyExchange {
type PublicKey;
type PrivateKey;
type SharedSecret;
const ALGORITHM: &'static str;
fn generate_keypair() -> Result<(Self::PrivateKey, Self::PublicKey)>;
fn compute_shared_secret(
private_key: &Self::PrivateKey,
peer_public_key: &Self::PublicKey,
) -> Result<Self::SharedSecret>;
}
pub trait KeyEncapsulation {
type PublicKey;
type PrivateKey;
type Ciphertext;
type SharedSecret;
const ALGORITHM: &'static str;
fn generate_keypair() -> Result<(Self::PrivateKey, Self::PublicKey)>;
fn encapsulate(public_key: &Self::PublicKey) -> Result<(Self::Ciphertext, Self::SharedSecret)>;
fn decapsulate(
private_key: &Self::PrivateKey,
ciphertext: &Self::Ciphertext,
) -> Result<Self::SharedSecret>;
}
pub trait Signer {
type PublicKey;
type PrivateKey;
type Signature;
const ALGORITHM: &'static str;
fn generate_keypair() -> Result<(Self::PrivateKey, Self::PublicKey)>;
fn sign(private_key: &Self::PrivateKey, message: &[u8]) -> Result<Self::Signature>;
fn verify(
public_key: &Self::PublicKey,
message: &[u8],
signature: &Self::Signature,
) -> Result<bool>;
}
pub trait BatchVerifier: Signer {
fn verify_batch(items: &[(&Self::PublicKey, &[u8], &Self::Signature)]) -> Result<bool>;
}
pub trait DeterministicSigner: Signer {
fn sign_deterministic(
private_key: &Self::PrivateKey,
message: &[u8],
) -> Result<Self::Signature>;
}
pub trait Hash {
const OUTPUT_SIZE: usize;
const ALGORITHM: &'static str;
fn hash(data: &[u8]) -> Vec<u8>;
fn new() -> Self;
fn update(&mut self, data: &[u8]);
fn finalize(self) -> Vec<u8>;
}
pub trait ExtendableOutputFunction {
const ALGORITHM: &'static str;
fn new() -> Self;
fn update(&mut self, data: &[u8]);
fn squeeze(&mut self, output: &mut [u8]);
fn finalize_xof(self, output_len: usize) -> Vec<u8>;
}
pub trait Mac {
const OUTPUT_SIZE: usize;
const KEY_SIZE: usize;
const ALGORITHM: &'static str;
fn compute(key: &[u8], data: &[u8]) -> Result<Vec<u8>>;
fn verify(key: &[u8], data: &[u8], tag: &[u8]) -> Result<bool>;
}
pub trait KeyDerivation {
const ALGORITHM: &'static str;
fn derive(
input_key_material: &[u8],
salt: Option<&[u8]>,
info: Option<&[u8]>,
output_length: usize,
) -> Result<Vec<u8>>;
}
pub trait PasswordBasedKdf {
type Params;
const ALGORITHM: &'static str;
fn derive(
password: &[u8],
salt: &[u8],
params: &Self::Params,
output_length: usize,
) -> Result<Vec<u8>>;
fn hash_password(password: &[u8], params: &Self::Params) -> Result<String>;
fn verify_password(password: &[u8], hash: &str) -> Result<bool>;
}
pub trait ZkProof {
type Proof;
type PublicInput;
type Witness;
type VerificationKey;
type ProvingKey;
fn prove(
proving_key: &Self::ProvingKey,
public_input: &Self::PublicInput,
witness: &Self::Witness,
) -> Result<Self::Proof>;
fn verify(
verification_key: &Self::VerificationKey,
public_input: &Self::PublicInput,
proof: &Self::Proof,
) -> Result<bool>;
}
pub trait SecretSharing {
type Share;
fn split(secret: &[u8], threshold: usize, total_shares: usize) -> Result<Vec<Self::Share>>;
fn reconstruct(shares: &[Self::Share]) -> Result<Vec<u8>>;
}
#[async_trait]
pub trait ThresholdSigner {
type KeyShare;
type SignatureShare;
type Signature;
type PublicKey;
async fn distributed_keygen(
threshold: usize,
total_participants: usize,
) -> Result<Vec<Self::KeyShare>>;
fn sign_share(key_share: &Self::KeyShare, message: &[u8]) -> Result<Self::SignatureShare>;
fn combine_signatures(
shares: &[Self::SignatureShare],
threshold: usize,
) -> Result<Self::Signature>;
fn verify(
public_key: &Self::PublicKey,
message: &[u8],
signature: &Self::Signature,
) -> Result<bool>;
}
#[async_trait]
pub trait KeyStore: Send + Sync {
async fn store(&self, id: &str, key: &[u8], metadata: Option<&[u8]>) -> Result<()>;
async fn retrieve(&self, id: &str) -> Result<Option<Vec<u8>>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn list(&self) -> Result<Vec<String>>;
async fn exists(&self, id: &str) -> Result<bool>;
}
pub trait SecureRng {
fn fill_bytes(&mut self, dest: &mut [u8]);
fn random_bytes(&mut self, len: usize) -> Vec<u8> {
let mut buf = vec![0u8; len];
self.fill_bytes(&mut buf);
buf
}
fn random_u64(&mut self) -> u64 {
let mut buf = [0u8; 8];
self.fill_bytes(&mut buf);
u64::from_le_bytes(buf)
}
fn random_u32(&mut self) -> u32 {
let mut buf = [0u8; 4];
self.fill_bytes(&mut buf);
u32::from_le_bytes(buf)
}
}
pub trait HybridEncrypt {
type PublicKey;
type PrivateKey;
fn encrypt(public_key: &Self::PublicKey, plaintext: &[u8]) -> Result<Vec<u8>>;
fn decrypt(private_key: &Self::PrivateKey, ciphertext: &[u8]) -> Result<Vec<u8>>;
}
pub trait PostQuantumHybrid {
type ClassicalPublicKey;
type ClassicalPrivateKey;
type PqPublicKey;
type PqPrivateKey;
#[allow(clippy::type_complexity)]
fn generate_keypair() -> Result<(
(Self::ClassicalPrivateKey, Self::PqPrivateKey),
(Self::ClassicalPublicKey, Self::PqPublicKey),
)>;
fn encapsulate(
classical_pk: &Self::ClassicalPublicKey,
pq_pk: &Self::PqPublicKey,
) -> Result<(Vec<u8>, Vec<u8>)>;
fn decapsulate(
classical_sk: &Self::ClassicalPrivateKey,
pq_sk: &Self::PqPrivateKey,
ciphertext: &[u8],
) -> Result<Vec<u8>>;
}