Vitamin C Protected
This crate is part of the Vitamin C framework to make cryptography code healthy.
Safe wrappers for sensitive data
Protected is a set of types that remove some of the sharp edges of working with sensitive data in Rust.
Its interface is conceptually similar to Option or Result.
Sensitive data footguns
Rust is a safe language, but it's still possible to make mistakes when working with sensitive data. These can include (but are not limited to):
- Not zeroizing sensitive data when it's no longer needed
- Accidentally leaking sensitive data in logs or error messages
- Performing comparison operations on sensitive data in a way that leaks timing information
- Serializing sensitive data in a way that leaks information
Protected and the other types in this crate aim to make it easier to avoid these mistakes.
Usage
The Protected type is the most basic building block in this crate.
You can use it to wrap any type that you want to protect so long as it implements the Zeroize trait.
use Protected;
let x = new;
Protected will call zeroize on the inner value when it goes out of scope.
Because that wipe is a real Drop, Protected is never Copy, even when the
inner type is: unlike a move, a Copy value has no single owner whose drop can
wipe it. Duplicating the wrapper is an explicit clone(), and each clone is
wiped when it drops. See the Protected type docs for the full rationale.
risky_unwrap is the escape hatch: it hands back the plain inner value along
with the obligation to wipe it. Combinators such as map show the plain value
only to their closure and return the result re-wrapped.
It also provides an "opaque" implementation of the Debug trait so you can debug protected values
without accidentally leaking their innards.
use ;
let x = new;
assert!;
The inner value is not accessible directly, but you can use the risky_unwrap method as an escape hatch to get it back.
risky_unwrap is defined in the [Controlled] trait so you'll need to bring that in scope.
use ;
let x = new;
assert_eq!;
Protected does not implement Deref so you cannot access the data directly.
This is to prevent accidental leakage of the inner value.
It also means comparisons (like PartialEq) are not implemented for Protected.
If you want to safely compare values, you can use [Equatable].
Equatable
The Equatable type is a wrapper around Protected that implements constant-time comparison.
It implements PartialEq for any inner type that implements [ConstantTimeEq].
use ;
let x: = new;
let y: = new;
assert_eq!;
Exportable
The Exportable type is a wrapper around Protected that supports safe serialization via the SafeSerialize and SafeDeserialize traits.
Usage
The Usage type is a wrapper around Protected that allows you to specify a scope for the data.
This adapter is WIP.
Working with wrapped values
None of the adapters implement Deref so you can't access the inner value directly.
This is to prevent accidental leakage of the inner value by being explicit about when and how you want to work with the inner value.
You can map over the inner value to transform it, so long as the adapter is the same type.
For example, you can map a Protected<T> to a Protected<U>.
use ;
// Calculate the sum of values in the array with the result as a `Protected`
let x: = new;
let result: = x.map;
assert_eq!;
If you have a pair of Protected values, you can zip them together with a function that combines them.
use ;
let x: = new;
let y: = new;
let z: = x.zip;
If the inner type is an Option you can call transpose to swap the Protected and the Option.
use ;
let x = new;
let y = x.transpose;
assert!;
A Protected of Protected can be "flattened" into a single Protected.
# use ;
let x = new;
let y = x.flatten;
assert_eq!;
Use [flatten_array] to convert a [Protected<T>; N] into a Protected<[T; N]>.
Protected digests
ProtectedDigest requires a fixed-output implementation that zeroizes its
internal state on drop. Enable the digest crate's zeroization feature, such as
sha2 = { version = "0.11", features = ["zeroize"] }.
Unkeyed digests use new; keyed fixed-output functions implementing KeyInit
use new_with_key so the key remains in a Controlled container at the API
boundary.
Secret inputs use update and protected outputs use finalize_into. Public
protocol framing and intentionally exposed outputs cross separate, explicitly
named channels:
use Sha256;
use ;
let secret = new;
let mut digest = new;
digest.update_public;
digest.update;
let mut output = new;
digest.finalize_into;
assert_ne!;
Also in this crate
Beyond the adapters above, the crate exports TimingSafeEq and Choice (timing-safe comparison), OpaqueDebug and Redacted (leak-resistant Debug), ProtectedDigest, Zeroed, and AsProtectedRef — see the docs.rs API reference for details.
Non-empty contexts
An AEAD associated-data value or PRF context can legitimately be empty, but a caller that uses one value to domain-separate fields needs it not to be. NonEmpty<T> carries that invariant in the type, checked once at construction: nonempty!("users/email") is checked at compile time (an empty literal does not compile), and NonEmpty::new(value) checks a dynamic value structurally — "", None, Some("") and ("", "") are all rejected, without parsing any encoding. An API that requires the invariant takes NonEmpty<T> directly; a bare &str argument cannot be value-checked at compile time, so there is deliberately no implicit conversion from a string or byte slice. Integers are never empty and convert with From, and a proven value extends with .with(tail) without a second check.
use ;
// Compile-time checked: nonempty!("") does not compile.
assert_eq!;
// Runtime checked, once, for dynamic values.
assert!;
assert_eq!;
Generators
Protected supports generating new values from functions that return the inner value.
# use ;
let input: = generate;
You can also generate values from functions that return a Result with the inner value.
# use ;
use FromUtf8Error;
let input: = generate_ok;
CipherStash
Vitamin C is brought to you by the team at CipherStash.
License: MIT