Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copilot Instructions for TSS.MSR

## Project Overview

TSS.MSR is a multi-language TPM 2.0 Software Stack providing complete TPM 2.0 API abstractions. It contains parallel implementations in C# (.NET), C++, Java, TypeScript/Node.js, Python, and Rust, plus the **TssCodeGen** tool that auto-generates TPM type/command definitions across all languages from the TPM 2.0 specification documents.

## Architecture

### Code Generation Pipeline (TssCodeGen)

TssCodeGen is the central tool that keeps all language implementations in sync with the TPM 2.0 spec. It runs a 3-phase workflow:

1. **Table Extraction** — Parses TPM 2.0 spec Word documents into `RawTables.xml` (slow, uses Office interop; cached)
2. **Type Extraction** — Builds an AST of all TPM 2.0 entities from the raw tables
3. **Code Generation** — Language-specific generators (`CGenDotNet`, `CGenCpp`, `CGenJava`, `CGenNode`, `CGenPy`, `CGenRust`) emit target code

**Generated output files:**
- **TSS.NET:** `TSS.Net/X_TpmDefs.cs`
- **TSS.CPP:** `TpmTypes.h`, `Tpm2.h`, `TpmTypes.cpp` (between `<<AUTOGEN_BEGIN>>` / `<<AUTOGEN_END>>` markers)
- **TSS.Java:** One `.java` file per type in `src/tss/tpm/` plus `Tpm.java`
- **TSS.JS:** `src/TpmTypes.ts`, `src/Tpm.ts`
- **TSS.Py:** `src/TpmTypes.py`, `src/Tpm.py`

**⚠️ Never manually edit auto-generated files.** They will be overwritten by TssCodeGen. Look for file headers stating "automatically generated" or `<<AUTOGEN_BEGIN>>` markers.

### Extension Mechanism (.snips files)

Each language has `.snips` files (e.g., `TpmExtensions.js.snips`) that inject hand-written methods into auto-generated classes during code generation. Lines starting with `>> CLASSNAME` mark insertion points for the target class.

### Shared Patterns Across All Languages

- **TpmStructure base class** — All TPM types inherit from it; provides `toTpm()`/`initFromTpm()` (or language equivalent) for binary serialization
- **Union-as-interface** — TPM unions are represented as interfaces (e.g., `TPMU_SCHEME_KEYEDHASH`), with concrete structs implementing them. Each implementer provides `GetUnionSelector()` returning the discriminator value
- **Device abstraction** — Platform-specific TPM access (Linux `/dev/tpmrm0`, Windows TBS, TCP simulator) behind a common `TpmDevice` interface/abstract class

### Naming Conventions

| Aspect | TSS.NET | Other Languages |
|--------|---------|-----------------|
| Types | CamelCase, drops `TPM_`/`TPMS_` prefixes (`TpmAlgId`, `TpmRsa`) | Preserves spec names (`TPM_ALG_ID`, `TPMS_RSA_PARMS`) |
| Fields | PascalCase (`AuthPolicy`) | camelCase (JS/Py) or spec-style (Java/Rust) |
| Commands | `Tpm2.Hash()` | `tpm.Hash()` or similar |

## Build Commands

### C++ (`TSS.CPP/`)
```bash
make # Build library + samples (debug)
make CONFIG=release # Release build
make test # Build and run samples as tests
make clean
```

### Java (`TSS.Java/`)
```bash
mvn clean compile # Compile
mvn clean install # Build and install
```

### .NET (`TSS.NET/`)
```bash
dotnet build TSS.NET.sln
```
Targets: .NET 4.7.2 and .NET 5. Release builds use strong-name signing.

### Rust (`TSS.Rust/`)
```bash
cargo build
cargo test
cargo run --example tpm_samples
```

### TssCodeGen
```bash
TssCodeGen [-spec <path>] [-dest <path>] [-extract] [-dotNet] [-cpp] [-java] [-node] [-py]
```
Use `-extract` to force re-parsing of spec documents. Without it, uses cached `RawTables.xml`. Language flags generate only selected targets.

### Tpm2Tester
```bash
# Run specific test profiles against a TPM simulator
TestSuiteApp -device tcp -address localhost:2321 -randSeed startup nv

# Run a single test
TestSuiteApp -device tcp -tests TestCaseName

# Stress test
TestSuiteApp -device tcp -stress -threads 4 -mins 10
```

## Key Dependencies

| Language | Crypto Library | Native Access |
|----------|---------------|---------------|
| .NET | BouncyCastle.NetCore | - |
| Java | BouncyCastle (bcprov-jdk15on) | JNA |
| JS | - | ffi-napi, ref-napi |
| Rust | rsa, aes, sha1/sha2, hmac | windows crate (Win32) / libc (Unix) |
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,6 @@ FakesAssemblies/
/Tpm2Tester/TestSuite/Properties/launchSettings.json
*.map
*.json

# Rust
target/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ All flavors of TPM 2.0 devices mentioned in the previous section communicate wit

In order to facilitate the development of applications and services using TPM 2.0, Microsoft has developed a series of TSS implementations for different programming languages. All these implementations provide complete representation of the TPM 2.0 API (commands, data structures, enumerations, unions) using the means of the corresponding languages, and some of them - additional functionality that greatly simplifies communications with TPM 2.0. All TSS.MSR implementations provide abstraction for Windows/Linux/Simulator TPM 2.0 devices.

### [TSS.Net] and [TSS.CPP]
### [TSS.Net], [TSS.CPP] and [TSS.Rust]

TSS.Net and TSS.CPP are written in C# and C++ correspondingly, and are the richest TSS implementations in this collection. Besides complete abstraction of the TPM 2.0 interface, they implement additional functionality, such as:
TSS.Net, TSS.CPP and TSS.Rust are written in C#, C++ and Rust correspondingly, and are the richest TSS implementations in this collection. Besides complete abstraction of the TPM 2.0 interface, they implement additional functionality, such as:

* automatic handling of HMAC and policy sessions;
* expected audit, policy and cpHashes computation;
Expand All @@ -48,6 +48,8 @@ Along with it comes a [sample test suite][TestSuite] that not only demonstrates

TSS.Net is a cross-platform .NET Standard library and requires Visual Studio 2017 or above to build it. It can target one of the following .NET framework flavors: .NET 4.7.2, .NET Core 2.1 (for both Windows and Linux), .NET Standard 2.0, and .NET UWP 10.0. You can download the latest versions of the .NET Framework [here](https://www.microsoft.com/net/download/windows).

TSS.Rust requires Rust 1.70 or above and uses Cargo as its build system.

TSS.Java uses Java SE 8 or above, TSS.JS requires Node.js 4.8.4 or higher, and TSS.Py supports Python 2.7 and 3.5+.

## Platform Crypto Provider Toolkit
Expand All @@ -70,6 +72,7 @@ For private feedback please use tssdotnet@microsoft.com (for all managed languag

[TSS.Net]: ./TSS.NET
[TSS.CPP]: ./TSS.CPP
[TSS.Rust]: ./TSS.Rust
[TSS.Java]: ./TSS.Java
[TSS.JS]: ./TSS.JS
[TSS.Py]: ./TSS.Py
Expand Down
2 changes: 1 addition & 1 deletion TSS.CPP/Src/TpmExtensions.cpp.snips
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static vector<TPMS_PCR_SELECTION> GetSelectionArray(TPM_ALG_ID hashAlg, UINT32 p
/// <summary> Is the PCR with index _pcr selected in this TPMS_PCR_SELECTION. </summary>
bool PcrIsSelected(UINT32 pcr)
{
return pcrSelect[pcr / 8] = (1 << (pcr % 8)) != 0;
return (pcrSelect[pcr / 8] >> (pcr % 8)) & 1;
}

/// <summary> Return the current PCR-selection as a UINT32 array. </summary>
Expand Down
2 changes: 1 addition & 1 deletion TSS.CPP/include/TpmTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3653,7 +3653,7 @@ class _DLLEXP_ TPMS_PCR_SELECTION : public TpmStructure
/// <summary> Is the PCR with index _pcr selected in this TPMS_PCR_SELECTION. </summary>
bool PcrIsSelected(UINT32 pcr)
{
return pcrSelect[pcr / 8] = (1 << (pcr % 8)) != 0;
return (pcrSelect[pcr / 8] >> (pcr % 8)) & 1;
}

/// <summary> Return the current PCR-selection as a UINT32 array. </summary>
Expand Down
2 changes: 1 addition & 1 deletion TSS.JS/src/TpmBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class TpmBase
{
// If the caller has not provided a session for a handle that requires authorization,
// a password session is automatically created.
if (this.sessions == null)
if (this.sessions == null)
this.sessions = new Array<Session>(numAuthHandles);
else if (this.sessions.length < numAuthHandles)
this.sessions = this.sessions.concat(new Array<Session>(numAuthHandles - this.sessions.length));
Expand Down
12 changes: 6 additions & 6 deletions TSS.Java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.67</version>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.4.0</version>
<version>5.14.0</version>
</dependency>
</dependencies>
<build>
Expand All @@ -53,7 +53,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<version>3.12.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand Down Expand Up @@ -95,11 +95,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.6.3</version>
<configuration>
<!-- needed to build with JDK 11/12/13 -->
<source>8</source>
<additionalparam>-Xdoclint:none</additionalparam>
<doclint>all,-missing</doclint>
</configuration>
<executions>
<execution>
Expand Down
Loading