#private-key #memory #constant-time #security #mlock #passwords #automatic

secstr

A data type suitable for storing sensitive information such as passwords and private keys in memory, featuring constant time equality, mlock and zeroing out

9 unstable releases

0.5.1 Oct 2, 2022
0.5.0 Mar 13, 2022
0.4.0 Feb 8, 2020
0.3.2 May 23, 2019
0.1.0 Sep 9, 2015

#652 in Cryptography

Download history 4969/week @ 2024-07-29 5606/week @ 2024-08-05 5543/week @ 2024-08-12 5544/week @ 2024-08-19 6152/week @ 2024-08-26 4392/week @ 2024-09-02 4649/week @ 2024-09-09 4656/week @ 2024-09-16 5023/week @ 2024-09-23 5271/week @ 2024-09-30 5559/week @ 2024-10-07 5789/week @ 2024-10-14 5766/week @ 2024-10-21 5115/week @ 2024-10-28 5678/week @ 2024-11-04 6759/week @ 2024-11-11

23,476 downloads per month
Used in 42 crates (22 directly)

Unlicense

47KB
959 lines

crates.io API Docs Build Status unlicense

secstr

A Rust library that implements a data type (wrapper around Vec<u8>) suitable for storing sensitive information such as passwords and private keys in memory. Inspired by Haskell securemem and .NET SecureString.

Featuring:

  • constant time comparison (does not short circuit on the first different character; but terminates instantly if strings have different length)
  • automatically zeroing out in the destructor
  • mlock and madvise protection if possible
  • formatting as ***SECRET*** to prevent leaking into logs
  • (optionally) using libsodium (through sodiumoxide's libsodium-sys) for zeroing, comparison, and hashing (std::hash::Hash)
  • (optionally) de/serializable into anything Serde supports as a byte string
  • (optionally) compile-time checked preconditions for the public unsafe API

Usage

extern crate secstr;
use secstr::*;

let pw = SecStr::from("correct horse battery staple");

// Compared in constant time:
// (Obviously, you should store hashes in real apps, not plaintext passwords)
let are_pws_equal = pw == SecStr::from("correct horse battery staple".to_string()); // true

// Formatting, printing without leaking secrets into logs
let text_to_print = format!("{}", SecStr::from("hello")); // "***SECRET***"

// Clearing memory
// THIS IS DONE AUTOMATICALLY IN THE DESTRUCTOR
// (but you can force it)
let mut my_sec = SecStr::from("hello");
my_sec.zero_out();
// (It also sets the length to 0)
assert_eq!(my_sec.unsecure(), b"");

Be careful with SecStr::from: if you have a borrowed string, it will be copied.
Use SecStr::new if you have a Vec<u8>.

Contributing

Please feel free to submit pull requests!

By participating in this project you agree to follow the Contributor Code of Conduct and to release your contributions under the Unlicense.

The list of contributors is available on GitHub.

License

This is free and unencumbered software released into the public domain.
For more information, please refer to the UNLICENSE file or unlicense.org.

Dependencies

~0–4.5MB
~25K SLoC