Debian Codegraph @

crypto/kdf/kdf.go

raw symbols SCIP
1 package kdf
2
3 import (
4 "crypto/subtle"
5 "strconv"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/cli/crypto/randutil"
9
10 "golang.org/x/crypto/argon2"
11 "golang.org/x/crypto/bcrypt"
12 "golang.org/x/crypto/scrypt"
13 )
14
15 // KDF is the type that all the key derivation functions implements. The
16 // current methods uses safe default values, but future improvements can add
17 // functional options to be able to use custom settings.
18 type KDF func(password []byte) (string, error)
19
20 // Scrypt uses scrypt-32768 to derive the given password. Returns the hash
21 // using the PHC string format.
22 func Scrypt(password []byte) (string, error) {
23 salt, err := randutil.Salt(16)
24 if err != nil {
25 return "", err
26 }
27
28 // use scrypt-32768 by default
29 p := scryptParams[scryptHash32768]
30 hash, err := scrypt.Key(password, salt, p.N, p.r, p.p, p.kl)
31 if err != nil {
32 return "", errors.Wrap(err, "error deriving password")
33 }
34
35 return phcEncode("scrypt", p.getParams(), salt, hash), nil
36 }
37
38 // Bcrypt uses bcrypt to derive the given password. Returns the hash
39 // using the Modular Crypt Format standard for bcrypt implementations.
40 func Bcrypt(password []byte) (string, error) {
41 hash, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
42 if err != nil {
43 return "", errors.Wrap(err, "error deriving password")
44
45 }
46 return string(hash), nil
47 }
48
49 // Argon2i uses Argon2i variant to derive the given password. Returns the hash
50 // using the PHC string format.
51 //
52 // Argon2i is optimized to resist side-channel attacks.
53 func Argon2i(password []byte) (string, error) {
54 salt, err := randutil.Salt(16)
55 if err != nil {
56 return "", err
57 }
58
59 p := argon2Params[argon2iHash]
60 hash := argon2.Key(password, salt, p.t, p.m, p.p, p.kl)
61 identifier := "argon2i$v=" + strconv.Itoa(argon2.Version)
62 return phcEncode(identifier, p.getParams(), salt, hash), nil
63 }
64
65 // Argon2id uses Argon2id variant to derive the given password. Returns the
66 // hash using the PHC string format.
67 //
68 // Argon2id is an hybrid version of Argon2d, that maximizes resistance to GPU
69 // attacks and Argon2i that is optimized to resist side-channel attacks. The
70 // Internet draft (https://tools.ietf.org/html/draft-irtf-cfrg-argon2-03)
71 // recommends using Argon2id.
72 func Argon2id(password []byte) (string, error) {
73 salt, err := randutil.Salt(16)
74 if err != nil {
75 return "", err
76 }
77
78 p := argon2Params[argon2idHash]
79 hash := argon2.IDKey(password, salt, p.t, p.m, p.p, p.kl)
80 identifier := "argon2id$v=" + strconv.Itoa(argon2.Version)
81 return phcEncode(identifier, p.getParams(), salt, hash), nil
82 }
83
84 // Compare compares the password with the given PHC encoded hash, returns true
85 // if they match. The time taken is a function of the length of the slices and
86 // is independent of the contents.
87 func Compare(password, phc []byte) (bool, error) {
88 id, version, params, salt, hash, err := phcDecode(string(phc))
89 if err != nil {
90 return false, errors.Wrap(err, "error decoding hash")
91 }
92
93 var hashedPass []byte
94 switch id {
95 case bcryptHash:
96 return (bcrypt.CompareHashAndPassword(hash, password) == nil), nil
97 case scryptHash:
98 p, err := newScryptParams(params)
99 if err != nil {
100 return false, err
101 }
102 hashedPass, err = scrypt.Key(password, salt, p.N, p.r, p.p, len(hash))
103 if err != nil {
104 return false, errors.Wrap(err, "error deriving input")
105 }
106 case argon2iHash:
107 p, err := newArgon2Params(params)
108 if err != nil {
109 return false, err
110 }
111 if version != 0 && version != argon2.Version {
112 return false, errors.Errorf("unsupported argon2 version '%d'", version)
113 }
114 hashedPass = argon2.Key(password, salt, p.t, p.m, p.p, uint32(len(hash)))
115 case argon2idHash:
116 p, err := newArgon2Params(params)
117 if err != nil {
118 return false, err
119 }
120 if version != 0 && version != argon2.Version {
121 return false, errors.Errorf("unsupported argon2 version '%d'", version)
122 }
123 hashedPass = argon2.IDKey(password, salt, p.t, p.m, p.p, uint32(len(hash)))
124 default:
125 return false, errors.Errorf("invalid or unsupported hash method with id '%s'", id)
126 }
127
128 return (subtle.ConstantTimeCompare(hash, hashedPass) == 1), nil
129 }
130
131 // CompareString compares the given password with the given PHC encoded hash,
132 // returns true if they match. The time taken is a function of the length of
133 // the slices and is independent of the contents.
134 func CompareString(password, phc string) (bool, error) {
135 return Compare([]byte(password), []byte(phc))
136 }