1 // Copyright 2022 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 //! Software TPM backend using the TPM2 simulator from the `tpm2` crate. 6 7 use std::env; 8 use std::fs; 9 use std::path::Path; 10 11 use anyhow::Context; 12 use tpm2::Simulator; 13 14 use super::virtio::TpmBackend; 15 16 pub struct SoftwareTpm { 17 simulator: Simulator, 18 } 19 20 impl SoftwareTpm { new<P: AsRef<Path>>(storage: P) -> anyhow::Result<Self>21 pub fn new<P: AsRef<Path>>(storage: P) -> anyhow::Result<Self> { 22 fs::create_dir_all(storage.as_ref()).context("failed to create directory for simulator")?; 23 env::set_current_dir(storage).context("failed to change into simulator directory")?; 24 let simulator = Simulator::singleton_in_current_directory(); 25 Ok(SoftwareTpm { simulator }) 26 } 27 } 28 29 impl TpmBackend for SoftwareTpm { execute_command<'a>(&'a mut self, command: &[u8]) -> &'a [u8]30 fn execute_command<'a>(&'a mut self, command: &[u8]) -> &'a [u8] { 31 self.simulator.execute_command(command) 32 } 33 } 34