firstrun

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 7 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

Overview

Package firstrun makes and reads a bloc's first-run code, and derives the first-run key from it.

The first-run code lets the operator claim the first sign-in of a managing system deployed into the bloc. ocfp makes the code when it builds the bloc, keeps it in the bloc's vault, and shows it to the operator once. The managing system holds only the key's public half, which it reads from the bloc's config tree through its deployment manifest. A person proves they hold the code by signing a statement with the key the code derives.

The derivation is exact, because the managing system and its setup page each implement it again and check the vectors in testdata/vectors.json:

  1. The code is 15 random bytes in RFC 4648 base32, with the upper-case alphabet A to Z and 2 to 7 and no padding, which gives 24 characters and 120 bits.
  2. It is shown as six groups of four characters joined by hyphens.
  3. A typed code is normalized by dropping spaces and hyphens, upper-casing ASCII letters, and reading 0 as O and 1 as I. What remains must be exactly 24 characters of the alphabet.
  4. The seed is the SHA-256 of the ASCII string Domain, one zero byte, and the 24 normalized characters.
  5. The key pair is the RFC 8032 Ed25519 key pair with that seed, and the public half is its 32 bytes in standard base64.

Nothing in the package panics on any input. Every refusal is an *Error with a code, and no refusal repeats the code it was given.

The code, the seed, and the private key are secrets, so the package keeps them in byte slices and overwrites with zeros every buffer it owns before the call that made it returns. A Go string can't be overwritten, so a string code, and every string the string functions return, stays in memory until the collector reclaims it and the memory is reused. Where that matters, use the functions whose names end in Bytes. They read the caller's []byte code in place and never change or keep it, and they return []byte results that belong to the caller, who clears them with Wipe when done. A seed is cleared with Wipe(seed[:]), and a private key with Wipe(priv), because its first 32 bytes are the seed.

Wiping in Go is best effort. Every buffer the package owns is on the heap, where the collector never moves it, so Wipe clears its only copy. What the package can't clear is the 32-byte seed left in stack frames and result slots, in seedOf, in sha256.Sum256, and in the values Seed and SeedBytes return, along with anything the compiler spills from a register. SHA-256 and Ed25519 also keep copies in their own internal state, and no Go string can be cleared at all.

Index

Constants

View Source
const (
	// RandomBytes is how many random bytes a code encodes.
	RandomBytes = 15
	// CodeLength is the number of characters in a normalized code.
	CodeLength = 24
	// GroupSize is the number of characters in each group of a formatted
	// code.
	GroupSize = 4
	// PublicSize is the size of the public half in bytes.
	PublicSize = ed25519.PublicKeySize
)

The code's shape.

View Source
const (
	// CodeMalformed refuses a typed code that is not 24 characters of the
	// alphabet once spaces and hyphens are dropped.
	CodeMalformed = "FIRST_RUN_CODE_MALFORMED"
	// CodeRandomUnavailable refuses to make a code when the random source
	// fails or runs dry.
	CodeRandomUnavailable = "FIRST_RUN_RANDOM_UNAVAILABLE"
	// CodePublicInvalid refuses a public half that is not 32 bytes in
	// standard base64.
	CodePublicInvalid = "FIRST_RUN_PUBLIC_INVALID"
)

The codes of the package's refusals.

View Source
const Domain = "ocfp-first-run-key-v1"

Domain is the string the seed hashes before the code, so the seed is bound to this one use.

Variables

This section is empty.

Functions

func CodeOf

func CodeOf(err error) string

CodeOf returns the code of the first *Error in err's chain, or the empty string when err holds none.

func Format

func Format(code string) (string, error)

Format returns a code as six groups of four characters joined by hyphens. It normalizes the code first, and refuses with CodeMalformed what doesn't normalize. A Go string can't be wiped, so use FormatBytes where that matters.

func FormatBytes

func FormatBytes(code []byte) ([]byte, error)

FormatBytes is Format for a code held in bytes. It reads code and never changes or keeps it. The caller owns the formatted code it returns, and clears it with Wipe when done.

func Generate

func Generate() (string, error)

Generate makes a new code from the operating system's random source and returns it in its normalized form. A Go string can't be wiped, so use GenerateBytes where that matters.

func GenerateBytes

func GenerateBytes() ([]byte, error)

GenerateBytes makes a new code from the operating system's random source and returns it in its normalized form. The caller owns the code and clears it with Wipe when done.

func GenerateBytesFrom

func GenerateBytesFrom(r io.Reader) ([]byte, error)

GenerateBytesFrom makes a code from the next RandomBytes bytes of r and returns it in its normalized form. It refuses with CodeRandomUnavailable when r is nil, fails, or ends early. It wipes the random bytes it read, and the caller owns the code and clears it with Wipe when done.

func GenerateFrom

func GenerateFrom(r io.Reader) (string, error)

GenerateFrom makes a code from the next RandomBytes bytes of r and returns it in its normalized form. It refuses with CodeRandomUnavailable when r is nil, fails, or ends early. A Go string can't be wiped, so use GenerateBytesFrom where that matters.

func KeyPair

func KeyPair(code string) (ed25519.PublicKey, ed25519.PrivateKey, error)

KeyPair returns the Ed25519 key pair whose seed the code derives. The private key holds the seed in its first 32 bytes, so the caller clears it with Wipe(priv) when done. The public half is a separate buffer. A Go string can't be wiped, so use KeyPairBytes where the code matters too.

func KeyPairBytes

func KeyPairBytes(code []byte) (ed25519.PublicKey, ed25519.PrivateKey, error)

KeyPairBytes is KeyPair for a code held in bytes. It reads code and never changes or keeps it. The private key holds the seed in its first 32 bytes, so the caller clears it with Wipe(priv) when done.

func Normalize

func Normalize(typed string) (string, error)

Normalize returns a typed code in its normalized form of 24 characters. It drops spaces and hyphens, upper-cases ASCII letters, and reads 0 as O and 1 as I. It refuses with CodeMalformed anything that is then not exactly 24 characters of the base32 alphabet. It reads the input byte by byte, so a non-ASCII letter is refused rather than upper-cased into the alphabet. A Go string can't be wiped, so use NormalizeBytes where that matters.

func NormalizeBytes

func NormalizeBytes(typed []byte) ([]byte, error)

NormalizeBytes is Normalize for a code held in bytes. It reads typed and never changes or keeps it, so the caller still owns typed and wipes it. The caller also owns the normalized code it returns, and clears it with Wipe when done. On a refusal it wipes what it had written.

func ParsePublic

func ParsePublic(s string) (ed25519.PublicKey, error)

ParsePublic reads a public half from its standard base64 form. It refuses with CodePublicInvalid anything but the padded standard encoding of exactly 32 bytes.

func PublicFor

func PublicFor(code string) (string, error)

PublicFor returns the public half, in standard base64, of the key the code derives. It wipes the seed and the private key before it returns. A Go string can't be wiped, so use PublicForBytes where that matters.

func PublicForBytes

func PublicForBytes(code []byte) (string, error)

PublicForBytes is PublicFor for a code held in bytes. It reads code and never changes or keeps it, and it wipes the seed and the private key before it returns.

func PublicString

func PublicString(pub ed25519.PublicKey) (string, error)

PublicString returns a public half as its 32 bytes in standard base64. It refuses with CodePublicInvalid a key of any other size.

func Seed

func Seed(code string) ([sha256.Size]byte, error)

Seed returns the Ed25519 seed a code derives: the SHA-256 of Domain, one zero byte, and the code's 24 normalized characters. The caller owns the seed and clears it with Wipe(seed[:]) when done. A Go string can't be wiped, so use SeedBytes where the code matters too.

func SeedBytes

func SeedBytes(code []byte) ([sha256.Size]byte, error)

SeedBytes is Seed for a code held in bytes. It reads code and never changes or keeps it. The caller owns the seed and clears it with Wipe(seed[:]) when done.

func Wipe

func Wipe(b []byte)

Wipe overwrites every byte of b with zero. Use it on a []byte code, on a seed as Wipe(seed[:]), and on a private key as Wipe(priv), whose first 32 bytes are the seed. Wiping a private key leaves the public half that came with it intact, because that is a separate buffer.

Wipe is never inlined, so the compiler can't drop the zeros as stores to memory that is not read again.

Types

type Error

type Error struct {
	Code   string
	Detail string
}

Error is a coded refusal. Detail never holds the code it refused.

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

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