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

Secure address generation #1170

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
48 changes: 32 additions & 16 deletions cmd/genkeys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ package main
import (
"crypto/ed25519"
"encoding/hex"
"flag"
"fmt"
"net"
"runtime"
"time"

"github.com/yggdrasil-network/yggdrasil-go/src/address"
"github.com/yggdrasil-network/yggdrasil-go/src/config"
)

type keySet struct {
Expand All @@ -27,23 +29,37 @@ type keySet struct {
}

func main() {
threads := runtime.GOMAXPROCS(0)
fmt.Println("Threads:", threads)
security := flag.Int("security", 0, "generates a key with a specific amount of security bits. defaults to 0 which continuously generates more keys")
flag.Parse()

start := time.Now()
var currentBest ed25519.PublicKey
newKeys := make(chan keySet, threads)
for i := 0; i < threads; i++ {
go doKeys(newKeys)
}
for {
newKey := <-newKeys
if isBetter(currentBest, newKey.pub) || len(currentBest) == 0 {
currentBest = newKey.pub
fmt.Println("-----", time.Since(start))
fmt.Println("Priv:", hex.EncodeToString(newKey.priv))
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
addr := address.AddrForKey(newKey.pub)
fmt.Println("IP:", net.IP(addr[:]).String())
if (*security > 0) {
// If higher than 0, generates a key with the set amount of security bits
var secureKey keySet
secureKey.priv, secureKey.pub = config.NewSecureKeyPair(*security)
fmt.Println("-----", time.Since(start))
fmt.Println("Priv:", hex.EncodeToString(secureKey.priv))
fmt.Println("Pub:", hex.EncodeToString(secureKey.pub))
addr := address.AddrForKey(secureKey.pub)
fmt.Println("IP:", net.IP(addr[:]).String())
} else {
threads := runtime.GOMAXPROCS(0)
fmt.Println("Threads:", threads)
var currentBest ed25519.PublicKey
newKeys := make(chan keySet, threads)
for i := 0; i < threads; i++ {
go doKeys(newKeys)
}
for {
newKey := <-newKeys
if isBetter(currentBest, newKey.pub) || len(currentBest) == 0 {
currentBest = newKey.pub
fmt.Println("-----", time.Since(start))
fmt.Println("Priv:", hex.EncodeToString(newKey.priv))
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
addr := address.AddrForKey(newKey.pub)
fmt.Println("IP:", net.IP(addr[:]).String())
}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions cmd/yggdrasil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type node struct {
// The main function is responsible for configuring and starting Yggdrasil.
func main() {
genconf := flag.Bool("genconf", false, "print a new config to stdout")
security := flag.Int("security", 0, "use in combination with either -genconf or -autoconf, generates a higher security address up to the security bits desired")
useconf := flag.Bool("useconf", false, "read HJSON/JSON config from stdin")
useconffile := flag.String("useconffile", "", "read HJSON/JSON config from specified file path")
normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised")
Expand All @@ -53,10 +54,10 @@ func main() {
getpkey := flag.Bool("publickey", false, "use in combination with either -useconf or -useconffile, outputs your public key")
loglevel := flag.String("loglevel", "info", "loglevel to enable")
flag.Parse()

done := make(chan struct{})
defer close(done)

// Catch interrupts from the operating system to exit gracefully.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)

Expand Down Expand Up @@ -87,6 +88,11 @@ func main() {
}

cfg := config.GenerateConfig()
if (*security > 0) {
// Checks if the security flag is set, and generates a key with that many security bits
newKey, _ := config.NewSecureKeyPair(*security)
cfg.PrivateKey = []byte(newKey)
}
var err error
switch {
case *ver:
Expand Down Expand Up @@ -271,7 +277,7 @@ func main() {
n.tun.SetupAdminHandlers(n.admin)
}
}

//Windows service shutdown
minwinsvc.SetOnExit(func() {
logger.Infof("Shutting down service ...")
Expand Down
37 changes: 37 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"io"
"math/big"
"os"
"runtime"
"strings"
"time"

"github.com/hjson/hjson-go/v4"
Expand Down Expand Up @@ -208,6 +210,41 @@ func (cfg *NodeConfig) NewPrivateKey() {
cfg.PrivateKey = KeyBytes(spriv)
}

func NewSecureKeyPair(bits int) (priv ed25519.PrivateKey, pub ed25519.PublicKey) {
// Generates a key pair with a prescribed number of security bits.
threads := runtime.GOMAXPROCS(0)
if (bits > 64) {
bits = 64
// Bounding the maximum number of security bits to the maximum public key length of 64.
}
type keySet struct {
priv ed25519.PrivateKey
pub ed25519.PublicKey
}
expected := strings.Repeat("0", bits)
// Generates the expected security substring in advance
newKeys := make(chan keySet, threads)
for i := 0; i < threads; i++ {
go func(out chan<- keySet) {
for {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
}
if !(hex.EncodeToString(pub)[0:bits] == expected) {
// Checks if the public key contains the expected security substring
continue
}
out <- keySet{priv, pub}
}
}(newKeys)
}
for {
newKey := <-newKeys
return newKey.priv, newKey.pub
}
}

func (cfg *NodeConfig) MarshalPEMPrivateKey() ([]byte, error) {
b, err := x509.MarshalPKCS8PrivateKey(ed25519.PrivateKey(cfg.PrivateKey))
if err != nil {
Expand Down
Loading