1 /* 2 * Copyright (c) 2019, 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 * @build jdk.test.lib.Convert 31 * @run main/othervm SecKeyFacSunJCEPrf 32 */ 33 34 import java.util.Arrays; 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 import jdk.test.lib.Convert; 42 43 public class SecKeyFacSunJCEPrf { 44 45 // One of the PBKDF2 HMAC-SHA1 test vectors from RFC 6070 46 private static final byte[] SALT = "salt".getBytes(); 47 private static final char[] PASS = "password".toCharArray(); 48 private static final int ITER = 4096; 49 private static final byte[] EXP_OUT = Convert.hexStringToByteArray( 50 "4B007901B765489ABEAD49D926F721D065A429C1"); 51 52 public static void main(String[] args) throws Exception { 53 // Instantiate the Evil Provider and insert it in the 54 // most-preferred position. 55 Provider evilProv = new EvilProvider(); 56 System.out.println("3rd Party Provider: " + evilProv); 57 Security.insertProviderAt(evilProv, 1); 58 59 SecretKeyFactory pbkdf2 = 60 SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE"); 61 PBEKeySpec pbks = new PBEKeySpec(PASS, SALT, ITER, 160); 62 63 SecretKey secKey1 = pbkdf2.generateSecret(pbks); 64 System.out.println("PBKDF2WithHmacSHA1:\n" + 65 Convert.byteArrayToHexString(secKey1.getEncoded())); 66 if (Arrays.equals(secKey1.getEncoded(), EXP_OUT)) { 67 System.out.println("Test Vector Passed"); 68 } else { 69 System.out.println("Test Vector Failed"); 70 System.out.println("Expected Output:\n" + 71 Convert.byteArrayToHexString(EXP_OUT)); 72 throw new RuntimeException(); 73 } 74 } 75 } 76