Path: blob/master/test/jdk/javax/crypto/SecretKeyFactory/SecKeyFacSunJCEPrf.java
41149 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 821872326* @summary Use SunJCE Mac in SecretKeyFactory PBKDF2 implementation27* @library evilprov.jar28* @library /test/lib29* @run main/othervm SecKeyFacSunJCEPrf30*/3132import java.util.Arrays;33import java.util.HexFormat;34import javax.crypto.SecretKeyFactory;35import javax.crypto.SecretKey;36import javax.crypto.spec.PBEKeySpec;37import java.security.Provider;38import java.security.Security;39import com.evilprovider.*;4041public class SecKeyFacSunJCEPrf {4243// One of the PBKDF2 HMAC-SHA1 test vectors from RFC 607044private static final byte[] SALT = "salt".getBytes();45private static final char[] PASS = "password".toCharArray();46private static final int ITER = 4096;47private static final byte[] EXP_OUT =48HexFormat.of().parseHex("4B007901B765489ABEAD49D926F721D065A429C1");4950public static void main(String[] args) throws Exception {51// Instantiate the Evil Provider and insert it in the52// most-preferred position.53Provider evilProv = new EvilProvider();54System.out.println("3rd Party Provider: " + evilProv);55Security.insertProviderAt(evilProv, 1);5657SecretKeyFactory pbkdf2 =58SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");59PBEKeySpec pbks = new PBEKeySpec(PASS, SALT, ITER, 160);6061SecretKey secKey1 = pbkdf2.generateSecret(pbks);62System.out.println("PBKDF2WithHmacSHA1:\n" +63HexFormat.of().withUpperCase().formatHex(secKey1.getEncoded()));64if (Arrays.equals(secKey1.getEncoded(), EXP_OUT)) {65System.out.println("Test Vector Passed");66} else {67System.out.println("Test Vector Failed");68System.out.println("Expected Output:\n" +69HexFormat.of().withUpperCase().formatHex(EXP_OUT));70throw new RuntimeException();71}72}73}74757677