-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff3Token_test.go
309 lines (270 loc) · 7.66 KB
/
ff3Token_test.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
// This test package was dervived from https://github.com/capitalone/fpe/blob/master/ff3/ff3_test.go
// It made sense that it uses it, since this is just a layer on top of it,
// I've added additional error checking since the ff3Token has both input to encrypt and output to decrypt requirements
package ff3Token
import (
"encoding/hex"
"fmt"
"testing"
)
// Test vectors taken from here: http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/FF3samples.pdf
type testVector struct {
key string
tweak string
plaintext string
ciphertext string
encryptError string
decryptError string
}
var testVectors = []testVector{
// simple change in the key, creates a completely different token,
// this simulates multiple environments that can have completely different tokens even if they encrypted the exact same data.
{
"EF4359D8D580AA4F7F036D6F04FC6A94",
"D8E7920AFA330A73",
"4147000000001234", // simulated Visa card
"WIrhsWqFLLbFPWpb",
"", "",
},
{
"EF4359D8D580AA4F7F036D6F04FC6A93",
"D8E7920AFA330A73",
"4147000000001234", // simulated Visa card
"IjKelwlRMiqljyYq",
"", "",
},
{
"EF4359D8D580AA4F7F036D6F04FC6A93",
"D8E7920AFA330A73",
"414700000000123x", // invalid input data this "CC number" has a letter in it
"IjKelwlRMiqljyYx",
"invalid input sent to Encrypt (must be numeric)",
"Decrypt failed to produce numeric output",
},
// test empty string (actual error comes from the FF3 lib, ff3token in theory shouldn't care, )
{
"EF4359D8D580AA4F7F036D6F04FC6A93",
"D8E7920AFA330A73",
"",
"",
"message length is not within min and max bounds",
"message length is not within min and max bounds",
},
{
"",
"",
"",
"",
"key length must be 128, 192, or 256 bits",
"key length must be 128, 192, or 256 bits",
},
{
"EF4359D8D580AA4F7F036D6F04FC6A93",
"",
"",
"",
"tweak must be 8 bytes, or 64 bits",
"tweak must be 8 bytes, or 64 bits",
},
{
"EF4359D8D580AA4F7F036D6F04FC6A94",
"D8E7920AFA330A73",
"4",
"W",
"message length is not within min and max bounds",
"message length is not within min and max bounds",
},
}
func TestEncrypt(t *testing.T) {
for idx, testVector := range testVectors {
sampleNumber := idx + 1
t.Run(fmt.Sprintf("Sample%d", sampleNumber), func(t *testing.T) {
key, err := hex.DecodeString(testVector.key)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("Unable to decode hex key: %v", testVector.key)
}
tweak, err := hex.DecodeString(testVector.tweak)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("Unable to decode tweak: %v", testVector.tweak)
}
cipher, err := NewCipher(key, tweak)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("Unable to create cipher: %v", err)
}
ciphertext, err := cipher.Encrypt(testVector.plaintext)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("%v", err)
}
if ciphertext != testVector.ciphertext {
t.Fatalf("\nSample%d\nKey:\t\t%s\nTweak:\t\t%s\nPlaintext:\t%s\nCiphertext:\t%s\nExpected:\t%s", sampleNumber, testVector.key, testVector.tweak, testVector.plaintext, ciphertext, testVector.ciphertext)
}
})
}
}
func TestDecrypt(t *testing.T) {
for idx, testVector := range testVectors {
sampleNumber := idx + 1
t.Run(fmt.Sprintf("Sample%d", sampleNumber), func(t *testing.T) {
key, err := hex.DecodeString(testVector.key)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("Unable to decode hex key: %v", testVector.key)
}
tweak, err := hex.DecodeString(testVector.tweak)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("Unable to decode tweak: %v", testVector.tweak)
}
ff3, err := NewCipher(key, tweak)
if err != nil {
if testVector.encryptError == err.Error() {
return
}
t.Fatalf("Unable to create cipher: %v", err)
}
plaintext, err := ff3.Decrypt(testVector.ciphertext)
if err != nil {
if testVector.decryptError == err.Error() {
return
}
t.Fatalf("%v", err)
}
if plaintext != testVector.plaintext {
t.Fatalf("\nSample%d\nKey:\t\t%s\nTweak:\t\t%s\nCiphertext:\t%s\nPlaintext:\t%s\nExpected:\t%s", sampleNumber, testVector.key, testVector.tweak, testVector.ciphertext, plaintext, testVector.plaintext)
}
})
}
}
// Note: panic(err) is just used for example purposes.
func ExampleCipher_Encrypt() {
// Key and tweak should be byte arrays. Put your key and tweak here.
// To make it easier for demo purposes, decode from a hex string here.
key, err := hex.DecodeString("EF4359D8D580AA4F7F036D6F04FC6A94")
if err != nil {
panic(err)
}
tweak, err := hex.DecodeString("D8E7920AFA330A73")
if err != nil {
panic(err)
}
// Create a new FF3 cipher "object"
FF3, err := NewCipher(key, tweak)
if err != nil {
panic(err)
}
original := "890121234567890000"
// Call the encryption function on an example test vector
ciphertext, err := FF3.Encrypt(original)
if err != nil {
panic(err)
}
fmt.Println(ciphertext)
// Output: OOGkpxFEKMmCufxYul
}
// Note: panic(err) is just used for example purposes.
func ExampleCipher_Decrypt() {
// Key and tweak should be byte arrays. Put your key and tweak here.
// To make it easier for demo purposes, decode from a hex string here.
key, err := hex.DecodeString("EF4359D8D580AA4F7F036D6F04FC6A94")
if err != nil {
panic(err)
}
tweak, err := hex.DecodeString("D8E7920AFA330A73")
if err != nil {
panic(err)
}
// Create a new FF3 cipher "object"
FF3, err := NewCipher(key, tweak)
if err != nil {
panic(err)
}
ciphertext := "OOGkpxFEKMmCufxYul"
plaintext, err := FF3.Decrypt(ciphertext)
if err != nil {
panic(err)
}
fmt.Println(plaintext)
// Output: 890121234567890000
}
func BenchmarkEncrypt(b *testing.B) {
for idx, testVector := range testVectors {
sampleNumber := idx + 1
b.Run(fmt.Sprintf("Sample%d", sampleNumber), func(b *testing.B) {
key, err := hex.DecodeString(testVector.key)
if err != nil {
b.Fatalf("Unable to decode hex key: %v", testVector.key)
}
tweak, err := hex.DecodeString(testVector.tweak)
if err != nil {
b.Fatalf("Unable to decode tweak: %v", testVector.tweak)
}
ff3, err := NewCipher(key, tweak)
if err != nil {
b.Fatalf("Unable to create cipher: %v", err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
ff3.Encrypt(testVector.plaintext)
}
})
}
}
func BenchmarkEncrypt0(b *testing.B) {
testVector := testVectors[0]
key, err := hex.DecodeString(testVector.key)
if err != nil {
b.Fatalf("Unable to decode hex key: %v", testVector.key)
}
tweak, err := hex.DecodeString(testVector.tweak)
if err != nil {
b.Fatalf("Unable to decode tweak: %v", testVector.tweak)
}
ff3, err := NewCipher(key, tweak)
if err != nil {
b.Fatalf("Unable to create cipher: %v", err)
}
b.Run(fmt.Sprintf("Sample%d", 0), func(b *testing.B) {
for n := 0; n < b.N; n++ {
ff3.Encrypt(testVector.plaintext)
}
})
}
func BenchmarkDecrypt(b *testing.B) {
for idx, testVector := range testVectors {
sampleNumber := idx + 1
b.Run(fmt.Sprintf("Sample%d", sampleNumber), func(b *testing.B) {
key, err := hex.DecodeString(testVector.key)
if err != nil {
b.Fatalf("Unable to decode hex key: %v", testVector.key)
}
tweak, err := hex.DecodeString(testVector.tweak)
if err != nil {
b.Fatalf("Unable to decode tweak: %v", testVector.tweak)
}
ff3, err := NewCipher(key, tweak)
if err != nil {
b.Fatalf("Unable to create cipher: %v", err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
ff3.Decrypt(testVector.ciphertext)
}
})
}
}