Skip to main content

tss_esapi/context/tpm_commands/
random_number_generator.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use 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    /// Get a number of random bytes from the TPM and return them.
14    ///
15    /// # Errors
16    /// * if converting `num_bytes` to `u16` fails, a `WrongParamSize` will be returned
17    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    /// Add additional information into the TPM RNG state
42    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}