enclave

package
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for darwin/amd64

Overview

Package enclave wraps macOS Security.framework P-256 keys (Secure Enclave hardware keys with automatic fallback to ephemeral software SecKey handles) and provides hybrid ECIES + AES-256-GCM envelope sealing for large payloads such as model weights and KV-cache chunks.

All Security.framework and CoreFoundation calls use purego bindings with zero cgo.

Secure Enclave key creation requires a code-signed binary with the keychain-access-groups (or application-identifier) entitlement. Call Probe to inspect whether Secure Enclave keys are available in the current process. By default Generate attempts to create a Secure Enclave key and falls back to an ephemeral software P-256 SecKey when the entitlement is absent; set Options.RequireHardware to fail closed instead.

Basic usage:

res := enclave.Probe()
fmt.Printf("secure enclave available=%t\n", res.Available)

key, err := enclave.Generate(enclave.Options{})
if err != nil {
	log.Fatal(err)
}
defer key.Close()

aad := []byte("model=llama-70b;tenant=acme")
sealed, err := enclave.SealPayload(key.PublicKeyBytes(), weights, aad)
if err != nil {
	log.Fatal(err)
}
plain, err := enclave.OpenPayload(key, sealed, aad)
if err != nil {
	log.Fatal(err)
}
Example
package main

import (
	"crypto/sha256"
	"fmt"
	"log"

	"github.com/tmc/apple/x/enclave"
)

func main() {
	key, err := enclave.Generate(enclave.Options{})
	if err != nil {
		log.Fatal(err)
	}
	defer key.Close()

	digest := sha256.Sum256([]byte("node-attestation-v1"))
	sig, err := key.Sign(digest[:])
	if err != nil {
		log.Fatal(err)
	}
	if err := key.Verify(digest[:], sig); err != nil {
		log.Fatal(err)
	}

	aad := []byte("model=demo;tenant=acme")
	sealed, err := enclave.SealPayload(key.PublicKeyBytes(), []byte("weights"), aad)
	if err != nil {
		log.Fatal(err)
	}
	plain, err := enclave.OpenPayload(key, sealed, aad)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(plain))
}
Output:
weights

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrHardwareUnavailable is returned when Options.RequireHardware is true
	// and Secure Enclave key creation is not available in the current process.
	ErrHardwareUnavailable = errors.New("enclave: secure enclave unavailable")

	// ErrClosed is returned when operating on a Key that has been closed.
	ErrClosed = errors.New("enclave: key closed")

	// ErrInvalidDigest is returned when a digest passed to Sign or Verify is
	// not a 32-byte SHA-256 digest.
	ErrInvalidDigest = errors.New("enclave: digest must be 32 bytes (SHA-256)")

	// ErrInvalidPublicKey is returned when an X9.63 P-256 public key is
	// malformed or rejected by Security.framework.
	ErrInvalidPublicKey = errors.New("enclave: invalid P-256 public key")

	// ErrInvalidEnvelope is returned when a sealed payload has a malformed
	// header, unsupported version, or truncated body.
	ErrInvalidEnvelope = errors.New("enclave: invalid sealed envelope")

	// ErrAuthFailed is returned when signature verification, ECIES decryption,
	// or AES-GCM authentication fails.
	ErrAuthFailed = errors.New("enclave: authentication failed")
)

Functions

func OpenPayload

func OpenPayload(key *Key, sealed, aad []byte) ([]byte, error)

OpenPayload unwraps and decrypts a hybrid envelope produced by SealPayload using key. Both the envelope header and aad are authenticated; any tampering with the header, wrapped DEK, ciphertext, or aad fails closed with an error wrapping ErrAuthFailed (or ErrInvalidEnvelope if structurally malformed).

func SealPayload

func SealPayload(pub, plaintext, aad []byte) ([]byte, error)

SealPayload encrypts plaintext of arbitrary size using a hybrid envelope: an ephemeral random 32-byte AES-256 data-encryption key (DEK) is wrapped under pub using P-256 ECIES (X9.63 SHA-256 + AES-GCM with variable IV), and plaintext is encrypted with AES-256-GCM bound to both the envelope header and aad.

pub must be a 65-byte uncompressed ANSI X9.63 P-256 public key as returned by Key.PublicKeyBytes.

Types

type Key

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

Key holds a macOS Security.framework P-256 key pair backed either by the Secure Enclave or by an ephemeral software SecKey.

A Key must be released with Key.Close when no longer needed. All methods are safe for concurrent use.

func Generate

func Generate(opts Options) (*Key, error)

Generate creates a new ephemeral P-256 key pair.

It first attempts to create a hardware-backed key in the Secure Enclave. If that fails and opts.RequireHardware is false, it falls back to an ephemeral software P-256 SecKey created via SecKeyCreateRandomKey. Call Key.IsHardwareBacked to check which backing was used.

func (*Key) Close

func (k *Key) Close() error

Close releases the underlying Security.framework key references. Calling Close more than once is a no-op.

func (*Key) IsHardwareBacked

func (k *Key) IsHardwareBacked() bool

IsHardwareBacked reports whether k is backed by the Secure Enclave hardware token rather than an ephemeral software SecKey.

func (*Key) PublicKeyBytes

func (k *Key) PublicKeyBytes() []byte

PublicKeyBytes returns a copy of the 65-byte uncompressed ANSI X9.63 P-256 public key (0x04 || X || Y). Returns nil if k is nil or closed.

func (*Key) SealDEK

func (k *Key) SealDEK(dek []byte) ([]byte, error)

SealDEK wraps a data-encryption key (typically 32 bytes for AES-256) using P-256 ECIES (X9.63 SHA-256 + AES-GCM with variable IV) under k's public key.

func (*Key) Sign

func (k *Key) Sign(digest []byte) ([]byte, error)

Sign signs a 32-byte SHA-256 digest using ECDSA-SHA256 (X9.62 DER-encoded signature).

func (*Key) UnsealDEK

func (k *Key) UnsealDEK(wrapped []byte) ([]byte, error)

UnsealDEK unwraps an ECIES-encrypted data-encryption key using k's private key. It returns an error wrapping ErrAuthFailed if decryption or authentication fails.

func (*Key) Verify

func (k *Key) Verify(digest, sig []byte) error

Verify checks an X9.62 DER-encoded ECDSA signature over a 32-byte SHA-256 digest using k's public key. It returns nil if valid or an error wrapping ErrAuthFailed if verification fails.

type Options

type Options struct {
	// RequireHardware fails key generation with [ErrHardwareUnavailable]
	// instead of falling back to an ephemeral software P-256 SecKey when the
	// Secure Enclave is unavailable in the current process.
	RequireHardware bool

	// ApplicationTag optionally attaches a private application tag to the key.
	ApplicationTag []byte
}

Options configures Generate.

type ProbeResult

type ProbeResult struct {
	// Available is true if the current process can create an ephemeral P-256
	// key in the Secure Enclave.
	Available bool

	// Reason explains why Secure Enclave key creation failed when Available is
	// false. Empty when Available is true.
	Reason string

	// OSStatus is the Security.framework error code returned during probing
	// when Available is false, or 0 when Available is true.
	OSStatus int
}

ProbeResult describes Secure Enclave key availability in the current process.

func Probe

func Probe() ProbeResult

Probe tests whether the current process can create an ephemeral P-256 key in the Secure Enclave. If creation succeeds, the probe key is immediately released.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/enclave"
)

func main() {
	res := enclave.Probe()
	fmt.Printf("probed=%t\n", res.Available || res.Reason != "")
}
Output:
probed=true

Jump to

Keyboard shortcuts

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