-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
237 lines (207 loc) · 4.85 KB
/
keys.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package cryptostack
import (
"bytes"
"crypto/rand"
"errors"
"io"
"github.com/agl/ed25519"
"github.com/dchest/blake2b"
"golang.org/x/crypto/nacl/box"
"golang.org/x/crypto/pbkdf2"
)
type Pkey struct {
Alg string `json:"alg"`
ID []byte `json:"id"`
Curve struct {
Pkey []byte `json:"pkey"`
} `json:"curve"`
Ed struct {
Pkey []byte `json:"pkey"`
} `json:"ed"`
curvePkey *[32]byte
edPkey *[32]byte
}
func NewPkey(curvePkey *[32]byte, edPkey *[32]byte) *Pkey {
pkey := &Pkey{Alg: "curve25519-ed25519"}
pkey.Curve.Pkey = curvePkey[:]
pkey.Ed.Pkey = edPkey[:]
pkey.curvePkey = curvePkey
pkey.edPkey = edPkey
return pkey
}
func (pkey Pkey) GetCurveKey() *[32]byte {
if pkey.curvePkey == nil {
pkey.curvePkey = &[32]byte{}
copy(pkey.curvePkey[:], pkey.Curve.Pkey)
}
curvePkey := *pkey.curvePkey
return &curvePkey
}
func (pkey Pkey) GetEdKey() *[32]byte {
if pkey.edPkey == nil {
pkey.edPkey = &[32]byte{}
copy(pkey.edPkey[:], pkey.Ed.Pkey)
}
edPkey := *pkey.edPkey
return &edPkey
}
func (pkey *Pkey) Verify(message []byte, sig []byte) error {
if len(sig) != 64 {
return errors.New("Signature size not equal 64")
}
s := &[64]byte{}
copy(s[:], sig)
if !ed25519.Verify(pkey.GetEdKey(), message, s) {
return errors.New("Verify failed")
}
return nil
}
type Skey struct {
Alg string `json:"alg"`
ID []byte `json:"id"`
Kdf struct {
Alg string `json:"alg"`
Salt []byte `json:"salt"`
Rounds int `json:"rounds"`
} `json:"kdf"`
Curve struct {
Pkey []byte `json:"pkey"`
Skey []byte `json:"skey"`
} `json:"curve"`
Ed struct {
Pkey []byte `json:"pkey"`
Skey []byte `json:"skey"`
} `json:"ed"`
Checksum []byte `json:"checksum"`
pkey *Pkey
curveSkey *[32]byte
edSkey *[64]byte
}
func (skey Skey) GetPkey() *Pkey {
return skey.pkey
}
func (skey Skey) GetCurveKey() *[32]byte {
curveSkey := *skey.curveSkey
return &curveSkey
}
func (skey Skey) GetEdKey() *[64]byte {
edSkey := *skey.edSkey
return &edSkey
}
func (skey *Skey) Sign(message []byte) []byte {
return ed25519.Sign(skey.edSkey, message)[:]
}
func (skey *Skey) Encrypt(password []byte) {
skey.xor(password)
}
func (skey *Skey) Decrypt(password []byte) error {
skey.xor(password)
curvePkey := &[32]byte{}
edPkey := &[32]byte{}
copy(curvePkey[:], skey.Curve.Pkey)
copy(edPkey[:], skey.Ed.Pkey)
skey.pkey = NewPkey(curvePkey, edPkey)
skey.pkey.ID = skey.ID
skey.curveSkey = &[32]byte{}
skey.edSkey = &[64]byte{}
copy(skey.curveSkey[:], skey.Curve.Skey)
copy(skey.edSkey[:], skey.Ed.Skey)
if !bytes.Equal(skey.Checksum, skey.checksum()) {
return errors.New("Bad checksum")
}
return nil
}
func (skey *Skey) checksum() []byte {
checksum := blake2b.New256()
checksum.Write(skey.ID)
checksum.Write(skey.Curve.Skey)
checksum.Write(skey.Ed.Skey)
return checksum.Sum([]byte{})
}
func (skey *Skey) xor(password []byte) {
s := skey
l := len(s.ID) + len(s.Ed.Pkey) + len(s.Ed.Skey) + len(s.Curve.Pkey) + len(s.Curve.Skey) + len(s.Checksum)
dk := pbkdf2.Key(password, s.Kdf.Salt, s.Kdf.Rounds, l, blake2b.New512)
v := [][]byte{s.ID, s.Curve.Pkey, s.Curve.Skey, s.Ed.Pkey, s.Ed.Skey, s.Checksum}
j := 0
for k := range v {
for i := range v[k] {
v[k][i] = v[k][i] ^ dk[j]
j++
}
}
}
func GenerateKey() (*Skey, error) {
curvePkey, curveSkey, err := box.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
edPkey, edSkey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
pkey := NewPkey(curvePkey, edPkey)
skey := Skey{pkey: pkey, curveSkey: curveSkey, edSkey: edSkey}
salt := make([]byte, 32)
_, err = rand.Read(salt)
if err != nil {
return nil, err
}
rounds := 4096
id := make([]byte, 8)
_, err = rand.Read(id)
if err != nil {
return nil, err
}
skey.Alg = pkey.Alg
pkey.ID = id
skey.ID = pkey.ID
skey.Kdf.Alg = "pbkdf2-blake2b"
skey.Kdf.Rounds = rounds
skey.Kdf.Salt = salt
skey.Curve.Pkey = pkey.Curve.Pkey
skey.Curve.Skey = curveSkey[:]
skey.Ed.Pkey = pkey.Ed.Pkey
skey.Ed.Skey = edSkey[:]
skey.Checksum = skey.checksum()
return &skey, nil
}
type Signature struct {
Alg string `json:"alg"`
Pkey *Pkey `json:"pkey"`
Hash []byte `json:"hash"`
Sig []byte `json:"sig"`
}
func NewSignature(pkey *Pkey) *Signature {
sig := &Signature{}
sig.Alg = "ed25519"
sig.Pkey = pkey
return sig
}
func (sig *Signature) Sign(skey *Skey, r io.Reader) error {
hash, err := sig.computeHash(r)
if err != nil {
return err
}
sig.Hash = hash
sig.Sig = skey.Sign(sig.Hash)
return nil
}
func (sig *Signature) Verify(r io.Reader) error {
hash, err := sig.computeHash(r)
if err != nil {
return err
}
if !bytes.Equal(sig.Hash, hash) {
return errors.New("Bad checksum")
}
return sig.Pkey.Verify(sig.Hash, sig.Sig)
}
func (sig *Signature) computeHash(r io.Reader) ([]byte, error) {
hash := blake2b.New512()
_, err := io.Copy(hash, r)
if err != nil {
return nil, err
}
return hash.Sum([]byte{}), nil
}