Path: blob/master/test/jdk/com/sun/crypto/provider/TLS/TestPRF.java
41155 views
/*1* Copyright (c) 2005, 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 TlsPrf27* @author Andreas Sterbenz28* @modules java.base/sun.security.internal.spec29*/3031import java.io.*;32import java.util.*;3334import java.security.Security;35import java.security.Provider;3637import javax.crypto.KeyGenerator;38import javax.crypto.SecretKey;3940import javax.crypto.spec.*;4142import sun.security.internal.spec.*;4344public class TestPRF extends Utils {4546private static int PREFIX_LENGTH = "prf-output: ".length();4748public static void main(String[] args) throws Exception {49Provider provider = Security.getProvider("SunJCE");5051InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));52BufferedReader reader = new BufferedReader(new InputStreamReader(in));5354int n = 0;55int lineNumber = 0;5657byte[] secret = null;58String label = null;59byte[] seed = null;60int length = 0;61byte[] output = null;6263while (true) {64String line = reader.readLine();65lineNumber++;66if (line == null) {67break;68}69if (line.startsWith("prf-") == false) {70continue;71}7273String data = line.substring(PREFIX_LENGTH);74if (line.startsWith("prf-secret:")) {75secret = parse(data);76} else if (line.startsWith("prf-label:")) {77label = data;78} else if (line.startsWith("prf-seed:")) {79seed = parse(data);80} else if (line.startsWith("prf-length:")) {81length = Integer.parseInt(data);82} else if (line.startsWith("prf-output:")) {83output = parse(data);8485System.out.print(".");86n++;8788KeyGenerator kg =89KeyGenerator.getInstance("SunTlsPrf", provider);90SecretKey inKey;91if (secret == null) {92inKey = null;93} else {94inKey = new SecretKeySpec(secret, "Generic");95}96TlsPrfParameterSpec spec =97new TlsPrfParameterSpec(inKey, label, seed, length,98null, -1, -1);99kg.init(spec);100SecretKey key = kg.generateKey();101byte[] enc = key.getEncoded();102if (Arrays.equals(output, enc) == false) {103throw new Exception("mismatch line: " + lineNumber);104}105} else {106throw new Exception("Unknown line: " + line);107}108}109if (n == 0) {110throw new Exception("no tests");111}112in.close();113System.out.println();114System.out.println("OK: " + n + " tests");115}116117}118119120