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