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/KeyGenerator/Test4628062.java
38867 views
1
/*
2
* Copyright (c) 2002, 2012, 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 4628062 4963723
27
* @summary Verify that AES KeyGenerator supports default initialization
28
* when init is not called
29
* @author Valerie Peng
30
*/
31
import java.security.*;
32
import javax.crypto.*;
33
import java.util.*;
34
35
public class Test4628062 {
36
37
private static final int[] AES_SIZES = { 16, 24, 32 }; // in bytes
38
private static final int[] HMACSHA224_SIZES = { 28 };
39
private static final int[] HMACSHA256_SIZES = { 32 };
40
private static final int[] HMACSHA384_SIZES = { 48 };
41
private static final int[] HMACSHA512_SIZES = { 64 };
42
43
public boolean execute(String algo, int[] keySizes) throws Exception {
44
KeyGenerator kg = KeyGenerator.getInstance(algo, "SunJCE");
45
46
// TEST FIX 4628062
47
Key keyWithDefaultSize = kg.generateKey();
48
byte[] encoding = keyWithDefaultSize.getEncoded();
49
int defKeyLen = encoding.length ;
50
if (defKeyLen == 0) {
51
throw new Exception("default key length is 0!");
52
} else if (defKeyLen != keySizes[0]) {
53
throw new Exception("default key length mismatch!");
54
}
55
56
// BONUS TESTS
57
if (keySizes.length > 1) {
58
// 1. call init(int keysize) with various valid key sizes
59
// and see if the generated key is the right size.
60
for (int i=0; i<keySizes.length; i++) {
61
kg.init(keySizes[i]*8); // in bits
62
Key key = kg.generateKey();
63
if (key.getEncoded().length != keySizes[i]) {
64
throw new Exception("key is generated with the wrong length!");
65
}
66
}
67
// 2. call init(int keysize) with invalid key size and see
68
// if the expected InvalidParameterException is thrown.
69
try {
70
kg.init(keySizes[0]*8+1);
71
} catch (InvalidParameterException ex) {
72
} catch (Exception ex) {
73
throw new Exception("wrong exception is thrown for invalid key size!");
74
}
75
}
76
// passed all tests...hooray!
77
return true;
78
}
79
80
public static void main (String[] args) throws Exception {
81
Security.addProvider(new com.sun.crypto.provider.SunJCE());
82
83
Test4628062 test = new Test4628062();
84
String testName = test.getClass().getName();
85
if (test.execute("AES", AES_SIZES)) {
86
System.out.println(testName + ": AES Passed!");
87
}
88
if (test.execute("HmacSHA224", HMACSHA224_SIZES)) {
89
System.out.println(testName + ": HmacSHA224 Passed!");
90
}
91
if (test.execute("HmacSHA256", HMACSHA256_SIZES)) {
92
System.out.println(testName + ": HmacSHA256 Passed!");
93
}
94
if (test.execute("HmacSHA384", HMACSHA384_SIZES)) {
95
System.out.println(testName + ": HmacSHA384 Passed!");
96
}
97
if (test.execute("HmacSHA512", HMACSHA512_SIZES)) {
98
System.out.println(testName + ": HmacSHA512 Passed!");
99
}
100
}
101
}
102
103