atecc

package
v0.0.0-...-fb366c3 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package atecc is a driver for the MicrochipTech ATECC608 device in Go.

It supports communication using I²C and USB.

This code is based on MicrochipTech's Cryptoauthlib code, thus its original copyright is retained for this code.

Copyright (c) 2022 Northvolt AB and the atecc authors. Copyright (c) 2015-2022 Microchip Technology Inc. and its subsidiaries.

Datasheets

Find all datasheets in the Trust Platform Design Suite git repository. https://github.com/MicrochipTech/cryptoauth_trustplatform_designsuite/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dev

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

func New

func New(ctx context.Context, hal HAL, cfg IfaceConfig) (*Dev, error)

New returns a new ATECC device using the supplied HAL for communication.

func NewHIDDev

func NewHIDDev(ctx context.Context, cfg IfaceConfig) (*Dev, io.Closer, error)

NewHIDDev returns an object that communicates over HID.

func NewI2CDev

func NewI2CDev(ctx context.Context, cfg IfaceConfig) (*Dev, error)

NewI2CDev returns an object that communicates over I²C to ATECC608A.

NOTE: the address passed in here is the actual target address. In upstream MicrochipTech cryptoauthlib, the address used is shifted.

func (*Dev) GenerateKey

func (d *Dev) GenerateKey(ctx context.Context, slot uint8) (crypto.PublicKey, error)

GenerateKey generates a new random private key in slot/handle.

func (*Dev) IsConfigZoneLocked

func (d *Dev) IsConfigZoneLocked(ctx context.Context) (bool, error)

IsConfigZoneLocked returns true if the configuration zone is locked.

This is the same as calling IsLocked(ctx, ZoneConfig).

func (*Dev) IsDataZoneLocked

func (d *Dev) IsDataZoneLocked(ctx context.Context) (bool, error)

IsDataZoneLocked returns true if the data zone is locked.

This is the same as calling IsLocked(ctx, ZoneData).

func (*Dev) IsLocked

func (d *Dev) IsLocked(ctx context.Context, zone Zone) (bool, error)

func (*Dev) LockConfigZone

func (d *Dev) LockConfigZone(ctx context.Context) error

func (*Dev) LockDataSlot

func (d *Dev) LockDataSlot(ctx context.Context, slot uint8) error

func (*Dev) LockDataZone

func (d *Dev) LockDataZone(ctx context.Context) error

func (*Dev) PrivateKey

func (d *Dev) PrivateKey(ctx context.Context, key uint8) (crypto.PrivateKey, error)

func (*Dev) PublicKey

func (d *Dev) PublicKey(ctx context.Context, slot uint8) (crypto.PublicKey, error)

PublicKey returns the public key in the specific slot.

func (*Dev) Random

func (d *Dev) Random(ctx context.Context) io.Reader

Random returns a random reader.

The underlying reader reads 32 byte random data from the device at a time.

Use io.ReadFull to fill a buffer.

func (*Dev) ReadConfigZone

func (d *Dev) ReadConfigZone(ctx context.Context) ([]byte, error)

ReadConfigZone reads the complete device configuration zone.

func (*Dev) ReadZone

func (d *Dev) ReadZone(ctx context.Context, zone Zone, slot uint16, block uint8, offset uint8, b []byte) (int, error)

func (*Dev) Revision

func (d *Dev) Revision(ctx context.Context) ([]byte, error)

Revision gets the device revision.

This information is hard coded into the device. Use it to determine the version of the device.

func (*Dev) SerialNumber

func (d *Dev) SerialNumber(ctx context.Context) ([]byte, error)

SerialNumber returns the serial number of the device.

The returned serial number will be 9 bytes.

func (*Dev) Sign

func (d *Dev) Sign(ctx context.Context, key int, msg []byte) ([]byte, error)

Sign signs the message using the private key in the specified slot.

This function executes the sign command to sign a 32-byte external message using the private key in the specified slot. It returns the ASN.1 encoded signature.

func (*Dev) VerifyExtern

func (d *Dev) VerifyExtern(ctx context.Context, msg, sig []byte, pub crypto.PublicKey) (bool, error)

VerifyExtern verifies a signature using external input.

The signature provided is expected to be in ASN.1 format.

func (*Dev) WriteBytesZone

func (d *Dev) WriteBytesZone(ctx context.Context, zone Zone, slot uint16, offset uint8, data []byte) error

WriteBytesZone writes the data into the config, OTP or data zone.

If ZoneConfig is unlocked, it may be written to. If ZoneData is unlocked, 32-byte writes are allowed to slots and OTP.

Offset and length must be multiples of 32 or the write will fail.

func (*Dev) WriteConfigZone

func (d *Dev) WriteConfigZone(ctx context.Context, data []byte) error

WriteConfigZone writes the data into the config zone.

This method works similar to how WriteBytesZone work except that it also writes the UserExtraData if all other data was written successfully.

Warning: if UserExtraData or UserExtraDataAdd is not 0x55 ('U'), these values will be permanent and the corresponding zones will be locked. If so, this is irreversible!

type DeviceType

type DeviceType int

DeviceType represents a physical device type.

const (
	DeviceATECC608 DeviceType = iota
)

func DeviceTypeFromInfo

func DeviceTypeFromInfo(revision []byte) (DeviceType, error)

DeviceTypeFromInfo returns the device type based on the info byte array.

func (DeviceType) String

func (dt DeviceType) String() string

type HAL

type HAL interface {
	// Read reads up to len(p) bytes into p from the device.
	Read(p []byte) (int, error)
	// Write writes len(p) bytes from p to the device.
	Write(p []byte) (int, error)
	// idle puts the device into idle state.
	Idle() error
	// Wake wakes the device up.
	Wake() error
}

type HIDConfig

type HIDConfig struct {
	// DevIndex is the HID enumeration index to use unless DevIdentity is set.
	DevIndex int

	// KitType indicates the underlying interface to use.
	//
	// This is known as dev_interface in cryptoauthlib.
	KitType KitType

	// DevIdentity is the identity of the device.
	//
	// For I²C, this is the I²C target address. For the SWI interface, this is
	// the bus number.
	DevIdentity uint8

	// VendorID of the kit.
	VendorID uint16

	// ProductID of the kit.
	ProductID uint16

	// PacketSize is the size of the USB packet.
	PacketSize int
}

type I2CConfig

type I2CConfig struct {
	Address uint16
	Bus     i2c.Bus
}

type IfaceConfig

type IfaceConfig struct {
	// IfaceType affects how communication with the device is done.
	IfaceType IfaceType
	// DeviceType affects how communication with the device is done.
	DeviceType DeviceType
	// I2C contains I²C specific configuration.
	I2C I2CConfig
	// HID contains HID specific configuration.
	HID HIDConfig
	// WakeDelay defines the time to wait for the device before waking up.
	//
	// This represents the tWHI + tWLO and is configured based on device type.
	WakeDelay time.Duration
	// RxRetries is the number of retries to attempt when receiving data.
	RxRetries int
	// Debug is used for debug output.
	Debug Logger
}

IfaceConfig is the configuration object for a device.

Logical device configurations describe the device type and logical interface.

func ConfigATECCX08A_I2CDefault

func ConfigATECCX08A_I2CDefault(bus i2c.Bus) IfaceConfig

ConfigATECCX08A_I2CDefault returns a default config for an ECCx08A device.

TODO: re-think where we put bus, who owns it (who closes, do we have Close?)

func ConfigATECCX08A_KitHIDDefault

func ConfigATECCX08A_KitHIDDefault() IfaceConfig

ConfigATECCX08A_KitHIDDefault returns a configuration for the Kit protocol.

type IfaceType

type IfaceType int
const (
	IfaceI2C IfaceType = iota
	IfaceHID
)

type KitType

type KitType int
const (
	KitTypeAuto KitType = iota
	KitTypeI2C
	KitTypeSWI
	KitTypeSPI
)

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger is the interface used for debug messages.

Some messages will be multiple lines.

type Zone

type Zone uint8

Zone is a configuration zone.

const (
	ZoneConfig Zone = 0x00
	ZoneOTP    Zone = 0x01
	ZoneData   Zone = 0x02
)

Configuration zones.

Jump to

Keyboard shortcuts

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