001: package org.bouncycastle.crypto.digests;
002:
003: import org.bouncycastle.crypto.ExtendedDigest;
004: import org.bouncycastle.util.Arrays;
005:
006: /**
007: * Implementation of WhirlpoolDigest, based on Java source published by Barreto
008: * and Rijmen.
009: *
010: */
011: public final class WhirlpoolDigest implements ExtendedDigest {
012: private static final int BYTE_LENGTH = 64;
013:
014: private static final int DIGEST_LENGTH_BYTES = 512 / 8;
015: private static final int ROUNDS = 10;
016: private static final int REDUCTION_POLYNOMIAL = 0x011d; // 2^8 + 2^4 + 2^3 + 2 + 1;
017:
018: private static final int[] SBOX = { 0x18, 0x23, 0xc6, 0xe8, 0x87,
019: 0xb8, 0x01, 0x4f, 0x36, 0xa6, 0xd2, 0xf5, 0x79, 0x6f, 0x91,
020: 0x52, 0x60, 0xbc, 0x9b, 0x8e, 0xa3, 0x0c, 0x7b, 0x35, 0x1d,
021: 0xe0, 0xd7, 0xc2, 0x2e, 0x4b, 0xfe, 0x57, 0x15, 0x77, 0x37,
022: 0xe5, 0x9f, 0xf0, 0x4a, 0xda, 0x58, 0xc9, 0x29, 0x0a, 0xb1,
023: 0xa0, 0x6b, 0x85, 0xbd, 0x5d, 0x10, 0xf4, 0xcb, 0x3e, 0x05,
024: 0x67, 0xe4, 0x27, 0x41, 0x8b, 0xa7, 0x7d, 0x95, 0xd8, 0xfb,
025: 0xee, 0x7c, 0x66, 0xdd, 0x17, 0x47, 0x9e, 0xca, 0x2d, 0xbf,
026: 0x07, 0xad, 0x5a, 0x83, 0x33, 0x63, 0x02, 0xaa, 0x71, 0xc8,
027: 0x19, 0x49, 0xd9, 0xf2, 0xe3, 0x5b, 0x88, 0x9a, 0x26, 0x32,
028: 0xb0, 0xe9, 0x0f, 0xd5, 0x80, 0xbe, 0xcd, 0x34, 0x48, 0xff,
029: 0x7a, 0x90, 0x5f, 0x20, 0x68, 0x1a, 0xae, 0xb4, 0x54, 0x93,
030: 0x22, 0x64, 0xf1, 0x73, 0x12, 0x40, 0x08, 0xc3, 0xec, 0xdb,
031: 0xa1, 0x8d, 0x3d, 0x97, 0x00, 0xcf, 0x2b, 0x76, 0x82, 0xd6,
032: 0x1b, 0xb5, 0xaf, 0x6a, 0x50, 0x45, 0xf3, 0x30, 0xef, 0x3f,
033: 0x55, 0xa2, 0xea, 0x65, 0xba, 0x2f, 0xc0, 0xde, 0x1c, 0xfd,
034: 0x4d, 0x92, 0x75, 0x06, 0x8a, 0xb2, 0xe6, 0x0e, 0x1f, 0x62,
035: 0xd4, 0xa8, 0x96, 0xf9, 0xc5, 0x25, 0x59, 0x84, 0x72, 0x39,
036: 0x4c, 0x5e, 0x78, 0x38, 0x8c, 0xd1, 0xa5, 0xe2, 0x61, 0xb3,
037: 0x21, 0x9c, 0x1e, 0x43, 0xc7, 0xfc, 0x04, 0x51, 0x99, 0x6d,
038: 0x0d, 0xfa, 0xdf, 0x7e, 0x24, 0x3b, 0xab, 0xce, 0x11, 0x8f,
039: 0x4e, 0xb7, 0xeb, 0x3c, 0x81, 0x94, 0xf7, 0xb9, 0x13, 0x2c,
040: 0xd3, 0xe7, 0x6e, 0xc4, 0x03, 0x56, 0x44, 0x7f, 0xa9, 0x2a,
041: 0xbb, 0xc1, 0x53, 0xdc, 0x0b, 0x9d, 0x6c, 0x31, 0x74, 0xf6,
042: 0x46, 0xac, 0x89, 0x14, 0xe1, 0x16, 0x3a, 0x69, 0x09, 0x70,
043: 0xb6, 0xd0, 0xed, 0xcc, 0x42, 0x98, 0xa4, 0x28, 0x5c, 0xf8,
044: 0x86 };
045:
046: private static final long[] C0 = new long[256];
047: private static final long[] C1 = new long[256];
048: private static final long[] C2 = new long[256];
049: private static final long[] C3 = new long[256];
050: private static final long[] C4 = new long[256];
051: private static final long[] C5 = new long[256];
052: private static final long[] C6 = new long[256];
053: private static final long[] C7 = new long[256];
054:
055: private final long[] _rc = new long[ROUNDS + 1];
056:
057: public WhirlpoolDigest() {
058: for (int i = 0; i < 256; i++) {
059: int v1 = SBOX[i];
060: int v2 = maskWithReductionPolynomial(v1 << 1);
061: int v4 = maskWithReductionPolynomial(v2 << 1);
062: int v5 = v4 ^ v1;
063: int v8 = maskWithReductionPolynomial(v4 << 1);
064: int v9 = v8 ^ v1;
065:
066: C0[i] = packIntoLong(v1, v1, v4, v1, v8, v5, v2, v9);
067: C1[i] = packIntoLong(v9, v1, v1, v4, v1, v8, v5, v2);
068: C2[i] = packIntoLong(v2, v9, v1, v1, v4, v1, v8, v5);
069: C3[i] = packIntoLong(v5, v2, v9, v1, v1, v4, v1, v8);
070: C4[i] = packIntoLong(v8, v5, v2, v9, v1, v1, v4, v1);
071: C5[i] = packIntoLong(v1, v8, v5, v2, v9, v1, v1, v4);
072: C6[i] = packIntoLong(v4, v1, v8, v5, v2, v9, v1, v1);
073: C7[i] = packIntoLong(v1, v4, v1, v8, v5, v2, v9, v1);
074:
075: }
076:
077: _rc[0] = 0L;
078: for (int r = 1; r <= ROUNDS; r++) {
079: int i = 8 * (r - 1);
080: _rc[r] = (C0[i] & 0xff00000000000000L)
081: ^ (C1[i + 1] & 0x00ff000000000000L)
082: ^ (C2[i + 2] & 0x0000ff0000000000L)
083: ^ (C3[i + 3] & 0x000000ff00000000L)
084: ^ (C4[i + 4] & 0x00000000ff000000L)
085: ^ (C5[i + 5] & 0x0000000000ff0000L)
086: ^ (C6[i + 6] & 0x000000000000ff00L)
087: ^ (C7[i + 7] & 0x00000000000000ffL);
088: }
089:
090: }
091:
092: private long packIntoLong(int b7, int b6, int b5, int b4, int b3,
093: int b2, int b1, int b0) {
094: return ((long) b7 << 56) ^ ((long) b6 << 48)
095: ^ ((long) b5 << 40) ^ ((long) b4 << 32)
096: ^ ((long) b3 << 24) ^ ((long) b2 << 16)
097: ^ ((long) b1 << 8) ^ b0;
098: }
099:
100: /*
101: * int's are used to prevent sign extension. The values that are really being used are
102: * actually just 0..255
103: */
104: private int maskWithReductionPolynomial(int input) {
105: int rv = input;
106: if (rv >= 0x100L) // high bit set
107: {
108: rv ^= REDUCTION_POLYNOMIAL; // reduced by the polynomial
109: }
110: return rv;
111: }
112:
113: // --------------------------------------------------------------------------------------//
114:
115: // -- buffer information --
116: private static final int BITCOUNT_ARRAY_SIZE = 32;
117: private byte[] _buffer = new byte[64];
118: private int _bufferPos = 0;
119: private short[] _bitCount = new short[BITCOUNT_ARRAY_SIZE];
120:
121: // -- internal hash state --
122: private long[] _hash = new long[8];
123: private long[] _K = new long[8]; // the round key
124: private long[] _L = new long[8];
125: private long[] _block = new long[8]; // mu (buffer)
126: private long[] _state = new long[8]; // the current "cipher" state
127:
128: /**
129: * Copy constructor. This will copy the state of the provided message
130: * digest.
131: */
132: public WhirlpoolDigest(WhirlpoolDigest originalDigest) {
133: System.arraycopy(originalDigest._rc, 0, _rc, 0, _rc.length);
134:
135: System.arraycopy(originalDigest._buffer, 0, _buffer, 0,
136: _buffer.length);
137:
138: this ._bufferPos = originalDigest._bufferPos;
139: System.arraycopy(originalDigest._bitCount, 0, _bitCount, 0,
140: _bitCount.length);
141:
142: // -- internal hash state --
143: System.arraycopy(originalDigest._hash, 0, _hash, 0,
144: _hash.length);
145: System.arraycopy(originalDigest._K, 0, _K, 0, _K.length);
146: System.arraycopy(originalDigest._L, 0, _L, 0, _L.length);
147: System.arraycopy(originalDigest._block, 0, _block, 0,
148: _block.length);
149: System.arraycopy(originalDigest._state, 0, _state, 0,
150: _state.length);
151: }
152:
153: public String getAlgorithmName() {
154: return "Whirlpool";
155: }
156:
157: public int getDigestSize() {
158: return DIGEST_LENGTH_BYTES;
159: }
160:
161: public int doFinal(byte[] out, int outOff) {
162: // sets out[outOff] .. out[outOff+DIGEST_LENGTH_BYTES]
163: finish();
164:
165: for (int i = 0; i < 8; i++) {
166: convertLongToByteArray(_hash[i], out, outOff + (i * 8));
167: }
168:
169: reset();
170: return getDigestSize();
171: }
172:
173: /**
174: * reset the chaining variables
175: */
176: public void reset() {
177: // set variables to null, blank, whatever
178: _bufferPos = 0;
179: Arrays.fill(_bitCount, (short) 0);
180: Arrays.fill(_buffer, (byte) 0);
181: Arrays.fill(_hash, 0);
182: Arrays.fill(_K, 0);
183: Arrays.fill(_L, 0);
184: Arrays.fill(_block, 0);
185: Arrays.fill(_state, 0);
186: }
187:
188: // this takes a buffer of information and fills the block
189: private void processFilledBuffer(byte[] in, int inOff) {
190: // copies into the block...
191: for (int i = 0; i < _state.length; i++) {
192: _block[i] = bytesToLongFromBuffer(_buffer, i * 8);
193: }
194: processBlock();
195: _bufferPos = 0;
196: Arrays.fill(_buffer, (byte) 0);
197: }
198:
199: private long bytesToLongFromBuffer(byte[] buffer, int startPos) {
200: long rv = (((buffer[startPos + 0] & 0xffL) << 56)
201: | ((buffer[startPos + 1] & 0xffL) << 48)
202: | ((buffer[startPos + 2] & 0xffL) << 40)
203: | ((buffer[startPos + 3] & 0xffL) << 32)
204: | ((buffer[startPos + 4] & 0xffL) << 24)
205: | ((buffer[startPos + 5] & 0xffL) << 16)
206: | ((buffer[startPos + 6] & 0xffL) << 8) | ((buffer[startPos + 7]) & 0xffL));
207:
208: return rv;
209: }
210:
211: private void convertLongToByteArray(long inputLong,
212: byte[] outputArray, int offSet) {
213: for (int i = 0; i < 8; i++) {
214: outputArray[offSet + i] = (byte) ((inputLong >> (56 - (i * 8))) & 0xff);
215: }
216: }
217:
218: protected void processBlock() {
219: // buffer contents have been transferred to the _block[] array via
220: // processFilledBuffer
221:
222: // compute and apply K^0
223: for (int i = 0; i < 8; i++) {
224: _state[i] = _block[i] ^ (_K[i] = _hash[i]);
225: }
226:
227: // iterate over the rounds
228: for (int round = 1; round <= ROUNDS; round++) {
229: for (int i = 0; i < 8; i++) {
230: _L[i] = 0;
231: _L[i] ^= C0[(int) (_K[(i - 0) & 7] >>> 56) & 0xff];
232: _L[i] ^= C1[(int) (_K[(i - 1) & 7] >>> 48) & 0xff];
233: _L[i] ^= C2[(int) (_K[(i - 2) & 7] >>> 40) & 0xff];
234: _L[i] ^= C3[(int) (_K[(i - 3) & 7] >>> 32) & 0xff];
235: _L[i] ^= C4[(int) (_K[(i - 4) & 7] >>> 24) & 0xff];
236: _L[i] ^= C5[(int) (_K[(i - 5) & 7] >>> 16) & 0xff];
237: _L[i] ^= C6[(int) (_K[(i - 6) & 7] >>> 8) & 0xff];
238: _L[i] ^= C7[(int) (_K[(i - 7) & 7]) & 0xff];
239: }
240:
241: System.arraycopy(_L, 0, _K, 0, _K.length);
242:
243: _K[0] ^= _rc[round];
244:
245: // apply the round transformation
246: for (int i = 0; i < 8; i++) {
247: _L[i] = _K[i];
248:
249: _L[i] ^= C0[(int) (_state[(i - 0) & 7] >>> 56) & 0xff];
250: _L[i] ^= C1[(int) (_state[(i - 1) & 7] >>> 48) & 0xff];
251: _L[i] ^= C2[(int) (_state[(i - 2) & 7] >>> 40) & 0xff];
252: _L[i] ^= C3[(int) (_state[(i - 3) & 7] >>> 32) & 0xff];
253: _L[i] ^= C4[(int) (_state[(i - 4) & 7] >>> 24) & 0xff];
254: _L[i] ^= C5[(int) (_state[(i - 5) & 7] >>> 16) & 0xff];
255: _L[i] ^= C6[(int) (_state[(i - 6) & 7] >>> 8) & 0xff];
256: _L[i] ^= C7[(int) (_state[(i - 7) & 7]) & 0xff];
257: }
258:
259: // save the current state
260: System.arraycopy(_L, 0, _state, 0, _state.length);
261: }
262:
263: // apply Miuaguchi-Preneel compression
264: for (int i = 0; i < 8; i++) {
265: _hash[i] ^= _state[i] ^ _block[i];
266: }
267:
268: }
269:
270: public void update(byte in) {
271: _buffer[_bufferPos] = in;
272:
273: //System.out.println("adding to buffer = "+_buffer[_bufferPos]);
274:
275: ++_bufferPos;
276:
277: if (_bufferPos == _buffer.length) {
278: processFilledBuffer(_buffer, 0);
279: }
280:
281: increment();
282: }
283:
284: /*
285: * increment() can be implemented in this way using 2 arrays or
286: * by having some temporary variables that are used to set the
287: * value provided by EIGHT[i] and carry within the loop.
288: *
289: * not having done any timing, this seems likely to be faster
290: * at the slight expense of 32*(sizeof short) bytes
291: */
292: private static final short[] EIGHT = new short[BITCOUNT_ARRAY_SIZE];
293: static {
294: EIGHT[BITCOUNT_ARRAY_SIZE - 1] = 8;
295: }
296:
297: private void increment() {
298: int carry = 0;
299: for (int i = _bitCount.length - 1; i >= 0; i--) {
300: int sum = (_bitCount[i] & 0xff) + EIGHT[i] + carry;
301:
302: carry = sum >>> 8;
303: _bitCount[i] = (short) (sum & 0xff);
304: }
305: }
306:
307: public void update(byte[] in, int inOff, int len) {
308: while (len > 0) {
309: update(in[inOff]);
310: ++inOff;
311: --len;
312: }
313:
314: }
315:
316: private void finish() {
317: /*
318: * this makes a copy of the current bit length. at the expense of an
319: * object creation of 32 bytes rather than providing a _stopCounting
320: * boolean which was the alternative I could think of.
321: */
322: byte[] bitLength = copyBitLength();
323:
324: _buffer[_bufferPos++] |= 0x80;
325:
326: if (_bufferPos == _buffer.length) {
327: processFilledBuffer(_buffer, 0);
328: }
329:
330: /*
331: * Final block contains
332: * [ ... data .... ][0][0][0][ length ]
333: *
334: * if [ length ] cannot fit. Need to create a new block.
335: */
336: if (_bufferPos > 32) {
337: while (_bufferPos != 0) {
338: update((byte) 0);
339: }
340: }
341:
342: while (_bufferPos <= 32) {
343: update((byte) 0);
344: }
345:
346: // copy the length information to the final 32 bytes of the
347: // 64 byte block....
348: System.arraycopy(bitLength, 0, _buffer, 32, bitLength.length);
349:
350: processFilledBuffer(_buffer, 0);
351: }
352:
353: private byte[] copyBitLength() {
354: byte[] rv = new byte[BITCOUNT_ARRAY_SIZE];
355: for (int i = 0; i < rv.length; i++) {
356: rv[i] = (byte) (_bitCount[i] & 0xff);
357: }
358: return rv;
359: }
360:
361: public int getByteLength() {
362: return BYTE_LENGTH;
363: }
364: }
|