embassy-nrf

Crates

git

Versions

nrf9151-s

Flavors

Skip to main content

Symmetric

Struct Symmetric 

Source
pub struct Symmetric<'d, M: Mode> { /* private fields */ }
Expand description

Driver for the symmetric crypto engines: AES, hash and ChaCha.

See the module documentation.

use embassy_nrf::crypto::symmetric::{Sha256, Symmetric};

let mut crypto = Symmetric::new_blocking(p.CRYPTO_SYMMETRIC);
let mut ctx = crypto.hash_start::<Sha256>();
crypto.hash_blocking_update(&mut ctx, b"hello");
let digest = crypto.hash_blocking_finish(ctx);

Implementations§

Source§

impl<'d, M: Mode> Symmetric<'d, M>

Source

pub fn aes_start<C: Cipher>( &mut self, cipher: C, dir: Direction, ) -> AesContext<C>

Starts a cipher operation.

The returned context holds all the state of the operation. Pass it to the other aes_blocking_* methods.

Source

pub fn aes_blocking_aad<C: AuthenticatedCipher>( &mut self, ctx: &mut AesContext<C>, aad: &[u8], last: bool, ) -> Result<(), Error>

Feeds additional authenticated data (AAD).

  • Call it before feeding any payload.
  • Chunks may have any size.
  • last marks the final chunk. It is optional. The AAD phase also ends when the first payload chunk is fed, or when the operation is finished.

Errors: AadAfterPayload, InvalidLength for CCM if the total does not match aad_len.

Source

pub fn aes_blocking_payload<C: Cipher>( &mut self, ctx: &mut AesContext<C>, input: &[u8], output: &mut [u8], last: bool, ) -> Result<(), Error>

Processes payload data from input into output.

  • input and output must have the same length. For AesCmac, output is ignored and may be empty.
  • last marks the final chunk.
  • ECB and CBC: every chunk must be a multiple of the block length.
  • GCM: every chunk except the last must be a multiple of the block length.
  • CTR, CMAC and CCM: chunks may have any length.

Errors: InvalidLength.

Source

pub fn aes_blocking_payload_in_place<C: Cipher>( &mut self, ctx: &mut AesContext<C>, data: &mut [u8], last: bool, ) -> Result<(), Error>

Processes payload data in place. See Self::aes_blocking_payload.

Source

pub fn aes_blocking_finish<C: Cipher>( &mut self, ctx: AesContext<C>, ) -> Result<Option<[u8; 16]>, Error>

Finishes the operation.

For CMAC, CCM and GCM this returns the authentication tag. For CCM only the first tag_len bytes of it are valid. For the other modes it returns None.

When decrypting with an authenticated mode, compare the returned tag with the received one in constant time before using the plaintext.

Errors: InvalidLength for CCM if the totals do not match aad_len and payload_len.

Source§

impl<'d, M: Mode> Symmetric<'d, M>

Source

pub fn chacha_start( &mut self, variant: ChaChaVariant, key: &[u8; 32], nonce: &[u8; 12], counter: u32, ) -> ChaChaContext

Starts a ChaCha keystream with the given key, nonce and initial block counter.

The (key, nonce) pair must never be reused.

Source

pub fn chacha_blocking_apply_keystream( &mut self, ctx: &mut ChaChaContext, input: &[u8], output: &mut [u8], ) -> Result<(), Error>

XORs the next bytes of the keystream into input, writing the result to output.

Encryption and decryption are the same operation. Data may have any length.

Errors: InvalidLength if input and output have different lengths.

Source

pub fn chacha_blocking_apply_keystream_in_place( &mut self, ctx: &mut ChaChaContext, data: &mut [u8], )

XORs the next bytes of the keystream into data in place.

Source§

impl<'d, M: Mode> Symmetric<'d, M>

Source

pub fn chachapoly_start( &mut self, variant: ChaChaVariant, key: &[u8; 32], nonce: &[u8; 12], dir: Direction, ) -> ChaChaPolyContext

Starts a ChaCha-Poly1305 authenticated encryption or decryption. With ChaChaVariant::ChaCha20 this is RFC 8439 ChaCha20-Poly1305.

The (key, nonce) pair must never be reused. The returned context holds all the state of the operation. Pass it to the other chachapoly_blocking_* methods.

Source

pub fn chachapoly_blocking_aad( &mut self, ctx: &mut ChaChaPolyContext, aad: &[u8], last: bool, ) -> Result<(), Error>

Feeds additional authenticated data (AAD).

  • Call it before feeding any payload.
  • Chunks may have any size.
  • last marks the final chunk. It is optional. The AAD phase also ends when the first payload chunk is fed, or when the operation is finished.

Errors: AadAfterPayload.

Source

pub fn chachapoly_blocking_payload( &mut self, ctx: &mut ChaChaPolyContext, input: &[u8], output: &mut [u8], last: bool, ) -> Result<(), Error>

Processes payload data from input into output.

  • input and output must have the same length.
  • last marks the final chunk.
  • Every chunk except the last must be a multiple of the block length, 64 bytes.

Errors: InvalidLength.

Source

pub fn chachapoly_blocking_payload_in_place( &mut self, ctx: &mut ChaChaPolyContext, data: &mut [u8], last: bool, ) -> Result<(), Error>

Processes payload data in place. See Self::chachapoly_blocking_payload.

Source

pub fn chachapoly_blocking_finish( &mut self, ctx: ChaChaPolyContext, ) -> Result<[u8; 16], Error>

Finishes the operation and returns the 16-byte Poly1305 tag.

When decrypting, compare the returned tag with the received one in constant time before using the plaintext.

Source§

impl<'d, M: Mode> Symmetric<'d, M>

Source

pub fn hash_start<A: HashAlgorithm>(&mut self) -> HashContext<A>

Starts a hash computation with algorithm A.

Source

pub fn hmac_start<A: HashAlgorithm>(&mut self, key: &[u8]) -> HmacContext<A>

Starts an HMAC computation with algorithm A and the given key.

The key may have any length.

Source

pub fn hash_blocking_update<H: DigestContext>( &mut self, ctx: &mut H, data: &[u8], )

Feeds message data into a hash or HMAC context.

Data may be fed in chunks of any size.

Source

pub fn hash_blocking_finish<H: DigestContext>( &mut self, ctx: H, ) -> <H::Algorithm as HashAlgorithm>::Digest

Finishes a hash or HMAC context and returns the digest.

Source§

impl<'d> Symmetric<'d, Blocking>

Source

pub fn new_blocking(_peri: Peri<'d, CRYPTO_SYMMETRIC>) -> Self

Creates a new blocking driver for the symmetric engines.

Auto Trait Implementations§

§

impl<'d, M> Freeze for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: Freeze,

§

impl<'d, M> RefUnwindSafe for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: RefUnwindSafe,

§

impl<'d, M> Send for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: Send,

§

impl<'d, M> Sync for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: Sync,

§

impl<'d, M> Unpin for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: Unpin,

§

impl<'d, M> UnsafeUnpin for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: UnsafeUnpin,

§

impl<'d, M> UnwindSafe for Symmetric<'d, M>
where PhantomData<(&'d (), M)>: 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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
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> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
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> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
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, <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<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.