-
Notifications
You must be signed in to change notification settings - Fork 0
/
FNRCipherTest.java
211 lines (164 loc) · 7.51 KB
/
FNRCipherTest.java
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
/*
* Copyright (c) 2015-2016, Octabase, Ltd. All Rights Reserved.
*
* FNR Cipher is a Java implementation for Flexible Naor and Reingold encryption scheme.
*
* FNR represents "Flexible Naor and Reingold"
*
* FNR is a small domain block cipher to encrypt small domain
* objects ( < 128 bits ) like IPv4, MAC, Credit Card numbers etc.
*
* FNR is designed by Sashank Dara (sadara@cisco.com), Scott Fluhrer (sfluhrer@cisco.com)
*
* Java implementation was written by Mehmet Gurevin (mehmet.gurevin@octabase.com)
*
* Licensed under the GNU Lesser General Public License, Version 2.1 (the "License");
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
package io.octa.security.fnr;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Random;
import org.junit.Assume;
import org.junit.AssumptionViolatedException;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import com.cisco.fnr.FNR;
@RunWith(FNRCipherTest.FNRCipherTestRunner.class)
public class FNRCipherTest extends FNRTestCase {
public static class FNRCipherTestRunner extends BlockJUnit4ClassRunner {
public FNRCipherTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
try {
runLeaf(methodBlock(method), description, notifier);
} catch (AssumptionViolatedException ex) {
notifier.fireTestIgnored(description);
}
}
}
}
@Test
public void testBitLengths() throws GeneralSecurityException {
byte test[] = new byte[128 / 8];
for (int i = 0; i < 128; i++) {
Arrays.fill(test, (byte) 0x00);
int j = 0;
for (; j < (i / 8); j++) {
test[j] = (byte) 0xFF;
}
test[j] = (byte) (0xFF >> (7 - (i % 8)));
FNRKey key = new FNRKey(randomAesKey(), i + 1);
FNRTweak tweak = key.generateTweak("alpha");
byte encrypted[] = FNRCipher.encrypt(key, tweak, test);
assertEquals(test.length, encrypted.length);
byte bitTest[] = new byte[encrypted.length];
System.arraycopy(encrypted, 0, bitTest, 0, encrypted.length);
for (j = 0; j < test.length; j++) {
bitTest[j] |= test[j];
}
assertTrue(Arrays.equals(bitTest, test));
byte decrypted[] = FNRCipher.decrypt(key, tweak, encrypted);
assertEquals(encrypted.length, decrypted.length);
assertTrue(Arrays.equals(test, decrypted));
}
}
@Test
public void fixCoberturaCoverage() throws GeneralSecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<?> privateConstructor = FNRCipher.class.getDeclaredConstructors()[0];
privateConstructor.setAccessible(true);
privateConstructor.newInstance((Object[]) null);
}
@Test(expected = NullPointerException.class)
public void testCodecOperateNullCodecGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 2);
FNRTweak tweak = key.generateTweak("test");
FNRCipher.encrypt(null, key, tweak, new Object());
}
@Test(expected = NullPointerException.class)
public void testCodecOperateNullInputGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 2);
FNRTweak tweak = key.generateTweak("test");
FNRCipher.encrypt(FNRCodec.BOOL, key, tweak, null);
}
@Test(expected = IllegalArgumentException.class)
public void testCodecOperateWrongKeySizeGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 2);
FNRTweak tweak = key.generateTweak("test");
FNRCipher.encrypt(FNRCodec.BOOL, key, tweak, true);
}
@Test(expected = NullPointerException.class)
public void testOperateNullKeyGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 2);
FNRTweak tweak = key.generateTweak("test");
FNRCipher.encrypt(null, tweak, new byte[0]);
}
@Test(expected = NullPointerException.class)
public void testCodecOperateNullTweakGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 2);
FNRCipher.encrypt(key, null, new byte[0]);
}
@Test(expected = NullPointerException.class)
public void testOperateNullInputGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 1);
FNRTweak tweak = key.generateTweak("test");
FNRCipher.encrypt(key, tweak, null);
}
@Test(expected = IllegalArgumentException.class)
public void testOperateWrongInputGuard() throws GeneralSecurityException {
FNRKey key = new FNRKey(randomAesKey(), 9);
FNRTweak tweak = key.generateTweak("test");
FNRCipher.encrypt(key, tweak, new byte[1]);
}
@Test
public void testNativeCompability() throws GeneralSecurityException {
Assume.assumeTrue(System.getProperty("os.name").toLowerCase().startsWith("linux") && System.getProperty("os.arch").contains("64"));
Random rand = new Random();
byte[] aes128Key = randomAesKey();
String tweakValue = "tweak";
byte test[] = new byte[128 / 8];
for (int i = 0; i < 128; i++) {
Arrays.fill(test, (byte) 0x00);
int j = 0;
for (; j < (i / 8); j++) {
test[j] = (byte) rand.nextInt(255);
}
test[j] = (byte) (0xFF >> (7 - (i % 8)));
FNRKey key = new FNRKey(aes128Key, i + 1, false);
FNRTweak tweak = key.generateTweak(tweakValue);
FNRKey keyBuiltIn = new FNRKey(aes128Key, i + 1, true);
FNRTweak tweakBuiltIn = keyBuiltIn.generateTweak(tweakValue);
assertTrue(Arrays.equals(tweak.get(), tweakBuiltIn.get()));
byte encrypted[] = FNRCipher.encrypt(key, tweak, test);
byte encryptedBuiltIn[] = FNRCipher.encrypt(keyBuiltIn, tweakBuiltIn, test);
assertTrue(Arrays.equals(encrypted, encryptedBuiltIn));
FNR jnaCipher = new FNR(aes128Key, tweakValue, i + 1);
byte encryptedJNA[] = jnaCipher.encrypt(test);
assertTrue(Arrays.equals(encrypted, encryptedJNA));
}
}
}