# havenoverflow/tpm2 architecture

> Architecture diagram of the GitHub repository havenoverflow/tpm2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/havenoverflow/tpm2
- Repository: https://github.com/havenoverflow/tpm2

## Overview

This is a C implementation of TPM 2.0 functionality, derived from the TCG library sample code; the tree also includes simulator and platform-support code. A principal command workflow is request decoding, command execution, and response encoding, but the available excerpts do not show the dispatcher wiring, so that flow is not asserted. The graph preserves major command domains and internal layers visible in the tree and source index. Edges are limited to relationships stated in excerpts or index references; much of the runtime wiring remains unverified.

Main entry points: [`ContextSave.c`](https://github.com/havenoverflow/tpm2/blob/main/ContextSave.c) (Context Save), [`ContextLoad.c`](https://github.com/havenoverflow/tpm2/blob/main/ContextLoad.c) (Context Load), [`Marshal_ContextLoad.c`](https://github.com/havenoverflow/tpm2/blob/main/Marshal_ContextLoad.c) (Command Marshalling), [`CommandDispatcher.c`](https://github.com/havenoverflow/tpm2/blob/main/CommandDispatcher.c) (Command Dispatcher)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_commands["TPM Commands"]
  node_context_save["Context Save<br/>[ContextSave.c]"]
  node_context_load["Context Load<br/>[ContextLoad.c]"]
  node_session_start["Start Auth Session<br/>[StartAuthSession.c]"]
  node_policy["Policy Commands<br/>[PolicyAuthorize.c]"]
  node_audit["Session Audit Digest"]
  node_object_auth["Object Authorization<br/>[ObjectChangeAuth.c]"]
  node_nv["NV Storage Commands<br/>[NV.c]"]
  node_pcr["PCR Commands<br/>[PCR.c]"]
end

subgraph group_state["TPM State"]
  node_context_support["Context Protection<br/>[Context_spt.c]"]
  node_session_state["Session Management<br/>[Session.c]"]
  node_session_process["Session Authorization<br/>[SessionProcess.c]"]
  node_global_state[("Global TPM State<br/>[Global.h]")]
end

subgraph group_crypto["Cryptography"]
  node_crypto_api["Crypto Utilities<br/>[CryptUtil_fp.h]"]
  node_rsa["RSA Primitives<br/>[CpriRSA_fp.h]"]
end

subgraph group_runtime["Runtime Support"]
  node_marshal["Command Marshalling"]
  node_dispatcher["Command Dispatcher"]
  node_generated["Generated Types<br/>[tpm_generated.h]"]
  node_memory["Memory Helpers<br/>[MemoryLib_fp.h]"]
  node_implementation["TPM Internals<br/>[Implementation.h]"]
end

subgraph group_platform["Simulator Platform"]
  node_platform_api["Platform Services<br/>[Platform.h]"]
  node_tcp_server["TCP Simulator<br/>[TcpServer.c]"]
end

node_client(("TPM Client"))

node_context_save -->|"uses protection"| node_context_support
node_context_load -->|"uses protection"| node_context_support
node_session_start -->|"creates session"| node_session_state
node_context_save -->|"reads state"| node_global_state
node_context_load -->|"updates state"| node_global_state
node_context_save -->|"copies context"| node_memory
node_context_load -->|"compares context"| node_memory
node_client -.->|"sends requests"| node_tcp_server
node_tcp_server -.->|"dispatches commands"| node_dispatcher
node_dispatcher -.->|"routes commands"| node_marshal
node_marshal -->|"uses types"| node_generated

click node_marshal "https://github.com/havenoverflow/tpm2/blob/main/Marshal_ContextLoad.c"
click node_dispatcher "https://github.com/havenoverflow/tpm2/blob/main/CommandDispatcher.c"
click node_generated "https://github.com/havenoverflow/tpm2/blob/main/tpm_generated.h"
click node_context_save "https://github.com/havenoverflow/tpm2/blob/main/ContextSave.c"
click node_context_load "https://github.com/havenoverflow/tpm2/blob/main/ContextLoad.c"
click node_context_support "https://github.com/havenoverflow/tpm2/blob/main/Context_spt.c"
click node_session_start "https://github.com/havenoverflow/tpm2/blob/main/StartAuthSession.c"
click node_session_state "https://github.com/havenoverflow/tpm2/blob/main/Session.c"
click node_session_process "https://github.com/havenoverflow/tpm2/blob/main/SessionProcess.c"
click node_policy "https://github.com/havenoverflow/tpm2/blob/main/PolicyAuthorize.c"
click node_audit "https://github.com/havenoverflow/tpm2/blob/main/GetSessionAuditDigest.c"
click node_object_auth "https://github.com/havenoverflow/tpm2/blob/main/ObjectChangeAuth.c"
click node_nv "https://github.com/havenoverflow/tpm2/blob/main/NV.c"
click node_pcr "https://github.com/havenoverflow/tpm2/blob/main/PCR.c"
click node_crypto_api "https://github.com/havenoverflow/tpm2/blob/main/CryptUtil_fp.h"
click node_rsa "https://github.com/havenoverflow/tpm2/blob/main/CpriRSA_fp.h"
click node_memory "https://github.com/havenoverflow/tpm2/blob/main/MemoryLib_fp.h"
click node_global_state "https://github.com/havenoverflow/tpm2/blob/main/Global.h"
click node_implementation "https://github.com/havenoverflow/tpm2/blob/main/Implementation.h"
click node_platform_api "https://github.com/havenoverflow/tpm2/blob/main/Platform.h"
click node_tcp_server "https://github.com/havenoverflow/tpm2/blob/main/TcpServer.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_context_save,node_context_load,node_session_start,node_policy,node_audit,node_object_auth,node_nv,node_pcr,node_client toneBlue
class node_context_support,node_session_state,node_session_process,node_global_state toneAmber
class node_crypto_api,node_rsa toneMint
class node_marshal,node_dispatcher,node_generated,node_memory,node_implementation toneRose
class node_platform_api,node_tcp_server toneIndigo
```

## Components

### TPM Commands

- [Context Save](https://github.com/havenoverflow/tpm2/blob/main/ContextSave.c): `ContextSave.c`
- [Context Load](https://github.com/havenoverflow/tpm2/blob/main/ContextLoad.c): `ContextLoad.c`
- [Start Auth Session](https://github.com/havenoverflow/tpm2/blob/main/StartAuthSession.c): `StartAuthSession.c`
- [Policy Commands](https://github.com/havenoverflow/tpm2/blob/main/PolicyAuthorize.c): `PolicyAuthorize.c`
- [Session Audit Digest](https://github.com/havenoverflow/tpm2/blob/main/GetSessionAuditDigest.c): `GetSessionAuditDigest.c`
- [Object Authorization](https://github.com/havenoverflow/tpm2/blob/main/ObjectChangeAuth.c): `ObjectChangeAuth.c`
- [NV Storage Commands](https://github.com/havenoverflow/tpm2/blob/main/NV.c): `NV.c`
- [PCR Commands](https://github.com/havenoverflow/tpm2/blob/main/PCR.c): `PCR.c`

### TPM State

- [Context Protection](https://github.com/havenoverflow/tpm2/blob/main/Context_spt.c): `Context_spt.c`
- [Session Management](https://github.com/havenoverflow/tpm2/blob/main/Session.c): `Session.c`
- [Session Authorization](https://github.com/havenoverflow/tpm2/blob/main/SessionProcess.c): `SessionProcess.c`
- [Global TPM State](https://github.com/havenoverflow/tpm2/blob/main/Global.h): `Global.h`

### Cryptography

- [Crypto Utilities](https://github.com/havenoverflow/tpm2/blob/main/CryptUtil_fp.h): `CryptUtil_fp.h`
- [RSA Primitives](https://github.com/havenoverflow/tpm2/blob/main/CpriRSA_fp.h): `CpriRSA_fp.h`

### Runtime Support

- [Command Marshalling](https://github.com/havenoverflow/tpm2/blob/main/Marshal_ContextLoad.c): `Marshal_ContextLoad.c`
- [Command Dispatcher](https://github.com/havenoverflow/tpm2/blob/main/CommandDispatcher.c): `CommandDispatcher.c`
- [Generated Types](https://github.com/havenoverflow/tpm2/blob/main/tpm_generated.h): `tpm_generated.h`
- [Memory Helpers](https://github.com/havenoverflow/tpm2/blob/main/MemoryLib_fp.h): `MemoryLib_fp.h`
- [TPM Internals](https://github.com/havenoverflow/tpm2/blob/main/Implementation.h): `Implementation.h`

### Simulator Platform

- [Platform Services](https://github.com/havenoverflow/tpm2/blob/main/Platform.h): `Platform.h`
- [TCP Simulator](https://github.com/havenoverflow/tpm2/blob/main/TcpServer.c): `TcpServer.c`

### Other components

- **TPM Client**

## Connections

- Context Save → Context Protection: uses protection (evidence: `Context_spt.c`)
- Context Load → Context Protection: uses protection (evidence: `Context_spt.c`)
- Start Auth Session → Session Management: creates session (evidence: `StartAuthSession.c`)
- Context Save → Global TPM State: reads state (evidence: `ContextSave.c`)
- Context Load → Global TPM State: updates state (evidence: `ContextLoad.c`)
- Context Save → Memory Helpers: copies context (evidence: `ContextSave.c`)
- Context Load → Memory Helpers: compares context (evidence: `ContextLoad.c`)
- TPM Client → TCP Simulator: sends requests
- TCP Simulator → Command Dispatcher: dispatches commands
- Command Dispatcher → Command Marshalling: routes commands
- Command Marshalling → Generated Types: uses types (evidence: `Marshal_ContextLoad.c`)
