Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ec/ed/EdDSAKeySize.java
41152 views
1
/*
2
* Copyright (c) 2020, 2021, 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
import static javax.crypto.Cipher.PRIVATE_KEY;
24
import static javax.crypto.Cipher.PUBLIC_KEY;
25
import java.security.InvalidKeyException;
26
import java.security.Key;
27
import java.security.KeyFactory;
28
import java.security.KeyPair;
29
import java.security.KeyPairGenerator;
30
import java.security.NoSuchAlgorithmException;
31
import java.security.NoSuchProviderException;
32
import java.security.SecureRandom;
33
import java.security.interfaces.EdECPrivateKey;
34
import java.security.interfaces.EdECPublicKey;
35
import java.security.spec.PKCS8EncodedKeySpec;
36
import java.security.spec.X509EncodedKeySpec;
37
import java.security.spec.EdECPrivateKeySpec;
38
import java.security.spec.EdECPublicKeySpec;
39
import java.security.spec.InvalidKeySpecException;
40
import java.security.spec.NamedParameterSpec;
41
import java.util.Arrays;
42
import java.util.HexFormat;
43
44
import jdk.test.lib.Convert;
45
46
/*
47
* @test
48
* @bug 8209632
49
* @summary Verify KeyLength for EDDSA, ED25519, ED448.
50
* @library /test/lib
51
* @build jdk.test.lib.Convert
52
* @run main EdDSAKeySize
53
*/
54
public class EdDSAKeySize {
55
56
private static final String EDDSA = "EDDSA";
57
private static final String ED25519 = "ED25519";
58
private static final String ED448 = "ED448";
59
private static final String OIDN25519 = "1.3.101.112";
60
private static final String OID25519 = "OID.1.3.101.112";
61
private static final String OIDN448 = "1.3.101.113";
62
private static final String OID448 = "OID.1.3.101.113";
63
private static final String PROVIDER = "SunEC";
64
private static final SecureRandom RND = new SecureRandom(new byte[]{0x1});
65
66
public static void main(String[] args) throws Exception {
67
68
for (boolean initWithRandom : new boolean[]{true, false}) {
69
70
// As per rfc8032 the generated keysize for ED25519 is
71
// 32 octets(256 bits) and ED448 is 57 octets(456 bits).
72
// Case with default parameter
73
testKeyAttributes(PROVIDER, EDDSA, initWithRandom, null, 256);
74
testKeyAttributes(PROVIDER, ED25519, initWithRandom, null, 256);
75
testKeyAttributes(PROVIDER, ED448, initWithRandom, null, 456);
76
77
// With named parameter
78
testKeyAttributes(PROVIDER, EDDSA, initWithRandom, ED25519, 256);
79
testKeyAttributes(PROVIDER, ED25519, initWithRandom, ED25519, 256);
80
testKeyAttributes(PROVIDER, OIDN25519, initWithRandom, ED25519, 256);
81
testKeyAttributes(PROVIDER, OID25519, initWithRandom, ED25519, 256);
82
testKeyAttributes(PROVIDER, ED448, initWithRandom, ED448, 456);
83
testKeyAttributes(PROVIDER, OIDN448, initWithRandom, ED448, 456);
84
testKeyAttributes(PROVIDER, OID448, initWithRandom, ED448, 456);
85
86
// With size parameter
87
testKeyAttributes(PROVIDER, EDDSA, initWithRandom, 255, 256);
88
testKeyAttributes(PROVIDER, ED25519, initWithRandom, 255, 256);
89
testKeyAttributes(PROVIDER, OIDN25519, initWithRandom, 255, 256);
90
testKeyAttributes(PROVIDER, OID25519, initWithRandom, 255, 256);
91
testKeyAttributes(PROVIDER, ED448, initWithRandom, 448, 456);
92
testKeyAttributes(PROVIDER, OIDN448, initWithRandom, 448, 456);
93
testKeyAttributes(PROVIDER, OID448, initWithRandom, 448, 456);
94
}
95
}
96
97
/**
98
* Test standard Key attributes.
99
*/
100
private static void testKeyAttributes(String provider, String name,
101
boolean initWithRandom, Object param, int keySize) throws Exception {
102
103
System.out.printf("Case name: %s, param: %s, Expected keysize: %s, "
104
+ "Initiate with random: %s%n", name, param, keySize,
105
initWithRandom);
106
KeyPairGenerator kpg = KeyPairGenerator.getInstance(name, provider);
107
if (initWithRandom) {
108
if (param instanceof Integer) {
109
kpg.initialize((Integer) param, RND);
110
} else if (param instanceof String) {
111
kpg.initialize(new NamedParameterSpec((String) param), RND);
112
}
113
} else {
114
if (param instanceof Integer) {
115
kpg.initialize((Integer) param);
116
} else if (param instanceof String) {
117
kpg.initialize(new NamedParameterSpec((String) param));
118
}
119
}
120
KeyPair kp = kpg.generateKeyPair();
121
NamedParameterSpec namedSpec = getNamedParamSpec(name);
122
123
// Verify original PrivateKey with it's different representation
124
Key[] privs = manipulateKey(provider, name, PRIVATE_KEY,
125
kp.getPrivate(), namedSpec);
126
Arrays.stream(privs).forEach(
127
priv -> testPrivateKey((EdECPrivateKey) kp.getPrivate(),
128
(EdECPrivateKey) priv, keySize));
129
130
// Verify original PublicKey with it's different representation
131
Key[] pubs = manipulateKey(provider, name, PUBLIC_KEY,
132
kp.getPublic(), namedSpec);
133
Arrays.stream(pubs).forEach(
134
pub -> testPublicKey((EdECPublicKey) kp.getPublic(),
135
(EdECPublicKey) pub));
136
System.out.println("Passed.");
137
}
138
139
private static NamedParameterSpec getNamedParamSpec(String algo) {
140
NamedParameterSpec namedSpec = switch (algo) {
141
case EDDSA
142
, OIDN25519, OID25519 -> new NamedParameterSpec(ED25519);
143
case OIDN448
144
, OID448 -> new NamedParameterSpec(ED448);
145
default->
146
new NamedParameterSpec(algo);
147
};
148
return namedSpec;
149
}
150
151
private static Key[] manipulateKey(String provider, String algo, int type,
152
Key key, NamedParameterSpec namedSpec)
153
throws NoSuchAlgorithmException, InvalidKeySpecException,
154
NoSuchProviderException, InvalidKeyException {
155
156
KeyFactory kf = KeyFactory.getInstance(algo, provider);
157
switch (type) {
158
case PUBLIC_KEY:
159
return new Key[]{
160
kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
161
kf.generatePublic(kf.getKeySpec(
162
key, EdECPublicKeySpec.class)),
163
kf.generatePublic(new EdECPublicKeySpec(namedSpec,
164
((EdECPublicKey) key).getPoint())),
165
kf.translateKey(key)
166
};
167
case PRIVATE_KEY:
168
return new Key[]{
169
kf.generatePrivate(new PKCS8EncodedKeySpec(key.getEncoded())),
170
kf.generatePrivate(
171
kf.getKeySpec(key, EdECPrivateKeySpec.class)),
172
kf.generatePrivate(new EdECPrivateKeySpec(namedSpec,
173
((EdECPrivateKey) key).getBytes().get())),
174
kf.translateKey(key)
175
};
176
}
177
throw new RuntimeException("We shouldn't reach here");
178
}
179
180
/**
181
* Basic PrivateKey Test cases
182
*/
183
private static void testPrivateKey(EdECPrivateKey orig,
184
EdECPrivateKey generated, int size) {
185
186
equals(orig.getBytes().get().length * 8, size);
187
equals(generated.getBytes().get().length * 8, size);
188
equals(orig.getBytes().get(), generated.getBytes().get());
189
equals(orig.getFormat(), generated.getFormat());
190
equals(orig.getEncoded(), generated.getEncoded());
191
equals(((NamedParameterSpec) orig.getParams()).getName(),
192
((NamedParameterSpec) generated.getParams()).getName());
193
}
194
195
/**
196
* Basic PublicKey Test cases
197
*/
198
private static void testPublicKey(EdECPublicKey orig,
199
EdECPublicKey generated) {
200
201
equals(orig.getPoint().getY(), generated.getPoint().getY());
202
equals(orig.getPoint().isXOdd(), generated.getPoint().isXOdd());
203
equals(orig.getFormat(), generated.getFormat());
204
equals(orig.getEncoded(), generated.getEncoded());
205
equals(((NamedParameterSpec) orig.getParams()).getName(),
206
((NamedParameterSpec) generated.getParams()).getName());
207
}
208
209
private static void equals(Object actual, Object expected) {
210
if (!actual.equals(expected)) {
211
throw new RuntimeException(String.format("Actual: %s, Expected: %s",
212
actual, expected));
213
}
214
}
215
216
private static void equals(byte[] actual, byte[] expected) {
217
if (!Arrays.equals(actual, expected)) {
218
throw new RuntimeException(String.format("Actual array: %s, "
219
+ "Expected array:%s", HexFormat.of().withUpperCase().formatHex(actual),
220
HexFormat.of().withUpperCase().formatHex(expected)));
221
}
222
}
223
}
224
225