secure-gate
Zero-cost, no_std-compatible wrappers for handling sensitive data in memory.
Fixed<T>– stack-allocated, zero-cost wrapper.Dynamic<T>– heap-allocated wrapper with full.into()ergonomics.- When the
zeroizefeature is enabled,FixedZeroizing<T>andDynamicZeroizing<T>provide automatic zeroing on drop.
Now with conversions — the most requested ergonomics upgrade ever.
Installation
[]
= "0.5.8"
Recommended (full power):
= { = "0.5.8", = ["zeroize", "rand", "conversions"] }
Features
| Feature | Description |
|---|---|
zeroize |
Automatic memory wiping on drop (via zeroize + secrecy) — recommended |
rand |
SecureRandomExt::random() — cryptographically secure key generation |
conversions |
NEW — .to_hex(), .to_hex_upper(), .to_base64url(), and .ct_eq() on all fixed secrets |
serde |
Optional serialization (deserialization disabled on Dynamic<T> for security) |
Works in no_std + alloc. Only pay for what you use.
Quick Start
use ;
fixed_alias!;
fixed_alias!;
dynamic_alias!;
// Cryptographically secure random keys
// Full ergonomics — with `conversions` feature
// Heap secrets — pure joy
let pw: Password = "hunter2".into;
assert_eq!;
Secure Conversions — conversions feature
.to_hex()/.to_hex_upper()→ perfect for logging, debugging.to_base64url()→ ideal for JSON export, URLs, config files.ct_eq()→ mandatory for secure equality — prevents timing attacks
Memory Guarantees (zeroize enabled)
| Type | Allocation | Auto-zero | Full wipe | Slack eliminated | Notes |
|---|---|---|---|---|---|
Fixed<T> |
Stack | Yes | Yes | Yes (no heap) | Zero-cost |
Dynamic<T> |
Heap | Yes | Yes | No (until drop) | Use finish_mut() to shrink |
FixedZeroizing<T> |
Stack | Yes | Yes | Yes | RAII wrapper |
DynamicZeroizing<T> |
Heap | Yes | Yes | No (until drop) | SecretBox prevents copies |
Important: DynamicZeroizing<T> uses .expose_secret() — no Deref.
Macros
// Fixed-size secrets
secure! // → Fixed<[u8; 32]>
// Heap secrets
secure! // → Dynamic<String>
secure! // → Dynamic<Vec<u8>>
// Type aliases — the recommended way
fixed_alias!
dynamic_alias!
Example Aliases
fixed_alias!;
fixed_alias!;
dynamic_alias!;
dynamic_alias!;
Zero-cost — proven on real hardware
| Implementation | Median time | Overhead vs raw |
|---|---|---|
raw [u8; 32] |
~460 ps | — |
Fixed<[u8; 32]> |
~460 ps | **+28 ps |
fixed_alias!(Key, 32) |
~475 ps | +13 ps |
Overhead is < 0.1 CPU cycles — indistinguishable from raw arrays.
Migration from v0.4.x
SecureGate<T>→Fixed<T>(stack) orDynamic<T>(heap).expose_secret()→value.expose_secret()- Automatic zeroing →
FixedZeroizing<T>orDynamicZeroizing<T>
Note: .view() and .view_mut() deprecated in v0.5.5 → removed in v0.6.0.
Changelog
License
MIT OR Apache-2.0