-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpe.go
209 lines (188 loc) · 4.81 KB
/
fpe.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
package feistel
import (
"encoding/binary"
"math/bits"
"github.com/cyrildever/feistel/common/utils"
"github.com/cyrildever/feistel/common/utils/base256"
"github.com/cyrildever/feistel/common/utils/hash"
"github.com/cyrildever/feistel/exception"
utls "github.com/cyrildever/go-utls/common/utils"
"github.com/cyrildever/go-utls/common/xor"
)
//--- TYPES
// FPECipher builds a format-preserving encrypted cipher using the key with the passed hash engine at each round.
// NB: There must be at least 2 rounds.
type FPECipher struct {
Engine hash.Engine
Key string
Rounds int
}
//--- METHODS
// Encrypt ...
func (f FPECipher) Encrypt(src string) (ciphered base256.Readable, err error) {
if len(f.Key) == 0 || f.Rounds < 2 || !hash.IsAvailableEngine(f.Engine) {
err = exception.NewWrongCipherParametersError()
return
}
if len(src) == 0 {
return
}
// Apply the FPE Feistel cipher
left, right, err := utils.Split(src)
if err != nil {
return
}
parts := []string{left, right}
for i := 0; i < f.Rounds; i++ {
left = right
if len(parts[1]) < len(parts[0]) {
neutral := xor.Neutral("0")
parts[1] += string(neutral)
}
rnd, e := f.round(parts[1], i)
if e != nil {
err = e
return
}
tmp := parts[0]
crop := false
if len(tmp)+1 == len(rnd) {
neutral := xor.Neutral(rnd[len(tmp):])
tmp += string(neutral)
crop = true
}
right, err = xor.String(tmp, rnd)
if err != nil {
return
}
if crop {
right = right[:len(right)-1]
}
parts = []string{left, right}
}
ciphered = base256.ToBase256Readable([]byte(parts[0] + parts[1]))
return
}
// EncryptNumber ...
func (f FPECipher) EncryptNumber(src uint64) (ciphered base256.Readable, err error) {
if len(f.Key) == 0 || f.Rounds < 2 || !hash.IsAvailableEngine(f.Engine) {
err = exception.NewWrongCipherParametersError()
return
}
if src < 256 {
bytes := make([]byte, 1)
bytes = append(bytes, uint64ToBytes(src)...)
res, e := f.Encrypt(string(bytes))
if e != nil {
err = e
return
}
ciphered = res
err = exception.NewTooSmallToPreserveLengthError()
return
}
bytes := uint64ToBytes(src)
str := string(bytes)
return f.Encrypt(str)
}
// EncryptString ...
func (f FPECipher) EncryptString(src string) (ciphered base256.Readable, err error) {
return f.Encrypt(src)
}
// Decrypt ...
func (f FPECipher) Decrypt(ciphered base256.Readable) (string, error) {
if len(f.Key) == 0 || f.Rounds < 2 || !hash.IsAvailableEngine(f.Engine) {
return "", exception.NewWrongCipherParametersError()
}
if ciphered.IsEmpty() {
return "", nil
}
// Apply the FPE Feistel cipher
left, right, err := utils.Split(string(ciphered.Bytes()))
if err != nil {
return "", err
}
// Compensating the way Split() works by moving the first byte at right to the end of left if using an odd number of rounds
if f.Rounds%2 != 0 && len(left) != len(right) {
left += right[:1]
right = right[1:]
}
for i := 0; i < f.Rounds; i++ {
leftRound := left
if len(left) < len(right) {
neutral := xor.Neutral("0")
leftRound += string(neutral)
}
rnd, err := f.round(leftRound, f.Rounds-i-1)
if err != nil {
return "", err
}
rightRound := right
extended := false
if len(rightRound)+1 == len(rnd) {
rightRound += left[len(left)-1:]
extended = true
}
tmp, err := xor.String(rightRound, rnd)
if err != nil {
return "", err
}
right = left
if extended {
tmp = tmp[:len(tmp)-1]
}
left = tmp
}
return string([]byte(left + right)), nil
}
// DecryptNumber ...
func (f FPECipher) DecryptNumber(ciphered base256.Readable) (uint64, error) {
deciphered, err := f.Decrypt(ciphered)
if err != nil {
return 0, err
}
return bytesToUint64([]byte(deciphered))
}
// DecryptString ...
func (f FPECipher) DecryptString(ciphered base256.Readable) (string, error) {
return f.Decrypt(ciphered)
}
// Feistel implementation
// round is the function applied at each round of the obfuscation process to the right side of the Feistel cipher
func (f FPECipher) round(item string, index int) (string, error) {
addition, err := utils.Add(item, utils.Extract(f.Key, index, len(item)))
if err != nil {
return "", err
}
hashed, err := hash.H([]byte(addition), f.Engine)
if err != nil {
return "", err
}
hexHashed := utls.ToHex(hashed)
return utils.Extract(hexHashed, index, len(item)), nil
}
//--- FUNCTIONS
// NewFPECipher ...
func NewFPECipher(engine hash.Engine, key string, rounds int) *FPECipher {
return &FPECipher{
Engine: engine,
Key: key,
Rounds: rounds,
}
}
//--- utilities
func uint64ToBytes(x uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, x)
return buf[bits.LeadingZeros64(x)>>3:]
}
func bytesToUint64(x []byte) (uint64, error) {
if len(x) > 8 {
return 0, exception.NewNotUint64Error()
}
buf := make([]byte, 8)
for i, b := range x {
buf[i+8-len(x)] = b
}
return binary.BigEndian.Uint64(buf), nil
}