| Crates.io | secbits |
| lib.rs | secbits |
| version | 0.3.3 |
| created_at | 2025-05-05 03:52:57.981786+00 |
| updated_at | 2025-06-23 01:42:15.912805+00 |
| description | A library for secure memory handling featuring |
| homepage | |
| repository | https://github.com/tecposter/security/tree/main/secbits |
| max_upload_size | |
| id | 1660133 |
| size | 33,471 |
A Rust library for secure memory handling with enhanced protection against common vulnerabilities and memory inspection attacks.
Confidentiality: Memory is locked and wiped on release
Integrity: Write access is controlled via guards
Availability: Prevents accidental exposure via core dumps
Least Privilege: Memory starts as no-access, transitions only when needed
Use Case: Sensitive data handling (cryptographic keys, passwords, PII)
Add to Cargo.toml:
[dependencies]
secbits = "0.3.0"
use secbits::SecBytes;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create secure storage
let mut secret = SecBytes::from_bytes("my_secret".as_bytes().to_vec())?;
// Store sensitive data (source gets zeroed)
secret.edit()?.append(b"extra data".to_vec())?;
// Read access
{
let view = secret.view()?;
assert_eq!(view.as_slice(), b"my_secretextra data");
} // drop view
// Write access (exclusive)
{
let mut edit = secret.edit()?;
edit.as_slice()[..3].copy_from_slice(b"NEW");
} // drop edit
println!("{:?}", std::str::from_utf8(secret.view()?.as_slice()));
assert_eq!(secret.view()?.as_slice(), b"NEWsecretextra data");
Ok(())
} // Memory automatically unlocked and zeroed here
pub struct SecSpace {
ptr: NonNull<u8>, // Non-null pointer to memory region
cap: usize, // Capacity in bytes (always page-aligned)
pkey: Option<i32>,
}
Key Features:
ProtectionMode::None - No access (default)ProtectionMode::Read - Read-onlyProtectionMode::ReadWrite - Read-writeSet memory to RW mode
Zero using platform-secure methods
Unlock and deallocate
pub struct SecBytes {
mem: SecSpace,
len: usize,
reader_count: AtomicUsize,
}
Key Features:
SecReadBytes: Shared read access (RO mode)SecWriteBytes: Exclusive write access (RW mode)Multiple readers allowed
Writers get exclusive access via &mut
// Always use RAII guards
{
let view = secret.read()?; // Auto sets RO
// use view...
} // Auto resets to NOACCESS
// Source data gets zeroed automatically
secret.edit()?.append(&mut sensitive_data)?;
Guarantees
Limitations