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/MD5.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.lang.invoke.MethodHandles;
29
import java.lang.invoke.VarHandle;
30
import java.nio.ByteOrder;
31
import java.util.Arrays;
32
import java.util.Objects;
33
34
import static sun.security.provider.ByteArrayAccess.*;
35
import jdk.internal.vm.annotation.IntrinsicCandidate;
36
37
/**
38
* The MD5 class is used to compute an MD5 message digest over a given
39
* buffer of bytes. It is an implementation of the RSA Data Security Inc
40
* MD5 algorithim as described in internet RFC 1321.
41
*
42
* @author Chuck McManis
43
* @author Benjamin Renaud
44
* @author Andreas Sterbenz
45
*/
46
public final class MD5 extends DigestBase {
47
48
// state of this object
49
private int[] state;
50
51
// rotation constants
52
private static final int S11 = 7;
53
private static final int S12 = 12;
54
private static final int S13 = 17;
55
private static final int S14 = 22;
56
private static final int S21 = 5;
57
private static final int S22 = 9;
58
private static final int S23 = 14;
59
private static final int S24 = 20;
60
private static final int S31 = 4;
61
private static final int S32 = 11;
62
private static final int S33 = 16;
63
private static final int S34 = 23;
64
private static final int S41 = 6;
65
private static final int S42 = 10;
66
private static final int S43 = 15;
67
private static final int S44 = 21;
68
69
// Standard constructor, creates a new MD5 instance.
70
public MD5() {
71
super("MD5", 16, 64);
72
state = new int[4];
73
implReset();
74
}
75
76
// clone this object
77
public Object clone() throws CloneNotSupportedException {
78
MD5 copy = (MD5) super.clone();
79
copy.state = copy.state.clone();
80
return copy;
81
}
82
83
/**
84
* Reset the state of this object.
85
*/
86
void implReset() {
87
// Load magic initialization constants.
88
state[0] = 0x67452301;
89
state[1] = 0xefcdab89;
90
state[2] = 0x98badcfe;
91
state[3] = 0x10325476;
92
}
93
94
/**
95
* Perform the final computations, any buffered bytes are added
96
* to the digest, the count is added to the digest, and the resulting
97
* digest is stored.
98
*/
99
void implDigest(byte[] out, int ofs) {
100
long bitsProcessed = bytesProcessed << 3;
101
102
int index = (int)bytesProcessed & 0x3f;
103
int padLen = (index < 56) ? (56 - index) : (120 - index);
104
engineUpdate(padding, 0, padLen);
105
106
i2bLittle4((int)bitsProcessed, buffer, 56);
107
i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);
108
implCompress(buffer, 0);
109
110
i2bLittle(state, 0, out, ofs, 16);
111
}
112
113
/* **********************************************************
114
* The MD5 Functions. The results of this
115
* implementation were checked against the RSADSI version.
116
* **********************************************************
117
*/
118
119
private static int FF(int a, int b, int c, int d, int x, int s, int ac) {
120
a += ((b & c) | ((~b) & d)) + x + ac;
121
return ((a << s) | (a >>> (32 - s))) + b;
122
}
123
124
private static int GG(int a, int b, int c, int d, int x, int s, int ac) {
125
a += ((b & d) | (c & (~d))) + x + ac;
126
return ((a << s) | (a >>> (32 - s))) + b;
127
}
128
129
private static int HH(int a, int b, int c, int d, int x, int s, int ac) {
130
a += ((b ^ c) ^ d) + x + ac;
131
return ((a << s) | (a >>> (32 - s))) + b;
132
}
133
134
private static int II(int a, int b, int c, int d, int x, int s, int ac) {
135
a += (c ^ (b | (~d))) + x + ac;
136
return ((a << s) | (a >>> (32 - s))) + b;
137
}
138
139
/**
140
* This is where the functions come together as the generic MD5
141
* transformation operation. It consumes sixteen
142
* bytes from the buffer, beginning at the specified offset.
143
*/
144
void implCompress(byte[] buf, int ofs) {
145
implCompressCheck(buf, ofs);
146
implCompress0(buf, ofs);
147
}
148
149
private void implCompressCheck(byte[] buf, int ofs) {
150
Objects.requireNonNull(buf);
151
152
// These checks are sufficient for the case when the method
153
// 'implCompressImpl' is replaced with a compiler
154
// intrinsic.
155
if ((ofs < 0) || ((buf.length - ofs) < 64)) {
156
throw new ArrayIndexOutOfBoundsException();
157
}
158
}
159
160
// The method 'implCompress0 seems not to use its parameters.
161
// The method can, however, be replaced with a compiler intrinsic
162
// that operates directly on the array 'buf' (starting from
163
// offset 'ofs') and not on array 'x', therefore 'buf' and 'ofs'
164
// must be passed as parameter to the method.
165
@IntrinsicCandidate
166
void implCompress0(byte[] buf, int ofs) {
167
int a = state[0];
168
int b = state[1];
169
int c = state[2];
170
int d = state[3];
171
172
int x0 = (int) LE.INT_ARRAY.get(buf, ofs);
173
int x1 = (int) LE.INT_ARRAY.get(buf, ofs + 4);
174
int x2 = (int) LE.INT_ARRAY.get(buf, ofs + 8);
175
int x3 = (int) LE.INT_ARRAY.get(buf, ofs + 12);
176
int x4 = (int) LE.INT_ARRAY.get(buf, ofs + 16);
177
int x5 = (int) LE.INT_ARRAY.get(buf, ofs + 20);
178
int x6 = (int) LE.INT_ARRAY.get(buf, ofs + 24);
179
int x7 = (int) LE.INT_ARRAY.get(buf, ofs + 28);
180
int x8 = (int) LE.INT_ARRAY.get(buf, ofs + 32);
181
int x9 = (int) LE.INT_ARRAY.get(buf, ofs + 36);
182
int x10 = (int) LE.INT_ARRAY.get(buf, ofs + 40);
183
int x11 = (int) LE.INT_ARRAY.get(buf, ofs + 44);
184
int x12 = (int) LE.INT_ARRAY.get(buf, ofs + 48);
185
int x13 = (int) LE.INT_ARRAY.get(buf, ofs + 52);
186
int x14 = (int) LE.INT_ARRAY.get(buf, ofs + 56);
187
int x15 = (int) LE.INT_ARRAY.get(buf, ofs + 60);
188
189
/* Round 1 */
190
a = FF ( a, b, c, d, x0, S11, 0xd76aa478); /* 1 */
191
d = FF ( d, a, b, c, x1, S12, 0xe8c7b756); /* 2 */
192
c = FF ( c, d, a, b, x2, S13, 0x242070db); /* 3 */
193
b = FF ( b, c, d, a, x3, S14, 0xc1bdceee); /* 4 */
194
a = FF ( a, b, c, d, x4, S11, 0xf57c0faf); /* 5 */
195
d = FF ( d, a, b, c, x5, S12, 0x4787c62a); /* 6 */
196
c = FF ( c, d, a, b, x6, S13, 0xa8304613); /* 7 */
197
b = FF ( b, c, d, a, x7, S14, 0xfd469501); /* 8 */
198
a = FF ( a, b, c, d, x8, S11, 0x698098d8); /* 9 */
199
d = FF ( d, a, b, c, x9, S12, 0x8b44f7af); /* 10 */
200
c = FF ( c, d, a, b, x10, S13, 0xffff5bb1); /* 11 */
201
b = FF ( b, c, d, a, x11, S14, 0x895cd7be); /* 12 */
202
a = FF ( a, b, c, d, x12, S11, 0x6b901122); /* 13 */
203
d = FF ( d, a, b, c, x13, S12, 0xfd987193); /* 14 */
204
c = FF ( c, d, a, b, x14, S13, 0xa679438e); /* 15 */
205
b = FF ( b, c, d, a, x15, S14, 0x49b40821); /* 16 */
206
207
/* Round 2 */
208
a = GG ( a, b, c, d, x1, S21, 0xf61e2562); /* 17 */
209
d = GG ( d, a, b, c, x6, S22, 0xc040b340); /* 18 */
210
c = GG ( c, d, a, b, x11, S23, 0x265e5a51); /* 19 */
211
b = GG ( b, c, d, a, x0, S24, 0xe9b6c7aa); /* 20 */
212
a = GG ( a, b, c, d, x5, S21, 0xd62f105d); /* 21 */
213
d = GG ( d, a, b, c, x10, S22, 0x2441453); /* 22 */
214
c = GG ( c, d, a, b, x15, S23, 0xd8a1e681); /* 23 */
215
b = GG ( b, c, d, a, x4, S24, 0xe7d3fbc8); /* 24 */
216
a = GG ( a, b, c, d, x9, S21, 0x21e1cde6); /* 25 */
217
d = GG ( d, a, b, c, x14, S22, 0xc33707d6); /* 26 */
218
c = GG ( c, d, a, b, x3, S23, 0xf4d50d87); /* 27 */
219
b = GG ( b, c, d, a, x8, S24, 0x455a14ed); /* 28 */
220
a = GG ( a, b, c, d, x13, S21, 0xa9e3e905); /* 29 */
221
d = GG ( d, a, b, c, x2, S22, 0xfcefa3f8); /* 30 */
222
c = GG ( c, d, a, b, x7, S23, 0x676f02d9); /* 31 */
223
b = GG ( b, c, d, a, x12, S24, 0x8d2a4c8a); /* 32 */
224
225
/* Round 3 */
226
a = HH ( a, b, c, d, x5, S31, 0xfffa3942); /* 33 */
227
d = HH ( d, a, b, c, x8, S32, 0x8771f681); /* 34 */
228
c = HH ( c, d, a, b, x11, S33, 0x6d9d6122); /* 35 */
229
b = HH ( b, c, d, a, x14, S34, 0xfde5380c); /* 36 */
230
a = HH ( a, b, c, d, x1, S31, 0xa4beea44); /* 37 */
231
d = HH ( d, a, b, c, x4, S32, 0x4bdecfa9); /* 38 */
232
c = HH ( c, d, a, b, x7, S33, 0xf6bb4b60); /* 39 */
233
b = HH ( b, c, d, a, x10, S34, 0xbebfbc70); /* 40 */
234
a = HH ( a, b, c, d, x13, S31, 0x289b7ec6); /* 41 */
235
d = HH ( d, a, b, c, x0, S32, 0xeaa127fa); /* 42 */
236
c = HH ( c, d, a, b, x3, S33, 0xd4ef3085); /* 43 */
237
b = HH ( b, c, d, a, x6, S34, 0x4881d05); /* 44 */
238
a = HH ( a, b, c, d, x9, S31, 0xd9d4d039); /* 45 */
239
d = HH ( d, a, b, c, x12, S32, 0xe6db99e5); /* 46 */
240
c = HH ( c, d, a, b, x15, S33, 0x1fa27cf8); /* 47 */
241
b = HH ( b, c, d, a, x2, S34, 0xc4ac5665); /* 48 */
242
243
/* Round 4 */
244
a = II ( a, b, c, d, x0, S41, 0xf4292244); /* 49 */
245
d = II ( d, a, b, c, x7, S42, 0x432aff97); /* 50 */
246
c = II ( c, d, a, b, x14, S43, 0xab9423a7); /* 51 */
247
b = II ( b, c, d, a, x5, S44, 0xfc93a039); /* 52 */
248
a = II ( a, b, c, d, x12, S41, 0x655b59c3); /* 53 */
249
d = II ( d, a, b, c, x3, S42, 0x8f0ccc92); /* 54 */
250
c = II ( c, d, a, b, x10, S43, 0xffeff47d); /* 55 */
251
b = II ( b, c, d, a, x1, S44, 0x85845dd1); /* 56 */
252
a = II ( a, b, c, d, x8, S41, 0x6fa87e4f); /* 57 */
253
d = II ( d, a, b, c, x15, S42, 0xfe2ce6e0); /* 58 */
254
c = II ( c, d, a, b, x6, S43, 0xa3014314); /* 59 */
255
b = II ( b, c, d, a, x13, S44, 0x4e0811a1); /* 60 */
256
a = II ( a, b, c, d, x4, S41, 0xf7537e82); /* 61 */
257
d = II ( d, a, b, c, x11, S42, 0xbd3af235); /* 62 */
258
c = II ( c, d, a, b, x2, S43, 0x2ad7d2bb); /* 63 */
259
b = II ( b, c, d, a, x9, S44, 0xeb86d391); /* 64 */
260
261
state[0] += a;
262
state[1] += b;
263
state[2] += c;
264
state[3] += d;
265
}
266
267
}
268
269