Function stackzero

Source
pub fn stackzero(len: usize)
Expand description

Zero a region of the stack in a way that won’t be optimized out

This function securely zeroes a region of the stack, ensuring that the operation won’t be optimized out by the compiler. This is useful for clearing sensitive data from the stack before returning from a function.

§Security Considerations

  • This function ensures that the stack memory is actually zeroed, even if the compiler would normally optimize out the operation
  • It should be used when sensitive data is stored on the stack and needs to be cleared before returning from a function

§Example

use libsodium_rs as sodium;
use sodium::utils;

fn process_sensitive_data() {
    // Create sensitive data on the stack
    let sensitive_data = [0x01, 0x02, 0x03, 0x04];

    // Use the data for some operation...

    // Securely clear the data from the stack
    utils::stackzero(sensitive_data.len());
}

§Arguments

  • len - The number of bytes to zero on the stack