-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.go
105 lines (86 loc) · 2.04 KB
/
blob.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
// +build go1.12
package obytes
import (
"crypto/rand"
chacha "golang.org/x/crypto/chacha20poly1305"
)
// this just an obfuscation mechanism to hide "structure" in a flow of bytes
// with an overhead, it is NOT in ANY WAY a way to SECURE DATA, it is a simple obfuscation.
func NewBlob(in []byte, mask_hi, mask_lo byte, prefixlen int) (out []byte, err error) {
randpfx := make([]byte, prefixlen)
_, err = rand.Read(randpfx)
if err != nil {
err = ErrCrypto
return
}
xkey := make([]byte, chacha.KeySize)
_, err = rand.Read(xkey)
if err != nil {
err = ErrCrypto
return
}
nonce := make([]byte, chacha.NonceSizeX)
_, err = rand.Read(nonce)
if err != nil {
err = ErrCrypto
return
}
oxkey, err := ObfuscateWithMask(xkey, mask_hi, mask_lo)
if err != nil {
err = ErrUnexpected
return
}
ononce, err := ObfuscateWithMask(nonce, mask_hi, mask_lo)
if err != nil {
err = ErrUnexpected
return
}
aead, err := chacha.NewX(xkey)
if err != nil {
err = ErrCrypto
return
}
ct := aead.Seal(nil, nonce, in, nil)
out = append(out, randpfx...)
out = append(out, ononce...)
out = append(out, oxkey...)
out = append(out, ct...)
return
}
func OpenBlob(in []byte, mask_hi, mask_lo byte, prefixlen int) (out []byte, err error) {
ononcelen := chacha.NonceSizeX * 2
okeylen := chacha.KeySize * 2
minlen := prefixlen + ononcelen + okeylen + 16
if len(in) < minlen {
err = ErrInvalidInput
return
}
ononce := in[prefixlen : prefixlen+ononcelen]
nonce, err := DeobfuscateWithMask(ononce, mask_hi, mask_lo)
if err != nil {
//err = derr
err = ErrUnexpected
return
}
oxkey := in[prefixlen+ononcelen : prefixlen+ononcelen+okeylen]
xkey, err := DeobfuscateWithMask(oxkey, mask_hi, mask_lo)
if err != nil {
//err = xerr
err = ErrUnexpected
return
}
aead, err := chacha.NewX(xkey)
if err != nil {
//err = cerr
err = ErrCrypto
return
}
ct := in[prefixlen+ononcelen+okeylen:]
out, err = aead.Open(nil, nonce, ct, nil)
if err != nil {
//err = fmt.Errorf("invalid plural box")
err = ErrCrypto
return
}
return
}