Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/tls/TestPRF.java
38855 views
/*1* Copyright (c) 2005, 2016, 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 6316539 634525126* @summary Basic known-answer-test for TlsPrf27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestPRF30* @run main/othervm TestPRF sm policy31*/3233import java.io.BufferedReader;34import java.nio.file.Files;35import java.nio.file.Paths;36import java.security.Provider;37import java.util.Arrays;38import javax.crypto.KeyGenerator;39import javax.crypto.SecretKey;40import javax.crypto.spec.SecretKeySpec;41import sun.security.internal.spec.TlsPrfParameterSpec;4243public class TestPRF extends PKCS11Test {4445private static final int PREFIX_LENGTH = "prf-output: ".length();4647public static void main(String[] args) throws Exception {48main(new TestPRF(), args);49}5051@Override52public void main(Provider provider) throws Exception {53if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {54System.out.println("Provider does not support algorithm, skipping");55return;56}5758try (BufferedReader reader = Files.newBufferedReader(59Paths.get(BASE, "prfdata.txt"))) {6061int n = 0;62int lineNumber = 0;6364byte[] secret = null;65String label = null;66byte[] seed = null;67int length = 0;68byte[] output = null;6970while (true) {71String line = reader.readLine();72lineNumber++;73if (line == null) {74break;75}76if (line.startsWith("prf-") == false) {77continue;78}7980String data = line.substring(PREFIX_LENGTH);81if (line.startsWith("prf-secret:")) {82secret = parse(data);83} else if (line.startsWith("prf-label:")) {84label = data;85} else if (line.startsWith("prf-seed:")) {86seed = parse(data);87} else if (line.startsWith("prf-length:")) {88length = Integer.parseInt(data);89} else if (line.startsWith("prf-output:")) {90output = parse(data);9192System.out.print(".");93n++;9495KeyGenerator kg =96KeyGenerator.getInstance("SunTlsPrf", provider);97SecretKey inKey;98if (secret == null) {99inKey = null;100} else {101inKey = new SecretKeySpec(secret, "Generic");102}103TlsPrfParameterSpec spec =104new TlsPrfParameterSpec(inKey, label, seed, length,105null, -1, -1);106SecretKey key;107try {108kg.init(spec);109key = kg.generateKey();110} catch (Exception e) {111if (secret == null) {112// This fails on Solaris, but since we never call this113// API for this case in JSSE, ignore the failure.114// (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE115// mechanism)116System.out.print("X");117continue;118}119System.out.println();120throw new Exception("Error on line: " + lineNumber, e);121}122byte[] enc = key.getEncoded();123if (Arrays.equals(output, enc) == false) {124System.out.println();125System.out.println("expected: " + toString(output));126System.out.println("actual: " + toString(enc));127throw new Exception("mismatch line: " + lineNumber);128}129} else {130throw new Exception("Unknown line: " + line);131}132}133if (n == 0) {134throw new Exception("no tests");135}136System.out.println();137System.out.println("OK: " + n + " tests");138}139}140141}142143144