crosvm/devices/src/software_tpm.rs
Daniel Verkamp 29950ef5a6 devices: make virtio tpm backend pluggable
Split the creation of the software TPM emulator from the virtio-tpm
device so that other backends can be used with virtio-tpm.

BUG=b:227283268
TEST=cargo build --features=tpm

Change-Id: Ic1ebd2ebd49615201892afbf86cd5be68f6fde8c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3213271
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-03-31 23:03:16 +00:00

33 lines
978 B
Rust

// Copyright 2022 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Software TPM backend using the TPM2 simulator from the `tpm2` crate.
use std::env;
use std::fs;
use std::path::Path;
use anyhow::Context;
use tpm2::Simulator;
use super::virtio::TpmBackend;
pub struct SoftwareTpm {
simulator: Simulator,
}
impl SoftwareTpm {
pub fn new<P: AsRef<Path>>(storage: P) -> anyhow::Result<Self> {
fs::create_dir_all(storage.as_ref()).context("failed to create directory for simulator")?;
env::set_current_dir(storage).context("failed to change into simulator directory")?;
let simulator = Simulator::singleton_in_current_directory();
Ok(SoftwareTpm { simulator })
}
}
impl TpmBackend for SoftwareTpm {
fn execute_command<'a>(&'a mut self, command: &[u8]) -> &'a [u8] {
self.simulator.execute_command(command)
}
}