-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencryptionShaper.go
189 lines (157 loc) · 4.84 KB
/
encryptionShaper.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
package protean
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
)
const CHUNK_SIZE = 16
const IV_SIZE = 16
// Accepted in serialised form by Configure().
type EncryptionConfig struct {
Key string
}
// Creates a sample (non-random) config, suitable for testing.
func sampleEncryptionConfig() EncryptionConfig {
var bytes = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
hexHeader := hex.EncodeToString(bytes)
return EncryptionConfig{Key: hexHeader}
}
// A packet shaper that encrypts the packets with AES CBC.
type EncryptionShaper struct {
key []byte
}
func NewEncryptionShaper() *EncryptionShaper {
shaper := &EncryptionShaper{}
config := sampleEncryptionConfig()
jsonConfig, err := json.Marshal(config)
if err != nil {
return nil
}
shaper.Configure(string(jsonConfig))
return shaper
}
// This method is required to implement the Transformer API.
// @param {[]byte} key Key to set, not used by this class.
func (shaper *EncryptionShaper) SetKey(key []byte) {
}
// Configure the Transformer with the headers to inject and the headers
// to remove.
func (shaper *EncryptionShaper) Configure(jsonConfig string) {
var config EncryptionConfig
err := json.Unmarshal([]byte(jsonConfig), &config)
if err != nil {
fmt.Println("Encryption shaper requires key parameter")
}
shaper.ConfigureStruct(config)
}
func (shaper *EncryptionShaper) ConfigureStruct(config EncryptionConfig) {
shaper.key = deserializeEncryptionConfig(config)
}
// Decode the key from string in the config information
func deserializeEncryptionConfig(config EncryptionConfig) []byte {
return deserializeEncryptionModel(config.Key)
}
// Decode the header from a string in the header model
func deserializeEncryptionModel(model string) []byte {
config, _ := hex.DecodeString(model)
return config
}
// Inject header.
func (shaper *EncryptionShaper) Transform(buffer []byte) [][]byte {
// This Transform performs the following steps:
// - Generate a new random CHUNK_SIZE-byte IV for every packet
// - Encrypt the packet contents with the random IV and symmetric key
// - Concatenate the IV and encrypted packet contents
var iv []byte = makeIV()
var encrypted []byte = encrypt(shaper.key, iv, buffer)
return [][]byte{append(iv, encrypted...)}
}
func (shaper *EncryptionShaper) Restore(buffer []byte) [][]byte {
// This Restore performs the following steps:
// - Split the first CHUNK_SIZE bytes from the rest of the packet
// The two parts are the IV and the encrypted packet contents
// - Decrypt the encrypted packet contents with the IV and symmetric key
// - Return the decrypted packet contents
var iv = buffer[0:IV_SIZE]
var ciphertext = buffer[IV_SIZE:]
return [][]byte{decrypt(shaper.key, iv, ciphertext)}
}
// No-op (we have no state or any resources to Dispose).
func (shaper *EncryptionShaper) Dispose() {
}
func makeIV() []byte {
var randomBytes = make([]byte, IV_SIZE)
rand.Read(randomBytes)
return randomBytes
}
func encrypt(key []byte, iv []byte, buffer []byte) []byte {
var length []byte = encodeShort(uint16(len(buffer)))
var remainder = (len(length) + len(buffer)) % CHUNK_SIZE
var plaintext []byte
if remainder == 0 {
plaintext = append(length, buffer...)
} else {
var padding = make([]byte, CHUNK_SIZE-remainder)
rand.Read(padding)
plaintext = append(length, buffer...)
plaintext = append(plaintext, padding...)
}
block, err := aes.NewCipher(key)
if err != nil {
return nil
}
var enc = cipher.NewCBCEncrypter(block, iv)
var ciphertext []byte
for x := 0; x < (len(plaintext) / CHUNK_SIZE); x++ {
plainChunk := plaintext[x*CHUNK_SIZE : (x+1)*CHUNK_SIZE]
cipherChunk := make([]byte, len(plainChunk))
enc.CryptBlocks(cipherChunk, plainChunk)
ciphertext = append(ciphertext, cipherChunk...)
}
return ciphertext
}
func encodeShort(value uint16) []byte {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, value)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Printf("% x", buf.Bytes())
return buf.Bytes()
}
func decodeShort(b []byte) uint16 {
var value uint16
reader := bytes.NewReader(b)
err := binary.Read(reader, binary.BigEndian, &value)
if err != nil {
fmt.Println("binary.Read failed:", err)
}
return value
}
func decrypt(key []byte, iv []byte, ciphertext []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
return nil
}
var dec = cipher.NewCBCDecrypter(block, iv)
var plaintext []byte
for x := 0; x < (len(ciphertext) / CHUNK_SIZE); x++ {
cipherChunk := ciphertext[x*CHUNK_SIZE : (x+1)*CHUNK_SIZE]
plainChunk := make([]byte, len(cipherChunk))
dec.CryptBlocks(plainChunk, cipherChunk)
plaintext = append(plaintext, plainChunk...)
}
lengthBytes := plaintext[0:2]
length := decodeShort(lengthBytes)
rest := plaintext[2:]
if len(rest) > int(length) {
return rest[0:length]
} else {
return rest
}
}