Skip to main content

Template

Struct Template 

Source
pub struct Template(/* private fields */);
Expand description

A TPM public-area key template.

Implementations§

Source§

impl Template

Source

pub fn srk(alg: KeyAlg) -> Result<Self>

Storage root key template (restricted decryption parent).

Source

pub fn attestation(alg: KeyAlg) -> Result<Self>

Attestation identity key (AIK) template — a restricted signing key used to certify other objects / quote PCRs.

Examples found in repository?
examples/full_flow.rs (line 82)
20fn main() -> Result<(), wolftpm::TpmError> {
21    let dev = Device::open()?;
22    println!("device            connected to TPM");
23
24    let mut nonce = [0u8; 16];
25    dev.get_random(&mut nonce)?;
26    println!("get_random        {}", hex(&nonce));
27
28    let srk = dev.create_primary(Hierarchy::Owner, KeyAlg::EccP256, None)?;
29    println!("create_primary    ECC SRK handle 0x{:08x}", srk.handle());
30
31    let signer = dev.create_and_load(&srk, &Template::signing(KeyAlg::EccP256)?, None)?;
32    println!(
33        "create_and_load   signing key handle 0x{:08x}",
34        signer.handle()
35    );
36
37    let digest = [0x11u8; 32];
38    let sig = signer.sign_hash(&digest)?;
39    signer.verify_hash(&digest, &sig)?;
40    println!("sign/verify       {} byte signature, verified", sig.len());
41
42    let sealed = dev.seal(&srk, b"my secret", None)?;
43    let secret = dev.unseal(sealed, &srk, None)?;
44    println!(
45        "seal/unseal       recovered \"{}\"",
46        core::str::from_utf8(&secret).unwrap_or("<bytes>")
47    );
48
49    // Scope the reloaded key so its handle is freed before the attestation
50    // step below. A TPM only holds a few transient objects at once (often 3),
51    // so keep the number of simultaneously-loaded keys small.
52    {
53        let blob = dev.create_key(&srk, &Template::signing(KeyAlg::EccP256)?, None)?;
54        let bytes = blob.to_bytes()?;
55        let restored = KeyBlob::from_bytes(&dev, &bytes)?;
56        let loaded = restored.load(&srk, None)?;
57        println!(
58            "key blob          {} bytes, reloaded as handle 0x{:08x}",
59            bytes.len(),
60            loaded.handle()
61        );
62    }
63
64    let before = dev.pcr_read(16, HashAlg::Sha256)?;
65    dev.pcr_extend(16, HashAlg::Sha256, &[0xAB; 32])?;
66    let after = dev.pcr_read(16, HashAlg::Sha256)?;
67    println!(
68        "pcr read/extend   PCR16 {}.. -> {}..",
69        hex(&before[..6]),
70        hex(&after[..6])
71    );
72
73    let index = 0x0150_0100;
74    let _ = dev.nv_delete(index);
75    let mut slot = dev.nv_create(index, 32, None)?;
76    dev.nv_write(&mut slot, b"metadata", 0)?;
77    let mut buf = [0u8; 32];
78    let n = dev.nv_read(&mut slot, &mut buf, 0)?;
79    dev.nv_delete(index)?;
80    println!("nv define/rw      {} bytes at index 0x{:08x}", n, index);
81
82    let aik = dev.create_and_load(&srk, &Template::attestation(KeyAlg::EccP256)?, None)?;
83    let att = dev.certify(&signer, &aik, &nonce)?;
84    println!("certify           {} byte attestation", att.attest.len());
85
86    println!("done              all operations succeeded");
87    Ok(())
88}
Source

pub fn ek(alg: KeyAlg) -> Result<Self>

Endorsement key (EK) template, created under the endorsement hierarchy.

Source

pub fn rsa_decrypt() -> Result<Self>

RSA decryption-key template (non-restricted), for RSA-OAEP encrypt and decrypt.

Source

pub fn signing(alg: KeyAlg) -> Result<Self>

General signing-key template (non-restricted, ECDSA/RSASSA).

Examples found in repository?
examples/full_flow.rs (line 31)
20fn main() -> Result<(), wolftpm::TpmError> {
21    let dev = Device::open()?;
22    println!("device            connected to TPM");
23
24    let mut nonce = [0u8; 16];
25    dev.get_random(&mut nonce)?;
26    println!("get_random        {}", hex(&nonce));
27
28    let srk = dev.create_primary(Hierarchy::Owner, KeyAlg::EccP256, None)?;
29    println!("create_primary    ECC SRK handle 0x{:08x}", srk.handle());
30
31    let signer = dev.create_and_load(&srk, &Template::signing(KeyAlg::EccP256)?, None)?;
32    println!(
33        "create_and_load   signing key handle 0x{:08x}",
34        signer.handle()
35    );
36
37    let digest = [0x11u8; 32];
38    let sig = signer.sign_hash(&digest)?;
39    signer.verify_hash(&digest, &sig)?;
40    println!("sign/verify       {} byte signature, verified", sig.len());
41
42    let sealed = dev.seal(&srk, b"my secret", None)?;
43    let secret = dev.unseal(sealed, &srk, None)?;
44    println!(
45        "seal/unseal       recovered \"{}\"",
46        core::str::from_utf8(&secret).unwrap_or("<bytes>")
47    );
48
49    // Scope the reloaded key so its handle is freed before the attestation
50    // step below. A TPM only holds a few transient objects at once (often 3),
51    // so keep the number of simultaneously-loaded keys small.
52    {
53        let blob = dev.create_key(&srk, &Template::signing(KeyAlg::EccP256)?, None)?;
54        let bytes = blob.to_bytes()?;
55        let restored = KeyBlob::from_bytes(&dev, &bytes)?;
56        let loaded = restored.load(&srk, None)?;
57        println!(
58            "key blob          {} bytes, reloaded as handle 0x{:08x}",
59            bytes.len(),
60            loaded.handle()
61        );
62    }
63
64    let before = dev.pcr_read(16, HashAlg::Sha256)?;
65    dev.pcr_extend(16, HashAlg::Sha256, &[0xAB; 32])?;
66    let after = dev.pcr_read(16, HashAlg::Sha256)?;
67    println!(
68        "pcr read/extend   PCR16 {}.. -> {}..",
69        hex(&before[..6]),
70        hex(&after[..6])
71    );
72
73    let index = 0x0150_0100;
74    let _ = dev.nv_delete(index);
75    let mut slot = dev.nv_create(index, 32, None)?;
76    dev.nv_write(&mut slot, b"metadata", 0)?;
77    let mut buf = [0u8; 32];
78    let n = dev.nv_read(&mut slot, &mut buf, 0)?;
79    dev.nv_delete(index)?;
80    println!("nv define/rw      {} bytes at index 0x{:08x}", n, index);
81
82    let aik = dev.create_and_load(&srk, &Template::attestation(KeyAlg::EccP256)?, None)?;
83    let att = dev.certify(&signer, &aik, &nonce)?;
84    println!("certify           {} byte attestation", att.attest.len());
85
86    println!("done              all operations succeeded");
87    Ok(())
88}
Source

pub fn hmac(hash: HashAlg) -> Result<Self>

TPM-generated keyed-hash HMAC key template, bound to this TPM.

The key material originates in the TPM (sensitiveDataOrigin) and is non-duplicable (fixedTPM/fixedParent), so the HMAC secret never leaves the TPM: create it once, keep only the wrapped blob, reload it, and compute with Key::hmac.

Source

pub fn symmetric(bits: u16) -> Result<Self>

AES symmetric-cipher key template (CFB mode) for TPM-backed bulk encrypt/decrypt via Key::aes_encrypt / Key::aes_decrypt. bits is 128 or 256.

Source

pub fn ecdh() -> Result<Self>

ECDH key-agreement key template (NIST P-256, restricted-decrypt with the ECDH scheme). Use with Key::ecdh_gen.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.