# stefanberger/libtpms architecture

> Architecture diagram of the GitHub repository stefanberger/libtpms, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/stefanberger/libtpms
- Repository: https://github.com/stefanberger/libtpms

## Overview

##### Main Components
- Public API Layer: Represent the “include/” directory content (headers like tpm_error.h, tpm_library.h, tpm_memory.h, etc.) – these form the public interface that external applications (for example, Qemu) will link against.
- TPM Implementations:
– TPM 1.2 Components: Show the submodule in “src/tpm12” responsible for TPM commands and state for the TPM 1.2 standard.
– TPM 2.0 Components: Illustrate the “src/tpm2” directory, which is substantial and contains many files organized by function (e.g., command processing, session management, context handling, and state marshaling).
- Cryptographic & Helper Modules:
– Under “src/tpm2/crypto” (and specifically “src/tpm2/crypto/openssl”), include cryptographic functions, such as those supporting RSA, ECC, hashing, and related functions.
– Optionally, denote any helper routines (memory, error handling, debugging facilitators).
- Build and Configuration System:
– Include the autotools build system (configure.ac, bootstrap.sh, autogen.sh, Makefile.am) as components that drive the build pipeline.
- Testing and Fuzzing:
– Represent the “tests/” directory that contains a variety of tests (functional tests, fuzzers using libFuzzer, OSS-fuzz integration, and CVE reproductions) as a separate testing harness.

##### Relationships and Data Flow
- Additionally, illustrate that the Build System encapsulates components (the compilation, configuration, and testing processes) and that the Testing/Fuzzing components interact with the library to ensure robustness.

##### Architectural Patterns
- Separation of Concerns: Clearly separate the publicly exposed API from internal implementations.
- Modular Design: Distinguish the TPM versions (TPM 1.2 vs. TPM 2.0) and the cryptographic support as independent but communicating modules.
- Integration-Focused Design: Emphasize that libtpms intentionally exposes only a narrow API so that it minimizes dependencies and simplifies integration with external systems.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    EC("External Client (e.g. Qemu)"):::external

    %% Public API Layer
    subgraph "Public API Layer"
        API("Public API: tpm_library.h, tpm_error.h, tpm_memory.h, ..."):::external
    end

    %% Core Library Engine
    subgraph "Core Library Engine"
        CORE("Core Engine: tpm_library.c, tpm_memory.c, tpm_nvfile.c"):::core
    end

    %% TPM Implementation Modules
    subgraph "TPM Implementations"
        TPM1("TPM 1.2 Module (src/tpm12)"):::tpm
        TPM2("TPM 2.0 Module (src/tpm2)"):::tpm
    end

    %% Cryptographic & Helper Modules
    CRYPTO("OpenSSL Crypto Integration (src/tpm2/crypto/openssl)"):::crypto

    %% Build and Configuration System
    subgraph "Build and Configuration System"
        BUILD("Build System: configure.ac, autogen.sh, bootstrap.sh, Makefile.am"):::build
    end

    %% Testing and Fuzzing Harness
    subgraph "Testing and Fuzzing"
        TEST("Testing Harness (tests)"):::test
    end

    %% Connections
    EC -->|"calls"| API
    API -->|"forwards"| CORE
    CORE -->|"dispatches"| TPM1
    CORE -->|"dispatches"| TPM2
    TPM2 -->|"crypto operations"| CRYPTO
    TPM1 -->|"minor crypto calls"| CRYPTO

    %% Integration of Build and Testing
    BUILD -->|"builds"| CORE
    BUILD -->|"builds"| API
    TEST -->|"tests"| CORE
    TEST -->|"tests"| TPM1
    TEST -->|"tests"| TPM2

    %% Click Events
    click API "https://github.com/stefanberger/libtpms/tree/master/include/libtpms"
    click CORE "https://github.com/stefanberger/libtpms/blob/master/src/tpm_library.c"
    click TPM1 "https://github.com/stefanberger/libtpms/tree/master/src/tpm12"
    click TPM2 "https://github.com/stefanberger/libtpms/tree/master/src/tpm2"
    click CRYPTO "https://github.com/stefanberger/libtpms/tree/master/src/tpm2/crypto/openssl"
    click BUILD "https://github.com/stefanberger/libtpms/blob/master/configure.ac"
    click TEST "https://github.com/stefanberger/libtpms/tree/master/tests"

    %% Styles
    classDef external fill:#FCE4EC,stroke:#AD1457,stroke-width:2px;
    classDef core fill:#BBDEFB,stroke:#1565C0,stroke-width:2px;
    classDef tpm fill:#E1BEE7,stroke:#6A1B9A,stroke-width:2px;
    classDef crypto fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef build fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef test fill:#FFECB3,stroke:#FF6F00,stroke-width:2px;
```
