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