2017-12-29 04:16:20 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
This file generates crypto keys.
|
|
|
|
It prints out a new set of keys each time if finds a "better" one.
|
|
|
|
By default, "better" means a higher NodeID (-> higher IP address).
|
|
|
|
This is because the IP address format can compress leading 1s in the address, to incrase the number of ID bits in the address.
|
|
|
|
|
|
|
|
If run with the "-sig" flag, it generates signing keys instead.
|
|
|
|
A "better" signing key means one with a higher TreeID.
|
|
|
|
This only matters if it's high enough to make you the root of the tree.
|
|
|
|
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "encoding/hex"
|
|
|
|
import "flag"
|
|
|
|
import "fmt"
|
2018-02-21 15:57:03 +00:00
|
|
|
import "runtime"
|
2017-12-29 04:16:20 +00:00
|
|
|
import . "yggdrasil"
|
|
|
|
|
|
|
|
var doSig = flag.Bool("sig", false, "generate new signing keys instead")
|
|
|
|
|
2018-02-21 15:57:03 +00:00
|
|
|
type keySet struct {
|
|
|
|
priv []byte
|
|
|
|
pub []byte
|
|
|
|
id []byte
|
|
|
|
ip string
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:16:20 +00:00
|
|
|
func main() {
|
2018-02-21 15:57:03 +00:00
|
|
|
threads := runtime.GOMAXPROCS(0)
|
|
|
|
var threadChannels []chan []byte
|
|
|
|
var currentBest []byte
|
|
|
|
newKeys := make(chan keySet, threads)
|
2018-01-04 22:37:51 +00:00
|
|
|
flag.Parse()
|
2018-02-21 15:57:03 +00:00
|
|
|
|
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
threadChannels = append(threadChannels, make(chan []byte, threads))
|
|
|
|
switch {
|
|
|
|
case *doSig:
|
|
|
|
go doSigKeys(newKeys, threadChannels[i])
|
|
|
|
default:
|
|
|
|
go doBoxKeys(newKeys, threadChannels[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
newKey := <-newKeys
|
|
|
|
if isBetter(currentBest[:], newKey.id[:]) || len(currentBest) == 0 {
|
|
|
|
currentBest = newKey.id
|
|
|
|
for _, channel := range threadChannels {
|
|
|
|
select {
|
|
|
|
case channel <- newKey.id:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("--------------------------------------------------------------------------------")
|
|
|
|
switch {
|
|
|
|
case *doSig:
|
|
|
|
fmt.Println("sigPriv:", hex.EncodeToString(newKey.priv[:]))
|
|
|
|
fmt.Println("sigPub:", hex.EncodeToString(newKey.pub[:]))
|
|
|
|
fmt.Println("TreeID:", hex.EncodeToString(newKey.id[:]))
|
|
|
|
default:
|
|
|
|
fmt.Println("boxPriv:", hex.EncodeToString(newKey.priv[:]))
|
|
|
|
fmt.Println("boxPub:", hex.EncodeToString(newKey.pub[:]))
|
|
|
|
fmt.Println("NodeID:", hex.EncodeToString(newKey.id[:]))
|
|
|
|
fmt.Println("IP:", newKey.ip)
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isBetter(oldID, newID []byte) bool {
|
2018-01-04 22:37:51 +00:00
|
|
|
for idx := range oldID {
|
|
|
|
if newID[idx] > oldID[idx] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if newID[idx] < oldID[idx] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 15:57:03 +00:00
|
|
|
func doBoxKeys(out chan<- keySet, in <-chan []byte) {
|
2018-01-04 22:37:51 +00:00
|
|
|
c := Core{}
|
|
|
|
pub, _ := c.DEBUG_newBoxKeys()
|
|
|
|
bestID := c.DEBUG_getNodeID(pub)
|
|
|
|
for idx := range bestID {
|
|
|
|
bestID[idx] = 0
|
|
|
|
}
|
|
|
|
for {
|
2018-02-21 15:57:03 +00:00
|
|
|
select {
|
|
|
|
case newBestID := <-in:
|
|
|
|
if isBetter(bestID[:], newBestID) {
|
|
|
|
copy(bestID[:], newBestID)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
pub, priv := c.DEBUG_newBoxKeys()
|
|
|
|
id := c.DEBUG_getNodeID(pub)
|
|
|
|
if !isBetter(bestID[:], id[:]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bestID = id
|
|
|
|
ip := c.DEBUG_addrForNodeID(id)
|
|
|
|
out <- keySet{priv[:], pub[:], id[:], ip}
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 15:57:03 +00:00
|
|
|
func doSigKeys(out chan<- keySet, in <-chan []byte) {
|
2018-01-04 22:37:51 +00:00
|
|
|
c := Core{}
|
|
|
|
pub, _ := c.DEBUG_newSigKeys()
|
|
|
|
bestID := c.DEBUG_getTreeID(pub)
|
|
|
|
for idx := range bestID {
|
|
|
|
bestID[idx] = 0
|
|
|
|
}
|
|
|
|
for {
|
2018-02-21 15:57:03 +00:00
|
|
|
select {
|
|
|
|
case newBestID := <-in:
|
|
|
|
if isBetter(bestID[:], newBestID) {
|
|
|
|
copy(bestID[:], newBestID)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
pub, priv := c.DEBUG_newSigKeys()
|
|
|
|
id := c.DEBUG_getTreeID(pub)
|
|
|
|
if !isBetter(bestID[:], id[:]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bestID = id
|
2018-02-21 15:57:03 +00:00
|
|
|
out <- keySet{priv[:], pub[:], id[:], ""}
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|