-
Notifications
You must be signed in to change notification settings - Fork 4
/
aesffx.go
344 lines (287 loc) · 7.8 KB
/
aesffx.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package aesffx
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"
)
const (
numRounds = 10
)
// NewCipher creates a new cipher capable of encrypting and decrypting messages
// using the AES-FFX mode for format-preserving encryption.
func NewCipher(radix uint32, key, tweak []byte) (*FFXCipher, error) {
var maxLength uint32 = (1 << 32) - 1
if radix > 65536 {
return nil, fmt.Errorf("radix must be between 2 and 2^16")
}
if len(key) != 16 {
return nil, fmt.Errorf("key length must be exactly 16 bytes")
}
if uint32(len(tweak)) > maxLength {
return nil, fmt.Errorf("tweak length must be smaller than (2^32) - 1")
}
var minLength uint32 = 2
if radix >= 10 {
minLength = 8
}
return &FFXCipher{
key: key,
tweak: tweak,
radix: radix,
minLength: minLength,
maxLength: maxLength,
}, nil
}
// FFXCipher represents the parameters needed for AES-FFX.
type FFXCipher struct {
key []byte
tweak []byte
radix uint32
minLength uint32
maxLength uint32
}
// Encrypt encrypts the given plaintext, producing ciphertext output.
func (f FFXCipher) Encrypt(src string) (string, error) {
n := uint32(len(src))
l := split(uint32(n))
A := src[:l]
B := src[l:]
for i := 0; i < numRounds; i++ {
fOut, err := f.feistelRound(n, f.tweak, i, B)
if err != nil {
return "", err
}
lmin := min(len(A), len(fOut))
C, err := blockAddition(lmin, int(f.radix), A, fOut)
if err != nil {
return "", nil
}
A = B
B = C
}
cipher := A + B
return cipher, nil
}
// Decrypt decrypts the given ciphertext, producing plaintext output.
func (f FFXCipher) Decrypt(src string) (string, error) {
n := uint32(len(src))
l := split(uint32(n))
A := src[:l]
B := src[l:]
for i := numRounds - 1; i > -1; i-- {
C := B
B = A
fOut, err := f.feistelRound(n, f.tweak, i, B)
if err != nil {
return "", nil
}
lmin := min(len(C), len(fOut))
A, err = blockSubtraction(lmin, int(f.radix), C, fOut)
if err != nil {
return "", nil
}
}
plain := A + B
return plain, nil
}
// blockAddition computes the block-wise radix addition of x and y.
func blockAddition(n, radix int, x, y string) (string, error) {
xInt, err := strconv.ParseInt(x, radix, n*8)
if err != nil {
return "", err
}
yInt, err := strconv.ParseInt(y, radix, n*8)
if err != nil {
return "", err
}
blockSum := (xInt + yInt) % int64(math.Pow(float64(radix), float64(n)))
out := strconv.FormatInt(blockSum, radix)
if len(out) < n {
out = strings.Repeat("0", n-len(out)) + out
}
return out, nil
}
// blockSubtraction computes the block-wise radix subtraction of x and y.
func blockSubtraction(n, radix int, x, y string) (string, error) {
xInt, err := strconv.ParseInt(x, radix, n*8)
if err != nil {
return "", err
}
yInt, err := strconv.ParseInt(y, radix, n*8)
if err != nil {
return "", err
}
diff := xInt - yInt
mod := int64(math.Pow(float64(radix), float64(n)))
blockDiff := diff % mod
if blockDiff < 0 {
blockDiff += mod
}
out := strconv.FormatInt(blockDiff, radix)
if len(out) < n {
out = strings.Repeat("0", n-len(out)) + out
}
return out, nil
}
// feistalRound runs the given block through the modified feistel network.
func (f FFXCipher) feistelRound(msgLength uint32, tweak []byte, roundNum int, block string) (string, error) {
t := len(tweak)
beta := int(math.Ceil(float64(msgLength) / 2))
// b = ceil(ceil(beta * log_2(radix)) / 8)
b := int(math.Ceil(math.Ceil(float64(beta)*math.Log2(float64(f.radix))) / 8))
// d = 4 * ceil(b/4)
d := 4 * int(math.Ceil(float64(b)/4))
var m int
if roundNum%2 == 0 {
m = int(math.Floor(float64(msgLength) / 2))
} else {
m = int(math.Ceil(float64(msgLength) / 2))
}
// p <- [vers] | [method] | [addition] | [radix] | [rnds(n)] | [split(n)] | [n] | [t]
p, err := generateP(msgLength, f.radix, t)
if err != nil {
return "", err
}
// q <- tweak | [0]^((−t−b−1) mod 16) | [roundNum] | [numradix(B)]
err = generateQ(block, p, tweak, t, b, roundNum, f.radix)
if err != nil {
return "", err
}
// Y = CBC-MAC_k(P || Q)
bigY, err := cbcMac(f.key, p.Bytes())
if err != nil {
panic(err)
}
aes, err := aes.NewCipher(f.key)
if err != nil {
return "", nil
}
// Y <- first d+4 bytes of (Y | AESK(Y XOR [1]16) | AESK(Y XOR [2]16) | AESK(Y XOR [3]16)...)
var yTemp bytes.Buffer
c := bytes.NewBuffer(make([]byte, 16))
i := 0
yTemp.Write(bigY)
for yTemp.Len() < (d + 4) {
h, err := hex.DecodeString(strings.Repeat("0"+strconv.Itoa(i), 16))
if err != nil {
return "", nil
}
aes.Encrypt(c.Bytes(), xorBytes(bigY, h))
yTemp.Write(c.Bytes())
i++
c.Reset()
}
// z = y mod r^m
y := binary.BigEndian.Uint64(yTemp.Bytes())
z := y % uint64(math.Pow(float64(f.radix), float64(m)))
fOut := strconv.FormatUint(z, int(f.radix))
// TODO(roasbeef): Factor out into padding funciton
if len(fOut) < beta {
fOut = strings.Repeat("0", beta-len(fOut)) + fOut
}
return fOut, nil
}
// split calculates the index to split the input string for our maximally
// balanced Feistel rounds.
func split(n uint32) uint32 {
return uint32(math.Floor(float64(n) / 2))
}
// cbcMac computes the AES-CBC-MAC of the msg with the given key.
func cbcMac(key, msg []byte) ([]byte, error) {
if len(msg)%16 != 0 {
return nil, fmt.Errorf("message length must be a multiple of 16, got %v", len(msg))
}
if len(key) != 16 {
return nil, fmt.Errorf("key length must be exactly 128-bits")
}
// Create a new aes cipher from our key.
aesBlock, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Initialize aes in CBC mode with a zero IV.
y := make([]byte, 16, 16)
cbc := cipher.NewCBCEncrypter(aesBlock, y)
for i := 0; i < len(msg); i += 16 {
x := msg[i:(i + 16)]
cbc.CryptBlocks(y, x)
}
return y, nil
}
// xorBytes returns a new byte slice which is the result of XOR'ing each byte
// amongst the passed arguments.
func xorBytes(x, y []byte) []byte {
out := make([]byte, len(x))
for i := 0; i < len(x); i++ {
out[i] = y[i] ^ x[i]
}
return out
}
// min returns the minimum of x and y.
func min(x, y int) int {
if x < y {
return x
}
return y
}
// generateP creates the first half of the IV our feistel round.
// This function returns a bytes.Buffer in so Q can easily be concatenated to
// it.
func generateP(mlen uint32, radix uint32, tweaklen int) (*bytes.Buffer, error) {
var p bytes.Buffer
p.Write([]byte{0x01}) // version
p.Write([]byte{0x02}) // method
p.Write([]byte{0x01}) // addition
p.Write([]byte{0x00}) // 0 byte prefix to force 3 bytes
err := binary.Write(&p, binary.BigEndian, uint16(radix)) // write 2 bytes of radix
if err != nil {
return nil, err
}
p.Write([]byte{0x0a}) // number of rounds is 10
err = binary.Write(&p, binary.BigEndian, uint8(split(mlen))) // split
if err != nil {
return nil, err
}
err = binary.Write(&p, binary.BigEndian, mlen)
if err != nil {
return nil, err
}
err = binary.Write(&p, binary.BigEndian, uint32(tweaklen))
if err != nil {
return nil, err
}
return &p, nil
}
func generateQ(block string, buf *bytes.Buffer, tweak []byte, tlen int, blockLen int, i int, radix uint32) error {
buf.Write(tweak)
numPads := ((-1 * tlen) - blockLen - 1) % 16
if numPads < 0 {
numPads += 16
}
zeroPad, err := hex.DecodeString(strings.Repeat("00", numPads))
if err != nil {
return err
}
buf.Write(zeroPad)
err = binary.Write(buf, binary.BigEndian, uint8(i))
if err != nil {
return err
}
var bBuffer bytes.Buffer
radixBlock, err := strconv.ParseUint(block, int(radix), blockLen*8)
if err != nil {
return err
}
err = binary.Write(&bBuffer, binary.BigEndian, radixBlock)
if err != nil {
return err
}
buf.Write(bBuffer.Bytes()[bBuffer.Len()-blockLen:])
return nil
}