secure_gate/macros/fixed.rs
1/// Creates a type alias for a fixed-size secure secret.
2///
3/// This macro generates a type alias to `Fixed<[u8; N]>` with optional visibility.
4/// The generated type inherits all methods from `Fixed`, including `.expose_secret()`.
5///
6/// # Syntax
7///
8/// - `fixed_alias!(vis Name, size);` — visibility required (e.g., `pub`, `pub(crate)`, or omit for private)
9///
10/// # Examples
11///
12/// Public alias:
13/// ```
14/// use secure_gate::fixed_alias;
15/// fixed_alias!(pub Aes256Key, 32);
16/// ```
17///
18/// Private alias:
19/// ```
20/// use secure_gate::fixed_alias;
21/// fixed_alias!(private_key, 32); // No visibility modifier = private
22/// ```
23///
24/// With custom visibility:
25/// ```
26/// use secure_gate::fixed_alias;
27/// fixed_alias!(pub(crate) InternalKey, 64); // Crate-visible
28/// ```
29///
30/// The generated type is zero-cost and works with all features.
31/// For random initialization, use Type::generate() (requires 'rand' feature).
32#[macro_export]
33macro_rules! fixed_alias {
34 ($vis:vis $name:ident, $size:literal) => {
35 #[doc = concat!("Fixed-size secure secret (", stringify!($size), " bytes)")]
36 const _: () = { let _ = [(); $size][0]; };
37 $vis type $name = $crate::Fixed<[u8; $size]>;
38 };
39 ($name:ident, $size:literal) => {
40 #[doc = concat!("Fixed-size secure secret (", stringify!($size), " bytes)")]
41 const _: () = { let _ = [(); $size][0]; };
42 type $name = $crate::Fixed<[u8; $size]>;
43 };
44}
45
46/// Creates a generic (const-sized) fixed secure buffer type.
47///
48/// This macro generates a type alias to `Fixed<[u8; N]>` with a custom doc string.
49/// Useful for libraries providing generic secret buffers.
50///
51/// # Examples
52///
53/// With custom doc:
54/// ```
55/// use secure_gate::fixed_generic_alias;
56/// fixed_generic_alias!(pub GenericBuffer, "Generic secure byte buffer");
57/// ```
58///
59/// With default doc:
60/// ```
61/// use secure_gate::fixed_generic_alias;
62/// fixed_generic_alias!(pub(crate) Buffer);
63/// ```
64/// For random initialization, use `Type::<N>::generate()` (requires 'rand' feature).
65#[macro_export]
66macro_rules! fixed_generic_alias {
67 ($vis:vis $name:ident, $doc:literal) => {
68 #[doc = $doc]
69 $vis type $name<const N: usize> = $crate::Fixed<[u8; N]>;
70 };
71 ($vis:vis $name:ident) => {
72 #[doc = "Fixed-size secure byte buffer"]
73 $vis type $name<const N: usize> = $crate::Fixed<[u8; N]>;
74 };
75}
76
77/// Creates a type alias for a random-only fixed-size secret.
78///
79/// This macro generates a type alias to `FixedRandom<N>`, which can only be
80/// instantiated via `.generate()` (requires the "rand" feature).
81///
82/// # Examples
83///
84/// Public alias:
85/// ```
86/// #[cfg(feature = "rand")]
87/// {
88/// use secure_gate::fixed_alias_random;
89/// fixed_alias_random!(pub MasterKey, 32);
90/// # }
91/// ```
92///
93/// Private alias:
94/// ```
95/// #[cfg(feature = "rand")]
96/// {
97/// use secure_gate::fixed_alias_random;
98/// fixed_alias_random!(PrivateKey, 32); // No visibility modifier = private
99/// # }
100/// ```
101/// Instantiate with Type::generate() (requires 'rand' feature).
102#[macro_export]
103macro_rules! fixed_alias_random {
104 ($vis:vis $name:ident, $size:literal) => {
105 #[doc = concat!("Random-only fixed-size secret (", stringify!($size), " bytes)")]
106 const _: () = { let _ = [(); $size][0]; };
107 $vis type $name = $crate::random::FixedRandom<$size>;
108 };
109 ($name:ident, $size:literal) => {
110 #[doc = concat!("Random-only fixed-size secret (", stringify!($size), " bytes)")]
111 const _: () = { let _ = [(); $size][0]; };
112 type $name = $crate::random::FixedRandom<$size>;
113 };
114}