Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/GHASH.java
67848 views
1
/*
2
* Copyright (c) 2013, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
/*
26
* (C) Copyright IBM Corp. 2013
27
* Copyright (c) 2015 Red Hat, Inc.
28
*/
29
30
package com.sun.crypto.provider;
31
32
import java.lang.invoke.MethodHandles;
33
import java.lang.invoke.VarHandle;
34
import java.nio.ByteBuffer;
35
import java.nio.ByteOrder;
36
import java.security.ProviderException;
37
38
import jdk.internal.vm.annotation.IntrinsicCandidate;
39
40
/**
41
* This class represents the GHASH function defined in NIST 800-38D
42
* under section 6.4. It needs to be constructed w/ a hash subkey, i.e.
43
* block H. Given input of 128-bit blocks, it will process and output
44
* a 128-bit block.
45
*
46
* <p>This function is used in the implementation of GCM mode.
47
*
48
* @since 1.8
49
*/
50
51
final class GHASH implements Cloneable, GCM {
52
53
private static final int AES_BLOCK_SIZE = 16;
54
55
// Handle for converting byte[] <-> long
56
private static final VarHandle asLongView =
57
MethodHandles.byteArrayViewVarHandle(long[].class,
58
ByteOrder.BIG_ENDIAN);
59
60
// Maximum buffer size rotating ByteBuffer->byte[] intrinsic copy
61
private static final int MAX_LEN = 1024;
62
63
// Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].
64
private static void blockMult(long[] st, long[] subH) {
65
long Z0 = 0;
66
long Z1 = 0;
67
long V0 = subH[0];
68
long V1 = subH[1];
69
long X;
70
71
// Separate loops for processing state[0] and state[1].
72
X = st[0];
73
for (int i = 0; i < 64; i++) {
74
// Zi+1 = Zi if bit i of x is 0
75
long mask = X >> 63;
76
Z0 ^= V0 & mask;
77
Z1 ^= V1 & mask;
78
79
// Save mask for conditional reduction below.
80
mask = (V1 << 63) >> 63;
81
82
// V = rightshift(V)
83
long carry = V0 & 1;
84
V0 = V0 >>> 1;
85
V1 = (V1 >>> 1) | (carry << 63);
86
87
// Conditional reduction modulo P128.
88
V0 ^= 0xe100000000000000L & mask;
89
X <<= 1;
90
}
91
92
X = st[1];
93
for (int i = 64; i < 127; i++) {
94
// Zi+1 = Zi if bit i of x is 0
95
long mask = X >> 63;
96
Z0 ^= V0 & mask;
97
Z1 ^= V1 & mask;
98
99
// Save mask for conditional reduction below.
100
mask = (V1 << 63) >> 63;
101
102
// V = rightshift(V)
103
long carry = V0 & 1;
104
V0 = V0 >>> 1;
105
V1 = (V1 >>> 1) | (carry << 63);
106
107
// Conditional reduction.
108
V0 ^= 0xe100000000000000L & mask;
109
X <<= 1;
110
}
111
112
// calculate Z128
113
long mask = X >> 63;
114
Z0 ^= V0 & mask;
115
Z1 ^= V1 & mask;
116
117
// Save result.
118
st[0] = Z0;
119
st[1] = Z1;
120
121
}
122
123
/* subkeyHtbl and state are stored in long[] for GHASH intrinsic use */
124
125
// hashtable subkeyHtbl holds 2*9 powers of subkeyH computed using
126
// carry-less multiplication
127
private long[] subkeyHtbl;
128
129
// buffer for storing hash
130
private final long[] state;
131
132
/**
133
* Initializes the cipher in the specified mode with the given key
134
* and iv.
135
*
136
* @param subkeyH the hash subkey
137
*
138
* @exception ProviderException if the given key is inappropriate for
139
* initializing this digest
140
*/
141
GHASH(byte[] subkeyH) throws ProviderException {
142
if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
143
throw new ProviderException("Internal error");
144
}
145
state = new long[2];
146
subkeyHtbl = new long[2*9];
147
subkeyHtbl[0] = (long)asLongView.get(subkeyH, 0);
148
subkeyHtbl[1] = (long)asLongView.get(subkeyH, 8);
149
}
150
151
// Cloning constructor
152
private GHASH(GHASH g) {
153
state = g.state.clone();
154
subkeyHtbl = g.subkeyHtbl.clone();
155
}
156
157
@Override
158
public GHASH clone() {
159
return new GHASH(this);
160
}
161
162
private static void processBlock(byte[] data, int ofs, long[] st,
163
long[] subH) {
164
st[0] ^= (long)asLongView.get(data, ofs);
165
st[1] ^= (long)asLongView.get(data, ofs + 8);
166
blockMult(st, subH);
167
}
168
169
int update(byte[] in) {
170
return update(in, 0, in.length);
171
}
172
173
int update(byte[] in, int inOfs, int inLen) {
174
if (inLen == 0) {
175
return 0;
176
}
177
int len = inLen - (inLen % AES_BLOCK_SIZE);
178
ghashRangeCheck(in, inOfs, len, state, subkeyHtbl);
179
processBlocks(in, inOfs, len / AES_BLOCK_SIZE, state, subkeyHtbl);
180
return len;
181
}
182
183
// Will process as many blocks it can and will leave the remaining.
184
int update(ByteBuffer ct, int inLen) {
185
inLen -= (inLen % AES_BLOCK_SIZE);
186
if (inLen == 0) {
187
return 0;
188
}
189
190
// If ct is a direct bytebuffer, send it directly to the intrinsic
191
if (ct.isDirect()) {
192
int processed = inLen;
193
processBlocksDirect(ct, inLen);
194
return processed;
195
} else if (!ct.isReadOnly()) {
196
// If a non-read only heap bytebuffer, use the array update method
197
int processed = update(ct.array(),
198
ct.arrayOffset() + ct.position(),
199
inLen);
200
ct.position(ct.position() + processed);
201
return processed;
202
}
203
204
// Read only heap bytebuffers have to be copied and operated on
205
int to_process = inLen;
206
byte[] in = new byte[Math.min(MAX_LEN, inLen)];
207
while (to_process > MAX_LEN ) {
208
ct.get(in, 0, MAX_LEN);
209
update(in, 0 , MAX_LEN);
210
to_process -= MAX_LEN;
211
}
212
ct.get(in, 0, to_process);
213
update(in, 0, to_process);
214
return inLen;
215
}
216
217
int doFinal(ByteBuffer src, int inLen) {
218
int processed = 0;
219
220
if (inLen >= AES_BLOCK_SIZE) {
221
processed = update(src, inLen);
222
}
223
224
if (inLen == processed) {
225
return processed;
226
}
227
byte[] block = new byte[AES_BLOCK_SIZE];
228
src.get(block, 0, inLen - processed);
229
update(block, 0, AES_BLOCK_SIZE);
230
return inLen;
231
}
232
233
int doFinal(byte[] in, int inOfs, int inLen) {
234
int remainder = inLen % AES_BLOCK_SIZE;
235
inOfs += update(in, inOfs, inLen - remainder);
236
if (remainder > 0) {
237
byte[] block = new byte[AES_BLOCK_SIZE];
238
System.arraycopy(in, inOfs, block, 0,
239
remainder);
240
update(block, 0, AES_BLOCK_SIZE);
241
}
242
return inLen;
243
}
244
245
private static void ghashRangeCheck(byte[] in, int inOfs, int inLen,
246
long[] st, long[] subH) {
247
if (inLen < 0) {
248
throw new RuntimeException("invalid input length: " + inLen);
249
}
250
if (inOfs < 0) {
251
throw new RuntimeException("invalid offset: " + inOfs);
252
}
253
if (inLen > in.length - inOfs) {
254
throw new RuntimeException("input length out of bound: " +
255
inLen + " > " + (in.length - inOfs));
256
}
257
if (inLen % AES_BLOCK_SIZE != 0) {
258
throw new RuntimeException("input length/block size mismatch: " +
259
inLen);
260
}
261
262
// These two checks are for C2 checking
263
if (st.length != 2) {
264
throw new RuntimeException("internal state has invalid length: " +
265
st.length);
266
}
267
if (subH.length != 18) {
268
throw new RuntimeException("internal subkeyHtbl has invalid length: " +
269
subH.length);
270
}
271
}
272
/*
273
* This is an intrinsified method. The method's argument list must match
274
* the hotspot signature. This method and methods called by it, cannot
275
* throw exceptions or allocate arrays as it will breaking intrinsics
276
*/
277
@IntrinsicCandidate
278
private static void processBlocks(byte[] data, int inOfs, int blocks,
279
long[] st, long[] subH) {
280
int offset = inOfs;
281
while (blocks > 0) {
282
processBlock(data, offset, st, subH);
283
blocks--;
284
offset += AES_BLOCK_SIZE;
285
}
286
}
287
288
// ProcessBlock for Direct ByteBuffers
289
private void processBlocksDirect(ByteBuffer ct, int inLen) {
290
byte[] data = new byte[Math.min(MAX_LEN, inLen)];
291
while (inLen > MAX_LEN) {
292
ct.get(data, 0, MAX_LEN);
293
processBlocks(data, 0, MAX_LEN / AES_BLOCK_SIZE, state,
294
subkeyHtbl);
295
inLen -= MAX_LEN;
296
}
297
if (inLen >= AES_BLOCK_SIZE) {
298
int len = inLen - (inLen % AES_BLOCK_SIZE);
299
ct.get(data, 0, len);
300
processBlocks(data, 0, len / AES_BLOCK_SIZE, state,
301
subkeyHtbl);
302
}
303
}
304
305
byte[] digest() {
306
byte[] result = new byte[AES_BLOCK_SIZE];
307
asLongView.set(result, 0, state[0]);
308
asLongView.set(result, 8, state[1]);
309
// Reset state
310
state[0] = 0;
311
state[1] = 0;
312
return result;
313
}
314
315
316
/**
317
* None of the out or dst values are necessary, they are to satisfy the
318
* GCM interface requirement
319
*/
320
@Override
321
public int update(byte[] in, int inOfs, int inLen, byte[] out, int outOfs) {
322
return update(in, inOfs, inLen);
323
}
324
325
@Override
326
public int update(byte[] in, int inOfs, int inLen, ByteBuffer dst) {
327
return update(in, inOfs, inLen);
328
}
329
330
@Override
331
public int update(ByteBuffer src, ByteBuffer dst) {
332
return update(src, src.remaining());
333
}
334
335
@Override
336
public int doFinal(byte[] in, int inOfs, int inLen, byte[] out,
337
int outOfs) {
338
return doFinal(in, inOfs, inLen);
339
}
340
341
@Override
342
public int doFinal(ByteBuffer src, ByteBuffer dst) {
343
return doFinal(src, src.remaining());
344
}
345
}
346
347