Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/SHA.java
41159 views
1
/*
2
* Copyright (c) 1996, 2021, 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.util.Arrays;
29
import java.util.Objects;
30
31
import static sun.security.provider.ByteArrayAccess.*;
32
import jdk.internal.vm.annotation.IntrinsicCandidate;
33
34
/**
35
* This class implements the Secure Hash Algorithm (SHA) developed by
36
* the National Institute of Standards and Technology along with the
37
* National Security Agency. This is the updated version of SHA
38
* fip-180 as superseded by fip-180-1.
39
*
40
* <p>It implement JavaSecurity MessageDigest, and can be used by in
41
* the Java Security framework, as a pluggable implementation, as a
42
* filter for the digest stream classes.
43
*
44
* @author Roger Riggs
45
* @author Benjamin Renaud
46
* @author Andreas Sterbenz
47
*/
48
public final class SHA extends DigestBase {
49
50
// Buffer of int's and count of characters accumulated
51
// 64 bytes are included in each hash block so the low order
52
// bits of count are used to know how to pack the bytes into ints
53
// and to know when to compute the block and start the next one.
54
private int[] W;
55
56
// state of this
57
private int[] state;
58
59
/**
60
* Creates a new SHA object.
61
*/
62
public SHA() {
63
super("SHA-1", 20, 64);
64
state = new int[5];
65
resetHashes();
66
}
67
68
/*
69
* Clones this object.
70
*/
71
public Object clone() throws CloneNotSupportedException {
72
SHA copy = (SHA) super.clone();
73
copy.state = copy.state.clone();
74
copy.W = null;
75
return copy;
76
}
77
78
/**
79
* Resets the buffers and hash value to start a new hash.
80
*/
81
void implReset() {
82
// Load magic initialization constants.
83
resetHashes();
84
// clear out old data
85
if (W != null) {
86
Arrays.fill(W, 0);
87
}
88
}
89
90
private void resetHashes() {
91
state[0] = 0x67452301;
92
state[1] = 0xefcdab89;
93
state[2] = 0x98badcfe;
94
state[3] = 0x10325476;
95
state[4] = 0xc3d2e1f0;
96
}
97
98
/**
99
* Computes the final hash and copies the 20 bytes to the output array.
100
*/
101
void implDigest(byte[] out, int ofs) {
102
long bitsProcessed = bytesProcessed << 3;
103
104
int index = (int)bytesProcessed & 0x3f;
105
int padLen = (index < 56) ? (56 - index) : (120 - index);
106
engineUpdate(padding, 0, padLen);
107
108
i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);
109
i2bBig4((int)bitsProcessed, buffer, 60);
110
implCompress(buffer, 0);
111
112
i2bBig(state, 0, out, ofs, 20);
113
}
114
115
// Constants for each round
116
private static final int round1_kt = 0x5a827999;
117
private static final int round2_kt = 0x6ed9eba1;
118
private static final int round3_kt = 0x8f1bbcdc;
119
private static final int round4_kt = 0xca62c1d6;
120
121
/**
122
* Compute a the hash for the current block.
123
*
124
* This is in the same vein as Peter Gutmann's algorithm listed in
125
* the back of Applied Cryptography, Compact implementation of
126
* "old" NIST Secure Hash Algorithm.
127
*/
128
void implCompress(byte[] buf, int ofs) {
129
implCompressCheck(buf, ofs);
130
implCompress0(buf, ofs);
131
}
132
133
private void implCompressCheck(byte[] buf, int ofs) {
134
Objects.requireNonNull(buf);
135
136
// Checks similar to those performed by the method 'b2iBig64'
137
// are sufficient for the case when the method 'implCompress0' is
138
// replaced with a compiler intrinsic.
139
if (ofs < 0 || (buf.length - ofs) < 64) {
140
throw new ArrayIndexOutOfBoundsException();
141
}
142
}
143
144
// The method 'implCompress0 seems not to use its parameters.
145
// The method can, however, be replaced with a compiler intrinsic
146
// that operates directly on the array 'buf' (starting from
147
// offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'
148
// must be passed as parameter to the method.
149
@IntrinsicCandidate
150
private void implCompress0(byte[] buf, int ofs) {
151
if (W == null) {
152
W = new int[80];
153
}
154
b2iBig64(buf, ofs, W);
155
// The first 16 ints have the byte stream, compute the rest of
156
// the buffer
157
for (int t = 16; t <= 79; t++) {
158
int temp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
159
W[t] = (temp << 1) | (temp >>> 31);
160
}
161
162
int a = state[0];
163
int b = state[1];
164
int c = state[2];
165
int d = state[3];
166
int e = state[4];
167
168
// Round 1
169
for (int i = 0; i < 20; i++) {
170
int temp = ((a<<5) | (a>>>(32-5))) +
171
((b&c)|((~b)&d))+ e + W[i] + round1_kt;
172
e = d;
173
d = c;
174
c = ((b<<30) | (b>>>(32-30)));
175
b = a;
176
a = temp;
177
}
178
179
// Round 2
180
for (int i = 20; i < 40; i++) {
181
int temp = ((a<<5) | (a>>>(32-5))) +
182
(b ^ c ^ d) + e + W[i] + round2_kt;
183
e = d;
184
d = c;
185
c = ((b<<30) | (b>>>(32-30)));
186
b = a;
187
a = temp;
188
}
189
190
// Round 3
191
for (int i = 40; i < 60; i++) {
192
int temp = ((a<<5) | (a>>>(32-5))) +
193
((b&c)|(b&d)|(c&d)) + e + W[i] + round3_kt;
194
e = d;
195
d = c;
196
c = ((b<<30) | (b>>>(32-30)));
197
b = a;
198
a = temp;
199
}
200
201
// Round 4
202
for (int i = 60; i < 80; i++) {
203
int temp = ((a<<5) | (a>>>(32-5))) +
204
(b ^ c ^ d) + e + W[i] + round4_kt;
205
e = d;
206
d = c;
207
c = ((b<<30) | (b>>>(32-30)));
208
b = a;
209
a = temp;
210
}
211
state[0] += a;
212
state[1] += b;
213
state[2] += c;
214
state[3] += d;
215
state[4] += e;
216
}
217
218
}
219
220