Skip to content

Commit e5e343f

Browse files
docs: improve challenge encoding and async TPM documentation
Enhanced documentation and warnings for better developer understanding. Challenge encoding: - Improved warning when challenge is not valid base64 - Show base64 decode error and challenge preview for debugging - Clarify UTF-8 fallback is for testing/backwards compatibility Async TPM operations: - Document async/sync bridge pattern using spawn_blocking - Explain runtime requirements (tokio runtime, blocking thread pool) - Clarify why TPM operations need blocking threads (hardware I/O, sync API) - Document error handling for both task panics and TPM failures Assisted-by: Claude 4 Sonnet Signed-off-by: Sergio Correia <[email protected]>
1 parent 88e06c7 commit e5e343f

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

keylime/src/auth.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,57 @@ impl SessionToken {
107107
}
108108
}
109109

110-
/// Mock TPM operations for testing
110+
/// TPM operations abstraction for authentication proof-of-possession
111+
///
112+
/// This trait provides an async interface for generating TPM-based cryptographic
113+
/// proofs during the authentication protocol. Implementations handle the details
114+
/// of interacting with TPM hardware or providing mock proofs for testing.
115+
///
116+
/// ## Async Design
117+
///
118+
/// The trait is async (`#[async_trait]`) even though underlying TPM operations
119+
/// are synchronous. This design allows:
120+
///
121+
/// 1. **Non-blocking execution**: TPM operations can run on blocking thread pools
122+
/// via `tokio::task::spawn_blocking` without blocking the async runtime
123+
/// 2. **Consistent API**: All authentication operations use async/await patterns
124+
/// 3. **Future extensibility**: Support for truly async TPM drivers if available
125+
///
126+
/// ## Runtime Requirements
127+
///
128+
/// Implementations that use real TPM hardware (like `RealTpmOperations`) require:
129+
/// - An active tokio runtime (checked via `tokio::runtime::Handle::try_current()`)
130+
/// - Available threads in the runtime's blocking thread pool
131+
///
132+
/// Mock implementations (like `MockTpmOperations`) don't have these requirements
133+
/// since they don't perform actual TPM operations.
134+
///
135+
/// ## Thread Safety
136+
///
137+
/// The `Send + Sync` bounds ensure implementations can be safely shared across
138+
/// threads, which is necessary for use in the async `AuthenticationClient`.
111139
#[async_trait::async_trait]
112140
pub trait TpmOperations: Send + Sync {
141+
/// Generate a cryptographic proof of possession for the given challenge
142+
///
143+
/// This method uses TPM2_Certify to prove possession of the Attestation Key (AK)
144+
/// by certifying it with itself and including the challenge as qualifying data.
145+
///
146+
/// # Arguments
147+
///
148+
/// * `challenge` - Challenge string from the verifier (typically base64-encoded)
149+
///
150+
/// # Returns
151+
///
152+
/// A `ProofOfPossession` containing the signed attestation message and signature
153+
///
154+
/// # Errors
155+
///
156+
/// Returns an error if:
157+
/// - TPM operations fail (hardware/driver issues)
158+
/// - Challenge encoding is invalid
159+
/// - Cryptographic operations fail
160+
/// - Runtime requirements are not met (for real TPM operations)
113161
async fn generate_proof(
114162
&self,
115163
challenge: &str,

keylime/src/context_info.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,25 @@ impl ContextInfo {
445445
let mut tpm_context = self.get_mutable_tpm_context().clone();
446446

447447
// Use TPM2_Certify to certify the AK with itself
448+
//
449+
// ## Async Runtime Bridge
450+
//
451+
// TPM operations are synchronous (blocking I/O) but we're in an async context.
452+
// We use `task::spawn_blocking` to run the TPM operation on a dedicated blocking
453+
// thread pool, preventing it from blocking the async executor.
454+
//
455+
// This is necessary because:
456+
// 1. TPM hardware operations involve blocking system calls
457+
// 2. The tss-esapi library provides a synchronous API
458+
// 3. Blocking the async runtime would prevent other tasks from making progress
459+
//
460+
// Requirements:
461+
// - A tokio runtime must be active when this function is called
462+
// - The runtime's blocking thread pool must have available threads
463+
//
464+
// Error handling:
465+
// - First `?` handles JoinError (if the spawned task panics)
466+
// - Second `?` handles TpmError from the TPM operation itself
448467
let (attest, signature) = task::spawn_blocking(move || {
449468
tpm_context.certify_credential(
450469
challenge_bytes.try_into().map_err(|_| {

0 commit comments

Comments
 (0)