tpm2-rand

Crates.iotpm2-rand
lib.rstpm2-rand
version0.2.0
created_at2025-05-27 18:47:57.187544+00
updated_at2025-05-27 19:20:03.826315+00
descriptionA simple TPM2-based random number generator
homepage
repositoryhttps://github.com/FyraLabs/ashura/tree/main/crates/tpm2-rand
max_upload_size
id1691474
size26,246
crates.io Maintainer (github:fyralabs:crates-io-maintainer)

documentation

README

tpm2-rand

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.

Features

It simply exposes the RNG functionality from the TPM2 device, That's it.

Usage

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.

License

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.

Commit count: 18

cargo fmt