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