tss_esapi/context/tpm_commands/
random_number_generator.rs1use crate::{
4 structures::{Digest, SensitiveData},
5 tss2_esys::{Esys_GetRandom, Esys_StirRandom},
6 Context, Error, Result, WrapperErrorKind as ErrorKind,
7};
8use log::error;
9use std::convert::{TryFrom, TryInto};
10use std::ptr::null_mut;
11
12impl Context {
13 pub fn get_random(&mut self, num_bytes: usize) -> Result<Digest> {
18 let mut random_bytes_ptr = null_mut();
19 let ret = unsafe {
20 Esys_GetRandom(
21 self.mut_context(),
22 self.optional_session_1(),
23 self.optional_session_2(),
24 self.optional_session_3(),
25 num_bytes
26 .try_into()
27 .map_err(|_| Error::local_error(ErrorKind::WrongParamSize))?,
28 &mut random_bytes_ptr,
29 )
30 };
31
32 let ret = Error::from_tss_rc(ret);
33 if ret.is_success() {
34 Digest::try_from(Context::ffi_data_to_owned(random_bytes_ptr))
35 } else {
36 error!("Error in getting random bytes: {}", ret);
37 Err(ret)
38 }
39 }
40
41 pub fn stir_random(&mut self, in_data: SensitiveData) -> Result<()> {
43 let ret = unsafe {
44 Esys_StirRandom(
45 self.mut_context(),
46 self.optional_session_1(),
47 self.optional_session_2(),
48 self.optional_session_3(),
49 &in_data.into(),
50 )
51 };
52 let ret = Error::from_tss_rc(ret);
53
54 if ret.is_success() {
55 Ok(())
56 } else {
57 error!("Error stirring random: {}", ret);
58 Err(ret)
59 }
60 }
61}