Skip to main content

DryocSecretBox

Struct DryocSecretBox 

Source
pub struct DryocSecretBox<Mac: ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> { /* private fields */ }
Expand description

An authenticated secret-key encrypted box, compatible with a libsodium box. Use with either VecBox or protected::LockedBox type aliases.

Refer to crate::dryocsecretbox for sample usage.

Implementations§

Source§

impl<Mac: NewByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: NewBytes + ResizableBytes + Zeroize> DryocSecretBox<Mac, Data>

Source

pub fn encrypt<Message: Bytes + ?Sized, Nonce: ByteArray<CRYPTO_SECRETBOX_NONCEBYTES>, SecretKey: ByteArray<CRYPTO_SECRETBOX_KEYBYTES>>( message: &Message, nonce: &Nonce, secret_key: &SecretKey, ) -> Result<Self, Error>

Encrypts a message using secret_key and returns a new DryocSecretBox with ciphertext and tag.

§Errors

Returns Error::InvalidLength with ErrorContext::Message if message is longer than CRYPTO_SECRETBOX_MESSAGEBYTES_MAX, or with ErrorContext::Ciphertext if Data’s ResizableBytes::resize leaves it shorter than message (storage that cannot grow to the message length).

§Panics

Panics if allocation or resizing panics.

Source§

impl<'a, Mac: ByteArray<CRYPTO_SECRETBOX_MACBYTES> + TryFrom<&'a [u8]> + Zeroize, Data: Bytes + From<&'a [u8]> + Zeroize> DryocSecretBox<Mac, Data>

Source

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

Initializes a DryocSecretBox from a slice. Expects the first CRYPTO_SECRETBOX_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§

impl<Mac: ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> DryocSecretBox<Mac, Data>

Source

pub fn from_parts(tag: Mac, data: Data) -> Self

Returns a new box with tag and data, consuming both.

Source

pub fn tag(&self) -> &Mac

Returns the authentication tag.

Source

pub fn data(&self) -> &Data

Returns the ciphertext.

Source

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

Available on crate feature alloc only.

Copies self into a new Vec.

Source

pub fn into_parts(self) -> (Mac, Data)

Moves the tag and data out of this instance, returning them as a tuple.

Source§

impl<Mac: ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> DryocSecretBox<Mac, Data>

Source

pub fn decrypt<Output: ResizableBytes + NewBytes, Nonce: ByteArray<CRYPTO_SECRETBOX_NONCEBYTES>, SecretKey: ByteArray<CRYPTO_SECRETBOX_KEYBYTES>>( &self, nonce: &Nonce, secret_key: &SecretKey, ) -> Result<Output, Error>

Decrypts this box using secret_key.

§Errors

Returns an error if the output storage is shorter than the ciphertext or authentication fails. Authentication fails when the key, nonce, tag, or ciphertext does not match the value used during encryption.

Source

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

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

Source§

impl DryocSecretBox<Mac, Vec<u8>>

Source

pub fn encrypt_to_vecbox<Message: Bytes + ?Sized, Nonce: ByteArray<CRYPTO_SECRETBOX_NONCEBYTES>, SecretKey: ByteArray<CRYPTO_SECRETBOX_KEYBYTES>>( message: &Message, nonce: &Nonce, secret_key: &SecretKey, ) -> Result<Self, Error>

Available on crate feature alloc only.

Encrypts a message using secret_key and returns a new DryocSecretBox with ciphertext and tag.

§Errors

Returns Error::InvalidLength with ErrorContext::Message if message is longer than CRYPTO_SECRETBOX_MESSAGEBYTES_MAX. The Vec storage always resizes, so this is the only error.

Source

pub fn decrypt_to_vec<Nonce: ByteArray<CRYPTO_SECRETBOX_NONCEBYTES>, SecretKey: ByteArray<CRYPTO_SECRETBOX_KEYBYTES>>( &self, nonce: &Nonce, secret_key: &SecretKey, ) -> Result<Vec<u8>, Error>

Available on crate feature alloc only.

Decrypts this box using secret_key and returns the plaintext.

§Errors

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

Source

pub fn into_vec(self) -> Vec<u8> ⓘ

Available on crate feature alloc only.

Consumes this box and returns tag || ciphertext as a Vec.

Trait Implementations§

Source§

impl<Mac: Clone + ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Clone + Bytes + Zeroize> Clone for DryocSecretBox<Mac, Data>

Source§

fn clone(&self) -> Self

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<Mac: Debug + ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Debug + Bytes + Zeroize> Debug for DryocSecretBox<Mac, Data>

Source§

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

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

impl<'de, Mac, Data> Deserialize<'de> for DryocSecretBox<Mac, Data>
where Mac: Deserialize<'de> + ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Deserialize<'de> + Bytes + Zeroize,

Available on crate feature serde only.
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<Mac: ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Bytes + Zeroize> PartialEq for DryocSecretBox<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<Mac, Data> Serialize for DryocSecretBox<Mac, Data>
where Mac: Serialize + ByteArray<CRYPTO_SECRETBOX_MACBYTES> + Zeroize, Data: Serialize + Bytes + Zeroize,

Available on crate feature serde only.
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<Mac, Data> Zeroize for DryocSecretBox<Mac, Data>
where Mac: Zeroize + ByteArray<CRYPTO_SECRETBOX_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<Mac, Data> Freeze for DryocSecretBox<Mac, Data>
where Mac: Freeze, Data: Freeze,

§

impl<Mac, Data> RefUnwindSafe for DryocSecretBox<Mac, Data>
where Mac: RefUnwindSafe, Data: RefUnwindSafe,

§

impl<Mac, Data> Send for DryocSecretBox<Mac, Data>
where Mac: Send, Data: Send,

§

impl<Mac, Data> Sync for DryocSecretBox<Mac, Data>
where Mac: Sync, Data: Sync,

§

impl<Mac, Data> Unpin for DryocSecretBox<Mac, Data>
where Mac: Unpin, Data: Unpin,

§

impl<Mac, Data> UnsafeUnpin for DryocSecretBox<Mac, Data>
where Mac: UnsafeUnpin, Data: UnsafeUnpin,

§

impl<Mac, Data> UnwindSafe for DryocSecretBox<Mac, Data>
where Mac: UnwindSafe, Data: 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<'de, T, C> Deserialize<'de, C> for T
where C: Config, T: SchemaRead<'de, C>,

Source§

fn deserialize(src: &'de [u8], config: C) -> Result<Self::Dst, ReadError>

Deserialize the input bytes into a new Self::Dst.
Source§

fn deserialize_into( src: &'de [u8], dst: &mut MaybeUninit<Self::Dst>, config: C, ) -> Result<(), ReadError>

Deserialize the input bytes into dst.
Source§

impl<'de, T> Deserialize<'de> for T
where T: SchemaRead<'de, Configuration>,

Source§

fn deserialize(src: &'de [u8]) -> Result<Self::Dst, ReadError>

Deserialize the input src bytes into a new Self::Dst.
Source§

fn deserialize_into( src: &'de [u8], dst: &mut MaybeUninit<Self::Dst>, ) -> Result<(), ReadError>

Deserialize the input src bytes into dst.
Source§

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

Source§

impl<T> DeserializeOwned for T

Source§

fn deserialize_from<'de>(src: impl Reader<'de>) -> Result<Self::Dst, ReadError>

Deserialize from the given Reader into a new Self::Dst.
Source§

fn deserialize_from_into<'de>( src: impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>, ) -> Result<(), ReadError>

Deserialize from the given Reader into dst.
Source§

impl<T, C> DeserializeOwned<C> for T
where C: Config, T: SchemaReadOwned<C>,

Source§

fn deserialize_from<'de>(src: impl Reader<'de>) -> Result<Self::Dst, ReadError>

Deserialize from the given Reader into a new Self::Dst.
Source§

fn deserialize_from_into<'de>( src: impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>, ) -> Result<(), ReadError>

Deserialize from the given Reader into dst.
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, C> SchemaReadOwned<C> for T
where C: ConfigCore, T: for<'de> SchemaRead<'de, C>,

Source§

impl<T> Serialize for T

Source§

fn serialize(src: &Self::Src) -> Result<Vec<u8>, WriteError>

Available on crate feature alloc only.
Serialize a serializable type into a Vec of bytes.
Source§

fn serialize_into(dst: impl Writer, src: &Self::Src) -> Result<(), WriteError>

Serialize a serializable type into the given byte buffer. Read more
Source§

fn serialized_size(src: &Self::Src) -> Result<u64, WriteError>

Get the size in bytes of the type when serialized.
Source§

impl<T, C> Serialize<C> for T
where C: Config, T: SchemaWrite<C> + ?Sized,

Source§

fn serialize(src: &Self::Src, config: C) -> Result<Vec<u8>, WriteError>

Available on crate feature alloc only.
Serialize a serializable type into a Vec of bytes.
Source§

fn serialize_into( dst: impl Writer, src: &Self::Src, config: C, ) -> Result<(), WriteError>

Serialize a serializable type into the given Writer. Read more
Source§

fn serialized_size(src: &Self::Src, config: C) -> Result<u64, WriteError>

Get the size in bytes of the type when serialized.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.