diff options
author | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-04-13 16:10:43 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-04-13 16:10:43 +0200 |
commit | 8fee6561fbc21bd44f67dff9f365aa14da293d99 (patch) | |
tree | e1f27b545439a7cb47254342cfbff6121bf41f6b | |
parent | 8a4f764a7304ecbff07d451a21926bdc1a7fb3d4 (diff) |
clean-up sigsum-debug structure
-rw-r--r-- | cmd/sigsum-debug/genkey/genkey.go (renamed from cmd/sigsum-debug/genkey.go) | 6 | ||||
-rw-r--r-- | cmd/sigsum-debug/hashkey/hashkey.go (renamed from cmd/sigsum-debug/hashkey.go) | 11 | ||||
-rw-r--r-- | cmd/sigsum-debug/hashleaf/hashleaf.go (renamed from cmd/sigsum-debug/hashleaf.go) | 15 | ||||
-rw-r--r-- | cmd/sigsum-debug/main.go | 70 | ||||
-rw-r--r-- | cmd/sigsum-debug/pubkey/pubkey.go (renamed from cmd/sigsum-debug/pubkey.go) | 11 | ||||
-rw-r--r-- | cmd/sigsum-debug/sign/sign.go (renamed from cmd/sigsum-debug/sign.go) | 11 | ||||
-rw-r--r-- | cmd/sigsum-debug/util.go | 43 |
7 files changed, 58 insertions, 109 deletions
diff --git a/cmd/sigsum-debug/genkey.go b/cmd/sigsum-debug/genkey/genkey.go index 71eb17d..5f71e41 100644 --- a/cmd/sigsum-debug/genkey.go +++ b/cmd/sigsum-debug/genkey/genkey.go @@ -1,4 +1,4 @@ -package main +package genkey import ( "crypto/ed25519" @@ -8,10 +8,10 @@ import ( "git.sigsum.org/sigsum-go/pkg/hex" ) -func CmdGenKey() error { +func Main(_ []string) error { _, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { - return fmt.Errorf("failed generating key-pair: %v", err) + return fmt.Errorf("genkey: %v", err) } fmt.Printf("%s\n", hex.Serialize(priv[:])) diff --git a/cmd/sigsum-debug/hashkey.go b/cmd/sigsum-debug/hashkey/hashkey.go index 6163a1e..f195239 100644 --- a/cmd/sigsum-debug/hashkey.go +++ b/cmd/sigsum-debug/hashkey/hashkey.go @@ -1,4 +1,4 @@ -package main +package hashkey import ( "crypto/ed25519" @@ -6,15 +6,16 @@ import ( "git.sigsum.org/sigsum-go/pkg/hex" "git.sigsum.org/sigsum-go/pkg/types" + "git.sigsum.org/sigsum-tools-go/internal/util" ) -func CmdHashKey() error { - b, err := decodeHexFromStdin() +func Main(_ []string) error { + b, err := util.HexFromStdin() if err != nil { - return err + return fmt.Errorf("hashkey: %v", err) } if len(b) != ed25519.PublicKeySize { - return fmt.Errorf("invalid private key: size") + return fmt.Errorf("hashkey: invalid private key size %d", len(b)) } pub := ed25519.PublicKey(b) keyHash := types.HashFn(pub) diff --git a/cmd/sigsum-debug/hashleaf.go b/cmd/sigsum-debug/hashleaf/hashleaf.go index 368e14b..00f8bbb 100644 --- a/cmd/sigsum-debug/hashleaf.go +++ b/cmd/sigsum-debug/hashleaf/hashleaf.go @@ -1,25 +1,26 @@ -package main +package hashleaf import ( - "fmt" "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 CmdHashLeaf(optPriv string, optShardHint uint64) error { - data, err := readStdin() +func Main(_ []string, optPriv string, optShardHint uint64) error { + data, err := util.BytesFromStdin() if err != nil { return fmt.Errorf("sign: %v", err) } - priv, err := privFromHex(optPriv) + priv, err := util.SignerFromHex(optPriv) if err != nil { return fmt.Errorf("sign: %v", err) } stm := types.Statement{ ShardHint: optShardHint, - Checksum: *types.HashFn(data), + Checksum: *types.HashFn(data), } sig, err := stm.Sign(priv) if err != nil { @@ -28,7 +29,7 @@ func CmdHashLeaf(optPriv string, optShardHint uint64) error { leaf := types.Leaf{ Statement: stm, Signature: *sig, - KeyHash: *types.HashFn(priv.Public().(ed25519.PublicKey)[:]), + KeyHash: *types.HashFn(priv.Public().(ed25519.PublicKey)[:]), } lh := types.LeafHash(leaf.ToBinary()) 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/pubkey.go index f74992a..8f3b467 100644 --- a/cmd/sigsum-debug/pubkey.go +++ b/cmd/sigsum-debug/pubkey/pubkey.go @@ -1,25 +1,26 @@ -package main +package pubkey import ( "crypto/ed25519" "fmt" "git.sigsum.org/sigsum-go/pkg/hex" + "git.sigsum.org/sigsum-tools-go/internal/util" ) -func CmdPubKey() error { - b, err := decodeHexFromStdin() +func Main(_ []string) error { + b, err := util.HexFromStdin() if err != nil { return err } if len(b) != ed25519.PrivateKeySize { - return fmt.Errorf("invalid private key: size") + 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("failed converting to public key") + return fmt.Errorf("pubkey: must parse as ed25519") } fmt.Printf("%s\n", hex.Serialize(pub[:])) diff --git a/cmd/sigsum-debug/sign.go b/cmd/sigsum-debug/sign/sign.go index 61d6d13..ac21472 100644 --- a/cmd/sigsum-debug/sign.go +++ b/cmd/sigsum-debug/sign/sign.go @@ -1,24 +1,25 @@ -package main +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 CmdSign(optPriv string, optShardHint uint64) error { - data, err := readStdin() +func Main(_ []string, optPriv string, optShardHint uint64) error { + data, err := util.BytesFromStdin() if err != nil { return fmt.Errorf("sign: %v", err) } - priv, err := privFromHex(optPriv) + priv, err := util.SignerFromHex(optPriv) if err != nil { return fmt.Errorf("sign: %v", err) } stm := types.Statement{ ShardHint: optShardHint, - Checksum: *types.HashFn(data), + Checksum: *types.HashFn(data), } sig, err := stm.Sign(priv) if err != 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 -} |