Poly1305

Struct Poly1305 

Source
pub struct Poly1305<State: Poly1305State = Init> { /* private fields */ }
Available on crate feature allow-non-fips only.
Expand description

The Poly1305 Message Authentication Code (MAC)

§Example

use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [7u8; 32].into();

let tag = Poly1305::new(key.as_ref())
    .aead_padding_ct()
    .update_ct(b"hello world")
    .update_ct(b", how are you")
    .finalize()
    .unwrap();

let o_tag = Poly1305::new(key.as_ref())
    .mac(b"hello world, how are you", b"")
    .unwrap();

assert_eq!(tag, o_tag);

Implementations§

Source§

impl Poly1305<Init>

Source

pub fn new<K: GenericKey>(key: K) -> Poly1305<Ready>

Creates a new Poly1305 instance with the provided key.

§Arguments
  • key - The secret key material used for MAC computation.
§Example
use wolf_crypto::mac::{Poly1305, poly1305::Key};

let key: Key = [42u8; 32].into();
let poly = Poly1305::new(key.as_ref());
Source§

impl Poly1305<Ready>

Source

pub fn mac<A: Aad>(self, input: &[u8], aad: A) -> Result<Tag, Unspecified>

Computes the MAC for the given input and additional data. This uses the TLS AEAD padding scheme. If this is undesirable, consider calling update followed by finalize manually.

§Arguments
  • input - A byte slice representing the message to authenticate.
  • aad - Any additional authenticated data.
§Returns

The associated authentication tag.

§Errors
  • The length of the aad is greater than u32::MAX.
  • The length of the input is greater than u32::MAX.
§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let tag = Poly1305::new(key.as_ref())
    .mac(b"message", b"aad")
    .unwrap();
Source

pub const fn aead_padding(self) -> StreamPoly1305Aead

Transitions the Poly1305 instance into the streaming state with the TLS AEAD padding scheme.

§Returns

A StreamPoly1305Aead instance for continued updates.

§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let stream = Poly1305::new(key.as_ref())
    .aead_padding()
    .update(b"chunk1")?
    .update(b"chunk2")?;
Source

pub const fn normal(self) -> StreamPoly1305

Transitions the Poly1305 instance into the streaming state.

§Returns

A StreamPoly1305 instance for continued updates.

§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let stream = Poly1305::new(key.as_ref()).normal()
    .update(b"chunk1")?
    .update(b"chunk2")?;
Source

pub const fn aead_padding_ct(self) -> CtPoly1305Aead

Transitions the Poly1305 instance into the streaming state with the TLS AEAD padding scheme.

The distinction between this and the standard aead_padding is that this accumulates errors up until the point of finalization in constant time.

§Returns

A CtPoly1305Aead instance for continued updates.

§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let stream = Poly1305::new(key.as_ref())
    .aead_padding_ct()
    .update_ct(b"chunk1")
    .update_ct(b"chunk2");
Source

pub const fn normal_ct(self) -> CtPoly1305

Transitions the Poly1305 instance into the streaming state.

The distinction between this and the standard normal is that this accumulates errors up until the point of finalization in constant time.

§Returns

A CtPoly1305 instance for continued updates.

§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let stream = Poly1305::new(key.as_ref()).normal_ct()
    .update_ct(b"chunk1")
    .update_ct(b"chunk2");
Source

pub fn update(self, input: &[u8]) -> Result<StreamPoly1305, Unspecified>

Updates the Poly1305 instance with additional input, transitioning it to a streaming state.

§Arguments
  • input - A byte slice representing the data to include in the MAC computation.
§Returns

A StreamPoly1305 instance for continued updates.

§Errors

If the length of input is greater than u32::MAX.

§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let stream = Poly1305::new(key.as_ref())
    .update(b"chunk1")?
    .update(b"chunk2")?;
Source

pub fn update_ct(self, input: &[u8]) -> CtPoly1305

Updates the Poly1305 instance with additional input in a constant-time manner.

§Arguments
  • input - A byte slice representing the data to include in the MAC computation.
§Returns

A CtPoly1305 instance containing the updated state.

§Example
use wolf_crypto::{mac::{Poly1305, poly1305::Key}, aead::Tag};

let key: Key = [42u8; 32].into();
let tag = Poly1305::new(key.as_ref())
    .update_ct(b"sensitive ")
    .update_ct(b"chunks")
    .finalize()
    .unwrap();

let o_tag = Poly1305::new(key.as_ref())
    .update_ct(b"sensitive chunks")
    .finalize().unwrap();

assert_eq!(tag, o_tag);

Trait Implementations§

Source§

impl Debug for Poly1305

Source§

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

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

impl<State: Poly1305State> From<Poly1305<State>> for Unspecified

Source§

fn from(_value: Poly1305<State>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<State> Freeze for Poly1305<State>

§

impl<State> RefUnwindSafe for Poly1305<State>
where State: RefUnwindSafe,

§

impl<State> Send for Poly1305<State>
where State: Send,

§

impl<State> Sync for Poly1305<State>
where State: Sync,

§

impl<State> Unpin for Poly1305<State>
where State: Unpin,

§

impl<State> UnwindSafe for Poly1305<State>
where State: 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> 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, 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.