aboutsummaryrefslogtreecommitdiff
path: root/pkg/okey/okey.go
blob: aba4f3e550f7e7de7656bb5a03c7f3a93e12af38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Package okey provides access to onion service private keys
package okey

import (
	"crypto"
	"fmt"
	"os"

	bed25519 "github.com/cretz/bine/torutil/ed25519"
)

// New parses the content of Tor's hs_ed25519_secret_key file by interpretting
// bytes 32..96 as the 64-byte expanded seed.  For reference, see:
// https://gitlab.torproject.org/tpo/core/tor/-/blob/main/src/feature/keymgt/loadkey.c#L379
func New(b []byte) (crypto.Signer, error) {
	if len(b) != 96 {
		return nil, fmt.Errorf("invalid key file size: %d", len(b))
	}
	return bed25519.PrivateKey(b[32:96]), nil
}

// NewFromHSDir reads and parses the hs_ed25519_secret_key file in a given directory
func NewFromHSDir(dir string) (crypto.Signer, error) {
	b, err := os.ReadFile(dir + "/hs_ed25519_secret_key")
	if err != nil {
		return nil, err
	}
	return New(b)
}