Source for gnu.java.security.hash.Whirlpool

   1: /* Whirlpool.java -- 
   2:    Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.java.security.hash;
  40: 
  41: import gnu.java.security.Configuration;
  42: import gnu.java.security.Registry;
  43: import gnu.java.security.util.Util;
  44: 
  45: import java.util.logging.Logger;
  46: 
  47: /**
  48:  * Whirlpool, a new 512-bit hashing function operating on messages less than
  49:  * 2 ** 256 bits in length. The function structure is designed according to the
  50:  * Wide Trail strategy and permits a wide variety of implementation trade-offs.
  51:  * <p>
  52:  * This implementation is of Whirlpool Version 3, described in [1] last revised
  53:  * on May 24th, 2003.
  54:  * <p>
  55:  * <b>IMPORTANT</b>: This implementation is not thread-safe.
  56:  * <p>
  57:  * References:
  58:  * <ol>
  59:  *    <li><a href="http://planeta.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html">
  60:  *    The WHIRLPOOL Hashing Function</a>.<br>
  61:  *    <a href="mailto:paulo.barreto@terra.com.br">Paulo S.L.M. Barreto</a> and
  62:  *    <a href="mailto:vincent.rijmen@iaik.tugraz.at">Vincent Rijmen</a>.</li>
  63:  * </ol>
  64:  */
  65: public final class Whirlpool
  66:     extends BaseHash
  67: {
  68:   private static final Logger log = Logger.getLogger(Whirlpool.class.getName());
  69:   private static final int BLOCK_SIZE = 64; // inner block size in bytes
  70: 
  71:   /** The digest of the 0-bit long message. */
  72:   private static final String DIGEST0 =
  73:       "19FA61D75522A4669B44E39C1D2E1726C530232130D407F89AFEE0964997F7A7"
  74:     + "3E83BE698B288FEBCF88E3E03C4F0757EA8964E59B63D93708B138CC42A66EB3";
  75: 
  76:   /** Default number of rounds. */
  77:   private static final int R = 10;
  78: 
  79:   /** Whirlpool S-box; p. 19. */
  80:   private static final String S_box = // p. 19 [WHIRLPOOL]
  81:       "\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152"
  82:     + "\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57"
  83:     + "\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85"
  84:     + "\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8"
  85:     + "\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333"
  86:     + "\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0"
  87:     + "\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE"
  88:     + "\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d"
  89:     + "\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF"
  90:     + "\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A"
  91:     + "\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c"
  92:     + "\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04"
  93:     + "\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB"
  94:     + "\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9"
  95:     + "\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1"
  96:     + "\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
  97: 
  98:   /** The 64-bit lookup tables; section 7.1 p. 13. */
  99:   private static final long[] T0 = new long[256];
 100:   private static final long[] T1 = new long[256];
 101:   private static final long[] T2 = new long[256];
 102:   private static final long[] T3 = new long[256];
 103:   private static final long[] T4 = new long[256];
 104:   private static final long[] T5 = new long[256];
 105:   private static final long[] T6 = new long[256];
 106:   private static final long[] T7 = new long[256];
 107: 
 108:   /** The round constants. */
 109:   private static final long[] rc = new long[R];
 110: 
 111:   /** caches the result of the correctness test, once executed. */
 112:   private static Boolean valid;
 113: 
 114:   /** The 512-bit context as 8 longs. */
 115:   private long H0, H1, H2, H3, H4, H5, H6, H7;
 116: 
 117:   /** Work area for computing the round key schedule. */
 118:   private long k00, k01, k02, k03, k04, k05, k06, k07;
 119:   private long Kr0, Kr1, Kr2, Kr3, Kr4, Kr5, Kr6, Kr7;
 120: 
 121:   /** work area for transforming the 512-bit buffer. */
 122:   private long n0, n1, n2, n3, n4, n5, n6, n7;
 123:   private long nn0, nn1, nn2, nn3, nn4, nn5, nn6, nn7;
 124: 
 125:   /** work area for holding block cipher's intermediate values. */
 126:   private long w0, w1, w2, w3, w4, w5, w6, w7;
 127: 
 128:   static
 129:     {
 130:       long time = System.currentTimeMillis();
 131:       int ROOT = 0x11D; // para. 2.1 [WHIRLPOOL]
 132:       int i, r, j;
 133:       long s1, s2, s4, s5, s8, s9, t;
 134:       char c;
 135:       final byte[] S = new byte[256];
 136:       for (i = 0; i < 256; i++)
 137:         {
 138:           c = S_box.charAt(i >>> 1);
 139: 
 140:           s1 = ((i & 1) == 0 ? c >>> 8 : c) & 0xFFL;
 141:           s2 = s1 << 1;
 142:           if (s2 > 0xFFL)
 143:             s2 ^= ROOT;
 144: 
 145:           s4 = s2 << 1;
 146:           if (s4 > 0xFFL)
 147:             s4 ^= ROOT;
 148: 
 149:           s5 = s4 ^ s1;
 150:           s8 = s4 << 1;
 151:           if (s8 > 0xFFL)
 152:             s8 ^= ROOT;
 153: 
 154:           s9 = s8 ^ s1;
 155: 
 156:           T0[i] = t = s1 << 56 | s1 << 48 | s4 << 40 | s1 << 32
 157:                     | s8 << 24 | s5 << 16 | s2 <<  8 | s9;
 158:           T1[i] = t >>>  8 | t << 56;
 159:           T2[i] = t >>> 16 | t << 48;
 160:           T3[i] = t >>> 24 | t << 40;
 161:           T4[i] = t >>> 32 | t << 32;
 162:           T5[i] = t >>> 40 | t << 24;
 163:           T6[i] = t >>> 48 | t << 16;
 164:           T7[i] = t >>> 56 | t <<  8;
 165:         }
 166:       for (r = 0, i = 0; r < R; )
 167:         rc[r++] = (T0[i++] & 0xFF00000000000000L)
 168:                 ^ (T1[i++] & 0x00FF000000000000L)
 169:                 ^ (T2[i++] & 0x0000FF0000000000L)
 170:                 ^ (T3[i++] & 0x000000FF00000000L)
 171:                 ^ (T4[i++] & 0x00000000FF000000L)
 172:                 ^ (T5[i++] & 0x0000000000FF0000L)
 173:                 ^ (T6[i++] & 0x000000000000FF00L)
 174:                 ^ (T7[i++] & 0x00000000000000FFL);
 175:       time = System.currentTimeMillis() - time;
 176:       if (Configuration.DEBUG)
 177:         {
 178:           log.fine("Static data");
 179:           log.fine("T0[]:");
 180:           StringBuilder sb;
 181:           for (i = 0; i < 64; i++)
 182:             {
 183:               sb = new StringBuilder();
 184:               for (j = 0; j < 4; j++)
 185:                 sb.append("0x").append(Util.toString(T0[i * 4 + j])).append(", ");
 186: 
 187:               log.fine(sb.toString());
 188:             }
 189:           log.fine("T1[]:");
 190:           for (i = 0; i < 64; i++)
 191:             {
 192:               sb = new StringBuilder();
 193:               for (j = 0; j < 4; j++)
 194:                 sb.append("0x").append(Util.toString(T1[i * 4 + j])).append(", ");
 195: 
 196:               log.fine(sb.toString());
 197:             }
 198:           log.fine("T2[]:");
 199:           for (i = 0; i < 64; i++)
 200:             {
 201:               sb = new StringBuilder();
 202:               for (j = 0; j < 4; j++)
 203:                 sb.append("0x").append(Util.toString(T2[i * 4 + j])).append(", ");
 204: 
 205:               log.fine(sb.toString());
 206:             }
 207:           log.fine("T3[]:");
 208:           for (i = 0; i < 64; i++)
 209:             {
 210:               sb = new StringBuilder();
 211:               for (j = 0; j < 4; j++)
 212:                 sb.append("0x").append(Util.toString(T3[i * 4 + j])).append(", ");
 213: 
 214:               log.fine(sb.toString());
 215:             }
 216:           log.fine("\nT4[]:");
 217:           for (i = 0; i < 64; i++)
 218:             {
 219:               sb = new StringBuilder();
 220:               for (j = 0; j < 4; j++)
 221:                 sb.append("0x").append(Util.toString(T4[i * 4 + j])).append(", ");
 222: 
 223:               log.fine(sb.toString());
 224:             }
 225:           log.fine("T5[]:");
 226:           for (i = 0; i < 64; i++)
 227:             {
 228:               sb = new StringBuilder();
 229:               for (j = 0; j < 4; j++)
 230:                 sb.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
 231: 
 232:               log.fine(sb.toString());
 233:             }
 234:           log.fine("T6[]:");
 235:           for (i = 0; i < 64; i++)
 236:             {
 237:               sb = new StringBuilder();
 238:               for (j = 0; j < 4; j++)
 239:                 sb.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
 240: 
 241:               log.fine(sb.toString());
 242:             }
 243:           log.fine("T7[]:");
 244:           for (i = 0; i < 64; i++)
 245:             {
 246:               sb = new StringBuilder();
 247:               for (j = 0; j < 4; j++)
 248:                 sb.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
 249: 
 250:               log.fine(sb.toString());
 251:             }
 252:           log.fine("rc[]:");
 253:           for (i = 0; i < R; i++)
 254:             log.fine("0x" + Util.toString(rc[i]));
 255: 
 256:           log.fine("Total initialization time: " + time + " ms.");
 257:         }
 258:     }
 259: 
 260:   /** Trivial 0-arguments constructor. */
 261:   public Whirlpool()
 262:   {
 263:     super(Registry.WHIRLPOOL_HASH, 20, BLOCK_SIZE);
 264:   }
 265: 
 266:   /**
 267:    * Private constructor for cloning purposes.
 268:    *
 269:    * @param md the instance to clone.
 270:    */
 271:   private Whirlpool(Whirlpool md)
 272:   {
 273:     this();
 274: 
 275:     this.H0 = md.H0;
 276:     this.H1 = md.H1;
 277:     this.H2 = md.H2;
 278:     this.H3 = md.H3;
 279:     this.H4 = md.H4;
 280:     this.H5 = md.H5;
 281:     this.H6 = md.H6;
 282:     this.H7 = md.H7;
 283:     this.count = md.count;
 284:     this.buffer = (byte[]) md.buffer.clone();
 285:   }
 286: 
 287:   public Object clone()
 288:   {
 289:     return (new Whirlpool(this));
 290:   }
 291: 
 292:   protected void transform(byte[] in, int offset)
 293:   {
 294:     // apply mu to the input
 295:     n0 = (in[offset++] & 0xFFL) << 56
 296:        | (in[offset++] & 0xFFL) << 48
 297:        | (in[offset++] & 0xFFL) << 40
 298:        | (in[offset++] & 0xFFL) << 32
 299:        | (in[offset++] & 0xFFL) << 24
 300:        | (in[offset++] & 0xFFL) << 16
 301:        | (in[offset++] & 0xFFL) <<  8
 302:        | (in[offset++] & 0xFFL);
 303:     n1 = (in[offset++] & 0xFFL) << 56
 304:        | (in[offset++] & 0xFFL) << 48
 305:        | (in[offset++] & 0xFFL) << 40
 306:        | (in[offset++] & 0xFFL) << 32
 307:        | (in[offset++] & 0xFFL) << 24
 308:        | (in[offset++] & 0xFFL) << 16
 309:        | (in[offset++] & 0xFFL) <<  8
 310:        | (in[offset++] & 0xFFL);
 311:     n2 = (in[offset++] & 0xFFL) << 56
 312:        | (in[offset++] & 0xFFL) << 48
 313:        | (in[offset++] & 0xFFL) << 40
 314:        | (in[offset++] & 0xFFL) << 32
 315:        | (in[offset++] & 0xFFL) << 24
 316:        | (in[offset++] & 0xFFL) << 16
 317:        | (in[offset++] & 0xFFL) <<  8
 318:        | (in[offset++] & 0xFFL);
 319:     n3 = (in[offset++] & 0xFFL) << 56
 320:        | (in[offset++] & 0xFFL) << 48
 321:        | (in[offset++] & 0xFFL) << 40
 322:        | (in[offset++] & 0xFFL) << 32
 323:        | (in[offset++] & 0xFFL) << 24
 324:        | (in[offset++] & 0xFFL) << 16
 325:        | (in[offset++] & 0xFFL) <<  8
 326:        | (in[offset++] & 0xFFL);
 327:     n4 = (in[offset++] & 0xFFL) << 56
 328:        | (in[offset++] & 0xFFL) << 48
 329:        | (in[offset++] & 0xFFL) << 40
 330:        | (in[offset++] & 0xFFL) << 32
 331:        | (in[offset++] & 0xFFL) << 24
 332:        | (in[offset++] & 0xFFL) << 16
 333:        | (in[offset++] & 0xFFL) <<  8
 334:        | (in[offset++] & 0xFFL);
 335:     n5 = (in[offset++] & 0xFFL) << 56
 336:        | (in[offset++] & 0xFFL) << 48
 337:        | (in[offset++] & 0xFFL) << 40
 338:        | (in[offset++] & 0xFFL) << 32
 339:        | (in[offset++] & 0xFFL) << 24
 340:        | (in[offset++] & 0xFFL) << 16
 341:        | (in[offset++] & 0xFFL) <<  8
 342:        | (in[offset++] & 0xFFL);
 343:     n6 = (in[offset++] & 0xFFL) << 56
 344:        | (in[offset++] & 0xFFL) << 48
 345:        | (in[offset++] & 0xFFL) << 40
 346:        | (in[offset++] & 0xFFL) << 32
 347:        | (in[offset++] & 0xFFL) << 24
 348:        | (in[offset++] & 0xFFL) << 16
 349:        | (in[offset++] & 0xFFL) <<  8
 350:        | (in[offset++] & 0xFFL);
 351:     n7 = (in[offset++] & 0xFFL) << 56
 352:        | (in[offset++] & 0xFFL) << 48
 353:        | (in[offset++] & 0xFFL) << 40
 354:        | (in[offset++] & 0xFFL) << 32
 355:        | (in[offset++] & 0xFFL) << 24
 356:        | (in[offset++] & 0xFFL) << 16
 357:        | (in[offset++] & 0xFFL) <<  8
 358:        | (in[offset++] & 0xFFL);
 359:     // transform K into the key schedule Kr; 0 <= r <= R
 360:     k00 = H0;
 361:     k01 = H1;
 362:     k02 = H2;
 363:     k03 = H3;
 364:     k04 = H4;
 365:     k05 = H5;
 366:     k06 = H6;
 367:     k07 = H7;
 368:     nn0 = n0 ^ k00;
 369:     nn1 = n1 ^ k01;
 370:     nn2 = n2 ^ k02;
 371:     nn3 = n3 ^ k03;
 372:     nn4 = n4 ^ k04;
 373:     nn5 = n5 ^ k05;
 374:     nn6 = n6 ^ k06;
 375:     nn7 = n7 ^ k07;
 376:     // intermediate cipher output
 377:     w0 = w1 = w2 = w3 = w4 = w5 = w6 = w7 = 0L;
 378:     for (int r = 0; r < R; r++)
 379:       {
 380:         // 1. compute intermediate round key schedule by applying ro[rc]
 381:         // to the previous round key schedule --rc being the round constant
 382:         Kr0 = T0[(int)((k00 >> 56) & 0xFFL)]
 383:             ^ T1[(int)((k07 >> 48) & 0xFFL)]
 384:             ^ T2[(int)((k06 >> 40) & 0xFFL)]
 385:             ^ T3[(int)((k05 >> 32) & 0xFFL)]
 386:             ^ T4[(int)((k04 >> 24) & 0xFFL)]
 387:             ^ T5[(int)((k03 >> 16) & 0xFFL)]
 388:             ^ T6[(int)((k02 >>  8) & 0xFFL)]
 389:             ^ T7[(int)( k01        & 0xFFL)] ^ rc[r];
 390:         Kr1 = T0[(int)((k01 >> 56) & 0xFFL)]
 391:             ^ T1[(int)((k00 >> 48) & 0xFFL)]
 392:             ^ T2[(int)((k07 >> 40) & 0xFFL)]
 393:             ^ T3[(int)((k06 >> 32) & 0xFFL)]
 394:             ^ T4[(int)((k05 >> 24) & 0xFFL)]
 395:             ^ T5[(int)((k04 >> 16) & 0xFFL)]
 396:             ^ T6[(int)((k03 >>  8) & 0xFFL)]
 397:             ^ T7[(int)( k02        & 0xFFL)];
 398:         Kr2 = T0[(int)((k02 >> 56) & 0xFFL)]
 399:             ^ T1[(int)((k01 >> 48) & 0xFFL)]
 400:             ^ T2[(int)((k00 >> 40) & 0xFFL)]
 401:             ^ T3[(int)((k07 >> 32) & 0xFFL)]
 402:             ^ T4[(int)((k06 >> 24) & 0xFFL)]
 403:             ^ T5[(int)((k05 >> 16) & 0xFFL)]
 404:             ^ T6[(int)((k04 >>  8) & 0xFFL)]
 405:             ^ T7[(int)( k03        & 0xFFL)];
 406:         Kr3 = T0[(int)((k03 >> 56) & 0xFFL)]
 407:             ^ T1[(int)((k02 >> 48) & 0xFFL)]
 408:             ^ T2[(int)((k01 >> 40) & 0xFFL)]
 409:             ^ T3[(int)((k00 >> 32) & 0xFFL)]
 410:             ^ T4[(int)((k07 >> 24) & 0xFFL)]
 411:             ^ T5[(int)((k06 >> 16) & 0xFFL)]
 412:             ^ T6[(int)((k05 >>  8) & 0xFFL)]
 413:             ^ T7[(int)( k04        & 0xFFL)];
 414:         Kr4 = T0[(int)((k04 >> 56) & 0xFFL)]
 415:             ^ T1[(int)((k03 >> 48) & 0xFFL)]
 416:             ^ T2[(int)((k02 >> 40) & 0xFFL)]
 417:             ^ T3[(int)((k01 >> 32) & 0xFFL)]
 418:             ^ T4[(int)((k00 >> 24) & 0xFFL)]
 419:             ^ T5[(int)((k07 >> 16) & 0xFFL)]
 420:             ^ T6[(int)((k06 >>  8) & 0xFFL)]
 421:             ^ T7[(int)( k05        & 0xFFL)];
 422:         Kr5 = T0[(int)((k05 >> 56) & 0xFFL)]
 423:             ^ T1[(int)((k04 >> 48) & 0xFFL)]
 424:             ^ T2[(int)((k03 >> 40) & 0xFFL)]
 425:             ^ T3[(int)((k02 >> 32) & 0xFFL)]
 426:             ^ T4[(int)((k01 >> 24) & 0xFFL)]
 427:             ^ T5[(int)((k00 >> 16) & 0xFFL)]
 428:             ^ T6[(int)((k07 >>  8) & 0xFFL)]
 429:             ^ T7[(int)( k06        & 0xFFL)];
 430:         Kr6 = T0[(int)((k06 >> 56) & 0xFFL)]
 431:             ^ T1[(int)((k05 >> 48) & 0xFFL)]
 432:             ^ T2[(int)((k04 >> 40) & 0xFFL)]
 433:             ^ T3[(int)((k03 >> 32) & 0xFFL)]
 434:             ^ T4[(int)((k02 >> 24) & 0xFFL)]
 435:             ^ T5[(int)((k01 >> 16) & 0xFFL)]
 436:             ^ T6[(int)((k00 >>  8) & 0xFFL)]
 437:             ^ T7[(int)( k07        & 0xFFL)];
 438:         Kr7 = T0[(int)((k07 >> 56) & 0xFFL)]
 439:             ^ T1[(int)((k06 >> 48) & 0xFFL)]
 440:             ^ T2[(int)((k05 >> 40) & 0xFFL)]
 441:             ^ T3[(int)((k04 >> 32) & 0xFFL)]
 442:             ^ T4[(int)((k03 >> 24) & 0xFFL)]
 443:             ^ T5[(int)((k02 >> 16) & 0xFFL)]
 444:             ^ T6[(int)((k01 >>  8) & 0xFFL)]
 445:             ^ T7[(int)( k00        & 0xFFL)];
 446:         k00 = Kr0;
 447:         k01 = Kr1;
 448:         k02 = Kr2;
 449:         k03 = Kr3;
 450:         k04 = Kr4;
 451:         k05 = Kr5;
 452:         k06 = Kr6;
 453:         k07 = Kr7;
 454:         // 2. incrementally compute the cipher output
 455:         w0 = T0[(int)((nn0 >> 56) & 0xFFL)]
 456:            ^ T1[(int)((nn7 >> 48) & 0xFFL)]
 457:            ^ T2[(int)((nn6 >> 40) & 0xFFL)]
 458:            ^ T3[(int)((nn5 >> 32) & 0xFFL)]
 459:            ^ T4[(int)((nn4 >> 24) & 0xFFL)]
 460:            ^ T5[(int)((nn3 >> 16) & 0xFFL)]
 461:            ^ T6[(int)((nn2 >>  8) & 0xFFL)]
 462:            ^ T7[(int)( nn1        & 0xFFL)] ^ Kr0;
 463:         w1 = T0[(int)((nn1 >> 56) & 0xFFL)]
 464:            ^ T1[(int)((nn0 >> 48) & 0xFFL)]
 465:            ^ T2[(int)((nn7 >> 40) & 0xFFL)]
 466:            ^ T3[(int)((nn6 >> 32) & 0xFFL)]
 467:            ^ T4[(int)((nn5 >> 24) & 0xFFL)]
 468:            ^ T5[(int)((nn4 >> 16) & 0xFFL)]
 469:            ^ T6[(int)((nn3 >>  8) & 0xFFL)]
 470:            ^ T7[(int)( nn2        & 0xFFL)] ^ Kr1;
 471:         w2 = T0[(int)((nn2 >> 56) & 0xFFL)]
 472:            ^ T1[(int)((nn1 >> 48) & 0xFFL)]
 473:            ^ T2[(int)((nn0 >> 40) & 0xFFL)]
 474:            ^ T3[(int)((nn7 >> 32) & 0xFFL)]
 475:            ^ T4[(int)((nn6 >> 24) & 0xFFL)]
 476:            ^ T5[(int)((nn5 >> 16) & 0xFFL)]
 477:            ^ T6[(int)((nn4 >>  8) & 0xFFL)]
 478:            ^ T7[(int)( nn3        & 0xFFL)] ^ Kr2;
 479:         w3 = T0[(int)((nn3 >> 56) & 0xFFL)]
 480:            ^ T1[(int)((nn2 >> 48) & 0xFFL)]
 481:            ^ T2[(int)((nn1 >> 40) & 0xFFL)]
 482:            ^ T3[(int)((nn0 >> 32) & 0xFFL)]
 483:            ^ T4[(int)((nn7 >> 24) & 0xFFL)]
 484:            ^ T5[(int)((nn6 >> 16) & 0xFFL)]
 485:            ^ T6[(int)((nn5 >>  8) & 0xFFL)]
 486:            ^ T7[(int)( nn4        & 0xFFL)] ^ Kr3;
 487:         w4 = T0[(int)((nn4 >> 56) & 0xFFL)]
 488:            ^ T1[(int)((nn3 >> 48) & 0xFFL)]
 489:            ^ T2[(int)((nn2 >> 40) & 0xFFL)]
 490:            ^ T3[(int)((nn1 >> 32) & 0xFFL)]
 491:            ^ T4[(int)((nn0 >> 24) & 0xFFL)]
 492:            ^ T5[(int)((nn7 >> 16) & 0xFFL)]
 493:            ^ T6[(int)((nn6 >>  8) & 0xFFL)]
 494:            ^ T7[(int)( nn5        & 0xFFL)] ^ Kr4;
 495:         w5 = T0[(int)((nn5 >> 56) & 0xFFL)]
 496:            ^ T1[(int)((nn4 >> 48) & 0xFFL)]
 497:            ^ T2[(int)((nn3 >> 40) & 0xFFL)]
 498:            ^ T3[(int)((nn2 >> 32) & 0xFFL)]
 499:            ^ T4[(int)((nn1 >> 24) & 0xFFL)]
 500:            ^ T5[(int)((nn0 >> 16) & 0xFFL)]
 501:            ^ T6[(int)((nn7 >>  8) & 0xFFL)]
 502:            ^ T7[(int)( nn6        & 0xFFL)] ^ Kr5;
 503:         w6 = T0[(int)((nn6 >> 56) & 0xFFL)]
 504:            ^ T1[(int)((nn5 >> 48) & 0xFFL)]
 505:            ^ T2[(int)((nn4 >> 40) & 0xFFL)]
 506:            ^ T3[(int)((nn3 >> 32) & 0xFFL)]
 507:            ^ T4[(int)((nn2 >> 24) & 0xFFL)]
 508:            ^ T5[(int)((nn1 >> 16) & 0xFFL)]
 509:            ^ T6[(int)((nn0 >>  8) & 0xFFL)]
 510:            ^ T7[(int)( nn7        & 0xFFL)] ^ Kr6;
 511:         w7 = T0[(int)((nn7 >> 56) & 0xFFL)]
 512:            ^ T1[(int)((nn6 >> 48) & 0xFFL)]
 513:            ^ T2[(int)((nn5 >> 40) & 0xFFL)]
 514:            ^ T3[(int)((nn4 >> 32) & 0xFFL)]
 515:            ^ T4[(int)((nn3 >> 24) & 0xFFL)]
 516:            ^ T5[(int)((nn2 >> 16) & 0xFFL)]
 517:            ^ T6[(int)((nn1 >>  8) & 0xFFL)]
 518:            ^ T7[(int)( nn0        & 0xFFL)] ^ Kr7;
 519:         nn0 = w0;
 520:         nn1 = w1;
 521:         nn2 = w2;
 522:         nn3 = w3;
 523:         nn4 = w4;
 524:         nn5 = w5;
 525:         nn6 = w6;
 526:         nn7 = w7;
 527:       }
 528:     // apply the Miyaguchi-Preneel hash scheme
 529:     H0 ^= w0 ^ n0;
 530:     H1 ^= w1 ^ n1;
 531:     H2 ^= w2 ^ n2;
 532:     H3 ^= w3 ^ n3;
 533:     H4 ^= w4 ^ n4;
 534:     H5 ^= w5 ^ n5;
 535:     H6 ^= w6 ^ n6;
 536:     H7 ^= w7 ^ n7;
 537:   }
 538: 
 539:   protected byte[] padBuffer()
 540:   {
 541:     // [WHIRLPOOL] p. 6:
 542:     // "...padded with a 1-bit, then with as few 0-bits as necessary to
 543:     // obtain a bit string whose length is an odd multiple of 256, and
 544:     // finally with the 256-bit right-justified binary representation of L."
 545:     // in this implementation we use 'count' as the number of bytes hashed
 546:     // so far. hence the minimal number of bytes added to the message proper
 547:     // are 33 (1 for the 1-bit followed by the 0-bits and the encoding of
 548:     // the count framed in a 256-bit block). our formula is then:
 549:     //        count + 33 + padding = 0 (mod BLOCK_SIZE)
 550:     int n = (int)((count + 33) % BLOCK_SIZE);
 551:     int padding = n == 0 ? 33 : BLOCK_SIZE - n + 33;
 552:     byte[] result = new byte[padding];
 553:     // padding is always binary 1 followed by binary 0s
 554:     result[0] = (byte) 0x80;
 555:     // save (right justified) the number of bits hashed
 556:     long bits = count * 8;
 557:     int i = padding - 8;
 558:     result[i++] = (byte)(bits >>> 56);
 559:     result[i++] = (byte)(bits >>> 48);
 560:     result[i++] = (byte)(bits >>> 40);
 561:     result[i++] = (byte)(bits >>> 32);
 562:     result[i++] = (byte)(bits >>> 24);
 563:     result[i++] = (byte)(bits >>> 16);
 564:     result[i++] = (byte)(bits >>>  8);
 565:     result[i  ] = (byte) bits;
 566:     return result;
 567:   }
 568: 
 569:   protected byte[] getResult()
 570:   {
 571:     // apply inverse mu to the context
 572:     return new byte[] {
 573:       (byte)(H0 >>> 56), (byte)(H0 >>> 48), (byte)(H0 >>> 40), (byte)(H0 >>> 32),
 574:       (byte)(H0 >>> 24), (byte)(H0 >>> 16), (byte)(H0 >>>  8), (byte) H0,
 575:       (byte)(H1 >>> 56), (byte)(H1 >>> 48), (byte)(H1 >>> 40), (byte)(H1 >>> 32),
 576:       (byte)(H1 >>> 24), (byte)(H1 >>> 16), (byte)(H1 >>>  8), (byte) H1,
 577:       (byte)(H2 >>> 56), (byte)(H2 >>> 48), (byte)(H2 >>> 40), (byte)(H2 >>> 32),
 578:       (byte)(H2 >>> 24), (byte)(H2 >>> 16), (byte)(H2 >>>  8), (byte) H2,
 579:       (byte)(H3 >>> 56), (byte)(H3 >>> 48), (byte)(H3 >>> 40), (byte)(H3 >>> 32),
 580:       (byte)(H3 >>> 24), (byte)(H3 >>> 16), (byte)(H3 >>>  8), (byte) H3,
 581:       (byte)(H4 >>> 56), (byte)(H4 >>> 48), (byte)(H4 >>> 40), (byte)(H4 >>> 32),
 582:       (byte)(H4 >>> 24), (byte)(H4 >>> 16), (byte)(H4 >>>  8), (byte) H4,
 583:       (byte)(H5 >>> 56), (byte)(H5 >>> 48), (byte)(H5 >>> 40), (byte)(H5 >>> 32),
 584:       (byte)(H5 >>> 24), (byte)(H5 >>> 16), (byte)(H5 >>>  8), (byte) H5,
 585:       (byte)(H6 >>> 56), (byte)(H6 >>> 48), (byte)(H6 >>> 40), (byte)(H6 >>> 32),
 586:       (byte)(H6 >>> 24), (byte)(H6 >>> 16), (byte)(H6 >>>  8), (byte) H6,
 587:       (byte)(H7 >>> 56), (byte)(H7 >>> 48), (byte)(H7 >>> 40), (byte)(H7 >>> 32),
 588:       (byte)(H7 >>> 24), (byte)(H7 >>> 16), (byte)(H7 >>>  8), (byte) H7 };
 589: 
 590:   }
 591: 
 592:   protected void resetContext()
 593:   {
 594:     H0 = H1 = H2 = H3 = H4 = H5 = H6 = H7 = 0L;
 595:   }
 596: 
 597:   public boolean selfTest()
 598:   {
 599:     if (valid == null)
 600:       {
 601:         String d = Util.toString(new Whirlpool().digest());
 602:         valid = Boolean.valueOf(DIGEST0.equals(d));
 603:       }
 604:     return valid.booleanValue();
 605:   }
 606: }