001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // This program is free software; you can redistribute it and/or modify
004: // it under the terms of the GNU General Public License and GNU Library
005: // General Public License as published by the Free Software Foundation;
006: // either version 2, or (at your option) any later version.
007: //
008: // This program is distributed in the hope that it will be useful,
009: // but WITHOUT ANY WARRANTY; without even the implied warranty of
010: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: // GNU General Public License and GNU Library General Public License
012: // for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // and GNU Library General Public License along with this program; if
016: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
017: // MA 02139, USA.
018: //
019: ///////////////////////////////////////////////////////////////////////////////
020:
021: package org.rdesktop.server.rdp.crypto;
022:
023: public final class SHA1 extends BlockMessageDigest implements Cloneable {
024: private static final int HASH_LENGTH = 20;
025: private static final int DATA_LENGTH = 64;
026:
027: private int[] m_w;
028: private int[] m_data;
029: private int[] m_digest;
030: private byte[] m_temp;
031:
032: private static int f1(int a, int b, int c) {
033: return (c ^ (a & (b ^ c))) + 0x5A827999;
034: }
035:
036: private static int f2(int a, int b, int c) {
037: return (a ^ b ^ c) + 0x6ED9EBA1;
038: }
039:
040: private static int f3(int a, int b, int c) {
041: return ((a & b) | (c & (a | b))) + 0x8F1BBCDC;
042: }
043:
044: private static int f4(int a, int b, int c) {
045: return (a ^ b ^ c) + 0xCA62C1D6;
046: }
047:
048: private static void byte2int(byte[] src, int srcOffset, int[] dst,
049: int dstOffset, int length) {
050: while (length-- > 0) {
051: dst[dstOffset++] = (src[srcOffset++] << 24)
052: | ((src[srcOffset++] & 0xFF) << 16)
053: | ((src[srcOffset++] & 0xFF) << 8)
054: | (src[srcOffset++] & 0xFF);
055: }
056: }
057:
058: public SHA1() {
059: super ("SHA-1");
060: java_init();
061: engineReset();
062: }
063:
064: private SHA1(SHA1 md) {
065: this ();
066: m_data = (int[]) md.m_data.clone();
067: m_digest = (int[]) md.m_digest.clone();
068: m_temp = (byte[]) md.m_temp.clone();
069: m_w = (int[]) md.m_w.clone();
070: }
071:
072: private void java_init() {
073: m_w = new int[80];
074: m_digest = new int[HASH_LENGTH / 4];
075: m_data = new int[DATA_LENGTH / 4];
076: m_temp = new byte[DATA_LENGTH];
077: }
078:
079: private void java_reset() {
080: m_digest[0] = 0x67452301;
081: m_digest[1] = 0xefcdab89;
082: m_digest[2] = 0x98badcfe;
083: m_digest[3] = 0x10325476;
084: m_digest[4] = 0xc3d2e1f0;
085: }
086:
087: private void java_transform(byte[] in) {
088: byte2int(in, 0, m_data, 0, DATA_LENGTH / 4);
089: transform(m_data);
090: }
091:
092: private byte[] java_digest(byte[] in, int pos) {
093: if (pos != 0) {
094: System.arraycopy(in, 0, m_temp, 0, pos);
095: }
096:
097: m_temp[pos++] = (byte) 0x80;
098:
099: if (pos > DATA_LENGTH - 8) {
100: while (pos < DATA_LENGTH) {
101: m_temp[pos++] = 0;
102: }
103:
104: byte2int(m_temp, 0, m_data, 0, DATA_LENGTH / 4);
105: transform(m_data);
106: pos = 0;
107: }
108:
109: while (pos < DATA_LENGTH - 8) {
110: m_temp[pos++] = 0;
111: }
112:
113: byte2int(m_temp, 0, m_data, 0, (DATA_LENGTH / 4) - 2);
114:
115: int bc = bitcount();
116: m_data[14] = 0;
117: m_data[15] = bc;
118:
119: transform(m_data);
120:
121: byte buf[] = new byte[HASH_LENGTH];
122:
123: int off = 0;
124: for (int i = 0; i < HASH_LENGTH / 4; ++i) {
125: int d = m_digest[i];
126: buf[off++] = (byte) (d >>> 24);
127: buf[off++] = (byte) (d >>> 16);
128: buf[off++] = (byte) (d >>> 8);
129: buf[off++] = (byte) d;
130: }
131:
132: return buf;
133: }
134:
135: private void transform(int[] X) {
136: int A = m_digest[0];
137: int B = m_digest[1];
138: int C = m_digest[2];
139: int D = m_digest[3];
140: int E = m_digest[4];
141:
142: int W[] = m_w;
143: for (int i = 0; i < 16; i++) {
144: W[i] = X[i];
145: }
146: for (int i = 16; i < 80; i++) {
147: int j = W[i - 16] ^ W[i - 14] ^ W[i - 8] ^ W[i - 3];
148: W[i] = j;
149: W[i] = (j << 1) | (j >>> -1);
150: }
151:
152: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[0];
153: B = ((B << 30) | (B >>> -30));
154: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[1];
155: A = ((A << 30) | (A >>> -30));
156: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[2];
157: E = ((E << 30) | (E >>> -30));
158: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[3];
159: D = ((D << 30) | (D >>> -30));
160: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[4];
161: C = ((C << 30) | (C >>> -30));
162: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[5];
163: B = ((B << 30) | (B >>> -30));
164: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[6];
165: A = ((A << 30) | (A >>> -30));
166: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[7];
167: E = ((E << 30) | (E >>> -30));
168: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[8];
169: D = ((D << 30) | (D >>> -30));
170: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[9];
171: C = ((C << 30) | (C >>> -30));
172: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[10];
173: B = ((B << 30) | (B >>> -30));
174: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[11];
175: A = ((A << 30) | (A >>> -30));
176: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[12];
177: E = ((E << 30) | (E >>> -30));
178: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[13];
179: D = ((D << 30) | (D >>> -30));
180: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[14];
181: C = ((C << 30) | (C >>> -30));
182: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[15];
183: B = ((B << 30) | (B >>> -30));
184: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[16];
185: A = ((A << 30) | (A >>> -30));
186: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[17];
187: E = ((E << 30) | (E >>> -30));
188: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[18];
189: D = ((D << 30) | (D >>> -30));
190: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[19];
191: C = ((C << 30) | (C >>> -30));
192: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[20];
193: B = ((B << 30) | (B >>> -30));
194: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[21];
195: A = ((A << 30) | (A >>> -30));
196: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[22];
197: E = ((E << 30) | (E >>> -30));
198: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[23];
199: D = ((D << 30) | (D >>> -30));
200: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[24];
201: C = ((C << 30) | (C >>> -30));
202: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[25];
203: B = ((B << 30) | (B >>> -30));
204: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[26];
205: A = ((A << 30) | (A >>> -30));
206: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[27];
207: E = ((E << 30) | (E >>> -30));
208: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[28];
209: D = ((D << 30) | (D >>> -30));
210: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[29];
211: C = ((C << 30) | (C >>> -30));
212: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[30];
213: B = ((B << 30) | (B >>> -30));
214: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[31];
215: A = ((A << 30) | (A >>> -30));
216: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[32];
217: E = ((E << 30) | (E >>> -30));
218: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[33];
219: D = ((D << 30) | (D >>> -30));
220: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[34];
221: C = ((C << 30) | (C >>> -30));
222: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[35];
223: B = ((B << 30) | (B >>> -30));
224: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[36];
225: A = ((A << 30) | (A >>> -30));
226: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[37];
227: E = ((E << 30) | (E >>> -30));
228: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[38];
229: D = ((D << 30) | (D >>> -30));
230: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[39];
231: C = ((C << 30) | (C >>> -30));
232: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[40];
233: B = ((B << 30) | (B >>> -30));
234: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[41];
235: A = ((A << 30) | (A >>> -30));
236: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[42];
237: E = ((E << 30) | (E >>> -30));
238: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[43];
239: D = ((D << 30) | (D >>> -30));
240: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[44];
241: C = ((C << 30) | (C >>> -30));
242: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[45];
243: B = ((B << 30) | (B >>> -30));
244: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[46];
245: A = ((A << 30) | (A >>> -30));
246: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[47];
247: E = ((E << 30) | (E >>> -30));
248: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[48];
249: D = ((D << 30) | (D >>> -30));
250: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[49];
251: C = ((C << 30) | (C >>> -30));
252: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[50];
253: B = ((B << 30) | (B >>> -30));
254: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[51];
255: A = ((A << 30) | (A >>> -30));
256: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[52];
257: E = ((E << 30) | (E >>> -30));
258: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[53];
259: D = ((D << 30) | (D >>> -30));
260: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[54];
261: C = ((C << 30) | (C >>> -30));
262: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[55];
263: B = ((B << 30) | (B >>> -30));
264: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[56];
265: A = ((A << 30) | (A >>> -30));
266: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[57];
267: E = ((E << 30) | (E >>> -30));
268: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[58];
269: D = ((D << 30) | (D >>> -30));
270: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[59];
271: C = ((C << 30) | (C >>> -30));
272: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[60];
273: B = ((B << 30) | (B >>> -30));
274: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[61];
275: A = ((A << 30) | (A >>> -30));
276: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[62];
277: E = ((E << 30) | (E >>> -30));
278: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[63];
279: D = ((D << 30) | (D >>> -30));
280: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[64];
281: C = ((C << 30) | (C >>> -30));
282: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[65];
283: B = ((B << 30) | (B >>> -30));
284: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[66];
285: A = ((A << 30) | (A >>> -30));
286: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[67];
287: E = ((E << 30) | (E >>> -30));
288: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[68];
289: D = ((D << 30) | (D >>> -30));
290: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[69];
291: C = ((C << 30) | (C >>> -30));
292: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[70];
293: B = ((B << 30) | (B >>> -30));
294: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[71];
295: A = ((A << 30) | (A >>> -30));
296: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[72];
297: E = ((E << 30) | (E >>> -30));
298: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[73];
299: D = ((D << 30) | (D >>> -30));
300: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[74];
301: C = ((C << 30) | (C >>> -30));
302: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[75];
303: B = ((B << 30) | (B >>> -30));
304: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[76];
305: A = ((A << 30) | (A >>> -30));
306: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[77];
307: E = ((E << 30) | (E >>> -30));
308: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[78];
309: D = ((D << 30) | (D >>> -30));
310: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[79];
311: C = ((C << 30) | (C >>> -30));
312:
313: m_digest[0] += A;
314: m_digest[1] += B;
315: m_digest[2] += C;
316: m_digest[3] += D;
317: m_digest[4] += E;
318: }
319:
320: protected int engineGetDigestLength() {
321: return HASH_LENGTH;
322: }
323:
324: protected int engineGetDataLength() {
325: return DATA_LENGTH;
326: }
327:
328: public Object clone() {
329: return new SHA1(this );
330: }
331:
332: public void engineReset() {
333: super .engineReset();
334: java_reset();
335: }
336:
337: protected void engineTransform(byte[] in) {
338: java_transform(in);
339: }
340:
341: public byte[] engineDigest(byte[] in, int length) {
342: byte b[] = java_digest(in, length);
343: engineReset();
344: return b;
345: }
346: }
|