-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom.go
115 lines (104 loc) · 2.62 KB
/
custom.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
package feistel
import (
"errors"
"github.com/cyrildever/feistel/common/padding"
"github.com/cyrildever/feistel/common/utils"
"github.com/cyrildever/feistel/exception"
utls "github.com/cyrildever/go-utls/common/utils"
"github.com/cyrildever/go-utls/common/xor"
)
//--- TYPES
// CustomCipher uses custom keys instead of the SHA-256 hashing function to provide a new key at each round.
// The number of rounds is then determined by the number of keys provided.
// NB: There must be at least two keys.
type CustomCipher struct {
Keys []string
}
//--- METHODS
// Encrypt ...
func (cc CustomCipher) Encrypt(src string) (ciphered []byte, err error) {
if len(cc.Keys) < 2 {
err = exception.NewWrongCipherParametersError()
return
}
if len(src) == 0 {
return
}
// Apply the balanced Feistel cipher
data := padding.Apply([]byte(src))
if len(data)%2 != 0 {
err = errors.New("invalid string length: cannot be split")
return
}
left, right, err := utils.Split(string(data))
if err != nil {
return
}
parts := []string{left, right}
for i := 0; i < len(cc.Keys); i++ {
left = right
rnd, e := cc.round(parts[1], i)
if e != nil {
err = e
return
}
right, err = xor.String(parts[0], rnd)
if err != nil {
return
}
parts = []string{left, right}
}
ciphered = []byte(parts[0] + parts[1])
return
}
// Decrypt ...
func (cc CustomCipher) Decrypt(ciphered []byte) (string, error) {
if len(cc.Keys) < 2 {
return "", exception.NewWrongCipherParametersError()
}
if len(ciphered) == 0 {
return "", nil
}
if len(ciphered)%2 != 0 {
return "", errors.New("invalid obfuscated data")
}
// Apply the balanced Feistel cipher
left, right, err := utils.Split(string(ciphered))
if err != nil {
return "", err
}
for i := 0; i < len(cc.Keys); i++ {
rnd, err := cc.round(left, len(cc.Keys)-i-1)
if err != nil {
return "", err
}
tmp, err := xor.String(right, rnd)
if err != nil {
return "", err
}
right = left
left = tmp
}
return string(padding.Unapply([]byte(left + right))), nil
}
// Feistel implementation
// round is the function applied at each round of the obfuscation process to the right side of the Feistel cipher
func (cc CustomCipher) round(item string, index int) (string, error) {
addition, err := utils.Add(item, utils.Extract(cc.Keys[index], index, len(item)))
if err != nil {
return "", err
}
hashed, err := utils.Hash([]byte(addition))
if err != nil {
return "", err
}
hexHashed := utls.ToHex(hashed)
return utils.Extract(hexHashed, index, len(item)), nil
}
//--- FUNCTIONS
// NewCustomCipher ...
func NewCustomCipher(keys []string) *CustomCipher {
return &CustomCipher{
Keys: keys,
}
}