blob: 823aaa6c60ac71ca14519ec3d61ac95a8cd12815 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6313661
27 * @summary Basic known-answer-test for TlsPrf
28 * @author Andreas Sterbenz
29 */
30
31import java.io.*;
32import java.util.*;
33
34import java.security.Security;
35import java.security.Provider;
36
37import javax.crypto.KeyGenerator;
38import javax.crypto.SecretKey;
39
40import javax.crypto.spec.*;
41
42import sun.security.internal.spec.*;
43
44public class TestPRF extends Utils {
45
46 private static int PREFIX_LENGTH = "prf-output: ".length();
47
48 public static void main(String[] args) throws Exception {
49 Provider provider = Security.getProvider("SunJCE");
50
51 InputStream in = new FileInputStream(new File(BASE, "prfdata.txt"));
52 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
53
54 int n = 0;
55 int lineNumber = 0;
56
57 byte[] secret = null;
58 String label = null;
59 byte[] seed = null;
60 int length = 0;
61 byte[] output = null;
62
63 while (true) {
64 String line = reader.readLine();
65 lineNumber++;
66 if (line == null) {
67 break;
68 }
69 if (line.startsWith("prf-") == false) {
70 continue;
71 }
72
73 String data = line.substring(PREFIX_LENGTH);
74 if (line.startsWith("prf-secret:")) {
75 secret = parse(data);
76 } else if (line.startsWith("prf-label:")) {
77 label = data;
78 } else if (line.startsWith("prf-seed:")) {
79 seed = parse(data);
80 } else if (line.startsWith("prf-length:")) {
81 length = Integer.parseInt(data);
82 } else if (line.startsWith("prf-output:")) {
83 output = parse(data);
84
85 System.out.print(".");
86 n++;
87
88 KeyGenerator kg = KeyGenerator.getInstance("SunTlsPrf", provider);
89 SecretKey inKey;
90 if (secret == null) {
91 inKey = null;
92 } else {
93 inKey = new SecretKeySpec(secret, "Generic");
94 }
95 TlsPrfParameterSpec spec = new TlsPrfParameterSpec(inKey, label, seed, length);
96 kg.init(spec);
97 SecretKey key = kg.generateKey();
98 byte[] enc = key.getEncoded();
99 if (Arrays.equals(output, enc) == false) {
100 throw new Exception("mismatch line: " + lineNumber);
101 }
102 } else {
103 throw new Exception("Unknown line: " + line);
104 }
105 }
106 if (n == 0) {
107 throw new Exception("no tests");
108 }
109 in.close();
110 System.out.println();
111 System.out.println("OK: " + n + " tests");
112 }
113
114}