Skip to main content

DryocBox

Struct DryocBox 

Source
pub struct DryocBox<EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> { /* private fields */ }
Expand description

A libsodium public-key authenticated encrypted box.

Refer to crate::dryocbox for sample usage.

Implementations§

Source§

impl<EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: NewByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: NewBytes + ResizableBytes + Zeroize> DryocBox<EphemeralPublicKey, Mac, Data>

Source

pub fn encrypt<Message: Bytes + ?Sized, Nonce: ByteArray<CRYPTO_BOX_NONCEBYTES>, RecipientPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>, SenderSecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES>>( message: &Message, nonce: &Nonce, recipient_public_key: &RecipientPublicKey, sender_secret_key: &SenderSecretKey, ) -> Result<Self, Error>

Encrypts a message using sender_secret_key for recipient_public_key, and returns a new DryocBox with ciphertext and tag.

§Errors

Returns an error if the message is too long, recipient_public_key is an unacceptable low-order key, or the output storage does not resize to the message length.

Source

pub fn precalc_encrypt<PrecalcSecretKey: ByteArray<CRYPTO_BOX_BEFORENMBYTES> + Zeroize, Message: Bytes + ?Sized, Nonce: ByteArray<CRYPTO_BOX_NONCEBYTES>>( message: &Message, nonce: &Nonce, precalc_secret_key: &PrecalcSecretKey, ) -> Result<Self, Error>

Encrypts a message using precalc_secret_key, and returns a new DryocBox with ciphertext and tag.

§Errors

Returns an error if the message is too long or the output storage does not resize to the message length.

Source§

impl<EphemeralPublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: NewByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: NewBytes + ResizableBytes + Zeroize> DryocBox<EphemeralPublicKey, Mac, Data>

Source

pub fn seal<Message: Bytes + ?Sized, RecipientPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>>( message: &Message, recipient_public_key: &RecipientPublicKey, ) -> Result<Self, Error>

Encrypts a message for recipient_public_key, using an ephemeral secret key and nonce. Returns a new DryocBox with ciphertext, tag, and ephemeral public key.

§Errors

Returns an error if the message is too long, recipient_public_key is an unacceptable low-order key, or the output storage does not resize to the message length.

§Panics

Panics if the operating system’s random number generator fails while creating the ephemeral keypair.

Source§

impl<'a, EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + TryFrom<&'a [u8]> + Zeroize, Mac: ByteArray<CRYPTO_BOX_MACBYTES> + TryFrom<&'a [u8]> + Zeroize, Data: Bytes + From<&'a [u8]> + Zeroize> DryocBox<EphemeralPublicKey, Mac, Data>

Source

pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, Error>

Initializes a DryocBox from a slice. Expects the first CRYPTO_BOX_MACBYTES bytes to contain the message authentication tag, with the remaining bytes containing the encrypted message.

§Errors

Returns an error if bytes is shorter than one authentication tag or the tag cannot be converted to Mac.

Source

pub fn from_sealed_bytes(bytes: &'a [u8]) -> Result<Self, Error>

Initializes a sealed DryocBox from a slice. Expects the first CRYPTO_BOX_PUBLICKEYBYTES bytes to contain the ephemeral public key, the next CRYPTO_BOX_MACBYTES bytes to be the message authentication tag, with the remaining bytes containing the encrypted message.

§Errors

Returns an error if bytes is shorter than one ephemeral public key plus one authentication tag, or if either field cannot be converted to its target type.

Source§

impl<EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> DryocBox<EphemeralPublicKey, Mac, Data>

Source

pub fn from_parts( tag: Mac, data: Data, ephemeral_pk: Option<EphemeralPublicKey>, ) -> Self

Returns a new box with tag, data and (optional) ephemeral_pk, consuming each.

Source

pub fn to_vec(&self) -> Vec<u8>

Copies self into a new Vec

Source

pub fn into_parts(self) -> (Mac, Data, Option<EphemeralPublicKey>)

Moves the tag, data, and (optional) ephemeral public key out of this instance, returning them as a tuple.

Source

pub fn decrypt<Nonce: ByteArray<CRYPTO_BOX_NONCEBYTES>, SenderPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES>, RecipientSecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES>, Output: ResizableBytes + NewBytes>( &self, nonce: &Nonce, sender_public_key: &SenderPublicKey, recipient_secret_key: &RecipientSecretKey, ) -> Result<Output, Error>

Decrypts this box using nonce, recipient_secret_key, and sender_public_key, returning the decrypted message upon success.

§Errors

Returns an error if the ciphertext is too long, sender_public_key is an unacceptable low-order key, the output storage has the wrong length, or authentication fails. Authentication fails for a wrong key, nonce, tag, or ciphertext.

Source

pub fn precalc_decrypt<PrecalcSecretKey: ByteArray<CRYPTO_BOX_BEFORENMBYTES> + Zeroize, Nonce: ByteArray<CRYPTO_BOX_NONCEBYTES>, Output: ResizableBytes + NewBytes>( &self, nonce: &Nonce, precalc_secret_key: &PrecalcSecretKey, ) -> Result<Output, Error>

Decrypts this box using nonce and precalc_secret_key, returning the decrypted message upon success.

§Errors

Returns an error if the ciphertext is too long, the output storage has the wrong length, or authentication fails because the precomputed key, nonce, tag, or ciphertext does not match.

Source

pub fn unseal<RecipientPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, RecipientSecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize, Output: ResizableBytes + NewBytes + Zeroize>( &self, recipient_keypair: &KeyPair<RecipientPublicKey, RecipientSecretKey>, ) -> Result<Output, Error>

Decrypts this sealed box using recipient_keypair.

§Errors

Returns an error if the ciphertext is too long, the box has no ephemeral public key, that key is an unacceptable low-order key, the output storage has the wrong length, or authentication fails. Authentication fails for the wrong recipient key pair or modified box data.

Source

pub fn to_bytes<Bytes: NewBytes + ResizableBytes>(&self) -> Bytes

Copies self into the target. Can be used with protected memory.

Source§

impl DryocBox<PublicKey, Mac, Vec<u8>>

Source

pub fn encrypt_to_vecbox<Message: Bytes + ?Sized, SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES>>( message: &Message, nonce: &Nonce, recipient_public_key: &PublicKey, sender_secret_key: &SecretKey, ) -> Result<Self, Error>

Encrypts a message using sender_secret_key for recipient_public_key, and returns a new DryocBox with ciphertext and tag.

§Errors

Returns an error if the message is too long or recipient_public_key is an unacceptable low-order key.

Source

pub fn precalc_encrypt_to_vecbox<Message: Bytes + ?Sized, PrecalcSecretKey: ByteArray<CRYPTO_BOX_BEFORENMBYTES> + Zeroize>( message: &Message, nonce: &Nonce, precalc_secret_key: &PrecalcSecretKey, ) -> Result<Self, Error>

Encrypts a message using precalc_secret_key, and returns a new DryocBox with ciphertext and tag.

§Errors

Returns an error if the message is too long or the output storage cannot hold the ciphertext.

Source

pub fn seal_to_vecbox<Message: Bytes + ?Sized>( message: &Message, recipient_public_key: &PublicKey, ) -> Result<Self, Error>

Encrypts a message for recipient_public_key, using an ephemeral secret key and nonce, and returns a new DryocBox with the ciphertext, ephemeral public key, and tag.

§Errors

Returns an error if the message is too long or recipient_public_key is an unacceptable low-order key.

§Panics

Panics if the operating system’s random number generator fails while creating the ephemeral keypair.

Source

pub fn decrypt_to_vec<SecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES>>( &self, nonce: &Nonce, sender_public_key: &PublicKey, recipient_secret_key: &SecretKey, ) -> Result<Vec<u8>, Error>

Decrypts this box using nonce, recipient_secret_key and sender_public_key, returning the decrypted message upon success.

§Errors

Returns an error if the ciphertext is too long, sender_public_key is an unacceptable low-order key, or authentication fails because a key, nonce, tag, or ciphertext is wrong.

Source

pub fn precalc_decrypt_to_vec<PrecalcSecretKey: ByteArray<CRYPTO_BOX_BEFORENMBYTES> + Zeroize>( &self, nonce: &Nonce, precalc_secret_key: &PrecalcSecretKey, ) -> Result<Vec<u8>, Error>

Decrypts this box using nonce and precalc_secret_key, returning the decrypted message upon success.

§Errors

Returns an error if the ciphertext is too long or authentication fails because the precomputed key, nonce, tag, or ciphertext does not match.

Source

pub fn unseal_to_vec<RecipientPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, RecipientSecretKey: ByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize>( &self, recipient_keypair: &KeyPair<RecipientPublicKey, RecipientSecretKey>, ) -> Result<Vec<u8>, Error>

Decrypts this sealed box using recipient_keypair.

§Errors

Returns an error if the ciphertext is too long, the box has no ephemeral public key, that key is an unacceptable low-order key, or authentication fails because the recipient key pair or box data is wrong.

Source§

impl<'a, EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Bytes + ResizableBytes + From<&'a [u8]> + Zeroize> DryocBox<EphemeralPublicKey, Mac, Data>

Source

pub fn new_with_data_and_mac(tag: Mac, input: &'a [u8]) -> Self

Returns a new box with ciphertext copied from input and the supplied tag. The box has no ephemeral public key.

Source

pub fn new_with_epk_data_and_mac( ephemeral_pk: EphemeralPublicKey, tag: Mac, input: &'a [u8], ) -> Self

Returns a new sealed box with ciphertext copied from input and the supplied ephemeral_pk and tag.

Trait Implementations§

Source§

impl<EphemeralPublicKey: Clone + ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: Clone + ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Clone + Bytes + Zeroize> Clone for DryocBox<EphemeralPublicKey, Mac, Data>

Source§

fn clone(&self) -> DryocBox<EphemeralPublicKey, Mac, Data>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<EphemeralPublicKey: Debug + ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: Debug + ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Debug + Bytes + Zeroize> Debug for DryocBox<EphemeralPublicKey, Mac, Data>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, EphemeralPublicKey, Mac, Data> Deserialize<'de> for DryocBox<EphemeralPublicKey, Mac, Data>
where EphemeralPublicKey: Deserialize<'de> + ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: Deserialize<'de> + ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Deserialize<'de> + Bytes + Zeroize,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> PartialEq for DryocBox<EphemeralPublicKey, Mac, Data>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<EphemeralPublicKey, Mac, Data> Serialize for DryocBox<EphemeralPublicKey, Mac, Data>
where EphemeralPublicKey: Serialize + ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac: Serialize + ByteArray<CRYPTO_BOX_MACBYTES> + Zeroize, Data: Serialize + Bytes + Zeroize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<EphemeralPublicKey: ByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, Mac, Data> Zeroize for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: Zeroize + ByteArray<CRYPTO_BOX_MACBYTES>, Data: Zeroize + Bytes,

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

§

impl<EphemeralPublicKey, Mac, Data> Freeze for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: Freeze, Data: Freeze, EphemeralPublicKey: Freeze,

§

impl<EphemeralPublicKey, Mac, Data> RefUnwindSafe for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: RefUnwindSafe, Data: RefUnwindSafe, EphemeralPublicKey: RefUnwindSafe,

§

impl<EphemeralPublicKey, Mac, Data> Send for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: Send, Data: Send, EphemeralPublicKey: Send,

§

impl<EphemeralPublicKey, Mac, Data> Sync for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: Sync, Data: Sync, EphemeralPublicKey: Sync,

§

impl<EphemeralPublicKey, Mac, Data> Unpin for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: Unpin, Data: Unpin, EphemeralPublicKey: Unpin,

§

impl<EphemeralPublicKey, Mac, Data> UnsafeUnpin for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: UnsafeUnpin, Data: UnsafeUnpin, EphemeralPublicKey: UnsafeUnpin,

§

impl<EphemeralPublicKey, Mac, Data> UnwindSafe for DryocBox<EphemeralPublicKey, Mac, Data>
where Mac: UnwindSafe, Data: UnwindSafe, EphemeralPublicKey: UnwindSafe,

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.