Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestPRF12.java
38867 views
1
/*
2
* Copyright (c) 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 12
28
*
29
* Vector obtained from the IETF TLS working group mailing list:
30
*
31
* http://www.ietf.org/mail-archive/web/tls/current/msg03416.html
32
*/
33
34
import java.io.*;
35
import java.util.*;
36
37
import java.security.Security;
38
import java.security.Provider;
39
40
import javax.crypto.KeyGenerator;
41
import javax.crypto.SecretKey;
42
43
import javax.crypto.spec.*;
44
45
import sun.security.internal.spec.*;
46
47
public class TestPRF12 extends Utils {
48
49
private static int PREFIX_LENGTH = "prf-output: ".length();
50
51
public static void main(String[] args) throws Exception {
52
Provider provider = Security.getProvider("SunJCE");
53
54
InputStream in = new FileInputStream(new File(BASE, "prf12data.txt"));
55
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
56
57
int n = 0;
58
int lineNumber = 0;
59
60
byte[] secret = null;
61
String label = null;
62
byte[] seed = null;
63
int length = 0;
64
String prfAlg = null;
65
int prfHashLength = 0;
66
int prfBlockSize = 0;
67
byte[] output = null;
68
69
while (true) {
70
String line = reader.readLine();
71
lineNumber++;
72
if (line == null) {
73
break;
74
}
75
if (line.startsWith("prf-") == false) {
76
continue;
77
}
78
79
String data = line.substring(PREFIX_LENGTH);
80
if (line.startsWith("prf-secret:")) {
81
secret = parse(data);
82
} else if (line.startsWith("prf-label:")) {
83
label = data;
84
} else if (line.startsWith("prf-seed:")) {
85
seed = parse(data);
86
} else if (line.startsWith("prf-length:")) {
87
length = Integer.parseInt(data);
88
} else if (line.startsWith("prf-alg:")) {
89
prfAlg = data;
90
switch (prfAlg) {
91
case "SHA-224":
92
prfHashLength = 28;
93
prfBlockSize = 64;
94
break;
95
case "SHA-256":
96
prfHashLength = 32;
97
prfBlockSize = 64;
98
break;
99
case "SHA-384":
100
prfHashLength = 48;
101
prfBlockSize = 128;
102
break;
103
case "SHA-512":
104
prfHashLength = 64;
105
prfBlockSize = 128;
106
break;
107
default:
108
throw new Exception("Unknown Alg in the data.");
109
}
110
} else if (line.startsWith("prf-output:")) {
111
output = parse(data);
112
113
System.out.print(".");
114
n++;
115
116
KeyGenerator kg =
117
KeyGenerator.getInstance("SunTls12Prf", provider);
118
SecretKey inKey;
119
if (secret == null) {
120
inKey = null;
121
} else {
122
inKey = new SecretKeySpec(secret, "Generic");
123
}
124
TlsPrfParameterSpec spec =
125
new TlsPrfParameterSpec(inKey, label, seed, length,
126
prfAlg, prfHashLength, prfBlockSize);
127
kg.init(spec);
128
SecretKey key = kg.generateKey();
129
byte[] enc = key.getEncoded();
130
if (Arrays.equals(output, enc) == false) {
131
throw new Exception("mismatch line: " + lineNumber);
132
}
133
} else {
134
throw new Exception("Unknown line: " + line);
135
}
136
}
137
if (n == 0) {
138
throw new Exception("no tests");
139
}
140
in.close();
141
System.out.println();
142
System.out.println("OK: " + n + " tests");
143
}
144
145
}
146
147