pub struct AsyncCryptoSystemGuard<'a> { /* private fields */ }Expand description
Async cryptosystem guard to help break up heavy blocking operations
Implementations§
Source§impl AsyncCryptoSystemGuard<'_>
impl AsyncCryptoSystemGuard<'_>
Sourcepub fn kind(&self) -> CryptoKind
pub fn kind(&self) -> CryptoKind
The CryptoKind of the guarded cryptosystem.
Sourcepub fn crypto(&self) -> VeilidComponentGuard<'_, Crypto>
pub fn crypto(&self) -> VeilidComponentGuard<'_, Crypto>
Get a guard on the Crypto component that owns this cryptosystem.
Sourcepub async fn cached_dh(
&self,
key: &PublicKey,
secret: &SecretKey,
) -> VeilidAPIResult<SharedSecret>
pub async fn cached_dh( &self, key: &PublicKey, secret: &SecretKey, ) -> VeilidAPIResult<SharedSecret>
Diffie-Hellman shared secret, served from the Crypto DH cache when present.
Local CPU only; awaits a single runtime yield. On a cache miss runs the DH inline (does not
offload to the rayon pool, unlike compute_dh).
Errors VeilidAPIError::Generic if key or secret carries the wrong kind or length, or
(on a cache miss) VeilidAPIError::Internal if key is not a valid curve point and
VeilidAPIError::Generic if the exchange is non-contributory.
Sourcepub async fn random_bytes(&self, len: usize) -> Bytes
pub async fn random_bytes(&self, len: usize) -> Bytes
Generate len cryptographically random bytes.
Sourcepub async fn hash_password(
&self,
password: Bytes,
salt: Bytes,
) -> VeilidAPIResult<String>
pub async fn hash_password( &self, password: Bytes, salt: Bytes, ) -> VeilidAPIResult<String>
Hash a password with the given salt, producing a verifier string.
CPU-heavy (Argon2); offloaded to the rayon thread pool off-WASM. No network or disk.
Errors VeilidAPIError::Generic if salt length is outside the Argon2 bounds or the KDF
fails, VeilidAPIError::ParseError if the salt fails base64 encoding.
Sourcepub async fn verify_password(
&self,
password: Bytes,
password_hash: &str,
) -> VeilidAPIResult<bool>
pub async fn verify_password( &self, password: Bytes, password_hash: &str, ) -> VeilidAPIResult<bool>
Verify a password against a hash produced by hash_password.
CPU-heavy (Argon2); offloaded to the rayon thread pool off-WASM. No network or disk.
Returns Ok(false) on mismatch. Errors VeilidAPIError::ParseError if password_hash is
not a valid PHC string.
Derive a shared secret deterministically from a password and salt.
CPU-heavy (Argon2) but run inline before a single yield (not offloaded), so it holds the thread for the full KDF. No network or disk.
Errors VeilidAPIError::Generic if salt length is outside the Argon2 bounds or the KDF fails.
Sourcepub async fn random_nonce(&self) -> Nonce
pub async fn random_nonce(&self) -> Nonce
Generate a random nonce.
Generate a random shared secret.
Sourcepub async fn compute_dh(
&self,
key: &PublicKey,
secret: &SecretKey,
) -> VeilidAPIResult<SharedSecret>
pub async fn compute_dh( &self, key: &PublicKey, secret: &SecretKey, ) -> VeilidAPIResult<SharedSecret>
Compute the Diffie-Hellman shared secret for a public key and secret key.
Local CPU only, offloaded to the rayon thread pool off-WASM; uncached, recomputes every call.
Use cached_dh to memoize.
Errors VeilidAPIError::Internal if key is not a valid curve point, VeilidAPIError::Generic
if the exchange is non-contributory (low-order public key).
Derive a domain-separated shared secret by hashing the DH result together with domain and the Veilid API domain.
Local CPU only; the DH step is offloaded to the rayon thread pool off-WASM (see
compute_dh).
Errors with the compute_dh errors if the key exchange fails.
Sourcepub async fn hpke_seal(
&self,
recipient: &EncapsulationKey,
aad: Bytes,
plaintext: Bytes,
) -> VeilidAPIResult<Bytes>
pub async fn hpke_seal( &self, recipient: &EncapsulationKey, aad: Bytes, plaintext: Bytes, ) -> VeilidAPIResult<Bytes>
Seal a plaintext to a recipient KEM encapsulation key with HPKE base mode (RFC 9180),
single-shot. aad is authenticated but not encrypted. Returns a self-describing sealed blob.
Sealing is one-way: only the recipient can open the blob, and the sealer cannot decrypt
what it just sealed, unlike the DH shared-secret pattern. Callers who already share a
symmetric key want encrypt_aead instead.
Local CPU only, offloaded to the rayon thread pool off-WASM (a KEM encapsulation always runs).
Errors VeilidAPIError::InvalidArgument if recipient is not a valid key,
VeilidAPIError::Generic if encapsulation fails (including a low-order key).
Sourcepub async fn hpke_open(
&self,
secret: &DecapsulationKey,
aad: Bytes,
sealed: Bytes,
) -> VeilidAPIResult<Bytes>
pub async fn hpke_open( &self, secret: &DecapsulationKey, aad: Bytes, sealed: Bytes, ) -> VeilidAPIResult<Bytes>
Open a sealed blob produced by hpke_seal with the recipient KEM
decapsulation key. aad must match what was supplied at seal. Only the recipient can
open a sealed blob; the sealer cannot.
Local CPU only, offloaded to the rayon thread pool off-WASM (a KEM decapsulation always runs).
Errors VeilidAPIError::ParseError if the blob is truncated or its version is unknown,
VeilidAPIError::InvalidArgument if the blob’s kind is not this cryptosystem’s kind or
secret is not a valid key, VeilidAPIError::Generic if decryption fails (tampered blob,
wrong recipient, or mismatched aad).
Sourcepub async fn generate_keypair(&self) -> KeyPair
pub async fn generate_keypair(&self) -> KeyPair
Generate a new keypair.
Sourcepub async fn generate_kem_keypair(&self) -> KemKeyPair
pub async fn generate_kem_keypair(&self) -> KemKeyPair
Generate a new KEM key pair.
Sourcepub async fn encapsulation_key_from_signing_key(
&self,
key: &PublicKey,
) -> VeilidAPIResult<EncapsulationKey>
pub async fn encapsulation_key_from_signing_key( &self, key: &PublicKey, ) -> VeilidAPIResult<EncapsulationKey>
Derive the KEM encapsulation key corresponding to a signing public key.
VLD0-only bridge (ed25519 to x25519); kinds whose signing and KEM keys are unrelated error
VeilidAPIError::Unimplemented.
Errors VeilidAPIError::InvalidArgument if key is not a valid signing public key.
Sourcepub async fn decapsulation_key_from_signing_secret(
&self,
secret: &SecretKey,
) -> VeilidAPIResult<DecapsulationKey>
pub async fn decapsulation_key_from_signing_secret( &self, secret: &SecretKey, ) -> VeilidAPIResult<DecapsulationKey>
Derive the KEM decapsulation key corresponding to a signing secret key.
VLD0-only bridge (ed25519 to x25519); kinds whose signing and KEM keys are unrelated error
VeilidAPIError::Unimplemented.
Errors VeilidAPIError::InvalidArgument if secret is not a valid signing secret key.
Sourcepub async fn generate_hash(&self, data: Bytes) -> HashDigest
pub async fn generate_hash(&self, data: Bytes) -> HashDigest
Hash a byte buffer.
Sourcepub async fn generate_hash_reader(
&self,
reader: &mut dyn Read,
) -> VeilidAPIResult<PublicKey>
pub async fn generate_hash_reader( &self, reader: &mut dyn Read, ) -> VeilidAPIResult<PublicKey>
Hash the entire contents of a reader.
Errors VeilidAPIError::Generic if reading from reader fails.
Length in bytes of a shared secret.
Sourcepub fn nonce_length(&self) -> usize
pub fn nonce_length(&self) -> usize
Length in bytes of a nonce.
Sourcepub fn hash_digest_length(&self) -> usize
pub fn hash_digest_length(&self) -> usize
Length in bytes of a hash digest.
Sourcepub fn public_key_length(&self) -> usize
pub fn public_key_length(&self) -> usize
Length in bytes of a public key.
Sourcepub fn secret_key_length(&self) -> usize
pub fn secret_key_length(&self) -> usize
Length in bytes of a secret key.
Sourcepub fn encapsulation_key_length(&self) -> usize
pub fn encapsulation_key_length(&self) -> usize
Length in bytes of a KEM encapsulation key.
Sourcepub fn decapsulation_key_length(&self) -> usize
pub fn decapsulation_key_length(&self) -> usize
Length in bytes of a KEM decapsulation key.
Sourcepub fn signature_length(&self) -> usize
pub fn signature_length(&self) -> usize
Length in bytes of a signature.
Sourcepub fn aead_overhead(&self) -> usize
pub fn aead_overhead(&self) -> usize
Number of extra bytes an AEAD operation adds to the ciphertext.
Sourcepub fn default_salt_length(&self) -> usize
pub fn default_salt_length(&self) -> usize
Default salt length in bytes for password hashing.
Validate that a shared secret is well-formed for this cryptosystem.
Errors VeilidAPIError::Generic if secret has the wrong kind or length.
Sourcepub fn check_nonce(&self, nonce: &Nonce) -> VeilidAPIResult<()>
pub fn check_nonce(&self, nonce: &Nonce) -> VeilidAPIResult<()>
Validate that a nonce is well-formed for this cryptosystem.
Errors VeilidAPIError::Generic if nonce has the wrong length.
Sourcepub fn check_hash_digest(&self, hash: &HashDigest) -> VeilidAPIResult<()>
pub fn check_hash_digest(&self, hash: &HashDigest) -> VeilidAPIResult<()>
Validate that a hash digest is well-formed for this cryptosystem.
Errors VeilidAPIError::Generic if hash has the wrong kind or length.
Sourcepub fn check_public_key(&self, key: &PublicKey) -> VeilidAPIResult<()>
pub fn check_public_key(&self, key: &PublicKey) -> VeilidAPIResult<()>
Validate that a public key is well-formed for this cryptosystem.
Errors VeilidAPIError::Generic if key has the wrong kind or length.
Sourcepub fn check_secret_key(&self, key: &SecretKey) -> VeilidAPIResult<()>
pub fn check_secret_key(&self, key: &SecretKey) -> VeilidAPIResult<()>
Validate that a secret key is well-formed for this cryptosystem.
Errors VeilidAPIError::Generic if key has the wrong kind or length.
Sourcepub fn check_signature(&self, signature: &Signature) -> VeilidAPIResult<()>
pub fn check_signature(&self, signature: &Signature) -> VeilidAPIResult<()>
Validate that a signature is well-formed for this cryptosystem.
Errors VeilidAPIError::Generic if signature has the wrong kind or length.
Sourcepub fn check_keypair(&self, keypair: &KeyPair) -> VeilidAPIResult<()>
pub fn check_keypair(&self, keypair: &KeyPair) -> VeilidAPIResult<()>
Validate that a keypair is well-formed for this cryptosystem. Structural check only; see
validate_keypair.
Errors VeilidAPIError::Generic if the pair or either key has the wrong kind or length.
Sourcepub async fn validate_keypair(
&self,
key: &PublicKey,
secret: &SecretKey,
) -> VeilidAPIResult<bool>
pub async fn validate_keypair( &self, key: &PublicKey, secret: &SecretKey, ) -> VeilidAPIResult<bool>
Check that a public key and secret key form a valid keypair.
Returns Ok(false) if they do not match. Errors VeilidAPIError::Generic if key or
secret has the wrong kind or length.
Sourcepub async fn validate_hash(
&self,
data: Bytes,
hash: &HashDigest,
) -> VeilidAPIResult<bool>
pub async fn validate_hash( &self, data: Bytes, hash: &HashDigest, ) -> VeilidAPIResult<bool>
Check that a buffer hashes to the given digest.
Errors VeilidAPIError::Generic if hash has the wrong kind or length.
Sourcepub async fn validate_hash_reader(
&self,
reader: &mut dyn Read,
hash: &HashDigest,
) -> VeilidAPIResult<bool>
pub async fn validate_hash_reader( &self, reader: &mut dyn Read, hash: &HashDigest, ) -> VeilidAPIResult<bool>
Check that a reader’s contents hash to the given digest.
Errors VeilidAPIError::Generic if hash has the wrong kind or length, or if reading from
reader fails.
Sourcepub async fn sign(
&self,
public_key: &PublicKey,
secret: &SecretKey,
data: Bytes,
) -> VeilidAPIResult<Signature>
pub async fn sign( &self, public_key: &PublicKey, secret: &SecretKey, data: Bytes, ) -> VeilidAPIResult<Signature>
Sign a buffer with a keypair, returning a detached signature.
Local CPU only, offloaded to the rayon thread pool off-WASM.
Errors VeilidAPIError::Generic if public_key or secret has the wrong kind or length,
VeilidAPIError::ParseError if they do not form a valid ed25519 keypair,
VeilidAPIError::Internal if signing fails.
Sourcepub async fn sign_in_place(
&self,
public_key: &PublicKey,
secret: &SecretKey,
data: BytesMut,
range: Range<usize>,
sig_idx: usize,
) -> VeilidAPIResult<BytesMut>
pub async fn sign_in_place( &self, public_key: &PublicKey, secret: &SecretKey, data: BytesMut, range: Range<usize>, sig_idx: usize, ) -> VeilidAPIResult<BytesMut>
Sign the bytes in range and write the signature into data at sig_idx, returning the buffer.
Local CPU only, offloaded to the rayon thread pool off-WASM.
Errors VeilidAPIError::Generic if public_key or secret has the wrong kind or length,
VeilidAPIError::ParseError if they do not form a valid ed25519 keypair or sig_idx is out
of bounds, VeilidAPIError::InvalidArgument if range is out of bounds,
VeilidAPIError::Internal if signing fails.
Sourcepub async fn verify(
&self,
public_key: &PublicKey,
data: Bytes,
signature: &Signature,
) -> VeilidAPIResult<bool>
pub async fn verify( &self, public_key: &PublicKey, data: Bytes, signature: &Signature, ) -> VeilidAPIResult<bool>
Verify a detached signature over a buffer against a public key.
Local CPU only, offloaded to the rayon thread pool off-WASM.
Returns Ok(false) if the signature does not match. Errors VeilidAPIError::Generic if
public_key or signature has the wrong kind or length, VeilidAPIError::ParseError if
public_key is not a valid ed25519 point.
Sourcepub async fn verify_in_place(
&self,
public_key: &PublicKey,
data: Bytes,
range: Range<usize>,
sig_idx: usize,
) -> VeilidAPIResult<bool>
pub async fn verify_in_place( &self, public_key: &PublicKey, data: Bytes, range: Range<usize>, sig_idx: usize, ) -> VeilidAPIResult<bool>
Verify the signature at sig_idx over the bytes in range of data against a public key.
Local CPU only, offloaded to the rayon thread pool off-WASM.
Returns Ok(false) if the signature does not match. Errors VeilidAPIError::Generic if
public_key has the wrong kind or length, VeilidAPIError::ParseError if public_key is
not a valid ed25519 point, VeilidAPIError::Internal if range or sig_idx is out of bounds.
Sourcepub async fn decrypt_aead(
&self,
body: Bytes,
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<Bytes>,
) -> VeilidAPIResult<Bytes>
pub async fn decrypt_aead( &self, body: Bytes, nonce: &Nonce, shared_secret: &SharedSecret, associated_data: Option<Bytes>, ) -> VeilidAPIResult<Bytes>
Decrypt and authenticate an AEAD ciphertext into a new buffer.
Local CPU only; offloaded to the rayon thread pool off-WASM once the buffer exceeds the scaling threshold, else run inline.
Errors VeilidAPIError::Generic if nonce or shared_secret has the wrong kind or length,
or if authentication fails (tampered ciphertext, wrong key/nonce, or mismatched
associated_data); VeilidAPIError::Internal on an internal length conversion failure.
Sourcepub async fn decrypt_in_place_aead(
&self,
body: BytesMut,
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<Bytes>,
) -> VeilidAPIResult<BytesMut>
pub async fn decrypt_in_place_aead( &self, body: BytesMut, nonce: &Nonce, shared_secret: &SharedSecret, associated_data: Option<Bytes>, ) -> VeilidAPIResult<BytesMut>
Decrypt and authenticate an AEAD ciphertext in place, returning the truncated plaintext buffer.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Generic if shared_secret has the wrong kind or length, or if
authentication fails (tampered ciphertext, wrong key/nonce, or mismatched associated_data);
VeilidAPIError::Internal on an internal length conversion failure.
Sourcepub async fn encrypt_aead(
&self,
body: Bytes,
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<Bytes>,
) -> VeilidAPIResult<Bytes>
pub async fn encrypt_aead( &self, body: Bytes, nonce: &Nonce, shared_secret: &SharedSecret, associated_data: Option<Bytes>, ) -> VeilidAPIResult<Bytes>
Encrypt and authenticate a buffer with AEAD into a new ciphertext.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Generic if nonce or shared_secret has the wrong kind or length;
VeilidAPIError::Internal on an internal length conversion failure.
Sourcepub async fn encrypt_in_place_aead(
&self,
body: BytesMut,
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<Bytes>,
) -> VeilidAPIResult<BytesMut>
pub async fn encrypt_in_place_aead( &self, body: BytesMut, nonce: &Nonce, shared_secret: &SharedSecret, associated_data: Option<Bytes>, ) -> VeilidAPIResult<BytesMut>
Encrypt and authenticate a buffer with AEAD in place, appending the authentication tag.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Generic if nonce or shared_secret has the wrong kind or length;
VeilidAPIError::Internal on an internal length conversion failure.
Sourcepub async fn crypt_b2b_no_auth(
&self,
in_buf: Bytes,
out_buf: BytesMut,
out_idx: usize,
nonce: &Nonce,
shared_secret: &SharedSecret,
) -> VeilidAPIResult<BytesMut>
pub async fn crypt_b2b_no_auth( &self, in_buf: Bytes, out_buf: BytesMut, out_idx: usize, nonce: &Nonce, shared_secret: &SharedSecret, ) -> VeilidAPIResult<BytesMut>
Unauthenticated buffer-to-buffer crypt: transform in_buf into out_buf starting at out_idx.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Generic if nonce or shared_secret has the wrong kind or length;
VeilidAPIError::Internal on an internal length conversion failure.
Sourcepub async fn crypt_in_place_no_auth(
&self,
body: BytesMut,
range: Range<usize>,
nonce: &Nonce,
shared_secret: &SharedSecret,
) -> VeilidAPIResult<BytesMut>
pub async fn crypt_in_place_no_auth( &self, body: BytesMut, range: Range<usize>, nonce: &Nonce, shared_secret: &SharedSecret, ) -> VeilidAPIResult<BytesMut>
Unauthenticated in-place crypt of the bytes in range.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Internal if range is out of bounds, VeilidAPIError::Generic if
nonce or shared_secret has the wrong kind or length.
Sourcepub async fn crypt_no_auth_aligned_8(
&self,
body: Bytes,
nonce: &Nonce,
shared_secret: &SharedSecret,
) -> VeilidAPIResult<Vec<u8>>
pub async fn crypt_no_auth_aligned_8( &self, body: Bytes, nonce: &Nonce, shared_secret: &SharedSecret, ) -> VeilidAPIResult<Vec<u8>>
Unauthenticated crypt into a fresh 8-byte-aligned output buffer.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Generic if nonce or shared_secret has the wrong kind or length;
VeilidAPIError::Internal on an internal length conversion failure.
Sourcepub async fn crypt_no_auth_unaligned(
&self,
body: Bytes,
nonce: &Nonce,
shared_secret: &SharedSecret,
) -> VeilidAPIResult<Vec<u8>>
pub async fn crypt_no_auth_unaligned( &self, body: Bytes, nonce: &Nonce, shared_secret: &SharedSecret, ) -> VeilidAPIResult<Vec<u8>>
Unauthenticated crypt into a fresh unaligned output buffer.
Local CPU only; offloaded to the rayon thread pool off-WASM for large buffers, else run inline.
Errors VeilidAPIError::Generic if nonce or shared_secret has the wrong kind or length;
VeilidAPIError::Internal on an internal length conversion failure.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for AsyncCryptoSystemGuard<'a>
impl<'a> !UnwindSafe for AsyncCryptoSystemGuard<'a>
impl<'a> Freeze for AsyncCryptoSystemGuard<'a>
impl<'a> Send for AsyncCryptoSystemGuard<'a>
impl<'a> Sync for AsyncCryptoSystemGuard<'a>
impl<'a> Unpin for AsyncCryptoSystemGuard<'a>
impl<'a> UnsafeUnpin for AsyncCryptoSystemGuard<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more