Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestPRF12.java
38867 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* @summary Basic known-answer-test for TlsPrf 1227*28* Vector obtained from the IETF TLS working group mailing list:29*30* http://www.ietf.org/mail-archive/web/tls/current/msg03416.html31*/3233import java.io.*;34import java.util.*;3536import java.security.Security;37import java.security.Provider;3839import javax.crypto.KeyGenerator;40import javax.crypto.SecretKey;4142import javax.crypto.spec.*;4344import sun.security.internal.spec.*;4546public class TestPRF12 extends Utils {4748private static int PREFIX_LENGTH = "prf-output: ".length();4950public static void main(String[] args) throws Exception {51Provider provider = Security.getProvider("SunJCE");5253InputStream in = new FileInputStream(new File(BASE, "prf12data.txt"));54BufferedReader reader = new BufferedReader(new InputStreamReader(in));5556int n = 0;57int lineNumber = 0;5859byte[] secret = null;60String label = null;61byte[] seed = null;62int length = 0;63String prfAlg = null;64int prfHashLength = 0;65int prfBlockSize = 0;66byte[] output = null;6768while (true) {69String line = reader.readLine();70lineNumber++;71if (line == null) {72break;73}74if (line.startsWith("prf-") == false) {75continue;76}7778String data = line.substring(PREFIX_LENGTH);79if (line.startsWith("prf-secret:")) {80secret = parse(data);81} else if (line.startsWith("prf-label:")) {82label = data;83} else if (line.startsWith("prf-seed:")) {84seed = parse(data);85} else if (line.startsWith("prf-length:")) {86length = Integer.parseInt(data);87} else if (line.startsWith("prf-alg:")) {88prfAlg = data;89switch (prfAlg) {90case "SHA-224":91prfHashLength = 28;92prfBlockSize = 64;93break;94case "SHA-256":95prfHashLength = 32;96prfBlockSize = 64;97break;98case "SHA-384":99prfHashLength = 48;100prfBlockSize = 128;101break;102case "SHA-512":103prfHashLength = 64;104prfBlockSize = 128;105break;106default:107throw new Exception("Unknown Alg in the data.");108}109} else if (line.startsWith("prf-output:")) {110output = parse(data);111112System.out.print(".");113n++;114115KeyGenerator kg =116KeyGenerator.getInstance("SunTls12Prf", provider);117SecretKey inKey;118if (secret == null) {119inKey = null;120} else {121inKey = new SecretKeySpec(secret, "Generic");122}123TlsPrfParameterSpec spec =124new TlsPrfParameterSpec(inKey, label, seed, length,125prfAlg, prfHashLength, prfBlockSize);126kg.init(spec);127SecretKey key = kg.generateKey();128byte[] enc = key.getEncoded();129if (Arrays.equals(output, enc) == false) {130throw new Exception("mismatch line: " + lineNumber);131}132} else {133throw new Exception("Unknown line: " + line);134}135}136if (n == 0) {137throw new Exception("no tests");138}139in.close();140System.out.println();141System.out.println("OK: " + n + " tests");142}143144}145146147