Debian Codegraph @

command/crypto/kdf/kdf.go

raw symbols SCIP
1 package kdf
2
3 import (
4 "fmt"
5
6 "github.com/smallstep/cli/crypto/kdf"
7
8 "github.com/pkg/errors"
9 "github.com/smallstep/cli/errs"
10 "github.com/smallstep/cli/utils"
11 "github.com/urfave/cli"
12 )
13
14 // Command returns the cli.Command for kdf and related subcommands.
15 func Command() cli.Command {
16 return cli.Command{
17 Name: "kdf",
18 Usage: "key derivation functions for password hashing and verification",
19 UsageText: "step crypto kdf <subcommand> [arguments] [global-flags] [subcommand-flags]",
20 Description: `**step crypto kdf** command group creates and verifies passwords using key
21 derivation functions.
22
23 ## EXAMPLES
24
25 Derive a password using **scrypt**:
26 '''
27 $ step crypto kdf hash
28 Enter password to hash: ********
29 $scrypt$ln=15,r=8,p=1$3TCG+xs8HWSIHonnqTp6Xg$UI8CYfz6koUaRMjDWEFgujIxM63fYnAcc0HhpUryFn8
30
31 $ step crypto kdf hash --insecure password
32 $scrypt$ln=15,r=8,p=1$U8Fl1sO6LWkFeXs5GQS0vA$Rj8nPeaBFQUzbU21N+hhm3I/s1WTxao7Dje4G6ZvO9Q
33 '''
34
35 Derive a password using **bcrypt**:
36 '''
37 $ step crypto kdf hash --alg bcrypt
38 Enter password to hash: ********
39 $2a$10$EgTYeokp/EhvlMpaDYX56O67M/Ve4JyTl9DHwailYYFOBT3COSTuy
40
41 $ step crypto kdf hash --alg bcrypt --insecure password
42 $2a$10$kgYs5dEKs2C6Y5PXnU7eTuPzHMeSoCnkvtTL7ghsPDdSSmw5ec/sS
43 '''
44
45 Derive a password using **argon2i**:
46 '''
47 $ step crypto kdf hash --alg argon2i
48 Enter password to hash: ********
49 $argon2i$v=19$m=32768,t=3,p=4$H0IxAhFFO7fOu5K2RYTxxA$ULEz/79vh3BtCcm7W/zRfJSpiEGULirrZ+PnHfspWKA
50 '''
51
52 Derive a password using **argon2id**:
53 '''
54 $ step crypto kdf hash --alg argon2id
55 Enter password to hash: ********
56 $argon2id$v=19$m=65536,t=1,p=4$HDi5gI15NwJrKveh2AAa9Q$30haKRwwUe5I4WfkPZPGmhJKTRTO+98x+sVnHhOHdK8
57 '''
58
59 Validate a hash:
60 '''
61 $ step crypto kdf compare '$scrypt$ln=15,r=8,p=1$3TCG+xs8HWSIHonnqTp6Xg$UI8CYfz6koUaRMjDWEFgujIxM63fYnAcc0HhpUryFn8'
62 Enter password to compare: ********
63 ok
64
65 $ step crypto kdf compare --insecure '$scrypt$ln=15,r=8,p=1$3TCG+xs8HWSIHonnqTp6Xg$UI8CYfz6koUaRMjDWEFgujIxM63fYnAcc0HhpUryFn8' password
66 ok
67
68 $ step crypto kdf compare '$2a$10$EgTYeokp/EhvlMpaDYX56O67M/Ve4JyTl9DHwailYYFOBT3COSTuy'
69 Enter password to compare: ********
70 ok
71
72 $ step crypto kdf compare --insecure '$2a$10$EgTYeokp/EhvlMpaDYX56O67M/Ve4JyTl9DHwailYYFOBT3COSTuy' password
73 ok
74
75 $ step crypto kdf compare '$argon2i$v=19$m=32768,t=3,p=4$H0IxAhFFO7fOu5K2RYTxxA$ULEz/79vh3BtCcm7W/zRfJSpiEGULirrZ+PnHfspWKA'
76 Enter password to compare: ********
77 ok
78
79 $ step crypto kdf compare --insecure '$argon2i$v=19$m=32768,t=3,p=4$H0IxAhFFO7fOu5K2RYTxxA$ULEz/79vh3BtCcm7W/zRfJSpiEGULirrZ+PnHfspWKA' password
80 ok
81
82 $ step crypto kdf compare --insecure '$argon2id$v=19$m=65536,t=1,p=4$HDi5gI15NwJrKveh2AAa9Q$30haKRwwUe5I4WfkPZPGmhJKTRTO+98x+sVnHhOHdK8'
83 Enter password to compare: ********
84 ok
85
86 $ step crypto kdf compare --insecure '$argon2id$v=19$m=65536,t=1,p=4$HDi5gI15NwJrKveh2AAa9Q$30haKRwwUe5I4WfkPZPGmhJKTRTO+98x+sVnHhOHdK8' password
87 ok
88 '''`,
89 Subcommands: cli.Commands{
90 hashCommand(),
91 compareCommand(),
92 },
93 }
94 }
95
96 func hashCommand() cli.Command {
97 return cli.Command{
98 Name: "hash",
99 Action: cli.ActionFunc(hashAction),
100 Usage: "derive a secret key from a secret value (e.g., a password)",
101 UsageText: `**step crypto kdf hash** [<input>]
102 [--alg ALGORITHM]`,
103 Description: `**step crypto kdf hash** uses a key derivation function (KDF) to produce a
104 pseudorandom secret key based on some (presumably secret) input value. This is
105 useful for password verification approaches based on password hashing. Key
106 derivation functions are designed to be computationally intensive, making it
107 more difficult for attackers to perform brute-force attacks on password
108 databases.
109
110 If this command is run without the optional <input> argument and STDIN is a TTY
111 (i.e., you're running the command in an interactive terminal and not piping
112 input to it) you'll be prompted to enter a value on STDERR. If STDIN is not a
113 TTY it will be read without prompting.
114
115 This command will produce a string encoding of the KDF output along with the
116 algorithm used, salt, and any parameters required for validation in PHC string
117 format.
118
119 The KDFs are run with parameters that are considered safe. The 'scrypt'
120 parameters are currently fixed at N=32768, r=8 and p=1. The 'bcrypt' work
121 factor is currently fixed at 10.
122
123 For examples, see **step help crypto kdf**.
124
125 ## POSITIONAL ARGUMENTS
126
127 <input>
128 : The input to the key derivation function. <input> is optional and its use is
129 not recommended. If this argument is provided the **--insecure** flag must also
130 be provided because your (presumably secret) <input> will likely be logged and
131 appear in places you might not expect. If omitted input is read from STDIN.`,
132 Flags: []cli.Flag{
133 cli.StringFlag{
134 Name: "alg",
135 Value: "scrypt",
136 Usage: `The KDF algorithm to use.
137
138 : <algorithm> must be one of:
139
140 **scrypt**
141 : A password-based KDF designed to use exponential time and memory.
142
143 **bcrypt**
144 : A password-based KDF designed to use exponential time.
145
146 **argon2i**
147 : A password-based KDF optimized to resist side-channel attacks.
148
149 **argon2id**
150 : A password-based KDF optimized to resist GPU and side-channel attacks.
151 `,
152 },
153 cli.BoolFlag{
154 Name: "insecure",
155 Hidden: true,
156 },
157 },
158 }
159 }
160
161 func hashAction(ctx *cli.Context) error {
162 var err error
163 var input []byte
164
165 // Get kdf method
166 var f kdf.KDF
167 switch alg := ctx.String("alg"); alg {
168 case "scrypt":
169 f = kdf.Scrypt
170 case "bcrypt":
171 f = kdf.Bcrypt
172 case "argon2i":
173 f = kdf.Argon2i
174 case "argon2id":
175 f = kdf.Argon2id
176 default:
177 return errs.InvalidFlagValue(ctx, "alg", alg, "")
178 }
179
180 // Grab input from terminal or arguments
181 switch ctx.NArg() {
182 case 0:
183 input, err = utils.ReadInput("Please enter the password to hash")
184 if err != nil {
185 return err
186 }
187 case 1:
188 if !ctx.Bool("insecure") {
189 return errs.InsecureArgument(ctx, "INPUT")
190 }
191 input = []byte(ctx.Args().Get(0))
192 default:
193 return errs.TooManyArguments(ctx)
194 }
195
196 // Hash input
197 hash, err := f(input)
198 if err != nil {
199 return err
200 }
201
202 fmt.Println(hash)
203 return nil
204 }
205
206 func compareCommand() cli.Command {
207 return cli.Command{
208 Name: "compare",
209 Action: cli.ActionFunc(compareAction),
210 Usage: "compare a plaintext value (e.g., a password) and a hash",
211 UsageText: "step crypto kdf compare <phc-hash> [<input>]",
212 Description: `The 'step crypto kdf compare' command compares a plaintext value (e.g., a
213 password) with an existing KDF password hash in PHC string format. The PHC
214 string input indicates which KDF algorithm and parameters to use.
215
216 If the input matches <phc-hash> the command prints a human readable message
217 indicating success to STDERR and returns 0. If the input does not match an
218 error will be printed to STDERR and the command will exit with a non-zero
219 return code.
220
221 If this command is run without the optional <input> argument and STDIN is a
222 TTY (i.e., you're running the command in an interactive terminal and not
223 piping input to it) you'll be prompted to enter a value on STDERR. If STDIN is
224 not a TTY it will be read without prompting.
225
226 For examples, see **step help crypto kdf**.
227
228 POSITIONAL ARGUMENTS
229
230 <phc-hash>
231 : The KDF password hash in PHC string format.
232
233 <input>
234 : The plaintext value to compare with <phc-hash>. <input> is optional and its
235 use is not recommended. If this argument is provided the **--insecure** flag
236 must also be provided because your (presumably secret) <input> will likely be
237 logged and appear in places you might not expect. If omitted input is read
238 from STDIN.`,
239 Flags: []cli.Flag{
240 cli.BoolFlag{
241 Name: "insecure",
242 Hidden: true,
243 },
244 },
245 }
246 }
247
248 func compareAction(ctx *cli.Context) error {
249 var err error
250 var input, hash []byte
251
252 switch ctx.NArg() {
253 case 0:
254 return errs.MissingArguments(ctx, "PHC_HASH")
255 case 1:
256 hash = []byte(ctx.Args().Get(0))
257 input, err = utils.ReadInput("Please enter the password to compare")
258 if err != nil {
259 return err
260 }
261 case 2:
262 if !ctx.Bool("insecure") {
263 return errs.InsecureArgument(ctx, "INPUT")
264 }
265 args := ctx.Args()
266 hash, input = []byte(args[0]), []byte(args[1])
267 default:
268 return errs.TooManyArguments(ctx)
269 }
270
271 ok, err := kdf.Compare(input, hash)
272 if err != nil {
273 return err
274 }
275
276 if ok {
277 fmt.Println("ok")
278 return nil
279 }
280 return errors.New("fail")
281 }