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/MD4.java
38830 views
1
/*
2
* Copyright (c) 2005, 2013, 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.*;
29
import java.util.Arrays;
30
31
import static sun.security.provider.ByteArrayAccess.*;
32
33
/**
34
* The MD4 class is used to compute an MD4 message digest over a given
35
* buffer of bytes. It is an implementation of the RSA Data Security Inc
36
* MD4 algorithim as described in internet RFC 1320.
37
*
38
* <p>The MD4 algorithm is very weak and should not be used unless it is
39
* unavoidable. Therefore, it is not registered in our standard providers. To
40
* obtain an implementation, call the static getInstance() method in this
41
* class.
42
*
43
* @author Andreas Sterbenz
44
*/
45
public final class MD4 extends DigestBase {
46
47
// state of this object
48
private int[] state;
49
// temporary buffer, used by implCompress()
50
private int[] x;
51
52
// rotation constants
53
private static final int S11 = 3;
54
private static final int S12 = 7;
55
private static final int S13 = 11;
56
private static final int S14 = 19;
57
private static final int S21 = 3;
58
private static final int S22 = 5;
59
private static final int S23 = 9;
60
private static final int S24 = 13;
61
private static final int S31 = 3;
62
private static final int S32 = 9;
63
private static final int S33 = 11;
64
private static final int S34 = 15;
65
66
private final static Provider md4Provider;
67
68
static {
69
md4Provider = new Provider("MD4Provider", 1.8d, "MD4 MessageDigest") {
70
private static final long serialVersionUID = -8850464997518327965L;
71
};
72
AccessController.doPrivileged(new PrivilegedAction<Void>() {
73
public Void run() {
74
md4Provider.put("MessageDigest.MD4", "sun.security.provider.MD4");
75
return null;
76
}
77
});
78
}
79
80
public static MessageDigest getInstance() {
81
try {
82
return MessageDigest.getInstance("MD4", md4Provider);
83
} catch (NoSuchAlgorithmException e) {
84
// should never occur
85
throw new ProviderException(e);
86
}
87
}
88
89
// Standard constructor, creates a new MD4 instance.
90
public MD4() {
91
super("MD4", 16, 64);
92
state = new int[4];
93
x = new int[16];
94
resetHashes();
95
}
96
97
// clone this object
98
public Object clone() throws CloneNotSupportedException {
99
MD4 copy = (MD4) super.clone();
100
copy.state = copy.state.clone();
101
copy.x = new int[16];
102
return copy;
103
}
104
105
/**
106
* Reset the state of this object.
107
*/
108
void implReset() {
109
// Load magic initialization constants.
110
resetHashes();
111
// clear out old data
112
Arrays.fill(x, 0);
113
}
114
115
private void resetHashes() {
116
state[0] = 0x67452301;
117
state[1] = 0xefcdab89;
118
state[2] = 0x98badcfe;
119
state[3] = 0x10325476;
120
}
121
122
/**
123
* Perform the final computations, any buffered bytes are added
124
* to the digest, the count is added to the digest, and the resulting
125
* digest is stored.
126
*/
127
void implDigest(byte[] out, int ofs) {
128
long bitsProcessed = bytesProcessed << 3;
129
130
int index = (int)bytesProcessed & 0x3f;
131
int padLen = (index < 56) ? (56 - index) : (120 - index);
132
engineUpdate(padding, 0, padLen);
133
134
i2bLittle4((int)bitsProcessed, buffer, 56);
135
i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);
136
implCompress(buffer, 0);
137
138
i2bLittle(state, 0, out, ofs, 16);
139
}
140
141
private static int FF(int a, int b, int c, int d, int x, int s) {
142
a += ((b & c) | ((~b) & d)) + x;
143
return ((a << s) | (a >>> (32 - s)));
144
}
145
146
private static int GG(int a, int b, int c, int d, int x, int s) {
147
a += ((b & c) | (b & d) | (c & d)) + x + 0x5a827999;
148
return ((a << s) | (a >>> (32 - s)));
149
}
150
151
private static int HH(int a, int b, int c, int d, int x, int s) {
152
a += ((b ^ c) ^ d) + x + 0x6ed9eba1;
153
return ((a << s) | (a >>> (32 - s)));
154
}
155
156
/**
157
* This is where the functions come together as the generic MD4
158
* transformation operation. It consumes sixteen
159
* bytes from the buffer, beginning at the specified offset.
160
*/
161
void implCompress(byte[] buf, int ofs) {
162
b2iLittle64(buf, ofs, x);
163
164
int a = state[0];
165
int b = state[1];
166
int c = state[2];
167
int d = state[3];
168
169
/* Round 1 */
170
a = FF (a, b, c, d, x[ 0], S11); /* 1 */
171
d = FF (d, a, b, c, x[ 1], S12); /* 2 */
172
c = FF (c, d, a, b, x[ 2], S13); /* 3 */
173
b = FF (b, c, d, a, x[ 3], S14); /* 4 */
174
a = FF (a, b, c, d, x[ 4], S11); /* 5 */
175
d = FF (d, a, b, c, x[ 5], S12); /* 6 */
176
c = FF (c, d, a, b, x[ 6], S13); /* 7 */
177
b = FF (b, c, d, a, x[ 7], S14); /* 8 */
178
a = FF (a, b, c, d, x[ 8], S11); /* 9 */
179
d = FF (d, a, b, c, x[ 9], S12); /* 10 */
180
c = FF (c, d, a, b, x[10], S13); /* 11 */
181
b = FF (b, c, d, a, x[11], S14); /* 12 */
182
a = FF (a, b, c, d, x[12], S11); /* 13 */
183
d = FF (d, a, b, c, x[13], S12); /* 14 */
184
c = FF (c, d, a, b, x[14], S13); /* 15 */
185
b = FF (b, c, d, a, x[15], S14); /* 16 */
186
187
/* Round 2 */
188
a = GG (a, b, c, d, x[ 0], S21); /* 17 */
189
d = GG (d, a, b, c, x[ 4], S22); /* 18 */
190
c = GG (c, d, a, b, x[ 8], S23); /* 19 */
191
b = GG (b, c, d, a, x[12], S24); /* 20 */
192
a = GG (a, b, c, d, x[ 1], S21); /* 21 */
193
d = GG (d, a, b, c, x[ 5], S22); /* 22 */
194
c = GG (c, d, a, b, x[ 9], S23); /* 23 */
195
b = GG (b, c, d, a, x[13], S24); /* 24 */
196
a = GG (a, b, c, d, x[ 2], S21); /* 25 */
197
d = GG (d, a, b, c, x[ 6], S22); /* 26 */
198
c = GG (c, d, a, b, x[10], S23); /* 27 */
199
b = GG (b, c, d, a, x[14], S24); /* 28 */
200
a = GG (a, b, c, d, x[ 3], S21); /* 29 */
201
d = GG (d, a, b, c, x[ 7], S22); /* 30 */
202
c = GG (c, d, a, b, x[11], S23); /* 31 */
203
b = GG (b, c, d, a, x[15], S24); /* 32 */
204
205
/* Round 3 */
206
a = HH (a, b, c, d, x[ 0], S31); /* 33 */
207
d = HH (d, a, b, c, x[ 8], S32); /* 34 */
208
c = HH (c, d, a, b, x[ 4], S33); /* 35 */
209
b = HH (b, c, d, a, x[12], S34); /* 36 */
210
a = HH (a, b, c, d, x[ 2], S31); /* 37 */
211
d = HH (d, a, b, c, x[10], S32); /* 38 */
212
c = HH (c, d, a, b, x[ 6], S33); /* 39 */
213
b = HH (b, c, d, a, x[14], S34); /* 40 */
214
a = HH (a, b, c, d, x[ 1], S31); /* 41 */
215
d = HH (d, a, b, c, x[ 9], S32); /* 42 */
216
c = HH (c, d, a, b, x[ 5], S33); /* 43 */
217
b = HH (b, c, d, a, x[13], S34); /* 44 */
218
a = HH (a, b, c, d, x[ 3], S31); /* 45 */
219
d = HH (d, a, b, c, x[11], S32); /* 46 */
220
c = HH (c, d, a, b, x[ 7], S33); /* 47 */
221
b = HH (b, c, d, a, x[15], S34); /* 48 */
222
223
state[0] += a;
224
state[1] += b;
225
state[2] += c;
226
state[3] += d;
227
}
228
229
}
230
231