Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/GHASH.java
41161 views
1
/*
2
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2015 Red Hat, Inc.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation. Oracle designates this
9
* particular file as subject to the "Classpath" exception as provided
10
* by Oracle in the LICENSE file that accompanied this code.
11
*
12
* This code is distributed in the hope that it will be useful, but WITHOUT
13
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15
* version 2 for more details (a copy is included in the LICENSE file that
16
* accompanied this code).
17
*
18
* You should have received a copy of the GNU General Public License version
19
* 2 along with this work; if not, write to the Free Software Foundation,
20
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
*
22
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23
* or visit www.oracle.com if you need additional information or have any
24
* questions.
25
*/
26
/*
27
* (C) Copyright IBM Corp. 2013
28
*/
29
30
package com.sun.crypto.provider;
31
32
import java.nio.ByteBuffer;
33
import java.security.ProviderException;
34
35
import jdk.internal.vm.annotation.IntrinsicCandidate;
36
37
/**
38
* This class represents the GHASH function defined in NIST 800-38D
39
* under section 6.4. It needs to be constructed w/ a hash subkey, i.e.
40
* block H. Given input of 128-bit blocks, it will process and output
41
* a 128-bit block.
42
*
43
* <p>This function is used in the implementation of GCM mode.
44
*
45
* @since 1.8
46
*/
47
final class GHASH {
48
49
private static long getLong(byte[] buffer, int offset) {
50
long result = 0;
51
int end = offset + 8;
52
for (int i = offset; i < end; ++i) {
53
result = (result << 8) + (buffer[i] & 0xFF);
54
}
55
return result;
56
}
57
58
private static void putLong(byte[] buffer, int offset, long value) {
59
int end = offset + 8;
60
for (int i = end - 1; i >= offset; --i) {
61
buffer[i] = (byte) value;
62
value >>= 8;
63
}
64
}
65
66
private static final int AES_BLOCK_SIZE = 16;
67
68
// Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].
69
private static void blockMult(long[] st, long[] subH) {
70
long Z0 = 0;
71
long Z1 = 0;
72
long V0 = subH[0];
73
long V1 = subH[1];
74
long X;
75
76
// Separate loops for processing state[0] and state[1].
77
X = st[0];
78
for (int i = 0; i < 64; i++) {
79
// Zi+1 = Zi if bit i of x is 0
80
long mask = X >> 63;
81
Z0 ^= V0 & mask;
82
Z1 ^= V1 & mask;
83
84
// Save mask for conditional reduction below.
85
mask = (V1 << 63) >> 63;
86
87
// V = rightshift(V)
88
long carry = V0 & 1;
89
V0 = V0 >>> 1;
90
V1 = (V1 >>> 1) | (carry << 63);
91
92
// Conditional reduction modulo P128.
93
V0 ^= 0xe100000000000000L & mask;
94
X <<= 1;
95
}
96
97
X = st[1];
98
for (int i = 64; i < 127; i++) {
99
// Zi+1 = Zi if bit i of x is 0
100
long mask = X >> 63;
101
Z0 ^= V0 & mask;
102
Z1 ^= V1 & mask;
103
104
// Save mask for conditional reduction below.
105
mask = (V1 << 63) >> 63;
106
107
// V = rightshift(V)
108
long carry = V0 & 1;
109
V0 = V0 >>> 1;
110
V1 = (V1 >>> 1) | (carry << 63);
111
112
// Conditional reduction.
113
V0 ^= 0xe100000000000000L & mask;
114
X <<= 1;
115
}
116
117
// calculate Z128
118
long mask = X >> 63;
119
Z0 ^= V0 & mask;
120
Z1 ^= V1 & mask;
121
122
// Save result.
123
st[0] = Z0;
124
st[1] = Z1;
125
126
}
127
128
/* subkeyHtbl and state are stored in long[] for GHASH intrinsic use */
129
130
// hashtable subkeyHtbl; holds 2*9 powers of subkeyH computed using carry-less multiplication
131
private long[] subkeyHtbl;
132
133
// buffer for storing hash
134
private final long[] state;
135
136
// variables for save/restore calls
137
private long stateSave0, stateSave1;
138
139
/**
140
* Initializes the cipher in the specified mode with the given key
141
* and iv.
142
*
143
* @param subkeyH the hash subkey
144
*
145
* @exception ProviderException if the given key is inappropriate for
146
* initializing this digest
147
*/
148
GHASH(byte[] subkeyH) throws ProviderException {
149
if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
150
throw new ProviderException("Internal error");
151
}
152
state = new long[2];
153
subkeyHtbl = new long[2*9];
154
subkeyHtbl[0] = getLong(subkeyH, 0);
155
subkeyHtbl[1] = getLong(subkeyH, 8);
156
}
157
158
/**
159
* Resets the GHASH object to its original state, i.e. blank w/
160
* the same subkey H. Used after digest() is called and to re-use
161
* this object for different data w/ the same H.
162
*/
163
void reset() {
164
state[0] = 0;
165
state[1] = 0;
166
}
167
168
/**
169
* Save the current snapshot of this GHASH object.
170
*/
171
void save() {
172
stateSave0 = state[0];
173
stateSave1 = state[1];
174
}
175
176
/**
177
* Restores this object using the saved snapshot.
178
*/
179
void restore() {
180
state[0] = stateSave0;
181
state[1] = stateSave1;
182
}
183
184
private static void processBlock(byte[] data, int ofs, long[] st, long[] subH) {
185
st[0] ^= getLong(data, ofs);
186
st[1] ^= getLong(data, ofs + 8);
187
blockMult(st, subH);
188
}
189
190
void update(byte[] in) {
191
update(in, 0, in.length);
192
}
193
194
void update(byte[] in, int inOfs, int inLen) {
195
if (inLen == 0) {
196
return;
197
}
198
ghashRangeCheck(in, inOfs, inLen, state, subkeyHtbl);
199
processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyHtbl);
200
}
201
202
// Maximum buffer size rotating ByteBuffer->byte[] intrinsic copy
203
private static final int MAX_LEN = 1024;
204
205
// Will process as many blocks it can and will leave the remaining.
206
int update(ByteBuffer src, int inLen) {
207
inLen -= (inLen % AES_BLOCK_SIZE);
208
if (inLen == 0) {
209
return 0;
210
}
211
212
int processed = inLen;
213
byte[] in = new byte[Math.min(MAX_LEN, inLen)];
214
while (processed > MAX_LEN ) {
215
src.get(in, 0, MAX_LEN);
216
update(in, 0 , MAX_LEN);
217
processed -= MAX_LEN;
218
}
219
src.get(in, 0, processed);
220
update(in, 0, processed);
221
return inLen;
222
}
223
224
void doLastBlock(ByteBuffer src, int inLen) {
225
int processed = update(src, inLen);
226
if (inLen == processed) {
227
return;
228
}
229
byte[] block = new byte[AES_BLOCK_SIZE];
230
src.get(block, 0, inLen - processed);
231
update(block, 0, AES_BLOCK_SIZE);
232
}
233
234
private static void ghashRangeCheck(byte[] in, int inOfs, int inLen, long[] st, long[] subH) {
235
if (inLen < 0) {
236
throw new RuntimeException("invalid input length: " + inLen);
237
}
238
if (inOfs < 0) {
239
throw new RuntimeException("invalid offset: " + inOfs);
240
}
241
if (inLen > in.length - inOfs) {
242
throw new RuntimeException("input length out of bound: " +
243
inLen + " > " + (in.length - inOfs));
244
}
245
if (inLen % AES_BLOCK_SIZE != 0) {
246
throw new RuntimeException("input length/block size mismatch: " +
247
inLen);
248
}
249
250
// These two checks are for C2 checking
251
if (st.length != 2) {
252
throw new RuntimeException("internal state has invalid length: " +
253
st.length);
254
}
255
if (subH.length != 18) {
256
throw new RuntimeException("internal subkeyHtbl has invalid length: " +
257
subH.length);
258
}
259
}
260
/*
261
* This is an intrinsified method. The method's argument list must match
262
* the hotspot signature. This method and methods called by it, cannot
263
* throw exceptions or allocate arrays as it will breaking intrinsics
264
*/
265
@IntrinsicCandidate
266
private static void processBlocks(byte[] data, int inOfs, int blocks, long[] st, long[] subH) {
267
int offset = inOfs;
268
while (blocks > 0) {
269
processBlock(data, offset, st, subH);
270
blocks--;
271
offset += AES_BLOCK_SIZE;
272
}
273
}
274
275
byte[] digest() {
276
byte[] result = new byte[AES_BLOCK_SIZE];
277
putLong(result, 0, state[0]);
278
putLong(result, 8, state[1]);
279
reset();
280
return result;
281
}
282
}
283
284