Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: key derivation for libp2p identities #28

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/libp2p/go-libp2p-record v0.2.0
github.com/libp2p/go-libp2p-routing-helpers v0.7.3
github.com/mitchellh/go-server-timing v1.0.1
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multiaddr v0.11.0
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/prometheus/client_golang v1.16.0
Expand All @@ -35,6 +36,7 @@ require (
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
golang.org/x/crypto v0.12.0
golang.org/x/sys v0.11.0
)

Expand Down Expand Up @@ -119,7 +121,6 @@ require (
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
Expand Down Expand Up @@ -172,7 +173,6 @@ require (
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
Expand Down
45 changes: 45 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"crypto/ed25519"
crand "crypto/rand"
"crypto/sha256"
"errors"
"io"

libp2p "github.com/libp2p/go-libp2p/core/crypto"
"github.com/mr-tron/base58"
"golang.org/x/crypto/hkdf"
)

const seedBytes = 32

// newSeed returns a b58 encoded random seed.
func newSeed() (string, error) {
bs := make([]byte, seedBytes)
_, err := io.ReadFull(crand.Reader, bs)
if err != nil {
return "", err
}
return base58.Encode(bs), nil

Check warning on line 24 in keys.go

View check run for this annotation

Codecov / codecov/patch

keys.go#L18-L24

Added lines #L18 - L24 were not covered by tests
}

// derive derives libp2p keys from a b58-encoded seed.
func deriveKey(b58secret string, info []byte) (libp2p.PrivKey, error) {
secret, err := base58.Decode(b58secret)
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
if len(secret) < seedBytes {
return nil, errors.New("derivation seed is too short")
}

Check warning on line 35 in keys.go

View check run for this annotation

Codecov / codecov/patch

keys.go#L28-L35

Added lines #L28 - L35 were not covered by tests

hash := sha256.New
hkdf := hkdf.New(hash, secret, nil, info)
keySeed := make([]byte, ed25519.SeedSize)
if _, err := io.ReadFull(hkdf, keySeed); err != nil {
return nil, err
}
key := ed25519.NewKeyFromSeed(keySeed)
return libp2p.UnmarshalEd25519PrivateKey(key)

Check warning on line 44 in keys.go

View check run for this annotation

Codecov / codecov/patch

keys.go#L37-L44

Added lines #L37 - L44 were not covered by tests
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
}
59 changes: 57 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"time"

logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/crypto"
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/urfave/cli/v2"
"go.opentelemetry.io/contrib/propagators/autoprop"
"go.opentelemetry.io/otel"
Expand All @@ -35,6 +37,18 @@
Value: "",
Usage: "specify the directory that cache data will be stored",
},
&cli.StringFlag{
Name: "seed",
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
Value: "",
EnvVars: []string{"RAINBOW_SEED"},
Usage: "Specify a seed to derive peerID from (needs --seed-index)",
},
&cli.IntFlag{
Name: "seed-index",
Value: -1,
EnvVars: []string{"RAINBOW_SEED_INDEX"},
Usage: "Specify an index to derivate the peerID from the key (needs --seed)",
},

Check warning on line 51 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L40-L51

Added lines #L40 - L51 were not covered by tests
&cli.IntFlag{
Name: "gateway-port",
Value: 8090,
Expand Down Expand Up @@ -93,6 +107,27 @@
},
}

app.Commands = []*cli.Command{
{
Name: "gen-seed",
Usage: "Generate a seed for key derivation",
Description: `
Running this command will generate a random seed and print it. The value can
be used with the RAINBOW_SEED env-var to use key-derivation from a single seed
to create libp2p identities for the gateway.
`,
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
seed, err := newSeed()
if err != nil {
return err
}
fmt.Println(seed)
return nil

Check warning on line 126 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L110-L126

Added lines #L110 - L126 were not covered by tests
},
},
}

app.Name = "rainbow"
app.Usage = "a standalone ipfs gateway"
app.Version = version
Expand All @@ -101,6 +136,21 @@
cdns := newCachedDNS(dnsCacheRefreshInterval)
defer cdns.Close()

var priv crypto.PrivKey
var err error
seed := cctx.String("seed")

index := cctx.Int("seed-index")
if len(seed) > 0 && index >= 0 {
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
priv, err = deriveKey(seed, []byte(fmt.Sprintf("rainbow-%d", index)))
} else {
keyFile := filepath.Join(ddir, "libp2p.key")
priv, err = loadOrInitPeerKey(keyFile)
}
if err != nil {
return err
}

Check warning on line 152 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L139-L152

Added lines #L139 - L152 were not covered by tests

gnd, err := Setup(cctx.Context, Config{
DataDir: ddir,
ConnMgrLow: cctx.Int("connmgr-low"),
Expand All @@ -109,7 +159,7 @@
MaxMemory: cctx.Uint64("max-memory"),
MaxFD: cctx.Int("max-fd"),
InMemBlockCache: cctx.Int64("inmem-block-cache"),
Libp2pKeyFile: filepath.Join(ddir, "libp2p.key"),
Libp2pKey: priv,

Check warning on line 162 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L162

Added line #L162 was not covered by tests
RoutingV1: cctx.String("routing"),
KuboRPCURLs: getEnvs(EnvKuboRPC, DefaultKuboRPC),
DHTSharedHost: cctx.Bool("dht-fallback-shared-host"),
Expand All @@ -133,7 +183,12 @@
Handler: handler,
}

fmt.Printf("Starting %s %s\n\n", name, version)
fmt.Printf("Starting %s %s\n", name, version)
pid, err := peer.IDFromPublicKey(priv.GetPublic())
if err != nil {
return err
}
fmt.Printf("PeerID: %s\n\n", pid)

Check warning on line 191 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L186-L191

Added lines #L186 - L191 were not covered by tests
registerVersionMetric(version)

tp, shutdown, err := newTracerProvider(cctx.Context)
Expand Down
13 changes: 5 additions & 8 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import (
"context"
crand "crypto/rand"
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -83,7 +85,7 @@
ListenAddrs []string
AnnounceAddrs []string

Libp2pKeyFile string
Libp2pKey crypto.PrivKey

ConnMgrLow int
ConnMgrHi int
Expand All @@ -102,11 +104,6 @@
}

func Setup(ctx context.Context, cfg Config) (*Node, error) {
peerkey, err := loadOrInitPeerKey(cfg.Libp2pKeyFile)
if err != nil {
return nil, err
}

ds, err := setupDatastore(cfg)
if err != nil {
return nil, err
Expand All @@ -129,7 +126,7 @@
libp2p.ListenAddrStrings(cfg.ListenAddrs...),
libp2p.NATPortMap(),
libp2p.ConnectionManager(cmgr),
libp2p.Identity(peerkey),
libp2p.Identity(cfg.Libp2pKey),

Check warning on line 129 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L129

Added line #L129 was not covered by tests
libp2p.BandwidthReporter(bwc),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
Expand Down Expand Up @@ -296,7 +293,7 @@
bn.Start(bswap)

err = os.Mkdir("denylists", 0755)
if err != nil {
if err != nil && !errors.Is(err, fs.ErrExist) {

Check warning on line 296 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L296

Added line #L296 was not covered by tests
return nil, err
}

Expand Down