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