Skip to content

Commit a5e1bbd

Browse files
authored
Merge pull request #347 from ionut-arm/tkc-docs
Expand documentation around TransientKeyContext
2 parents 7205c4c + 896d6be commit a5e1bbd

File tree

11 files changed

+92
-28
lines changed

11 files changed

+92
-28
lines changed

tss-esapi/src/abstraction/transient/mod.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55
//!
66
//! This module presents an abstraction over the TPM functionality exposed through the core
77
//! `Context` structure. The abstraction works by hiding resource handle management from the
8-
//! client. This is achieved by passing objects back and forth in the form of contexts. Thus, when
9-
//! an object is created, its saved context is returned and the object is flushed from the TPM.
10-
//! Whenever the client needs to use said object, it calls the desired operation with the context
11-
//! as a parameter - the context is loaded in the TPM, the operation performed and the context
12-
//! flushed out again before the result is returned.
13-
//!
14-
//! Object contexts thus act as an opaque handle that can, however, be used by the client to seralize
15-
//! and persist the underlying data.
8+
//! client.
169
use crate::{
1710
attributes::{ObjectAttributesBuilder, SessionAttributesBuilder},
1811
constants::{tss::*, SessionType, Tss2ResponseCodeKind},
@@ -56,7 +49,9 @@ pub enum KeyParams {
5649
scheme: RsaScheme,
5750
/// Public exponent of the key
5851
///
59-
/// If set to 0, it will default to 2^16 - 1
52+
/// If set to 0, it will default to 2^16 - 1.
53+
///
54+
/// (Note that the default value for [`RsaExponent`] is 0)
6055
pub_exponent: RsaExponent,
6156
},
6257
Ecc {
@@ -72,6 +67,9 @@ pub enum KeyParams {
7267
/// The `public` field represents the public part of the key in plain text,
7368
/// while `private` is the encrypted version of the private key.
7469
///
70+
/// For information on public key formats, see the documentation of [`PublicKey`].
71+
/// The private part of the key should be treated as an opaque binary blob.
72+
///
7573
/// # Warning
7674
///
7775
/// If the Owner hierarchy is cleared, any key material generated
@@ -94,6 +92,13 @@ impl KeyMaterial {
9492
}
9593
}
9694

95+
/// Structure containing all the defining elements of a TPM key
96+
///
97+
/// - `material` identifies the numeric value of the key object
98+
/// - `params` identifies the algorithm to use on the key and other relevant
99+
/// parameters
100+
/// - `auth` identifies the optional authentication value to be used with the
101+
/// key
97102
#[derive(Debug, Clone)]
98103
pub struct ObjectWrapper {
99104
pub material: KeyMaterial,
@@ -104,13 +109,10 @@ pub struct ObjectWrapper {
104109
/// Structure offering an abstracted programming experience.
105110
///
106111
/// The `TransientKeyContext` makes use of a root key from which the other, client-controlled
107-
/// keyes are derived.
112+
/// keys are derived.
108113
///
109-
/// Currently, only functionality necessary for RSA key creation and usage (for signing,
110-
/// verifying signatures, encryption and decryption) is implemented. The RSA SSA
111-
/// asymmetric scheme with SHA256 is used for all created and imported signing keys.
112-
/// The RSA OAEP asymmetric scheme with SHA256 is used for all created and imported
113-
/// signing/encryption/decryption keys.
114+
/// This abstraction makes public key cryptography more accessible, focusing on asymmetric
115+
/// encryption and signatures in particular, by allowing users to offload object and session management.
114116
#[allow(clippy::module_name_repetitions)]
115117
#[derive(Debug)]
116118
pub struct TransientKeyContext {
@@ -126,6 +128,10 @@ impl TransientKeyContext {
126128
/// If successful, the result contains the [KeyMaterial] of the key and a vector of
127129
/// bytes forming the authentication value for said key.
128130
///
131+
/// The following key attributes are always **set**: `fixed_tpm`, `fixed_parent`, `sensitive_data_origin`,
132+
/// `user_with_auth`. The `restricted` attribute is **not set**. See section 8.3 in the Structures
133+
/// spec for a detailed description of these attributes.
134+
///
129135
/// # Constraints
130136
/// * `auth_size` must be at most 32
131137
///
@@ -170,7 +176,7 @@ impl TransientKeyContext {
170176

171177
/// Load the public part of a key.
172178
///
173-
/// Returns the key context.
179+
/// Returns the appropriate key material after verifying that the key can be loaded.
174180
pub fn load_external_public_key(
175181
&mut self,
176182
public_key: PublicKey,
@@ -190,8 +196,10 @@ impl TransientKeyContext {
190196

191197
/// Encrypt a message with an existing key.
192198
///
193-
/// Takes the key as a parameter, encrypts the message and returns the ciphertext. A label (i.e.
194-
/// nonce) can also be provided.
199+
/// Takes the key as a set of parameters (`key_material`, `key_params`, `key_auth`), encrypts the message
200+
/// and returns the ciphertext. A label can also be provided which will be associated with the ciphertext.
201+
///
202+
/// Note: the data passed as `label` MUST end in a `0x00` byte.
195203
pub fn rsa_encrypt(
196204
&mut self,
197205
key_material: KeyMaterial,
@@ -228,8 +236,10 @@ impl TransientKeyContext {
228236

229237
/// Decrypt ciphertext with an existing key.
230238
///
231-
/// Takes the key as a parameter, decrypts the ciphertext and returns the plaintext. A label (i.e.
232-
/// nonce) can also be provided.
239+
/// Takes the key as a set of parameters (`key_material`, `key_params`, `key_auth`), decrypts the ciphertext
240+
/// and returns the plaintext. A label which was associated with the ciphertext can also be provided.
241+
///
242+
/// Note: the data passed as `label` MUST end in a `0x00` byte.
233243
pub fn rsa_decrypt(
234244
&mut self,
235245
key_material: KeyMaterial,
@@ -266,7 +276,7 @@ impl TransientKeyContext {
266276

267277
/// Sign a digest with an existing key.
268278
///
269-
/// Takes the key as a parameter, signs and returns the signature.
279+
/// Takes the key as a set of parameters (`key_material`, `key_params`, `key_auth`), signs and returns the signature.
270280
pub fn sign(
271281
&mut self,
272282
key_material: KeyMaterial,

tss-esapi/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,26 @@
3535
//! at varying levels of abstraction.
3636
//! Only platforms based on processors with a word size of at least 16 bits are supported.
3737
//!
38+
//! # Relevant specifications
39+
//! This library is built with insight from Trusted Computing Group specifications. The specs most relevant
40+
//! here are:
41+
//! * the [Trusted Platform Module Library Specification, Family “2.0”, Level 00, Revision 01.59](https://trustedcomputinggroup.org/work-groups/trusted-platform-module/)
42+
//! * the [TCG TSS 2.0 Enhanced System API (ESAPI) Specification, version 1.00, revision 14](https://trustedcomputinggroup.org/resource/tcg-tss-2-0-enhanced-system-api-esapi-specification/)
43+
//!
44+
//! The different parts of the first spec mentioned above (henceforth called the TPM2 spec) can be
45+
//! referenced individually throughout the documentation of this crate, using their part number or name.
46+
//! For example,
47+
//! [Part 1, Architecture](https://trustedcomputinggroup.org/wp-content/uploads/TCG_TPM2_r1p59_Part1_Architecture_pub.pdf)
48+
//! could be referenced as "the Architecture spec" or "part 1 of the TPM2 spec".
49+
//!
50+
//! The second spec mentioned above will henceforth be called the ESAPI or ESys spec.
51+
//!
52+
//! Some parts of the code relate to features or functionality defined in other specifications (such as the
53+
//! [Marshaling/Unmarshaling API v1, rev7 spec](https://trustedcomputinggroup.org/resource/tcg-tss-2-0-marshalingunmarshaling-api-specification/)),
54+
//! and in such cases the specification should be linked and referenced in full.
55+
//!
3856
//! # Code structure
39-
//! Our code structure is mostly derived from
40-
//! [part 2 of the TPM2 TCG spec](https://trustedcomputinggroup.org/wp-content/uploads/TCG_TPM2_r1p59_Part2_Structures_pub.pdf).
57+
//! Our code structure is mostly derived from part 2 of the TPM2 spec.
4158
//! For simplicity, however, we have reduced the depth of the import tree, so most (if not all) types
4259
//! are at most one level away from root.
4360
//!

tss-esapi/src/structures/attestation/attest.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Attest {
2727
}
2828

2929
impl Attest {
30-
/// Returns ttestation type
30+
/// Returns attestation type
3131
pub const fn attestation_type(&self) -> AttestationType {
3232
self.attestation_type
3333
}
@@ -37,7 +37,7 @@ impl Attest {
3737
&self.qualified_signer
3838
}
3939

40-
/// Retirns the extra data specified by the caller.
40+
/// Returns the extra data specified by the caller.
4141
pub const fn extra_data(&self) -> &Data {
4242
&self.extra_data
4343
}
@@ -121,6 +121,7 @@ impl TryFrom<TPMS_ATTEST> for Attest {
121121
impl Marshall for Attest {
122122
const BUFFER_SIZE: usize = std::mem::size_of::<TPMS_ATTEST>();
123123

124+
/// Produce a marshalled [`TPMS_ATTEST`]
124125
fn marshall(&self) -> Result<Vec<u8>> {
125126
let mut buffer = vec![0; Self::BUFFER_SIZE];
126127
let mut offset = 0;
@@ -153,6 +154,7 @@ impl Marshall for Attest {
153154
}
154155

155156
impl UnMarshall for Attest {
157+
/// Unmarshall the structure from [`TPMS_ATTEST`]
156158
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
157159
let mut dest = TPMS_ATTEST::default();
158160
let mut offset = 0;

tss-esapi/src/structures/buffers/public.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl TryFrom<Public> for PublicBuffer {
119119
impl Marshall for PublicBuffer {
120120
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2B_PUBLIC>();
121121

122+
/// Produce a marshalled [`TPM2B_PUBLIC`]
122123
fn marshall(&self) -> Result<Vec<u8>> {
123124
let mut buffer = vec![0; Self::BUFFER_SIZE];
124125
let mut offset = 0;
@@ -151,6 +152,7 @@ impl Marshall for PublicBuffer {
151152
}
152153

153154
impl UnMarshall for PublicBuffer {
155+
/// Unmarshall the structure from [`TPM2B_PUBLIC`]
154156
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
155157
let mut dest = TPM2B_PUBLIC::default();
156158
let mut offset = 0;

tss-esapi/src/structures/buffers/sensitive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl TryFrom<Sensitive> for SensitiveBuffer {
118118
impl Marshall for SensitiveBuffer {
119119
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2B_SENSITIVE>();
120120

121+
/// Produce a marshalled [`TPM2B_SENSITIVE`]
121122
fn marshall(&self) -> Result<Vec<u8>> {
122123
let mut buffer = vec![0; Self::BUFFER_SIZE];
123124
let mut offset = 0;
@@ -150,6 +151,7 @@ impl Marshall for SensitiveBuffer {
150151
}
151152

152153
impl UnMarshall for SensitiveBuffer {
154+
/// Unmarshall the structure from [`TPM2B_SENSITIVE`]
153155
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
154156
let mut dest = TPM2B_SENSITIVE::default();
155157
let mut offset = 0;

tss-esapi/src/structures/signatures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use log::error;
1010
use std::convert::{TryFrom, TryInto};
1111

1212
/// Type holding RSA signature information.
13+
///
14+
/// For more information about the contents of `signature` see Annex B
15+
/// in the Architecture spec.
1316
#[derive(Debug, Clone, PartialEq, Eq)]
1417
pub struct RsaSignature {
1518
hashing_algorithm: HashingAlgorithm,
@@ -70,6 +73,9 @@ impl TryFrom<TPMS_SIGNATURE_RSA> for RsaSignature {
7073
}
7174

7275
/// Type holding ECC signature information.
76+
///
77+
/// For more information about the contents of `signature_r` and `signature_s`
78+
/// see Annex B in the Architecture spec (or Annex D for SM2 signatures).
7379
#[derive(Debug, Clone, PartialEq, Eq)]
7480
pub struct EccSignature {
7581
hashing_algorithm: HashingAlgorithm,

tss-esapi/src/structures/tagged/public.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl PublicBuilder {
7676
self
7777
}
7878

79-
/// Adds the name hash algorithm for the [Public] strucutre
79+
/// Adds the name hash algorithm for the [Public] structure
8080
/// to the builder.
8181
pub const fn with_name_hashing_algorithm(
8282
mut self,
@@ -86,7 +86,7 @@ impl PublicBuilder {
8686
self
8787
}
8888

89-
/// Adds the auth policy for the [Public] strucutre
89+
/// Adds the auth policy for the [Public] structure
9090
/// to the builder
9191
pub fn with_auth_policy(mut self, auth_policy: Digest) -> Self {
9292
self.auth_policy = Some(auth_policy);
@@ -494,6 +494,9 @@ impl TryFrom<TPMT_PUBLIC> for Public {
494494
impl Marshall for Public {
495495
const BUFFER_SIZE: usize = std::mem::size_of::<TPMT_PUBLIC>();
496496

497+
/// Produce a marshalled [TPMT_PUBLIC]
498+
///
499+
/// Note: for [TPM2B_PUBLIC] marshalling use [PublicBuffer][`crate::structures::PublicBuffer]
497500
fn marshall(&self) -> Result<Vec<u8>> {
498501
let mut buffer = vec![0; Self::BUFFER_SIZE];
499502
let mut offset = 0;
@@ -526,6 +529,9 @@ impl Marshall for Public {
526529
}
527530

528531
impl UnMarshall for Public {
532+
/// Unmarshall the structure from [`TPMT_PUBLIC`]
533+
///
534+
/// Note: for [TPM2B_PUBLIC] unmarshalling use [PublicBuffer][`crate::structures::PublicBuffer]
529535
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
530536
let mut dest = TPMT_PUBLIC::default();
531537
let mut offset = 0;

tss-esapi/src/structures/tagged/sensitive.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ impl TryFrom<TPMT_SENSITIVE> for Sensitive {
166166
impl Marshall for Sensitive {
167167
const BUFFER_SIZE: usize = std::mem::size_of::<TPMT_SENSITIVE>();
168168

169+
/// Produce a marshalled [`TPMT_SENSITIVE`]
170+
///
171+
/// Note: for [TPM2B_SENSITIVE] marshalling use [SensitiveBuffer][`crate::structures::SensitiveBuffer]
169172
fn marshall(&self) -> Result<Vec<u8>> {
170173
let mut buffer = vec![0; Self::BUFFER_SIZE];
171174
let mut offset = 0;
@@ -198,6 +201,9 @@ impl Marshall for Sensitive {
198201
}
199202

200203
impl UnMarshall for Sensitive {
204+
/// Unmarshall the structure from [`TPMT_SENSITIVE`]
205+
///
206+
/// Note: for [TPM2B_SENSITIVE] marshalling use [SensitiveBuffer][`crate::structures::SensitiveBuffer]
201207
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
202208
let mut dest = TPMT_SENSITIVE::default();
203209
let mut offset = 0;

tss-esapi/src/structures/tagged/signature.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl TryFrom<TPMT_SIGNATURE> for Signature {
132132
impl Marshall for Signature {
133133
const BUFFER_SIZE: usize = std::mem::size_of::<TPMT_SIGNATURE>();
134134

135+
/// Produce a marshalled [`TPMT_SIGNATURE`]
135136
fn marshall(&self) -> Result<Vec<u8>> {
136137
let tpmt_sig = TPMT_SIGNATURE::try_from(self.clone())?;
137138
let mut offset = 0;
@@ -164,6 +165,7 @@ impl Marshall for Signature {
164165
}
165166

166167
impl UnMarshall for Signature {
168+
/// Unmarshall the structure from [`TPMT_SIGNATURE`]
167169
fn unmarshall(public_buffer: &[u8]) -> Result<Self> {
168170
let mut tpmt_sig = TPMT_SIGNATURE::default();
169171
let mut offset = 0;

tss-esapi/src/structures/tagged/symmetric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
Error, Result, WrapperErrorKind,
1010
};
1111
use std::convert::{TryFrom, TryInto};
12-
/// Enum repsesnting the symmetric algorithm definition.
12+
/// Enum representing the symmetric algorithm definition.
1313
///
1414
/// # Details
1515
/// This corresponds to TPMT_SYM_DEF.

0 commit comments

Comments
 (0)