Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/TestKeyPairGeneratorExponent.java
41149 views
1
/*
2
* Copyright (c) 2020, 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 8216012
27
* @summary Tests the RSA public key exponent for KeyPairGenerator
28
* @run main/timeout=60 TestKeyPairGeneratorExponent
29
*/
30
31
import java.math.BigInteger;
32
33
import java.security.*;
34
import java.security.interfaces.*;
35
import java.security.spec.*;
36
37
public class TestKeyPairGeneratorExponent {
38
private static int keyLen = 512;
39
40
private static BigInteger[] validExponents = new BigInteger[] {
41
RSAKeyGenParameterSpec.F0,
42
RSAKeyGenParameterSpec.F4,
43
BigInteger.ONE.shiftLeft(keyLen - 1).subtract(BigInteger.ONE)
44
};
45
46
private static BigInteger[] invalidExponents = new BigInteger[] {
47
BigInteger.valueOf(-1),
48
BigInteger.ZERO,
49
BigInteger.ONE,
50
// without this fix, an even value causes an infinite loop
51
BigInteger.valueOf(4)
52
};
53
54
public static void testValidExponents(KeyPairGenerator kpg,
55
BigInteger exponent) {
56
System.out.println("Testing exponent = " + exponent.toString(16));
57
try {
58
kpg.initialize(new RSAKeyGenParameterSpec(keyLen, exponent));
59
kpg.generateKeyPair();
60
System.out.println("OK, key pair generated");
61
} catch(InvalidAlgorithmParameterException iape){
62
throw new RuntimeException("Error: Unexpected Exception: " + iape);
63
}
64
}
65
66
public static void testInvalidExponents(KeyPairGenerator kpg,
67
BigInteger exponent) {
68
System.out.println("Testing exponent = " + exponent.toString(16));
69
try {
70
kpg.initialize(new RSAKeyGenParameterSpec(keyLen, exponent));
71
kpg.generateKeyPair();
72
throw new RuntimeException("Error: Expected IAPE not thrown.");
73
} catch(InvalidAlgorithmParameterException iape){
74
// Expected InvalidAlgorithmParameterException was thrown
75
System.out.println("OK, expected IAPE thrown");
76
} catch(Exception e) {
77
e.printStackTrace();
78
throw new RuntimeException("Error: unexpected exception " + e);
79
}
80
}
81
82
public static void main(String[] args) throws Exception {
83
KeyPairGenerator kpg =
84
KeyPairGenerator.getInstance("RSA", "SunRsaSign");
85
86
for(BigInteger validExponent : validExponents) {
87
testValidExponents(kpg, validExponent);
88
}
89
90
for(BigInteger invalidExponent : invalidExponents) {
91
testInvalidExponents(kpg, invalidExponent);
92
}
93
}
94
}
95
96