# gizhw:tpm

a TPM 2.0 through the kernel's resource manager (/dev/tpmrm0): commands are built as bytes (big-endian, the TPM's wire format) and replies are checked and parsed. The TPM command codes and the layouts are the TCG TPM 2.0 specification's. Every call is on a Handle of gizhw:device.

```gizlang
import * as Tpm from "gizhw:tpm";
```

## Types

### `Property`

```gizlang
type Property = { property: i64; value: i64; }
```

A TPM property (TPM_CAP_TPM_PROPERTIES): its code and value.

- `property`: `i64`
- `value`: `i64`

### `Pcr`

```gizlang
type Pcr = { hash: i64; digest: bytes; }
```

The PCR digest the reply gives for one PCR (the first of the selection), and the selection's hash algorithm.

- `hash`: `i64`
- `digest`: `bytes`

## Functions

### `command`

```gizlang
function command(code: u64, body: bytes): bytes
```

A command: the header (tag, total size, code, all big-endian) and its parameters.

Effects: fail.

### `getRandomCommand`

```gizlang
function getRandomCommand(n: i64): bytes
```

TPM2_GetRandom: `n` random bytes.

Effects: fail.

### `getCapabilityCommand`

```gizlang
function getCapabilityCommand(capability: i64, property: i64, count: i64): bytes
```

TPM2_GetCapability for `capability`, from `property`, up to `count` values.

Effects: fail.

### `pcrReadCommand`

```gizlang
function pcrReadCommand(pcr: i64): bytes
```

TPM2_PCR_Read of one PCR, in the SHA-256 bank: the selection is one PCR bit in a 3-byte bitmap.

Effects: fail.

### `reply`

```gizlang
function reply(data: bytes): bytes throws string
```

The body of a reply, or the TPM's response code as the error.

Effects: fail.

### `send`

```gizlang
function send(h: Device.Handle, cmd: bytes): bytes throws string
```

Sends a command and gives the reply body.

Effects: fail, io.

### `parseRandom`

```gizlang
function parseRandom(body: bytes): bytes
```

The random bytes of a TPM2_GetRandom reply body (a 2-byte size, then the bytes).

Effects: fail.

### `getRandom`

```gizlang
function getRandom(h: Device.Handle, n: i64): bytes throws string
```

Random bytes from the TPM behind `h`.

Effects: fail, io.

### `parseProperties`

```gizlang
function parseProperties(body: bytes): Vec<Property>
```

The TPM properties in a TPM2_GetCapability reply body (more-data flag, capability, count, then code and value
pairs).

Effects: fail.

### `properties`

```gizlang
function properties(h: Device.Handle, count: i64): Vec<Property> throws string
```

The TPM properties from the TPM behind `h`.

Effects: fail, io.

### `parsePcr`

```gizlang
function parsePcr(body: bytes): Pcr
```

The first digest of a TPM2_PCR_Read reply body: the update counter, the selection, then the digests.

Effects: fail.

### `pcrRead`

```gizlang
function pcrRead(h: Device.Handle, pcr: i64): Pcr throws string
```

The digest of one PCR from the TPM behind `h`.

Effects: fail, io.

### `open`

```gizlang
function open(sys: System, d: Device.Device): Device.Handle throws string
```

The handle on the TPM's resource manager (/dev/tpmrmN), read-write.

Effects: fail, io.

### `tpms`

```gizlang
function tpms(sys: System): Vec<Device.Device> throws IoError
```

The TPMs the tpm class has.

Effects: fail, io.

