From 8fee6561fbc21bd44f67dff9f365aa14da293d99 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Wed, 13 Apr 2022 16:10:43 +0200 Subject: clean-up sigsum-debug structure --- cmd/sigsum-debug/genkey.go | 19 ---------- cmd/sigsum-debug/genkey/genkey.go | 19 ++++++++++ cmd/sigsum-debug/hashkey.go | 24 ------------ cmd/sigsum-debug/hashkey/hashkey.go | 25 +++++++++++++ cmd/sigsum-debug/hashleaf.go | 37 ------------------ cmd/sigsum-debug/hashleaf/hashleaf.go | 38 +++++++++++++++++++ cmd/sigsum-debug/main.go | 70 +++++++++++++++-------------------- cmd/sigsum-debug/pubkey.go | 27 -------------- cmd/sigsum-debug/pubkey/pubkey.go | 28 ++++++++++++++ cmd/sigsum-debug/sign.go | 30 --------------- cmd/sigsum-debug/sign/sign.go | 31 ++++++++++++++++ cmd/sigsum-debug/util.go | 43 --------------------- 12 files changed, 170 insertions(+), 221 deletions(-) delete mode 100644 cmd/sigsum-debug/genkey.go create mode 100644 cmd/sigsum-debug/genkey/genkey.go delete mode 100644 cmd/sigsum-debug/hashkey.go create mode 100644 cmd/sigsum-debug/hashkey/hashkey.go delete mode 100644 cmd/sigsum-debug/hashleaf.go create mode 100644 cmd/sigsum-debug/hashleaf/hashleaf.go delete mode 100644 cmd/sigsum-debug/pubkey.go create mode 100644 cmd/sigsum-debug/pubkey/pubkey.go delete mode 100644 cmd/sigsum-debug/sign.go create mode 100644 cmd/sigsum-debug/sign/sign.go delete mode 100644 cmd/sigsum-debug/util.go (limited to 'cmd/sigsum-debug') diff --git a/cmd/sigsum-debug/genkey.go b/cmd/sigsum-debug/genkey.go deleted file mode 100644 index 71eb17d..0000000 --- a/cmd/sigsum-debug/genkey.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "crypto/ed25519" - "crypto/rand" - "fmt" - - "git.sigsum.org/sigsum-go/pkg/hex" -) - -func CmdGenKey() error { - _, priv, err := ed25519.GenerateKey(rand.Reader) - if err != nil { - return fmt.Errorf("failed generating key-pair: %v", err) - } - - fmt.Printf("%s\n", hex.Serialize(priv[:])) - return nil -} diff --git a/cmd/sigsum-debug/genkey/genkey.go b/cmd/sigsum-debug/genkey/genkey.go new file mode 100644 index 0000000..5f71e41 --- /dev/null +++ b/cmd/sigsum-debug/genkey/genkey.go @@ -0,0 +1,19 @@ +package genkey + +import ( + "crypto/ed25519" + "crypto/rand" + "fmt" + + "git.sigsum.org/sigsum-go/pkg/hex" +) + +func Main(_ []string) error { + _, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + return fmt.Errorf("genkey: %v", err) + } + + fmt.Printf("%s\n", hex.Serialize(priv[:])) + return nil +} diff --git a/cmd/sigsum-debug/hashkey.go b/cmd/sigsum-debug/hashkey.go deleted file mode 100644 index 6163a1e..0000000 --- a/cmd/sigsum-debug/hashkey.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "crypto/ed25519" - "fmt" - - "git.sigsum.org/sigsum-go/pkg/hex" - "git.sigsum.org/sigsum-go/pkg/types" -) - -func CmdHashKey() error { - b, err := decodeHexFromStdin() - if err != nil { - return err - } - if len(b) != ed25519.PublicKeySize { - return fmt.Errorf("invalid private key: size") - } - pub := ed25519.PublicKey(b) - keyHash := types.HashFn(pub) - - fmt.Printf("%s\n", hex.Serialize(keyHash[:])) - return nil -} diff --git a/cmd/sigsum-debug/hashkey/hashkey.go b/cmd/sigsum-debug/hashkey/hashkey.go new file mode 100644 index 0000000..f195239 --- /dev/null +++ b/cmd/sigsum-debug/hashkey/hashkey.go @@ -0,0 +1,25 @@ +package hashkey + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-go/pkg/hex" + "git.sigsum.org/sigsum-go/pkg/types" + "git.sigsum.org/sigsum-tools-go/internal/util" +) + +func Main(_ []string) error { + b, err := util.HexFromStdin() + if err != nil { + return fmt.Errorf("hashkey: %v", err) + } + if len(b) != ed25519.PublicKeySize { + return fmt.Errorf("hashkey: invalid private key size %d", len(b)) + } + pub := ed25519.PublicKey(b) + keyHash := types.HashFn(pub) + + fmt.Printf("%s\n", hex.Serialize(keyHash[:])) + return nil +} diff --git a/cmd/sigsum-debug/hashleaf.go b/cmd/sigsum-debug/hashleaf.go deleted file mode 100644 index 368e14b..0000000 --- a/cmd/sigsum-debug/hashleaf.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "fmt" - "crypto/ed25519" - - "git.sigsum.org/sigsum-go/pkg/hex" - "git.sigsum.org/sigsum-go/pkg/types" -) - -func CmdHashLeaf(optPriv string, optShardHint uint64) error { - data, err := readStdin() - if err != nil { - return fmt.Errorf("sign: %v", err) - } - priv, err := privFromHex(optPriv) - if err != nil { - return fmt.Errorf("sign: %v", err) - } - stm := types.Statement{ - ShardHint: optShardHint, - Checksum: *types.HashFn(data), - } - sig, err := stm.Sign(priv) - if err != nil { - fmt.Errorf("sign: %v", err) - } - leaf := types.Leaf{ - Statement: stm, - Signature: *sig, - KeyHash: *types.HashFn(priv.Public().(ed25519.PublicKey)[:]), - } - lh := types.LeafHash(leaf.ToBinary()) - - fmt.Printf("%s\n", hex.Serialize(lh[:])) - return nil -} diff --git a/cmd/sigsum-debug/hashleaf/hashleaf.go b/cmd/sigsum-debug/hashleaf/hashleaf.go new file mode 100644 index 0000000..00f8bbb --- /dev/null +++ b/cmd/sigsum-debug/hashleaf/hashleaf.go @@ -0,0 +1,38 @@ +package hashleaf + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-go/pkg/hex" + "git.sigsum.org/sigsum-go/pkg/types" + "git.sigsum.org/sigsum-tools-go/internal/util" +) + +func Main(_ []string, optPriv string, optShardHint uint64) error { + data, err := util.BytesFromStdin() + if err != nil { + return fmt.Errorf("sign: %v", err) + } + priv, err := util.SignerFromHex(optPriv) + if err != nil { + return fmt.Errorf("sign: %v", err) + } + stm := types.Statement{ + ShardHint: optShardHint, + Checksum: *types.HashFn(data), + } + sig, err := stm.Sign(priv) + if err != nil { + fmt.Errorf("sign: %v", err) + } + leaf := types.Leaf{ + Statement: stm, + Signature: *sig, + KeyHash: *types.HashFn(priv.Public().(ed25519.PublicKey)[:]), + } + lh := types.LeafHash(leaf.ToBinary()) + + fmt.Printf("%s\n", hex.Serialize(lh[:])) + return nil +} diff --git a/cmd/sigsum-debug/main.go b/cmd/sigsum-debug/main.go index 8b49e0b..2bc05de 100644 --- a/cmd/sigsum-debug/main.go +++ b/cmd/sigsum-debug/main.go @@ -1,8 +1,9 @@ // package main provides a tool named sigsum-debug. // -// Install as follows: +// Build as follows: // -// $ go install -ldflags="-X 'main.someVersion=git commit $(git rev-list -1 HEAD)'" +// $ go build -ldflags="-X 'main.someVersion=git commit $(git rev-list -1 HEAD)'" +// $ mv sigsum-debug $GOPATH/bin/ // // Usage: // @@ -15,6 +16,13 @@ import ( "fmt" "log" "os" + + "git.sigsum.org/sigsum-tools-go/cmd/sigsum-debug/genkey" + "git.sigsum.org/sigsum-tools-go/cmd/sigsum-debug/hashkey" + "git.sigsum.org/sigsum-tools-go/cmd/sigsum-debug/hashleaf" + "git.sigsum.org/sigsum-tools-go/cmd/sigsum-debug/pubkey" + "git.sigsum.org/sigsum-tools-go/cmd/sigsum-debug/sign" + "git.sigsum.org/sigsum-tools-go/internal/options" ) const usage = `sigsum-debug version %s @@ -49,29 +57,31 @@ Usage: ` var ( - optPriv, optPub string + optPriv string + optPub string optShardHint uint64 - someVersion = "unknown" + someVersion = "devel" ) func main() { log.SetFlags(0) var err error - switch cmd := parseCommand(); cmd.Name() { + cmd := options.Parse(printUsage, setOptions) + switch cmd.Name() { case "help": cmd.Usage() case "genkey": - err = CmdGenKey() + err = genkey.Main(cmd.Args()) case "pubkey": - err = CmdPubKey() + err = pubkey.Main(cmd.Args()) case "hashkey": - err = CmdHashKey() + err = hashkey.Main(cmd.Args()) case "hashleaf": - err = CmdHashLeaf(optPriv, optShardHint) + err = hashleaf.Main(cmd.Args(), optPriv, optShardHint) case "sign": - err = CmdSign(optPriv, optShardHint) + err = sign.Main(cmd.Args(), optPriv, optShardHint) case "cosign": err = fmt.Errorf("TODO") default: @@ -84,42 +94,20 @@ func main() { } } -func parseCommand() *flag.FlagSet { - args := os.Args - if len(args) < 2 { - args = append(args, "") - } - - fs := flag.NewFlagSet(args[1], flag.ExitOnError) - fs.Usage = func() { - log.Printf(usage, someVersion) - } - registerOptions(fs) - fs.Parse(args[2:]) - return fs +func printUsage() { + log.Printf(usage, someVersion) } -func registerOptions(fs *flag.FlagSet) { +func setOptions(fs *flag.FlagSet) { switch cmd := fs.Name(); cmd { - default: case "hashleaf": - registerStringOption(fs, &optPriv, "k", "key", "") - registerUint64Option(fs, &optShardHint, "s", "shard-hint", 0) + options.AddString(fs, &optPriv, "k", "key", "") + options.AddUint64(fs, &optShardHint, "s", "shard-hint", 0) case "sign": - registerStringOption(fs, &optPriv, "k", "key", "") - registerUint64Option(fs, &optShardHint, "s", "shard-hint", 0) + options.AddString(fs, &optPriv, "k", "key", "") + options.AddUint64(fs, &optShardHint, "s", "shard-hint", 0) case "cosign": - registerStringOption(fs, &optPriv, "w", "--witness-priv", "") - registerStringOption(fs, &optPub, "l", "--log-pub", "") + options.AddString(fs, &optPriv, "w", "--witness-priv", "") + options.AddString(fs, &optPub, "l", "--log-pub", "") } } - -func registerStringOption(fs *flag.FlagSet, opt *string, short, long, value string) { - fs.StringVar(opt, short, value, "") - fs.StringVar(opt, long, value, "") -} - -func registerUint64Option(fs *flag.FlagSet, opt *uint64, short, long string, value uint64) { - fs.Uint64Var(opt, short, value, "") - fs.Uint64Var(opt, long, value, "") -} diff --git a/cmd/sigsum-debug/pubkey.go b/cmd/sigsum-debug/pubkey.go deleted file mode 100644 index f74992a..0000000 --- a/cmd/sigsum-debug/pubkey.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "crypto/ed25519" - "fmt" - - "git.sigsum.org/sigsum-go/pkg/hex" -) - -func CmdPubKey() error { - b, err := decodeHexFromStdin() - if err != nil { - return err - } - if len(b) != ed25519.PrivateKeySize { - return fmt.Errorf("invalid private key: size") - } - - priv := ed25519.PrivateKey(b) - pub, ok := priv.Public().(ed25519.PublicKey) - if !ok { - return fmt.Errorf("failed converting to public key") - } - - fmt.Printf("%s\n", hex.Serialize(pub[:])) - return nil -} diff --git a/cmd/sigsum-debug/pubkey/pubkey.go b/cmd/sigsum-debug/pubkey/pubkey.go new file mode 100644 index 0000000..8f3b467 --- /dev/null +++ b/cmd/sigsum-debug/pubkey/pubkey.go @@ -0,0 +1,28 @@ +package pubkey + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-go/pkg/hex" + "git.sigsum.org/sigsum-tools-go/internal/util" +) + +func Main(_ []string) error { + b, err := util.HexFromStdin() + if err != nil { + return err + } + if len(b) != ed25519.PrivateKeySize { + return fmt.Errorf("pubkey: invalid key size %d", len(b)) + } + + priv := ed25519.PrivateKey(b) + pub, ok := priv.Public().(ed25519.PublicKey) + if !ok { + return fmt.Errorf("pubkey: must parse as ed25519") + } + + fmt.Printf("%s\n", hex.Serialize(pub[:])) + return nil +} diff --git a/cmd/sigsum-debug/sign.go b/cmd/sigsum-debug/sign.go deleted file mode 100644 index 61d6d13..0000000 --- a/cmd/sigsum-debug/sign.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "fmt" - - "git.sigsum.org/sigsum-go/pkg/hex" - "git.sigsum.org/sigsum-go/pkg/types" -) - -func CmdSign(optPriv string, optShardHint uint64) error { - data, err := readStdin() - if err != nil { - return fmt.Errorf("sign: %v", err) - } - priv, err := privFromHex(optPriv) - if err != nil { - return fmt.Errorf("sign: %v", err) - } - stm := types.Statement{ - ShardHint: optShardHint, - Checksum: *types.HashFn(data), - } - sig, err := stm.Sign(priv) - if err != nil { - fmt.Errorf("sign: %v", err) - } - - fmt.Printf("%s\n", hex.Serialize(sig[:])) - return nil -} diff --git a/cmd/sigsum-debug/sign/sign.go b/cmd/sigsum-debug/sign/sign.go new file mode 100644 index 0000000..ac21472 --- /dev/null +++ b/cmd/sigsum-debug/sign/sign.go @@ -0,0 +1,31 @@ +package sign + +import ( + "fmt" + + "git.sigsum.org/sigsum-go/pkg/hex" + "git.sigsum.org/sigsum-go/pkg/types" + "git.sigsum.org/sigsum-tools-go/internal/util" +) + +func Main(_ []string, optPriv string, optShardHint uint64) error { + data, err := util.BytesFromStdin() + if err != nil { + return fmt.Errorf("sign: %v", err) + } + priv, err := util.SignerFromHex(optPriv) + if err != nil { + return fmt.Errorf("sign: %v", err) + } + stm := types.Statement{ + ShardHint: optShardHint, + Checksum: *types.HashFn(data), + } + sig, err := stm.Sign(priv) + if err != nil { + fmt.Errorf("sign: %v", err) + } + + fmt.Printf("%s\n", hex.Serialize(sig[:])) + return nil +} diff --git a/cmd/sigsum-debug/util.go b/cmd/sigsum-debug/util.go deleted file mode 100644 index f2778b0..0000000 --- a/cmd/sigsum-debug/util.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "crypto" - "bytes" - "fmt" - "io/ioutil" - "os" - "crypto/ed25519" - - "git.sigsum.org/sigsum-go/pkg/hex" -) - -func readStdin() ([]byte, error) { - b, err := ioutil.ReadAll(os.Stdin) - if err != nil { - return nil, fmt.Errorf("stdin: %v", err) - } - return b, nil -} - -func decodeHexFromStdin() ([]byte, error) { - b, err := readStdin() - if err != nil { - return nil, fmt.Errorf("failed reading stdin: %v", err) - } - b, err = hex.Deserialize(string(bytes.TrimSpace(b))) - if err != nil { - return nil, fmt.Errorf("invalid private key: %v", err) - } - return b, nil -} - -func privFromHex(s string) (crypto.Signer, error) { - b, err := hex.Deserialize(optPriv) - if err != nil { - return nil, fmt.Errorf("invalid private key: %v", err) - } - if len(b) != ed25519.PrivateKeySize { - return nil, fmt.Errorf("invalid private key: size") - } - return ed25519.PrivateKey(b), nil -} -- cgit v1.2.3