| Crates.io | tpm2-rand |
| lib.rs | tpm2-rand |
| version | 0.2.0 |
| created_at | 2025-05-27 18:47:57.187544+00 |
| updated_at | 2025-05-27 19:20:03.826315+00 |
| description | A simple TPM2-based random number generator |
| homepage | |
| repository | https://github.com/FyraLabs/ashura/tree/main/crates/tpm2-rand |
| max_upload_size | |
| id | 1691474 |
| size | 26,246 |
This crate provides a simple rand generator that uses a TPM2 device to generate random numbers. It implements the rand_core::RngCore trait, allowing it to be used seamlessly with the rand ecosystem.
It simply exposes the RNG functionality from the TPM2 device, That's it.
To use this crate, add the following to your Cargo.toml:
[dependencies]
tpm2-rand = "0"
tss-esapi = "7"
Then, you can use it in your code as follows:
use tpm2_rand::Tpm2Rand;
use tss_esapi::{
Context,
TctiNameConf,
};
let context = Context::new(TctiNameConf::from_environment_variable().unwrap()).unwrap();
let mut rng = Tpm2Rand::new(context).unwrap();
let random_bytes = rng.gen::<[u8; 32]>();
println!("Random bytes: {:?}", random_bytes);
You should check out tss-esapi documentation for more details on how to set up the TPM2 context and handle errors.
This crate is licensed under either the Apache License, Version 2.0, or the MIT License, at your option. See the LICENSE-APACHE and LICENSE-MIT files for details.