Path: blob/master/test/jdk/com/sun/crypto/provider/TLS/TestPRF12.java
41155 views
/*1* Copyright (c) 2010, 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 631366126* @modules java.base/sun.security.internal.spec27* @summary Basic known-answer-test for TlsPrf 1228*29* Vector obtained from the IETF TLS working group mailing list:30*31* http://www.ietf.org/mail-archive/web/tls/current/msg03416.html32*/3334import java.io.*;35import java.util.*;3637import java.security.Security;38import java.security.Provider;3940import javax.crypto.KeyGenerator;41import javax.crypto.SecretKey;4243import javax.crypto.spec.*;4445import sun.security.internal.spec.*;4647public class TestPRF12 extends Utils {4849private static int PREFIX_LENGTH = "prf-output: ".length();5051public static void main(String[] args) throws Exception {52Provider provider = Security.getProvider("SunJCE");5354InputStream in = new FileInputStream(new File(BASE, "prf12data.txt"));55BufferedReader reader = new BufferedReader(new InputStreamReader(in));5657int n = 0;58int lineNumber = 0;5960byte[] secret = null;61String label = null;62byte[] seed = null;63int length = 0;64String prfAlg = null;65int prfHashLength = 0;66int prfBlockSize = 0;67byte[] output = null;6869while (true) {70String line = reader.readLine();71lineNumber++;72if (line == null) {73break;74}75if (line.startsWith("prf-") == false) {76continue;77}7879String data = line.substring(PREFIX_LENGTH);80if (line.startsWith("prf-secret:")) {81secret = parse(data);82} else if (line.startsWith("prf-label:")) {83label = data;84} else if (line.startsWith("prf-seed:")) {85seed = parse(data);86} else if (line.startsWith("prf-length:")) {87length = Integer.parseInt(data);88} else if (line.startsWith("prf-alg:")) {89prfAlg = data;90switch (prfAlg) {91case "SHA-224":92prfHashLength = 28;93prfBlockSize = 64;94break;95case "SHA-256":96prfHashLength = 32;97prfBlockSize = 64;98break;99case "SHA-384":100prfHashLength = 48;101prfBlockSize = 128;102break;103case "SHA-512":104prfHashLength = 64;105prfBlockSize = 128;106break;107default:108throw new Exception("Unknown Alg in the data.");109}110} else if (line.startsWith("prf-output:")) {111output = parse(data);112113System.out.print(".");114n++;115116KeyGenerator kg =117KeyGenerator.getInstance("SunTls12Prf", provider);118SecretKey inKey;119if (secret == null) {120inKey = null;121} else {122inKey = new SecretKeySpec(secret, "Generic");123}124TlsPrfParameterSpec spec =125new TlsPrfParameterSpec(inKey, label, seed, length,126prfAlg, prfHashLength, prfBlockSize);127kg.init(spec);128SecretKey key = kg.generateKey();129byte[] enc = key.getEncoded();130if (Arrays.equals(output, enc) == false) {131throw new Exception("mismatch line: " + lineNumber);132}133} else {134throw new Exception("Unknown line: " + line);135}136}137if (n == 0) {138throw new Exception("no tests");139}140in.close();141System.out.println();142System.out.println("OK: " + n + " tests");143}144145}146147148