1+ import javax .crypto .KDF ;
2+ import javax .crypto .spec .HKDFParameterSpec ;
3+
4+ public class KDFDataflowTest {
5+ public static void main (String [] args ) throws Exception {
6+ String userInput = args [0 ]; // source
7+ byte [] taintedBytes = userInput .getBytes ();
8+
9+ testBuilderPattern (taintedBytes );
10+ testSeparateBuilder (taintedBytes );
11+ testKDFWithSalt (taintedBytes );
12+ testStaticParameterSpec (taintedBytes );
13+ testCleanUsage ();
14+ }
15+
16+ public static void testBuilderPattern (byte [] taintedIKM ) throws Exception {
17+ HKDFParameterSpec .Builder builder = HKDFParameterSpec .ofExtract ();
18+ builder .addIKM (taintedIKM );
19+ HKDFParameterSpec spec = builder .thenExpand ("info" .getBytes (), 32 );
20+
21+ KDF kdf = KDF .getInstance ("HKDF-SHA256" );
22+ byte [] result = kdf .deriveData (spec );
23+ sink (result ); // should flag
24+ }
25+
26+ public static void testSeparateBuilder (byte [] taintedIKM ) throws Exception {
27+ HKDFParameterSpec .Builder builder1 = HKDFParameterSpec .ofExtract ();
28+ HKDFParameterSpec .Builder builder2 = builder1 .addIKM (taintedIKM );
29+ HKDFParameterSpec spec = builder2 .thenExpand ("info" .getBytes (), 32 );
30+
31+ KDF kdf = KDF .getInstance ("HKDF-SHA256" );
32+ byte [] result = kdf .deriveData (spec );
33+ sink (result ); // should flag
34+ }
35+
36+ public static void sink (Object o ) {}
37+
38+ public static void testKDFWithSalt (byte [] taintedIKM ) throws Exception {
39+ HKDFParameterSpec .Builder builder = HKDFParameterSpec .ofExtract ();
40+ builder .addIKM (taintedIKM );
41+ builder .addSalt ("sensitive-salt" .getBytes ());
42+ HKDFParameterSpec spec = builder .thenExpand ("info" .getBytes (), 32 );
43+
44+ KDF kdf = KDF .getInstance ("HKDF-SHA256" );
45+ byte [] result = kdf .deriveData (spec );
46+ sink (result ); // should flag
47+ }
48+
49+ public static void testStaticParameterSpec (byte [] taintedIKM ) throws Exception {
50+ javax .crypto .spec .SecretKeySpec secretKey = new javax .crypto .spec .SecretKeySpec (taintedIKM , "AES" );
51+ HKDFParameterSpec spec = HKDFParameterSpec .expandOnly (
52+ secretKey , "info" .getBytes (), 32 );
53+
54+ KDF kdf = KDF .getInstance ("HKDF-SHA256" );
55+ byte [] result = kdf .deriveData (spec );
56+ sink (result ); // should flag
57+ }
58+
59+ public static void testCleanUsage () throws Exception {
60+ byte [] cleanKeyMaterial = "static-key-material" .getBytes ();
61+
62+ HKDFParameterSpec .Builder builder = HKDFParameterSpec .ofExtract ();
63+ builder .addIKM (cleanKeyMaterial ); // clean input
64+ HKDFParameterSpec spec = builder .thenExpand ("info" .getBytes (), 32 );
65+
66+ KDF kdf = KDF .getInstance ("HKDF-SHA256" );
67+ byte [] cleanResult = kdf .deriveData (spec );
68+ sink (cleanResult ); // should NOT flag - no taint source
69+ }
70+ }
0 commit comments