001: /* This was:
002: * Id: SHA.java,v 1.7 2001/06/25 15:39:55 gelderen Exp
003: * http://anoncvs.cryptix.org/cvs.php/projects/jce/src/cryptix.jce.provider.md/SHA1.java
004: *
005: * Copyright (C) 1995-2000 The Cryptix Foundation Limited.
006: * All rights reserved.
007: *
008: * Use, modification, copying and distribution of this software is subject to
009: * the terms and conditions of the Cryptix General Licence. You should have
010: * received a copy of the Cryptix General Licence along with this library;
011: * if not, you can download a copy from http://www.cryptix.org/ .
012:
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016:
017: * Redistributions of source code must retain the copyright notice,
018: * this list of conditions and the following disclaimer.
019:
020: * Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in the
022: * documentation and/or other materials provided with the
023: * distribution.
024:
025: * THIS SOFTWARE IS PROVIDED BY THE CRYPTIX FOUNDATION LIMITED AND
026: * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
027: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
028: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029: * DISCLAIMED. IN NO EVENT SHALL THE CRYPTIX FOUNDATION LIMITED OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
031: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
033: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
034: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
035: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
036: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
037: * SUCH DAMAGE.
038: */
039:
040: package uk.org.ponder.hashutil;
041:
042: /**
043: * SHA* core. Subclasses implement the function that expands the input block
044: * to 80 32-bit words. This function differs between SHA0 and SHA1.
045: *
046: * @version Revision: 1.7
047: * @author Jeroen C. van Gelderen
048: * @author Antranig Basman
049: */
050: abstract class SHA extends PaddingMD {
051:
052: // Constants
053: //...........................................................................
054:
055: /** Size (in bytes) of this hash */
056: private static final int HASH_SIZE = 20;
057:
058: // Instance variables
059: //...........................................................................
060:
061: /** 5 32-bit words (interim result) */
062: private final int[] context;
063:
064: /** 512 bits work buffer = 80 x 32-bit words */
065: private final int[] buffer;
066:
067: // Constructors
068: //...........................................................................
069:
070: public SHA() {
071: super (HASH_SIZE, PaddingMD.MODE_SHA);
072: this .context = new int[5];
073: this .buffer = new int[80];
074: coreReset();
075: }
076:
077: protected SHA(SHA src) {
078: super (src);
079: this .context = (int[]) src.context.clone();
080: this .buffer = (int[]) src.buffer.clone();
081: }
082:
083: // Concreteness
084: //...........................................................................
085:
086: protected void coreDigest(byte[] buf, int off) {
087: for (int i = 0; i < context.length; i++)
088: for (int j = 0; j < 4; j++)
089: buf[off + (i * 4 + (3 - j))] = (byte) (context[i] >>> (8 * j));
090: }
091:
092: protected void coreReset() {
093: // initial values
094: context[0] = 0x67452301;
095: context[1] = 0xefcdab89;
096: context[2] = 0x98badcfe;
097: context[3] = 0x10325476;
098: context[4] = 0xc3d2e1f0;
099: }
100:
101: protected void coreUpdate(byte[] block, int offset) {
102: int[] W = buffer;
103:
104: // extract the bytes into our working buffer
105: for (int i = 0; i < 16; i++)
106: W[i] = (block[offset++]) << 24
107: | (block[offset++] & 0xFF) << 16
108: | (block[offset++] & 0xFF) << 8
109: | (block[offset++] & 0xFF);
110:
111: expand(W);
112:
113: int A = context[0];
114: int B = context[1];
115: int C = context[2];
116: int D = context[3];
117: int E = context[4];
118:
119: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[0];
120: B = ((B << 30) | (B >>> -30));
121: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[1];
122: A = ((A << 30) | (A >>> -30));
123: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[2];
124: E = ((E << 30) | (E >>> -30));
125: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[3];
126: D = ((D << 30) | (D >>> -30));
127: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[4];
128: C = ((C << 30) | (C >>> -30));
129: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[5];
130: B = ((B << 30) | (B >>> -30));
131: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[6];
132: A = ((A << 30) | (A >>> -30));
133: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[7];
134: E = ((E << 30) | (E >>> -30));
135: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[8];
136: D = ((D << 30) | (D >>> -30));
137: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[9];
138: C = ((C << 30) | (C >>> -30));
139: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[10];
140: B = ((B << 30) | (B >>> -30));
141: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[11];
142: A = ((A << 30) | (A >>> -30));
143: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[12];
144: E = ((E << 30) | (E >>> -30));
145: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[13];
146: D = ((D << 30) | (D >>> -30));
147: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[14];
148: C = ((C << 30) | (C >>> -30));
149: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[15];
150: B = ((B << 30) | (B >>> -30));
151: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[16];
152: A = ((A << 30) | (A >>> -30));
153: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[17];
154: E = ((E << 30) | (E >>> -30));
155: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[18];
156: D = ((D << 30) | (D >>> -30));
157: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[19];
158: C = ((C << 30) | (C >>> -30));
159: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[20];
160: B = ((B << 30) | (B >>> -30));
161: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[21];
162: A = ((A << 30) | (A >>> -30));
163: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[22];
164: E = ((E << 30) | (E >>> -30));
165: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[23];
166: D = ((D << 30) | (D >>> -30));
167: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[24];
168: C = ((C << 30) | (C >>> -30));
169: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[25];
170: B = ((B << 30) | (B >>> -30));
171: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[26];
172: A = ((A << 30) | (A >>> -30));
173: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[27];
174: E = ((E << 30) | (E >>> -30));
175: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[28];
176: D = ((D << 30) | (D >>> -30));
177: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[29];
178: C = ((C << 30) | (C >>> -30));
179: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[30];
180: B = ((B << 30) | (B >>> -30));
181: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[31];
182: A = ((A << 30) | (A >>> -30));
183: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[32];
184: E = ((E << 30) | (E >>> -30));
185: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[33];
186: D = ((D << 30) | (D >>> -30));
187: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[34];
188: C = ((C << 30) | (C >>> -30));
189: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[35];
190: B = ((B << 30) | (B >>> -30));
191: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[36];
192: A = ((A << 30) | (A >>> -30));
193: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[37];
194: E = ((E << 30) | (E >>> -30));
195: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[38];
196: D = ((D << 30) | (D >>> -30));
197: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[39];
198: C = ((C << 30) | (C >>> -30));
199: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[40];
200: B = ((B << 30) | (B >>> -30));
201: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[41];
202: A = ((A << 30) | (A >>> -30));
203: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[42];
204: E = ((E << 30) | (E >>> -30));
205: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[43];
206: D = ((D << 30) | (D >>> -30));
207: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[44];
208: C = ((C << 30) | (C >>> -30));
209: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[45];
210: B = ((B << 30) | (B >>> -30));
211: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[46];
212: A = ((A << 30) | (A >>> -30));
213: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[47];
214: E = ((E << 30) | (E >>> -30));
215: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[48];
216: D = ((D << 30) | (D >>> -30));
217: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[49];
218: C = ((C << 30) | (C >>> -30));
219: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[50];
220: B = ((B << 30) | (B >>> -30));
221: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[51];
222: A = ((A << 30) | (A >>> -30));
223: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[52];
224: E = ((E << 30) | (E >>> -30));
225: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[53];
226: D = ((D << 30) | (D >>> -30));
227: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[54];
228: C = ((C << 30) | (C >>> -30));
229: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[55];
230: B = ((B << 30) | (B >>> -30));
231: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[56];
232: A = ((A << 30) | (A >>> -30));
233: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[57];
234: E = ((E << 30) | (E >>> -30));
235: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[58];
236: D = ((D << 30) | (D >>> -30));
237: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[59];
238: C = ((C << 30) | (C >>> -30));
239: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[60];
240: B = ((B << 30) | (B >>> -30));
241: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[61];
242: A = ((A << 30) | (A >>> -30));
243: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[62];
244: E = ((E << 30) | (E >>> -30));
245: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[63];
246: D = ((D << 30) | (D >>> -30));
247: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[64];
248: C = ((C << 30) | (C >>> -30));
249: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[65];
250: B = ((B << 30) | (B >>> -30));
251: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[66];
252: A = ((A << 30) | (A >>> -30));
253: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[67];
254: E = ((E << 30) | (E >>> -30));
255: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[68];
256: D = ((D << 30) | (D >>> -30));
257: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[69];
258: C = ((C << 30) | (C >>> -30));
259: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[70];
260: B = ((B << 30) | (B >>> -30));
261: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[71];
262: A = ((A << 30) | (A >>> -30));
263: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[72];
264: E = ((E << 30) | (E >>> -30));
265: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[73];
266: D = ((D << 30) | (D >>> -30));
267: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[74];
268: C = ((C << 30) | (C >>> -30));
269: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[75];
270: B = ((B << 30) | (B >>> -30));
271: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[76];
272: A = ((A << 30) | (A >>> -30));
273: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[77];
274: E = ((E << 30) | (E >>> -30));
275: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[78];
276: D = ((D << 30) | (D >>> -30));
277: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[79];
278: C = ((C << 30) | (C >>> -30));
279:
280: context[0] += A;
281: context[1] += B;
282: context[2] += C;
283: context[3] += D;
284: context[4] += E;
285: }
286:
287: private static int f1(int a, int b, int c) {
288: return (c ^ (a & (b ^ c))) + 0x5A827999;
289: }
290:
291: private static int f2(int a, int b, int c) {
292: return (a ^ b ^ c) + 0x6ED9EBA1;
293: }
294:
295: private static int f3(int a, int b, int c) {
296: return ((a & b) | (c & (a | b))) + 0x8F1BBCDC;
297: }
298:
299: private static int f4(int a, int b, int c) {
300: return (a ^ b ^ c) + 0xCA62C1D6;
301: }
302:
303: // Abstract methods
304: //...........................................................................
305:
306: protected abstract void expand(int[] W);
307: }
|