wolf_crypto/mac/poly1305/state.rs
1//! Defines the various states that a `Poly1305` instance can be in.
2//!
3//! These states ensure that the MAC computation follows the correct sequence of operations,
4//! enhancing type safety and preventing misuse at compile time.
5//!
6//! # States
7//!
8//! - `Init`: The initial state before any operations have been performed.
9//! - `Ready`: The state after initialization, ready to perform MAC computations.
10//! - `Streaming`: The state during streaming updates of the MAC computation.
11
12use crate::sealed::Sealed;
13
14/// Represents the state of a `Poly1305` instance.
15///
16/// This trait is sealed and cannot be implemented outside of this crate.
17pub trait Poly1305State : Sealed {}
18
19/// Macro to generate state structs and implement necessary traits.
20///
21/// This macro simplifies the creation of new states by generating the struct and implementing the
22/// `Sealed` and `Poly1305State` traits for it.
23///
24/// # Arguments
25///
26/// * `$(#[$meta:meta])*` - Optional attributes for the struct.
27/// * `$vis:vis` - The visibility of the struct.
28/// * `$ident:ident` - The identifier/name of the struct.
29macro_rules! make_state {
30 ($(#[$meta:meta])* $vis:vis $ident:ident) => {
31 $(#[$meta])*
32 $vis struct $ident;
33
34 impl Sealed for $ident {}
35 impl Poly1305State for $ident {}
36 };
37 ($(
38 $(#[$meta:meta])*
39 $vis:vis $ident:ident
40 ),* $(,)?) => {
41 $(
42 make_state! {
43 $(#[$meta])*
44 $vis $ident
45 }
46 )*
47 };
48}
49
50make_state! {
51 /// The initial state of a `Poly1305` instance before any operations.
52 pub Init,
53 /// The ready state of a `Poly1305` instance after initialization.
54 pub Ready,
55 /// The streaming state of a `Poly1305` instance during updates.
56 pub Streaming,
57}