1/*
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 6313661
27 * @summary Basic known-answer-test for TlsPrf
28 * @author Andreas Sterbenz
29 * @modules java.base/sun.security.internal.spec
30 */
31
32import java.io.*;
33import java.util.*;
34
35import java.security.Security;
36import java.security.Provider;
37
38import javax.crypto.KeyGenerator;
39import javax.crypto.SecretKey;
40
41import javax.crypto.spec.*;
42
43import sun.security.internal.spec.*;
44
45public class TestPRF extends Utils {
46
47    private static int PREFIX_LENGTH = "prf-output: ".length();
48
49    public static void main(String[] args) throws Exception {
50        Provider provider = Security.getProvider("SunJCE");
51
52        InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));
53        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
54
55        int n = 0;
56        int lineNumber = 0;
57
58        byte[] secret = null;
59        String label = null;
60        byte[] seed = null;
61        int length = 0;
62        byte[] output = null;
63
64        while (true) {
65            String line = reader.readLine();
66            lineNumber++;
67            if (line == null) {
68                break;
69            }
70            if (line.startsWith("prf-") == false) {
71                continue;
72            }
73
74            String data = line.substring(PREFIX_LENGTH);
75            if (line.startsWith("prf-secret:")) {
76                secret = parse(data);
77            } else if (line.startsWith("prf-label:")) {
78                label = data;
79            } else if (line.startsWith("prf-seed:")) {
80                seed = parse(data);
81            } else if (line.startsWith("prf-length:")) {
82                length = Integer.parseInt(data);
83            } else if (line.startsWith("prf-output:")) {
84                output = parse(data);
85
86                System.out.print(".");
87                n++;
88
89                KeyGenerator kg =
90                    KeyGenerator.getInstance("SunTlsPrf", provider);
91                SecretKey inKey;
92                if (secret == null) {
93                    inKey = null;
94                } else {
95                    inKey = new SecretKeySpec(secret, "Generic");
96                }
97                TlsPrfParameterSpec spec =
98                    new TlsPrfParameterSpec(inKey, label, seed, length,
99                        null, -1, -1);
100                kg.init(spec);
101                SecretKey key = kg.generateKey();
102                byte[] enc = key.getEncoded();
103                if (Arrays.equals(output, enc) == false) {
104                    throw new Exception("mismatch line: " + lineNumber);
105                }
106            } else {
107                throw new Exception("Unknown line: " + line);
108            }
109        }
110        if (n == 0) {
111            throw new Exception("no tests");
112        }
113        in.close();
114        System.out.println();
115        System.out.println("OK: " + n + " tests");
116    }
117
118}
119