-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
261 lines (204 loc) · 5.23 KB
/
block.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
package unknownaccess
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"errors"
"io"
"math/big"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/hkdf"
)
const (
dataSize = 140
authTagSize = 16
)
var (
ErrFailDecrypt = errors.New("failed to decrypt block")
ErrDataFull = errors.New("all data blocks full")
ErrContentBig = errors.New("content too long")
)
type Block struct {
IV [12]byte //12 bytes
Data [3][dataSize]byte // (124bytes content + 16bytes tag)*3 bytes
}
type Offset uint16
func NewBlock() *Block {
b := &Block{}
rand.Read(b.IV[:])
return b
}
func (b *Block) SetS1(passphrase string, data []byte) error {
return b.setSecret(1, passphrase, data)
}
func (b *Block) SetS2(passphrase string, data []byte) error {
return b.setSecret(2, passphrase, data)
}
func (b *Block) SetS3(passphrase string, data []byte) error {
return b.setSecret(3, passphrase, data)
}
func (b *Block) setSecret(position uint8, passphrase string, data []byte) error {
if len(data)+authTagSize > dataSize {
return ErrContentBig
}
nonce, err := b.iv(position)
if err != nil {
return err
}
enc, err := b.newCipher(passphrase, nonce)
if err != nil {
return err
}
// compute PKCS#5 padding
padding := pkcs5(len(data), dataSize-authTagSize)
// use generated IV and cipher to encrypt data and place into block
ciphertext := enc.Seal(nil, nonce, append(data, padding...), nil)
// write ciphertext to block
copy(b.Data[position-1][:], ciphertext)
return nil
}
func (b *Block) newCipher(passphrase string, nonce []byte) (cipher.AEAD, error) {
// Run passphrase through Argon2Key KDF with default settings to
// generate a 256bit key
k := argon2.Key([]byte(passphrase), nonce, 3, 32*1024, 4, 32)
cipherAES, err := aes.NewCipher(k)
if err != nil {
return nil, err
}
// create new GCM cipher
return cipher.NewGCM(cipherAES)
}
func (b *Block) iv(position uint8) ([]byte, error) {
ivKDF := hkdf.New(sha256.New, b.IV[:], nil, nil)
iv := make([]byte, 12)
// derive IV for corresponding location
for i := 0; i <= int(position); i++ {
n, err := ivKDF.Read(iv)
if err != nil {
return nil, err
} else if n < 12 {
return nil, io.ErrShortWrite
}
}
return iv, nil
}
// Decrypt will find your secret within the block and decrypt it
func (b *Block) Decrypt(passphrase string) ([]byte, error) {
// attempt to decrypt each block one by one until successful
for i := uint8(1); i <= 3; i++ {
data, err := b.decryptSecret(i, passphrase)
if err == nil {
return data, nil
}
}
return nil, ErrFailDecrypt
}
func (b *Block) decryptSecret(position uint8, passphrase string) ([]byte, error) {
nonce, err := b.iv(position)
if err != nil {
return nil, err
}
enc, err := b.newCipher(passphrase, nonce)
if err != nil {
return nil, err
}
plaintext, err := enc.Open(nil, nonce, b.Data[position-1][:], nil)
if err != nil {
return nil, err
}
return pkcs5Unmarshal(plaintext), nil
}
func (b *Block) WriteDecoys() error {
indexes := b.unusedDataIndexes()
for _, p := range indexes {
n, err := rand.Read(b.Data[p][:])
if err != nil {
return err
} else if n != dataSize {
return io.ErrShortWrite
}
}
return nil
}
// Encrypt will randomly position your secret within the block and encrypt it
func (b *Block) Encrypt(passphrase string, data []byte) error {
freeIndexes := b.unusedDataIndexes()
if len(freeIndexes) == 0 {
return ErrDataFull
}
// pick random index from list
indexReference, err := rand.Int(rand.Reader, big.NewInt(int64(len(freeIndexes))))
if err != nil {
return err
}
index := freeIndexes[indexReference.Int64()]
return b.setSecret(uint8(index)+1, passphrase, data)
}
func (b *Block) unusedDataIndexes() (indexList []int) {
// create a blank array to compare to data blocks
blank := make([]byte, dataSize)
// check each data block and see which ones are all zeros
for i := 0; i < 3; i++ {
if bytes.Equal(b.Data[i][:], blank) {
indexList = append(indexList, i)
}
}
return indexList
}
// Encode will encode the block to a byte format
func (b *Block) Marshal() ([]byte, error) {
buf := bytes.NewBuffer(nil)
_, err := b.Encode(buf)
return buf.Bytes(), err
}
// Encode will encode the block to a byte format
func (b *Block) Encode(w io.Writer) (n int, err error) {
if err = b.WriteDecoys(); err != nil {
return 0, err
}
written, err := w.Write(b.IV[:])
n += written
if err != nil {
return n, err
} else if n < 12 {
return n, io.ErrShortWrite
}
// write data blocks
for i := 0; i < 3; i++ {
written, err = w.Write(b.Data[i][:])
n += written
if err != nil {
return n, err
} else if n < dataSize {
return n, io.ErrShortWrite
}
}
return n, nil
}
// Unmarshal will read the block and import it to a type
func Unmarshal(block []byte) (*Block, error) {
buf := bytes.NewBuffer(block)
return Decode(buf)
}
// Decode will read the block and import it to a type
func Decode(r io.Reader) (*Block, error) {
b := &Block{}
n, err := r.Read(b.IV[:])
if err != nil {
return nil, err
} else if n < 12 {
return nil, io.ErrUnexpectedEOF
}
// read data blocks
for i := 0; i < 3; i++ {
n, err = r.Read(b.Data[i][:])
if err != nil {
return nil, err
} else if n < dataSize {
return nil, io.ErrUnexpectedEOF
}
}
return b, nil
}