Debian Codegraph @

command/crypto/nacl/sign.go

raw symbols SCIP
1 package nacl
2
3 import (
4 "crypto/rand"
5 "fmt"
6 "io/ioutil"
7 "os"
8
9 "github.com/pkg/errors"
10 "github.com/smallstep/cli/command"
11 "github.com/smallstep/cli/errs"
12 "github.com/smallstep/cli/flags"
13 "github.com/smallstep/cli/ui"
14 "github.com/smallstep/cli/utils"
15 "github.com/urfave/cli"
16 "golang.org/x/crypto/nacl/sign"
17 )
18
19 func signCommand() cli.Command {
20 return cli.Command{
21 Name: "sign",
22 Usage: "sign small messages using public-key cryptography",
23 UsageText: "step crypto nacl sign <subcommand> [arguments] [global-flags] [subcommand-flags]",
24 Description: `**step crypto nacl sign** command group uses public-key cryptography to sign and
25 verify messages. The implementation is based on NaCl's crypto_sign function.
26
27 NaCl crypto_sign is crypto_sign_edwards25519sha512batch, a particular
28 combination of Curve25519 in Edwards form and SHA-512 into a signature scheme
29 suitable for high-speed batch verification. This function is conjectured to meet
30 the standard notion of unforgeability under chosen-message attacks.
31
32 These commands are interoperable with NaCl: https://nacl.cr.yp.to/sign.html
33
34 ## EXAMPLES
35
36 Create a keypair for verifying and signing messages:
37 '''
38 $ step crypto nacl sign keypair nacl.sign.pub nacl.sign.priv
39 '''
40
41 Sign a message using the private key:
42 '''
43 $ step crypto nacl sign sign nacl.sign.priv
44 Please enter text to sign: ********
45 rNrOfqsv4svlRnVPSVYe2REXodL78yEMHtNkzAGNp4MgHuVGoyayp0zx4D5rjTzYVVrD2HRP306ZILT62ohvCG1lc3NhZ2U
46
47 $ cat message.txt | step crypto nacl sign sign ~/step/keys/nacl.recipient.sign.priv
48 rNrOfqsv4svlRnVPSVYe2REXodL78yEMHtNkzAGNp4MgHuVGoyayp0zx4D5rjTzYVVrD2HRP306ZILT62ohvCG1lc3NhZ2U
49 '''
50
51 Verify the signed message using the public key:
52 '''
53 $ echo rNrOfqsv4svlRnVPSVYe2REXodL78yEMHtNkzAGNp4MgHuVGoyayp0zx4D5rjTzYVVrD2HRP306ZILT62ohvCG1lc3NhZ2U \
54 | step crypto nacl sign open nacl.sign.pub
55 message
56 '''`,
57 Subcommands: cli.Commands{
58 signKeypairCommand(),
59 signOpenCommand(),
60 signSignCommand(),
61 },
62 }
63 }
64
65 func signKeypairCommand() cli.Command {
66 return cli.Command{
67 Name: "keypair",
68 Action: command.ActionFunc(signKeypairAction),
69 Usage: "generate a pair for use with sign and open",
70 UsageText: "**step crypto nacl sign keypair** <pub-file> <priv-file>",
71 Description: `**step crypto nacl sign keypair** generates a secret key and a corresponding
72 public key valid for verifying and signing messages.
73
74 This command uses an implementation of NaCl's crypto_sign_keypair function.
75
76 For examples, see **step help crypto nacl sign**.`,
77 Flags: []cli.Flag{flags.Force},
78 }
79 }
80
81 func signOpenCommand() cli.Command {
82 return cli.Command{
83 Name: "open",
84 Action: cli.ActionFunc(signOpenAction),
85 Usage: "verify a signed message produced by sign",
86 UsageText: "**step crypto nacl sign open** <pub-file>",
87 Description: `**step crypto nacl sign open** verifies the signature of a message using the
88 signer's public key.
89
90 This command uses an implementation of NaCl's crypto_sign_open function.
91
92 For examples, see **step help crypto nacl sign**.`,
93 Flags: []cli.Flag{
94 cli.BoolFlag{
95 Name: "raw",
96 Usage: "Indicates that input is not base64 encoded",
97 },
98 },
99 }
100 }
101
102 func signSignCommand() cli.Command {
103 return cli.Command{
104 Name: "sign",
105 Action: cli.ActionFunc(signSignAction),
106 Usage: "sign a message using Ed25519",
107 UsageText: "**step crypto nacl sign sign** <priv-file>",
108 Description: `**step crypto nacl sign sign** signs a message m using the signer's private
109 key.
110
111 This command uses an implementation of NaCl's crypto_sign function.
112
113 For examples, see **step help crypto nacl sign**.`,
114 Flags: []cli.Flag{
115 cli.BoolFlag{
116 Name: "raw",
117 Usage: "Do not base64 encode output",
118 },
119 },
120 }
121 }
122
123 func signKeypairAction(ctx *cli.Context) error {
124 if err := errs.NumberOfArguments(ctx, 2); err != nil {
125 return err
126 }
127
128 args := ctx.Args()
129 pubFile, privFile := args[0], args[1]
130 if pubFile == privFile {
131 return errs.EqualArguments(ctx, "<pub-file>", "<priv-file>")
132 }
133
134 pub, priv, err := sign.GenerateKey(rand.Reader)
135 if err != nil {
136 return errors.Wrap(err, "error generating key")
137 }
138
139 if err := utils.WriteFile(pubFile, pub[:], 0600); err != nil {
140 return errs.FileError(err, pubFile)
141 }
142
143 if err := utils.WriteFile(privFile, priv[:], 0600); err != nil {
144 return errs.FileError(err, privFile)
145 }
146
147 ui.Printf("Your public key has been saved in %s.\n", pubFile)
148 ui.Printf("Your private key has been saved in %s.\n", privFile)
149 return nil
150 }
151
152 func signOpenAction(ctx *cli.Context) error {
153 if err := errs.NumberOfArguments(ctx, 1); err != nil {
154 return err
155 }
156
157 pubFile := ctx.Args().Get(0)
158 pub, err := ioutil.ReadFile(pubFile)
159 if err != nil {
160 return errs.FileError(err, pubFile)
161 } else if len(pub) != 32 {
162 return errors.New("invalid public key: key size is not 32 bytes")
163 }
164
165 input, err := utils.ReadInput("Write signed message to open")
166 if err != nil {
167 return errors.Wrap(err, "error reading input")
168 }
169
170 var rawInput []byte
171 if ctx.Bool("raw") {
172 rawInput = input
173 } else {
174 // DecodeLen returns the maximum length,
175 // Decode will return the actual length.
176 rawInput = make([]byte, b64Encoder.DecodedLen(len(input)))
177 n, err := b64Encoder.Decode(rawInput, input)
178 if err != nil {
179 return errors.Wrap(err, "error decoding base64 input")
180 }
181 rawInput = rawInput[:n]
182 }
183
184 var pb [32]byte
185 copy(pb[:], pub)
186
187 raw, ok := sign.Open(nil, rawInput, &pb)
188 if !ok {
189 return errors.New("error authenticating input")
190 }
191
192 os.Stdout.Write(raw)
193 return nil
194 }
195
196 func signSignAction(ctx *cli.Context) error {
197 if err := errs.NumberOfArguments(ctx, 1); err != nil {
198 return err
199 }
200
201 privFile := ctx.Args().Get(0)
202 priv, err := ioutil.ReadFile(privFile)
203 if err != nil {
204 return errs.FileError(err, privFile)
205 } else if len(priv) != 64 {
206 return errors.New("invalid private key: key size is not 64 bytes")
207 }
208
209 input, err := utils.ReadInput("Please enter text to sign")
210 if err != nil {
211 return errors.Wrap(err, "error reading input")
212 }
213
214 var pv [64]byte
215 copy(pv[:], priv)
216
217 raw := sign.Sign(nil, input, &pv)
218 if ctx.Bool("raw") {
219 os.Stdout.Write(raw)
220 } else {
221 fmt.Println(b64Encoder.EncodeToString(raw))
222 }
223
224 return nil
225 }