-
Notifications
You must be signed in to change notification settings - Fork 0
/
keydata.go
297 lines (276 loc) · 7.43 KB
/
keydata.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package dshards
import (
"bytes"
"crypto/rsa"
"errors"
"fmt"
"math/big"
"github.com/cjslep/syrup"
)
const (
kKeyData = "keydata"
kKeyNote = "rsa-pcks1-sha256"
)
type PublicKeyer interface {
PublicKey() rsa.PublicKey
}
type PrivateKeyer interface {
PublicKeyer
PrivateKey() *rsa.PrivateKey
}
var _ PublicKeyer = new(EncryptedKeyData)
type EncryptedKeyData struct {
vk rsa.PublicKey
encwk []byte
}
var _ PublicKeyer = new(DecryptedKeyData)
var _ PrivateKeyer = new(DecryptedKeyData)
type DecryptedKeyData struct {
vk rsa.PublicKey
wk *rsa.PrivateKey
// Used to encrypt the write-key (wk)
key SymmetricKey
s Suite
}
func NewDecryptedKeyData(key SymmetricKey, s Suite) *DecryptedKeyData {
return &DecryptedKeyData{key: key, s: s}
}
func (e EncryptedKeyData) PublicKey() rsa.PublicKey {
return e.vk
}
func (d DecryptedKeyData) PublicKey() rsa.PublicKey {
return d.vk
}
func (d DecryptedKeyData) PrivateKey() *rsa.PrivateKey {
return d.wk
}
func (k *EncryptedKeyData) Unmarshal(b []byte) (err error) {
buf := bytes.NewBuffer(b)
var v interface{}
err = syrup.NewDecoder(syrup.NewPrototypeEncoding(), buf).Decode(&v)
if err != nil {
return
}
var vs []interface{}
var ok bool
if vs, ok = v.([]interface{}); !ok {
err = fmt.Errorf("decoded keydata is not a []interface{}: %T", v)
return
}
if len(vs) != 3 {
err = fmt.Errorf("decoded keydata not len() 3: %d", len(vs))
return
}
if s, ok := vs[0].(string); !ok || s != kKeyData {
err = fmt.Errorf("decoded keydata first item not %q: %s", kKeyData, vs[0])
return
}
// Handle public key list
if vs1, ok := vs[1].([]interface{}); !ok {
err = fmt.Errorf("decoded keydata second item not list: %T", vs[1])
return
} else {
if len(vs1) != 2 {
err = fmt.Errorf("decoded keydata public key list not len() 2: %d", len(vs1))
return
} else if s, ok := vs1[0].(string); !ok || s != kKeyNote {
err = fmt.Errorf("decoded keydata public key unknown type: %s", vs1[0])
return
}
if m, ok := vs1[1].(map[interface{}]interface{}); !ok {
err = fmt.Errorf("decoded keydata second item not dict: %T", vs1[1])
return
} else if n, ok := m[interface{}("n")]; !ok {
err = errors.New("decoded keydata second item has no n")
return
} else if e, ok := m[interface{}("e")]; !ok {
err = errors.New("decoded keydata second item has no e")
} else {
switch nv := n.(type) {
case int64:
k.vk.N = big.NewInt(nv)
case *big.Int:
k.vk.N = nv
default:
err = fmt.Errorf("decoded keydata publickey n not int64 nor bigint: %T", n)
return
}
switch ev := e.(type) {
case int64:
k.vk.E = int(ev)
default:
err = fmt.Errorf("decoded keydata publickey e not int64: %T", n)
return
}
}
}
// Handle encrypted private key list
if vs2, ok := vs[2].([]interface{}); !ok {
err = fmt.Errorf("decoded keydata third item not list: %T", vs[2])
return
} else {
if len(vs2) != 2 {
err = fmt.Errorf("decoded keydata encrypted private key list not len() 2: %d", len(vs2))
return
} else if s, ok := vs2[0].(string); !ok || s != kKeyNote {
err = fmt.Errorf("decoded keydata encrypted private key unknown type: %s", vs2[0])
return
}
if eb, ok := vs2[1].([]byte); !ok {
err = fmt.Errorf("decoded keydata encrypted private key not bytes: %T", vs2[1])
return
} else {
k.encwk = eb
}
}
return
}
func (k *DecryptedKeyData) Unmarshal(b []byte) (err error) {
ek := &EncryptedKeyData{}
err = ek.Unmarshal(b)
if err != nil {
return
}
k.vk = ek.vk
var dec []byte
dec, err = decryptWriteKey(ek.encwk, k.key, k.s)
if err != nil {
return
}
buf := bytes.NewBuffer(dec)
defer buf.Reset()
var v interface{}
err = syrup.NewDecoder(syrup.NewPrototypeEncoding(), buf).Decode(&v)
if err != nil {
return
}
k.wk = &rsa.PrivateKey{}
if m, ok := v.(map[interface{}]interface{}); !ok {
err = fmt.Errorf("decoded keydata decrypted private key unexpected type: %T", v)
return
} else if di, ok := m[interface{}("d")]; !ok {
err = errors.New("decoded keydata private write key has no d")
return
} else if d, ok := di.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key d unexpected type: %T", di)
return
} else if dpi, ok := m[interface{}("dp")]; !ok {
err = errors.New("decoded keydata private write key has no dp")
return
} else if dp, ok := dpi.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key dp unexpected type: %T", dpi)
return
} else if dqi, ok := m[interface{}("dq")]; !ok {
err = errors.New("decoded keydata private write key has no dq")
return
} else if dq, ok := dqi.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key dq unexpected type: %T", dqi)
return
} else if ei, ok := m[interface{}("e")]; !ok {
err = errors.New("decoded keydata private write key has no e")
return
} else if e, ok := ei.(int64); !ok {
err = fmt.Errorf("decoded keydata private write key e unexpected type: %T", ei)
return
} else if ni, ok := m[interface{}("n")]; !ok {
err = errors.New("decoded keydata private write key has no n")
return
} else if n, ok := ni.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key n unexpected type: %T", ni)
return
} else if pi, ok := m[interface{}("p")]; !ok {
err = errors.New("decoded keydata private write key has no p")
return
} else if p, ok := pi.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key p unexpected type: %T", pi)
return
} else if qi, ok := m[interface{}("q")]; !ok {
err = errors.New("decoded keydata private write key has no q")
return
} else if q, ok := qi.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key q unexpected type: %T", qi)
return
} else if qInvi, ok := m[interface{}("qInv")]; !ok {
err = errors.New("decoded keydata private write key has no qInv")
return
} else if qInv, ok := qInvi.(*big.Int); !ok {
err = fmt.Errorf("decoded keydata private write key qInv unexpected type: %T", qInvi)
return
} else {
k.wk.D = d
k.wk.Precomputed.Dp = dp
k.wk.Precomputed.Dq = dq
k.wk.PublicKey.E = int(e)
k.wk.PublicKey.N = n
k.wk.Primes = []*big.Int{p, q}
k.wk.Precomputed.Qinv = qInv
}
return
}
func (k EncryptedKeyData) Marshal() (b []byte, err error) {
var buf bytes.Buffer
v := []interface{}{
kKeyData,
[]interface{}{
kKeyNote,
pubKey{
N: k.vk.N,
E: k.vk.E,
},
},
[]interface{}{
kKeyNote,
k.encwk,
},
}
err = syrup.NewEncoder(syrup.NewPrototypeEncoding(), &buf).Encode(v)
b = buf.Bytes()
return
}
func (k DecryptedKeyData) Marshal() (b []byte, err error) {
var buf bytes.Buffer
v := []interface{}{
kKeyData,
[]interface{}{
kKeyNote,
pubKey{
N: k.vk.N,
E: k.vk.E,
},
},
}
// Append entire []interface{} as the third item.
if len(k.wk.Primes) != 2 {
err = fmt.Errorf("dshards: keydata cannot serialize %d primes", len(k.wk.Primes))
return
}
k.wk.Precompute()
var toEncBuf bytes.Buffer
defer toEncBuf.Reset()
encV := privKey{
D: k.wk.D,
Dp: k.wk.Precomputed.Dp,
Dq: k.wk.Precomputed.Dq,
E: k.wk.PublicKey.E,
N: k.wk.PublicKey.N,
P: k.wk.Primes[0],
Q: k.wk.Primes[1],
QInv: k.wk.Precomputed.Qinv,
}
err = syrup.NewEncoder(syrup.NewPrototypeEncoding(), &toEncBuf).Encode(encV)
if err != nil {
return
}
var enc []byte
enc, err = encryptWriteKey(toEncBuf.Bytes(), k.key, k.s)
if err != nil {
return
}
v = append(v, []interface{}{
kKeyNote,
enc,
})
err = syrup.NewEncoder(syrup.NewPrototypeEncoding(), &buf).Encode(v)
b = buf.Bytes()
return
}