diff options
Diffstat (limited to 'pkg/okey/okey.go')
-rw-r--r-- | pkg/okey/okey.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/pkg/okey/okey.go b/pkg/okey/okey.go new file mode 100644 index 0000000..aba4f3e --- /dev/null +++ b/pkg/okey/okey.go @@ -0,0 +1,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) +} |