-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.go
111 lines (89 loc) · 2.19 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/bwesterb/go-xmssmt"
"github.com/urfave/cli"
)
func cmdGenerate(c *cli.Context) error {
var err error
params, err := xmssmt.ParamsFromName2(c.String("alg"))
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"There is no XMSS[MT] instance %s: %v", c.String("alg"), err), 1)
}
if c.NArg() != 0 {
return cli.NewExitError("I don't expect arguments; only flags", 10)
}
if c.IsSet("n") {
params.N = uint32(c.Int("n"))
}
if c.IsSet("w") {
params.WotsW = uint16(c.Int("w"))
}
if c.IsSet("d") {
params.D = uint32(c.Int("d"))
}
if c.IsSet("full-height") {
params.FullHeight = uint32(c.Int("full-height"))
}
if c.IsSet("hash") {
switch c.String("hash") {
case "sha2":
params.Func = xmssmt.SHA2
case "shake":
params.Func = xmssmt.SHAKE
case "shake256":
params.Func = xmssmt.SHAKE256
default:
return cli.NewExitError(fmt.Sprintf(
"The hash function %s is not supported", c.String("hash")), 2)
}
}
if c.IsSet("prf") {
switch c.String("prf") {
case "rfc":
params.Prf = xmssmt.RFC
case "nist":
params.Prf = xmssmt.NIST
default:
return cli.NewExitError(fmt.Sprintf(
"The PRF %s is not supported", c.String("prf")), 23)
}
}
ctx, err := xmssmt.NewContext(*params)
if err != nil {
return cli.NewExitError(err, 3)
}
if !c.Bool("force") {
if _, err = os.Stat(c.String("privkey")); !os.IsNotExist(err) {
return cli.NewExitError(fmt.Sprintf(
"%s: already exists", c.String("privkey")), 8)
}
if _, err = os.Stat(c.String("pubkey")); !os.IsNotExist(err) {
return cli.NewExitError(fmt.Sprintf(
"%s: already exists", c.String("pubkey")), 9)
}
}
sk, pk, err := ctx.GenerateKeyPair(c.String("privkey"))
if err != nil {
return cli.NewExitError(err, 4)
}
err = sk.Close()
if err != nil {
return cli.NewExitError(err, 5)
}
pkBytes, err := pk.MarshalBinary()
if err != nil {
return cli.NewExitError(err, 6)
}
pkBase64, _ := pk.MarshalText()
err = ioutil.WriteFile(c.String("pubkey"), pkBytes, 0644)
if err != nil {
return cli.NewExitError(err, 7)
}
fmt.Printf("Successfully generated keypair.\n\n")
fmt.Printf("Public key: %s\n", pkBase64)
return nil
}