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 1 commit
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
41 changes: 41 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"crypto/ed25519"
crand "crypto/rand"
"crypto/sha256"
"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 23 in keys.go

View check run for this annotation

Codecov / codecov/patch

keys.go#L17-L23

Added lines #L17 - L23 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
}

Check warning on line 31 in keys.go

View check run for this annotation

Codecov / codecov/patch

keys.go#L27-L31

Added lines #L27 - L31 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 40 in keys.go

View check run for this annotation

Codecov / codecov/patch

keys.go#L33-L40

Added lines #L33 - L40 were not covered by tests
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
}
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 50 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"time"

logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/urfave/cli/v2"
"go.opentelemetry.io/contrib/propagators/autoprop"
"go.opentelemetry.io/otel"
Expand All @@ -35,6 +36,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 50 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L39-L50

Added lines #L39 - L50 were not covered by tests
&cli.IntFlag{
Name: "gateway-port",
Value: 8090,
Expand Down Expand Up @@ -93,6 +106,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 125 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L109-L125

Added lines #L109 - L125 were not covered by tests
},
},
}

app.Name = "rainbow"
app.Usage = "a standalone ipfs gateway"
app.Version = version
Expand All @@ -101,6 +135,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 151 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L138-L151

Added lines #L138 - L151 were not covered by tests

gnd, err := Setup(cctx.Context, Config{
DataDir: ddir,
ConnMgrLow: cctx.Int("connmgr-low"),
Expand All @@ -109,7 +158,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 161 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L161

Added line #L161 was not covered by tests
RoutingV1: cctx.String("routing"),
KuboRPCURLs: getEnvs(EnvKuboRPC, DefaultKuboRPC),
DHTSharedHost: cctx.Bool("dht-fallback-shared-host"),
Expand Down
9 changes: 2 additions & 7 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
ListenAddrs []string
AnnounceAddrs []string

Libp2pKeyFile string
Libp2pKey crypto.PrivKey

ConnMgrLow int
ConnMgrHi int
Expand All @@ -102,11 +102,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 +124,7 @@
libp2p.ListenAddrStrings(cfg.ListenAddrs...),
libp2p.NATPortMap(),
libp2p.ConnectionManager(cmgr),
libp2p.Identity(peerkey),
libp2p.Identity(cfg.Libp2pKey),

Check warning on line 127 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L127

Added line #L127 was not covered by tests
libp2p.BandwidthReporter(bwc),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
Expand Down