secure_gate/traits/reveal_secret.rs
1//! Traits for controlled, polymorphic secret revelation.
2//!
3//! > **Import path:** `use secure_gate::RevealSecret;`
4//!
5//! This module defines the core `RevealSecret` trait. See
6//! [`revealed_secrets`](crate::traits::revealed_secrets) for
7//! [`EncodedSecret`](crate::EncodedSecret), the wrapper for encoded output.
8//!
9//! The design ensures:
10//! - No implicit borrowing (`Deref`, `AsRef`, etc.) from the secret wrapper itself —
11//! reaching the secret always names a method
12//! - Scoped access is preferred (minimizes lifetime of exposed references)
13//! - Direct exposure is possible but clearly marked as an escape hatch
14//! - Owned consumption is available for FFI hand-off and type migration
15//! - Length metadata (`len`, `is_empty` via [`SecretLen`]) is available without
16//! exposing contents — though length itself can be sensitive; see [`SecretLen`]
17//!
18//! # Three-Tier Access Model
19//!
20//! All secret access follows an explicit hierarchy. Prefer tiers earlier in the list:
21//!
22//! | Tier | Method | When to use |
23//! |------|--------|-------------|
24//! | 1 — Scoped borrow (preferred) | `with_secret` / `with_secret_mut` | Almost all application code |
25//! | 2 — Direct reference (escape hatch) | `expose_secret` / `expose_secret_mut` | FFI, third-party APIs requiring `&T` |
26//! | 3 — Owned consumption | `into_inner` | FFI hand-off, type migration, APIs requiring `T` by value |
27//!
28//! **Audit note:** `into_inner` does not appear in an `expose_secret*` grep sweep —
29//! audit it separately. See SECURITY.md for the full list of auditable access surfaces.
30//!
31//! # Key Traits
32//!
33//! | Trait | Access | Preferred Method | Escape Hatch | Metadata | Feature |
34//! |------------------------|------------|---------------------------|--------------------------|-------------------|-------------|
35//! | [`RevealSecret`] | Read-only | `with_secret` (scoped) | `expose_secret` | — (see [`SecretLen`]) | Always |
36//! | [`crate::RevealSecretMut`] | Mutable | `with_secret_mut` (scoped)| `expose_secret_mut` | Inherits above | Always |
37//!
38//! # Security Model
39//!
40//! - **Core wrappers** (`Fixed<T>`, `Dynamic<T>`) implement both traits → full access.
41//! - **Generated newtypes** (`fixed_newtype!` / `dynamic_newtype!`) implement both, forwarding to the wrapper they hold.
42//! - **`EncodedSecret` implements neither.** It is an output wrapper, not a secret wrapper: its contents are reached through `Deref<Target = str>` and its two named consumers.
43//! - **Zero-cost** — all methods are `#[inline(always)]` where possible.
44//! - **Scoped access preferred** — `with_secret` / `with_secret_mut` limit borrow lifetime, reducing leak risk.
45//! - **Direct exposure** (`expose_secret` / `expose_secret_mut`) is provided for legitimate needs (FFI, third-party APIs), but marked as an escape hatch.
46//! - **Owned consumption** (`into_inner`) is available when the secret must be moved out of the
47//! wrapper. It transfers ownership of the plain value and nothing is copied; the wrapper's
48//! zeroize-on-drop does not follow it, so the caller owns the value's lifetime from there.
49//!
50//! # Note for `RevealSecret` Implementors
51//!
52//! Adding `into_inner` to this trait means every `RevealSecret` implementor must provide
53//! an owned-extraction implementation. For wrappers intentionally limited to borrowing
54//! semantics, implement `into_inner` with `unimplemented!()` or a compile-time guard, and
55//! document the design rationale clearly.
56//!
57//! # Usage Guidelines
58//!
59//! The preferred and recommended way to access secrets is the scoped `with_secret` /
60//! `with_secret_mut` methods. `expose_secret` / `expose_secret_mut` are escape hatches
61//! for rare cases and should be audited closely. `into_inner` is reserved for the uncommon
62//! case where ownership of the inner value is required.
63//!
64//! - **Always prefer scoped methods** (`with_secret`, `with_secret_mut`) in application code.
65//! - Use direct exposure only when necessary (e.g., passing raw pointer + length to C FFI).
66//! - Audit every `expose_secret*` call — they should be rare and well-justified.
67//! - Audit every `into_inner` call — it transfers ownership out of the wrapper's protection.
68//!
69//! # Examples
70//!
71//! Scoped (recommended):
72//!
73//! ```rust
74//! use secure_gate::{Fixed, RevealSecret};
75//!
76//! let secret = Fixed::new([42u8; 4]);
77//! let sum: u32 = secret.with_secret(|bytes| bytes.iter().map(|&b| b as u32).sum());
78//! assert_eq!(sum, 42 * 4);
79//! ```
80//!
81//! Direct (escape hatch – use with caution):
82//!
83//! ```rust
84//! use secure_gate::{Fixed, RevealSecret};
85//!
86//! let secret = Fixed::new([42u8; 4]);
87//!
88//! // Example: FFI call needing raw pointer + length
89//! // unsafe {
90//! // c_function(secret.expose_secret().as_ptr(), secret.len());
91//! // }
92//! ```
93//!
94//! Mutable scoped:
95//!
96//! ```rust
97//! use secure_gate::{Fixed, RevealSecret, RevealSecretMut};
98//!
99//! let mut secret = Fixed::new([0u8; 4]);
100//! secret.with_secret_mut(|bytes| bytes[0] = 99);
101//! assert_eq!(secret.expose_secret()[0], 99);
102//! ```
103//!
104//! Owned consumption (into_inner):
105//!
106//! ```rust
107//! use secure_gate::{Fixed, RevealSecret};
108//!
109//! let key = Fixed::new([0xABu8; 16]);
110//! // Consumes `key` and transfers ownership of the bytes: they move into `owned`
111//! // and an inert sentinel is left for `Fixed::drop` to zeroize in their place.
112//! let owned: [u8; 16] = key.into_inner();
113//! assert_eq!(owned, [0xABu8; 16]);
114//! // `owned` is an ordinary array now — you own its lifetime.
115//! ```
116//!
117//! Polymorphic generic code:
118//!
119//! ```rust
120//! use secure_gate::SecretLen;
121//!
122//! fn print_length<S: SecretLen>(secret: &S) {
123//! println!("Length: {} bytes", secret.len());
124//! }
125//! ```
126//!
127//! These traits are the foundation of secure-gate's security model: all secret access is
128//! explicit, auditable, and controlled. Prefer scoped methods in nearly all cases.
129//!
130//! # Implementation Notes
131//!
132//! Long-lived `expose_secret()` references can defeat scoping — the borrow outlives the
133//! call site and the compiler cannot enforce that the secret is not retained. This is an
134//! intentional escape hatch for FFI and legacy APIs; audit every call site.
135
136/// Read-only access to a wrapped secret.
137///
138/// Implemented for **all** [`Fixed<T>`](crate::Fixed) and [`Dynamic<T>`](crate::Dynamic),
139/// whatever the inner type — length metadata lives in the narrower [`SecretLen`].
140/// Prefer the scoped [`with_secret`](Self::with_secret) method; use
141/// [`expose_secret`](Self::expose_secret) only when a long-lived reference is
142/// unavoidable. See [`RevealSecretMut`](crate::RevealSecretMut) for the mutable
143/// counterpart.
144pub trait RevealSecret {
145 /// The inner secret type being revealed.
146 ///
147 /// This can be a sized type (e.g. `[u8; N]`, `u32`) or unsized (e.g. `str`, `[u8]`).
148 type Inner: ?Sized;
149
150 /// Provides scoped (recommended) read-only access to the secret.
151 ///
152 /// The closure receives a reference that cannot escape — the borrow ends when
153 /// the closure returns, minimizing the lifetime of the exposed secret.
154 /// Prefer this over [`expose_secret`](Self::expose_secret) in all application code.
155 ///
156 /// # The reference cannot escape. A copy of what it points at can.
157 ///
158 /// This method governs *access*. It does not — and cannot — constrain what the
159 /// closure body does with the bytes once it can see them. Every line below compiles,
160 /// and each leaves an owned copy in ordinary memory that this wrapper will never
161 /// wipe, because it does not know the copy exists:
162 ///
163 /// ```rust
164 /// use secure_gate::{Fixed, RevealSecret};
165 ///
166 /// let key = Fixed::new([0xABu8; 32]);
167 ///
168 /// let escaped: [u8; 32] = key.with_secret(|p| *p); // `[u8; N]` is `Copy`
169 /// let also: [u8; 32] = key.with_secret(|&p| p); // same, with no `*` to notice
170 /// assert_eq!(escaped, also); // both outlived the closure
171 /// ```
172 ///
173 /// The deref forms need `Self::Inner: Copy`, so the borrow checker rejects them for
174 /// `Dynamic<Vec<u8>>` and `Dynamic<String>` (`E0507`). **That immunity does not extend
175 /// to a method that copies**, and every inner type has one — `to_vec`, `clone`,
176 /// `to_string`, `<[u8; N]>::try_from`. Those copy *through* the borrow instead of
177 /// moving out of it, so no rule applies and no operator appears in the source. No
178 /// `rustc` or `clippy` lint detects any of it.
179 ///
180 /// Copy wrapper-to-wrapper instead, so the bytes never exist outside something that
181 /// wipes them:
182 ///
183 /// ```rust
184 /// use secure_gate::{Fixed, RevealSecret, RevealSecretMut};
185 ///
186 /// let src = Fixed::new([0x5Au8; 32]);
187 /// let mut dst = Fixed::new([0u8; 32]);
188 ///
189 /// src.with_secret(|s| dst.with_secret_mut(|d| d.copy_from_slice(s)));
190 /// assert!(dst.with_secret(|d| d.iter().all(|&b| b == 0x5A)));
191 /// ```
192 ///
193 /// Where the destination is new, prefer a sized constructor and fill it in place —
194 /// [`Fixed::new_with`](crate::Fixed::new_with) or `Dynamic::new_with(len, |slot| …)`.
195 /// See `SECURITY.md` § "Copying the secret out of a reveal borrow" for the full
196 /// scoping, the detection patterns and what they cannot catch.
197 ///
198 /// # Examples
199 ///
200 /// ```rust
201 /// use secure_gate::{Fixed, RevealSecret};
202 ///
203 /// let secret = Fixed::new([42u8; 4]);
204 /// let sum: u32 = secret.with_secret(|bytes| bytes.iter().map(|&b| b as u32).sum());
205 /// assert_eq!(sum, 42 * 4);
206 /// ```
207 fn with_secret<F, R>(&self, f: F) -> R
208 where
209 F: FnOnce(&Self::Inner) -> R;
210
211 /// Returns a direct (auditable) read-only reference to the secret.
212 ///
213 /// Long-lived `expose_secret()` references can defeat scoping — prefer
214 /// [`with_secret`](Self::with_secret) in application code. Use this only when
215 /// a long-lived reference is unavoidable (e.g. FFI, third-party APIs).
216 ///
217 /// Everything in [`with_secret`](Self::with_secret)'s note on copying applies here
218 /// with a wider window: `*secret.expose_secret()` copies a `Copy` inner value, and
219 /// `secret.expose_secret().to_vec()` copies any of them. There is no closure to bound
220 /// how far the reference travels first, which is why this is the auditable spelling
221 /// rather than the recommended one.
222 ///
223 /// # Coming from `secrecy`
224 ///
225 /// This is the closest analogue to `secrecy`'s `ExposeSecret::expose_secret`, and the
226 /// spelling is the same on purpose — this crate kept that vocabulary when it went its
227 /// own way. What differs is that it is not the only door. As of `secrecy` 0.10.3,
228 /// `fn expose_secret(&self) -> &S` is documented there as *"the only method providing
229 /// access to a secret"*; here it is the auditable escape hatch, and
230 /// [`with_secret`](Self::with_secret) is the recommended path.
231 ///
232 /// That matters because the habit transfers silently. Reaching for the familiar name
233 /// compiles, works, and protects the secret exactly as it did before — so nothing
234 /// signals that the narrower tool exists. `with_secret` confines the borrow to a
235 /// closure, which this method cannot do; neither one stops the body from *copying* the
236 /// secret out. Prefer `with_secret` unless a long-lived reference is genuinely
237 /// unavoidable, and see `SECURITY.md` § "Copying the secret out of a reveal borrow"
238 /// for what neither of them can prevent.
239 ///
240 /// # Examples
241 ///
242 /// ```rust
243 /// use secure_gate::{Fixed, RevealSecret};
244 ///
245 /// let secret = Fixed::new([42u8; 4]);
246 ///
247 /// // Auditable escape hatch — FFI use case:
248 /// // unsafe { c_fn(secret.expose_secret().as_ptr(), secret.len()); }
249 /// let _ = secret.expose_secret();
250 /// ```
251 fn expose_secret(&self) -> &Self::Inner;
252
253 /// Consumes the wrapper and transfers ownership of the plain inner value.
254 ///
255 /// This is the safe, idiomatic path when ownership of the secret is required — for
256 /// example, to hand the value to an API that takes `T` by value, to move between
257 /// wrapper types, or at FFI boundaries where the callee takes ownership.
258 ///
259 /// **Protection ends here.** The returned value is an ordinary `[u8; N]` / `String` /
260 /// `Vec<T>` with no zeroize-on-drop and no redacted `Debug` — you own the secret and
261 /// its lifetime from this call onward. The value is moved, never duplicated-and-kept:
262 /// an inert [`SentinelValue`](crate::SentinelValue) is left for the wrapper's `Drop` to
263 /// zeroize in its place, so exactly one copy survives the call and it is yours. For
264 /// `Dynamic` that move is literal — the allocation itself is handed over, nothing is
265 /// read or written. For `Fixed` the secret is stored inline, so the bytes are
266 /// transferred into your slot and the wrapper's slot receives the sentinel; there is no
267 /// indirection to hand over instead. If you want the protection to continue, do not call this
268 /// — keep the wrapper, or move the value into a new one.
269 ///
270 /// # Availability
271 ///
272 /// Only callable when `Self::Inner: Sized + SentinelValue + Zeroize`. The
273 /// [`SentinelValue`](crate::SentinelValue) bound is required to construct the inert
274 /// placeholder that the wrapper's `Drop` impl runs on after the real secret is moved
275 /// out. (A plain `Default` bound is deliberately **not** used: the standard library
276 /// only implements `Default` for arrays up to 32 elements, which would make
277 /// `into_inner` unusable for `[u8; 64]` and other common key sizes.) The `Zeroize`
278 /// bound is required because the wrapper still zeroizes the sentinel on drop.
279 /// For custom inner types that intentionally omit `SentinelValue` (e.g. key types
280 /// where no safe placeholder value exists), `into_inner` is not callable — use
281 /// `with_secret` or `expose_secret` instead.
282 ///
283 /// The three concrete implementations in this crate all satisfy the bounds:
284 /// - `Fixed<[u8; N]>` — `[u8; N]: SentinelValue + Zeroize` for **any** `N` ✓
285 /// - `Dynamic<String>` — `String: SentinelValue + Zeroize` ✓
286 /// - `Dynamic<Vec<T>>` — `Vec<T>: SentinelValue + Zeroize` ✓
287 ///
288 /// # Allocation Behavior
289 ///
290 /// `Fixed::into_inner` is zero-cost (no allocation). The `Dynamic::into_inner`
291 /// impls allocate one small `Box<T>` sentinel (24 bytes on 64-bit) before swapping
292 /// out the real secret; if that allocation panics (OOM), the original `inner` is
293 /// untouched and `Dynamic::drop` zeroizes the secret during unwind. Confidentiality
294 /// is preserved on the panic path. See the per-impl docs on
295 /// [`Dynamic`](crate::Dynamic) for the exact pattern.
296 ///
297 /// # Examples
298 ///
299 /// ```rust
300 /// use secure_gate::{Fixed, RevealSecret};
301 ///
302 /// let key = Fixed::new([0xABu8; 16]);
303 /// let owned: [u8; 16] = key.into_inner();
304 /// assert_eq!(owned, [0xABu8; 16]);
305 /// // `owned` is a plain array — no wiping, no redaction. You own it.
306 /// ```
307 ///
308 /// ```rust
309 /// # #[cfg(feature = "alloc")]
310 /// # {
311 /// use secure_gate::{Dynamic, RevealSecret};
312 ///
313 /// let pw = Dynamic::<String>::new("hunter2".to_string());
314 /// let owned: String = pw.into_inner();
315 /// assert_eq!(owned, "hunter2");
316 /// // Same allocation, moved out — no copy was made.
317 /// # }
318 /// ```
319 fn into_inner(self) -> Self::Inner
320 where
321 Self: Sized,
322 Self::Inner: Sized + crate::SentinelValue + zeroize::Zeroize;
323}
324
325/// Length metadata for secrets whose inner type has a meaningful length.
326///
327/// Separated from [`RevealSecret`] so the core access trait can be implemented
328/// for **every** inner type — including local user-defined ones (see the
329/// [`CloneableSecret`](crate::CloneableSecret) newtype pattern), which have no
330/// meaningful `len()`. Implemented for `Fixed<[T; N]>`, `Dynamic<String>`, and
331/// `Dynamic<Vec<T>>`.
332///
333/// # Security
334///
335/// These methods do not expose secret **contents** — but that is not the same
336/// as length being harmless, and the distinction matters:
337///
338/// - For `Fixed<[u8; N]>`, `len()` is `N`: a compile-time constant already
339/// visible in the type. Nothing is revealed.
340/// - For variable-length secrets (`Dynamic<String>`, `Dynamic<Vec<u8>>`),
341/// length **can be sensitive**. A password's length narrows a brute-force
342/// search; a token's length can fingerprint its issuer. Treat it as metadata
343/// *about* a secret, not as public data: validate against it (minimum-length
344/// checks, buffer sizing) but never log it, put it in an error message that
345/// leaves the process, or persist it next to an identifier.
346///
347/// This trait is deliberately separate from [`RevealSecret`] so that measuring
348/// a secret is a visible choice. `use secure_gate::SecretLen;` is an audit
349/// marker — `grep SecretLen` finds every module that measures secrets — and
350/// generic code bounded only on `RevealSecret` cannot call `len()`. The other
351/// route, `with_secret(|s| s.len())`, needs no trait but surfaces in the
352/// `with_secret` audit sweep instead. Neither is prevented; both are greppable.
353/// This is accident-prevention, not a barrier.
354///
355/// Constant-time comparison is **not** length-hiding: `ConstantTimeEq` on
356/// variable-length secrets delegates to `subtle`, whose slice comparison
357/// short-circuits when lengths differ, so length *inequality* is observable
358/// through timing whether or not `len()` is ever called. Lengths are usually
359/// public in the protocols where that matters; for password verification, be
360/// aware of it.
361///
362/// # Examples
363///
364/// ```rust
365/// use secure_gate::{Fixed, SecretLen};
366///
367/// let key = Fixed::new([0u8; 32]);
368/// assert_eq!(key.len(), 32);
369/// assert_eq!(key.byte_len(), 32);
370/// assert!(!key.is_empty());
371/// ```
372pub trait SecretLen: RevealSecret {
373 /// Returns the number of elements in the secret, matching the underlying
374 /// container's `len()` — element count for `Vec<T>` and `[T; N]`, byte
375 /// count for `String` (where the element is a byte).
376 fn len(&self) -> usize;
377
378 /// Returns the total size of the secret in bytes.
379 ///
380 /// For single-byte element types (`Vec<u8>`, `String`, `[u8; N]`) this
381 /// equals `len()`. Implementations for multi-byte element types override
382 /// this to return `len() * core::mem::size_of::<T>()`.
383 #[inline(always)]
384 fn byte_len(&self) -> usize {
385 self.len()
386 }
387
388 /// Returns `true` if the secret is empty.
389 #[inline(always)]
390 fn is_empty(&self) -> bool {
391 self.len() == 0
392 }
393}