Module vec_utils

Source
Expand description

§Secure Vector Utilities Module

This module provides secure memory management utilities for vectors and slices, designed specifically for handling sensitive cryptographic material.

§Key Features

  • Memory Zeroing: Securely clear sensitive data from memory
  • Memory Locking: Prevent sensitive data from being swapped to disk
  • Secure Vectors: A vector-like container with enhanced memory protection

§Security Considerations

When working with cryptographic keys, passwords, or other sensitive data, it’s crucial to handle memory securely. This module provides tools to:

  • Prevent sensitive data from being written to disk via swap
  • Ensure memory is properly zeroed when no longer needed
  • Protect against memory-related vulnerabilities

§Usage Example

use libsodium_rs as sodium;
use libsodium_rs::utils::vec_utils;
use sodium::ensure_init;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    ensure_init()?;

    // Create a secure vector for a cryptographic key
    let mut secure_key = vec_utils::secure_vec::<u8>(32)?;

    // Fill it with random data or your key material
    for i in 0..secure_key.len() {
        secure_key[i] = i as u8;
    }

    // Use the key for cryptographic operations...

    // When secure_key goes out of scope, the memory is
    // automatically zeroed and freed
    Ok(())
}

§Relationship to Other Modules

This module is part of the utils module and complements the core memory management functions with vector-specific utilities.

Structs§

SecureVec
A secure vector with enhanced memory protection

Functions§

memzero
Securely zero a slice’s memory
mlock
Lock a vector’s memory to prevent it from being swapped to disk
munlock
Unlock a previously locked vector’s memory
secure_vec
Create a new secure vector with enhanced memory protection