1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Traits for polymorphic secret handling.
//!
//! > **Note:** All traits in this module are re-exported at the crate root
//! > (`secure_gate::RevealSecret`, not `secure_gate::traits::RevealSecret`).
//! > You should never need to import from `secure_gate::traits::*` directly.
//!
//! This module defines the core traits that enable generic, zero-cost, and secure
//! operations across different secret wrapper types (`Fixed<T>`, `Dynamic<T>`, etc.).
//! These traits allow writing polymorphic code that preserves strong security invariants:
//! explicit access, controlled mutability, timing safety, and opt-in risk features.
//!
//! # Core Traits
//!
//! | Trait | Purpose | Requires Feature | Notes |
//! |------------------------|----------------------------------------------|--------------------------|-----------------------------------------------------------------------|
//! | [`RevealSecret`] | Read-only scoped / direct access | Always available | Preferred: `with_secret` (scoped); escape hatch: `expose_secret` |
//! | [`SecretLen`] | Length metadata (`len`, `byte_len`, …) | Always available | Only for `[T; N]`, `String`, `Vec<T>`; does not expose contents, but length can itself be sensitive — see trait docs |
//! | [`RevealSecretMut`] | Mutable scoped / direct access | Always available | Same preference: `with_secret_mut` over `expose_secret_mut` |
//! | [`SentinelValue`] | Inert placeholder left by `into_inner` | Always available | Implemented for `[T; N]` (any `N`), `String`, `Vec<T>` |
//! | [`FixedStorage`] | Asserts the inner type cannot reallocate | Always available | Required by `Fixed::new`. Implemented for primitives, `[T; N]`, small tuples and `Box<T>`; a custom inner type writes one line. `Vec`/`String` deliberately excluded |
//! | [`ConstantTimeEq`] | Deterministic constant-time equality | `ct-eq` | Timing-attack resistant byte comparison |
//! | [`CloneableSecret`] | Opt-in marker for safe cloning | `cloneable` | Requires explicit impl on inner type; zeroize preserved. See [`SECURITY.md`](https://github.com/Slurp9187/secure-gate/blob/release/0.8/SECURITY.md) for opt-in risk details. |
//! | [`SerializableSecret`] | Opt-in marker for Serde serialization | `serde-serialize` | Serialization exposes secret — use with extreme caution. See [`SECURITY.md`](https://github.com/Slurp9187/secure-gate/blob/release/0.8/SECURITY.md) for opt-in risk details. |
//! | [`EncodableBytes`] | Gates the encoder blanket impls | Any `encoding-*` | Load-bearing: every `To*` blanket is `T: AsRef<[u8]> + EncodableBytes`, which is what keeps `str`, `String` and [`EncodedSecret`] out |
//! | [`SecureEncoding`] | Vestigial marker for byte-shaped types | Any `encoding-*` | Auto-implemented for `T: AsRef<[u8]>`. Nothing in the crate bounds on it; the `To*` traits are gated by [`EncodableBytes`] |
//! | [`SecureDecoding`] | Vestigial marker for string-shaped types | Any `encoding-*` | Auto-implemented for `T: AsRef<str>`. Nothing in the crate bounds on it; the `From*Str` blankets are plain `AsRef<str>` |
//!
//! # Security Guarantees
//!
//! - **No implicit access while held** — Reaching a secret inside `Fixed`/`Dynamic`
//! requires an explicit trait method. `into_inner` ends that protection and hands
//! back the plain value; the encoders return [`EncodedSecret`], which derefs by design
//! - **Scoped preference** — `with_secret` / `with_secret_mut` limit borrow lifetime
//! - **Zero-cost** — All methods use `#[inline(always)]` where possible
//! - **Timing safety** — `ConstantTimeEq` provides constant-time equality
//! - **Opt-in risk** — Cloning and serialization require deliberate marker impls
//! - **Read-only output** — [`EncodedSecret`] exposes its buffer only through `Deref`
//! and its two named consumers; there is no mutable access to encoded output
//!
//! # Feature Gates
//!
//! Some traits are only available when their corresponding Cargo features are enabled:
//!
//! - `ct-eq` → [`ConstantTimeEq`]
//! - `cloneable` → [`CloneableSecret`]
//! - `serde-serialize`→ [`SerializableSecret`]
//! - `encoding-*` → [`SecureEncoding`], [`SecureDecoding`], and per-format traits
//!
//! The encoding traits (`ToHex`, `FromHexStr`, etc.) are re-exported from submodules for convenience.
//!
//! See individual trait docs for detailed usage and examples.
pub use EncodedSecret;
pub use ;
pub use RevealSecretMut;
pub use FixedStorage;
pub use SentinelValue;
pub use ConstantTimeEq;
// Re-export per-format decoding traits (feature-gated; blanket impls return Vec<u8> — alloc required)
pub use FromBase32Str;
pub use FromBase64UrlStr;
pub use FromBech32Str;
pub use FromBech32mStr;
pub use FromHexStr;
// Re-export per-format encoding traits (feature-gated)
// Note: blanket impls of ToBase32, ToBase64Url, ToBech32, ToBech32m require alloc (String output).
// Each trait is exported only with its own encoding feature *and* alloc, because every
// encoder returns EncodedSecret and that type requires alloc.
pub use ToBase32;
pub use ToBase64Url;
pub use Case;
pub use ToBech32;
pub use EncodableBytes;
pub use ;
pub use ;
pub use ;
pub use ToBech32m;
pub use ToHex;
/// Marker trait for types that support secure encoding operations.
///
/// Automatically implemented for any type that implements `AsRef<[u8]>`,
/// such as `&[u8]`, `Vec<u8>`, `[u8; N]`, etc.
///
/// It does **not** gate the encoding traits. Every `To*` blanket is bounded on
/// `AsRef<[u8]> + `[`EncodableBytes`], and this marker is
/// implemented for `str` and `String` too — precisely the types `EncodableBytes`
/// exists to reject. Nothing in the crate bounds on this trait; the 0.9 line removed
/// it. It is retained here for backwards compatibility.
///
/// Since this is a marker trait (no methods), it exists only to allow trait
/// bounds and extension methods to be available where appropriate.
///
/// Requires at least one `encoding-*` feature to be enabled.
/// Marker trait for types that support secure decoding operations.
///
/// Automatically implemented for any type that implements `AsRef<str>`,
/// such as `&str`, `String`, etc.
///
/// It does **not** gate the decoding traits: each `From*Str` blanket is bounded on
/// plain `AsRef<str>` and does not mention this marker. Nothing in the crate bounds
/// on it; the 0.9 line removed it. Retained here for backwards compatibility.
///
/// Like `SecureEncoding`, this is a marker trait with no methods — it exists
/// to allow trait bounds and extension methods where relevant.
///
/// Requires at least one `encoding-*` feature to be enabled.
pub use CloneableSecret;
pub use SerializableSecret;