Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyOutputSize.java
41152 views
1
/*
2
* Copyright (c) 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
24
/**
25
* @test
26
* @bug 8255410
27
* @summary Check ChaCha20-Poly1305 cipher output size
28
* @library /test/lib ..
29
* @build jdk.test.lib.Convert
30
* @run main TestChaChaPolyOutputSize
31
*/
32
33
import java.nio.ByteBuffer;
34
import java.security.GeneralSecurityException;
35
import java.security.Key;
36
import java.security.SecureRandom;
37
import java.security.Provider;
38
import java.security.NoSuchAlgorithmException;
39
import javax.crypto.Cipher;
40
import javax.crypto.spec.ChaCha20ParameterSpec;
41
import javax.crypto.spec.IvParameterSpec;
42
import javax.crypto.spec.SecretKeySpec;
43
44
public class TestChaChaPolyOutputSize extends PKCS11Test {
45
46
private static final SecureRandom SR = new SecureRandom();
47
48
private static final SecretKeySpec KEY = new SecretKeySpec(new byte[32],
49
"ChaCha20");
50
51
private static final String ALGO = "ChaCha20-Poly1305";
52
53
public static void main(String args[]) throws Exception {
54
main(new TestChaChaPolyOutputSize(), args);
55
}
56
57
@Override
58
public void main(Provider p) throws GeneralSecurityException {
59
System.out.println("Testing " + p.getName());
60
try {
61
Cipher.getInstance(ALGO, p);
62
} catch (NoSuchAlgorithmException nsae) {
63
System.out.println("Skip; no support for " + ALGO);
64
return;
65
}
66
testGetOutSize(p);
67
testMultiPartAEADDec(p);
68
}
69
70
private static void testGetOutSize(Provider p)
71
throws GeneralSecurityException {
72
73
Cipher ccp = Cipher.getInstance(ALGO, p);
74
ccp.init(Cipher.ENCRYPT_MODE, KEY,
75
new IvParameterSpec(getRandBuf(12)));
76
77
// Encryption lengths are calculated as the input length plus the tag
78
// length (16).
79
testOutLen(ccp, 0, 16);
80
testOutLen(ccp, 5, 21);
81
testOutLen(ccp, 5120, 5136);
82
// perform an update, then test with a final block
83
byte[] input = new byte[5120];
84
SR.nextBytes(input);
85
byte[] updateOut = ccp.update(input);
86
testOutLen(ccp, 1024, 1040 +
87
(5120 - (updateOut == null? 0 : updateOut.length)));
88
89
// Decryption lengths are handled differently for AEAD mode. The length
90
// should be zero for anything up to and including the first 16 bytes
91
// (since that's the tag). Anything above that should be the input
92
// length plus any unprocessed input (via update calls), minus the
93
// 16 byte tag.
94
ccp.init(Cipher.DECRYPT_MODE, KEY, new IvParameterSpec(getRandBuf(12)));
95
testOutLen(ccp, 0, 0);
96
testOutLen(ccp, 5, 0);
97
testOutLen(ccp, 16, 0);
98
testOutLen(ccp, 5120, 5104);
99
// Perform an update, then test with the length of a final chunk
100
// of data.
101
updateOut = ccp.update(input);
102
testOutLen(ccp, 1024, 6128 - (updateOut == null? 0 : updateOut.length));
103
}
104
105
private static void testMultiPartAEADDec(Provider p)
106
throws GeneralSecurityException {
107
IvParameterSpec ivps = new IvParameterSpec(getRandBuf(12));
108
109
// Encrypt some data so we can test decryption.
110
byte[] pText = getRandBuf(2048);
111
ByteBuffer pTextBase = ByteBuffer.wrap(pText);
112
113
Cipher enc = Cipher.getInstance(ALGO, p);
114
enc.init(Cipher.ENCRYPT_MODE, KEY, ivps);
115
ByteBuffer ctBuf = ByteBuffer.allocateDirect(
116
enc.getOutputSize(pText.length));
117
enc.doFinal(pTextBase, ctBuf);
118
119
// Create a new direct plain text ByteBuffer which will catch the
120
// decrypted data.
121
ByteBuffer ptBuf = ByteBuffer.allocateDirect(pText.length);
122
123
// Set the cipher text buffer limit to roughly half the data so we can
124
// do an update/final sequence.
125
ctBuf.position(0).limit(1024);
126
127
Cipher dec = Cipher.getInstance(ALGO, p);
128
dec.init(Cipher.DECRYPT_MODE, KEY, ivps);
129
dec.update(ctBuf, ptBuf);
130
System.out.println("CTBuf: " + ctBuf);
131
System.out.println("PTBuf: " + ptBuf);
132
ctBuf.limit(ctBuf.capacity());
133
dec.doFinal(ctBuf, ptBuf);
134
135
// NOTE: do not use flip() which will set limit based on current
136
// position. ptBuf curr pos = 2048 vs pTextBase pos = 0
137
ptBuf.flip();
138
pTextBase.flip();
139
System.out.println("PT Base:" + pTextBase);
140
System.out.println("PT Actual:" + ptBuf);
141
142
if (pTextBase.compareTo(ptBuf) != 0) {
143
StringBuilder sb = new StringBuilder();
144
sb.append("Plaintext mismatch: Original: ").
145
append(pTextBase.toString()).append("\nActual :").
146
append(ptBuf);
147
throw new RuntimeException(sb.toString());
148
}
149
}
150
151
private static void testOutLen(Cipher c, int inLen, int expOut) {
152
int actualOut = c.getOutputSize(inLen);
153
if (actualOut != expOut) {
154
throw new RuntimeException("Cipher " + c + ", in: " + inLen +
155
", expOut: " + expOut + ", actual: " + actualOut);
156
}
157
}
158
159
private static byte[] getRandBuf(int len) {
160
byte[] buf = new byte[len];
161
SR.nextBytes(buf);
162
return buf;
163
}
164
}
165
166