1 /*
 2  * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8218723
27  * @summary Use SunJCE Mac in SecretKeyFactory PBKDF2 implementation
28  * @library evilprov.jar
29  * @library /test/lib
30  * @run main/othervm SecKeyFacSunJCEPrf
31  */
32 
33 import java.util.Arrays;
34 import java.util.Hex;
35 import javax.crypto.SecretKeyFactory;
36 import javax.crypto.SecretKey;
37 import javax.crypto.spec.PBEKeySpec;
38 import java.security.Provider;
39 import java.security.Security;
40 import com.evilprovider.*;
41 
42 public class SecKeyFacSunJCEPrf {
43 
44     // One of the PBKDF2 HMAC-SHA1 test vectors from RFC 6070
45     private static final byte[] SALT = "salt".getBytes();
46     private static final char[] PASS = "password".toCharArray();
47     private static final int ITER = 4096;
48     private static final byte[] EXP_OUT =
49             Hex.decoder().decode("4B007901B765489ABEAD49D926F721D065A429C1");
50 
51     public static void main(String[] args) throws Exception {
52         // Instantiate the Evil Provider and insert it in the
53         // most-preferred position.
54         Provider evilProv = new EvilProvider();
55         System.out.println("3rd Party Provider: " + evilProv);
56         Security.insertProviderAt(evilProv, 1);
57 
58         SecretKeyFactory pbkdf2 =
59                 SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");
60         PBEKeySpec pbks = new PBEKeySpec(PASS, SALT, ITER, 160);
61 
62         SecretKey secKey1 = pbkdf2.generateSecret(pbks);
63         System.out.println("PBKDF2WithHmacSHA1:\n" +
64                     Hex.encoder().encode(secKey1.getEncoded()));
65         if (Arrays.equals(secKey1.getEncoded(), EXP_OUT)) {
66             System.out.println("Test Vector Passed");
67         } else {
68             System.out.println("Test Vector Failed");
69             System.out.println("Expected Output:\n" +
70                     Hex.encoder().encode(EXP_OUT));
71             throw new RuntimeException();
72         }
73     }
74 }
75