-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypair.go
255 lines (200 loc) · 4.92 KB
/
keypair.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package npki
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"errors"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/scrypt"
)
type KeypairType byte
const (
KeypairTypeInvalid KeypairType = 0x0
KeypairTypeEd25519 = 0x1
KeypairTypeDH25519 = 0x2
)
func GenerateDH25519() (Keypair, error) {
var pubkey, privkey [32]byte
if _, err := rand.Read(privkey[:]); err != nil {
return Keypair{}, err
}
curve25519.ScalarBaseMult(&pubkey, &privkey)
return Keypair{
algo: KeypairTypeDH25519,
pubkey: pubkey[:],
privkey: privkey[:],
}, nil
}
func GenerateEd25519() (Keypair, error) {
if pub, priv, err := ed25519.GenerateKey(nil); err != nil {
return Keypair{}, err
} else {
return Keypair{
algo: KeypairTypeEd25519,
pubkey: []byte(pub),
privkey: []byte(priv),
}, nil
}
}
var (
ErrInvalidKeyFormat = errors.New("invalid key format")
ErrInvalidChecksum = errors.New("invalid checksum")
ErrDecryptionFailed = errors.New("decryption failed")
ErrMissingKey = errors.New("missing key")
)
func DecodeKeypair(data []byte, key []byte, isKDF bool) (Keypair, error) {
var err error
var kp Keypair
var size int
kp.algo = KeypairType(data[0])
switch kp.algo {
case KeypairTypeEd25519:
size = ed25519.PrivateKeySize
case KeypairTypeDH25519:
size = 32
default:
return Keypair{}, ErrInvalidKeyFormat
}
kp.privkey = make([]byte, size)
kp.pubkey = make([]byte, 32)
flagE := data[1]&0x80 == 0x80
flagS := data[1]&0x40 == 0x40
flagN := data[1]&0x20 == 0x20
if flagE {
size += 16 + chacha20poly1305.NonceSize
}
if flagS {
size += 32 // scrypt salt
}
// verify checksum
digest := sha256.Sum256(data[:2+size])
if !bytes.Equal(data[2+size:2+size+4], digest[:4]) {
return Keypair{}, ErrInvalidChecksum
}
if flagE { // E
if key == nil {
return Keypair{}, ErrMissingKey
}
var nonce []byte
var ciphertext []byte
if flagS { // S
if !isKDF {
return Keypair{}, ErrMissingKey
}
salt := data[2 : 2+32]
if key, err = scrypt.Key(key, salt, 16384, 8, 1, 32); err != nil {
return Keypair{}, err
}
nonce = data[2+32 : 2+32+chacha20poly1305.NonceSize]
ciphertext = data[2+32+chacha20poly1305.NonceSize : 2+size]
} else {
nonce = data[2 : 2+chacha20poly1305.NonceSize]
ciphertext = data[2+chacha20poly1305.NonceSize : 2+size]
}
chacha, err := chacha20poly1305.New(key)
if err != nil {
return Keypair{}, err
}
key, err = chacha.Open(nil, nonce, ciphertext, nil)
if err != nil {
return Keypair{}, ErrDecryptionFailed
}
copy(kp.privkey, key)
} else if flagN {
copy(kp.pubkey, data[2:size+2])
} else {
copy(kp.privkey, data[2:size+2])
}
if !flagN {
if kp.algo == KeypairTypeEd25519 {
copy(kp.pubkey, Ed25519PublicKey(kp.privkey))
} else if kp.algo == KeypairTypeDH25519 {
copy(kp.pubkey, DH25519PublicKey(kp.privkey))
}
}
return kp, nil
}
type Keypair struct {
algo KeypairType
pubkey []byte
privkey []byte
}
func (t *Keypair) Type() KeypairType {
return t.algo
}
func (t *Keypair) PublicKey() []byte {
return t.pubkey
}
func (t *Keypair) PrivateKey() []byte {
return t.privkey
}
func (t *Keypair) Neuter() {
t.privkey = make([]byte, 0)
}
func (t *Keypair) Sign(data []byte) ([]byte, error) {
if t.algo != KeypairTypeEd25519 {
return nil, ErrInvalidKeyFormat
}
return ed25519.Sign(ed25519.PrivateKey(t.privkey), data), nil
}
func (t *Keypair) SignCertificate(cert Certificate) (Certificate, error) {
cert.Extensions = append(cert.Extensions, Extension{SignedWithEd25519ExtensionType, ExtensionFlagAffectsValidation, t.PublicKey()})
var err error
cert.Signature, err = t.Sign(cert.Encode())
if err != nil {
return Certificate{}, err
}
return cert, nil
}
func (t *Keypair) Encode(key []byte, kdf bool) ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte(byte(t.algo)) // Type
var b byte
var salt []byte
if len(key) > 0 {
// 0th bit: is encrypted
b |= 0x80 // E
// 2nd bit: key is a passphrase that should be run through a KDF
if kdf {
b |= 0x40 // S
var err error
salt = make([]byte, 32)
if _, err = rand.Read(salt); err != nil {
return nil, err
}
if key, err = scrypt.Key(key, salt, 16384, 8, 1, 32); err != nil {
return nil, err
}
}
}
// 3rd bit: neutered
if len(t.privkey) == 0 {
b |= 0x20 // N
}
buf.WriteByte(b)
if len(key) > 0 {
chacha, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
nonce := make([]byte, chacha20poly1305.NonceSize)
if _, err := rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := chacha.Seal(nil, nonce, t.privkey, nil)
if len(salt) > 0 {
buf.Write(salt)
}
buf.Write(nonce)
buf.Write(ciphertext)
} else if len(t.privkey) > 0 {
buf.Write(t.privkey)
} else {
buf.Write(t.pubkey)
}
digest := sha256.Sum256(buf.Bytes())
buf.Write(digest[:4]) // Checksum
return buf.Bytes(), nil
}