Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/spec/GCMParameterSpec/GCMParameterSpecTest.java
41154 views
1
/*
2
* Copyright (c) 2011, 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 7031343
27
* @summary Provide API changes to support GCM AEAD ciphers
28
* @author Brad Wetmore
29
*/
30
31
import javax.crypto.AEADBadTagException;
32
import javax.crypto.spec.GCMParameterSpec;
33
import java.util.Arrays;
34
35
public class GCMParameterSpecTest {
36
37
// 16 elements
38
private static byte[] bytes = new byte[] {
39
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
40
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
41
42
private static int failed = 0;
43
44
public static void main(String[] args) throws Exception {
45
newGCMParameterSpecFail(-1, bytes);
46
newGCMParameterSpecFail(128, null);
47
newGCMParameterSpecPass(128, bytes);
48
49
newGCMParameterSpecFail(-1, bytes, 2, 4);
50
newGCMParameterSpecFail(128, null, 2, 4);
51
newGCMParameterSpecFail(128, bytes, -2, 4);
52
newGCMParameterSpecFail(128, bytes, 2, -4);
53
newGCMParameterSpecFail(128, bytes, 2, 15); // one too many
54
55
newGCMParameterSpecPass(128, bytes, 2, 14); // ok.
56
newGCMParameterSpecPass(96, bytes, 4, 4);
57
newGCMParameterSpecPass(96, bytes, 0, 0);
58
59
// Might as well check the Exception constructors.
60
try {
61
new AEADBadTagException();
62
new AEADBadTagException("Bad Tag Seen");
63
} catch (Exception e) {
64
e.printStackTrace();
65
failed++;
66
}
67
68
if (failed != 0) {
69
throw new Exception("Test(s) failed");
70
}
71
}
72
73
private static void newGCMParameterSpecPass(
74
int tLen, byte[] src) {
75
try {
76
GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
77
if (gcmps.getTLen() != tLen) {
78
throw new Exception("tLen's not equal");
79
}
80
if (!Arrays.equals(gcmps.getIV(), src)) {
81
throw new Exception("IV's not equal");
82
}
83
} catch (Exception e) {
84
e.printStackTrace();
85
failed++;
86
}
87
}
88
89
private static void newGCMParameterSpecFail(
90
int tLen, byte[] src) {
91
try {
92
new GCMParameterSpec(tLen, src);
93
new Exception("Didn't Fail as Expected").printStackTrace();
94
failed++;
95
} catch (IllegalArgumentException e) {
96
// swallow
97
}
98
}
99
100
private static void newGCMParameterSpecPass(
101
int tLen, byte[] src, int offset, int len) {
102
try {
103
GCMParameterSpec gcmps =
104
new GCMParameterSpec(tLen, src, offset, len);
105
if (gcmps.getTLen() != tLen) {
106
throw new Exception("tLen's not equal");
107
}
108
if (!Arrays.equals(gcmps.getIV(),
109
Arrays.copyOfRange(src, offset, offset + len))) {
110
System.out.println(offset + " " + len);
111
System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
112
throw new Exception("IV's not equal");
113
}
114
} catch (Exception e) {
115
e.printStackTrace();
116
failed++;
117
}
118
}
119
120
private static void newGCMParameterSpecFail(
121
int tLen, byte[] src, int offset, int len) {
122
try {
123
new GCMParameterSpec(tLen, src, offset, len);
124
new Exception("Didn't Fail as Expected").printStackTrace();
125
failed++;
126
} catch (IllegalArgumentException e) {
127
// swallow
128
}
129
}
130
}
131
132