detached 973e682

issue_keys.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package usecase
4
5import (
6	"context"
7	"crypto/hkdf"
8	"crypto/hmac"
9	"crypto/sha256"
10	"fmt"
11
12	"bnema.dev/ermite/internal/domain"
13	"bnema.dev/ermite/internal/ports"
14)
15
16// HKDF info strings of the subkeys of the issue form key. Each purpose has its
17// own key, so that a value signed for one can never pass for another.
18const (
19	infoIPHash    = "ermite issue ip-hash"
20	infoPoW       = "ermite issue pow"
21	infoFormToken = "ermite issue form-token"
22)
23
24// IssueKeys holds the subkeys of the public issue form, derived with
25// HKDF-SHA256 from the server key of the IssueKeyStore.
26type IssueKeys struct {
27	ipHash, pow, formToken []byte
28}
29
30// NewIssueKeys loads the server key (generated on the first call) and derives
31// the subkeys.
32func NewIssueKeys(ctx context.Context, store ports.IssueKeyStore) (*IssueKeys, error) {
33	master, err := store.Key(ctx)
34	if err != nil {
35		return nil, fmt.Errorf("load issue key: %w", err)
36	}
37	k := &IssueKeys{}
38	for _, d := range []struct {
39		dst  *[]byte
40		info string
41	}{{&k.ipHash, infoIPHash}, {&k.pow, infoPoW}, {&k.formToken, infoFormToken}} {
42		if *d.dst, err = hkdf.Key(sha256.New, master, nil, d.info, sha256.Size); err != nil {
43			return nil, fmt.Errorf("derive %q: %w", d.info, err)
44		}
45	}
46	return k, nil
47}
48
49// IPHash returns HMAC-SHA256 of the client key (clientlimit.Key of the
50// address) under the ip-hash subkey: domain.IPHashLen bytes. It is stable
51// across restarts and says nothing about the address without the key.
52func (k *IssueKeys) IPHash(addrKey string) []byte {
53	m := hmac.New(sha256.New, k.ipHash)
54	m.Write([]byte(addrKey))
55	return m.Sum(nil)[:domain.IPHashLen]
56}
57
58// PoWKey returns a copy of the subkey that signs the proof-of-work challenges.
59func (k *IssueKeys) PoWKey() []byte { return append([]byte(nil), k.pow...) }
60
61// FormTokenKey returns a copy of the subkey that signs the form tokens.
62func (k *IssueKeys) FormTokenKey() []byte { return append([]byte(nil), k.formToken...) }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 4

Tags 45

↑↓ moveenter openesc close