author | coffeys |
Thu, 23 Aug 2018 11:37:14 +0100 | |
changeset 51504 | c9a3e3cac9c7 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24573
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.provider; |
|
27 |
||
28 |
import java.security.MessageDigestSpi; |
|
29 |
import java.security.DigestException; |
|
30 |
import java.security.ProviderException; |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
31 |
import java.util.Arrays; |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
32 |
import java.util.Objects; |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
33 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
34 |
import jdk.internal.HotSpotIntrinsicCandidate; |
2 | 35 |
|
36 |
/** |
|
37 |
* Common base message digest implementation for the Sun provider. |
|
38 |
* It implements all the JCA methods as suitable for a Java message digest |
|
39 |
* implementation of an algorithm based on a compression function (as all |
|
40 |
* commonly used algorithms are). The individual digest subclasses only need to |
|
41 |
* implement the following methods: |
|
42 |
* |
|
43 |
* . abstract void implCompress(byte[] b, int ofs); |
|
44 |
* . abstract void implDigest(byte[] out, int ofs); |
|
45 |
* . abstract void implReset(); |
|
46 |
* |
|
47 |
* See the inline documentation for details. |
|
48 |
* |
|
49 |
* @since 1.5 |
|
50 |
* @author Andreas Sterbenz |
|
51 |
*/ |
|
52 |
abstract class DigestBase extends MessageDigestSpi implements Cloneable { |
|
53 |
||
54 |
// one element byte array, temporary storage for update(byte) |
|
55 |
private byte[] oneByte; |
|
56 |
||
57 |
// algorithm name to use in the exception message |
|
58 |
private final String algorithm; |
|
59 |
// length of the message digest in bytes |
|
60 |
private final int digestLength; |
|
61 |
||
62 |
// size of the input to the compression function in bytes |
|
63 |
private final int blockSize; |
|
64 |
// buffer to store partial blocks, blockSize bytes large |
|
65 |
// Subclasses should not access this array directly except possibly in their |
|
66 |
// implDigest() method. See MD5.java as an example. |
|
12685 | 67 |
byte[] buffer; |
2 | 68 |
// offset into buffer |
69 |
private int bufOfs; |
|
70 |
||
71 |
// number of bytes processed so far. subclasses should not modify |
|
72 |
// this value. |
|
73 |
// also used as a flag to indicate reset status |
|
74 |
// -1: need to call engineReset() before next call to update() |
|
75 |
// 0: is already reset |
|
76 |
long bytesProcessed; |
|
77 |
||
78 |
/** |
|
79 |
* Main constructor. |
|
80 |
*/ |
|
81 |
DigestBase(String algorithm, int digestLength, int blockSize) { |
|
82 |
super(); |
|
83 |
this.algorithm = algorithm; |
|
84 |
this.digestLength = digestLength; |
|
85 |
this.blockSize = blockSize; |
|
86 |
buffer = new byte[blockSize]; |
|
87 |
} |
|
88 |
||
89 |
// return digest length. See JCA doc. |
|
90 |
protected final int engineGetDigestLength() { |
|
91 |
return digestLength; |
|
92 |
} |
|
93 |
||
94 |
// single byte update. See JCA doc. |
|
95 |
protected final void engineUpdate(byte b) { |
|
96 |
if (oneByte == null) { |
|
97 |
oneByte = new byte[1]; |
|
98 |
} |
|
99 |
oneByte[0] = b; |
|
100 |
engineUpdate(oneByte, 0, 1); |
|
101 |
} |
|
102 |
||
103 |
// array update. See JCA doc. |
|
104 |
protected final void engineUpdate(byte[] b, int ofs, int len) { |
|
105 |
if (len == 0) { |
|
106 |
return; |
|
107 |
} |
|
108 |
if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) { |
|
109 |
throw new ArrayIndexOutOfBoundsException(); |
|
110 |
} |
|
111 |
if (bytesProcessed < 0) { |
|
112 |
engineReset(); |
|
113 |
} |
|
114 |
bytesProcessed += len; |
|
115 |
// if buffer is not empty, we need to fill it before proceeding |
|
116 |
if (bufOfs != 0) { |
|
117 |
int n = Math.min(len, blockSize - bufOfs); |
|
118 |
System.arraycopy(b, ofs, buffer, bufOfs, n); |
|
119 |
bufOfs += n; |
|
120 |
ofs += n; |
|
121 |
len -= n; |
|
122 |
if (bufOfs >= blockSize) { |
|
123 |
// compress completed block now |
|
124 |
implCompress(buffer, 0); |
|
125 |
bufOfs = 0; |
|
126 |
} |
|
127 |
} |
|
128 |
// compress complete blocks |
|
24573
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
129 |
if (len >= blockSize) { |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
130 |
int limit = ofs + len; |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
131 |
ofs = implCompressMultiBlock(b, ofs, limit - blockSize); |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
132 |
len = limit - ofs; |
2 | 133 |
} |
134 |
// copy remainder to buffer |
|
135 |
if (len > 0) { |
|
136 |
System.arraycopy(b, ofs, buffer, 0, len); |
|
137 |
bufOfs = len; |
|
138 |
} |
|
139 |
} |
|
140 |
||
24573
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
141 |
// compress complete blocks |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
142 |
private int implCompressMultiBlock(byte[] b, int ofs, int limit) { |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
143 |
implCompressMultiBlockCheck(b, ofs, limit); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
144 |
return implCompressMultiBlock0(b, ofs, limit); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
145 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
146 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
147 |
@HotSpotIntrinsicCandidate |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
148 |
private int implCompressMultiBlock0(byte[] b, int ofs, int limit) { |
24573
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
149 |
for (; ofs <= limit; ofs += blockSize) { |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
150 |
implCompress(b, ofs); |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
151 |
} |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
152 |
return ofs; |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
153 |
} |
fc2756f8dfef
8035974: Refactor DigestBase.engineUpdate() method for better code generation by JIT compiler
kvn
parents:
14342
diff
changeset
|
154 |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
155 |
private void implCompressMultiBlockCheck(byte[] b, int ofs, int limit) { |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
156 |
if (limit < 0) { |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
157 |
return; // not an error because implCompressMultiBlockImpl won't execute if limit < 0 |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
158 |
// and an exception is thrown if ofs < 0. |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
159 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
160 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
161 |
Objects.requireNonNull(b); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
162 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
163 |
if (ofs < 0 || ofs >= b.length) { |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
164 |
throw new ArrayIndexOutOfBoundsException(ofs); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
165 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
166 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
167 |
int endIndex = (limit / blockSize) * blockSize + blockSize - 1; |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
168 |
if (endIndex >= b.length) { |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
169 |
throw new ArrayIndexOutOfBoundsException(endIndex); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
170 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
171 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
172 |
|
2 | 173 |
// reset this object. See JCA doc. |
174 |
protected final void engineReset() { |
|
175 |
if (bytesProcessed == 0) { |
|
176 |
// already reset, ignore |
|
177 |
return; |
|
178 |
} |
|
179 |
implReset(); |
|
180 |
bufOfs = 0; |
|
181 |
bytesProcessed = 0; |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
182 |
Arrays.fill(buffer, (byte) 0x00); |
2 | 183 |
} |
184 |
||
185 |
// return the digest. See JCA doc. |
|
186 |
protected final byte[] engineDigest() { |
|
187 |
byte[] b = new byte[digestLength]; |
|
188 |
try { |
|
189 |
engineDigest(b, 0, b.length); |
|
190 |
} catch (DigestException e) { |
|
191 |
throw (ProviderException) |
|
192 |
new ProviderException("Internal error").initCause(e); |
|
193 |
} |
|
194 |
return b; |
|
195 |
} |
|
196 |
||
197 |
// return the digest in the specified array. See JCA doc. |
|
198 |
protected final int engineDigest(byte[] out, int ofs, int len) |
|
199 |
throws DigestException { |
|
200 |
if (len < digestLength) { |
|
201 |
throw new DigestException("Length must be at least " |
|
202 |
+ digestLength + " for " + algorithm + "digests"); |
|
203 |
} |
|
204 |
if ((ofs < 0) || (len < 0) || (ofs > out.length - len)) { |
|
205 |
throw new DigestException("Buffer too short to store digest"); |
|
206 |
} |
|
207 |
if (bytesProcessed < 0) { |
|
208 |
engineReset(); |
|
209 |
} |
|
210 |
implDigest(out, ofs); |
|
211 |
bytesProcessed = -1; |
|
212 |
return digestLength; |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Core compression function. Processes blockSize bytes at a time |
|
217 |
* and updates the state of this object. |
|
218 |
*/ |
|
219 |
abstract void implCompress(byte[] b, int ofs); |
|
220 |
||
221 |
/** |
|
222 |
* Return the digest. Subclasses do not need to reset() themselves, |
|
223 |
* DigestBase calls implReset() when necessary. |
|
224 |
*/ |
|
225 |
abstract void implDigest(byte[] out, int ofs); |
|
226 |
||
227 |
/** |
|
228 |
* Reset subclass specific state to their initial values. DigestBase |
|
229 |
* calls this method when necessary. |
|
230 |
*/ |
|
231 |
abstract void implReset(); |
|
232 |
||
12685 | 233 |
public Object clone() throws CloneNotSupportedException { |
234 |
DigestBase copy = (DigestBase) super.clone(); |
|
235 |
copy.buffer = copy.buffer.clone(); |
|
236 |
return copy; |
|
237 |
} |
|
2 | 238 |
|
239 |
// padding used for the MD5, and SHA-* message digests |
|
240 |
static final byte[] padding; |
|
241 |
||
242 |
static { |
|
243 |
// we need 128 byte padding for SHA-384/512 |
|
244 |
// and an additional 8 bytes for the high 8 bytes of the 16 |
|
245 |
// byte bit counter in SHA-384/512 |
|
246 |
padding = new byte[136]; |
|
247 |
padding[0] = (byte)0x80; |
|
248 |
} |
|
249 |
} |