Skip to content

Commit 4ccc558

Browse files
committed
New JUnit tests for org.owasp.esapi.crypto.KeyDerivationFunction class.
1 parent efe07c3 commit 4ccc558

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package org.owasp.esapi.crypto;
2+
3+
import javax.crypto.SecretKey;
4+
import javax.crypto.spec.SecretKeySpec;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.security.InvalidKeyException;
7+
8+
import static org.junit.Assert.*;
9+
import org.junit.BeforeClass;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import junit.framework.JUnit4TestAdapter;
14+
15+
import org.owasp.esapi.crypto.KeyDerivationFunction;
16+
import org.owasp.esapi.crypto.CryptoHelper;
17+
import org.owasp.esapi.errors.EncryptionException;
18+
19+
public class KeyDerivationFunctionTest {
20+
21+
private static SecretKey desKey;
22+
private static SecretKey tdes2key;
23+
private static SecretKey tdes3key;
24+
private static SecretKey aes128key;
25+
private static SecretKey aes192key;
26+
private static SecretKey aes256key;
27+
private static SecretKey shortKey;
28+
29+
private KeyDerivationFunction kdfSha1;
30+
private KeyDerivationFunction kdfSha256;
31+
32+
@BeforeClass
33+
public static void setupStatic() {
34+
try {
35+
desKey = CryptoHelper.generateSecretKey("DES", 56);
36+
tdes2key = CryptoHelper.generateSecretKey("DESede", 112);
37+
tdes3key = CryptoHelper.generateSecretKey("DESede", 168);
38+
aes128key = CryptoHelper.generateSecretKey("AES", 128);
39+
aes128key = CryptoHelper.generateSecretKey("AES", 128);
40+
aes192key = CryptoHelper.generateSecretKey("AES", 192);
41+
aes256key = CryptoHelper.generateSecretKey("AES", 256);
42+
43+
shortKey = new SecretKeySpec(desKey.getEncoded(), 0, 5, "Blowfish"); // 40-bits. Blowfish has var key size
44+
} catch (EncryptionException e) {
45+
fail("Caught unexpected EncryptionException while generating keys; msg was "
46+
+ e.getMessage());
47+
}
48+
}
49+
50+
@Before
51+
public void setup() {
52+
kdfSha1 = new KeyDerivationFunction( KeyDerivationFunction.PRF_ALGORITHMS.HmacSHA1 );
53+
kdfSha256 = new KeyDerivationFunction( KeyDerivationFunction.PRF_ALGORITHMS.HmacSHA256 );
54+
}
55+
56+
@Test(expected = EncryptionException.class)
57+
public void testKeyTooShort() throws EncryptionException {
58+
// System.out.println("testKeyTooShort");
59+
try {
60+
SecretKey key = kdfSha1.computeDerivedKey( shortKey, 128, "encryption" );
61+
fail("testKeyTooShort: Expected IllegalArgumentException to be thrown.");
62+
} catch ( NoSuchAlgorithmException | InvalidKeyException e ) {
63+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
64+
}
65+
}
66+
67+
@Test(expected = IllegalArgumentException.class)
68+
public void testKeySizeTooShort() {
69+
// System.out.println("testKeySizeTooShort");
70+
try {
71+
SecretKey key = kdfSha1.computeDerivedKey( aes128key, 40, "encryption" ); // Min size is 56 bits
72+
fail("testKeySizeTooShort: Expected IllegalArgumentException to be thrown.");
73+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
74+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
75+
}
76+
}
77+
78+
@Test(expected = IllegalArgumentException.class)
79+
public void testNullKey() {
80+
// System.out.println("testNullKey");
81+
try {
82+
SecretKey key = kdfSha1.computeDerivedKey( null, 56, "encryption" ); // Null key disallowed
83+
assertTrue(key == null); // Not reached!
84+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
85+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
86+
}
87+
}
88+
89+
@Test(expected = IllegalArgumentException.class)
90+
public void testKeySizeNotEvenNumberOfBytes() {
91+
// System.out.println("testKeySizeNotEvenNumberOfBytes");
92+
try {
93+
SecretKey key = kdfSha1.computeDerivedKey( aes128key, 60, "encryption" ); // 60 % 8 == 4
94+
assertTrue(key == null); // Not reached!
95+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
96+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
97+
}
98+
}
99+
100+
@Test(expected = IllegalArgumentException.class)
101+
public void testPurposeNull() {
102+
// System.out.println("testPurposeNull");
103+
try {
104+
SecretKey key = kdfSha1.computeDerivedKey( aes128key, 128, null ); // purpose is null
105+
assertTrue(key == null); // Not reached!
106+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
107+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
108+
}
109+
}
110+
111+
@Test(expected = IllegalArgumentException.class)
112+
public void testPurposeEmpty() {
113+
// System.out.println("testPurposeEmpty");
114+
try {
115+
SecretKey key = kdfSha1.computeDerivedKey( aes128key, 128, "" ); // purpose is empty string
116+
assertTrue(key == null); // Not reached!
117+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
118+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
119+
}
120+
}
121+
122+
@Test
123+
public void testSunnyDay() {
124+
// System.out.println("testSunnyDay");
125+
try {
126+
SecretKey key1 = kdfSha1.computeDerivedKey( aes128key, 128, "encryption" );
127+
assertTrue(key1 != null);
128+
assertTrue( key1.getEncoded().length == 128 / 8 );
129+
130+
SecretKey key2 = kdfSha1.computeDerivedKey( aes128key, 128, "authenticity" );
131+
assertTrue(key2 != null);
132+
assertTrue( key2.getEncoded().length == 128 / 8 );
133+
134+
SecretKey key1b = kdfSha1.computeDerivedKey( aes128key, 128, "encryption" );
135+
136+
assertTrue( java.security.MessageDigest.isEqual( key1.getEncoded(), key1b.getEncoded() ) );
137+
assertFalse( java.security.MessageDigest.isEqual( key1.getEncoded(), key2.getEncoded() ) );
138+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
139+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
140+
}
141+
}
142+
143+
@Test
144+
public void testSunnyDay2() { // Two sunny day tests in a row!? This inevitably will fail if run in Columbus, OH.
145+
// System.out.println("testSunnyDay2");
146+
try {
147+
SecretKey key1 = kdfSha256.computeDerivedKey( aes256key, 192, "Why am I here?" );
148+
assertTrue(key1 != null);
149+
assertTrue( key1.getEncoded().length == 192 / 8 );
150+
151+
// Be honest. You thought I was goint to say "42", didn't you?
152+
SecretKey key2 = kdfSha256.computeDerivedKey( aes256key, 192, "No doubt, to annoy people." );
153+
assertTrue(key2 != null);
154+
assertTrue( key2.getEncoded().length == 192 / 8 );
155+
156+
// Should be different because different purpose given for each.
157+
assertFalse( java.security.MessageDigest.isEqual( key1.getEncoded(), key2.getEncoded() ) );
158+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
159+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
160+
}
161+
}
162+
163+
@Test
164+
public void testSetContext() {
165+
// System.out.println("testSetContext");
166+
try {
167+
SecretKey key1 = kdfSha256.computeDerivedKey( aes128key, 128, "encryption" );
168+
assertTrue(key1 != null);
169+
assertTrue( key1.getEncoded().length == 128 / 8 );
170+
171+
SecretKey key2 = kdfSha1.computeDerivedKey( aes128key, 128, "encryption" );
172+
173+
// Should be false because one uses HmacSHA256 and the other uses HmacSHA1
174+
assertFalse( java.security.MessageDigest.isEqual( key1.getEncoded(), key2.getEncoded() ) );
175+
176+
kdfSha256.setContext( "plugh xyzzy" ); // Change context. Originally it is empty string.
177+
SecretKey key1b = kdfSha256.computeDerivedKey( aes128key, 128, "encryption" );
178+
assertTrue(key1b != null);
179+
assertTrue( key1b.getEncoded().length == 128 / 8 );
180+
181+
// Should be false because different contexts used.
182+
assertFalse( java.security.MessageDigest.isEqual( key1.getEncoded(), key1b.getEncoded() ) );
183+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
184+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
185+
}
186+
}
187+
188+
@Test(expected = IllegalArgumentException.class)
189+
public void testSetContextToNull() {
190+
// System.out.println("testSetContextToNull");
191+
try {
192+
SecretKey key1 = kdfSha256.computeDerivedKey( aes128key, 128, "encryption" );
193+
kdfSha256.setContext( null ); // Throws IllegalArgumentExeption
194+
195+
fail("testSetContextToNull: Expected IllegalArgumentException to be thrown.");
196+
} catch ( NoSuchAlgorithmException | InvalidKeyException | EncryptionException e ) {
197+
fail("Caught unexpected exception " + e.getClass().getName() + ": exception msg: " + e);
198+
}
199+
}
200+
201+
/**
202+
* Run all the test cases in this suite. This is to allow running from
203+
* {@code org.owasp.esapi.AllTests} which uses a JUnit 3 test runner.
204+
*/
205+
public static junit.framework.Test suite() {
206+
// System.out.println("In suite()");
207+
return new JUnit4TestAdapter(KeyDerivationFunctionTest.class);
208+
}
209+
}

0 commit comments

Comments
 (0)