Skip to main content

AsyncCryptoSystemGuard

Struct AsyncCryptoSystemGuard 

Source
pub struct AsyncCryptoSystemGuard<'a> { /* private fields */ }
Expand description

Async cryptosystem guard to help break up heavy blocking operations

Implementations§

Source§

impl AsyncCryptoSystemGuard<'_>

Source

pub fn kind(&self) -> CryptoKind

The CryptoKind of the guarded cryptosystem.

Source

pub fn crypto(&self) -> VeilidComponentGuard<'_, Crypto>

Get a guard on the Crypto component that owns this cryptosystem.

Source

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.

Source

pub async fn random_bytes(&self, len: usize) -> Bytes

Generate len cryptographically random bytes.

Source

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.

Source

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.

Source

pub async fn derive_shared_secret( &self, password: Bytes, salt: Bytes, ) -> VeilidAPIResult<SharedSecret>

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.

Source

pub async fn random_nonce(&self) -> Nonce

Generate a random nonce.

Source

pub async fn random_shared_secret(&self) -> SharedSecret

Generate a random shared secret.

Source

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).

Source

pub async fn generate_shared_secret( &self, key: &PublicKey, secret: &SecretKey, domain: Bytes, ) -> VeilidAPIResult<SharedSecret>

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.

Source

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).

Source

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).

Source

pub async fn generate_keypair(&self) -> KeyPair

Generate a new keypair.

Source

pub async fn generate_kem_keypair(&self) -> KemKeyPair

Generate a new KEM key pair.

Source

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.

Source

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.

Source

pub async fn generate_hash(&self, data: Bytes) -> HashDigest

Hash a byte buffer.

Source

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.

Source

pub fn shared_secret_length(&self) -> usize

Length in bytes of a shared secret.

Source

pub fn nonce_length(&self) -> usize

Length in bytes of a nonce.

Source

pub fn hash_digest_length(&self) -> usize

Length in bytes of a hash digest.

Source

pub fn public_key_length(&self) -> usize

Length in bytes of a public key.

Source

pub fn secret_key_length(&self) -> usize

Length in bytes of a secret key.

Source

pub fn encapsulation_key_length(&self) -> usize

Length in bytes of a KEM encapsulation key.

Source

pub fn decapsulation_key_length(&self) -> usize

Length in bytes of a KEM decapsulation key.

Source

pub fn signature_length(&self) -> usize

Length in bytes of a signature.

Source

pub fn aead_overhead(&self) -> usize

Number of extra bytes an AEAD operation adds to the ciphertext.

Source

pub fn default_salt_length(&self) -> usize

Default salt length in bytes for password hashing.

Source

pub fn check_shared_secret(&self, secret: &SharedSecret) -> VeilidAPIResult<()>

Validate that a shared secret is well-formed for this cryptosystem.

Errors VeilidAPIError::Generic if secret has the wrong kind or length.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more