tpm

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 18 Imported by: 3

README

tpm

This package is an abstraction on top of the go-tpm libraries to use a local TPM to create and use RSA, ECC, and AES keys that are bound to that TPM. The keys can never be used without the TPM that was used to create them.

Any number of keys can be created and used concurrently. The library takes care loading the right key in the TPM, as needed.

By default, 2048-bit RSA keys are created. AES keys, ECC keys, and RSA keys of different sizes can also be created if the TPM supports them.

Example:

package main

import (
	"crypto"
	"crypto/rsa"
	"crypto/sha256"
	"log"

	"github.com/c2FmZQ/tpm"
)

const keyPassphrase = "foobar"

func main() {
	tpm, err := tpm.New(tpm.WithObjectAuth([]byte(keyPassphrase)))
	if err != nil {
		log.Fatalf("tpm.New: %v", err)
	}
	defer tpm.Close()

	// CreateKey returns a []byte that can be saved offline.
	keyctx, err := tpm.CreateKey()
	if err != nil {
		log.Fatalf("tpm.CreateKey: %v", err)
	}

	key, err := tpm.Key(keyctx)
	if err != nil {
		log.Fatalf("tpm.Key: %v", err)
	}

	payload := "Hello world!"

	encrypted, err := key.Encrypt([]byte(payload))
	if err != nil {
		log.Fatalf("key.Encrypt: %v", err)
	}
	decrypted, err := key.Decrypt(nil, encrypted, nil)
	if err != nil {
		log.Fatalf("key.Decrypt: %v", err)
	}
	_ = decrypted

	hashed := sha256.Sum256([]byte(payload))
	sig, err := key.Sign(nil, hashed[:], crypto.SHA256)
	if err != nil {
		log.Fatalf("key.Sign: %v", err)
	}
	if err := rsa.VerifyPKCS1v15(key.Public().(*rsa.PublicKey), crypto.SHA256, hashed[:], sig); err != nil {
		log.Fatalf("rsa.VerifyPKCS1v15: %v", err)
	}
}

Documentation

Overview

Package tpm is an abstraction on top of the go-tpm libraries to use a local TPM to create and use RSA, ECC, and AES keys that are bound to that TPM. The keys can never be used without the TPM that was used to create them.

Any number of keys can be created and used concurrently. The library takes care loading the right key in the TPM, as needed.

By default, 2048-bit RSA keys are created. AES keys, ECC keys, and RSA keys of different sizes can also be created if the TPM supports them.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWrongKeyType = errors.New("operation not implemented with this key type")
	ErrInvalidCurve = errors.New("invalid curve id")
	ErrDecrypt      = errors.New("decryption error")
)

Functions

This section is empty.

Types

type Key

type Key struct {
	// contains filtered or unexported fields
}

Key executes the expected operations via the TPM. It implements the crypto.Signer and crypto.Decrypter interfaces.

func (*Key) Bits

func (k *Key) Bits() int

Bits returns the key size.

func (*Key) Curve

func (k *Key) Curve() elliptic.Curve

Curve returns the key curve ID.

func (*Key) Decrypt

func (k *Key) Decrypt(_ io.Reader, ciphertext []byte, _ crypto.DecrypterOpts) (plaintext []byte, err error)

Decrypt decrypts ciphertext with the key.

func (*Key) Encrypt

func (k *Key) Encrypt(cleartext []byte) (ciphertext []byte, err error)

Encrypt encrypts cleartext with the key.

func (*Key) Public

func (k *Key) Public() crypto.PublicKey

Public returns the public key.

func (*Key) Sign

func (k *Key) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error)

Sign signs a digest with the key (RSA only).

func (*Key) Type

func (k *Key) Type() KeyType

Type returns the key type.

type KeyOption

type KeyOption func(*keyOptions)

KeyOption is an option that can be passed to CreateKey.

func WithAES

func WithAES(bits int) KeyOption

WithAES indicates that an AES key should be created.

func WithECC

func WithECC(curve elliptic.Curve) KeyOption

WithECC indicates that an ECC key should be created.

func WithRSA

func WithRSA(bits int) KeyOption

WithRSA indicates that an RSA key should be created.

type KeyType

type KeyType int
const (
	TypeRSA KeyType = 1
	TypeECC KeyType = 2
	TypeAES KeyType = 3
)

func (KeyType) String

func (t KeyType) String() string

type Option

type Option func(*TPM)

Option is an option that can be passed to New.

func WithEndorsementAuth

func WithEndorsementAuth(pp []byte) Option

WithEndorsementAuth specifies the endorsement passphrase.

func WithObjectAuth

func WithObjectAuth(pp []byte) Option

WithObjectAuth specifies the passphrase to set on created keys.

func WithTPM

func WithTPM(rwc io.ReadWriteCloser) Option

WithTPM specifies an already open TPM device to use.

type TPM

type TPM struct {
	// contains filtered or unexported fields
}

TPM uses a local Trusted Platform Module (TPM) device to create and use RSA keys that are bound to that TPM. The private keys can never be used without the TPM created them.

func New

func New(opts ...Option) (*TPM, error)

New returns a new TPM that's ready to use.

func (*TPM) Close

func (t *TPM) Close() error

Close closes the connections to the TPM.

func (*TPM) CreateKey

func (t *TPM) CreateKey(opts ...KeyOption) ([]byte, error)

CreateKey creates a new key and returns a saved TPM context. The TPM context can be saved offline. Call Key() to use the key tied to the context. Any number of keys can be created.

func (*TPM) Key

func (t *TPM) Key(savedContext []byte) (*Key, error)

Key returns the Key tied to the saved TPM context.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL