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