-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
146 lines (129 loc) · 3.81 KB
/
config.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package passhash
import (
"crypto/rand"
"fmt"
"io"
"reflect"
)
var randReader = rand.Reader
// GetRandReader gets the io.Reader responsible for generating random bytes used by passhash
func GetRandReader() io.Reader {
return randReader
}
// SetRandReader sets the io.Reader used by passhash to generate random bytes
func SetRandReader(reader io.Reader) {
randReader = reader
}
// WorkFactor describes the work/cost for a KDF
// The interface is similar to Go's "encoding" Marshaler/Unmarshalers
type WorkFactor interface {
Marshal() ([]int, error)
Unmarshal([]int) error
}
// WorkFactorsEqual determines if 2 WorkFactors are equivalent
func WorkFactorsEqual(a, b WorkFactor) bool {
if reflect.TypeOf(a) != reflect.TypeOf(b) {
return false
}
aM, err := a.Marshal()
if err != nil {
return false
}
bM, err := b.Marshal()
if err != nil {
return false
}
if len(aM) != len(bM) {
return false
}
for i, aV := range aM {
if aV != bM[i] {
return false
}
}
return true
}
// Pbkdf2WorkFactor specifies the work/cost parameters for PBKDF2
type Pbkdf2WorkFactor struct {
Iter int
}
// Marshal returns the marshaled WorkFactor
func (wf *Pbkdf2WorkFactor) Marshal() ([]int, error) {
return []int{wf.Iter}, nil
}
// Unmarshal unmarshals the WorkFactor
func (wf *Pbkdf2WorkFactor) Unmarshal(p []int) error {
if len(p) != 1 {
return fmt.Errorf("Invalid parameters to unmarshal %T", wf)
}
wf.Iter = p[0]
return nil
}
// BcryptWorkFactor specifies the work/cost parameters for bcrypt
type BcryptWorkFactor struct {
Cost int
}
// Marshal returns the marshaled WorkFactor
func (wf *BcryptWorkFactor) Marshal() ([]int, error) {
return []int{wf.Cost}, nil
}
// Unmarshal unmarshals the WorkFactor
func (wf *BcryptWorkFactor) Unmarshal(p []int) error {
if len(p) != 1 {
return fmt.Errorf("Invalid parameters to unmarshal %T", wf)
}
wf.Cost = p[0]
return nil
}
// ScryptWorkFactor specifies the work/cost parameters for scrypt
type ScryptWorkFactor struct {
R int
P int
N int
}
// Marshal returns the marshaled WorkFactor
func (wf *ScryptWorkFactor) Marshal() ([]int, error) {
return []int{wf.R, wf.P, wf.N}, nil
}
// Unmarshal unmarshals the WorkFactor
func (wf *ScryptWorkFactor) Unmarshal(p []int) error {
if len(p) != 3 {
return fmt.Errorf("Invalid parameters to unmarshal %T", wf)
}
wf.R = p[0]
wf.P = p[1]
wf.N = p[2]
return nil
}
// Config provides configuration for managing credentials. e.g. creation, storing, verifying, and auditing
type Config struct {
Kdf Kdf // The key derivation function
WorkFactor WorkFactor // The work factor for the kdf
SaltSize int // The size of the salt in bytes
KeyLength int // The size of the output key (e.g. hash) in bytes
AuditLogger AuditLogger // The AuditLogger to use
Store CredentialStore // The CredentialStore to use
PasswordPolicies []PasswordPolicy // The password policies to enforce
}
// NewCredential creates a new Credential with the provided Config
func (c Config) NewCredential(userID UserID, password string) (*Credential, error) {
passwordPolicyFailures := PasswordPoliciesNotMet{}
for _, pp := range c.PasswordPolicies {
if err := pp.PasswordAcceptable(password); err != nil {
passwordPolicyFailures.UnMetPasswordPolicies = append(passwordPolicyFailures.UnMetPasswordPolicies,
PasswordPolicyError{PasswordPolicy: pp, Err: err})
}
}
if len(passwordPolicyFailures.UnMetPasswordPolicies) > 0 {
return nil, passwordPolicyFailures
}
salt := make([]byte, c.SaltSize)
if _, err := randReader.Read(salt); err != nil {
return nil, err
}
hash, err := getPasswordHash(c.Kdf, c.WorkFactor, salt, c.KeyLength, password)
if err != nil {
return nil, err
}
return &Credential{UserID: userID, Kdf: c.Kdf, WorkFactor: c.WorkFactor, Salt: salt, Hash: hash}, nil
}