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/SHA2.java
41159 views
1
/*
2
* Copyright (c) 2002, 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 jdk.internal.vm.annotation.IntrinsicCandidate;
32
import static sun.security.provider.ByteArrayAccess.*;
33
34
/**
35
* This class implements the Secure Hash Algorithm SHA-256 developed by
36
* the National Institute of Standards and Technology along with the
37
* National Security Agency.
38
*
39
* <p>It implements java.security.MessageDigestSpi, and can be used
40
* through Java Cryptography Architecture (JCA), as a pluggable
41
* MessageDigest implementation.
42
*
43
* @since 1.4.2
44
* @author Valerie Peng
45
* @author Andreas Sterbenz
46
*/
47
abstract class SHA2 extends DigestBase {
48
49
private static final int ITERATION = 64;
50
// Constants for each round
51
private static final int[] ROUND_CONSTS = {
52
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
53
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
54
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
55
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
56
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
57
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
58
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
59
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
60
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
61
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
62
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
63
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
64
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
65
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
66
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
67
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
68
};
69
70
// buffer used by implCompress()
71
private int[] W;
72
73
// state of this object
74
private int[] state;
75
76
// initial state value. different between SHA-224 and SHA-256
77
private final int[] initialHashes;
78
79
/**
80
* Creates a new SHA object.
81
*/
82
SHA2(String name, int digestLength, int[] initialHashes) {
83
super(name, digestLength, 64);
84
this.initialHashes = initialHashes;
85
state = new int[8];
86
resetHashes();
87
}
88
89
/**
90
* Resets the buffers and hash value to start a new hash.
91
*/
92
void implReset() {
93
resetHashes();
94
if (W != null) {
95
Arrays.fill(W, 0);
96
}
97
}
98
99
private void resetHashes() {
100
System.arraycopy(initialHashes, 0, state, 0, state.length);
101
}
102
103
void implDigest(byte[] out, int ofs) {
104
long bitsProcessed = bytesProcessed << 3;
105
106
int index = (int)bytesProcessed & 0x3f;
107
int padLen = (index < 56) ? (56 - index) : (120 - index);
108
engineUpdate(padding, 0, padLen);
109
110
i2bBig4((int)(bitsProcessed >>> 32), buffer, 56);
111
i2bBig4((int)bitsProcessed, buffer, 60);
112
implCompress(buffer, 0);
113
114
i2bBig(state, 0, out, ofs, engineGetDigestLength());
115
}
116
117
/**
118
* Process the current block to update the state variable state.
119
*/
120
void implCompress(byte[] buf, int ofs) {
121
implCompressCheck(buf, ofs);
122
implCompress0(buf, ofs);
123
}
124
125
private void implCompressCheck(byte[] buf, int ofs) {
126
Objects.requireNonNull(buf);
127
128
// Checks similar to those performed by the method 'b2iBig64'
129
// are sufficient for the case when the method 'implCompress0' is
130
// replaced with a compiler intrinsic.
131
if (ofs < 0 || (buf.length - ofs) < 64) {
132
throw new ArrayIndexOutOfBoundsException();
133
}
134
}
135
136
// The method 'implCompressImpl' seems not to use its parameters.
137
// The method can, however, be replaced with a compiler intrinsic
138
// that operates directly on the array 'buf' (starting from
139
// offset 'ofs') and not on array 'W', therefore 'buf' and 'ofs'
140
// must be passed as parameter to the method.
141
@IntrinsicCandidate
142
private void implCompress0(byte[] buf, int ofs) {
143
if (W == null) {
144
W = new int[64];
145
}
146
b2iBig64(buf, ofs, W);
147
// The first 16 ints are from the byte stream, compute the rest of
148
// the W[]'s
149
for (int t = 16; t < ITERATION; t++) {
150
int W_t2 = W[t - 2];
151
int W_t15 = W[t - 15];
152
153
// S(x,s) is right rotation of x by s positions:
154
// S(x,s) = (x >>> s) | (x << (32 - s))
155
// R(x,s) is right shift of x by s positions:
156
// R(x,s) = (x >>> s)
157
158
// delta0(x) = S(x, 7) ^ S(x, 18) ^ R(x, 3)
159
int delta0_W_t15 =
160
((W_t15 >>> 7) | (W_t15 << 25)) ^
161
((W_t15 >>> 18) | (W_t15 << 14)) ^
162
(W_t15 >>> 3);
163
164
// delta1(x) = S(x, 17) ^ S(x, 19) ^ R(x, 10)
165
int delta1_W_t2 =
166
((W_t2 >>> 17) | (W_t2 << 15)) ^
167
((W_t2 >>> 19) | (W_t2 << 13)) ^
168
(W_t2 >>> 10);
169
170
W[t] = delta0_W_t15 + delta1_W_t2 + W[t-7] + W[t-16];
171
}
172
173
int a = state[0];
174
int b = state[1];
175
int c = state[2];
176
int d = state[3];
177
int e = state[4];
178
int f = state[5];
179
int g = state[6];
180
int h = state[7];
181
182
for (int i = 0; i < ITERATION; i++) {
183
// S(x,s) is right rotation of x by s positions:
184
// S(x,s) = (x >>> s) | (x << (32 - s))
185
186
// sigma0(x) = S(x,2) xor S(x,13) xor S(x,22)
187
int sigma0_a =
188
((a >>> 2) | (a << 30)) ^
189
((a >>> 13) | (a << 19)) ^
190
((a >>> 22) | (a << 10));
191
192
// sigma1(x) = S(x,6) xor S(x,11) xor S(x,25)
193
int sigma1_e =
194
((e >>> 6) | (e << 26)) ^
195
((e >>> 11) | (e << 21)) ^
196
((e >>> 25) | (e << 7));
197
198
// ch(x,y,z) = (x and y) xor ((complement x) and z)
199
int ch_efg = (e & f) ^ ((~e) & g);
200
201
// maj(x,y,z) = (x and y) xor (x and z) xor (y and z)
202
int maj_abc = (a & b) ^ (a & c) ^ (b & c);
203
204
int T1 = h + sigma1_e + ch_efg + ROUND_CONSTS[i] + W[i];
205
int T2 = sigma0_a + maj_abc;
206
h = g;
207
g = f;
208
f = e;
209
e = d + T1;
210
d = c;
211
c = b;
212
b = a;
213
a = T1 + T2;
214
}
215
216
state[0] += a;
217
state[1] += b;
218
state[2] += c;
219
state[3] += d;
220
state[4] += e;
221
state[5] += f;
222
state[6] += g;
223
state[7] += h;
224
}
225
226
public Object clone() throws CloneNotSupportedException {
227
SHA2 copy = (SHA2) super.clone();
228
copy.state = copy.state.clone();
229
copy.W = null;
230
return copy;
231
}
232
233
/**
234
* SHA-224 implementation class.
235
*/
236
public static final class SHA224 extends SHA2 {
237
private static final int[] INITIAL_HASHES = {
238
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
239
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
240
};
241
242
public SHA224() {
243
super("SHA-224", 28, INITIAL_HASHES);
244
}
245
}
246
247
/**
248
* SHA-256 implementation class.
249
*/
250
public static final class SHA256 extends SHA2 {
251
private static final int[] INITIAL_HASHES = {
252
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
253
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
254
};
255
256
public SHA256() {
257
super("SHA-256", 32, INITIAL_HASHES);
258
}
259
}
260
}
261
262