Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestPRF.java
38867 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*/2930import java.io.*;31import java.util.*;3233import java.security.Security;34import java.security.Provider;3536import javax.crypto.KeyGenerator;37import javax.crypto.SecretKey;3839import javax.crypto.spec.*;4041import sun.security.internal.spec.*;4243public class TestPRF extends Utils {4445private static int PREFIX_LENGTH = "prf-output: ".length();4647public static void main(String[] args) throws Exception {48Provider provider = Security.getProvider("SunJCE");4950InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));51BufferedReader reader = new BufferedReader(new InputStreamReader(in));5253int n = 0;54int lineNumber = 0;5556byte[] secret = null;57String label = null;58byte[] seed = null;59int length = 0;60byte[] output = null;6162while (true) {63String line = reader.readLine();64lineNumber++;65if (line == null) {66break;67}68if (line.startsWith("prf-") == false) {69continue;70}7172String data = line.substring(PREFIX_LENGTH);73if (line.startsWith("prf-secret:")) {74secret = parse(data);75} else if (line.startsWith("prf-label:")) {76label = data;77} else if (line.startsWith("prf-seed:")) {78seed = parse(data);79} else if (line.startsWith("prf-length:")) {80length = Integer.parseInt(data);81} else if (line.startsWith("prf-output:")) {82output = parse(data);8384System.out.print(".");85n++;8687KeyGenerator kg =88KeyGenerator.getInstance("SunTlsPrf", provider);89SecretKey inKey;90if (secret == null) {91inKey = null;92} else {93inKey = new SecretKeySpec(secret, "Generic");94}95TlsPrfParameterSpec spec =96new TlsPrfParameterSpec(inKey, label, seed, length,97null, -1, -1);98kg.init(spec);99SecretKey key = kg.generateKey();100byte[] enc = key.getEncoded();101if (Arrays.equals(output, enc) == false) {102throw new Exception("mismatch line: " + lineNumber);103}104} else {105throw new Exception("Unknown line: " + line);106}107}108if (n == 0) {109throw new Exception("no tests");110}111in.close();112System.out.println();113System.out.println("OK: " + n + " tests");114}115116}117118119