forked from cloudflare/gokey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.go
221 lines (178 loc) · 3.96 KB
/
keygen.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package gokey
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"io"
"strings"
"unicode"
deterministicRsaKeygen "github.com/cloudflare/gokey/rsa"
"golang.org/x/crypto/ed25519"
)
type KeyType int
const (
EC256 KeyType = iota
EC384
EC521
RSA2048
RSA4096
X25519
ED25519
)
//go:generate stringer -type KeyType
type KeyGen struct {
rng io.Reader
}
type PasswordSpec struct {
Length int
Upper int
Lower int
Digits int
Special int
AllowedSpecial string
}
func (spec *PasswordSpec) Valid() bool {
if spec.AllowedSpecial != "" {
for _, c := range spec.AllowedSpecial {
if !unicode.IsSymbol(c) && !unicode.IsPunct(c) {
return false
}
}
}
return spec.Length >= spec.Upper+spec.Lower+spec.Digits+spec.Special
}
func allowed(num, fromSpec int) bool {
if num > 0 && fromSpec == 0 {
return false
}
if num < fromSpec {
return false
}
return true
}
func (spec *PasswordSpec) Compliant(password string) bool {
var upper, lower, digits, special int
for _, c := range password {
if unicode.IsUpper(c) {
upper++
}
if unicode.IsLower(c) {
lower++
}
if unicode.IsDigit(c) {
digits++
}
if unicode.IsSymbol(c) || unicode.IsPunct(c) {
if spec.AllowedSpecial == "" {
special++
} else {
if strings.ContainsRune(spec.AllowedSpecial, c) {
special++
} else {
return false
}
}
}
}
if !allowed(upper, spec.Upper) || !allowed(lower, spec.Lower) || !allowed(digits, spec.Digits) || !allowed(special, spec.Special) {
return false
}
return true
}
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?"
func randRange(rng io.Reader, max byte) (byte, error) {
var base [1]byte
for {
_, err := io.ReadFull(rng, base[:])
if err != nil {
return 0, err
}
if 255 == base[0] {
continue
}
rem := 255 % max
buck := 255 / max
if base[0] < 255-rem {
return base[0] / buck, nil
}
}
}
func (keygen *KeyGen) genRandStr(length int) (string, error) {
bytes := make([]byte, length)
for i := 0; i < length; i++ {
pos, err := randRange(keygen.rng, byte(len(chars)))
if err != nil {
return "", err
}
bytes[i] = chars[pos]
}
return string(bytes), nil
}
func (keygen *KeyGen) GeneratePassword(spec *PasswordSpec) (string, error) {
if !spec.Valid() {
return "", errors.New("invalid password specification")
}
for {
password, err := keygen.genRandStr(spec.Length)
if err != nil {
return "", err
}
if spec.Compliant(password) {
return password, nil
}
}
}
func (keygen *KeyGen) generateRsa(kt KeyType) (crypto.PrivateKey, error) {
bits := 0
switch kt {
case RSA2048:
bits = 2048
case RSA4096:
bits = 4096
default:
return nil, errors.New("invalid RSA key size requested")
}
return deterministicRsaKeygen.GenerateKey(keygen.rng, bits)
}
func (keygen *KeyGen) generateEc(kt KeyType) (crypto.PrivateKey, error) {
var curve elliptic.Curve
switch kt {
case EC256:
curve = elliptic.P256()
case EC384:
curve = elliptic.P384()
case EC521:
curve = elliptic.P521()
default:
return nil, errors.New("invalid EC key size requested")
}
return ecdsa.GenerateKey(curve, keygen.rng)
}
func (keygen *KeyGen) generate25519(kt KeyType) (crypto.PrivateKey, error) {
switch kt {
case X25519:
var privKey [32]byte
_, err := io.ReadFull(keygen.rng, privKey[:])
// from https://cr.yp.to/ecdh.html
privKey[0] &= 248
privKey[31] &= 127
privKey[31] |= 64
return x25519PrivateKey(privKey[:]), err
case ED25519:
_, privKey, err := ed25519.GenerateKey(keygen.rng)
return &privKey, err
}
return nil, errors.New("invalid key type requested")
}
func (keygen *KeyGen) GenerateKey(kt KeyType) (crypto.PrivateKey, error) {
switch kt {
case EC256, EC384, EC521:
return keygen.generateEc(kt)
case RSA2048, RSA4096:
return keygen.generateRsa(kt)
case X25519, ED25519:
return keygen.generate25519(kt)
}
return nil, errors.New("invalid key type requested")
}