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>
impl<'d, M: Mode> Symmetric<'d, M>
Sourcepub fn aes_start<C: Cipher>(
&mut self,
cipher: C,
dir: Direction,
) -> AesContext<C>
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.
Sourcepub fn aes_blocking_aad<C: AuthenticatedCipher>(
&mut self,
ctx: &mut AesContext<C>,
aad: &[u8],
last: bool,
) -> Result<(), Error>
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.
lastmarks 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.
Sourcepub fn aes_blocking_payload<C: Cipher>(
&mut self,
ctx: &mut AesContext<C>,
input: &[u8],
output: &mut [u8],
last: bool,
) -> Result<(), Error>
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.
inputandoutputmust have the same length. ForAesCmac,outputis ignored and may be empty.lastmarks 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.
Sourcepub fn aes_blocking_payload_in_place<C: Cipher>(
&mut self,
ctx: &mut AesContext<C>,
data: &mut [u8],
last: bool,
) -> Result<(), Error>
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.
Sourcepub fn aes_blocking_finish<C: Cipher>(
&mut self,
ctx: AesContext<C>,
) -> Result<Option<[u8; 16]>, Error>
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>
impl<'d, M: Mode> Symmetric<'d, M>
Sourcepub fn chacha_start(
&mut self,
variant: ChaChaVariant,
key: &[u8; 32],
nonce: &[u8; 12],
counter: u32,
) -> ChaChaContext
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.
Sourcepub fn chacha_blocking_apply_keystream(
&mut self,
ctx: &mut ChaChaContext,
input: &[u8],
output: &mut [u8],
) -> Result<(), Error>
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.
Sourcepub fn chacha_blocking_apply_keystream_in_place(
&mut self,
ctx: &mut ChaChaContext,
data: &mut [u8],
)
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>
impl<'d, M: Mode> Symmetric<'d, M>
Sourcepub fn chachapoly_start(
&mut self,
variant: ChaChaVariant,
key: &[u8; 32],
nonce: &[u8; 12],
dir: Direction,
) -> ChaChaPolyContext
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.
Sourcepub fn chachapoly_blocking_aad(
&mut self,
ctx: &mut ChaChaPolyContext,
aad: &[u8],
last: bool,
) -> Result<(), Error>
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.
lastmarks 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.
Sourcepub fn chachapoly_blocking_payload(
&mut self,
ctx: &mut ChaChaPolyContext,
input: &[u8],
output: &mut [u8],
last: bool,
) -> Result<(), Error>
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.
inputandoutputmust have the same length.lastmarks the final chunk.- Every chunk except the last must be a multiple of the block length, 64 bytes.
Errors: InvalidLength.
Sourcepub fn chachapoly_blocking_payload_in_place(
&mut self,
ctx: &mut ChaChaPolyContext,
data: &mut [u8],
last: bool,
) -> Result<(), Error>
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.
Sourcepub fn chachapoly_blocking_finish(
&mut self,
ctx: ChaChaPolyContext,
) -> Result<[u8; 16], Error>
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>
impl<'d, M: Mode> Symmetric<'d, M>
Sourcepub fn hash_start<A: HashAlgorithm>(&mut self) -> HashContext<A>
pub fn hash_start<A: HashAlgorithm>(&mut self) -> HashContext<A>
Starts a hash computation with algorithm A.
Sourcepub fn hmac_start<A: HashAlgorithm>(&mut self, key: &[u8]) -> HmacContext<A>
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.
Sourcepub fn hash_blocking_update<H: DigestContext>(
&mut self,
ctx: &mut H,
data: &[u8],
)
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.
Sourcepub fn hash_blocking_finish<H: DigestContext>(
&mut self,
ctx: H,
) -> <H::Algorithm as HashAlgorithm>::Digest
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>
impl<'d> Symmetric<'d, Blocking>
Sourcepub fn new_blocking(_peri: Peri<'d, CRYPTO_SYMMETRIC>) -> Self
pub fn new_blocking(_peri: Peri<'d, CRYPTO_SYMMETRIC>) -> Self
Creates a new blocking driver for the symmetric engines.