This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
290 lines (244 loc) · 5.69 KB
/
encoding.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
///// Encoding Layer
package backuplib
import (
"bufio"
"encoding/gob"
"errors"
"fmt"
"github.com/iden3/go-backup/ff"
fc "github.com/iden3/go-backup/filecrypt"
"github.com/iden3/go-backup/shamir"
"github.com/iden3/go-iden3-core/db"
"github.com/iden3/go-iden3-crypto/babyjub"
"os"
)
// Types of data we can include in the backup. Needed to register the data strcuture
const (
START_TYPES = iota
WALLET_CONFIG
CUSTODIAN
SSHARING
SHARES
PKEYS
STORAGE
NTYPES
// Add other possible data types that we need encoding
)
func initEncoding() {
for i := START_TYPES + 1; i < NTYPES; i++ {
encodeType(i)
}
}
// Register data strucrure
func encodeType(dtype int) {
switch dtype {
case WALLET_CONFIG:
gob.Register(&WalletConfig{})
case CUSTODIAN:
gob.Register(&Custodians{})
var el []Custodian
gob.Register(el)
case SSHARING:
gob.Register(&shamir.Shamir{})
case SHARES:
el1, _ := ff.NewElement(PRIME)
var el2 shamir.Share
var el3 []shamir.Share
gob.Register(el1)
gob.Register(el2)
gob.Register(el3)
case PKEYS:
var el PrivateKeys
var el1 *babyjub.PrivateKey
gob.Register(&el)
gob.Register(el1)
case STORAGE:
var el db.KV
var el1 []db.KV
gob.Register(&el)
gob.Register(el1)
}
}
// Transform share encoding to []byte
func encodeShareToByte(shares []shamir.Share, folder string) []byte {
tmpFname := folder + "share-tmp.dat"
encodeShare(shares, tmpFname)
defer os.Remove(tmpFname)
data := readBinaryFile(tmpFname)
return data
}
// Read file
func readBinaryFile(tmpFname string) []byte {
file, err := os.Open(tmpFname)
defer file.Close()
if err != nil {
panic(err)
}
stats, err := file.Stat()
if err != nil {
panic(err)
}
var size int64 = stats.Size()
bytes := make([]byte, size)
bufr := bufio.NewReader(file)
_, err = bufr.Read(bytes)
return bytes
}
// Generate share blocks to distribure via secret sharing and return
// filecrpyt bytestream
func encodeShare(shares []shamir.Share, fname string) error {
// Key header -> no key (only for hmac)
//key := []byte("ThisIsMySecretKey")
fileCrypt, err := fc.New(1, fname, nil, nil, fc.FC_KEY_T_NOKEY)
if err != nil {
return fmt.Errorf("New FC : %w", err)
}
// Encryption header -> not encrypted
err = fileCrypt.AddBlock([]byte("SHARES"), fc.FC_CLEAR, shares)
if err != nil {
return fmt.Errorf("Add Block : %w", err)
}
return nil
}
// Decode and decrypt file using provided key
func DecodeUnencrypted(fname string) error {
//key := []byte("ThisIsMySecretKey")
info := decode(fname, nil)
rxCustodians := retrieveCustodians(info)
if rxCustodians == nil {
return errors.New("Invalid Custodian Format")
} else {
initCustodians()
custodians := GetCustodians()
custodians.Data = rxCustodians
SetCustodians(custodians)
}
rxSecretCfg := retrieveSSharing(info)
if rxSecretCfg == nil {
return errors.New("Invalid Secret Sharing Format")
} else {
initSecretCfg()
secretCfg := GetSecretCfg()
secretCfg.MinShares = rxSecretCfg.GetMinShares()
secretCfg.MaxShares = rxSecretCfg.GetMaxShares()
secretCfg.ElementType = rxSecretCfg.GetElType()
SetSecretCfg(secretCfg)
}
return nil
}
func DecodeEncrypted(fname string) error {
key := GetkOp()
info := decode(fname, key)
retrievedWallet := retrieveWallet(info)
if retrievedWallet == nil {
return errors.New("Invalid Wallet Format")
} else {
SetWallet(retrievedWallet)
}
retrievedShares := retrieveShares(info)
if retrievedShares == nil {
return errors.New("Invalid shares Format")
} else {
shares := GetShares()
shares.Data = fromShares(retrievedShares)
SetShares(shares)
}
retrievedPrivateKeys := retrievePrivateKeys(info)
if retrievedPrivateKeys == nil {
return errors.New("Invalid Private Keys Format")
} else {
SetPrivateKeys(retrievedPrivateKeys)
}
retrievedStorage := retrieveStorage(info)
if retrievedStorage == nil {
return errors.New("Invalid Storage Format")
} else {
SetStorage(retrievedStorage)
}
return nil
}
// Decode and decrypt file using provided key
func decode(fname string, key []byte) []interface{} {
newFC, _ := fc.NewFromFile(key, fname)
results, _ := newFC.DecryptAll(key)
return results
}
// Retrieve functions return a specific data type from a generic type
// retrieve Custodian data structure
func retrieveCustodians(info []interface{}) []Custodian {
var r []Custodian
for _, el := range info {
switch el.(type) {
case []Custodian:
r = el.([]Custodian)
return r
case *Custodians:
r1 := el.(*Custodians)
return r1.Data
}
}
return nil
}
// retrieve Secret Sharing config data structure
func retrieveSSharing(info []interface{}) *shamir.Shamir {
var r *shamir.Shamir
for _, el := range info {
switch el.(type) {
case *shamir.Shamir:
r = el.(*shamir.Shamir)
return r
}
}
return nil
}
// Retreive wallet config
func retrieveWallet(info []interface{}) *WalletConfig {
var r *WalletConfig
for _, el := range info {
switch el.(type) {
case *WalletConfig:
r = el.(*WalletConfig)
return r
}
}
return nil
}
// retreive PrivateKey
func retrievePrivateKeys(info []interface{}) *PrivateKeys {
for _, el := range info {
switch el.(type) {
case *PrivateKeys:
var r *PrivateKeys
r = el.(*PrivateKeys)
return r
}
}
return nil
}
// retreive Storage
func retrieveStorage(info []interface{}) []db.KV {
for _, el := range info {
switch el.(type) {
case []db.KV:
var r []db.KV
r = el.([]db.KV)
return r
}
}
return nil
}
// Retrieve shares data structure
func retrieveShares(info []interface{}) []shamir.Share {
r := make([]shamir.Share, 0)
for _, el := range info {
switch el.(type) {
case []shamir.Share:
r = el.([]shamir.Share)
return r
case shamir.Share:
r = append(r, el.(shamir.Share))
return r
}
}
return nil
}