diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-07-02 19:00:41 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-07-02 19:02:15 +0200 |
commit | d887f79b62c86cf7a29ecc206c5755ff56879713 (patch) | |
tree | 8aa6fa341722b0e4ee4f38d2f4fd7a7fc46abca8 /openpgp/src/crypto/mod.rs | |
parent | 92371c26e33119d2ea162d9a8bfdabe45f9400ec (diff) |
openpgp: New function crypto::random.
- Add and use a function that fills a buffer with a thread-local
random number generator.
Diffstat (limited to 'openpgp/src/crypto/mod.rs')
-rw-r--r-- | openpgp/src/crypto/mod.rs | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs index 5a6b06c0..6645ecdc 100644 --- a/openpgp/src/crypto/mod.rs +++ b/openpgp/src/crypto/mod.rs @@ -27,6 +27,13 @@ pub use self::asymmetric::{ KeyPair, }; +/// Fills the given buffer with random data. +pub fn random<B: AsMut<[u8]>>(mut buf: B) { + use std::cell::RefCell; + thread_local!(static RNG: RefCell<Yarrow> = Default::default()); + RNG.with(|rng| rng.borrow_mut().random(buf.as_mut())); +} + /// Holds a session key. /// /// The session key is cleared when dropped. @@ -37,7 +44,7 @@ impl SessionKey { /// Creates a new session key. pub fn new(size: usize) -> Self { let mut sk: mem::Protected = vec![0; size].into(); - Yarrow::default().random(&mut sk); + random(&mut sk); Self(sk) } |