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:
- 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.
- It is shown as six groups of four characters joined by hyphens.
- 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.
- The seed is the SHA-256 of the ASCII string Domain, one zero byte, and the 24 normalized characters.
- 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
- func CodeOf(err error) string
- func Format(code string) (string, error)
- func FormatBytes(code []byte) ([]byte, error)
- func Generate() (string, error)
- func GenerateBytes() ([]byte, error)
- func GenerateBytesFrom(r io.Reader) ([]byte, error)
- func GenerateFrom(r io.Reader) (string, error)
- func KeyPair(code string) (ed25519.PublicKey, ed25519.PrivateKey, error)
- func KeyPairBytes(code []byte) (ed25519.PublicKey, ed25519.PrivateKey, error)
- func Normalize(typed string) (string, error)
- func NormalizeBytes(typed []byte) ([]byte, error)
- func ParsePublic(s string) (ed25519.PublicKey, error)
- func PublicFor(code string) (string, error)
- func PublicForBytes(code []byte) (string, error)
- func PublicString(pub ed25519.PublicKey) (string, error)
- func Seed(code string) ([sha256.Size]byte, error)
- func SeedBytes(code []byte) ([sha256.Size]byte, error)
- func Wipe(b []byte)
- type Error
Constants ¶
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.
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" // 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.
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 ¶
CodeOf returns the code of the first *Error in err's chain, or the empty string when err holds none.
func Format ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
PublicString returns a public half as its 32 bytes in standard base64. It refuses with CodePublicInvalid a key of any other size.
func Seed ¶
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 ¶
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.