wolf_crypto/
macros.rs

1macro_rules! non_fips {
2    ($($item:item)*) => {
3        $(
4            #[cfg_attr(docsrs, doc(cfg(feature = "allow-non-fips")))]
5            #[cfg(feature = "allow-non-fips")]
6            $item
7        )*
8    };
9}
10
11macro_rules! hidden {
12    ($($item:item)*) => {
13        $(
14            #[doc(hidden)]
15            $item
16        )*
17    };
18}
19
20macro_rules! std {
21    ($($item:item)*) => {
22        $(
23            #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
24            #[cfg(feature = "std")]
25            $item
26        )*
27    };
28}
29
30macro_rules! alloc {
31    ($($item:item)*) => {
32        $(
33            #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
34            #[cfg(any(feature = "alloc", test))]
35            $item
36        )*
37    };
38}
39
40macro_rules! panic_api {
41    ($($item:item)*) => {
42        $(
43            #[cfg_attr(docsrs, doc(cfg(feature = "can-panic")))]
44            #[cfg(feature = "can-panic")]
45            $item
46        )*
47    };
48}
49
50macro_rules! no_std_io {
51    ($($item:item)*) => {
52        $(
53            #[cfg_attr(docsrs, doc(cfg(feature = "embedded-io")))]
54            #[cfg(feature = "embedded-io")]
55            $item
56        )*
57    }
58}
59
60#[cfg_attr(not(feature = "allow-non-fips"), allow(unused_macros))]
61macro_rules! io_impls {
62    ($($item:item)*) => {
63        $(
64            #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "embedded-io"))))]
65            #[cfg(any(feature = "std", feature = "embedded-io"))]
66            $item
67        )*
68    };
69}
70
71#[cfg_attr(not(feature = "allow-non-fips"), allow(unused_macros))]
72macro_rules! opaque_dbg {
73    ($struct:ident $(<$lt:lifetime>)?) => {
74        impl $(<$lt>)? ::core::fmt::Debug for $struct $(<$lt>)? {
75            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
76                f.write_str(concat!(stringify!($struct), "{{ ... }}"))
77            }
78        }
79    };
80    ($struct:ident <$($param:ident),*>) => {
81        impl ::core::fmt::Debug for $struct <$($param),*> {
82            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
83                ::core::fmt::Formatter::write_str(
84                    f,
85                    concat!(stringify!($struct), "<", $(stringify!($param)),*, ">")
86                )
87            }
88        }
89    }
90}
91
92#[cfg_attr(not(feature = "allow-non-fips"), allow(unused_macros))]
93macro_rules! into_result {
94    ($res:expr, ok => $ok:expr, err => $err:expr) => {
95        if $res.is_ok() {
96            Ok($ok)
97        } else {
98            Err($err)
99        }
100    };
101}
102
103#[cfg_attr(not(feature = "allow-non-fips"), allow(unused_macros))]
104macro_rules! define_state {
105    (
106        $(#[$meta:meta])*
107        $name:ident
108    ) => {
109        $(#[$meta])*
110        pub struct $name;
111
112        impl $crate::sealed::Sealed for $name {}
113        impl State for $name {}
114    };
115
116    ($(
117        $(#[$meta:meta])*
118        $name:ident
119    ),* $(,)?) => {
120        $(
121            define_state! {
122                $(#[$meta])*
123                $name
124            }
125        )*
126    };
127}
128
129#[cfg_attr(not(feature = "allow-non-fips"), allow(unused_macros))]
130macro_rules! arb_key {
131    (struct $ident:ident :: $construct:ident ([u8; $sz:literal])) => {
132        #[cfg(test)]
133        impl ::proptest::arbitrary::Arbitrary for $ident {
134            type Parameters = ();
135
136            fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
137                use ::proptest::strategy::Strategy as _;
138                ::proptest::prelude::any::<[u8; $sz]>().prop_map(Self::$construct).boxed()
139            }
140
141            type Strategy = ::proptest::strategy::BoxedStrategy<Self>;
142        }
143
144        #[cfg(kani)]
145        impl ::kani::Arbitrary for $ident {
146            fn any() -> Self {
147                Self::$construct(::kani::any())
148            }
149        }
150    };
151    (enum $ident:ident {
152        $(
153            $variant:ident ([u8; $sz:literal])
154        ),*
155        $(,)?
156    }) => {
157        #[cfg(test)]
158        impl ::proptest::arbitrary::Arbitrary for $ident {
159            type Parameters = ();
160
161            fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
162                use ::proptest::strategy::Strategy as _;
163
164                ::proptest::prop_oneof![
165                    $(::proptest::prelude::any::<[u8; $sz]>().prop_map(Self::$variant)),*
166                ].boxed()
167            }
168
169            type Strategy = ::proptest::strategy::BoxedStrategy<Self>;
170        }
171    }
172}
173
174#[cfg(any(test, check, kani))]
175macro_rules! logic {
176    (($premise:expr) ==> ($conclusion:expr)) => {
177        !($premise) || $conclusion
178    };
179    (($conclusion:expr) <== ($premise:expr)) => {
180        logic!(($premise) ==> ($conclusion))
181    };
182    (($a:expr) <==> ($b:expr)) => {
183        logic!(($a) ==> ($b)) && logic!(($a) <== ($b))
184    }
185}
186
187#[cfg(any(test, check, kani))]
188/// Basic macro just so that I can think more clearly about my assertions with infix notation.
189macro_rules! ensure {
190    // implications
191    (($premise:expr) ==> ($conclusion:expr)) => {
192        #[cfg(not(kani))] {
193            assert!(
194                logic!(($premise) ==> ($conclusion)),
195                concat!(stringify!($premise), " -> ", stringify!($conclusion))
196            );
197        }
198        #[cfg(kani)] {
199            kani::assert(
200                logic!(($premise) ==> ($conclusion)),
201                concat!(stringify!($premise), " -> ", stringify!($conclusion))
202            );
203        }
204    };
205    (($conclusion:expr) <== ($premise:expr)) => {
206        #[cfg(not(kani))] {
207            assert!(
208                logic!(($conclusion) <== ($premise)),
209                concat!(stringify!($conclusion), " <- ", stringify!($premise))
210            );
211        }
212        #[cfg(kani)] {
213            kani::assert(
214                logic!(($conclusion) <== ($premise)),
215                concat!(stringify!($conclusion), " <- ", stringify!($premise))
216            );
217        }
218    };
219    // biconditional
220    (($a:expr) <==> ($b:expr)) => {
221        #[cfg(not(kani))] {
222            assert!(
223                logic!(($a) <==> ($b)),
224                concat!(stringify!($a), " <-> ", stringify!($b))
225            )
226        }
227        #[cfg(kani)] {
228            kani::assert(
229                logic!(($a) <==> ($b)),
230                concat!(stringify!($a), " <-> ", stringify!($b))
231            )
232        }
233    };
234}
235
236macro_rules! mark_fips {
237    ($for:ident $(, $sealed:ident)?) => {
238        mark_fips! { @seal_no_ty $for $(, $sealed)? }
239        impl $crate::Fips for $for {}
240    };
241    (@seal_no_ty $for:ident, $sealed:ident) => {
242        impl $crate::sealed::FipsSealed for $for {}
243    };
244    (@seal_no_ty $for:ident) => {};
245    ($for:ident <$($lt:lifetime,)+ $($generic:ident),*> $(, $sealed:ident)?) => {
246        mark_fips!{ @seal $for $($sealed)? $($lt,)* $($generic, )* }
247        impl <$($lt,)* $($generic : $crate::Fips),* >
248        $crate::Fips for $for <$($lt,)* $($generic),* > {}
249    };
250    (@seal $name:ident $sealed:ident $($tt:tt)*) => {
251       impl <$($tt)*> $crate::sealed::FipsSealed for $name <$($tt)*> {}
252    };
253    (@seal $name:ident $($tt:tt)*) => {
254       impl <$($tt)*> $crate::sealed::$sealed for $name <$($tt)*> {}
255    }
256}