pub struct SecretTree { /* fields omitted */ }
Seeded structure that can be used to produce secrets and child SecretTree
s.
During the program lifecycle, a root SecretTree
should be restored from
a secure persistent form (e.g., a passphrase-encrypted file) and then used to derive
child trees and secrets. On the first use, the root should be initialized from a CSPRNG, such
as rand::thread_rng()
. The tree is not needed during the program execution and can
be safely dropped after deriving necessary secrets (which zeroes out the tree seed).
It is possible to modify the derivation hierarchy over the course of program evolution
by adding new secrets or abandoning the existing ones.
However, the purpose of any given tree path should be fixed; that is, if some version
of a program used path foo/bar
to derive an Ed25519 keypair, a newer version
shouldn’t use foo/bar
to derive an AES-128 key. Violating this rule may lead
to leaking the secret.
use secret_tree::{SecretTree, Name};
use rand::{Rng, thread_rng};
let tree = SecretTree::new(&mut thread_rng());
let mut first_secret = [0_u8; 32];
tree.child(Name::new("first")).fill(&mut first_secret);
let child_store = tree.child(Name::new("sequence"));
let more_secrets: Vec<[u64; 4]> = (0..5)
.map(|i| child_store.index(i).rng().gen())
.collect();
let seed = *tree.seed();
drop(tree);
let tree = SecretTree::from_seed(&seed).unwrap();
let mut restored_secret = [0_u8; 32];
tree.child(Name::new("first")).fill(&mut restored_secret);
assert_eq!(first_secret, restored_secret);
Generates a tree by sampling its seed from the supplied RNG.
Restores a tree from the seed.
Converts this tree into a cryptographically secure pseudo-random number generator
(CSPRNG). This RNG can then be used to reproducibly create secrets (e.g., secret keys).
fill()
should be preferred if the secret allows it. While using a CSPRNG to generate
secrets is theoretically sound, it introduces a new entity that may leak information.
fill()
is especially useful if the filled buffer implements zeroing on drop;
the state of a CSPRNG generator returned by rng()
is not zeroed on drop and thus
creates a potential attack vector. (However theoretical it may be; ChaChaRng
has a notably small state size - ~160 bytes, so it may be better localized
and have lower risk to be accessed by the adversary than other CSPRNG implementations.)
Fills the specified buffer with a key derived from the seed of this tree.
The buffer must be equivalent to 16..=64
bytes; the method panics otherwise.
Use rng()
if the buffer size may be outside these bounds,
or if the secret must be derived in a more complex way.
Produces a child with the specified string identifier.
Produces a child with the specified integer index.
Executes the destructor for this type. Read more
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static