veilid/veilid-core/src/crypto/crypto_system.rs

114 lines
2.9 KiB
Rust
Raw Normal View History

2023-01-29 18:13:54 +00:00
use super::*;
pub trait CryptoSystem {
// Accessors
2023-02-08 02:44:50 +00:00
fn kind(&self) -> CryptoKind;
2023-01-29 18:13:54 +00:00
fn crypto(&self) -> Crypto;
// Cached Operations
fn cached_dh(
&self,
2023-02-08 02:44:50 +00:00
key: &PublicKey,
secret: &SecretKey,
2023-01-29 18:13:54 +00:00
) -> Result<SharedSecret, VeilidAPIError>;
// Generation
fn random_nonce(&self) -> Nonce;
fn random_shared_secret(&self) -> SharedSecret;
fn compute_dh(
&self,
2023-02-08 02:44:50 +00:00
key: &PublicKey,
secret: &SecretKey,
2023-01-29 18:13:54 +00:00
) -> Result<SharedSecret, VeilidAPIError>;
2023-03-03 15:55:31 +00:00
fn generate_keypair(&self) -> KeyPair;
2023-02-08 02:44:50 +00:00
fn generate_hash(&self, data: &[u8]) -> PublicKey;
2023-01-29 18:13:54 +00:00
fn generate_hash_reader(
&self,
reader: &mut dyn std::io::Read,
2023-02-08 02:44:50 +00:00
) -> Result<PublicKey, VeilidAPIError>;
2023-01-29 18:13:54 +00:00
// Validation
2023-02-08 02:44:50 +00:00
fn validate_keypair(&self, dht_key: &PublicKey, dht_key_secret: &SecretKey) -> bool;
fn validate_hash(&self, data: &[u8], dht_key: &PublicKey) -> bool;
2023-01-29 18:13:54 +00:00
fn validate_hash_reader(
&self,
reader: &mut dyn std::io::Read,
2023-02-24 02:07:46 +00:00
key: &PublicKey,
2023-01-29 18:13:54 +00:00
) -> Result<bool, VeilidAPIError>;
// Distance Metric
2023-02-08 02:44:50 +00:00
fn distance(&self, key1: &PublicKey, key2: &PublicKey) -> PublicKeyDistance;
2023-01-29 18:13:54 +00:00
// Authentication
fn sign(
&self,
2023-02-24 02:07:46 +00:00
key: &PublicKey,
secret: &SecretKey,
2023-01-29 18:13:54 +00:00
data: &[u8],
2023-02-08 02:44:50 +00:00
) -> Result<Signature, VeilidAPIError>;
2023-01-29 18:13:54 +00:00
fn verify(
&self,
2023-02-24 02:07:46 +00:00
key: &PublicKey,
2023-01-29 18:13:54 +00:00
data: &[u8],
2023-02-08 02:44:50 +00:00
signature: &Signature,
2023-01-29 18:13:54 +00:00
) -> Result<(), VeilidAPIError>;
// AEAD Encrypt/Decrypt
fn aead_overhead(&self) -> usize;
fn decrypt_in_place_aead(
&self,
body: &mut Vec<u8>,
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<(), VeilidAPIError>;
fn decrypt_aead(
&self,
body: &[u8],
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>, VeilidAPIError>;
fn encrypt_in_place_aead(
&self,
body: &mut Vec<u8>,
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<(), VeilidAPIError>;
fn encrypt_aead(
&self,
body: &[u8],
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>, VeilidAPIError>;
// NoAuth Encrypt/Decrypt
fn crypt_in_place_no_auth(
&self,
body: &mut Vec<u8>,
nonce: &Nonce,
shared_secret: &SharedSecret,
);
fn crypt_b2b_no_auth(
&self,
in_buf: &[u8],
out_buf: &mut [u8],
nonce: &Nonce,
shared_secret: &SharedSecret,
);
fn crypt_no_auth_aligned_8(
&self,
body: &[u8],
nonce: &Nonce,
shared_secret: &SharedSecret,
) -> Vec<u8>;
fn crypt_no_auth_unaligned(
&self,
body: &[u8],
nonce: &Nonce,
shared_secret: &SharedSecret,
) -> Vec<u8>;
}