Path: blob/master/test/jdk/sun/security/pkcs11/tls/TestPRF.java
41155 views
/*1* Copyright (c) 2005, 2020, 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 /test/lib ..29* @modules java.base/sun.security.internal.spec30* jdk.crypto.cryptoki31* @run main/othervm TestPRF32* @run main/othervm -Djava.security.manager=allow TestPRF sm policy33*/3435import java.io.BufferedReader;36import java.nio.file.Files;37import java.nio.file.Paths;38import java.security.Provider;39import java.util.Arrays;40import javax.crypto.KeyGenerator;41import javax.crypto.SecretKey;42import javax.crypto.spec.SecretKeySpec;43import sun.security.internal.spec.TlsPrfParameterSpec;4445public class TestPRF extends PKCS11Test {4647private static final int PREFIX_LENGTH = "prf-output: ".length();4849public static void main(String[] args) throws Exception {50main(new TestPRF(), args);51}5253@Override54public void main(Provider provider) throws Exception {55if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {56System.out.println("Provider does not support algorithm, skipping");57return;58}5960try (BufferedReader reader = Files.newBufferedReader(61Paths.get(BASE, "prfdata.txt"))) {6263int n = 0;64int lineNumber = 0;6566byte[] secret = null;67String label = null;68byte[] seed = null;69int length = 0;70byte[] output = null;7172while (true) {73String line = reader.readLine();74lineNumber++;75if (line == null) {76break;77}78if (line.startsWith("prf-") == false) {79continue;80}8182String data = line.substring(PREFIX_LENGTH);83if (line.startsWith("prf-secret:")) {84secret = parse(data);85} else if (line.startsWith("prf-label:")) {86label = data;87} else if (line.startsWith("prf-seed:")) {88seed = parse(data);89} else if (line.startsWith("prf-length:")) {90length = Integer.parseInt(data);91} else if (line.startsWith("prf-output:")) {92output = parse(data);9394System.out.print(".");95n++;9697KeyGenerator kg =98KeyGenerator.getInstance("SunTlsPrf", provider);99SecretKey inKey;100if (secret == null) {101inKey = null;102} else {103inKey = new SecretKeySpec(secret, "Generic");104}105TlsPrfParameterSpec spec =106new TlsPrfParameterSpec(inKey, label, seed, length,107null, -1, -1);108SecretKey key;109try {110kg.init(spec);111key = kg.generateKey();112} catch (Exception e) {113System.out.println();114throw new Exception("Error on line: " + lineNumber, e);115}116byte[] enc = key.getEncoded();117if (Arrays.equals(output, enc) == false) {118System.out.println();119System.out.println("expected: " + toString(output));120System.out.println("actual: " + toString(enc));121throw new Exception("mismatch line: " + lineNumber);122}123} else {124throw new Exception("Unknown line: " + line);125}126}127if (n == 0) {128throw new Exception("no tests");129}130System.out.println();131System.out.println("OK: " + n + " tests");132}133}134135}136137138