Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/GHASH.java
38922 views
1
/*
2
* Copyright (c) 2013, 2015, 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.security.ProviderException;
33
34
/**
35
* This class represents the GHASH function defined in NIST 800-38D
36
* under section 6.4. It needs to be constructed w/ a hash subkey, i.e.
37
* block H. Given input of 128-bit blocks, it will process and output
38
* a 128-bit block.
39
*
40
* <p>This function is used in the implementation of GCM mode.
41
*
42
* @since 1.8
43
*/
44
final class GHASH {
45
46
private static long getLong(byte[] buffer, int offset) {
47
long result = 0;
48
int end = offset + 8;
49
for (int i = offset; i < end; ++i) {
50
result = (result << 8) + (buffer[i] & 0xFF);
51
}
52
return result;
53
}
54
55
private static void putLong(byte[] buffer, int offset, long value) {
56
int end = offset + 8;
57
for (int i = end - 1; i >= offset; --i) {
58
buffer[i] = (byte) value;
59
value >>= 8;
60
}
61
}
62
63
private static final int AES_BLOCK_SIZE = 16;
64
65
// Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].
66
private static void blockMult(long[] st, long[] subH) {
67
long Z0 = 0;
68
long Z1 = 0;
69
long V0 = subH[0];
70
long V1 = subH[1];
71
long X;
72
73
// Separate loops for processing state[0] and state[1].
74
X = st[0];
75
for (int i = 0; i < 64; i++) {
76
// Zi+1 = Zi if bit i of x is 0
77
long mask = X >> 63;
78
Z0 ^= V0 & mask;
79
Z1 ^= V1 & mask;
80
81
// Save mask for conditional reduction below.
82
mask = (V1 << 63) >> 63;
83
84
// V = rightshift(V)
85
long carry = V0 & 1;
86
V0 = V0 >>> 1;
87
V1 = (V1 >>> 1) | (carry << 63);
88
89
// Conditional reduction modulo P128.
90
V0 ^= 0xe100000000000000L & mask;
91
X <<= 1;
92
}
93
94
X = st[1];
95
for (int i = 64; i < 127; i++) {
96
// Zi+1 = Zi if bit i of x is 0
97
long mask = X >> 63;
98
Z0 ^= V0 & mask;
99
Z1 ^= V1 & mask;
100
101
// Save mask for conditional reduction below.
102
mask = (V1 << 63) >> 63;
103
104
// V = rightshift(V)
105
long carry = V0 & 1;
106
V0 = V0 >>> 1;
107
V1 = (V1 >>> 1) | (carry << 63);
108
109
// Conditional reduction.
110
V0 ^= 0xe100000000000000L & mask;
111
X <<= 1;
112
}
113
114
// calculate Z128
115
long mask = X >> 63;
116
Z0 ^= V0 & mask;
117
Z1 ^= V1 & mask;
118
119
// Save result.
120
st[0] = Z0;
121
st[1] = Z1;
122
123
}
124
125
/* subkeyH and state are stored in long[] for GHASH intrinsic use */
126
127
// hash subkey H; should not change after the object has been constructed
128
private final long[] subkeyH;
129
130
// buffer for storing hash
131
private final long[] state;
132
133
// variables for save/restore calls
134
private long stateSave0, stateSave1;
135
136
/**
137
* Initializes the cipher in the specified mode with the given key
138
* and iv.
139
*
140
* @param subkeyH the hash subkey
141
*
142
* @exception ProviderException if the given key is inappropriate for
143
* initializing this digest
144
*/
145
GHASH(byte[] subkeyH) throws ProviderException {
146
if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
147
throw new ProviderException("Internal error");
148
}
149
state = new long[2];
150
this.subkeyH = new long[2];
151
this.subkeyH[0] = getLong(subkeyH, 0);
152
this.subkeyH[1] = getLong(subkeyH, 8);
153
}
154
155
/**
156
* Resets the GHASH object to its original state, i.e. blank w/
157
* the same subkey H. Used after digest() is called and to re-use
158
* this object for different data w/ the same H.
159
*/
160
void reset() {
161
state[0] = 0;
162
state[1] = 0;
163
}
164
165
/**
166
* Save the current snapshot of this GHASH object.
167
*/
168
void save() {
169
stateSave0 = state[0];
170
stateSave1 = state[1];
171
}
172
173
/**
174
* Restores this object using the saved snapshot.
175
*/
176
void restore() {
177
state[0] = stateSave0;
178
state[1] = stateSave1;
179
}
180
181
private static void processBlock(byte[] data, int ofs, long[] st, long[] subH) {
182
st[0] ^= getLong(data, ofs);
183
st[1] ^= getLong(data, ofs + 8);
184
blockMult(st, subH);
185
}
186
187
void update(byte[] in) {
188
update(in, 0, in.length);
189
}
190
191
void update(byte[] in, int inOfs, int inLen) {
192
if (inLen == 0) {
193
return;
194
}
195
ghashRangeCheck(in, inOfs, inLen, state, subkeyH);
196
processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyH);
197
}
198
199
private static void ghashRangeCheck(byte[] in, int inOfs, int inLen, long[] st, long[] subH) {
200
if (inLen < 0) {
201
throw new RuntimeException("invalid input length: " + inLen);
202
}
203
if (inOfs < 0) {
204
throw new RuntimeException("invalid offset: " + inOfs);
205
}
206
if (inLen > in.length - inOfs) {
207
throw new RuntimeException("input length out of bound: " +
208
inLen + " > " + (in.length - inOfs));
209
}
210
if (inLen % AES_BLOCK_SIZE != 0) {
211
throw new RuntimeException("input length/block size mismatch: " +
212
inLen);
213
}
214
215
// These two checks are for C2 checking
216
if (st.length != 2) {
217
throw new RuntimeException("internal state has invalid length: " +
218
st.length);
219
}
220
if (subH.length != 2) {
221
throw new RuntimeException("internal subkeyH has invalid length: " +
222
subH.length);
223
}
224
}
225
/*
226
* This is an intrinsified method. The method's argument list must match
227
* the hotspot signature. This method and methods called by it, cannot
228
* throw exceptions or allocate arrays as it will breaking intrinsics
229
*/
230
private static void processBlocks(byte[] data, int inOfs, int blocks, long[] st, long[] subH) {
231
int offset = inOfs;
232
while (blocks > 0) {
233
processBlock(data, offset, st, subH);
234
blocks--;
235
offset += AES_BLOCK_SIZE;
236
}
237
}
238
239
byte[] digest() {
240
byte[] result = new byte[AES_BLOCK_SIZE];
241
putLong(result, 0, state[0]);
242
putLong(result, 8, state[1]);
243
reset();
244
return result;
245
}
246
}
247
248