View | Details | Return to bug 26067 | Differences between
and this patch

Collapse All | Expand All

(-)BaseHash.java (-42 / +19 lines)
Lines 39-52 Link Here
39
package gnu.java.security.hash;
39
package gnu.java.security.hash;
40
40
41
/**
41
/**
42
 * <p>A base abstract class to facilitate hash implementations.</p>
42
 * A base abstract class to facilitate hash implementations.
43
 */
43
 */
44
public abstract class BaseHash implements IMessageDigest
44
public abstract class BaseHash
45
    implements IMessageDigest
45
{
46
{
46
47
  // Constants and variables
48
  // -------------------------------------------------------------------------
49
50
  /** The canonical name prefix of the hash. */
47
  /** The canonical name prefix of the hash. */
51
  protected String name;
48
  protected String name;
52
49
Lines 62-73 Link Here
62
  /** Temporary input buffer. */
59
  /** Temporary input buffer. */
63
  protected byte[] buffer;
60
  protected byte[] buffer;
64
61
65
  // Constructor(s)
66
  // -------------------------------------------------------------------------
67
68
  /**
62
  /**
69
   * <p>Trivial constructor for use by concrete subclasses.</p>
63
   * Trivial constructor for use by concrete subclasses.
70
   *
64
   * 
71
   * @param name the canonical name prefix of this instance.
65
   * @param name the canonical name prefix of this instance.
72
   * @param hashSize the block size of the output in bytes.
66
   * @param hashSize the block size of the output in bytes.
73
   * @param blockSize the block size of the internal transform.
67
   * @param blockSize the block size of the internal transform.
Lines 84-97 Link Here
84
    resetContext();
78
    resetContext();
85
  }
79
  }
86
80
87
  // Class methods
88
  // -------------------------------------------------------------------------
89
90
  // Instance methods
91
  // -------------------------------------------------------------------------
92
93
  // IMessageDigest interface implementation ---------------------------------
94
95
  public String name()
81
  public String name()
96
  {
82
  {
97
    return name;
83
    return name;
Lines 114-122 Link Here
114
    count++;
100
    count++;
115
    buffer[i] = b;
101
    buffer[i] = b;
116
    if (i == (blockSize - 1))
102
    if (i == (blockSize - 1))
117
      {
103
      transform(buffer, 0);
118
        transform(buffer, 0);
119
      }
120
  }
104
  }
121
105
122
  public void update(byte[] b)
106
  public void update(byte[] b)
Lines 136-151 Link Here
136
        System.arraycopy(b, offset, buffer, n, partLen);
120
        System.arraycopy(b, offset, buffer, n, partLen);
137
        transform(buffer, 0);
121
        transform(buffer, 0);
138
        for (i = partLen; i + blockSize - 1 < len; i += blockSize)
122
        for (i = partLen; i + blockSize - 1 < len; i += blockSize)
139
          {
123
          transform(b, offset + i);
140
            transform(b, offset + i);
124
141
          }
142
        n = 0;
125
        n = 0;
143
      }
126
      }
144
127
145
    if (i < len)
128
    if (i < len)
146
      {
129
      System.arraycopy(b, offset + i, buffer, n, len - i);
147
        System.arraycopy(b, offset + i, buffer, n, len - i);
148
      }
149
  }
130
  }
150
131
151
  public byte[] digest()
132
  public byte[] digest()
Lines 163-193 Link Here
163
  { // reset this instance for future re-use
144
  { // reset this instance for future re-use
164
    count = 0L;
145
    count = 0L;
165
    for (int i = 0; i < blockSize;)
146
    for (int i = 0; i < blockSize;)
166
      {
147
      buffer[i++] = 0;
167
        buffer[i++] = 0;
168
      }
169
148
170
    resetContext();
149
    resetContext();
171
  }
150
  }
172
151
173
  // methods to be implemented by concrete subclasses ------------------------
174
175
  public abstract Object clone();
152
  public abstract Object clone();
176
153
177
  public abstract boolean selfTest();
154
  public abstract boolean selfTest();
178
155
179
  /**
156
  /**
180
   * <p>Returns the byte array to use as padding before completing a hash
157
   * Returns the byte array to use as padding before completing a hash
181
   * operation.</p>
158
   * operation.
182
   *
159
   * 
183
   * @return the bytes to pad the remaining bytes in the buffer before
160
   * @return the bytes to pad the remaining bytes in the buffer before
184
   * completing a hash operation.
161
   *         completing a hash operation.
185
   */
162
   */
186
  protected abstract byte[] padBuffer();
163
  protected abstract byte[] padBuffer();
187
164
188
  /**
165
  /**
189
   * <p>Constructs the result from the contents of the current context.</p>
166
   * Constructs the result from the contents of the current context.
190
   *
167
   * 
191
   * @return the output of the completed hash operation.
168
   * @return the output of the completed hash operation.
192
   */
169
   */
193
  protected abstract byte[] getResult();
170
  protected abstract byte[] getResult();
Lines 196-206 Link Here
196
  protected abstract void resetContext();
173
  protected abstract void resetContext();
197
174
198
  /**
175
  /**
199
   * <p>The block digest transformation per se.</p>
176
   * The block digest transformation per se.
200
   *
177
   * 
201
   * @param in the <i>blockSize</i> long block, as an array of bytes to digest.
178
   * @param in the <i>blockSize</i> long block, as an array of bytes to digest.
202
   * @param offset the index where the data to digest is located within the
179
   * @param offset the index where the data to digest is located within the
203
   * input buffer.
180
   *          input buffer.
204
   */
181
   */
205
  protected abstract void transform(byte[] in, int offset);
182
  protected abstract void transform(byte[] in, int offset);
206
}
183
}
(-)HashFactory.java (-67 / +24 lines)
Lines 45-157 Link Here
45
import java.util.Set;
45
import java.util.Set;
46
46
47
/**
47
/**
48
 * <p>A <i>Factory</i> to instantiate message digest algorithm instances.</p>
48
 * A <i>Factory</i> to instantiate message digest algorithm instances.
49
 */
49
 */
50
public class HashFactory
50
public class HashFactory
51
{
51
{
52
53
  // Constants and variables
54
  // -------------------------------------------------------------------------
55
56
  // Constructor(s)
57
  // -------------------------------------------------------------------------
58
59
  /** Trivial constructor to enforce <i>Singleton</i> pattern. */
52
  /** Trivial constructor to enforce <i>Singleton</i> pattern. */
60
  private HashFactory()
53
  private HashFactory()
61
  {
54
  {
62
    super();
55
    super();
63
  }
56
  }
64
57
65
  // Class methods
66
  // -------------------------------------------------------------------------
67
68
  /**
58
  /**
69
   * <p>Return an instance of a hash algorithm given its name.</p>
59
   * Return an instance of a hash algorithm given its name.
70
   *
60
   * 
71
   * @param name the name of the hash algorithm.
61
   * @param name the name of the hash algorithm.
72
   * @return an instance of the hash algorithm, or null if none found.
62
   * @return an instance of the hash algorithm, or null if none found.
73
   * @exception InternalError if the implementation does not pass its self-
63
   * @exception InternalError if the implementation does not pass its self-
74
   * test.
64
   *              test.
75
   */
65
   */
76
  public static IMessageDigest getInstance(String name)
66
  public static IMessageDigest getInstance(String name)
77
  {
67
  {
78
    if (name == null)
68
    if (name == null)
79
      {
69
      return null;
80
        return null;
81
      }
82
70
83
    name = name.trim();
71
    name = name.trim();
84
    IMessageDigest result = null;
72
    IMessageDigest result = null;
85
    if (name.equalsIgnoreCase(Registry.WHIRLPOOL_HASH))
73
    if (name.equalsIgnoreCase(Registry.WHIRLPOOL_HASH))
86
      {
74
      result = new Whirlpool();
87
        result = new Whirlpool();
88
      }
89
    else if (name.equalsIgnoreCase(Registry.RIPEMD128_HASH)
75
    else if (name.equalsIgnoreCase(Registry.RIPEMD128_HASH)
90
             || name.equalsIgnoreCase(Registry.RIPEMD_128_HASH))
76
             || name.equalsIgnoreCase(Registry.RIPEMD_128_HASH))
91
      {
77
      result = new RipeMD128();
92
        result = new RipeMD128();
93
      }
94
    else if (name.equalsIgnoreCase(Registry.RIPEMD160_HASH)
78
    else if (name.equalsIgnoreCase(Registry.RIPEMD160_HASH)
95
             || name.equalsIgnoreCase(Registry.RIPEMD_160_HASH))
79
             || name.equalsIgnoreCase(Registry.RIPEMD_160_HASH))
96
      {
80
      result = new RipeMD160();
97
        result = new RipeMD160();
98
      }
99
    else if (name.equalsIgnoreCase(Registry.SHA160_HASH)
81
    else if (name.equalsIgnoreCase(Registry.SHA160_HASH)
100
             || name.equalsIgnoreCase(Registry.SHA_1_HASH)
82
             || name.equalsIgnoreCase(Registry.SHA_1_HASH)
101
             || name.equalsIgnoreCase(Registry.SHA1_HASH)
83
             || name.equalsIgnoreCase(Registry.SHA1_HASH)
102
             || name.equalsIgnoreCase(Registry.SHA_HASH))
84
             || name.equalsIgnoreCase(Registry.SHA_HASH))
103
      {
85
      result = new Sha160();
104
        result = new Sha160();
105
      }
106
    else if (name.equalsIgnoreCase(Registry.SHA256_HASH))
86
    else if (name.equalsIgnoreCase(Registry.SHA256_HASH))
107
      {
87
      result = new Sha256();
108
        result = new Sha256();
109
      }
110
    else if (name.equalsIgnoreCase(Registry.SHA384_HASH))
88
    else if (name.equalsIgnoreCase(Registry.SHA384_HASH))
111
      {
89
      result = new Sha384();
112
        result = new Sha384();
113
      }
114
    else if (name.equalsIgnoreCase(Registry.SHA512_HASH))
90
    else if (name.equalsIgnoreCase(Registry.SHA512_HASH))
115
      {
91
      result = new Sha512();
116
        result = new Sha512();
117
      }
118
    else if (name.equalsIgnoreCase(Registry.TIGER_HASH))
92
    else if (name.equalsIgnoreCase(Registry.TIGER_HASH))
119
      {
93
      result = new Tiger();
120
        result = new Tiger();
121
      }
122
    else if (name.equalsIgnoreCase(Registry.HAVAL_HASH))
94
    else if (name.equalsIgnoreCase(Registry.HAVAL_HASH))
123
      {
95
      result = new Haval();
124
        result = new Haval();
125
      }
126
    else if (name.equalsIgnoreCase(Registry.MD5_HASH))
96
    else if (name.equalsIgnoreCase(Registry.MD5_HASH))
127
      {
97
      result = new MD5();
128
        result = new MD5();
129
      }
130
    else if (name.equalsIgnoreCase(Registry.MD4_HASH))
98
    else if (name.equalsIgnoreCase(Registry.MD4_HASH))
131
      {
99
      result = new MD4();
132
        result = new MD4();
133
      }
134
    else if (name.equalsIgnoreCase(Registry.MD2_HASH))
100
    else if (name.equalsIgnoreCase(Registry.MD2_HASH))
135
      {
101
      result = new MD2();
136
        result = new MD2();
137
      }
138
    else if (name.equalsIgnoreCase(Registry.HAVAL_HASH))
102
    else if (name.equalsIgnoreCase(Registry.HAVAL_HASH))
139
      {
103
      result = new Haval();
140
        result = new Haval();
104
141
      }
105
    if (result != null && ! result.selfTest())
142
106
      throw new InternalError(result.name());
143
    if (result != null && !result.selfTest())
144
      {
145
        throw new InternalError(result.name());
146
      }
147
107
148
    return result;
108
    return result;
149
  }
109
  }
150
110
151
  /**
111
  /**
152
   * <p>Returns a {@link Set} of names of hash algorithms supported by this
112
   * Returns a {@link Set} of names of hash algorithms supported by this
153
   * <i>Factory</i>.</p>
113
   * <i>Factory</i>.
154
   *
114
   * 
155
   * @return a {@link Set} of hash names (Strings).
115
   * @return a {@link Set} of hash names (Strings).
156
   */
116
   */
157
  public static final Set getNames()
117
  public static final Set getNames()
Lines 172-178 Link Here
172
132
173
    return Collections.unmodifiableSet(hs);
133
    return Collections.unmodifiableSet(hs);
174
  }
134
  }
175
176
  // Instance methods
177
  // -------------------------------------------------------------------------
178
}
135
}
(-)Haval.java (-181 / +229 lines)
Lines 42-66 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>The <i>HAVAL</i> message-digest algorithm is a variable output length,
45
 * The <i>HAVAL</i> message-digest algorithm is a variable output length, with
46
 * with variable number of rounds. By default, this implementation allows
46
 * variable number of rounds. By default, this implementation allows <i>HAVAL</i>
47
 * <i>HAVAL</i> to be used as a drop-in replacement for <i>MD5</i>.</p>
47
 * to be used as a drop-in replacement for <i>MD5</i>.
48
 *
48
 * <p>
49
 * <p>References:</p>
49
 * References:
50
 *
51
 * <ol>
50
 * <ol>
52
 *    <li>HAVAL - A One-Way Hashing Algorithm with Variable Length of Output<br>
51
 * <li>HAVAL - A One-Way Hashing Algorithm with Variable Length of Output<br>
53
 *    Advances in Cryptology - AUSCRYPT'92, Lecture Notes in Computer Science,<br>
52
 * Advances in Cryptology - AUSCRYPT'92, Lecture Notes in Computer Science,<br>
54
 *    Springer-Verlag, 1993; <br>
53
 * Springer-Verlag, 1993; <br>
55
 *    Y. Zheng, J. Pieprzyk and J. Seberry.</li>
54
 * Y. Zheng, J. Pieprzyk and J. Seberry.</li>
56
 * </ol>
55
 * </ol>
57
 */
56
 */
58
public class Haval extends BaseHash
57
public class Haval
58
    extends BaseHash
59
{
59
{
60
61
  // Constants and variables
62
  // -------------------------------------------------------------------------
63
64
  public static final int HAVAL_VERSION = 1;
60
  public static final int HAVAL_VERSION = 1;
65
61
66
  public static final int HAVAL_128_BIT = 16;
62
  public static final int HAVAL_128_BIT = 16;
Lines 88-107 Link Here
88
84
89
  /**
85
  /**
90
   * Number of HAVAL rounds. Allowed values are integers in the range <code>3
86
   * Number of HAVAL rounds. Allowed values are integers in the range <code>3
91
   * .. 5</code>. The default is <code>3</code>.
87
   * .. 5</code>.
88
   * The default is <code>3</code>.
92
   */
89
   */
93
  private int rounds = HAVAL_3_ROUND;
90
  private int rounds = HAVAL_3_ROUND;
94
91
95
  /** 128-bit interim result. */
92
  /** 128-bit interim result. */
96
  private int h0, h1, h2, h3, h4, h5, h6, h7;
93
  private int h0, h1, h2, h3, h4, h5, h6, h7;
97
94
98
  // Constructor(s)
99
  // -------------------------------------------------------------------------
100
101
  /**
95
  /**
102
   * <p>Calls the constructor with two argument using {@link #HAVAL_128_BIT} as
96
   * Calls the constructor with two argument using {@link #HAVAL_128_BIT} as the
103
   * the value for the output size (i.e. <code>128</code> bits, and
97
   * value for the output size (i.e. <code>128</code> bits, and
104
   * {@link #HAVAL_3_ROUND} for the value of number of rounds.</p>
98
   * {@link #HAVAL_3_ROUND} for the value of number of rounds.
105
   */
99
   */
106
  public Haval()
100
  public Haval()
107
  {
101
  {
Lines 109-117 Link Here
109
  }
103
  }
110
104
111
  /**
105
  /**
112
   * <p>Calls the constructor with two arguments using the designated output
106
   * Calls the constructor with two arguments using the designated output size,
113
   * size, and {@link #HAVAL_3_ROUND} for the value of number of rounds.</p>
107
   * and {@link #HAVAL_3_ROUND} for the value of number of rounds.
114
   *
108
   * 
115
   * @param size the output size in bytes of this instance.
109
   * @param size the output size in bytes of this instance.
116
   * @throws IllegalArgumentException if the designated output size is invalid.
110
   * @throws IllegalArgumentException if the designated output size is invalid.
117
   * @see #HAVAL_128_BIT
111
   * @see #HAVAL_128_BIT
Lines 126-141 Link Here
126
  }
120
  }
127
121
128
  /**
122
  /**
129
   * <p>Constructs a <code>Haval</code> instance with the designated output
123
   * Constructs a <code>Haval</code> instance with the designated output size
130
   * size (in bytes).  Valid output <code>size</code> values are <code>16</code>,
124
   * (in bytes). Valid output <code>size</code> values are <code>16</code>,
131
   * <code>20</code>, <code>24</code>, <code>28</code> and <code>32</code>.
125
   * <code>20</code>, <code>24</code>, <code>28</code> and
132
   * Valid values for <code>rounds</code> are in the range <code>3..5</code>
126
   * <code>32</code>. Valid values for <code>rounds</code> are in the range
133
   * inclusive.</p>
127
   * <code>3..5</code> inclusive.
134
   *
128
   * 
135
   * @param size the output size in bytes of this instance.
129
   * @param size the output size in bytes of this instance.
136
   * @param rounds the number of rounds to apply when transforming data.
130
   * @param rounds the number of rounds to apply when transforming data.
137
   * @throws IllegalArgumentException if the designated output size is invalid,
131
   * @throws IllegalArgumentException if the designated output size is invalid,
138
   * or if the number of rounds is invalid.
132
   *           or if the number of rounds is invalid.
139
   * @see #HAVAL_128_BIT
133
   * @see #HAVAL_128_BIT
140
   * @see #HAVAL_160_BIT
134
   * @see #HAVAL_160_BIT
141
   * @see #HAVAL_192_BIT
135
   * @see #HAVAL_192_BIT
Lines 149-172 Link Here
149
  {
143
  {
150
    super(Registry.HAVAL_HASH, size, BLOCK_SIZE);
144
    super(Registry.HAVAL_HASH, size, BLOCK_SIZE);
151
145
152
    if (size != HAVAL_128_BIT && size != HAVAL_160_BIT && size != HAVAL_192_BIT
146
    if (size != HAVAL_128_BIT
153
        && size != HAVAL_224_BIT && size != HAVAL_256_BIT)
147
        && size != HAVAL_160_BIT
154
      {
148
        && size != HAVAL_192_BIT
155
        throw new IllegalArgumentException("Invalid HAVAL output size");
149
        && size != HAVAL_224_BIT
156
      }
150
        && size != HAVAL_256_BIT)
151
      throw new IllegalArgumentException("Invalid HAVAL output size");
157
152
158
    if (rounds != HAVAL_3_ROUND && rounds != HAVAL_4_ROUND
153
    if (rounds != HAVAL_3_ROUND
154
        && rounds != HAVAL_4_ROUND
159
        && rounds != HAVAL_5_ROUND)
155
        && rounds != HAVAL_5_ROUND)
160
      {
156
      throw new IllegalArgumentException("Invalid HAVAL number of rounds");
161
        throw new IllegalArgumentException("Invalid HAVAL number of rounds");
162
      }
163
157
164
    this.rounds = rounds;
158
    this.rounds = rounds;
165
  }
159
  }
166
160
167
  /**
161
  /**
168
   * <p>Private constructor for cloning purposes.</p>
162
   * Private constructor for cloning purposes.
169
   *
163
   * 
170
   * @param md the instance to clone.
164
   * @param md the instance to clone.
171
   */
165
   */
172
  private Haval(Haval md)
166
  private Haval(Haval md)
Lines 185-277 Link Here
185
    this.buffer = (byte[]) md.buffer.clone();
179
    this.buffer = (byte[]) md.buffer.clone();
186
  }
180
  }
187
181
188
  // Constructor(s)
189
  // -------------------------------------------------------------------------
190
191
  // Class methods
192
  // -------------------------------------------------------------------------
193
194
  // Instance methods
195
  // -------------------------------------------------------------------------
196
197
  // java.lang.Cloneable interface implementation ----------------------------
198
199
  public Object clone()
182
  public Object clone()
200
  {
183
  {
201
    return new Haval(this);
184
    return new Haval(this);
202
  }
185
  }
203
186
204
  // Implementation of concrete methods in BaseHash --------------------------
205
206
  protected synchronized void transform(byte[] in, int i)
187
  protected synchronized void transform(byte[] in, int i)
207
  {
188
  {
208
    int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
189
    int X0 = (in[i++] & 0xFF)
209
             | (in[i++] & 0xFF) << 24;
190
           | (in[i++] & 0xFF) << 8
210
    int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
191
           | (in[i++] & 0xFF) << 16
211
             | (in[i++] & 0xFF) << 24;
192
           | (in[i++] & 0xFF) << 24;
212
    int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
193
    int X1 = (in[i++] & 0xFF)
213
             | (in[i++] & 0xFF) << 24;
194
           | (in[i++] & 0xFF) << 8
214
    int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
195
           | (in[i++] & 0xFF) << 16
215
             | (in[i++] & 0xFF) << 24;
196
           | (in[i++] & 0xFF) << 24;
216
    int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
197
    int X2 = (in[i++] & 0xFF)
217
             | (in[i++] & 0xFF) << 24;
198
           | (in[i++] & 0xFF) << 8
218
    int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
199
           | (in[i++] & 0xFF) << 16
219
             | (in[i++] & 0xFF) << 24;
200
           | (in[i++] & 0xFF) << 24;
220
    int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
201
    int X3 = (in[i++] & 0xFF)
221
             | (in[i++] & 0xFF) << 24;
202
           | (in[i++] & 0xFF) << 8
222
    int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
203
           | (in[i++] & 0xFF) << 16
223
             | (in[i++] & 0xFF) << 24;
204
           | (in[i++] & 0xFF) << 24;
224
    int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
205
    int X4 = (in[i++] & 0xFF)
225
             | (in[i++] & 0xFF) << 24;
206
           | (in[i++] & 0xFF) << 8
226
    int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
207
           | (in[i++] & 0xFF) << 16
227
             | (in[i++] & 0xFF) << 24;
208
           | (in[i++] & 0xFF) << 24;
228
    int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
209
    int X5 = (in[i++] & 0xFF)
229
              | (in[i++] & 0xFF) << 24;
210
           | (in[i++] & 0xFF) << 8
230
    int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
211
           | (in[i++] & 0xFF) << 16
231
              | (in[i++] & 0xFF) << 24;
212
           | (in[i++] & 0xFF) << 24;
232
    int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
213
    int X6 = (in[i++] & 0xFF)
233
              | (in[i++] & 0xFF) << 24;
214
           | (in[i++] & 0xFF) << 8
234
    int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
215
           | (in[i++] & 0xFF) << 16
235
              | (in[i++] & 0xFF) << 24;
216
           | (in[i++] & 0xFF) << 24;
236
    int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
217
    int X7 = (in[i++] & 0xFF)
237
              | (in[i++] & 0xFF) << 24;
218
           | (in[i++] & 0xFF) << 8
238
    int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
219
           | (in[i++] & 0xFF) << 16
239
              | (in[i++] & 0xFF) << 24;
220
           | (in[i++] & 0xFF) << 24;
240
    int X16 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
221
    int X8 = (in[i++] & 0xFF)
241
              | (in[i++] & 0xFF) << 24;
222
           | (in[i++] & 0xFF) << 8
242
    int X17 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
223
           | (in[i++] & 0xFF) << 16
243
              | (in[i++] & 0xFF) << 24;
224
           | (in[i++] & 0xFF) << 24;
244
    int X18 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
225
    int X9 = (in[i++] & 0xFF)
245
              | (in[i++] & 0xFF) << 24;
226
           | (in[i++] & 0xFF) << 8
246
    int X19 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
227
           | (in[i++] & 0xFF) << 16
247
              | (in[i++] & 0xFF) << 24;
228
           | (in[i++] & 0xFF) << 24;
248
    int X20 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
229
    int X10 = (in[i++] & 0xFF)
249
              | (in[i++] & 0xFF) << 24;
230
            | (in[i++] & 0xFF) << 8
250
    int X21 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
231
            | (in[i++] & 0xFF) << 16
251
              | (in[i++] & 0xFF) << 24;
232
            | (in[i++] & 0xFF) << 24;
252
    int X22 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
233
    int X11 = (in[i++] & 0xFF)
253
              | (in[i++] & 0xFF) << 24;
234
            | (in[i++] & 0xFF) << 8
254
    int X23 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
235
            | (in[i++] & 0xFF) << 16
255
              | (in[i++] & 0xFF) << 24;
236
            | (in[i++] & 0xFF) << 24;
256
    int X24 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
237
    int X12 = (in[i++] & 0xFF)
257
              | (in[i++] & 0xFF) << 24;
238
            | (in[i++] & 0xFF) << 8
258
    int X25 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
239
            | (in[i++] & 0xFF) << 16
259
              | (in[i++] & 0xFF) << 24;
240
            | (in[i++] & 0xFF) << 24;
260
    int X26 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
241
    int X13 = (in[i++] & 0xFF)
261
              | (in[i++] & 0xFF) << 24;
242
            | (in[i++] & 0xFF) << 8
262
    int X27 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
243
            | (in[i++] & 0xFF) << 16
263
              | (in[i++] & 0xFF) << 24;
244
            | (in[i++] & 0xFF) << 24;
264
    int X28 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
245
    int X14 = (in[i++] & 0xFF)
265
              | (in[i++] & 0xFF) << 24;
246
            | (in[i++] & 0xFF) << 8
266
    int X29 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
247
            | (in[i++] & 0xFF) << 16
267
              | (in[i++] & 0xFF) << 24;
248
            | (in[i++] & 0xFF) << 24;
268
    int X30 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
249
    int X15 = (in[i++] & 0xFF)
269
              | (in[i++] & 0xFF) << 24;
250
            | (in[i++] & 0xFF) << 8
270
    int X31 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
251
            | (in[i++] & 0xFF) << 16
271
              | (in[i++] & 0xFF) << 24;
252
            | (in[i++] & 0xFF) << 24;
272
253
    int X16 = (in[i++] & 0xFF)
254
            | (in[i++] & 0xFF) << 8
255
            | (in[i++] & 0xFF) << 16
256
            | (in[i++] & 0xFF) << 24;
257
    int X17 = (in[i++] & 0xFF)
258
            | (in[i++] & 0xFF) << 8
259
            | (in[i++] & 0xFF) << 16
260
            | (in[i++] & 0xFF) << 24;
261
    int X18 = (in[i++] & 0xFF)
262
            | (in[i++] & 0xFF) << 8
263
            | (in[i++] & 0xFF) << 16
264
            | (in[i++] & 0xFF) << 24;
265
    int X19 = (in[i++] & 0xFF)
266
            | (in[i++] & 0xFF) << 8
267
            | (in[i++] & 0xFF) << 16
268
            | (in[i++] & 0xFF) << 24;
269
    int X20 = (in[i++] & 0xFF)
270
            | (in[i++] & 0xFF) << 8
271
            | (in[i++] & 0xFF) << 16
272
            | (in[i++] & 0xFF) << 24;
273
    int X21 = (in[i++] & 0xFF)
274
            | (in[i++] & 0xFF) << 8
275
            | (in[i++] & 0xFF) << 16
276
            | (in[i++] & 0xFF) << 24;
277
    int X22 = (in[i++] & 0xFF)
278
            | (in[i++] & 0xFF) << 8
279
            | (in[i++] & 0xFF) << 16
280
            | (in[i++] & 0xFF) << 24;
281
    int X23 = (in[i++] & 0xFF)
282
            | (in[i++] & 0xFF) << 8
283
            | (in[i++] & 0xFF) << 16
284
            | (in[i++] & 0xFF) << 24;
285
    int X24 = (in[i++] & 0xFF)
286
            | (in[i++] & 0xFF) << 8
287
            | (in[i++] & 0xFF) << 16
288
            | (in[i++] & 0xFF) << 24;
289
    int X25 = (in[i++] & 0xFF)
290
            | (in[i++] & 0xFF) << 8
291
            | (in[i++] & 0xFF) << 16
292
            | (in[i++] & 0xFF) << 24;
293
    int X26 = (in[i++] & 0xFF)
294
            | (in[i++] & 0xFF) << 8
295
            | (in[i++] & 0xFF) << 16
296
            | (in[i++] & 0xFF) << 24;
297
    int X27 = (in[i++] & 0xFF)
298
            | (in[i++] & 0xFF) << 8
299
            | (in[i++] & 0xFF) << 16
300
            | (in[i++] & 0xFF) << 24;
301
    int X28 = (in[i++] & 0xFF)
302
            | (in[i++] & 0xFF) << 8
303
            | (in[i++] & 0xFF) << 16
304
            | (in[i++] & 0xFF) << 24;
305
    int X29 = (in[i++] & 0xFF)
306
            | (in[i++] & 0xFF) << 8
307
            | (in[i++] & 0xFF) << 16
308
            | (in[i++] & 0xFF) << 24;
309
    int X30 = (in[i++] & 0xFF)
310
            | (in[i++] & 0xFF) << 8
311
            | (in[i++] & 0xFF) << 16
312
            | (in[i++] & 0xFF) << 24;
313
    int X31 = (in[i++] & 0xFF)
314
            | (in[i++] & 0xFF) << 8
315
            | (in[i++] & 0xFF) << 16
316
            | (in[i++] & 0xFF) << 24;
273
    int t0 = h0, t1 = h1, t2 = h2, t3 = h3, t4 = h4, t5 = h5, t6 = h6, t7 = h7;
317
    int t0 = h0, t1 = h1, t2 = h2, t3 = h3, t4 = h4, t5 = h5, t6 = h6, t7 = h7;
274
275
    // Pass 1
318
    // Pass 1
276
    t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X0);
319
    t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X0);
277
    t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X1);
320
    t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X1);
Lines 458-464 Link Here
458
            t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X15, 0x409F60C4);
501
            t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X15, 0x409F60C4);
459
          }
502
          }
460
      }
503
      }
461
462
    h7 += t7;
504
    h7 += t7;
463
    h6 += t6;
505
    h6 += t6;
464
    h5 += t5;
506
    h5 += t5;
Lines 471-500 Link Here
471
513
472
  protected byte[] padBuffer()
514
  protected byte[] padBuffer()
473
  {
515
  {
474
    // pad out to 118 mod 128.  other 10 bytes have special use.
516
    // pad out to 118 mod 128. other 10 bytes have special use.
475
    int n = (int) (count % BLOCK_SIZE);
517
    int n = (int)(count % BLOCK_SIZE);
476
    int padding = (n < 118) ? (118 - n) : (246 - n);
518
    int padding = (n < 118) ? (118 - n) : (246 - n);
477
    byte[] result = new byte[padding + 10];
519
    byte[] result = new byte[padding + 10];
478
    result[0] = (byte) 0x01;
520
    result[0] = (byte) 0x01;
479
480
    // save the version number (LSB 3), the number of rounds (3 bits in the
521
    // save the version number (LSB 3), the number of rounds (3 bits in the
481
    // middle), the fingerprint length (MSB 2 bits and next byte) and the
522
    // middle), the fingerprint length (MSB 2 bits and next byte) and the
482
    // number of bits in the unpadded message.
523
    // number of bits in the unpadded message.
483
    int bl = hashSize * 8;
524
    int bl = hashSize * 8;
484
    result[padding++] = (byte) (((bl & 0x03) << 6) | ((rounds & 0x07) << 3) | (HAVAL_VERSION & 0x07));
525
    int sigByte = (bl & 0x03) << 6;
485
    result[padding++] = (byte) (bl >>> 2);
526
    sigByte |= (rounds & 0x07) << 3;
486
527
    sigByte |= HAVAL_VERSION & 0x07;
528
    result[padding++] = (byte) sigByte;
529
    result[padding++] = (byte)(bl >>> 2);
487
    // save number of bits, casting the long to an array of 8 bytes
530
    // save number of bits, casting the long to an array of 8 bytes
488
    long bits = count << 3;
531
    long bits = count << 3;
489
    result[padding++] = (byte) bits;
532
    result[padding++] = (byte) bits;
490
    result[padding++] = (byte) (bits >>> 8);
533
    result[padding++] = (byte)(bits >>> 8);
491
    result[padding++] = (byte) (bits >>> 16);
534
    result[padding++] = (byte)(bits >>> 16);
492
    result[padding++] = (byte) (bits >>> 24);
535
    result[padding++] = (byte)(bits >>> 24);
493
    result[padding++] = (byte) (bits >>> 32);
536
    result[padding++] = (byte)(bits >>> 32);
494
    result[padding++] = (byte) (bits >>> 40);
537
    result[padding++] = (byte)(bits >>> 40);
495
    result[padding++] = (byte) (bits >>> 48);
538
    result[padding++] = (byte)(bits >>> 48);
496
    result[padding] = (byte) (bits >>> 56);
539
    result[padding  ] = (byte)(bits >>> 56);
497
498
    return result;
540
    return result;
499
  }
541
  }
500
542
Lines 505-553 Link Here
505
    byte[] result = new byte[hashSize];
547
    byte[] result = new byte[hashSize];
506
    if (hashSize >= HAVAL_256_BIT)
548
    if (hashSize >= HAVAL_256_BIT)
507
      {
549
      {
508
        result[31] = (byte) (h7 >>> 24);
550
        result[31] = (byte)(h7 >>> 24);
509
        result[30] = (byte) (h7 >>> 16);
551
        result[30] = (byte)(h7 >>> 16);
510
        result[29] = (byte) (h7 >>> 8);
552
        result[29] = (byte)(h7 >>> 8);
511
        result[28] = (byte) h7;
553
        result[28] = (byte) h7;
512
      }
554
      }
513
    if (hashSize >= HAVAL_224_BIT)
555
    if (hashSize >= HAVAL_224_BIT)
514
      {
556
      {
515
        result[27] = (byte) (h6 >>> 24);
557
        result[27] = (byte)(h6 >>> 24);
516
        result[26] = (byte) (h6 >>> 16);
558
        result[26] = (byte)(h6 >>> 16);
517
        result[25] = (byte) (h6 >>> 8);
559
        result[25] = (byte)(h6 >>> 8);
518
        result[24] = (byte) h6;
560
        result[24] = (byte) h6;
519
      }
561
      }
520
    if (hashSize >= HAVAL_192_BIT)
562
    if (hashSize >= HAVAL_192_BIT)
521
      {
563
      {
522
        result[23] = (byte) (h5 >>> 24);
564
        result[23] = (byte)(h5 >>> 24);
523
        result[22] = (byte) (h5 >>> 16);
565
        result[22] = (byte)(h5 >>> 16);
524
        result[21] = (byte) (h5 >>> 8);
566
        result[21] = (byte)(h5 >>> 8);
525
        result[20] = (byte) h5;
567
        result[20] = (byte) h5;
526
      }
568
      }
527
    if (hashSize >= HAVAL_160_BIT)
569
    if (hashSize >= HAVAL_160_BIT)
528
      {
570
      {
529
        result[19] = (byte) (h4 >>> 24);
571
        result[19] = (byte)(h4 >>> 24);
530
        result[18] = (byte) (h4 >>> 16);
572
        result[18] = (byte)(h4 >>> 16);
531
        result[17] = (byte) (h4 >>> 8);
573
        result[17] = (byte)(h4 >>> 8);
532
        result[16] = (byte) h4;
574
        result[16] = (byte) h4;
533
      }
575
      }
534
    result[15] = (byte) (h3 >>> 24);
576
    result[15] = (byte)(h3 >>> 24);
535
    result[14] = (byte) (h3 >>> 16);
577
    result[14] = (byte)(h3 >>> 16);
536
    result[13] = (byte) (h3 >>> 8);
578
    result[13] = (byte)(h3 >>> 8);
537
    result[12] = (byte) h3;
579
    result[12] = (byte) h3;
538
    result[11] = (byte) (h2 >>> 24);
580
    result[11] = (byte)(h2 >>> 24);
539
    result[10] = (byte) (h2 >>> 16);
581
    result[10] = (byte)(h2 >>> 16);
540
    result[9] = (byte) (h2 >>> 8);
582
    result[ 9] = (byte)(h2 >>> 8);
541
    result[8] = (byte) h2;
583
    result[ 8] = (byte) h2;
542
    result[7] = (byte) (h1 >>> 24);
584
    result[ 7] = (byte)(h1 >>> 24);
543
    result[6] = (byte) (h1 >>> 16);
585
    result[ 6] = (byte)(h1 >>> 16);
544
    result[5] = (byte) (h1 >>> 8);
586
    result[ 5] = (byte)(h1 >>> 8);
545
    result[4] = (byte) h1;
587
    result[ 4] = (byte) h1;
546
    result[3] = (byte) (h0 >>> 24);
588
    result[ 3] = (byte)(h0 >>> 24);
547
    result[2] = (byte) (h0 >>> 16);
589
    result[ 2] = (byte)(h0 >>> 16);
548
    result[1] = (byte) (h0 >>> 8);
590
    result[ 1] = (byte)(h0 >>> 8);
549
    result[0] = (byte) h0;
591
    result[ 0] = (byte) h0;
550
551
    return result;
592
    return result;
552
  }
593
  }
553
594
Lines 567-579 Link Here
567
  {
608
  {
568
    if (valid == null)
609
    if (valid == null)
569
      {
610
      {
570
        valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new Haval().digest())));
611
        String d = Util.toString(new Haval().digest());
612
        valid = Boolean.valueOf(DIGEST0.equals(d));
571
      }
613
      }
572
    return valid.booleanValue();
614
    return valid.booleanValue();
573
  }
615
  }
574
616
575
  // helper methods ----------------------------------------------------------
576
577
  /** Tailors the last output. */
617
  /** Tailors the last output. */
578
  private void tailorDigestBits()
618
  private void tailorDigestBits()
579
  {
619
  {
Lines 581-597 Link Here
581
    switch (hashSize)
621
    switch (hashSize)
582
      {
622
      {
583
      case HAVAL_128_BIT:
623
      case HAVAL_128_BIT:
584
        t = (h7 & 0x000000FF) | (h6 & 0xFF000000) | (h5 & 0x00FF0000)
624
        t = (h7 & 0x000000FF)
585
            | (h4 & 0x0000FF00);
625
          | (h6 & 0xFF000000)
626
          | (h5 & 0x00FF0000)
627
          | (h4 & 0x0000FF00);
586
        h0 += t >>> 8 | t << 24;
628
        h0 += t >>> 8 | t << 24;
587
        t = (h7 & 0x0000FF00) | (h6 & 0x000000FF) | (h5 & 0xFF000000)
629
        t = (h7 & 0x0000FF00)
588
            | (h4 & 0x00FF0000);
630
          | (h6 & 0x000000FF)
631
          | (h5 & 0xFF000000)
632
          | (h4 & 0x00FF0000);
589
        h1 += t >>> 16 | t << 16;
633
        h1 += t >>> 16 | t << 16;
590
        t = (h7 & 0x00FF0000) | (h6 & 0x0000FF00) | (h5 & 0x000000FF)
634
        t = (h7 & 0x00FF0000)
591
            | (h4 & 0xFF000000);
635
          | (h6 & 0x0000FF00)
636
          | (h5 & 0x000000FF)
637
          | (h4 & 0xFF000000);
592
        h2 += t >>> 24 | t << 8;
638
        h2 += t >>> 24 | t << 8;
593
        t = (h7 & 0xFF000000) | (h6 & 0x00FF0000) | (h5 & 0x0000FF00)
639
        t = (h7 & 0xFF000000)
594
            | (h4 & 0x000000FF);
640
          | (h6 & 0x00FF0000)
641
          | (h5 & 0x0000FF00)
642
          | (h4 & 0x000000FF);
595
        h3 += t;
643
        h3 += t;
596
        break;
644
        break;
597
      case HAVAL_160_BIT:
645
      case HAVAL_160_BIT:
Lines 625-633 Link Here
625
        h1 += ((h7 >>> 22) & 0x1F);
673
        h1 += ((h7 >>> 22) & 0x1F);
626
        h2 += ((h7 >>> 18) & 0x0F);
674
        h2 += ((h7 >>> 18) & 0x0F);
627
        h3 += ((h7 >>> 13) & 0x1F);
675
        h3 += ((h7 >>> 13) & 0x1F);
628
        h4 += ((h7 >>> 9) & 0x0F);
676
        h4 += ((h7 >>>  9) & 0x0F);
629
        h5 += ((h7 >>> 4) & 0x1F);
677
        h5 += ((h7 >>>  4) & 0x1F);
630
        h6 += (h7 & 0x0F);
678
        h6 +=  (h7         & 0x0F);
631
      }
679
      }
632
  }
680
  }
633
681
Lines 748-755 Link Here
748
796
749
  private int f4(int x6, int x5, int x4, int x3, int x2, int x1, int x0)
797
  private int f4(int x6, int x5, int x4, int x3, int x2, int x1, int x0)
750
  {
798
  {
751
    return x4 & (x5 & ~x2 ^ x3 & ~x6 ^ x1 ^ x6 ^ x0) ^ x3 & (x1 & x2 ^ x5 ^ x6)
799
    return x4 & (x5 & ~x2 ^ x3 & ~x6 ^ x1 ^ x6 ^ x0) ^ x3
752
           ^ x2 & x6 ^ x0;
800
           & (x1 & x2 ^ x5 ^ x6) ^ x2 & x6 ^ x0;
753
  }
801
  }
754
802
755
  private int f5(int x6, int x5, int x4, int x3, int x2, int x1, int x0)
803
  private int f5(int x6, int x5, int x4, int x3, int x2, int x1, int x0)
(-)IMessageDigest.java (-40 / +32 lines)
Lines 39-102 Link Here
39
package gnu.java.security.hash;
39
package gnu.java.security.hash;
40
40
41
/**
41
/**
42
 * <p>The basic visible methods of any hash algorithm.</p>
42
 * The basic visible methods of any hash algorithm.
43
 *
43
 * <p>
44
 * <p>A hash (or message digest) algorithm produces its output by iterating a
44
 * A hash (or message digest) algorithm produces its output by iterating a basic
45
 * basic compression function on blocks of data.</p>
45
 * compression function on blocks of data.
46
 */
46
 */
47
public interface IMessageDigest extends Cloneable
47
public interface IMessageDigest
48
    extends Cloneable
48
{
49
{
49
50
  // Constants
51
  // -------------------------------------------------------------------------
52
53
  // Methods
54
  // -------------------------------------------------------------------------
55
56
  /**
50
  /**
57
   * <p>Returns the canonical name of this algorithm.</p>
51
   * Returns the canonical name of this algorithm.
58
   *
52
   * 
59
   * @return the canonical name of this instance.
53
   * @return the canonical name of this instance.
60
   */
54
   */
61
  String name();
55
  String name();
62
56
63
  /**
57
  /**
64
   * <p>Returns the output length in bytes of this message digest algorithm.</p>
58
   * Returns the output length in bytes of this message digest algorithm.
65
   *
59
   * 
66
   * @return the output length in bytes of this message digest algorithm.
60
   * @return the output length in bytes of this message digest algorithm.
67
   */
61
   */
68
  int hashSize();
62
  int hashSize();
69
63
70
  /**
64
  /**
71
   * <p>Returns the algorithm's (inner) block size in bytes.</p>
65
   * Returns the algorithm's (inner) block size in bytes.
72
   *
66
   * 
73
   * @return the algorithm's inner block size in bytes.
67
   * @return the algorithm's inner block size in bytes.
74
   */
68
   */
75
  int blockSize();
69
  int blockSize();
76
70
77
  /**
71
  /**
78
   * <p>Continues a message digest operation using the input byte.</p>
72
   * Continues a message digest operation using the input byte.
79
   *
73
   * 
80
   * @param b the input byte to digest.
74
   * @param b the input byte to digest.
81
   */
75
   */
82
  void update(byte b);
76
  void update(byte b);
83
77
84
  /**
78
  /**
85
   * <p>Continues a message digest operation, by filling the buffer, processing
79
   * Continues a message digest operation, by filling the buffer, processing
86
   * data in the algorithm's HASH_SIZE-bit block(s), updating the context and
80
   * data in the algorithm's HASH_SIZE-bit block(s), updating the context and
87
   * count, and buffering the remaining bytes in buffer for the next
81
   * count, and buffering the remaining bytes in buffer for the next operation.
88
   * operation.</p>
82
   * 
89
   *
90
   * @param in the input block.
83
   * @param in the input block.
91
   */
84
   */
92
  void update(byte[] in);
85
  void update(byte[] in);
93
86
94
  /**
87
  /**
95
   * <p>Continues a message digest operation, by filling the buffer, processing
88
   * Continues a message digest operation, by filling the buffer, processing
96
   * data in the algorithm's HASH_SIZE-bit block(s), updating the context and
89
   * data in the algorithm's HASH_SIZE-bit block(s), updating the context and
97
   * count, and buffering the remaining bytes in buffer for the next
90
   * count, and buffering the remaining bytes in buffer for the next operation.
98
   * operation.</p>
91
   * 
99
   *
100
   * @param in the input block.
92
   * @param in the input block.
101
   * @param offset start of meaningful bytes in input block.
93
   * @param offset start of meaningful bytes in input block.
102
   * @param length number of bytes, in input block, to consider.
94
   * @param length number of bytes, in input block, to consider.
Lines 104-134 Link Here
104
  void update(byte[] in, int offset, int length);
96
  void update(byte[] in, int offset, int length);
105
97
106
  /**
98
  /**
107
   * <p>Completes the message digest by performing final operations such as
99
   * Completes the message digest by performing final operations such as padding
108
   * padding and resetting the instance.</p>
100
   * and resetting the instance.
109
   *
101
   * 
110
   * @return the array of bytes representing the hash value.
102
   * @return the array of bytes representing the hash value.
111
   */
103
   */
112
  byte[] digest();
104
  byte[] digest();
113
105
114
  /**
106
  /**
115
   * <p>Resets the current context of this instance clearing any eventually cached
107
   * Resets the current context of this instance clearing any eventually cached
116
   * intermediary values.</p>
108
   * intermediary values.
117
   */
109
   */
118
  void reset();
110
  void reset();
119
111
120
  /**
112
  /**
121
   * <p>A basic test. Ensures that the digest of a pre-determined message is equal
113
   * A basic test. Ensures that the digest of a pre-determined message is equal
122
   * to a known pre-computed value.</p>
114
   * to a known pre-computed value.
123
   *
115
   * 
124
   * @return <tt>true</tt> if the implementation passes a basic self-test.
116
   * @return <code>true</code> if the implementation passes a basic self-test.
125
   * Returns <tt>false</tt> otherwise.
117
   *         Returns <code>false</code> otherwise.
126
   */
118
   */
127
  boolean selfTest();
119
  boolean selfTest();
128
120
129
  /**
121
  /**
130
   * <p>Returns a clone copy of this instance.</p>
122
   * Returns a clone copy of this instance.
131
   *
123
   * 
132
   * @return a clone copy of this instance.
124
   * @return a clone copy of this instance.
133
   */
125
   */
134
  Object clone();
126
  Object clone();
(-)MD2.java (-104 / +59 lines)
Lines 42-54 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>An implementation of the MD2 message digest algorithm.</p>
45
 * An implementation of the MD2 message digest algorithm.
46
 *
46
 * <p>
47
 * <p>MD2 is not widely used. Unless it is needed for compatibility with
47
 * MD2 is not widely used. Unless it is needed for compatibility with
48
 * existing systems, it is not recommended for use in new applications.</p>
48
 * existing systems, it is not recommended for use in new applications.
49
 *
49
 * <p>
50
 * <p>References:</p>
50
 * References:
51
 *
52
 * <ol>
51
 * <ol>
53
 *    <li>The <a href="http://www.ietf.org/rfc/rfc1319.txt">MD2</a>
52
 *    <li>The <a href="http://www.ietf.org/rfc/rfc1319.txt">MD2</a>
54
 *    Message-Digest Algorithm.<br>
53
 *    Message-Digest Algorithm.<br>
Lines 57-68 Link Here
57
 *    under section RFC 1319.</li>
56
 *    under section RFC 1319.</li>
58
 * </ol>
57
 * </ol>
59
 */
58
 */
60
public class MD2 extends BaseHash
59
public class MD2
60
    extends BaseHash
61
{
61
{
62
63
  // Constants and variables
64
  // -------------------------------------------------------------------------
65
66
  /** An MD2 message digest is always 128-bits long, or 16 bytes. */
62
  /** An MD2 message digest is always 128-bits long, or 16 bytes. */
67
  private static final int DIGEST_LENGTH = 16;
63
  private static final int DIGEST_LENGTH = 16;
68
64
Lines 70-103 Link Here
70
  private static final int BLOCK_LENGTH = 16;
66
  private static final int BLOCK_LENGTH = 16;
71
67
72
  /** 256 byte "random" permutation of the digits of pi. */
68
  /** 256 byte "random" permutation of the digits of pi. */
73
  private static final byte[] PI = { 41, 46, 67, -55, -94, -40, 124, 1, 61, 54,
69
  private static final byte[] PI = {
74
                                    84, -95, -20, -16, 6, 19, 98, -89, 5, -13,
70
        41,   46,   67,  -55,  -94,  -40,  124,   1,
75
                                    -64, -57, 115, -116, -104, -109, 43, -39,
71
        61,   54,   84,  -95,  -20,  -16,    6,  19,
76
                                    -68, 76, -126, -54, 30, -101, 87, 60, -3,
72
        98,  -89,    5,  -13,  -64,  -57,  115, -116,
77
                                    -44, -32, 22, 103, 66, 111, 24, -118, 23,
73
      -104, -109,   43,  -39,  -68,   76, -126,  -54,
78
                                    -27, 18, -66, 78, -60, -42, -38, -98, -34,
74
        30, -101,   87,   60,   -3,  -44,  -32,   22,
79
                                    73, -96, -5, -11, -114, -69, 47, -18, 122,
75
       103,   66,  111,   24, -118,   23,  -27,   18,
80
                                    -87, 104, 121, -111, 21, -78, 7, 63, -108,
76
       -66,   78,  -60,  -42,  -38,  -98,  -34,   73,
81
                                    -62, 16, -119, 11, 34, 95, 33, -128, 127,
77
       -96,   -5,  -11, -114,  -69,   47,  -18,  122,
82
                                    93, -102, 90, -112, 50, 39, 53, 62, -52,
78
       -87,  104,  121, -111,   21,  -78,    7,   63,
83
                                    -25, -65, -9, -105, 3, -1, 25, 48, -77, 72,
79
      -108,  -62,   16, -119,   11,   34,   95,   33,
84
                                    -91, -75, -47, -41, 94, -110, 42, -84, 86,
80
      -128,  127,   93, -102,   90, -112,   50,   39,
85
                                    -86, -58, 79, -72, 56, -46, -106, -92, 125,
81
        53,   62,  -52,  -25,  -65,   -9, -105,    3,
86
                                    -74, 118, -4, 107, -30, -100, 116, 4, -15,
82
        -1,   25,   48,  -77,   72,  -91,  -75,  -47,
87
                                    69, -99, 112, 89, 100, 113, -121, 32, -122,
83
       -41,   94, -110,   42,  -84,   86,  -86,  -58,
88
                                    91, -49, 101, -26, 45, -88, 2, 27, 96, 37,
84
        79,  -72,   56,  -46, -106,  -92,  125,  -74,
89
                                    -83, -82, -80, -71, -10, 28, 70, 97, 105,
85
       118,   -4,  107,  -30, -100,  116,    4,  -15,
90
                                    52, 64, 126, 15, 85, 71, -93, 35, -35, 81,
86
        69,  -99,  112,   89,  100,  113, -121,   32,
91
                                    -81, 58, -61, 92, -7, -50, -70, -59, -22,
87
      -122,   91,  -49,  101,  -26,   45,  -88,    2,
92
                                    38, 44, 83, 13, 110, -123, 40, -124, 9,
88
        27,   96,   37,  -83,  -82,  -80,  -71,  -10,
93
                                    -45, -33, -51, -12, 65, -127, 77, 82, 106,
89
        28,   70,   97,  105,   52,   64,  126,   15,
94
                                    -36, 55, -56, 108, -63, -85, -6, 36, -31,
90
        85,   71,  -93,   35,  -35,   81,  -81,   58,
95
                                    123, 8, 12, -67, -79, 74, 120, -120, -107,
91
       -61,   92,   -7,  -50,  -70,  -59,  -22,   38,
96
                                    -117, -29, 99, -24, 109, -23, -53, -43, -2,
92
        44,   83,   13,  110, -123,   40, -124,    9,
97
                                    59, 0, 29, 57, -14, -17, -73, 14, 102, 88,
93
       -45,  -33,  -51,  -12,   65, -127,   77,   82,
98
                                    -48, -28, -90, 119, 114, -8, -21, 117, 75,
94
       106,  -36,   55,  -56,  108,  -63,  -85,   -6,
99
                                    10, 49, 68, 80, -76, -113, -19, 31, 26,
95
        36,  -31,  123,    8,   12,  -67,  -79,   74,
100
                                    -37, -103, -115, 51, -97, 17, -125, 20 };
96
       120, -120, -107, -117,  -29,   99,  -24,  109,
97
       -23,  -53,  -43,   -2,   59,    0,   29,   57,
98
       -14,  -17,  -73,   14,  102,   88,  -48,  -28,
99
       -90,  119,  114,   -8,  -21,  117,   75,   10,
100
        49,   68,   80,  -76, -113,  -19,   31,   26,
101
       -37, -103, -115,   51, - 97,   17, -125,   20 };
101
102
102
  /** The output of this message digest when no data has been input. */
103
  /** The output of this message digest when no data has been input. */
103
  private static final String DIGEST0 = "8350E5A3E24C153DF2275C9F80692773";
104
  private static final String DIGEST0 = "8350E5A3E24C153DF2275C9F80692773";
Lines 114-122 Link Here
114
   */
115
   */
115
  private byte[] work;
116
  private byte[] work;
116
117
117
  // Constructor(s)
118
  // -------------------------------------------------------------------------
119
120
  /** Creates a new MD2 digest ready for use. */
118
  /** Creates a new MD2 digest ready for use. */
121
  public MD2()
119
  public MD2()
122
  {
120
  {
Lines 124-130 Link Here
124
  }
122
  }
125
123
126
  /**
124
  /**
127
   * <p>Private constructor used for cloning.</p>
125
   * Private constructor used for cloning.
128
   *
126
   *
129
   * @param md2 the instance to clone.
127
   * @param md2 the instance to clone.
130
   */
128
   */
Lines 135-172 Link Here
135
    // superclass field
133
    // superclass field
136
    this.count = md2.count;
134
    this.count = md2.count;
137
    this.buffer = (byte[]) md2.buffer.clone();
135
    this.buffer = (byte[]) md2.buffer.clone();
138
139
    // private field
136
    // private field
140
    this.checksum = (byte[]) md2.checksum.clone();
137
    this.checksum = (byte[]) md2.checksum.clone();
141
    this.work = (byte[]) md2.work.clone();
138
    this.work = (byte[]) md2.work.clone();
142
  }
139
  }
143
140
144
  // Class methods
145
  // -------------------------------------------------------------------------
146
147
  // Instance methods
148
  // -------------------------------------------------------------------------
149
150
  // java.lang.Cloneable interface implementation ----------------------------
151
152
  public Object clone()
141
  public Object clone()
153
  {
142
  {
154
    return new MD2(this);
143
    return new MD2(this);
155
  }
144
  }
156
145
157
  // Implementation of abstract methods in BaseHash --------------------------
158
159
  protected byte[] getResult()
146
  protected byte[] getResult()
160
  {
147
  {
161
    byte[] result = new byte[DIGEST_LENGTH];
148
    byte[] result = new byte[DIGEST_LENGTH];
162
163
    // Encrypt checksum as last block.
149
    // Encrypt checksum as last block.
164
    encryptBlock(checksum, 0);
150
    encryptBlock(checksum, 0);
165
166
    for (int i = 0; i < BLOCK_LENGTH; i++)
151
    for (int i = 0; i < BLOCK_LENGTH; i++)
167
      {
152
      result[i] = work[i];
168
        result[i] = work[i];
169
      }
170
153
171
    return result;
154
    return result;
172
  }
155
  }
Lines 181-197 Link Here
181
  {
164
  {
182
    if (valid == null)
165
    if (valid == null)
183
      {
166
      {
184
        valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD2().digest())));
167
        String d = Util.toString(new MD2().digest());
168
        valid = Boolean.valueOf(DIGEST0.equals(d));
185
      }
169
      }
186
    return valid.booleanValue();
170
    return valid.booleanValue();
187
  }
171
  }
188
172
189
  /**
173
  /**
190
   * <p>Generates an array of padding bytes. The padding is defined as
174
   * Generates an array of padding bytes. The padding is defined as
191
   * <code>i</code> bytes of value <code>i</code>, where <code>i</code> is the
175
   * <code>i</code> bytes of value <code>i</code>, where <code>i</code> is the
192
   * number of bytes to fill the last block of the message to
176
   * number of bytes to fill the last block of the message to
193
   * <code>BLOCK_LENGTH</code> bytes (or <code>BLOCK_LENGTH</code> bytes when
177
   * <code>BLOCK_LENGTH</code> bytes (or <code>BLOCK_LENGTH</code> bytes when
194
   * the last block was completely full).</p>
178
   * the last block was completely full).
195
   *
179
   *
196
   * @return the bytes to pad the remaining bytes in the buffer before
180
   * @return the bytes to pad the remaining bytes in the buffer before
197
   * completing a hash operation.
181
   * completing a hash operation.
Lines 200-246 Link Here
200
  {
184
  {
201
    int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH);
185
    int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH);
202
    if (length == 0)
186
    if (length == 0)
203
      {
187
      length = BLOCK_LENGTH;
204
        length = BLOCK_LENGTH;
188
205
      }
206
    byte[] pad = new byte[length];
189
    byte[] pad = new byte[length];
207
    for (int i = 0; i < length; i++)
190
    for (int i = 0; i < length; i++)
208
      {
191
      pad[i] = (byte) length;
209
        pad[i] = (byte) length;
192
210
      }
211
    return pad;
193
    return pad;
212
  }
194
  }
213
195
214
  /**
196
  /**
215
   * <p>Adds <code>BLOCK_LENGTH</code> bytes to the running digest.</p>
197
   * Adds <code>BLOCK_LENGTH</code> bytes to the running digest.
216
   *
198
   *
217
   * @param in the byte array to take the <code>BLOCK_LENGTH</code> bytes from.
199
   * @param in the byte array to take the <code>BLOCK_LENGTH</code> bytes from.
218
   * @param off the offset to start from in the given byte array.
200
   * @param off the offset to start from in the given byte array.
219
   */
201
   */
220
  protected void transform(byte[] in, int off)
202
  protected void transform(byte[] in, int off)
221
  {
203
  {
222
    // encryptBlock(in, off);
223
    // updateCheckSum(in, off);
224
    updateCheckSumAndEncryptBlock(in, off);
204
    updateCheckSumAndEncryptBlock(in, off);
225
  }
205
  }
226
206
227
  // Private instance methods ------------------------------------------------
228
229
  /**
230
   * Updates the checksum with the <code>BLOCK_LENGTH</code> bytes from the
231
   * given array starting at <code>off</code>.
232
   */
233
  /*
234
   private void updateCheckSum(byte[] in, int off) {
235
   byte l = checksum[BLOCK_LENGTH-1];
236
   for (int i = 0; i < BLOCK_LENGTH; i++) {
237
   byte b = in[off+i];
238
   //         l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF));
239
   l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]);
240
   checksum[i] = l;
241
   }
242
   }
243
   */
244
  /**
207
  /**
245
   * Adds a new block (<code>BLOCK_LENGTH</code> bytes) to the running digest
208
   * Adds a new block (<code>BLOCK_LENGTH</code> bytes) to the running digest
246
   * from the given byte array starting from the given offset.
209
   * from the given byte array starting from the given offset.
Lines 251-270 Link Here
251
      {
214
      {
252
        byte b = in[off + i];
215
        byte b = in[off + i];
253
        work[BLOCK_LENGTH + i] = b;
216
        work[BLOCK_LENGTH + i] = b;
254
        work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b);
217
        work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b);
255
      }
218
      }
256
257
    byte t = 0;
219
    byte t = 0;
258
    for (int i = 0; i < 18; i++)
220
    for (int i = 0; i < 18; i++)
259
      {
221
      {
260
        for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
222
        for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
261
          {
223
          {
262
            //            t = (byte)((work[j] & 0xFF) ^ (PI[t & 0xFF] & 0xFF));
224
            t = (byte)(work[j] ^ PI[t & 0xFF]);
263
            t = (byte) (work[j] ^ PI[t & 0xFF]);
264
            work[j] = t;
225
            work[j] = t;
265
          }
226
          }
266
        //         t = (byte)((t + i) & 0xFF);
227
        t = (byte)(t + i);
267
        t = (byte) (t + i);
268
      }
228
      }
269
  }
229
  }
270
230
Lines 278-301 Link Here
278
      {
238
      {
279
        byte b = in[off + i];
239
        byte b = in[off + i];
280
        work[BLOCK_LENGTH + i] = b;
240
        work[BLOCK_LENGTH + i] = b;
281
        //         work[BLOCK_LENGTH*2+i] = (byte)((work[i] & 0xFF) ^ (b & 0xFF));
241
        work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b);
282
        work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b);
242
        l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]);
283
        //         l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF));
284
        l = (byte) (checksum[i] ^ PI[(b ^ l) & 0xFF]);
285
        checksum[i] = l;
243
        checksum[i] = l;
286
      }
244
      }
287
288
    byte t = 0;
245
    byte t = 0;
289
    for (int i = 0; i < 18; i++)
246
    for (int i = 0; i < 18; i++)
290
      {
247
      {
291
        for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
248
        for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
292
          {
249
          {
293
            //            t = (byte)((work[j] & 0xFF) ^ (PI[t & 0xFF] & 0xFF));
250
            t = (byte)(work[j] ^ PI[t & 0xFF]);
294
            t = (byte) (work[j] ^ PI[t & 0xFF]);
295
            work[j] = t;
251
            work[j] = t;
296
          }
252
          }
297
        //         t = (byte)((t + i) & 0xFF);
253
        t = (byte)(t + i);
298
        t = (byte) (t + i);
299
      }
254
      }
300
  }
255
  }
301
}
256
}
(-)MD4.java (-80 / +89 lines)
Lines 42-55 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>An implementation of Ron Rivest's MD4 message digest algorithm.</p>
45
 * An implementation of Ron Rivest's MD4 message digest algorithm.
46
 *
46
 * <p>
47
 * <p>MD4 was the precursor to the stronger {@link gnu.java.security.hash.MD5}
47
 * MD4 was the precursor to the stronger {@link gnu.java.security.hash.MD5}
48
 * algorithm, and while not considered cryptograpically secure itself, MD4 is
48
 * algorithm, and while not considered cryptograpically secure itself, MD4 is
49
 * in use in various applications. It is slightly faster than MD5.</p>
49
 * in use in various applications. It is slightly faster than MD5.
50
 *
50
 * <p>
51
 * <p>References:</p>
51
 * References:
52
 *
53
 * <ol>
52
 * <ol>
54
 *    <li>The <a href="http://www.ietf.org/rfc/rfc1320.txt">MD4</a>
53
 *    <li>The <a href="http://www.ietf.org/rfc/rfc1320.txt">MD4</a>
55
 *    Message-Digest Algorithm.<br>
54
 *    Message-Digest Algorithm.<br>
Lines 58-69 Link Here
58
 *
57
 *
59
 * @author Casey Marshall (rsdio@metastatic.org)
58
 * @author Casey Marshall (rsdio@metastatic.org)
60
 */
59
 */
61
public class MD4 extends BaseHash
60
public class MD4
61
    extends BaseHash
62
{
62
{
63
64
  // Constants and variables
65
  // -------------------------------------------------------------------------
66
67
  /** An MD4 message digest is always 128-bits long, or 16 bytes. */
63
  /** An MD4 message digest is always 128-bits long, or 16 bytes. */
68
  private static final int DIGEST_LENGTH = 16;
64
  private static final int DIGEST_LENGTH = 16;
69
65
Lines 86-98 Link Here
86
82
87
  private int a, b, c, d;
83
  private int a, b, c, d;
88
84
89
  // Constructor(s)
90
  // -------------------------------------------------------------------------
91
92
  /**
85
  /**
93
   * <p>Public constructor. Initializes the chaining variables, sets the byte
86
   * Public constructor. Initializes the chaining variables, sets the byte
94
   * count to <code>0</code>, and creates a new block of <code>512</code> bits.
87
   * count to <code>0</code>, and creates a new block of <code>512</code> bits.
95
   * </p>
96
   */
88
   */
97
  public MD4()
89
  public MD4()
98
  {
90
  {
Lines 100-106 Link Here
100
  }
92
  }
101
93
102
  /**
94
  /**
103
   * <p>Trivial private constructor for cloning purposes.</p>
95
   * Trivial private constructor for cloning purposes.
104
   *
96
   *
105
   * @param that the instance to clone.
97
   * @param that the instance to clone.
106
   */
98
   */
Lines 116-145 Link Here
116
    this.buffer = (byte[]) that.buffer.clone();
108
    this.buffer = (byte[]) that.buffer.clone();
117
  }
109
  }
118
110
119
  // Class methods
120
  // -------------------------------------------------------------------------
121
122
  // Instance methods
123
  // -------------------------------------------------------------------------
124
125
  // java.lang.Cloneable interface implementation ----------------------------
126
127
  public Object clone()
111
  public Object clone()
128
  {
112
  {
129
    return new MD4(this);
113
    return new MD4(this);
130
  }
114
  }
131
115
132
  // Implementation of abstract methods in BashHash --------------------------
133
134
  protected byte[] getResult()
116
  protected byte[] getResult()
135
  {
117
  {
136
    byte[] digest = { (byte) a, (byte) (a >>> 8), (byte) (a >>> 16),
118
    return new byte[] {
137
                     (byte) (a >>> 24), (byte) b, (byte) (b >>> 8),
119
        (byte) a, (byte)(a >>> 8), (byte)(a >>> 16), (byte)(a >>> 24),
138
                     (byte) (b >>> 16), (byte) (b >>> 24), (byte) c,
120
        (byte) b, (byte)(b >>> 8), (byte)(b >>> 16), (byte)(b >>> 24),
139
                     (byte) (c >>> 8), (byte) (c >>> 16), (byte) (c >>> 24),
121
        (byte) c, (byte)(c >>> 8), (byte)(c >>> 16), (byte)(c >>> 24),
140
                     (byte) d, (byte) (d >>> 8), (byte) (d >>> 16),
122
        (byte) d, (byte)(d >>> 8), (byte)(d >>> 16), (byte)(d >>> 24) };
141
                     (byte) (d >>> 24) };
142
    return digest;
143
  }
123
  }
144
124
145
  protected void resetContext()
125
  protected void resetContext()
Lines 154-221 Link Here
154
  {
134
  {
155
    if (valid == null)
135
    if (valid == null)
156
      {
136
      {
157
        valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD4().digest())));
137
        String d = Util.toString(new MD4().digest());
138
        valid = Boolean.valueOf(DIGEST0.equals(d));
158
      }
139
      }
159
    return valid.booleanValue();
140
    return valid.booleanValue();
160
  }
141
  }
161
142
162
  protected byte[] padBuffer()
143
  protected byte[] padBuffer()
163
  {
144
  {
164
    int n = (int) (count % BLOCK_LENGTH);
145
    int n = (int)(count % BLOCK_LENGTH);
165
    int padding = (n < 56) ? (56 - n) : (120 - n);
146
    int padding = (n < 56) ? (56 - n) : (120 - n);
166
    byte[] pad = new byte[padding + 8];
147
    byte[] pad = new byte[padding + 8];
167
168
    pad[0] = (byte) 0x80;
148
    pad[0] = (byte) 0x80;
169
    long bits = count << 3;
149
    long bits = count << 3;
170
    pad[padding++] = (byte) bits;
150
    pad[padding++] = (byte) bits;
171
    pad[padding++] = (byte) (bits >>> 8);
151
    pad[padding++] = (byte)(bits >>> 8);
172
    pad[padding++] = (byte) (bits >>> 16);
152
    pad[padding++] = (byte)(bits >>> 16);
173
    pad[padding++] = (byte) (bits >>> 24);
153
    pad[padding++] = (byte)(bits >>> 24);
174
    pad[padding++] = (byte) (bits >>> 32);
154
    pad[padding++] = (byte)(bits >>> 32);
175
    pad[padding++] = (byte) (bits >>> 40);
155
    pad[padding++] = (byte)(bits >>> 40);
176
    pad[padding++] = (byte) (bits >>> 48);
156
    pad[padding++] = (byte)(bits >>> 48);
177
    pad[padding] = (byte) (bits >>> 56);
157
    pad[padding  ] = (byte)(bits >>> 56);
178
179
    return pad;
158
    return pad;
180
  }
159
  }
181
160
182
  protected void transform(byte[] in, int i)
161
  protected void transform(byte[] in, int i)
183
  {
162
  {
184
    int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
163
    int X0 = (in[i++] & 0xFF)
185
             | in[i++] << 24;
164
           | (in[i++] & 0xFF) << 8
186
    int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
165
           | (in[i++] & 0xFF) << 16
187
             | in[i++] << 24;
166
           | in[i++] << 24;
188
    int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
167
    int X1 = (in[i++] & 0xFF)
189
             | in[i++] << 24;
168
           | (in[i++] & 0xFF) << 8
190
    int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
169
           | (in[i++] & 0xFF) << 16
191
             | in[i++] << 24;
170
           | in[i++] << 24;
192
    int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
171
    int X2 = (in[i++] & 0xFF)
193
             | in[i++] << 24;
172
           | (in[i++] & 0xFF) << 8
194
    int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
173
           | (in[i++] & 0xFF) << 16
195
             | in[i++] << 24;
174
           | in[i++] << 24;
196
    int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
175
    int X3 = (in[i++] & 0xFF)
197
             | in[i++] << 24;
176
           | (in[i++] & 0xFF) << 8
198
    int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
177
           | (in[i++] & 0xFF) << 16
199
             | in[i++] << 24;
178
           | in[i++] << 24;
200
    int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
179
    int X4 = (in[i++] & 0xFF)
201
             | in[i++] << 24;
180
           | (in[i++] & 0xFF) << 8
202
    int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
181
           | (in[i++] & 0xFF) << 16
203
             | in[i++] << 24;
182
           | in[i++] << 24;
204
    int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
183
    int X5 = (in[i++] & 0xFF)
205
              | in[i++] << 24;
184
           | (in[i++] & 0xFF) << 8
206
    int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
185
           | (in[i++] & 0xFF) << 16
207
              | in[i++] << 24;
186
           | in[i++] << 24;
208
    int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
187
    int X6 = (in[i++] & 0xFF)
209
              | in[i++] << 24;
188
           | (in[i++] & 0xFF) << 8
210
    int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
189
           | (in[i++] & 0xFF) << 16
211
              | in[i++] << 24;
190
           | in[i++] << 24;
212
    int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
191
    int X7 = (in[i++] & 0xFF)
213
              | in[i++] << 24;
192
           | (in[i++] & 0xFF) << 8
214
    int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
193
           | (in[i++] & 0xFF) << 16
215
              | in[i] << 24;
194
           | in[i++] << 24;
216
195
    int X8 = (in[i++] & 0xFF)
196
           | (in[i++] & 0xFF) << 8
197
           | (in[i++] & 0xFF) << 16
198
           | in[i++] << 24;
199
    int X9 = (in[i++] & 0xFF)
200
           | (in[i++] & 0xFF) << 8
201
           | (in[i++] & 0xFF) << 16
202
           | in[i++] << 24;
203
    int X10 = (in[i++] & 0xFF)
204
            | (in[i++] & 0xFF) << 8
205
            | (in[i++] & 0xFF) << 16
206
            | in[i++] << 24;
207
    int X11 = (in[i++] & 0xFF)
208
            | (in[i++] & 0xFF) << 8
209
            | (in[i++] & 0xFF) << 16
210
            | in[i++] << 24;
211
    int X12 = (in[i++] & 0xFF)
212
            | (in[i++] & 0xFF) << 8
213
            | (in[i++] & 0xFF) << 16
214
            | in[i++] << 24;
215
    int X13 = (in[i++] & 0xFF)
216
            | (in[i++] & 0xFF) << 8
217
            | (in[i++] & 0xFF) << 16
218
            | in[i++] << 24;
219
    int X14 = (in[i++] & 0xFF)
220
            | (in[i++] & 0xFF) << 8
221
            | (in[i++] & 0xFF) << 16
222
            | in[i++] << 24;
223
    int X15 = (in[i++] & 0xFF)
224
            | (in[i++] & 0xFF) << 8
225
            | (in[i++] & 0xFF) << 16
226
            | in[i] << 24;
217
    int aa, bb, cc, dd;
227
    int aa, bb, cc, dd;
218
219
    aa = a;
228
    aa = a;
220
    bb = b;
229
    bb = b;
221
    cc = c;
230
    cc = c;
(-)MD5.java (-80 / +86 lines)
Lines 42-67 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>The MD5 message-digest algorithm takes as input a message of arbitrary
45
 * The MD5 message-digest algorithm takes as input a message of arbitrary
46
 * length and produces as output a 128-bit "fingerprint" or "message digest" of
46
 * length and produces as output a 128-bit "fingerprint" or "message digest" of
47
 * the input. It is conjectured that it is computationally infeasible to
47
 * the input. It is conjectured that it is computationally infeasible to
48
 * produce two messages having the same message digest, or to produce any
48
 * produce two messages having the same message digest, or to produce any
49
 * message having a given prespecified target message digest.</p>
49
 * message having a given prespecified target message digest.
50
 *
50
 * <p>
51
 * <p>References:</p>
51
 * References:
52
 *
53
 * <ol>
52
 * <ol>
54
 *    <li>The <a href="http://www.ietf.org/rfc/rfc1321.txt">MD5</a> Message-
53
 *    <li>The <a href="http://www.ietf.org/rfc/rfc1321.txt">MD5</a> Message-
55
 *    Digest Algorithm.<br>
54
 *    Digest Algorithm.<br>
56
 *    R. Rivest.</li>
55
 *    R. Rivest.</li>
57
 * </ol>
56
 * </ol>
58
 */
57
 */
59
public class MD5 extends BaseHash
58
public class MD5
59
    extends BaseHash
60
{
60
{
61
62
  // Constants and variables
63
  // -------------------------------------------------------------------------
64
65
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
61
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
66
62
67
  private static final String DIGEST0 = "D41D8CD98F00B204E9800998ECF8427E";
63
  private static final String DIGEST0 = "D41D8CD98F00B204E9800998ECF8427E";
Lines 72-80 Link Here
72
  /** 128-bit interim result. */
68
  /** 128-bit interim result. */
73
  private int h0, h1, h2, h3;
69
  private int h0, h1, h2, h3;
74
70
75
  // Constructor(s)
76
  // -------------------------------------------------------------------------
77
78
  /** Trivial 0-arguments constructor. */
71
  /** Trivial 0-arguments constructor. */
79
  public MD5()
72
  public MD5()
80
  {
73
  {
Lines 82-88 Link Here
82
  }
75
  }
83
76
84
  /**
77
  /**
85
   * <p>Private constructor for cloning purposes.</p>
78
   * Private constructor for cloning purposes.
86
   *
79
   *
87
   * @param md the instance to clone.
80
   * @param md the instance to clone.
88
   */
81
   */
Lines 98-158 Link Here
98
    this.buffer = (byte[]) md.buffer.clone();
91
    this.buffer = (byte[]) md.buffer.clone();
99
  }
92
  }
100
93
101
  // Class methods
102
  // -------------------------------------------------------------------------
103
104
  // Instance methods
105
  // -------------------------------------------------------------------------
106
107
  // java.lang.Cloneable interface implementation ----------------------------
108
109
  public Object clone()
94
  public Object clone()
110
  {
95
  {
111
    return new MD5(this);
96
    return new MD5(this);
112
  }
97
  }
113
98
114
  // Implementation of concrete methods in BaseHash --------------------------
115
116
  protected synchronized void transform(byte[] in, int i)
99
  protected synchronized void transform(byte[] in, int i)
117
  {
100
  {
118
    int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
101
    int X0 = (in[i++] & 0xFF)
119
             | in[i++] << 24;
102
           | (in[i++] & 0xFF) << 8
120
    int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
103
           | (in[i++] & 0xFF) << 16
121
             | in[i++] << 24;
104
           |  in[i++]         << 24;
122
    int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
105
    int X1 = (in[i++] & 0xFF)
123
             | in[i++] << 24;
106
           | (in[i++] & 0xFF) << 8
124
    int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
107
           | (in[i++] & 0xFF) << 16
125
             | in[i++] << 24;
108
           |  in[i++]         << 24;
126
    int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
109
    int X2 = (in[i++] & 0xFF)
127
             | in[i++] << 24;
110
           | (in[i++] & 0xFF) << 8
128
    int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
111
           | (in[i++] & 0xFF) << 16
129
             | in[i++] << 24;
112
           |  in[i++]         << 24;
130
    int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
113
    int X3 = (in[i++] & 0xFF)
131
             | in[i++] << 24;
114
           | (in[i++] & 0xFF) << 8
132
    int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
115
           | (in[i++] & 0xFF) << 16
133
             | in[i++] << 24;
116
           |  in[i++]         << 24;
134
    int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
117
    int X4 = (in[i++] & 0xFF)
135
             | in[i++] << 24;
118
           | (in[i++] & 0xFF) << 8
136
    int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
119
           | (in[i++] & 0xFF) << 16
137
             | in[i++] << 24;
120
           |  in[i++]         << 24;
138
    int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
121
    int X5 = (in[i++] & 0xFF)
139
              | in[i++] << 24;
122
           | (in[i++] & 0xFF) << 8
140
    int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
123
           | (in[i++] & 0xFF) << 16
141
              | in[i++] << 24;
124
           |  in[i++]         << 24;
142
    int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
125
    int X6 = (in[i++] & 0xFF)
143
              | in[i++] << 24;
126
           | (in[i++] & 0xFF) << 8
144
    int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
127
           | (in[i++] & 0xFF) << 16
145
              | in[i++] << 24;
128
           |  in[i++]         << 24;
146
    int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
129
    int X7 = (in[i++] & 0xFF)
147
              | in[i++] << 24;
130
           | (in[i++] & 0xFF) << 8
148
    int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
131
           | (in[i++] & 0xFF) << 16
149
              | in[i] << 24;
132
           |  in[i++]         << 24;
150
133
    int X8 = (in[i++] & 0xFF)
134
           | (in[i++] & 0xFF) << 8
135
           | (in[i++] & 0xFF) << 16
136
           |  in[i++]         << 24;
137
    int X9 = (in[i++] & 0xFF)
138
           | (in[i++] & 0xFF) << 8
139
           | (in[i++] & 0xFF) << 16
140
           |  in[i++]         << 24;
141
    int X10 = (in[i++] & 0xFF)
142
            | (in[i++] & 0xFF) << 8
143
            | (in[i++] & 0xFF) << 16
144
            |  in[i++]         << 24;
145
    int X11 = (in[i++] & 0xFF)
146
            | (in[i++] & 0xFF) << 8
147
            | (in[i++] & 0xFF) << 16
148
            |  in[i++]         << 24;
149
    int X12 = (in[i++] & 0xFF)
150
            | (in[i++] & 0xFF) << 8
151
            | (in[i++] & 0xFF) << 16
152
            |  in[i++]         << 24;
153
    int X13 = (in[i++] & 0xFF)
154
            | (in[i++] & 0xFF) << 8
155
            | (in[i++] & 0xFF) << 16
156
            |  in[i++]         << 24;
157
    int X14 = (in[i++] & 0xFF)
158
            | (in[i++] & 0xFF) << 8
159
            | (in[i++] & 0xFF) << 16
160
            |  in[i++]         << 24;
161
    int X15 = (in[i++] & 0xFF)
162
            | (in[i++] & 0xFF) << 8
163
            | (in[i++] & 0xFF) << 16
164
            |  in[i]           << 24;
151
    int A = h0;
165
    int A = h0;
152
    int B = h1;
166
    int B = h1;
153
    int C = h2;
167
    int C = h2;
154
    int D = h3;
168
    int D = h3;
155
156
    // hex constants are from md5.c in FSF Gnu Privacy Guard 0.9.2
169
    // hex constants are from md5.c in FSF Gnu Privacy Guard 0.9.2
157
    // round 1
170
    // round 1
158
    A += ((B & C) | (~B & D)) + X0 + 0xD76AA478;
171
    A += ((B & C) | (~B & D)) + X0 + 0xD76AA478;
Lines 310-348 Link Here
310
323
311
  protected byte[] padBuffer()
324
  protected byte[] padBuffer()
312
  {
325
  {
313
    int n = (int) (count % BLOCK_SIZE);
326
    int n = (int)(count % BLOCK_SIZE);
314
    int padding = (n < 56) ? (56 - n) : (120 - n);
327
    int padding = (n < 56) ? (56 - n) : (120 - n);
315
    byte[] result = new byte[padding + 8];
328
    byte[] result = new byte[padding + 8];
316
317
    // padding is always binary 1 followed by binary 0s
329
    // padding is always binary 1 followed by binary 0s
318
    result[0] = (byte) 0x80;
330
    result[0] = (byte) 0x80;
319
320
    // save number of bits, casting the long to an array of 8 bytes
331
    // save number of bits, casting the long to an array of 8 bytes
321
    long bits = count << 3;
332
    long bits = count << 3;
322
    result[padding++] = (byte) bits;
333
    result[padding++] = (byte) bits;
323
    result[padding++] = (byte) (bits >>> 8);
334
    result[padding++] = (byte)(bits >>> 8);
324
    result[padding++] = (byte) (bits >>> 16);
335
    result[padding++] = (byte)(bits >>> 16);
325
    result[padding++] = (byte) (bits >>> 24);
336
    result[padding++] = (byte)(bits >>> 24);
326
    result[padding++] = (byte) (bits >>> 32);
337
    result[padding++] = (byte)(bits >>> 32);
327
    result[padding++] = (byte) (bits >>> 40);
338
    result[padding++] = (byte)(bits >>> 40);
328
    result[padding++] = (byte) (bits >>> 48);
339
    result[padding++] = (byte)(bits >>> 48);
329
    result[padding] = (byte) (bits >>> 56);
340
    result[padding  ] = (byte)(bits >>> 56);
330
331
    return result;
341
    return result;
332
  }
342
  }
333
343
334
  protected byte[] getResult()
344
  protected byte[] getResult()
335
  {
345
  {
336
    byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8),
346
    return new byte[] {
337
                                (byte) (h0 >>> 16), (byte) (h0 >>> 24),
347
        (byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24),
338
                                (byte) h1, (byte) (h1 >>> 8),
348
        (byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24),
339
                                (byte) (h1 >>> 16), (byte) (h1 >>> 24),
349
        (byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24),
340
                                (byte) h2, (byte) (h2 >>> 8),
350
        (byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24) };
341
                                (byte) (h2 >>> 16), (byte) (h2 >>> 24),
342
                                (byte) h3, (byte) (h3 >>> 8),
343
                                (byte) (h3 >>> 16), (byte) (h3 >>> 24) };
344
345
    return result;
346
  }
351
  }
347
352
348
  protected void resetContext()
353
  protected void resetContext()
Lines 358-364 Link Here
358
  {
363
  {
359
    if (valid == null)
364
    if (valid == null)
360
      {
365
      {
361
        valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD5().digest())));
366
        String d = Util.toString(new MD5().digest());
367
        valid = Boolean.valueOf(DIGEST0.equals(d));
362
      }
368
      }
363
    return valid.booleanValue();
369
    return valid.booleanValue();
364
  }
370
  }
(-)RipeMD128.java (-91 / +57 lines)
Lines 42-95 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>RIPEMD-128 is a 128-bit message digest.</p>
45
 * RIPEMD-128 is a 128-bit message digest.
46
 *
46
 * <p>
47
 * <p>References:</p>
47
 * References:
48
 *
49
 * <ol>
48
 * <ol>
50
 *    <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html">
49
 *    <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html">
51
 *    RIPEMD160</a>: A Strengthened Version of RIPEMD.<br>
50
 *    RIPEMD160</a>: A Strengthened Version of RIPEMD.<br>
52
 *    Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li>
51
 *    Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li>
53
 * </ol>
52
 * </ol>
54
 */
53
 */
55
public class RipeMD128 extends BaseHash
54
public class RipeMD128
55
    extends BaseHash
56
{
56
{
57
58
  // Constants and variables
59
  // -------------------------------------------------------------------------
60
61
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
57
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
62
58
63
  private static final String DIGEST0 = "CDF26213A150DC3ECB610F18F6B38B46";
59
  private static final String DIGEST0 = "CDF26213A150DC3ECB610F18F6B38B46";
64
60
65
  /** Constants for the transform method. */
61
  /** Constants for the transform method. */
66
  // selection of message word
62
  // selection of message word
67
  private static final int[] R = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
63
  private static final int[] R = {
68
                                  13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0,
64
       0,  1,  2,  3,  4,  5,  6, 7,  8, 9, 10, 11, 12, 13, 14, 15,
69
                                  9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8,
65
       7,  4, 13,  1, 10,  6, 15, 3, 12, 0,  9,  5,  2, 14, 11,  8,
70
                                  1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10,
66
       3, 10, 14,  4,  9, 15,  8, 1,  2, 7,  0,  6, 13, 11,  5, 12,
71
                                  0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 };
67
       1,  9, 11, 10,  0,  8, 12, 4, 13, 3,  7, 15, 14,  5,  6,  2 };
72
68
73
  private static final int[] Rp = { 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1,
69
  private static final int[] Rp = {
74
                                   10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14,
70
       5, 14, 7, 0, 9,  2, 11,  4, 13,  6, 15,  8, 1, 10,  3, 12,
75
                                   15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14,
71
       6, 11, 3, 7, 0, 13,  5, 10, 14, 15,  8, 12, 4,  9,  1,  2,
76
                                   6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4,
72
      15,  5, 1, 3, 7, 14,  6,  9, 11,  8, 12,  2, 10, 0,  4, 13,
77
                                   1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 };
73
       8,  6, 4, 1, 3, 11, 15,  0,  5, 12,  2, 13,  9, 7, 10, 14 };
78
74
79
  // amount for rotate left (rol)
75
  // amount for rotate left (rol)
80
  private static final int[] S = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15,
76
  private static final int[] S = {
81
                                  6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12,
77
      11, 14, 15, 12,  5,  8,  7,  9, 11, 13, 14, 15,  6,  7,  9,  8,
82
                                  15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9,
78
       7,  6,  8, 13, 11,  9,  7, 15,  7, 12, 15,  9, 11,  7, 13, 12,
83
                                  13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12,
79
      11, 13,  6,  7, 14,  9, 13, 15, 14,  8, 13,  6,  5, 12,  7,  5,
84
                                  14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5,
80
      11, 12, 14, 15, 14, 15,  9,  8,  9, 14,  5,  6,  8,  6,  5, 12 };
85
                                  12 };
81
86
82
  private static final int[] Sp = {
87
  private static final int[] Sp = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11,
83
       8,  9,  9, 11, 13, 15, 15,  5,  7,  7,  8, 11, 14, 14, 12,  6,
88
                                   14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11,
84
       9, 13, 15,  7, 12,  8,  9, 11,  7,  7, 12,  7,  6, 15, 13, 11,
89
                                   7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8,
85
       9,  7, 15, 11,  8,  6,  6, 14, 12, 13,  5, 14, 13, 13,  7,  5,
90
                                   6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15,
86
      15,  5,  8, 11, 14, 14,  6, 14,  6,  9, 12,  9, 12,  5, 15,  8 };
91
                                   5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5,
92
                                   15, 8 };
93
87
94
  /** caches the result of the correctness test, once executed. */
88
  /** caches the result of the correctness test, once executed. */
95
  private static Boolean valid;
89
  private static Boolean valid;
Lines 100-108 Link Here
100
  /** 512 bits work buffer = 16 x 32-bit words */
94
  /** 512 bits work buffer = 16 x 32-bit words */
101
  private int[] X = new int[16];
95
  private int[] X = new int[16];
102
96
103
  // Constructor(s)
104
  // -------------------------------------------------------------------------
105
106
  /** Trivial 0-arguments constructor. */
97
  /** Trivial 0-arguments constructor. */
107
  public RipeMD128()
98
  public RipeMD128()
108
  {
99
  {
Lines 110-116 Link Here
110
  }
101
  }
111
102
112
  /**
103
  /**
113
   * <p>Private constructor for cloning purposes.</p>
104
   * Private constructor for cloning purposes.
114
   *
105
   *
115
   * @param md the instance to clone.
106
   * @param md the instance to clone.
116
   */
107
   */
Lines 126-165 Link Here
126
    this.buffer = (byte[]) md.buffer.clone();
117
    this.buffer = (byte[]) md.buffer.clone();
127
  }
118
  }
128
119
129
  // Class methods
130
  // -------------------------------------------------------------------------
131
132
  // Instance methods
133
  // -------------------------------------------------------------------------
134
135
  // java.lang.Cloneable interface implementation ----------------------------
136
137
  public Object clone()
120
  public Object clone()
138
  {
121
  {
139
    return new RipeMD128(this);
122
    return new RipeMD128(this);
140
  }
123
  }
141
124
142
  // Implementation of concrete methods in BaseHash --------------------------
143
144
  protected void transform(byte[] in, int offset)
125
  protected void transform(byte[] in, int offset)
145
  {
126
  {
146
    int A, B, C, D, Ap, Bp, Cp, Dp, T, s, i;
127
    int A, B, C, D, Ap, Bp, Cp, Dp, T, s, i;
147
128
    // encode 64 bytes from input block into an array of 16 unsigned integers.
148
    // encode 64 bytes from input block into an array of 16 unsigned
149
    // integers.
150
    for (i = 0; i < 16; i++)
129
    for (i = 0; i < 16; i++)
151
      {
130
      X[i] = (in[offset++] & 0xFF)
152
        X[i] = (in[offset++] & 0xFF) | (in[offset++] & 0xFF) << 8
131
           | (in[offset++] & 0xFF) << 8
153
               | (in[offset++] & 0xFF) << 16 | in[offset++] << 24;
132
           | (in[offset++] & 0xFF) << 16
154
      }
133
           |  in[offset++]         << 24;
155
156
    A = Ap = h0;
134
    A = Ap = h0;
157
    B = Bp = h1;
135
    B = Bp = h1;
158
    C = Cp = h2;
136
    C = Cp = h2;
159
    D = Dp = h3;
137
    D = Dp = h3;
160
138
    for (i = 0; i < 16; i++) // rounds 0...15
161
    for (i = 0; i < 16; i++)
139
      {
162
      { // rounds 0...15
163
        s = S[i];
140
        s = S[i];
164
        T = A + (B ^ C ^ D) + X[i];
141
        T = A + (B ^ C ^ D) + X[i];
165
        A = D;
142
        A = D;
Lines 174-182 Link Here
174
        Cp = Bp;
151
        Cp = Bp;
175
        Bp = T << s | T >>> (32 - s);
152
        Bp = T << s | T >>> (32 - s);
176
      }
153
      }
177
154
    for (; i < 32; i++) // rounds 16...31
178
    for (; i < 32; i++)
155
      {
179
      { // rounds 16...31
180
        s = S[i];
156
        s = S[i];
181
        T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999;
157
        T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999;
182
        A = D;
158
        A = D;
Lines 191-199 Link Here
191
        Cp = Bp;
167
        Cp = Bp;
192
        Bp = T << s | T >>> (32 - s);
168
        Bp = T << s | T >>> (32 - s);
193
      }
169
      }
194
170
    for (; i < 48; i++) // rounds 32...47
195
    for (; i < 48; i++)
171
      {
196
      { // rounds 32...47
197
        s = S[i];
172
        s = S[i];
198
        T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1;
173
        T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1;
199
        A = D;
174
        A = D;
Lines 208-216 Link Here
208
        Cp = Bp;
183
        Cp = Bp;
209
        Bp = T << s | T >>> (32 - s);
184
        Bp = T << s | T >>> (32 - s);
210
      }
185
      }
211
186
    for (; i < 64; i++) // rounds 48...63
212
    for (; i < 64; i++)
187
      {
213
      { // rounds 48...63
214
        s = S[i];
188
        s = S[i];
215
        T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC;
189
        T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC;
216
        A = D;
190
        A = D;
Lines 225-231 Link Here
225
        Cp = Bp;
199
        Cp = Bp;
226
        Bp = T << s | T >>> (32 - s);
200
        Bp = T << s | T >>> (32 - s);
227
      }
201
      }
228
229
    T = h1 + C + Dp;
202
    T = h1 + C + Dp;
230
    h1 = h2 + D + Ap;
203
    h1 = h2 + D + Ap;
231
    h2 = h3 + A + Bp;
204
    h2 = h3 + A + Bp;
Lines 235-273 Link Here
235
208
236
  protected byte[] padBuffer()
209
  protected byte[] padBuffer()
237
  {
210
  {
238
    int n = (int) (count % BLOCK_SIZE);
211
    int n = (int)(count % BLOCK_SIZE);
239
    int padding = (n < 56) ? (56 - n) : (120 - n);
212
    int padding = (n < 56) ? (56 - n) : (120 - n);
240
    byte[] result = new byte[padding + 8];
213
    byte[] result = new byte[padding + 8];
241
242
    // padding is always binary 1 followed by binary 0s
214
    // padding is always binary 1 followed by binary 0s
243
    result[0] = (byte) 0x80;
215
    result[0] = (byte) 0x80;
244
245
    // save number of bits, casting the long to an array of 8 bytes
216
    // save number of bits, casting the long to an array of 8 bytes
246
    long bits = count << 3;
217
    long bits = count << 3;
247
    result[padding++] = (byte) bits;
218
    result[padding++] = (byte) bits;
248
    result[padding++] = (byte) (bits >>> 8);
219
    result[padding++] = (byte)(bits >>> 8);
249
    result[padding++] = (byte) (bits >>> 16);
220
    result[padding++] = (byte)(bits >>> 16);
250
    result[padding++] = (byte) (bits >>> 24);
221
    result[padding++] = (byte)(bits >>> 24);
251
    result[padding++] = (byte) (bits >>> 32);
222
    result[padding++] = (byte)(bits >>> 32);
252
    result[padding++] = (byte) (bits >>> 40);
223
    result[padding++] = (byte)(bits >>> 40);
253
    result[padding++] = (byte) (bits >>> 48);
224
    result[padding++] = (byte)(bits >>> 48);
254
    result[padding] = (byte) (bits >>> 56);
225
    result[padding  ] = (byte)(bits >>> 56);
255
256
    return result;
226
    return result;
257
  }
227
  }
258
228
259
  protected byte[] getResult()
229
  protected byte[] getResult()
260
  {
230
  {
261
    byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8),
231
    return new byte[] {
262
                                (byte) (h0 >>> 16), (byte) (h0 >>> 24),
232
        (byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24),
263
                                (byte) h1, (byte) (h1 >>> 8),
233
        (byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24),
264
                                (byte) (h1 >>> 16), (byte) (h1 >>> 24),
234
        (byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24),
265
                                (byte) h2, (byte) (h2 >>> 8),
235
        (byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24)
266
                                (byte) (h2 >>> 16), (byte) (h2 >>> 24),
236
    };
267
                                (byte) h3, (byte) (h3 >>> 8),
268
                                (byte) (h3 >>> 16), (byte) (h3 >>> 24) };
269
270
    return result;
271
  }
237
  }
272
238
273
  protected void resetContext()
239
  protected void resetContext()
Lines 283-290 Link Here
283
  {
249
  {
284
    if (valid == null)
250
    if (valid == null)
285
      {
251
      {
286
        valid = Boolean.valueOf
252
        String d = Util.toString(new RipeMD128().digest());
287
          (DIGEST0.equals(Util.toString(new RipeMD128().digest())));
253
        valid = Boolean.valueOf(DIGEST0.equals(d));
288
      }
254
      }
289
    return valid.booleanValue();
255
    return valid.booleanValue();
290
  }
256
  }
(-)RipeMD160.java (-100 / +63 lines)
Lines 42-100 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>RIPEMD-160 is a 160-bit message digest.</p>
45
 * RIPEMD-160 is a 160-bit message digest.
46
 *
46
 * <p>
47
 * <p>References:</p>
47
 * References:
48
 *
49
 * <ol>
48
 * <ol>
50
 *    <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html">
49
 *    <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html">
51
 *    RIPEMD160</a>: A Strengthened Version of RIPEMD.<br>
50
 *    RIPEMD160</a>: A Strengthened Version of RIPEMD.<br>
52
 *    Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li>
51
 *    Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li>
53
 * </ol>
52
 * </ol>
54
 */
53
 */
55
public class RipeMD160 extends BaseHash
54
public class RipeMD160
55
    extends BaseHash
56
{
56
{
57
58
  // Constants and variables
59
  // -------------------------------------------------------------------------
60
61
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
57
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
62
58
63
  private static final String DIGEST0 = "9C1185A5C5E9FC54612808977EE8F548B2258D31";
59
  private static final String DIGEST0 = "9C1185A5C5E9FC54612808977EE8F548B2258D31";
64
60
65
  // selection of message word
61
  // selection of message word
66
  private static final int[] R = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
62
  private static final int[] R = {
67
                                  13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0,
63
      0,  1,  2,  3,  4,  5,  6,  7,  8, 9, 10, 11, 12, 13, 14, 15,
68
                                  9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8,
64
      7,  4, 13,  1, 10,  6, 15,  3, 12, 0,  9,  5,  2, 14, 11,  8,
69
                                  1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10,
65
      3, 10, 14,  4,  9, 15,  8,  1,  2, 7,  0,  6, 13, 11,  5, 12,
70
                                  0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 4, 0,
66
      1,  9, 11, 10,  0,  8, 12,  4, 13, 3,  7, 15, 14,  5,  6,  2,
71
                                  5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15,
67
      4,  0,  5,  9,  7, 12,  2, 10, 14, 1,  3,  8, 11,  6, 15, 13 };
72
                                  13 };
68
73
69
  private static final int[] Rp = {
74
  private static final int[] Rp = { 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1,
70
       5, 14,  7, 0, 9,  2, 11,  4, 13,  6, 15,  8,  1, 10,  3, 12,
75
                                   10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14,
71
       6, 11,  3, 7, 0, 13,  5, 10, 14, 15,  8, 12,  4,  9,  1,  2,
76
                                   15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14,
72
      15,  5,  1, 3, 7, 14,  6,  9, 11,  8, 12,  2, 10,  0,  4, 13,
77
                                   6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4,
73
       8,  6,  4, 1, 3, 11, 15,  0,  5, 12,  2, 13,  9,  7, 10, 14,
78
                                   1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
74
      12, 15, 10, 4, 1,  5,  8,  7,  6,  2, 13, 14,  0,  3,  9, 11 };
79
                                   12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0,
80
                                   3, 9, 11 };
81
75
82
  // amount for rotate left (rol)
76
  // amount for rotate left (rol)
83
  private static final int[] S = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15,
77
  private static final int[] S = {
84
                                  6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12,
78
      11, 14, 15, 12,  5,  8,  7,  9, 11, 13, 14, 15,  6,  7,  9,  8,
85
                                  15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9,
79
       7,  6,  8, 13, 11,  9,  7, 15,  7, 12, 15,  9, 11,  7, 13, 12,
86
                                  13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12,
80
      11, 13,  6,  7, 14,  9, 13, 15, 14,  8, 13,  6,  5, 12,  7,  5,
87
                                  14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5,
81
      11, 12, 14, 15, 14, 15,  9,  8,  9, 14,  5,  6,  8,  6,  5, 12,
88
                                  12, 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13,
82
       9, 15,  5, 11,  6,  8, 13, 12,  5, 12, 13, 14, 11,  8,  5,  6 };
89
                                  14, 11, 8, 5, 6 };
83
90
84
  private static final int[] Sp = {
91
  private static final int[] Sp = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11,
85
       8,  9,  9, 11, 13, 15, 15,  5,  7,  7,  8, 11, 14, 14, 12,  6,
92
                                   14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11,
86
       9, 13, 15,  7, 12,  8,  9, 11,  7,  7, 12,  7,  6, 15, 13, 11,
93
                                   7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8,
87
       9,  7, 15, 11,  8,  6,  6, 14, 12, 13,  5, 14, 13, 13,  7,  5,
94
                                   6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15,
88
      15,  5,  8, 11, 14, 14,  6, 14,  6,  9, 12,  9, 12,  5, 15,  8,
95
                                   5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5,
89
       8,  5, 12,  9, 12,  5, 14,  6,  8, 13,  6,  5, 15, 13, 11, 11 };
96
                                   15, 8, 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6,
97
                                   5, 15, 13, 11, 11 };
98
90
99
  /** caches the result of the correctness test, once executed. */
91
  /** caches the result of the correctness test, once executed. */
100
  private static Boolean valid;
92
  private static Boolean valid;
Lines 105-113 Link Here
105
  /** 512 bits work buffer = 16 x 32-bit words */
97
  /** 512 bits work buffer = 16 x 32-bit words */
106
  private int[] X = new int[16];
98
  private int[] X = new int[16];
107
99
108
  // Constructor(s)
109
  // -------------------------------------------------------------------------
110
111
  /** Trivial 0-arguments constructor. */
100
  /** Trivial 0-arguments constructor. */
112
  public RipeMD160()
101
  public RipeMD160()
113
  {
102
  {
Lines 115-121 Link Here
115
  }
104
  }
116
105
117
  /**
106
  /**
118
   * <p>Private constructor for cloning purposes.</p>
107
   * Private constructor for cloning purposes.
119
   *
108
   *
120
   * @param md the instance to clone.
109
   * @param md the instance to clone.
121
   */
110
   */
Lines 132-171 Link Here
132
    this.buffer = (byte[]) md.buffer.clone();
121
    this.buffer = (byte[]) md.buffer.clone();
133
  }
122
  }
134
123
135
  // Class methods
136
  // -------------------------------------------------------------------------
137
138
  // Instance methods
139
  // -------------------------------------------------------------------------
140
141
  // java.lang.Cloneable interface implementation ----------------------------
142
143
  public Object clone()
124
  public Object clone()
144
  {
125
  {
145
    return (new RipeMD160(this));
126
    return (new RipeMD160(this));
146
  }
127
  }
147
128
148
  // Implementation of concrete methods in BaseHash --------------------------
149
150
  protected void transform(byte[] in, int offset)
129
  protected void transform(byte[] in, int offset)
151
  {
130
  {
152
    int A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, T, s, i;
131
    int A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, T, s, i;
153
154
    // encode 64 bytes from input block into an array of 16 unsigned integers
132
    // encode 64 bytes from input block into an array of 16 unsigned integers
155
    for (i = 0; i < 16; i++)
133
    for (i = 0; i < 16; i++)
156
      {
134
      X[i] = (in[offset++] & 0xFF)
157
        X[i] = (in[offset++] & 0xFF) | (in[offset++] & 0xFF) << 8
135
           | (in[offset++] & 0xFF) << 8
158
               | (in[offset++] & 0xFF) << 16 | in[offset++] << 24;
136
           | (in[offset++] & 0xFF) << 16
159
      }
137
           |  in[offset++]         << 24;
160
161
    A = Ap = h0;
138
    A = Ap = h0;
162
    B = Bp = h1;
139
    B = Bp = h1;
163
    C = Cp = h2;
140
    C = Cp = h2;
164
    D = Dp = h3;
141
    D = Dp = h3;
165
    E = Ep = h4;
142
    E = Ep = h4;
166
143
    for (i = 0; i < 16; i++) // rounds 0...15
167
    for (i = 0; i < 16; i++)
144
      {
168
      { // rounds 0...15
169
        s = S[i];
145
        s = S[i];
170
        T = A + (B ^ C ^ D) + X[i];
146
        T = A + (B ^ C ^ D) + X[i];
171
        A = E;
147
        A = E;
Lines 182-190 Link Here
182
        Cp = Bp;
158
        Cp = Bp;
183
        Bp = (T << s | T >>> (32 - s)) + Ap;
159
        Bp = (T << s | T >>> (32 - s)) + Ap;
184
      }
160
      }
185
161
    for (; i < 32; i++) // rounds 16...31
186
    for (; i < 32; i++)
162
      {
187
      { // rounds 16...31
188
        s = S[i];
163
        s = S[i];
189
        T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999;
164
        T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999;
190
        A = E;
165
        A = E;
Lines 201-209 Link Here
201
        Cp = Bp;
176
        Cp = Bp;
202
        Bp = (T << s | T >>> (32 - s)) + Ap;
177
        Bp = (T << s | T >>> (32 - s)) + Ap;
203
      }
178
      }
204
179
    for (; i < 48; i++) // rounds 32...47
205
    for (; i < 48; i++)
180
      {
206
      { // rounds 32...47
207
        s = S[i];
181
        s = S[i];
208
        T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1;
182
        T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1;
209
        A = E;
183
        A = E;
Lines 220-228 Link Here
220
        Cp = Bp;
194
        Cp = Bp;
221
        Bp = (T << s | T >>> (32 - s)) + Ap;
195
        Bp = (T << s | T >>> (32 - s)) + Ap;
222
      }
196
      }
223
197
    for (; i < 64; i++) // rounds 48...63
224
    for (; i < 64; i++)
198
      {
225
      { // rounds 48...63
226
        s = S[i];
199
        s = S[i];
227
        T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC;
200
        T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC;
228
        A = E;
201
        A = E;
Lines 239-247 Link Here
239
        Cp = Bp;
212
        Cp = Bp;
240
        Bp = (T << s | T >>> (32 - s)) + Ap;
213
        Bp = (T << s | T >>> (32 - s)) + Ap;
241
      }
214
      }
242
215
    for (; i < 80; i++) // rounds 64...79
243
    for (; i < 80; i++)
216
      {
244
      { // rounds 64...79
245
        s = S[i];
217
        s = S[i];
246
        T = A + (B ^ (C | ~D)) + X[R[i]] + 0xA953FD4E;
218
        T = A + (B ^ (C | ~D)) + X[R[i]] + 0xA953FD4E;
247
        A = E;
219
        A = E;
Lines 258-264 Link Here
258
        Cp = Bp;
230
        Cp = Bp;
259
        Bp = (T << s | T >>> (32 - s)) + Ap;
231
        Bp = (T << s | T >>> (32 - s)) + Ap;
260
      }
232
      }
261
262
    T = h1 + C + Dp;
233
    T = h1 + C + Dp;
263
    h1 = h2 + D + Ep;
234
    h1 = h2 + D + Ep;
264
    h2 = h3 + E + Ap;
235
    h2 = h3 + E + Ap;
Lines 269-309 Link Here
269
240
270
  protected byte[] padBuffer()
241
  protected byte[] padBuffer()
271
  {
242
  {
272
    int n = (int) (count % BLOCK_SIZE);
243
    int n = (int)(count % BLOCK_SIZE);
273
    int padding = (n < 56) ? (56 - n) : (120 - n);
244
    int padding = (n < 56) ? (56 - n) : (120 - n);
274
    byte[] result = new byte[padding + 8];
245
    byte[] result = new byte[padding + 8];
275
276
    // padding is always binary 1 followed by binary 0s
246
    // padding is always binary 1 followed by binary 0s
277
    result[0] = (byte) 0x80;
247
    result[0] = (byte) 0x80;
278
279
    // save number of bits, casting the long to an array of 8 bytes
248
    // save number of bits, casting the long to an array of 8 bytes
280
    long bits = count << 3;
249
    long bits = count << 3;
281
    result[padding++] = (byte) bits;
250
    result[padding++] = (byte) bits;
282
    result[padding++] = (byte) (bits >>> 8);
251
    result[padding++] = (byte)(bits >>> 8);
283
    result[padding++] = (byte) (bits >>> 16);
252
    result[padding++] = (byte)(bits >>> 16);
284
    result[padding++] = (byte) (bits >>> 24);
253
    result[padding++] = (byte)(bits >>> 24);
285
    result[padding++] = (byte) (bits >>> 32);
254
    result[padding++] = (byte)(bits >>> 32);
286
    result[padding++] = (byte) (bits >>> 40);
255
    result[padding++] = (byte)(bits >>> 40);
287
    result[padding++] = (byte) (bits >>> 48);
256
    result[padding++] = (byte)(bits >>> 48);
288
    result[padding] = (byte) (bits >>> 56);
257
    result[padding  ] = (byte)(bits >>> 56);
289
290
    return result;
258
    return result;
291
  }
259
  }
292
260
293
  protected byte[] getResult()
261
  protected byte[] getResult()
294
  {
262
  {
295
    byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8),
263
    return new byte[] {
296
                                (byte) (h0 >>> 16), (byte) (h0 >>> 24),
264
        (byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24),
297
                                (byte) h1, (byte) (h1 >>> 8),
265
        (byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24),
298
                                (byte) (h1 >>> 16), (byte) (h1 >>> 24),
266
        (byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24),
299
                                (byte) h2, (byte) (h2 >>> 8),
267
        (byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24),
300
                                (byte) (h2 >>> 16), (byte) (h2 >>> 24),
268
        (byte) h4, (byte)(h4 >>> 8), (byte)(h4 >>> 16), (byte)(h4 >>> 24)
301
                                (byte) h3, (byte) (h3 >>> 8),
269
    };
302
                                (byte) (h3 >>> 16), (byte) (h3 >>> 24),
303
                                (byte) h4, (byte) (h4 >>> 8),
304
                                (byte) (h4 >>> 16), (byte) (h4 >>> 24) };
305
306
    return result;
307
  }
270
  }
308
271
309
  protected void resetContext()
272
  protected void resetContext()
Lines 320-327 Link Here
320
  {
283
  {
321
    if (valid == null)
284
    if (valid == null)
322
      {
285
      {
323
        valid = Boolean.valueOf
286
        String d = Util.toString(new RipeMD160().digest());
324
          (DIGEST0.equals(Util.toString(new RipeMD160().digest())));
287
        valid = Boolean.valueOf(DIGEST0.equals(d));
325
      }
288
      }
326
    return valid.booleanValue();
289
    return valid.booleanValue();
327
  }
290
  }
(-)Sha160.java (-104 / +37 lines)
Lines 42-48 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>The Secure Hash Algorithm (SHA-1) is required for use with the Digital
45
 * The Secure Hash Algorithm (SHA-1) is required for use with the Digital
46
 * Signature Algorithm (DSA) as specified in the Digital Signature Standard
46
 * Signature Algorithm (DSA) as specified in the Digital Signature Standard
47
 * (DSS) and whenever a secure hash algorithm is required for federal
47
 * (DSS) and whenever a secure hash algorithm is required for federal
48
 * applications. For a message of length less than 2^64 bits, the SHA-1
48
 * applications. For a message of length less than 2^64 bits, the SHA-1
Lines 51-65 Link Here
51
 * message. The SHA-1 is also used to compute a message digest for the received
51
 * message. The SHA-1 is also used to compute a message digest for the received
52
 * version of the message during the process of verifying the signature. Any
52
 * version of the message during the process of verifying the signature. Any
53
 * change to the message in transit will, with very high probability, result in
53
 * change to the message in transit will, with very high probability, result in
54
 * a different message digest, and the signature will fail to verify.</p>
54
 * a different message digest, and the signature will fail to verify.
55
 *
55
 * <p>
56
 * <p>The SHA-1 is designed to have the following properties: it is
56
 * The SHA-1 is designed to have the following properties: it is
57
 * computationally infeasible to find a message which corresponds to a given
57
 * computationally infeasible to find a message which corresponds to a given
58
 * message digest, or to find two different messages which produce the same
58
 * message digest, or to find two different messages which produce the same
59
 * message digest.</p>
59
 * message digest.
60
 *
60
 * <p>
61
 * <p>References:</p>
61
 * References:
62
 *
63
 * <ol>
62
 * <ol>
64
 *    <li><a href="http://www.itl.nist.gov/fipspubs/fip180-1.htm">SECURE HASH
63
 *    <li><a href="http://www.itl.nist.gov/fipspubs/fip180-1.htm">SECURE HASH
65
 *    STANDARD</a><br>
64
 *    STANDARD</a><br>
Lines 67-78 Link Here
67
 *    </li>
66
 *    </li>
68
 * </ol>
67
 * </ol>
69
 */
68
 */
70
public class Sha160 extends BaseHash
69
public class Sha160
70
    extends BaseHash
71
{
71
{
72
73
  // Constants and variables
74
  // -------------------------------------------------------------------------
75
76
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
72
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
77
73
78
  private static final String DIGEST0 = "A9993E364706816ABA3E25717850C26C9CD0D89D";
74
  private static final String DIGEST0 = "A9993E364706816ABA3E25717850C26C9CD0D89D";
Lines 85-93 Link Here
85
  /** 160-bit interim result. */
81
  /** 160-bit interim result. */
86
  private int h0, h1, h2, h3, h4;
82
  private int h0, h1, h2, h3, h4;
87
83
88
  // Constructor(s)
89
  // -------------------------------------------------------------------------
90
91
  /** Trivial 0-arguments constructor. */
84
  /** Trivial 0-arguments constructor. */
92
  public Sha160()
85
  public Sha160()
93
  {
86
  {
Lines 95-101 Link Here
95
  }
88
  }
96
89
97
  /**
90
  /**
98
   * <p>Private constructor for cloning purposes.</p>
91
   * Private constructor for cloning purposes.
99
   *
92
   *
100
   * @param md the instance to clone.
93
   * @param md the instance to clone.
101
   */
94
   */
Lines 112-169 Link Here
112
    this.buffer = (byte[]) md.buffer.clone();
105
    this.buffer = (byte[]) md.buffer.clone();
113
  }
106
  }
114
107
115
  // Class methods
116
  // -------------------------------------------------------------------------
117
118
  public static final int[] G(int hh0, int hh1, int hh2, int hh3, int hh4,
108
  public static final int[] G(int hh0, int hh1, int hh2, int hh3, int hh4,
119
                              byte[] in, int offset)
109
                              byte[] in, int offset)
120
  {
110
  {
121
    //      int[] w = new int[80];
122
    //      int i, T;
123
    //      for (i = 0; i < 16; i++) {
124
    //         w[i] = in[offset++]         << 24 |
125
    //               (in[offset++] & 0xFF) << 16 |
126
    //               (in[offset++] & 0xFF) <<  8 |
127
    //               (in[offset++] & 0xFF);
128
    //      }
129
    //      for (i = 16; i < 80; i++) {
130
    //         T = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
131
    //         w[i] = T << 1 | T >>> 31;
132
    //      }
133
134
    //      return sha(hh0, hh1, hh2, hh3, hh4, in, offset, w);
135
    return sha(hh0, hh1, hh2, hh3, hh4, in, offset);
111
    return sha(hh0, hh1, hh2, hh3, hh4, in, offset);
136
  }
112
  }
137
113
138
  // Instance methods
139
  // -------------------------------------------------------------------------
140
141
  // java.lang.Cloneable interface implementation ----------------------------
142
143
  public Object clone()
114
  public Object clone()
144
  {
115
  {
145
    return new Sha160(this);
116
    return new Sha160(this);
146
  }
117
  }
147
118
148
  // Implementation of concrete methods in BaseHash --------------------------
149
150
  protected void transform(byte[] in, int offset)
119
  protected void transform(byte[] in, int offset)
151
  {
120
  {
152
    //      int i, T;
153
    //      for (i = 0; i < 16; i++) {
154
    //         W[i] = in[offset++]         << 24 |
155
    //               (in[offset++] & 0xFF) << 16 |
156
    //               (in[offset++] & 0xFF) <<  8 |
157
    //               (in[offset++] & 0xFF);
158
    //      }
159
    //      for (i = 16; i < 80; i++) {
160
    //         T = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
161
    //         W[i] = T << 1 | T >>> 31;
162
    //      }
163
164
    //      int[] result = sha(h0, h1, h2, h3, h4, in, offset, W);
165
    int[] result = sha(h0, h1, h2, h3, h4, in, offset);
121
    int[] result = sha(h0, h1, h2, h3, h4, in, offset);
166
167
    h0 = result[0];
122
    h0 = result[0];
168
    h1 = result[1];
123
    h1 = result[1];
169
    h2 = result[2];
124
    h2 = result[2];
Lines 173-213 Link Here
173
128
174
  protected byte[] padBuffer()
129
  protected byte[] padBuffer()
175
  {
130
  {
176
    int n = (int) (count % BLOCK_SIZE);
131
    int n = (int)(count % BLOCK_SIZE);
177
    int padding = (n < 56) ? (56 - n) : (120 - n);
132
    int padding = (n < 56) ? (56 - n) : (120 - n);
178
    byte[] result = new byte[padding + 8];
133
    byte[] result = new byte[padding + 8];
179
180
    // padding is always binary 1 followed by binary 0s
134
    // padding is always binary 1 followed by binary 0s
181
    result[0] = (byte) 0x80;
135
    result[0] = (byte) 0x80;
182
183
    // save number of bits, casting the long to an array of 8 bytes
136
    // save number of bits, casting the long to an array of 8 bytes
184
    long bits = count << 3;
137
    long bits = count << 3;
185
    result[padding++] = (byte) (bits >>> 56);
138
    result[padding++] = (byte)(bits >>> 56);
186
    result[padding++] = (byte) (bits >>> 48);
139
    result[padding++] = (byte)(bits >>> 48);
187
    result[padding++] = (byte) (bits >>> 40);
140
    result[padding++] = (byte)(bits >>> 40);
188
    result[padding++] = (byte) (bits >>> 32);
141
    result[padding++] = (byte)(bits >>> 32);
189
    result[padding++] = (byte) (bits >>> 24);
142
    result[padding++] = (byte)(bits >>> 24);
190
    result[padding++] = (byte) (bits >>> 16);
143
    result[padding++] = (byte)(bits >>> 16);
191
    result[padding++] = (byte) (bits >>> 8);
144
    result[padding++] = (byte)(bits >>> 8);
192
    result[padding] = (byte) bits;
145
    result[padding  ] = (byte) bits;
193
194
    return result;
146
    return result;
195
  }
147
  }
196
148
197
  protected byte[] getResult()
149
  protected byte[] getResult()
198
  {
150
  {
199
    byte[] result = new byte[] { (byte) (h0 >>> 24), (byte) (h0 >>> 16),
151
    return new byte[] {
200
                                (byte) (h0 >>> 8), (byte) h0,
152
        (byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>> 8), (byte) h0,
201
                                (byte) (h1 >>> 24), (byte) (h1 >>> 16),
153
        (byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>> 8), (byte) h1,
202
                                (byte) (h1 >>> 8), (byte) h1,
154
        (byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
203
                                (byte) (h2 >>> 24), (byte) (h2 >>> 16),
155
        (byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>> 8), (byte) h3,
204
                                (byte) (h2 >>> 8), (byte) h2,
156
        (byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>> 8), (byte) h4 };
205
                                (byte) (h3 >>> 24), (byte) (h3 >>> 16),
206
                                (byte) (h3 >>> 8), (byte) h3,
207
                                (byte) (h4 >>> 24), (byte) (h4 >>> 16),
208
                                (byte) (h4 >>> 8), (byte) h4 };
209
210
    return result;
211
  }
157
  }
212
158
213
  protected void resetContext()
159
  protected void resetContext()
Lines 234-244 Link Here
234
    return valid.booleanValue();
180
    return valid.booleanValue();
235
  }
181
  }
236
182
237
  // SHA specific methods ----------------------------------------------------
183
  private static synchronized final int[] sha(int hh0, int hh1, int hh2,
238
184
                                              int hh3, int hh4, byte[] in,
239
  private static final synchronized int[]
185
                                              int offset)
240
  //   sha(int hh0, int hh1, int hh2, int hh3, int hh4, byte[] in, int offset, int[] w) {
241
  sha(int hh0, int hh1, int hh2, int hh3, int hh4, byte[] in, int offset)
242
  {
186
  {
243
    int A = hh0;
187
    int A = hh0;
244
    int B = hh1;
188
    int B = hh1;
Lines 246-265 Link Here
246
    int D = hh3;
190
    int D = hh3;
247
    int E = hh4;
191
    int E = hh4;
248
    int r, T;
192
    int r, T;
249
250
    for (r = 0; r < 16; r++)
193
    for (r = 0; r < 16; r++)
251
      {
194
      w[r] =  in[offset++]         << 24
252
        w[r] = in[offset++] << 24 | (in[offset++] & 0xFF) << 16
195
           | (in[offset++] & 0xFF) << 16
253
               | (in[offset++] & 0xFF) << 8 | (in[offset++] & 0xFF);
196
           | (in[offset++] & 0xFF) << 8
254
      }
197
           | (in[offset++] & 0xFF);
255
    for (r = 16; r < 80; r++)
198
    for (r = 16; r < 80; r++)
256
      {
199
      {
257
        T = w[r - 3] ^ w[r - 8] ^ w[r - 14] ^ w[r - 16];
200
        T = w[r - 3] ^ w[r - 8] ^ w[r - 14] ^ w[r - 16];
258
        w[r] = T << 1 | T >>> 31;
201
        w[r] = T << 1 | T >>> 31;
259
      }
202
      }
260
203
    for (r = 0; r < 20; r++) // rounds 0-19
261
    // rounds 0-19
262
    for (r = 0; r < 20; r++)
263
      {
204
      {
264
        T = (A << 5 | A >>> 27) + ((B & C) | (~B & D)) + E + w[r] + 0x5A827999;
205
        T = (A << 5 | A >>> 27) + ((B & C) | (~B & D)) + E + w[r] + 0x5A827999;
265
        E = D;
206
        E = D;
Lines 268-276 Link Here
268
        B = A;
209
        B = A;
269
        A = T;
210
        A = T;
270
      }
211
      }
271
212
    for (r = 20; r < 40; r++) // rounds 20-39
272
    // rounds 20-39
273
    for (r = 20; r < 40; r++)
274
      {
213
      {
275
        T = (A << 5 | A >>> 27) + (B ^ C ^ D) + E + w[r] + 0x6ED9EBA1;
214
        T = (A << 5 | A >>> 27) + (B ^ C ^ D) + E + w[r] + 0x6ED9EBA1;
276
        E = D;
215
        E = D;
Lines 279-299 Link Here
279
        B = A;
218
        B = A;
280
        A = T;
219
        A = T;
281
      }
220
      }
282
221
    for (r = 40; r < 60; r++) // rounds 40-59
283
    // rounds 40-59
284
    for (r = 40; r < 60; r++)
285
      {
222
      {
286
        T = (A << 5 | A >>> 27) + (B & C | B & D | C & D) + E + w[r]
223
        T = (A << 5 | A >>> 27) + (B & C | B & D | C & D) + E + w[r] + 0x8F1BBCDC;
287
            + 0x8F1BBCDC;
288
        E = D;
224
        E = D;
289
        D = C;
225
        D = C;
290
        C = B << 30 | B >>> 2;
226
        C = B << 30 | B >>> 2;
291
        B = A;
227
        B = A;
292
        A = T;
228
        A = T;
293
      }
229
      }
294
230
    for (r = 60; r < 80; r++) // rounds 60-79
295
    // rounds 60-79
296
    for (r = 60; r < 80; r++)
297
      {
231
      {
298
        T = (A << 5 | A >>> 27) + (B ^ C ^ D) + E + w[r] + 0xCA62C1D6;
232
        T = (A << 5 | A >>> 27) + (B ^ C ^ D) + E + w[r] + 0xCA62C1D6;
299
        E = D;
233
        E = D;
Lines 302-308 Link Here
302
        B = A;
236
        B = A;
303
        A = T;
237
        A = T;
304
      }
238
      }
305
306
    return new int[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E };
239
    return new int[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E };
307
  }
240
  }
308
}
241
}
(-)Sha256.java (-88 / +62 lines)
Lines 42-87 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>Implementation of SHA2-1 [SHA-256] per the IETF Draft Specification.</p>
45
 * Implementation of SHA2-1 [SHA-256] per the IETF Draft Specification.
46
 *
46
 * <p>
47
 * <p>References:</p>
47
 * References:
48
 * <ol>
48
 * <ol>
49
 *    <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
49
 *    <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
50
 *    Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
50
 *    Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
51
 *    <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
51
 *    <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
52
 * </ol>
52
 * </ol>
53
 */
53
 */
54
public class Sha256 extends BaseHash
54
public class Sha256
55
    extends BaseHash
55
{
56
{
56
57
  private static final int[] k = {
57
  // Constants and variables
58
      0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
58
  // -------------------------------------------------------------------------
59
      0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
59
  private static final int[] k = { 0x428a2f98, 0x71374491, 0xb5c0fbcf,
60
      0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
60
                                  0xe9b5dba5, 0x3956c25b, 0x59f111f1,
61
      0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
61
                                  0x923f82a4, 0xab1c5ed5, 0xd807aa98,
62
      0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
62
                                  0x12835b01, 0x243185be, 0x550c7dc3,
63
      0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
63
                                  0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
64
      0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
64
                                  0xc19bf174, 0xe49b69c1, 0xefbe4786,
65
      0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
65
                                  0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
66
      0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
66
                                  0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
67
      0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
67
                                  0x983e5152, 0xa831c66d, 0xb00327c8,
68
      0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
68
                                  0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
69
      0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
69
                                  0x06ca6351, 0x14292967, 0x27b70a85,
70
      0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
70
                                  0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
71
      0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
71
                                  0x650a7354, 0x766a0abb, 0x81c2c92e,
72
      0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
72
                                  0x92722c85, 0xa2bfe8a1, 0xa81a664b,
73
      0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
73
                                  0xc24b8b70, 0xc76c51a3, 0xd192e819,
74
  };
74
                                  0xd6990624, 0xf40e3585, 0x106aa070,
75
                                  0x19a4c116, 0x1e376c08, 0x2748774c,
76
                                  0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
77
                                  0x5b9cca4f, 0x682e6ff3, 0x748f82ee,
78
                                  0x78a5636f, 0x84c87814, 0x8cc70208,
79
                                  0x90befffa, 0xa4506ceb, 0xbef9a3f7,
80
                                  0xc67178f2 };
81
75
82
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
76
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
83
77
84
  private static final String DIGEST0 = "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD";
78
  private static final String DIGEST0 =
79
      "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD";
85
80
86
  private static final int[] w = new int[64];
81
  private static final int[] w = new int[64];
87
82
Lines 91-99 Link Here
91
  /** 256-bit interim result. */
86
  /** 256-bit interim result. */
92
  private int h0, h1, h2, h3, h4, h5, h6, h7;
87
  private int h0, h1, h2, h3, h4, h5, h6, h7;
93
88
94
  // Constructor(s)
95
  // -------------------------------------------------------------------------
96
97
  /** Trivial 0-arguments constructor. */
89
  /** Trivial 0-arguments constructor. */
98
  public Sha256()
90
  public Sha256()
99
  {
91
  {
Lines 101-107 Link Here
101
  }
93
  }
102
94
103
  /**
95
  /**
104
   * <p>Private constructor for cloning purposes.</p>
96
   * Private constructor for cloning purposes.
105
   *
97
   *
106
   * @param md the instance to clone.
98
   * @param md the instance to clone.
107
   */
99
   */
Lines 121-151 Link Here
121
    this.buffer = (byte[]) md.buffer.clone();
113
    this.buffer = (byte[]) md.buffer.clone();
122
  }
114
  }
123
115
124
  // Class methods
125
  // -------------------------------------------------------------------------
126
127
  public static final int[] G(int hh0, int hh1, int hh2, int hh3, int hh4,
116
  public static final int[] G(int hh0, int hh1, int hh2, int hh3, int hh4,
128
                              int hh5, int hh6, int hh7, byte[] in, int offset)
117
                              int hh5, int hh6, int hh7, byte[] in, int offset)
129
  {
118
  {
130
    return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
119
    return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
131
  }
120
  }
132
121
133
  // Instance methods
134
  // -------------------------------------------------------------------------
135
136
  // java.lang.Cloneable interface implementation ----------------------------
137
138
  public Object clone()
122
  public Object clone()
139
  {
123
  {
140
    return new Sha256(this);
124
    return new Sha256(this);
141
  }
125
  }
142
126
143
  // Implementation of concrete methods in BaseHash --------------------------
144
145
  protected void transform(byte[] in, int offset)
127
  protected void transform(byte[] in, int offset)
146
  {
128
  {
147
    int[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
129
    int[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
148
149
    h0 = result[0];
130
    h0 = result[0];
150
    h1 = result[1];
131
    h1 = result[1];
151
    h2 = result[2];
132
    h2 = result[2];
Lines 158-198 Link Here
158
139
159
  protected byte[] padBuffer()
140
  protected byte[] padBuffer()
160
  {
141
  {
161
    int n = (int) (count % BLOCK_SIZE);
142
    int n = (int)(count % BLOCK_SIZE);
162
    int padding = (n < 56) ? (56 - n) : (120 - n);
143
    int padding = (n < 56) ? (56 - n) : (120 - n);
163
    byte[] result = new byte[padding + 8];
144
    byte[] result = new byte[padding + 8];
164
165
    // padding is always binary 1 followed by binary 0s
145
    // padding is always binary 1 followed by binary 0s
166
    result[0] = (byte) 0x80;
146
    result[0] = (byte) 0x80;
167
168
    // save number of bits, casting the long to an array of 8 bytes
147
    // save number of bits, casting the long to an array of 8 bytes
169
    long bits = count << 3;
148
    long bits = count << 3;
170
    result[padding++] = (byte) (bits >>> 56);
149
    result[padding++] = (byte)(bits >>> 56);
171
    result[padding++] = (byte) (bits >>> 48);
150
    result[padding++] = (byte)(bits >>> 48);
172
    result[padding++] = (byte) (bits >>> 40);
151
    result[padding++] = (byte)(bits >>> 40);
173
    result[padding++] = (byte) (bits >>> 32);
152
    result[padding++] = (byte)(bits >>> 32);
174
    result[padding++] = (byte) (bits >>> 24);
153
    result[padding++] = (byte)(bits >>> 24);
175
    result[padding++] = (byte) (bits >>> 16);
154
    result[padding++] = (byte)(bits >>> 16);
176
    result[padding++] = (byte) (bits >>> 8);
155
    result[padding++] = (byte)(bits >>> 8);
177
    result[padding] = (byte) bits;
156
    result[padding  ] = (byte) bits;
178
179
    return result;
157
    return result;
180
  }
158
  }
181
159
182
  protected byte[] getResult()
160
  protected byte[] getResult()
183
  {
161
  {
184
    return new byte[] { (byte) (h0 >>> 24), (byte) (h0 >>> 16),
162
    return new byte[] {
185
                       (byte) (h0 >>> 8), (byte) h0, (byte) (h1 >>> 24),
163
        (byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>> 8), (byte) h0,
186
                       (byte) (h1 >>> 16), (byte) (h1 >>> 8), (byte) h1,
164
        (byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>> 8), (byte) h1,
187
                       (byte) (h2 >>> 24), (byte) (h2 >>> 16),
165
        (byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
188
                       (byte) (h2 >>> 8), (byte) h2, (byte) (h3 >>> 24),
166
        (byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>> 8), (byte) h3,
189
                       (byte) (h3 >>> 16), (byte) (h3 >>> 8), (byte) h3,
167
        (byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>> 8), (byte) h4,
190
                       (byte) (h4 >>> 24), (byte) (h4 >>> 16),
168
        (byte)(h5 >>> 24), (byte)(h5 >>> 16), (byte)(h5 >>> 8), (byte) h5,
191
                       (byte) (h4 >>> 8), (byte) h4, (byte) (h5 >>> 24),
169
        (byte)(h6 >>> 24), (byte)(h6 >>> 16), (byte)(h6 >>> 8), (byte) h6,
192
                       (byte) (h5 >>> 16), (byte) (h5 >>> 8), (byte) h5,
170
        (byte)(h7 >>> 24), (byte)(h7 >>> 16), (byte)(h7 >>> 8), (byte) h7 };
193
                       (byte) (h6 >>> 24), (byte) (h6 >>> 16),
194
                       (byte) (h6 >>> 8), (byte) h6, (byte) (h7 >>> 24),
195
                       (byte) (h7 >>> 16), (byte) (h7 >>> 8), (byte) h7 };
196
  }
171
  }
197
172
198
  protected void resetContext()
173
  protected void resetContext()
Lines 219-231 Link Here
219
        String result = Util.toString(md.digest());
194
        String result = Util.toString(md.digest());
220
        valid = Boolean.valueOf(DIGEST0.equals(result));
195
        valid = Boolean.valueOf(DIGEST0.equals(result));
221
      }
196
      }
222
223
    return valid.booleanValue();
197
    return valid.booleanValue();
224
  }
198
  }
225
199
226
  // SHA specific methods ----------------------------------------------------
200
  private static synchronized final int[] sha(int hh0, int hh1, int hh2,
227
228
  private static final synchronized int[] sha(int hh0, int hh1, int hh2,
229
                                              int hh3, int hh4, int hh5,
201
                                              int hh3, int hh4, int hh5,
230
                                              int hh6, int hh7, byte[] in,
202
                                              int hh6, int hh7, byte[] in,
231
                                              int offset)
203
                                              int offset)
Lines 239-267 Link Here
239
    int G = hh6;
211
    int G = hh6;
240
    int H = hh7;
212
    int H = hh7;
241
    int r, T, T2;
213
    int r, T, T2;
242
243
    for (r = 0; r < 16; r++)
214
    for (r = 0; r < 16; r++)
244
      {
215
      w[r] = (in[offset++]         << 24
245
        w[r] = (in[offset++] << 24 | (in[offset++] & 0xFF) << 16
216
           | (in[offset++] & 0xFF) << 16
246
                | (in[offset++] & 0xFF) << 8 | (in[offset++] & 0xFF));
217
           | (in[offset++] & 0xFF) << 8
247
      }
218
           | (in[offset++] & 0xFF));
248
    for (r = 16; r < 64; r++)
219
    for (r = 16; r < 64; r++)
249
      {
220
      {
250
        T = w[r - 2];
221
        T =  w[r -  2];
251
        T2 = w[r - 15];
222
        T2 = w[r - 15];
252
        w[r] = ((((T >>> 17) | (T << 15)) ^ ((T >>> 19) | (T << 13)) ^ (T >>> 10))
223
        w[r] = ((((T >>> 17) | (T << 15)) ^ ((T >>> 19) | (T << 13)) ^ (T >>> 10))
253
                + w[r - 7]
224
                + w[r - 7]
254
                + (((T2 >>> 7) | (T2 << 25)) ^ ((T2 >>> 18) | (T2 << 14)) ^ (T2 >>> 3))
225
                + (((T2 >>> 7) | (T2 << 25))
255
                + w[r - 16]);
226
                    ^ ((T2 >>> 18) | (T2 << 14))
227
                    ^ (T2 >>> 3)) + w[r - 16]);
256
      }
228
      }
257
258
    for (r = 0; r < 64; r++)
229
    for (r = 0; r < 64; r++)
259
      {
230
      {
260
        T = (H
231
        T = (H
261
             + (((E >>> 6) | (E << 26)) ^ ((E >>> 11) | (E << 21)) ^ ((E >>> 25) | (E << 7)))
232
             + (((E >>> 6) | (E << 26))
233
                 ^ ((E >>> 11) | (E << 21))
234
                 ^ ((E >>> 25) | (E << 7)))
262
             + ((E & F) ^ (~E & G)) + k[r] + w[r]);
235
             + ((E & F) ^ (~E & G)) + k[r] + w[r]);
263
        T2 = ((((A >>> 2) | (A << 30)) ^ ((A >>> 13) | (A << 19)) ^ ((A >>> 22) | (A << 10)))
236
        T2 = ((((A >>> 2) | (A << 30))
264
              + ((A & B) ^ (A & C) ^ (B & C)));
237
               ^ ((A >>> 13) | (A << 19))
238
               ^ ((A >>> 22) | (A << 10))) + ((A & B) ^ (A & C) ^ (B & C)));
265
        H = G;
239
        H = G;
266
        G = F;
240
        G = F;
267
        F = E;
241
        F = E;
Lines 271-278 Link Here
271
        B = A;
245
        B = A;
272
        A = T + T2;
246
        A = T + T2;
273
      }
247
      }
274
248
    return new int[] {
275
    return new int[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E, hh5 + F,
249
        hh0 + A, hh1 + B, hh2 + C, hh3 + D,
276
                       hh6 + G, hh7 + H };
250
        hh4 + E, hh5 + F, hh6 + G, hh7 + H };
277
  }
251
  }
278
}
252
}
(-)Sha384.java (-123 / +80 lines)
Lines 42-107 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>Implementation of SHA2-2 [SHA-384] per the IETF Draft Specification.</p>
45
 * Implementation of SHA2-2 [SHA-384] per the IETF Draft Specification.
46
 *
46
 * <p>
47
 * <p>References:</p>
47
 * References:
48
 * <ol>
48
 * <ol>
49
 *    <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
49
 *    <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
50
 *    Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
50
 *    Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
51
 *    <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
51
 *    <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
52
 * </ol>
52
 * </ol>
53
 */
53
 */
54
public class Sha384 extends BaseHash
54
public class Sha384
55
    extends BaseHash
55
{
56
{
56
57
  private static final long[] k = {
57
  // Constants and variables
58
      0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL,
58
  // -------------------------------------------------------------------------
59
      0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L,
59
60
      0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L,
60
  private static final long[] k = { 0x428a2f98d728ae22L, 0x7137449123ef65cdL,
61
      0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
61
                                   0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
62
      0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L,
62
                                   0x3956c25bf348b538L, 0x59f111f1b605d019L,
63
      0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
63
                                   0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
64
      0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L,
64
                                   0xd807aa98a3030242L, 0x12835b0145706fbeL,
65
      0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
65
                                   0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
66
      0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL,
66
                                   0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L,
67
      0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
67
                                   0x9bdc06a725c71235L, 0xc19bf174cf692694L,
68
      0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL,
68
                                   0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
69
      0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
69
                                   0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
70
      0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L,
70
                                   0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L,
71
      0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
71
                                   0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
72
      0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L,
72
                                   0x983e5152ee66dfabL, 0xa831c66d2db43210L,
73
      0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
73
                                   0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
74
      0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L,
74
                                   0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
75
      0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
75
                                   0x06ca6351e003826fL, 0x142929670a0e6e70L,
76
      0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL,
76
                                   0x27b70a8546d22ffcL, 0x2e1b21385c26c926L,
77
      0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
77
                                   0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
78
      0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L,
78
                                   0x650a73548baf63deL, 0x766a0abb3c77b2a8L,
79
      0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L,
79
                                   0x81c2c92e47edaee6L, 0x92722c851482353bL,
80
      0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL,
80
                                   0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
81
      0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
81
                                   0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
82
      0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL,
82
                                   0xd192e819d6ef5218L, 0xd69906245565a910L,
83
      0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
83
                                   0xf40e35855771202aL, 0x106aa07032bbd1b8L,
84
      0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
84
                                   0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L,
85
                                   0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
86
                                   0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
87
                                   0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
88
                                   0x748f82ee5defb2fcL, 0x78a5636f43172f60L,
89
                                   0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
90
                                   0x90befffa23631e28L, 0xa4506cebde82bde9L,
91
                                   0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
92
                                   0xca273eceea26619cL, 0xd186b8c721c0c207L,
93
                                   0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
94
                                   0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L,
95
                                   0x113f9804bef90daeL, 0x1b710b35131c471bL,
96
                                   0x28db77f523047d84L, 0x32caab7b40c72493L,
97
                                   0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
98
                                   0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
99
                                   0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
100
85
101
  private static final int BLOCK_SIZE = 128; // inner block size in bytes
86
  private static final int BLOCK_SIZE = 128; // inner block size in bytes
102
87
103
  private static final String DIGEST0 = "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED"
88
  private static final String DIGEST0 =
104
                                        + "8086072BA1E7CC2358BAECA134C825A7";
89
      "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED"
90
    + "8086072BA1E7CC2358BAECA134C825A7";
105
91
106
  private static final long[] w = new long[80];
92
  private static final long[] w = new long[80];
107
93
Lines 111-119 Link Here
111
  /** 512-bit interim result. */
97
  /** 512-bit interim result. */
112
  private long h0, h1, h2, h3, h4, h5, h6, h7;
98
  private long h0, h1, h2, h3, h4, h5, h6, h7;
113
99
114
  // Constructor(s)
115
  // -------------------------------------------------------------------------
116
117
  /** Trivial 0-arguments constructor. */
100
  /** Trivial 0-arguments constructor. */
118
  public Sha384()
101
  public Sha384()
119
  {
102
  {
Lines 121-127 Link Here
121
  }
104
  }
122
105
123
  /**
106
  /**
124
   * <p>Private constructor for cloning purposes.</p>
107
   * Private constructor for cloning purposes.
125
   *
108
   *
126
   * @param md the instance to clone.
109
   * @param md the instance to clone.
127
   */
110
   */
Lines 141-149 Link Here
141
    this.buffer = (byte[]) md.buffer.clone();
124
    this.buffer = (byte[]) md.buffer.clone();
142
  }
125
  }
143
126
144
  // Class methods
145
  // -------------------------------------------------------------------------
146
147
  public static final long[] G(long hh0, long hh1, long hh2, long hh3,
127
  public static final long[] G(long hh0, long hh1, long hh2, long hh3,
148
                               long hh4, long hh5, long hh6, long hh7,
128
                               long hh4, long hh5, long hh6, long hh7,
149
                               byte[] in, int offset)
129
                               byte[] in, int offset)
Lines 151-172 Link Here
151
    return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
131
    return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
152
  }
132
  }
153
133
154
  // Instance methods
155
  // -------------------------------------------------------------------------
156
157
  // java.lang.Cloneable interface implementation ----------------------------
158
159
  public Object clone()
134
  public Object clone()
160
  {
135
  {
161
    return new Sha384(this);
136
    return new Sha384(this);
162
  }
137
  }
163
138
164
  // Implementation of concrete methods in BaseHash --------------------------
165
166
  protected void transform(byte[] in, int offset)
139
  protected void transform(byte[] in, int offset)
167
  {
140
  {
168
    long[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
141
    long[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
169
170
    h0 = result[0];
142
    h0 = result[0];
171
    h1 = result[1];
143
    h1 = result[1];
172
    h2 = result[2];
144
    h2 = result[2];
Lines 179-235 Link Here
179
151
180
  protected byte[] padBuffer()
152
  protected byte[] padBuffer()
181
  {
153
  {
182
    int n = (int) (count % BLOCK_SIZE);
154
    int n = (int)(count % BLOCK_SIZE);
183
    int padding = (n < 112) ? (112 - n) : (240 - n);
155
    int padding = (n < 112) ? (112 - n) : (240 - n);
184
    byte[] result = new byte[padding + 16];
156
    byte[] result = new byte[padding + 16];
185
186
    // padding is always binary 1 followed by binary 0s
157
    // padding is always binary 1 followed by binary 0s
187
    result[0] = (byte) 0x80;
158
    result[0] = (byte) 0x80;
188
189
    // save number of bits, casting the long to an array of 8 bytes
159
    // save number of bits, casting the long to an array of 8 bytes
190
    // TODO: FIX Only ~35 bits of 128 bit counter usable this way
160
    // TODO: FIX Only ~35 bits of 128 bit counter usable this way
191
    long bits = count << 3;
161
    long bits = count << 3;
192
    padding += 8;
162
    padding += 8;
193
    result[padding++] = (byte) (bits >>> 56);
163
    result[padding++] = (byte)(bits >>> 56);
194
    result[padding++] = (byte) (bits >>> 48);
164
    result[padding++] = (byte)(bits >>> 48);
195
    result[padding++] = (byte) (bits >>> 40);
165
    result[padding++] = (byte)(bits >>> 40);
196
    result[padding++] = (byte) (bits >>> 32);
166
    result[padding++] = (byte)(bits >>> 32);
197
    result[padding++] = (byte) (bits >>> 24);
167
    result[padding++] = (byte)(bits >>> 24);
198
    result[padding++] = (byte) (bits >>> 16);
168
    result[padding++] = (byte)(bits >>> 16);
199
    result[padding++] = (byte) (bits >>> 8);
169
    result[padding++] = (byte)(bits >>> 8);
200
    result[padding] = (byte) bits;
170
    result[padding  ] = (byte) bits;
201
202
    return result;
171
    return result;
203
  }
172
  }
204
173
205
  protected byte[] getResult()
174
  protected byte[] getResult()
206
  {
175
  {
207
    return new byte[] { (byte) (h0 >>> 56), (byte) (h0 >>> 48),
176
    return new byte[] {
208
                       (byte) (h0 >>> 40), (byte) (h0 >>> 32),
177
        (byte)(h0 >>> 56), (byte)(h0 >>> 48), (byte)(h0 >>> 40), (byte)(h0 >>> 32),
209
                       (byte) (h0 >>> 24), (byte) (h0 >>> 16),
178
        (byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>>  8), (byte) h0,
210
                       (byte) (h0 >>> 8), (byte) h0, (byte) (h1 >>> 56),
179
        (byte)(h1 >>> 56), (byte)(h1 >>> 48), (byte)(h1 >>> 40), (byte)(h1 >>> 32),
211
                       (byte) (h1 >>> 48), (byte) (h1 >>> 40),
180
        (byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>>  8), (byte) h1,
212
                       (byte) (h1 >>> 32), (byte) (h1 >>> 24),
181
        (byte)(h2 >>> 56), (byte)(h2 >>> 48), (byte)(h2 >>> 40), (byte)(h2 >>> 32),
213
                       (byte) (h1 >>> 16), (byte) (h1 >>> 8), (byte) h1,
182
        (byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>>  8), (byte) h2,
214
                       (byte) (h2 >>> 56), (byte) (h2 >>> 48),
183
        (byte)(h3 >>> 56), (byte)(h3 >>> 48), (byte)(h3 >>> 40), (byte)(h3 >>> 32),
215
                       (byte) (h2 >>> 40), (byte) (h2 >>> 32),
184
        (byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>>  8), (byte) h3,
216
                       (byte) (h2 >>> 24), (byte) (h2 >>> 16),
185
        (byte)(h4 >>> 56), (byte)(h4 >>> 48), (byte)(h4 >>> 40), (byte)(h4 >>> 32),
217
                       (byte) (h2 >>> 8), (byte) h2, (byte) (h3 >>> 56),
186
        (byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>>  8), (byte) h4,
218
                       (byte) (h3 >>> 48), (byte) (h3 >>> 40),
187
        (byte)(h5 >>> 56), (byte)(h5 >>> 48), (byte)(h5 >>> 40), (byte)(h5 >>> 32),
219
                       (byte) (h3 >>> 32), (byte) (h3 >>> 24),
188
        (byte)(h5 >>> 24), (byte)(h5 >>> 16), (byte)(h5 >>>  8), (byte) h5 };
220
                       (byte) (h3 >>> 16), (byte) (h3 >>> 8), (byte) h3,
221
                       (byte) (h4 >>> 56), (byte) (h4 >>> 48),
222
                       (byte) (h4 >>> 40), (byte) (h4 >>> 32),
223
                       (byte) (h4 >>> 24), (byte) (h4 >>> 16),
224
                       (byte) (h4 >>> 8), (byte) h4, (byte) (h5 >>> 56),
225
                       (byte) (h5 >>> 48), (byte) (h5 >>> 40),
226
                       (byte) (h5 >>> 32), (byte) (h5 >>> 24),
227
                       (byte) (h5 >>> 16), (byte) (h5 >>> 8), (byte) h5
228
    //         (byte)(h6 >>> 56), (byte)(h6 >>> 48), (byte)(h6 >>> 40), (byte)(h6 >>> 32),
229
    //         (byte)(h6 >>> 24), (byte)(h6 >>> 16), (byte)(h6 >>>  8), (byte) h6,
230
    //         (byte)(h7 >>> 56), (byte)(h7 >>> 48), (byte)(h7 >>> 40), (byte)(h7 >>> 32),
231
    //         (byte)(h7 >>> 24), (byte)(h7 >>> 16), (byte)(h7 >>>  8), (byte) h7
232
    };
233
  }
189
  }
234
190
235
  protected void resetContext()
191
  protected void resetContext()
Lines 259-267 Link Here
259
    return valid.booleanValue();
215
    return valid.booleanValue();
260
  }
216
  }
261
217
262
  // SHA specific methods ----------------------------------------------------
218
  private static synchronized final long[] sha(long hh0, long hh1, long hh2,
263
264
  private static final synchronized long[] sha(long hh0, long hh1, long hh2,
265
                                               long hh3, long hh4, long hh5,
219
                                               long hh3, long hh4, long hh5,
266
                                               long hh6, long hh7, byte[] in,
220
                                               long hh6, long hh7, byte[] in,
267
                                               int offset)
221
                                               int offset)
Lines 276-310 Link Here
276
    long H = hh7;
230
    long H = hh7;
277
    long T, T2;
231
    long T, T2;
278
    int r;
232
    int r;
279
280
    for (r = 0; r < 16; r++)
233
    for (r = 0; r < 16; r++)
281
      {
234
      w[r] =  (long) in[offset++]         << 56
282
        w[r] = (long) in[offset++] << 56 | ((long) in[offset++] & 0xFF) << 48
235
           | ((long) in[offset++] & 0xFF) << 48
283
               | ((long) in[offset++] & 0xFF) << 40
236
           | ((long) in[offset++] & 0xFF) << 40
284
               | ((long) in[offset++] & 0xFF) << 32
237
           | ((long) in[offset++] & 0xFF) << 32
285
               | ((long) in[offset++] & 0xFF) << 24
238
           | ((long) in[offset++] & 0xFF) << 24
286
               | ((long) in[offset++] & 0xFF) << 16
239
           | ((long) in[offset++] & 0xFF) << 16
287
               | ((long) in[offset++] & 0xFF) << 8
240
           | ((long) in[offset++] & 0xFF) << 8
288
               | ((long) in[offset++] & 0xFF);
241
           | ((long) in[offset++] & 0xFF);
289
      }
290
    for (r = 16; r < 80; r++)
242
    for (r = 16; r < 80; r++)
291
      {
243
      {
292
        T = w[r - 2];
244
        T = w[r - 2];
293
        T2 = w[r - 15];
245
        T2 = w[r - 15];
294
        w[r] = (((T >>> 19) | (T << 45)) ^ ((T >>> 61) | (T << 3)) ^ (T >>> 6))
246
        w[r] = (((T >>> 19) | (T << 45)) ^ ((T >>> 61) | (T << 3)) ^ (T >>> 6))
295
               + w[r - 7]
247
               + w[r - 7]
296
               + (((T2 >>> 1) | (T2 << 63)) ^ ((T2 >>> 8) | (T2 << 56)) ^ (T2 >>> 7))
248
               + (((T2 >>> 1) | (T2 << 63))
249
                   ^ ((T2 >>> 8) | (T2 << 56))
250
                   ^ (T2 >>> 7))
297
               + w[r - 16];
251
               + w[r - 16];
298
      }
252
      }
299
300
    for (r = 0; r < 80; r++)
253
    for (r = 0; r < 80; r++)
301
      {
254
      {
302
255
303
        T = H
256
        T = H
304
            + (((E >>> 14) | (E << 50)) ^ ((E >>> 18) | (E << 46)) ^ ((E >>> 41) | (E << 23)))
257
            + (((E >>> 14) | (E << 50))
258
                ^ ((E >>> 18) | (E << 46))
259
                ^ ((E >>> 41) | (E << 23)))
305
            + ((E & F) ^ ((~E) & G)) + k[r] + w[r];
260
            + ((E & F) ^ ((~E) & G)) + k[r] + w[r];
306
        // T IS INCORRECT SOMEHOW
261
        // T IS INCORRECT SOMEHOW
307
        T2 = (((A >>> 28) | (A << 36)) ^ ((A >>> 34) | (A << 30)) ^ ((A >>> 39) | (A << 25)))
262
        T2 = (((A >>> 28) | (A << 36))
263
               ^ ((A >>> 34) | (A << 30))
264
               ^ ((A >>> 39) | (A << 25)))
308
             + ((A & B) ^ (A & C) ^ (B & C));
265
             + ((A & B) ^ (A & C) ^ (B & C));
309
        H = G;
266
        H = G;
310
        G = F;
267
        G = F;
Lines 315-322 Link Here
315
        B = A;
272
        B = A;
316
        A = T + T2;
273
        A = T + T2;
317
      }
274
      }
318
275
    return new long[] {
319
    return new long[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E, hh5 + F,
276
        hh0 + A, hh1 + B, hh2 + C, hh3 + D,
320
                       hh6 + G, hh7 + H };
277
        hh4 + E, hh5 + F, hh6 + G, hh7 + H };
321
  }
278
  }
322
}
279
}
(-)Sha512.java (-125 / +84 lines)
Lines 42-107 Link Here
42
import gnu.java.security.util.Util;
42
import gnu.java.security.util.Util;
43
43
44
/**
44
/**
45
 * <p>Implementation of SHA2-3 [SHA-512] per the IETF Draft Specification.</p>
45
 * Implementation of SHA2-3 [SHA-512] per the IETF Draft Specification.
46
 *
46
 * <p>
47
 * <p>References:</p>
47
 * References:
48
 * <ol>
48
 * <ol>
49
 *    <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
49
 *    <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
50
 *    Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
50
 *    Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
51
 *    <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
51
 *    <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
52
 * </ol>
52
 * </ol>
53
 */
53
 */
54
public class Sha512 extends BaseHash
54
public class Sha512
55
    extends BaseHash
55
{
56
{
56
57
  private static final long[] k = {
57
  // Constants and variables
58
      0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL,
58
  // -------------------------------------------------------------------------
59
      0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L,
59
60
      0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L,
60
  private static final long[] k = { 0x428a2f98d728ae22L, 0x7137449123ef65cdL,
61
      0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
61
                                   0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
62
      0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L,
62
                                   0x3956c25bf348b538L, 0x59f111f1b605d019L,
63
      0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
63
                                   0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
64
      0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L,
64
                                   0xd807aa98a3030242L, 0x12835b0145706fbeL,
65
      0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
65
                                   0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
66
      0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL,
66
                                   0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L,
67
      0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
67
                                   0x9bdc06a725c71235L, 0xc19bf174cf692694L,
68
      0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL,
68
                                   0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
69
      0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
69
                                   0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
70
      0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L,
70
                                   0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L,
71
      0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
71
                                   0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
72
      0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L,
72
                                   0x983e5152ee66dfabL, 0xa831c66d2db43210L,
73
      0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
73
                                   0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
74
      0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L,
74
                                   0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
75
      0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
75
                                   0x06ca6351e003826fL, 0x142929670a0e6e70L,
76
      0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL,
76
                                   0x27b70a8546d22ffcL, 0x2e1b21385c26c926L,
77
      0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
77
                                   0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
78
      0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L,
78
                                   0x650a73548baf63deL, 0x766a0abb3c77b2a8L,
79
      0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L,
79
                                   0x81c2c92e47edaee6L, 0x92722c851482353bL,
80
      0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL,
80
                                   0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
81
      0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
81
                                   0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
82
      0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL,
82
                                   0xd192e819d6ef5218L, 0xd69906245565a910L,
83
      0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
83
                                   0xf40e35855771202aL, 0x106aa07032bbd1b8L,
84
      0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
84
                                   0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L,
85
                                   0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
86
                                   0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
87
                                   0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
88
                                   0x748f82ee5defb2fcL, 0x78a5636f43172f60L,
89
                                   0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
90
                                   0x90befffa23631e28L, 0xa4506cebde82bde9L,
91
                                   0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
92
                                   0xca273eceea26619cL, 0xd186b8c721c0c207L,
93
                                   0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
94
                                   0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L,
95
                                   0x113f9804bef90daeL, 0x1b710b35131c471bL,
96
                                   0x28db77f523047d84L, 0x32caab7b40c72493L,
97
                                   0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
98
                                   0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
99
                                   0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
100
85
101
  private static final int BLOCK_SIZE = 128; // inner block size in bytes
86
  private static final int BLOCK_SIZE = 128; // inner block size in bytes
102
87
103
  private static final String DIGEST0 = "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A"
88
  private static final String DIGEST0 =
104
                                        + "2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F";
89
      "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A"
90
    + "2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F";
105
91
106
  private static final long[] w = new long[80];
92
  private static final long[] w = new long[80];
107
93
Lines 111-119 Link Here
111
  /** 512-bit interim result. */
97
  /** 512-bit interim result. */
112
  private long h0, h1, h2, h3, h4, h5, h6, h7;
98
  private long h0, h1, h2, h3, h4, h5, h6, h7;
113
99
114
  // Constructor(s)
115
  // -------------------------------------------------------------------------
116
117
  /** Trivial 0-arguments constructor. */
100
  /** Trivial 0-arguments constructor. */
118
  public Sha512()
101
  public Sha512()
119
  {
102
  {
Lines 121-127 Link Here
121
  }
104
  }
122
105
123
  /**
106
  /**
124
   * <p>Private constructor for cloning purposes.</p>
107
   * Private constructor for cloning purposes.
125
   *
108
   *
126
   * @param md the instance to clone.
109
   * @param md the instance to clone.
127
   */
110
   */
Lines 141-149 Link Here
141
    this.buffer = (byte[]) md.buffer.clone();
124
    this.buffer = (byte[]) md.buffer.clone();
142
  }
125
  }
143
126
144
  // Class methods
145
  // -------------------------------------------------------------------------
146
147
  public static final long[] G(long hh0, long hh1, long hh2, long hh3,
127
  public static final long[] G(long hh0, long hh1, long hh2, long hh3,
148
                               long hh4, long hh5, long hh6, long hh7,
128
                               long hh4, long hh5, long hh6, long hh7,
149
                               byte[] in, int offset)
129
                               byte[] in, int offset)
Lines 151-172 Link Here
151
    return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
131
    return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
152
  }
132
  }
153
133
154
  // Instance methods
155
  // -------------------------------------------------------------------------
156
157
  // java.lang.Cloneable interface implementation ----------------------------
158
159
  public Object clone()
134
  public Object clone()
160
  {
135
  {
161
    return new Sha512(this);
136
    return new Sha512(this);
162
  }
137
  }
163
138
164
  // Implementation of concrete methods in BaseHash --------------------------
165
166
  protected void transform(byte[] in, int offset)
139
  protected void transform(byte[] in, int offset)
167
  {
140
  {
168
    long[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
141
    long[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
169
170
    h0 = result[0];
142
    h0 = result[0];
171
    h1 = result[1];
143
    h1 = result[1];
172
    h2 = result[2];
144
    h2 = result[2];
Lines 179-237 Link Here
179
151
180
  protected byte[] padBuffer()
152
  protected byte[] padBuffer()
181
  {
153
  {
182
    int n = (int) (count % BLOCK_SIZE);
154
    int n = (int)(count % BLOCK_SIZE);
183
    int padding = (n < 112) ? (112 - n) : (240 - n);
155
    int padding = (n < 112) ? (112 - n) : (240 - n);
184
    byte[] result = new byte[padding + 16];
156
    byte[] result = new byte[padding + 16];
185
186
    // padding is always binary 1 followed by binary 0s
157
    // padding is always binary 1 followed by binary 0s
187
    result[0] = (byte) 0x80;
158
    result[0] = (byte) 0x80;
188
189
    // save number of bits, casting the long to an array of 8 bytes
159
    // save number of bits, casting the long to an array of 8 bytes
190
    // TODO: FIX Only ~35 bits of 128 bit counter usable this way
160
    // TODO: FIX Only ~35 bits of 128 bit counter usable this way
191
    long bits = count << 3;
161
    long bits = count << 3;
192
    padding += 8;
162
    padding += 8;
193
    result[padding++] = (byte) (bits >>> 56);
163
    result[padding++] = (byte)(bits >>> 56);
194
    result[padding++] = (byte) (bits >>> 48);
164
    result[padding++] = (byte)(bits >>> 48);
195
    result[padding++] = (byte) (bits >>> 40);
165
    result[padding++] = (byte)(bits >>> 40);
196
    result[padding++] = (byte) (bits >>> 32);
166
    result[padding++] = (byte)(bits >>> 32);
197
    result[padding++] = (byte) (bits >>> 24);
167
    result[padding++] = (byte)(bits >>> 24);
198
    result[padding++] = (byte) (bits >>> 16);
168
    result[padding++] = (byte)(bits >>> 16);
199
    result[padding++] = (byte) (bits >>> 8);
169
    result[padding++] = (byte)(bits >>> 8);
200
    result[padding] = (byte) bits;
170
    result[padding  ] = (byte) bits;
201
202
    return result;
171
    return result;
203
  }
172
  }
204
173
205
  protected byte[] getResult()
174
  protected byte[] getResult()
206
  {
175
  {
207
    return new byte[] { (byte) (h0 >>> 56), (byte) (h0 >>> 48),
176
    return new byte[] {
208
                       (byte) (h0 >>> 40), (byte) (h0 >>> 32),
177
        (byte)(h0 >>> 56), (byte)(h0 >>> 48), (byte)(h0 >>> 40), (byte)(h0 >>> 32),
209
                       (byte) (h0 >>> 24), (byte) (h0 >>> 16),
178
        (byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>>  8), (byte) h0,
210
                       (byte) (h0 >>> 8), (byte) h0, (byte) (h1 >>> 56),
179
        (byte)(h1 >>> 56), (byte)(h1 >>> 48), (byte)(h1 >>> 40), (byte)(h1 >>> 32),
211
                       (byte) (h1 >>> 48), (byte) (h1 >>> 40),
180
        (byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>>  8), (byte) h1,
212
                       (byte) (h1 >>> 32), (byte) (h1 >>> 24),
181
        (byte)(h2 >>> 56), (byte)(h2 >>> 48), (byte)(h2 >>> 40), (byte)(h2 >>> 32),
213
                       (byte) (h1 >>> 16), (byte) (h1 >>> 8), (byte) h1,
182
        (byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
214
                       (byte) (h2 >>> 56), (byte) (h2 >>> 48),
183
        (byte)(h3 >>> 56), (byte)(h3 >>> 48), (byte)(h3 >>> 40), (byte)(h3 >>> 32),
215
                       (byte) (h2 >>> 40), (byte) (h2 >>> 32),
184
        (byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>>  8), (byte) h3,
216
                       (byte) (h2 >>> 24), (byte) (h2 >>> 16),
185
        (byte)(h4 >>> 56), (byte)(h4 >>> 48), (byte)(h4 >>> 40), (byte)(h4 >>> 32),
217
                       (byte) (h2 >>> 8), (byte) h2, (byte) (h3 >>> 56),
186
        (byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>>  8), (byte) h4,
218
                       (byte) (h3 >>> 48), (byte) (h3 >>> 40),
187
        (byte)(h5 >>> 56), (byte)(h5 >>> 48), (byte)(h5 >>> 40), (byte)(h5 >>> 32),
219
                       (byte) (h3 >>> 32), (byte) (h3 >>> 24),
188
        (byte)(h5 >>> 24), (byte)(h5 >>> 16), (byte)(h5 >>> 8), (byte) h5,
220
                       (byte) (h3 >>> 16), (byte) (h3 >>> 8), (byte) h3,
189
        (byte)(h6 >>> 56), (byte)(h6 >>> 48), (byte)(h6 >>> 40), (byte)(h6 >>> 32),
221
                       (byte) (h4 >>> 56), (byte) (h4 >>> 48),
190
        (byte)(h6 >>> 24), (byte)(h6 >>> 16), (byte)(h6 >>>  8), (byte) h6,
222
                       (byte) (h4 >>> 40), (byte) (h4 >>> 32),
191
        (byte)(h7 >>> 56), (byte)(h7 >>> 48), (byte)(h7 >>> 40), (byte)(h7 >>> 32),
223
                       (byte) (h4 >>> 24), (byte) (h4 >>> 16),
192
        (byte)(h7 >>> 24), (byte)(h7 >>> 16), (byte)(h7 >>>  8), (byte) h7 };
224
                       (byte) (h4 >>> 8), (byte) h4, (byte) (h5 >>> 56),
225
                       (byte) (h5 >>> 48), (byte) (h5 >>> 40),
226
                       (byte) (h5 >>> 32), (byte) (h5 >>> 24),
227
                       (byte) (h5 >>> 16), (byte) (h5 >>> 8), (byte) h5,
228
                       (byte) (h6 >>> 56), (byte) (h6 >>> 48),
229
                       (byte) (h6 >>> 40), (byte) (h6 >>> 32),
230
                       (byte) (h6 >>> 24), (byte) (h6 >>> 16),
231
                       (byte) (h6 >>> 8), (byte) h6, (byte) (h7 >>> 56),
232
                       (byte) (h7 >>> 48), (byte) (h7 >>> 40),
233
                       (byte) (h7 >>> 32), (byte) (h7 >>> 24),
234
                       (byte) (h7 >>> 16), (byte) (h7 >>> 8), (byte) h7 };
235
  }
193
  }
236
194
237
  protected void resetContext()
195
  protected void resetContext()
Lines 261-269 Link Here
261
    return valid.booleanValue();
219
    return valid.booleanValue();
262
  }
220
  }
263
221
264
  // SHA specific methods ----------------------------------------------------
222
  private static synchronized final long[] sha(long hh0, long hh1, long hh2,
265
266
  private static final synchronized long[] sha(long hh0, long hh1, long hh2,
267
                                               long hh3, long hh4, long hh5,
223
                                               long hh3, long hh4, long hh5,
268
                                               long hh6, long hh7, byte[] in,
224
                                               long hh6, long hh7, byte[] in,
269
                                               int offset)
225
                                               int offset)
Lines 278-310 Link Here
278
    long H = hh7;
234
    long H = hh7;
279
    long T, T2;
235
    long T, T2;
280
    int r;
236
    int r;
281
282
    for (r = 0; r < 16; r++)
237
    for (r = 0; r < 16; r++)
283
      {
238
      w[r] =  (long) in[offset++]         << 56
284
        w[r] = (long) in[offset++] << 56 | ((long) in[offset++] & 0xFF) << 48
239
           | ((long) in[offset++] & 0xFF) << 48
285
               | ((long) in[offset++] & 0xFF) << 40
240
           | ((long) in[offset++] & 0xFF) << 40
286
               | ((long) in[offset++] & 0xFF) << 32
241
           | ((long) in[offset++] & 0xFF) << 32
287
               | ((long) in[offset++] & 0xFF) << 24
242
           | ((long) in[offset++] & 0xFF) << 24
288
               | ((long) in[offset++] & 0xFF) << 16
243
           | ((long) in[offset++] & 0xFF) << 16
289
               | ((long) in[offset++] & 0xFF) << 8
244
           | ((long) in[offset++] & 0xFF) << 8
290
               | ((long) in[offset++] & 0xFF);
245
           | ((long) in[offset++] & 0xFF);
291
      }
292
    for (r = 16; r < 80; r++)
246
    for (r = 16; r < 80; r++)
293
      {
247
      {
294
        T = w[r - 2];
248
        T = w[r - 2];
295
        T2 = w[r - 15];
249
        T2 = w[r - 15];
296
        w[r] = (((T >>> 19) | (T << 45)) ^ ((T >>> 61) | (T << 3)) ^ (T >>> 6))
250
        w[r] = (((T >>> 19) | (T << 45)) ^ ((T >>> 61) | (T << 3)) ^ (T >>> 6))
297
               + w[r - 7]
251
               + w[r - 7]
298
               + (((T2 >>> 1) | (T2 << 63)) ^ ((T2 >>> 8) | (T2 << 56)) ^ (T2 >>> 7))
252
               + (((T2 >>> 1) | (T2 << 63))
253
                   ^ ((T2 >>> 8) | (T2 << 56))
254
                   ^ (T2 >>> 7))
299
               + w[r - 16];
255
               + w[r - 16];
300
      }
256
      }
301
302
    for (r = 0; r < 80; r++)
257
    for (r = 0; r < 80; r++)
303
      {
258
      {
304
        T = H
259
        T = H
305
            + (((E >>> 14) | (E << 50)) ^ ((E >>> 18) | (E << 46)) ^ ((E >>> 41) | (E << 23)))
260
            + (((E >>> 14) | (E << 50))
261
                ^ ((E >>> 18) | (E << 46))
262
                ^ ((E >>> 41) | (E << 23)))
306
            + ((E & F) ^ ((~E) & G)) + k[r] + w[r];
263
            + ((E & F) ^ ((~E) & G)) + k[r] + w[r];
307
        T2 = (((A >>> 28) | (A << 36)) ^ ((A >>> 34) | (A << 30)) ^ ((A >>> 39) | (A << 25)))
264
        T2 = (((A >>> 28) | (A << 36))
265
               ^ ((A >>> 34) | (A << 30))
266
               ^ ((A >>> 39) | (A << 25)))
308
             + ((A & B) ^ (A & C) ^ (B & C));
267
             + ((A & B) ^ (A & C) ^ (B & C));
309
        H = G;
268
        H = G;
310
        G = F;
269
        G = F;
Lines 315-322 Link Here
315
        B = A;
274
        B = A;
316
        A = T + T2;
275
        A = T + T2;
317
      }
276
      }
318
277
    return new long[] {
319
    return new long[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E, hh5 + F,
278
        hh0 + A, hh1 + B, hh2 + C, hh3 + D,
320
                       hh6 + G, hh7 + H };
279
        hh4 + E, hh5 + F, hh6 + G, hh7 + H };
321
  }
280
  }
322
}
281
}
(-)Tiger.java (-709 / +630 lines)
Lines 46-74 Link Here
46
 * Biham, with the goal of producing a secure, fast hash function that
46
 * Biham, with the goal of producing a secure, fast hash function that
47
 * performs especially well on next-generation 64-bit architectures, but
47
 * performs especially well on next-generation 64-bit architectures, but
48
 * is still efficient on 32- and 16-bit architectures.
48
 * is still efficient on 32- and 16-bit architectures.
49
 *
49
 * <p>
50
 * <p>Tiger processes data in 512-bit blocks and produces a 192-bit
50
 * Tiger processes data in 512-bit blocks and produces a 192-bit
51
 * digest.</p>
51
 * digest.
52
 *
52
 * <p>
53
 * <p>References:</p>
53
 * References:
54
 * <ol>
54
 * <ol>
55
 * <li><a
55
 * <li><a
56
 * href="http://www.cs.technion.ac.il/~biham/Reports/Tiger/">Tiger: A
56
 * href="http://www.cs.technion.ac.il/~biham/Reports/Tiger/">Tiger: A
57
 * Fast New Hash Function</a>, Ross Anderson and Eli Biham.</a></li>
57
 * Fast New Hash Function</a>, Ross Anderson and Eli Biham.</a></li>
58
 * </ol>
58
 * </ol>
59
 */
59
 */
60
public class Tiger extends BaseHash
60
public class Tiger
61
    extends BaseHash
61
{
62
{
62
63
  // Constants and variables.
64
  // -----------------------------------------------------------------------
65
66
  private static final int HASH_SIZE = 24;
63
  private static final int HASH_SIZE = 24;
67
64
68
  private static final int BLOCK_SIZE = 64;
65
  private static final int BLOCK_SIZE = 64;
69
66
70
  /** Result when no data has been input. */
67
  /** Result when no data has been input. */
71
  private static final String DIGEST0 = "3293AC630C13F0245F92BBB1766E16167A4E58492DDE73F3";
68
  private static final String DIGEST0 =
69
      "3293AC630C13F0245F92BBB1766E16167A4E58492DDE73F3";
72
70
73
  private static final long A = 0x0123456789ABCDEFL;
71
  private static final long A = 0x0123456789ABCDEFL;
74
72
Lines 77-600 Link Here
77
  private static final long C = 0xF096A5B4C3B2E187L;
75
  private static final long C = 0xF096A5B4C3B2E187L;
78
76
79
  /** S-Box T1. */
77
  /** S-Box T1. */
80
  private static final long[] T1 = { 0x02AAB17CF7E90C5EL, 0xAC424B03E243A8ECL,
78
  private static final long[] T1 = {
81
                                    0x72CD5BE30DD5FCD3L, 0x6D019B93F6F97F3AL,
79
      0x02AAB17CF7E90C5EL, 0xAC424B03E243A8ECL, 0x72CD5BE30DD5FCD3L,
82
                                    0xCD9978FFD21F9193L, 0x7573A1C9708029E2L,
80
      0x6D019B93F6F97F3AL, 0xCD9978FFD21F9193L, 0x7573A1C9708029E2L,
83
                                    0xB164326B922A83C3L, 0x46883EEE04915870L,
81
      0xB164326B922A83C3L, 0x46883EEE04915870L, 0xEAACE3057103ECE6L,
84
                                    0xEAACE3057103ECE6L, 0xC54169B808A3535CL,
82
      0xC54169B808A3535CL, 0x4CE754918DDEC47CL, 0x0AA2F4DFDC0DF40CL,
85
                                    0x4CE754918DDEC47CL, 0x0AA2F4DFDC0DF40CL,
83
      0x10B76F18A74DBEFAL, 0xC6CCB6235AD1AB6AL, 0x13726121572FE2FFL,
86
                                    0x10B76F18A74DBEFAL, 0xC6CCB6235AD1AB6AL,
84
      0x1A488C6F199D921EL, 0x4BC9F9F4DA0007CAL, 0x26F5E6F6E85241C7L,
87
                                    0x13726121572FE2FFL, 0x1A488C6F199D921EL,
85
      0x859079DBEA5947B6L, 0x4F1885C5C99E8C92L, 0xD78E761EA96F864BL,
88
                                    0x4BC9F9F4DA0007CAL, 0x26F5E6F6E85241C7L,
86
      0x8E36428C52B5C17DL, 0x69CF6827373063C1L, 0xB607C93D9BB4C56EL,
89
                                    0x859079DBEA5947B6L, 0x4F1885C5C99E8C92L,
87
      0x7D820E760E76B5EAL, 0x645C9CC6F07FDC42L, 0xBF38A078243342E0L,
90
                                    0xD78E761EA96F864BL, 0x8E36428C52B5C17DL,
88
      0x5F6B343C9D2E7D04L, 0xF2C28AEB600B0EC6L, 0x6C0ED85F7254BCACL,
91
                                    0x69CF6827373063C1L, 0xB607C93D9BB4C56EL,
89
      0x71592281A4DB4FE5L, 0x1967FA69CE0FED9FL, 0xFD5293F8B96545DBL,
92
                                    0x7D820E760E76B5EAL, 0x645C9CC6F07FDC42L,
90
      0xC879E9D7F2A7600BL, 0x860248920193194EL, 0xA4F9533B2D9CC0B3L,
93
                                    0xBF38A078243342E0L, 0x5F6B343C9D2E7D04L,
91
      0x9053836C15957613L, 0xDB6DCF8AFC357BF1L, 0x18BEEA7A7A370F57L,
94
                                    0xF2C28AEB600B0EC6L, 0x6C0ED85F7254BCACL,
92
      0x037117CA50B99066L, 0x6AB30A9774424A35L, 0xF4E92F02E325249BL,
95
                                    0x71592281A4DB4FE5L, 0x1967FA69CE0FED9FL,
93
      0x7739DB07061CCAE1L, 0xD8F3B49CECA42A05L, 0xBD56BE3F51382F73L,
96
                                    0xFD5293F8B96545DBL, 0xC879E9D7F2A7600BL,
94
      0x45FAED5843B0BB28L, 0x1C813D5C11BF1F83L, 0x8AF0E4B6D75FA169L,
97
                                    0x860248920193194EL, 0xA4F9533B2D9CC0B3L,
95
      0x33EE18A487AD9999L, 0x3C26E8EAB1C94410L, 0xB510102BC0A822F9L,
98
                                    0x9053836C15957613L, 0xDB6DCF8AFC357BF1L,
96
      0x141EEF310CE6123BL, 0xFC65B90059DDB154L, 0xE0158640C5E0E607L,
99
                                    0x18BEEA7A7A370F57L, 0x037117CA50B99066L,
97
      0x884E079826C3A3CFL, 0x930D0D9523C535FDL, 0x35638D754E9A2B00L,
100
                                    0x6AB30A9774424A35L, 0xF4E92F02E325249BL,
98
      0x4085FCCF40469DD5L, 0xC4B17AD28BE23A4CL, 0xCAB2F0FC6A3E6A2EL,
101
                                    0x7739DB07061CCAE1L, 0xD8F3B49CECA42A05L,
99
      0x2860971A6B943FCDL, 0x3DDE6EE212E30446L, 0x6222F32AE01765AEL,
102
                                    0xBD56BE3F51382F73L, 0x45FAED5843B0BB28L,
100
      0x5D550BB5478308FEL, 0xA9EFA98DA0EDA22AL, 0xC351A71686C40DA7L,
103
                                    0x1C813D5C11BF1F83L, 0x8AF0E4B6D75FA169L,
101
      0x1105586D9C867C84L, 0xDCFFEE85FDA22853L, 0xCCFBD0262C5EEF76L,
104
                                    0x33EE18A487AD9999L, 0x3C26E8EAB1C94410L,
102
      0xBAF294CB8990D201L, 0xE69464F52AFAD975L, 0x94B013AFDF133E14L,
105
                                    0xB510102BC0A822F9L, 0x141EEF310CE6123BL,
103
      0x06A7D1A32823C958L, 0x6F95FE5130F61119L, 0xD92AB34E462C06C0L,
106
                                    0xFC65B90059DDB154L, 0xE0158640C5E0E607L,
104
      0xED7BDE33887C71D2L, 0x79746D6E6518393EL, 0x5BA419385D713329L,
107
                                    0x884E079826C3A3CFL, 0x930D0D9523C535FDL,
105
      0x7C1BA6B948A97564L, 0x31987C197BFDAC67L, 0xDE6C23C44B053D02L,
108
                                    0x35638D754E9A2B00L, 0x4085FCCF40469DD5L,
106
      0x581C49FED002D64DL, 0xDD474D6338261571L, 0xAA4546C3E473D062L,
109
                                    0xC4B17AD28BE23A4CL, 0xCAB2F0FC6A3E6A2EL,
107
      0x928FCE349455F860L, 0x48161BBACAAB94D9L, 0x63912430770E6F68L,
110
                                    0x2860971A6B943FCDL, 0x3DDE6EE212E30446L,
108
      0x6EC8A5E602C6641CL, 0x87282515337DDD2BL, 0x2CDA6B42034B701BL,
111
                                    0x6222F32AE01765AEL, 0x5D550BB5478308FEL,
109
      0xB03D37C181CB096DL, 0xE108438266C71C6FL, 0x2B3180C7EB51B255L,
112
                                    0xA9EFA98DA0EDA22AL, 0xC351A71686C40DA7L,
110
      0xDF92B82F96C08BBCL, 0x5C68C8C0A632F3BAL, 0x5504CC861C3D0556L,
113
                                    0x1105586D9C867C84L, 0xDCFFEE85FDA22853L,
111
      0xABBFA4E55FB26B8FL, 0x41848B0AB3BACEB4L, 0xB334A273AA445D32L,
114
                                    0xCCFBD0262C5EEF76L, 0xBAF294CB8990D201L,
112
      0xBCA696F0A85AD881L, 0x24F6EC65B528D56CL, 0x0CE1512E90F4524AL,
115
                                    0xE69464F52AFAD975L, 0x94B013AFDF133E14L,
113
      0x4E9DD79D5506D35AL, 0x258905FAC6CE9779L, 0x2019295B3E109B33L,
116
                                    0x06A7D1A32823C958L, 0x6F95FE5130F61119L,
114
      0xF8A9478B73A054CCL, 0x2924F2F934417EB0L, 0x3993357D536D1BC4L,
117
                                    0xD92AB34E462C06C0L, 0xED7BDE33887C71D2L,
115
      0x38A81AC21DB6FF8BL, 0x47C4FBF17D6016BFL, 0x1E0FAADD7667E3F5L,
118
                                    0x79746D6E6518393EL, 0x5BA419385D713329L,
116
      0x7ABCFF62938BEB96L, 0xA78DAD948FC179C9L, 0x8F1F98B72911E50DL,
119
                                    0x7C1BA6B948A97564L, 0x31987C197BFDAC67L,
117
      0x61E48EAE27121A91L, 0x4D62F7AD31859808L, 0xECEBA345EF5CEAEBL,
120
                                    0xDE6C23C44B053D02L, 0x581C49FED002D64DL,
118
      0xF5CEB25EBC9684CEL, 0xF633E20CB7F76221L, 0xA32CDF06AB8293E4L,
121
                                    0xDD474D6338261571L, 0xAA4546C3E473D062L,
119
      0x985A202CA5EE2CA4L, 0xCF0B8447CC8A8FB1L, 0x9F765244979859A3L,
122
                                    0x928FCE349455F860L, 0x48161BBACAAB94D9L,
120
      0xA8D516B1A1240017L, 0x0BD7BA3EBB5DC726L, 0xE54BCA55B86ADB39L,
123
                                    0x63912430770E6F68L, 0x6EC8A5E602C6641CL,
121
      0x1D7A3AFD6C478063L, 0x519EC608E7669EDDL, 0x0E5715A2D149AA23L,
124
                                    0x87282515337DDD2BL, 0x2CDA6B42034B701BL,
122
      0x177D4571848FF194L, 0xEEB55F3241014C22L, 0x0F5E5CA13A6E2EC2L,
125
                                    0xB03D37C181CB096DL, 0xE108438266C71C6FL,
123
      0x8029927B75F5C361L, 0xAD139FABC3D6E436L, 0x0D5DF1A94CCF402FL,
126
                                    0x2B3180C7EB51B255L, 0xDF92B82F96C08BBCL,
124
      0x3E8BD948BEA5DFC8L, 0xA5A0D357BD3FF77EL, 0xA2D12E251F74F645L,
127
                                    0x5C68C8C0A632F3BAL, 0x5504CC861C3D0556L,
125
      0x66FD9E525E81A082L, 0x2E0C90CE7F687A49L, 0xC2E8BCBEBA973BC5L,
128
                                    0xABBFA4E55FB26B8FL, 0x41848B0AB3BACEB4L,
126
      0x000001BCE509745FL, 0x423777BBE6DAB3D6L, 0xD1661C7EAEF06EB5L,
129
                                    0xB334A273AA445D32L, 0xBCA696F0A85AD881L,
127
      0xA1781F354DAACFD8L, 0x2D11284A2B16AFFCL, 0xF1FC4F67FA891D1FL,
130
                                    0x24F6EC65B528D56CL, 0x0CE1512E90F4524AL,
128
      0x73ECC25DCB920ADAL, 0xAE610C22C2A12651L, 0x96E0A810D356B78AL,
131
                                    0x4E9DD79D5506D35AL, 0x258905FAC6CE9779L,
129
      0x5A9A381F2FE7870FL, 0xD5AD62EDE94E5530L, 0xD225E5E8368D1427L,
132
                                    0x2019295B3E109B33L, 0xF8A9478B73A054CCL,
130
      0x65977B70C7AF4631L, 0x99F889B2DE39D74FL, 0x233F30BF54E1D143L,
133
                                    0x2924F2F934417EB0L, 0x3993357D536D1BC4L,
131
      0x9A9675D3D9A63C97L, 0x5470554FF334F9A8L, 0x166ACB744A4F5688L,
134
                                    0x38A81AC21DB6FF8BL, 0x47C4FBF17D6016BFL,
132
      0x70C74CAAB2E4AEADL, 0xF0D091646F294D12L, 0x57B82A89684031D1L,
135
                                    0x1E0FAADD7667E3F5L, 0x7ABCFF62938BEB96L,
133
      0xEFD95A5A61BE0B6BL, 0x2FBD12E969F2F29AL, 0x9BD37013FEFF9FE8L,
136
                                    0xA78DAD948FC179C9L, 0x8F1F98B72911E50DL,
134
      0x3F9B0404D6085A06L, 0x4940C1F3166CFE15L, 0x09542C4DCDF3DEFBL,
137
                                    0x61E48EAE27121A91L, 0x4D62F7AD31859808L,
135
      0xB4C5218385CD5CE3L, 0xC935B7DC4462A641L, 0x3417F8A68ED3B63FL,
138
                                    0xECEBA345EF5CEAEBL, 0xF5CEB25EBC9684CEL,
136
      0xB80959295B215B40L, 0xF99CDAEF3B8C8572L, 0x018C0614F8FCB95DL,
139
                                    0xF633E20CB7F76221L, 0xA32CDF06AB8293E4L,
137
      0x1B14ACCD1A3ACDF3L, 0x84D471F200BB732DL, 0xC1A3110E95E8DA16L,
140
                                    0x985A202CA5EE2CA4L, 0xCF0B8447CC8A8FB1L,
138
      0x430A7220BF1A82B8L, 0xB77E090D39DF210EL, 0x5EF4BD9F3CD05E9DL,
141
                                    0x9F765244979859A3L, 0xA8D516B1A1240017L,
139
      0x9D4FF6DA7E57A444L, 0xDA1D60E183D4A5F8L, 0xB287C38417998E47L,
142
                                    0x0BD7BA3EBB5DC726L, 0xE54BCA55B86ADB39L,
140
      0xFE3EDC121BB31886L, 0xC7FE3CCC980CCBEFL, 0xE46FB590189BFD03L,
143
                                    0x1D7A3AFD6C478063L, 0x519EC608E7669EDDL,
141
      0x3732FD469A4C57DCL, 0x7EF700A07CF1AD65L, 0x59C64468A31D8859L,
144
                                    0x0E5715A2D149AA23L, 0x177D4571848FF194L,
142
      0x762FB0B4D45B61F6L, 0x155BAED099047718L, 0x68755E4C3D50BAA6L,
145
                                    0xEEB55F3241014C22L, 0x0F5E5CA13A6E2EC2L,
143
      0xE9214E7F22D8B4DFL, 0x2ADDBF532EAC95F4L, 0x32AE3909B4BD0109L,
146
                                    0x8029927B75F5C361L, 0xAD139FABC3D6E436L,
144
      0x834DF537B08E3450L, 0xFA209DA84220728DL, 0x9E691D9B9EFE23F7L,
147
                                    0x0D5DF1A94CCF402FL, 0x3E8BD948BEA5DFC8L,
145
      0x0446D288C4AE8D7FL, 0x7B4CC524E169785BL, 0x21D87F0135CA1385L,
148
                                    0xA5A0D357BD3FF77EL, 0xA2D12E251F74F645L,
146
      0xCEBB400F137B8AA5L, 0x272E2B66580796BEL, 0x3612264125C2B0DEL,
149
                                    0x66FD9E525E81A082L, 0x2E0C90CE7F687A49L,
147
      0x057702BDAD1EFBB2L, 0xD4BABB8EACF84BE9L, 0x91583139641BC67BL,
150
                                    0xC2E8BCBEBA973BC5L, 0x000001BCE509745FL,
148
      0x8BDC2DE08036E024L, 0x603C8156F49F68EDL, 0xF7D236F7DBEF5111L,
151
                                    0x423777BBE6DAB3D6L, 0xD1661C7EAEF06EB5L,
149
      0x9727C4598AD21E80L, 0xA08A0896670A5FD7L, 0xCB4A8F4309EBA9CBL,
152
                                    0xA1781F354DAACFD8L, 0x2D11284A2B16AFFCL,
150
      0x81AF564B0F7036A1L, 0xC0B99AA778199ABDL, 0x959F1EC83FC8E952L,
153
                                    0xF1FC4F67FA891D1FL, 0x73ECC25DCB920ADAL,
151
      0x8C505077794A81B9L, 0x3ACAAF8F056338F0L, 0x07B43F50627A6778L,
154
                                    0xAE610C22C2A12651L, 0x96E0A810D356B78AL,
152
      0x4A44AB49F5ECCC77L, 0x3BC3D6E4B679EE98L, 0x9CC0D4D1CF14108CL,
155
                                    0x5A9A381F2FE7870FL, 0xD5AD62EDE94E5530L,
153
      0x4406C00B206BC8A0L, 0x82A18854C8D72D89L, 0x67E366B35C3C432CL,
156
                                    0xD225E5E8368D1427L, 0x65977B70C7AF4631L,
154
      0xB923DD61102B37F2L, 0x56AB2779D884271DL, 0xBE83E1B0FF1525AFL,
157
                                    0x99F889B2DE39D74FL, 0x233F30BF54E1D143L,
155
      0xFB7C65D4217E49A9L, 0x6BDBE0E76D48E7D4L, 0x08DF828745D9179EL,
158
                                    0x9A9675D3D9A63C97L, 0x5470554FF334F9A8L,
156
      0x22EA6A9ADD53BD34L, 0xE36E141C5622200AL, 0x7F805D1B8CB750EEL,
159
                                    0x166ACB744A4F5688L, 0x70C74CAAB2E4AEADL,
157
      0xAFE5C7A59F58E837L, 0xE27F996A4FB1C23CL, 0xD3867DFB0775F0D0L,
160
                                    0xF0D091646F294D12L, 0x57B82A89684031D1L,
158
      0xD0E673DE6E88891AL, 0x123AEB9EAFB86C25L, 0x30F1D5D5C145B895L,
161
                                    0xEFD95A5A61BE0B6BL, 0x2FBD12E969F2F29AL,
159
      0xBB434A2DEE7269E7L, 0x78CB67ECF931FA38L, 0xF33B0372323BBF9CL,
162
                                    0x9BD37013FEFF9FE8L, 0x3F9B0404D6085A06L,
160
      0x52D66336FB279C74L, 0x505F33AC0AFB4EAAL, 0xE8A5CD99A2CCE187L,
163
                                    0x4940C1F3166CFE15L, 0x09542C4DCDF3DEFBL,
161
      0x534974801E2D30BBL, 0x8D2D5711D5876D90L, 0x1F1A412891BC038EL,
164
                                    0xB4C5218385CD5CE3L, 0xC935B7DC4462A641L,
162
      0xD6E2E71D82E56648L, 0x74036C3A497732B7L, 0x89B67ED96361F5ABL,
165
                                    0x3417F8A68ED3B63FL, 0xB80959295B215B40L,
163
      0xFFED95D8F1EA02A2L, 0xE72B3BD61464D43DL, 0xA6300F170BDC4820L,
166
                                    0xF99CDAEF3B8C8572L, 0x018C0614F8FCB95DL,
164
      0xEBC18760ED78A77AL };
167
                                    0x1B14ACCD1A3ACDF3L, 0x84D471F200BB732DL,
168
                                    0xC1A3110E95E8DA16L, 0x430A7220BF1A82B8L,
169
                                    0xB77E090D39DF210EL, 0x5EF4BD9F3CD05E9DL,
170
                                    0x9D4FF6DA7E57A444L, 0xDA1D60E183D4A5F8L,
171
                                    0xB287C38417998E47L, 0xFE3EDC121BB31886L,
172
                                    0xC7FE3CCC980CCBEFL, 0xE46FB590189BFD03L,
173
                                    0x3732FD469A4C57DCL, 0x7EF700A07CF1AD65L,
174
                                    0x59C64468A31D8859L, 0x762FB0B4D45B61F6L,
175
                                    0x155BAED099047718L, 0x68755E4C3D50BAA6L,
176
                                    0xE9214E7F22D8B4DFL, 0x2ADDBF532EAC95F4L,
177
                                    0x32AE3909B4BD0109L, 0x834DF537B08E3450L,
178
                                    0xFA209DA84220728DL, 0x9E691D9B9EFE23F7L,
179
                                    0x0446D288C4AE8D7FL, 0x7B4CC524E169785BL,
180
                                    0x21D87F0135CA1385L, 0xCEBB400F137B8AA5L,
181
                                    0x272E2B66580796BEL, 0x3612264125C2B0DEL,
182
                                    0x057702BDAD1EFBB2L, 0xD4BABB8EACF84BE9L,
183
                                    0x91583139641BC67BL, 0x8BDC2DE08036E024L,
184
                                    0x603C8156F49F68EDL, 0xF7D236F7DBEF5111L,
185
                                    0x9727C4598AD21E80L, 0xA08A0896670A5FD7L,
186
                                    0xCB4A8F4309EBA9CBL, 0x81AF564B0F7036A1L,
187
                                    0xC0B99AA778199ABDL, 0x959F1EC83FC8E952L,
188
                                    0x8C505077794A81B9L, 0x3ACAAF8F056338F0L,
189
                                    0x07B43F50627A6778L, 0x4A44AB49F5ECCC77L,
190
                                    0x3BC3D6E4B679EE98L, 0x9CC0D4D1CF14108CL,
191
                                    0x4406C00B206BC8A0L, 0x82A18854C8D72D89L,
192
                                    0x67E366B35C3C432CL, 0xB923DD61102B37F2L,
193
                                    0x56AB2779D884271DL, 0xBE83E1B0FF1525AFL,
194
                                    0xFB7C65D4217E49A9L, 0x6BDBE0E76D48E7D4L,
195
                                    0x08DF828745D9179EL, 0x22EA6A9ADD53BD34L,
196
                                    0xE36E141C5622200AL, 0x7F805D1B8CB750EEL,
197
                                    0xAFE5C7A59F58E837L, 0xE27F996A4FB1C23CL,
198
                                    0xD3867DFB0775F0D0L, 0xD0E673DE6E88891AL,
199
                                    0x123AEB9EAFB86C25L, 0x30F1D5D5C145B895L,
200
                                    0xBB434A2DEE7269E7L, 0x78CB67ECF931FA38L,
201
                                    0xF33B0372323BBF9CL, 0x52D66336FB279C74L,
202
                                    0x505F33AC0AFB4EAAL, 0xE8A5CD99A2CCE187L,
203
                                    0x534974801E2D30BBL, 0x8D2D5711D5876D90L,
204
                                    0x1F1A412891BC038EL, 0xD6E2E71D82E56648L,
205
                                    0x74036C3A497732B7L, 0x89B67ED96361F5ABL,
206
                                    0xFFED95D8F1EA02A2L, 0xE72B3BD61464D43DL,
207
                                    0xA6300F170BDC4820L, 0xEBC18760ED78A77AL };
208
165
209
  /** S-Box T2. */
166
  /** S-Box T2. */
210
  private static final long[] T2 = { 0xE6A6BE5A05A12138L, 0xB5A122A5B4F87C98L,
167
  private static final long[] T2 = {
211
                                    0x563C6089140B6990L, 0x4C46CB2E391F5DD5L,
168
      0xE6A6BE5A05A12138L, 0xB5A122A5B4F87C98L, 0x563C6089140B6990L,
212
                                    0xD932ADDBC9B79434L, 0x08EA70E42015AFF5L,
169
      0x4C46CB2E391F5DD5L, 0xD932ADDBC9B79434L, 0x08EA70E42015AFF5L,
213
                                    0xD765A6673E478CF1L, 0xC4FB757EAB278D99L,
170
      0xD765A6673E478CF1L, 0xC4FB757EAB278D99L, 0xDF11C6862D6E0692L,
214
                                    0xDF11C6862D6E0692L, 0xDDEB84F10D7F3B16L,
171
      0xDDEB84F10D7F3B16L, 0x6F2EF604A665EA04L, 0x4A8E0F0FF0E0DFB3L,
215
                                    0x6F2EF604A665EA04L, 0x4A8E0F0FF0E0DFB3L,
172
      0xA5EDEEF83DBCBA51L, 0xFC4F0A2A0EA4371EL, 0xE83E1DA85CB38429L,
216
                                    0xA5EDEEF83DBCBA51L, 0xFC4F0A2A0EA4371EL,
173
      0xDC8FF882BA1B1CE2L, 0xCD45505E8353E80DL, 0x18D19A00D4DB0717L,
217
                                    0xE83E1DA85CB38429L, 0xDC8FF882BA1B1CE2L,
174
      0x34A0CFEDA5F38101L, 0x0BE77E518887CAF2L, 0x1E341438B3C45136L,
218
                                    0xCD45505E8353E80DL, 0x18D19A00D4DB0717L,
175
      0xE05797F49089CCF9L, 0xFFD23F9DF2591D14L, 0x543DDA228595C5CDL,
219
                                    0x34A0CFEDA5F38101L, 0x0BE77E518887CAF2L,
176
      0x661F81FD99052A33L, 0x8736E641DB0F7B76L, 0x15227725418E5307L,
220
                                    0x1E341438B3C45136L, 0xE05797F49089CCF9L,
177
      0xE25F7F46162EB2FAL, 0x48A8B2126C13D9FEL, 0xAFDC541792E76EEAL,
221
                                    0xFFD23F9DF2591D14L, 0x543DDA228595C5CDL,
178
      0x03D912BFC6D1898FL, 0x31B1AAFA1B83F51BL, 0xF1AC2796E42AB7D9L,
222
                                    0x661F81FD99052A33L, 0x8736E641DB0F7B76L,
179
      0x40A3A7D7FCD2EBACL, 0x1056136D0AFBBCC5L, 0x7889E1DD9A6D0C85L,
223
                                    0x15227725418E5307L, 0xE25F7F46162EB2FAL,
180
      0xD33525782A7974AAL, 0xA7E25D09078AC09BL, 0xBD4138B3EAC6EDD0L,
224
                                    0x48A8B2126C13D9FEL, 0xAFDC541792E76EEAL,
181
      0x920ABFBE71EB9E70L, 0xA2A5D0F54FC2625CL, 0xC054E36B0B1290A3L,
225
                                    0x03D912BFC6D1898FL, 0x31B1AAFA1B83F51BL,
182
      0xF6DD59FF62FE932BL, 0x3537354511A8AC7DL, 0xCA845E9172FADCD4L,
226
                                    0xF1AC2796E42AB7D9L, 0x40A3A7D7FCD2EBACL,
183
      0x84F82B60329D20DCL, 0x79C62CE1CD672F18L, 0x8B09A2ADD124642CL,
227
                                    0x1056136D0AFBBCC5L, 0x7889E1DD9A6D0C85L,
184
      0xD0C1E96A19D9E726L, 0x5A786A9B4BA9500CL, 0x0E020336634C43F3L,
228
                                    0xD33525782A7974AAL, 0xA7E25D09078AC09BL,
185
      0xC17B474AEB66D822L, 0x6A731AE3EC9BAAC2L, 0x8226667AE0840258L,
229
                                    0xBD4138B3EAC6EDD0L, 0x920ABFBE71EB9E70L,
186
      0x67D4567691CAECA5L, 0x1D94155C4875ADB5L, 0x6D00FD985B813FDFL,
230
                                    0xA2A5D0F54FC2625CL, 0xC054E36B0B1290A3L,
187
      0x51286EFCB774CD06L, 0x5E8834471FA744AFL, 0xF72CA0AEE761AE2EL,
231
                                    0xF6DD59FF62FE932BL, 0x3537354511A8AC7DL,
188
      0xBE40E4CDAEE8E09AL, 0xE9970BBB5118F665L, 0x726E4BEB33DF1964L,
232
                                    0xCA845E9172FADCD4L, 0x84F82B60329D20DCL,
189
      0x703B000729199762L, 0x4631D816F5EF30A7L, 0xB880B5B51504A6BEL,
233
                                    0x79C62CE1CD672F18L, 0x8B09A2ADD124642CL,
190
      0x641793C37ED84B6CL, 0x7B21ED77F6E97D96L, 0x776306312EF96B73L,
234
                                    0xD0C1E96A19D9E726L, 0x5A786A9B4BA9500CL,
191
      0xAE528948E86FF3F4L, 0x53DBD7F286A3F8F8L, 0x16CADCE74CFC1063L,
235
                                    0x0E020336634C43F3L, 0xC17B474AEB66D822L,
192
      0x005C19BDFA52C6DDL, 0x68868F5D64D46AD3L, 0x3A9D512CCF1E186AL,
236
                                    0x6A731AE3EC9BAAC2L, 0x8226667AE0840258L,
193
      0x367E62C2385660AEL, 0xE359E7EA77DCB1D7L, 0x526C0773749ABE6EL,
237
                                    0x67D4567691CAECA5L, 0x1D94155C4875ADB5L,
194
      0x735AE5F9D09F734BL, 0x493FC7CC8A558BA8L, 0xB0B9C1533041AB45L,
238
                                    0x6D00FD985B813FDFL, 0x51286EFCB774CD06L,
195
      0x321958BA470A59BDL, 0x852DB00B5F46C393L, 0x91209B2BD336B0E5L,
239
                                    0x5E8834471FA744AFL, 0xF72CA0AEE761AE2EL,
196
      0x6E604F7D659EF19FL, 0xB99A8AE2782CCB24L, 0xCCF52AB6C814C4C7L,
240
                                    0xBE40E4CDAEE8E09AL, 0xE9970BBB5118F665L,
197
      0x4727D9AFBE11727BL, 0x7E950D0C0121B34DL, 0x756F435670AD471FL,
241
                                    0x726E4BEB33DF1964L, 0x703B000729199762L,
198
      0xF5ADD442615A6849L, 0x4E87E09980B9957AL, 0x2ACFA1DF50AEE355L,
242
                                    0x4631D816F5EF30A7L, 0xB880B5B51504A6BEL,
199
      0xD898263AFD2FD556L, 0xC8F4924DD80C8FD6L, 0xCF99CA3D754A173AL,
243
                                    0x641793C37ED84B6CL, 0x7B21ED77F6E97D96L,
200
      0xFE477BACAF91BF3CL, 0xED5371F6D690C12DL, 0x831A5C285E687094L,
244
                                    0x776306312EF96B73L, 0xAE528948E86FF3F4L,
201
      0xC5D3C90A3708A0A4L, 0x0F7F903717D06580L, 0x19F9BB13B8FDF27FL,
245
                                    0x53DBD7F286A3F8F8L, 0x16CADCE74CFC1063L,
202
      0xB1BD6F1B4D502843L, 0x1C761BA38FFF4012L, 0x0D1530C4E2E21F3BL,
246
                                    0x005C19BDFA52C6DDL, 0x68868F5D64D46AD3L,
203
      0x8943CE69A7372C8AL, 0xE5184E11FEB5CE66L, 0x618BDB80BD736621L,
247
                                    0x3A9D512CCF1E186AL, 0x367E62C2385660AEL,
204
      0x7D29BAD68B574D0BL, 0x81BB613E25E6FE5BL, 0x071C9C10BC07913FL,
248
                                    0xE359E7EA77DCB1D7L, 0x526C0773749ABE6EL,
205
      0xC7BEEB7909AC2D97L, 0xC3E58D353BC5D757L, 0xEB017892F38F61E8L,
249
                                    0x735AE5F9D09F734BL, 0x493FC7CC8A558BA8L,
206
      0xD4EFFB9C9B1CC21AL, 0x99727D26F494F7ABL, 0xA3E063A2956B3E03L,
250
                                    0xB0B9C1533041AB45L, 0x321958BA470A59BDL,
207
      0x9D4A8B9A4AA09C30L, 0x3F6AB7D500090FB4L, 0x9CC0F2A057268AC0L,
251
                                    0x852DB00B5F46C393L, 0x91209B2BD336B0E5L,
208
      0x3DEE9D2DEDBF42D1L, 0x330F49C87960A972L, 0xC6B2720287421B41L,
252
                                    0x6E604F7D659EF19FL, 0xB99A8AE2782CCB24L,
209
      0x0AC59EC07C00369CL, 0xEF4EAC49CB353425L, 0xF450244EEF0129D8L,
253
                                    0xCCF52AB6C814C4C7L, 0x4727D9AFBE11727BL,
210
      0x8ACC46E5CAF4DEB6L, 0x2FFEAB63989263F7L, 0x8F7CB9FE5D7A4578L,
254
                                    0x7E950D0C0121B34DL, 0x756F435670AD471FL,
211
      0x5BD8F7644E634635L, 0x427A7315BF2DC900L, 0x17D0C4AA2125261CL,
255
                                    0xF5ADD442615A6849L, 0x4E87E09980B9957AL,
212
      0x3992486C93518E50L, 0xB4CBFEE0A2D7D4C3L, 0x7C75D6202C5DDD8DL,
256
                                    0x2ACFA1DF50AEE355L, 0xD898263AFD2FD556L,
213
      0xDBC295D8E35B6C61L, 0x60B369D302032B19L, 0xCE42685FDCE44132L,
257
                                    0xC8F4924DD80C8FD6L, 0xCF99CA3D754A173AL,
214
      0x06F3DDB9DDF65610L, 0x8EA4D21DB5E148F0L, 0x20B0FCE62FCD496FL,
258
                                    0xFE477BACAF91BF3CL, 0xED5371F6D690C12DL,
215
      0x2C1B912358B0EE31L, 0xB28317B818F5A308L, 0xA89C1E189CA6D2CFL,
259
                                    0x831A5C285E687094L, 0xC5D3C90A3708A0A4L,
216
      0x0C6B18576AAADBC8L, 0xB65DEAA91299FAE3L, 0xFB2B794B7F1027E7L,
260
                                    0x0F7F903717D06580L, 0x19F9BB13B8FDF27FL,
217
      0x04E4317F443B5BEBL, 0x4B852D325939D0A6L, 0xD5AE6BEEFB207FFCL,
261
                                    0xB1BD6F1B4D502843L, 0x1C761BA38FFF4012L,
218
      0x309682B281C7D374L, 0xBAE309A194C3B475L, 0x8CC3F97B13B49F05L,
262
                                    0x0D1530C4E2E21F3BL, 0x8943CE69A7372C8AL,
219
      0x98A9422FF8293967L, 0x244B16B01076FF7CL, 0xF8BF571C663D67EEL,
263
                                    0xE5184E11FEB5CE66L, 0x618BDB80BD736621L,
220
      0x1F0D6758EEE30DA1L, 0xC9B611D97ADEB9B7L, 0xB7AFD5887B6C57A2L,
264
                                    0x7D29BAD68B574D0BL, 0x81BB613E25E6FE5BL,
221
      0x6290AE846B984FE1L, 0x94DF4CDEACC1A5FDL, 0x058A5BD1C5483AFFL,
265
                                    0x071C9C10BC07913FL, 0xC7BEEB7909AC2D97L,
222
      0x63166CC142BA3C37L, 0x8DB8526EB2F76F40L, 0xE10880036F0D6D4EL,
266
                                    0xC3E58D353BC5D757L, 0xEB017892F38F61E8L,
223
      0x9E0523C9971D311DL, 0x45EC2824CC7CD691L, 0x575B8359E62382C9L,
267
                                    0xD4EFFB9C9B1CC21AL, 0x99727D26F494F7ABL,
224
      0xFA9E400DC4889995L, 0xD1823ECB45721568L, 0xDAFD983B8206082FL,
268
                                    0xA3E063A2956B3E03L, 0x9D4A8B9A4AA09C30L,
225
      0xAA7D29082386A8CBL, 0x269FCD4403B87588L, 0x1B91F5F728BDD1E0L,
269
                                    0x3F6AB7D500090FB4L, 0x9CC0F2A057268AC0L,
226
      0xE4669F39040201F6L, 0x7A1D7C218CF04ADEL, 0x65623C29D79CE5CEL,
270
                                    0x3DEE9D2DEDBF42D1L, 0x330F49C87960A972L,
227
      0x2368449096C00BB1L, 0xAB9BF1879DA503BAL, 0xBC23ECB1A458058EL,
271
                                    0xC6B2720287421B41L, 0x0AC59EC07C00369CL,
228
      0x9A58DF01BB401ECCL, 0xA070E868A85F143DL, 0x4FF188307DF2239EL,
272
                                    0xEF4EAC49CB353425L, 0xF450244EEF0129D8L,
229
      0x14D565B41A641183L, 0xEE13337452701602L, 0x950E3DCF3F285E09L,
273
                                    0x8ACC46E5CAF4DEB6L, 0x2FFEAB63989263F7L,
230
      0x59930254B9C80953L, 0x3BF299408930DA6DL, 0xA955943F53691387L,
274
                                    0x8F7CB9FE5D7A4578L, 0x5BD8F7644E634635L,
231
      0xA15EDECAA9CB8784L, 0x29142127352BE9A0L, 0x76F0371FFF4E7AFBL,
275
                                    0x427A7315BF2DC900L, 0x17D0C4AA2125261CL,
232
      0x0239F450274F2228L, 0xBB073AF01D5E868BL, 0xBFC80571C10E96C1L,
276
                                    0x3992486C93518E50L, 0xB4CBFEE0A2D7D4C3L,
233
      0xD267088568222E23L, 0x9671A3D48E80B5B0L, 0x55B5D38AE193BB81L,
277
                                    0x7C75D6202C5DDD8DL, 0xDBC295D8E35B6C61L,
234
      0x693AE2D0A18B04B8L, 0x5C48B4ECADD5335FL, 0xFD743B194916A1CAL,
278
                                    0x60B369D302032B19L, 0xCE42685FDCE44132L,
235
      0x2577018134BE98C4L, 0xE77987E83C54A4ADL, 0x28E11014DA33E1B9L,
279
                                    0x06F3DDB9DDF65610L, 0x8EA4D21DB5E148F0L,
236
      0x270CC59E226AA213L, 0x71495F756D1A5F60L, 0x9BE853FB60AFEF77L,
280
                                    0x20B0FCE62FCD496FL, 0x2C1B912358B0EE31L,
237
      0xADC786A7F7443DBFL, 0x0904456173B29A82L, 0x58BC7A66C232BD5EL,
281
                                    0xB28317B818F5A308L, 0xA89C1E189CA6D2CFL,
238
      0xF306558C673AC8B2L, 0x41F639C6B6C9772AL, 0x216DEFE99FDA35DAL,
282
                                    0x0C6B18576AAADBC8L, 0xB65DEAA91299FAE3L,
239
      0x11640CC71C7BE615L, 0x93C43694565C5527L, 0xEA038E6246777839L,
283
                                    0xFB2B794B7F1027E7L, 0x04E4317F443B5BEBL,
240
      0xF9ABF3CE5A3E2469L, 0x741E768D0FD312D2L, 0x0144B883CED652C6L,
284
                                    0x4B852D325939D0A6L, 0xD5AE6BEEFB207FFCL,
241
      0xC20B5A5BA33F8552L, 0x1AE69633C3435A9DL, 0x97A28CA4088CFDECL,
285
                                    0x309682B281C7D374L, 0xBAE309A194C3B475L,
242
      0x8824A43C1E96F420L, 0x37612FA66EEEA746L, 0x6B4CB165F9CF0E5AL,
286
                                    0x8CC3F97B13B49F05L, 0x98A9422FF8293967L,
243
      0x43AA1C06A0ABFB4AL, 0x7F4DC26FF162796BL, 0x6CBACC8E54ED9B0FL,
287
                                    0x244B16B01076FF7CL, 0xF8BF571C663D67EEL,
244
      0xA6B7FFEFD2BB253EL, 0x2E25BC95B0A29D4FL, 0x86D6A58BDEF1388CL,
288
                                    0x1F0D6758EEE30DA1L, 0xC9B611D97ADEB9B7L,
245
      0xDED74AC576B6F054L, 0x8030BDBC2B45805DL, 0x3C81AF70E94D9289L,
289
                                    0xB7AFD5887B6C57A2L, 0x6290AE846B984FE1L,
246
      0x3EFF6DDA9E3100DBL, 0xB38DC39FDFCC8847L, 0x123885528D17B87EL,
290
                                    0x94DF4CDEACC1A5FDL, 0x058A5BD1C5483AFFL,
247
      0xF2DA0ED240B1B642L, 0x44CEFADCD54BF9A9L, 0x1312200E433C7EE6L,
291
                                    0x63166CC142BA3C37L, 0x8DB8526EB2F76F40L,
248
      0x9FFCC84F3A78C748L, 0xF0CD1F72248576BBL, 0xEC6974053638CFE4L,
292
                                    0xE10880036F0D6D4EL, 0x9E0523C9971D311DL,
249
      0x2BA7B67C0CEC4E4CL, 0xAC2F4DF3E5CE32EDL, 0xCB33D14326EA4C11L,
293
                                    0x45EC2824CC7CD691L, 0x575B8359E62382C9L,
250
      0xA4E9044CC77E58BCL, 0x5F513293D934FCEFL, 0x5DC9645506E55444L,
294
                                    0xFA9E400DC4889995L, 0xD1823ECB45721568L,
251
      0x50DE418F317DE40AL, 0x388CB31A69DDE259L, 0x2DB4A83455820A86L,
295
                                    0xDAFD983B8206082FL, 0xAA7D29082386A8CBL,
252
      0x9010A91E84711AE9L, 0x4DF7F0B7B1498371L, 0xD62A2EABC0977179L,
296
                                    0x269FCD4403B87588L, 0x1B91F5F728BDD1E0L,
253
      0x22FAC097AA8D5C0EL };
297
                                    0xE4669F39040201F6L, 0x7A1D7C218CF04ADEL,
298
                                    0x65623C29D79CE5CEL, 0x2368449096C00BB1L,
299
                                    0xAB9BF1879DA503BAL, 0xBC23ECB1A458058EL,
300
                                    0x9A58DF01BB401ECCL, 0xA070E868A85F143DL,
301
                                    0x4FF188307DF2239EL, 0x14D565B41A641183L,
302
                                    0xEE13337452701602L, 0x950E3DCF3F285E09L,
303
                                    0x59930254B9C80953L, 0x3BF299408930DA6DL,
304
                                    0xA955943F53691387L, 0xA15EDECAA9CB8784L,
305
                                    0x29142127352BE9A0L, 0x76F0371FFF4E7AFBL,
306
                                    0x0239F450274F2228L, 0xBB073AF01D5E868BL,
307
                                    0xBFC80571C10E96C1L, 0xD267088568222E23L,
308
                                    0x9671A3D48E80B5B0L, 0x55B5D38AE193BB81L,
309
                                    0x693AE2D0A18B04B8L, 0x5C48B4ECADD5335FL,
310
                                    0xFD743B194916A1CAL, 0x2577018134BE98C4L,
311
                                    0xE77987E83C54A4ADL, 0x28E11014DA33E1B9L,
312
                                    0x270CC59E226AA213L, 0x71495F756D1A5F60L,
313
                                    0x9BE853FB60AFEF77L, 0xADC786A7F7443DBFL,
314
                                    0x0904456173B29A82L, 0x58BC7A66C232BD5EL,
315
                                    0xF306558C673AC8B2L, 0x41F639C6B6C9772AL,
316
                                    0x216DEFE99FDA35DAL, 0x11640CC71C7BE615L,
317
                                    0x93C43694565C5527L, 0xEA038E6246777839L,
318
                                    0xF9ABF3CE5A3E2469L, 0x741E768D0FD312D2L,
319
                                    0x0144B883CED652C6L, 0xC20B5A5BA33F8552L,
320
                                    0x1AE69633C3435A9DL, 0x97A28CA4088CFDECL,
321
                                    0x8824A43C1E96F420L, 0x37612FA66EEEA746L,
322
                                    0x6B4CB165F9CF0E5AL, 0x43AA1C06A0ABFB4AL,
323
                                    0x7F4DC26FF162796BL, 0x6CBACC8E54ED9B0FL,
324
                                    0xA6B7FFEFD2BB253EL, 0x2E25BC95B0A29D4FL,
325
                                    0x86D6A58BDEF1388CL, 0xDED74AC576B6F054L,
326
                                    0x8030BDBC2B45805DL, 0x3C81AF70E94D9289L,
327
                                    0x3EFF6DDA9E3100DBL, 0xB38DC39FDFCC8847L,
328
                                    0x123885528D17B87EL, 0xF2DA0ED240B1B642L,
329
                                    0x44CEFADCD54BF9A9L, 0x1312200E433C7EE6L,
330
                                    0x9FFCC84F3A78C748L, 0xF0CD1F72248576BBL,
331
                                    0xEC6974053638CFE4L, 0x2BA7B67C0CEC4E4CL,
332
                                    0xAC2F4DF3E5CE32EDL, 0xCB33D14326EA4C11L,
333
                                    0xA4E9044CC77E58BCL, 0x5F513293D934FCEFL,
334
                                    0x5DC9645506E55444L, 0x50DE418F317DE40AL,
335
                                    0x388CB31A69DDE259L, 0x2DB4A83455820A86L,
336
                                    0x9010A91E84711AE9L, 0x4DF7F0B7B1498371L,
337
                                    0xD62A2EABC0977179L, 0x22FAC097AA8D5C0EL };
338
254
339
  /** S-Box T3. */
255
  /** S-Box T3. */
340
  private static final long[] T3 = { 0xF49FCC2FF1DAF39BL, 0x487FD5C66FF29281L,
256
  private static final long[] T3 = {
341
                                    0xE8A30667FCDCA83FL, 0x2C9B4BE3D2FCCE63L,
257
      0xF49FCC2FF1DAF39BL, 0x487FD5C66FF29281L, 0xE8A30667FCDCA83FL,
342
                                    0xDA3FF74B93FBBBC2L, 0x2FA165D2FE70BA66L,
258
      0x2C9B4BE3D2FCCE63L, 0xDA3FF74B93FBBBC2L, 0x2FA165D2FE70BA66L,
343
                                    0xA103E279970E93D4L, 0xBECDEC77B0E45E71L,
259
      0xA103E279970E93D4L, 0xBECDEC77B0E45E71L, 0xCFB41E723985E497L,
344
                                    0xCFB41E723985E497L, 0xB70AAA025EF75017L,
260
      0xB70AAA025EF75017L, 0xD42309F03840B8E0L, 0x8EFC1AD035898579L,
345
                                    0xD42309F03840B8E0L, 0x8EFC1AD035898579L,
261
      0x96C6920BE2B2ABC5L, 0x66AF4163375A9172L, 0x2174ABDCCA7127FBL,
346
                                    0x96C6920BE2B2ABC5L, 0x66AF4163375A9172L,
262
      0xB33CCEA64A72FF41L, 0xF04A4933083066A5L, 0x8D970ACDD7289AF5L,
347
                                    0x2174ABDCCA7127FBL, 0xB33CCEA64A72FF41L,
263
      0x8F96E8E031C8C25EL, 0xF3FEC02276875D47L, 0xEC7BF310056190DDL,
348
                                    0xF04A4933083066A5L, 0x8D970ACDD7289AF5L,
264
      0xF5ADB0AEBB0F1491L, 0x9B50F8850FD58892L, 0x4975488358B74DE8L,
349
                                    0x8F96E8E031C8C25EL, 0xF3FEC02276875D47L,
265
      0xA3354FF691531C61L, 0x0702BBE481D2C6EEL, 0x89FB24057DEDED98L,
350
                                    0xEC7BF310056190DDL, 0xF5ADB0AEBB0F1491L,
266
      0xAC3075138596E902L, 0x1D2D3580172772EDL, 0xEB738FC28E6BC30DL,
351
                                    0x9B50F8850FD58892L, 0x4975488358B74DE8L,
267
      0x5854EF8F63044326L, 0x9E5C52325ADD3BBEL, 0x90AA53CF325C4623L,
352
                                    0xA3354FF691531C61L, 0x0702BBE481D2C6EEL,
268
      0xC1D24D51349DD067L, 0x2051CFEEA69EA624L, 0x13220F0A862E7E4FL,
353
                                    0x89FB24057DEDED98L, 0xAC3075138596E902L,
269
      0xCE39399404E04864L, 0xD9C42CA47086FCB7L, 0x685AD2238A03E7CCL,
354
                                    0x1D2D3580172772EDL, 0xEB738FC28E6BC30DL,
270
      0x066484B2AB2FF1DBL, 0xFE9D5D70EFBF79ECL, 0x5B13B9DD9C481854L,
355
                                    0x5854EF8F63044326L, 0x9E5C52325ADD3BBEL,
271
      0x15F0D475ED1509ADL, 0x0BEBCD060EC79851L, 0xD58C6791183AB7F8L,
356
                                    0x90AA53CF325C4623L, 0xC1D24D51349DD067L,
272
      0xD1187C5052F3EEE4L, 0xC95D1192E54E82FFL, 0x86EEA14CB9AC6CA2L,
357
                                    0x2051CFEEA69EA624L, 0x13220F0A862E7E4FL,
273
      0x3485BEB153677D5DL, 0xDD191D781F8C492AL, 0xF60866BAA784EBF9L,
358
                                    0xCE39399404E04864L, 0xD9C42CA47086FCB7L,
274
      0x518F643BA2D08C74L, 0x8852E956E1087C22L, 0xA768CB8DC410AE8DL,
359
                                    0x685AD2238A03E7CCL, 0x066484B2AB2FF1DBL,
275
      0x38047726BFEC8E1AL, 0xA67738B4CD3B45AAL, 0xAD16691CEC0DDE19L,
360
                                    0xFE9D5D70EFBF79ECL, 0x5B13B9DD9C481854L,
276
      0xC6D4319380462E07L, 0xC5A5876D0BA61938L, 0x16B9FA1FA58FD840L,
361
                                    0x15F0D475ED1509ADL, 0x0BEBCD060EC79851L,
277
      0x188AB1173CA74F18L, 0xABDA2F98C99C021FL, 0x3E0580AB134AE816L,
362
                                    0xD58C6791183AB7F8L, 0xD1187C5052F3EEE4L,
278
      0x5F3B05B773645ABBL, 0x2501A2BE5575F2F6L, 0x1B2F74004E7E8BA9L,
363
                                    0xC95D1192E54E82FFL, 0x86EEA14CB9AC6CA2L,
279
      0x1CD7580371E8D953L, 0x7F6ED89562764E30L, 0xB15926FF596F003DL,
364
                                    0x3485BEB153677D5DL, 0xDD191D781F8C492AL,
280
      0x9F65293DA8C5D6B9L, 0x6ECEF04DD690F84CL, 0x4782275FFF33AF88L,
365
                                    0xF60866BAA784EBF9L, 0x518F643BA2D08C74L,
281
      0xE41433083F820801L, 0xFD0DFE409A1AF9B5L, 0x4325A3342CDB396BL,
366
                                    0x8852E956E1087C22L, 0xA768CB8DC410AE8DL,
282
      0x8AE77E62B301B252L, 0xC36F9E9F6655615AL, 0x85455A2D92D32C09L,
367
                                    0x38047726BFEC8E1AL, 0xA67738B4CD3B45AAL,
283
      0xF2C7DEA949477485L, 0x63CFB4C133A39EBAL, 0x83B040CC6EBC5462L,
368
                                    0xAD16691CEC0DDE19L, 0xC6D4319380462E07L,
284
      0x3B9454C8FDB326B0L, 0x56F56A9E87FFD78CL, 0x2DC2940D99F42BC6L,
369
                                    0xC5A5876D0BA61938L, 0x16B9FA1FA58FD840L,
285
      0x98F7DF096B096E2DL, 0x19A6E01E3AD852BFL, 0x42A99CCBDBD4B40BL,
370
                                    0x188AB1173CA74F18L, 0xABDA2F98C99C021FL,
286
      0xA59998AF45E9C559L, 0x366295E807D93186L, 0x6B48181BFAA1F773L,
371
                                    0x3E0580AB134AE816L, 0x5F3B05B773645ABBL,
287
      0x1FEC57E2157A0A1DL, 0x4667446AF6201AD5L, 0xE615EBCACFB0F075L,
372
                                    0x2501A2BE5575F2F6L, 0x1B2F74004E7E8BA9L,
288
      0xB8F31F4F68290778L, 0x22713ED6CE22D11EL, 0x3057C1A72EC3C93BL,
373
                                    0x1CD7580371E8D953L, 0x7F6ED89562764E30L,
289
      0xCB46ACC37C3F1F2FL, 0xDBB893FD02AAF50EL, 0x331FD92E600B9FCFL,
374
                                    0xB15926FF596F003DL, 0x9F65293DA8C5D6B9L,
290
      0xA498F96148EA3AD6L, 0xA8D8426E8B6A83EAL, 0xA089B274B7735CDCL,
375
                                    0x6ECEF04DD690F84CL, 0x4782275FFF33AF88L,
291
      0x87F6B3731E524A11L, 0x118808E5CBC96749L, 0x9906E4C7B19BD394L,
376
                                    0xE41433083F820801L, 0xFD0DFE409A1AF9B5L,
292
      0xAFED7F7E9B24A20CL, 0x6509EADEEB3644A7L, 0x6C1EF1D3E8EF0EDEL,
377
                                    0x4325A3342CDB396BL, 0x8AE77E62B301B252L,
293
      0xB9C97D43E9798FB4L, 0xA2F2D784740C28A3L, 0x7B8496476197566FL,
378
                                    0xC36F9E9F6655615AL, 0x85455A2D92D32C09L,
294
      0x7A5BE3E6B65F069DL, 0xF96330ED78BE6F10L, 0xEEE60DE77A076A15L,
379
                                    0xF2C7DEA949477485L, 0x63CFB4C133A39EBAL,
295
      0x2B4BEE4AA08B9BD0L, 0x6A56A63EC7B8894EL, 0x02121359BA34FEF4L,
380
                                    0x83B040CC6EBC5462L, 0x3B9454C8FDB326B0L,
296
      0x4CBF99F8283703FCL, 0x398071350CAF30C8L, 0xD0A77A89F017687AL,
381
                                    0x56F56A9E87FFD78CL, 0x2DC2940D99F42BC6L,
297
      0xF1C1A9EB9E423569L, 0x8C7976282DEE8199L, 0x5D1737A5DD1F7ABDL,
382
                                    0x98F7DF096B096E2DL, 0x19A6E01E3AD852BFL,
298
      0x4F53433C09A9FA80L, 0xFA8B0C53DF7CA1D9L, 0x3FD9DCBC886CCB77L,
383
                                    0x42A99CCBDBD4B40BL, 0xA59998AF45E9C559L,
299
      0xC040917CA91B4720L, 0x7DD00142F9D1DCDFL, 0x8476FC1D4F387B58L,
384
                                    0x366295E807D93186L, 0x6B48181BFAA1F773L,
300
      0x23F8E7C5F3316503L, 0x032A2244E7E37339L, 0x5C87A5D750F5A74BL,
385
                                    0x1FEC57E2157A0A1DL, 0x4667446AF6201AD5L,
301
      0x082B4CC43698992EL, 0xDF917BECB858F63CL, 0x3270B8FC5BF86DDAL,
386
                                    0xE615EBCACFB0F075L, 0xB8F31F4F68290778L,
302
      0x10AE72BB29B5DD76L, 0x576AC94E7700362BL, 0x1AD112DAC61EFB8FL,
387
                                    0x22713ED6CE22D11EL, 0x3057C1A72EC3C93BL,
303
      0x691BC30EC5FAA427L, 0xFF246311CC327143L, 0x3142368E30E53206L,
388
                                    0xCB46ACC37C3F1F2FL, 0xDBB893FD02AAF50EL,
304
      0x71380E31E02CA396L, 0x958D5C960AAD76F1L, 0xF8D6F430C16DA536L,
389
                                    0x331FD92E600B9FCFL, 0xA498F96148EA3AD6L,
305
      0xC8FFD13F1BE7E1D2L, 0x7578AE66004DDBE1L, 0x05833F01067BE646L,
390
                                    0xA8D8426E8B6A83EAL, 0xA089B274B7735CDCL,
306
      0xBB34B5AD3BFE586DL, 0x095F34C9A12B97F0L, 0x247AB64525D60CA8L,
391
                                    0x87F6B3731E524A11L, 0x118808E5CBC96749L,
307
      0xDCDBC6F3017477D1L, 0x4A2E14D4DECAD24DL, 0xBDB5E6D9BE0A1EEBL,
392
                                    0x9906E4C7B19BD394L, 0xAFED7F7E9B24A20CL,
308
      0x2A7E70F7794301ABL, 0xDEF42D8A270540FDL, 0x01078EC0A34C22C1L,
393
                                    0x6509EADEEB3644A7L, 0x6C1EF1D3E8EF0EDEL,
309
      0xE5DE511AF4C16387L, 0x7EBB3A52BD9A330AL, 0x77697857AA7D6435L,
394
                                    0xB9C97D43E9798FB4L, 0xA2F2D784740C28A3L,
310
      0x004E831603AE4C32L, 0xE7A21020AD78E312L, 0x9D41A70C6AB420F2L,
395
                                    0x7B8496476197566FL, 0x7A5BE3E6B65F069DL,
311
      0x28E06C18EA1141E6L, 0xD2B28CBD984F6B28L, 0x26B75F6C446E9D83L,
396
                                    0xF96330ED78BE6F10L, 0xEEE60DE77A076A15L,
312
      0xBA47568C4D418D7FL, 0xD80BADBFE6183D8EL, 0x0E206D7F5F166044L,
397
                                    0x2B4BEE4AA08B9BD0L, 0x6A56A63EC7B8894EL,
313
      0xE258A43911CBCA3EL, 0x723A1746B21DC0BCL, 0xC7CAA854F5D7CDD3L,
398
                                    0x02121359BA34FEF4L, 0x4CBF99F8283703FCL,
314
      0x7CAC32883D261D9CL, 0x7690C26423BA942CL, 0x17E55524478042B8L,
399
                                    0x398071350CAF30C8L, 0xD0A77A89F017687AL,
315
      0xE0BE477656A2389FL, 0x4D289B5E67AB2DA0L, 0x44862B9C8FBBFD31L,
400
                                    0xF1C1A9EB9E423569L, 0x8C7976282DEE8199L,
316
      0xB47CC8049D141365L, 0x822C1B362B91C793L, 0x4EB14655FB13DFD8L,
401
                                    0x5D1737A5DD1F7ABDL, 0x4F53433C09A9FA80L,
317
      0x1ECBBA0714E2A97BL, 0x6143459D5CDE5F14L, 0x53A8FBF1D5F0AC89L,
402
                                    0xFA8B0C53DF7CA1D9L, 0x3FD9DCBC886CCB77L,
318
      0x97EA04D81C5E5B00L, 0x622181A8D4FDB3F3L, 0xE9BCD341572A1208L,
403
                                    0xC040917CA91B4720L, 0x7DD00142F9D1DCDFL,
319
      0x1411258643CCE58AL, 0x9144C5FEA4C6E0A4L, 0x0D33D06565CF620FL,
404
                                    0x8476FC1D4F387B58L, 0x23F8E7C5F3316503L,
320
      0x54A48D489F219CA1L, 0xC43E5EAC6D63C821L, 0xA9728B3A72770DAFL,
405
                                    0x032A2244E7E37339L, 0x5C87A5D750F5A74BL,
321
      0xD7934E7B20DF87EFL, 0xE35503B61A3E86E5L, 0xCAE321FBC819D504L,
406
                                    0x082B4CC43698992EL, 0xDF917BECB858F63CL,
322
      0x129A50B3AC60BFA6L, 0xCD5E68EA7E9FB6C3L, 0xB01C90199483B1C7L,
407
                                    0x3270B8FC5BF86DDAL, 0x10AE72BB29B5DD76L,
323
      0x3DE93CD5C295376CL, 0xAED52EDF2AB9AD13L, 0x2E60F512C0A07884L,
408
                                    0x576AC94E7700362BL, 0x1AD112DAC61EFB8FL,
324
      0xBC3D86A3E36210C9L, 0x35269D9B163951CEL, 0x0C7D6E2AD0CDB5FAL,
409
                                    0x691BC30EC5FAA427L, 0xFF246311CC327143L,
325
      0x59E86297D87F5733L, 0x298EF221898DB0E7L, 0x55000029D1A5AA7EL,
410
                                    0x3142368E30E53206L, 0x71380E31E02CA396L,
326
      0x8BC08AE1B5061B45L, 0xC2C31C2B6C92703AL, 0x94CC596BAF25EF42L,
411
                                    0x958D5C960AAD76F1L, 0xF8D6F430C16DA536L,
327
      0x0A1D73DB22540456L, 0x04B6A0F9D9C4179AL, 0xEFFDAFA2AE3D3C60L,
412
                                    0xC8FFD13F1BE7E1D2L, 0x7578AE66004DDBE1L,
328
      0xF7C8075BB49496C4L, 0x9CC5C7141D1CD4E3L, 0x78BD1638218E5534L,
413
                                    0x05833F01067BE646L, 0xBB34B5AD3BFE586DL,
329
      0xB2F11568F850246AL, 0xEDFABCFA9502BC29L, 0x796CE5F2DA23051BL,
414
                                    0x095F34C9A12B97F0L, 0x247AB64525D60CA8L,
330
      0xAAE128B0DC93537CL, 0x3A493DA0EE4B29AEL, 0xB5DF6B2C416895D7L,
415
                                    0xDCDBC6F3017477D1L, 0x4A2E14D4DECAD24DL,
331
      0xFCABBD25122D7F37L, 0x70810B58105DC4B1L, 0xE10FDD37F7882A90L,
416
                                    0xBDB5E6D9BE0A1EEBL, 0x2A7E70F7794301ABL,
332
      0x524DCAB5518A3F5CL, 0x3C9E85878451255BL, 0x4029828119BD34E2L,
417
                                    0xDEF42D8A270540FDL, 0x01078EC0A34C22C1L,
333
      0x74A05B6F5D3CECCBL, 0xB610021542E13ECAL, 0x0FF979D12F59E2ACL,
418
                                    0xE5DE511AF4C16387L, 0x7EBB3A52BD9A330AL,
334
      0x6037DA27E4F9CC50L, 0x5E92975A0DF1847DL, 0xD66DE190D3E623FEL,
419
                                    0x77697857AA7D6435L, 0x004E831603AE4C32L,
335
      0x5032D6B87B568048L, 0x9A36B7CE8235216EL, 0x80272A7A24F64B4AL,
420
                                    0xE7A21020AD78E312L, 0x9D41A70C6AB420F2L,
336
      0x93EFED8B8C6916F7L, 0x37DDBFF44CCE1555L, 0x4B95DB5D4B99BD25L,
421
                                    0x28E06C18EA1141E6L, 0xD2B28CBD984F6B28L,
337
      0x92D3FDA169812FC0L, 0xFB1A4A9A90660BB6L, 0x730C196946A4B9B2L,
422
                                    0x26B75F6C446E9D83L, 0xBA47568C4D418D7FL,
338
      0x81E289AA7F49DA68L, 0x64669A0F83B1A05FL, 0x27B3FF7D9644F48BL,
423
                                    0xD80BADBFE6183D8EL, 0x0E206D7F5F166044L,
339
      0xCC6B615C8DB675B3L, 0x674F20B9BCEBBE95L, 0x6F31238275655982L,
424
                                    0xE258A43911CBCA3EL, 0x723A1746B21DC0BCL,
340
      0x5AE488713E45CF05L, 0xBF619F9954C21157L, 0xEABAC46040A8EAE9L,
425
                                    0xC7CAA854F5D7CDD3L, 0x7CAC32883D261D9CL,
341
      0x454C6FE9F2C0C1CDL, 0x419CF6496412691CL, 0xD3DC3BEF265B0F70L,
426
                                    0x7690C26423BA942CL, 0x17E55524478042B8L,
342
      0x6D0E60F5C3578A9EL };
427
                                    0xE0BE477656A2389FL, 0x4D289B5E67AB2DA0L,
428
                                    0x44862B9C8FBBFD31L, 0xB47CC8049D141365L,
429
                                    0x822C1B362B91C793L, 0x4EB14655FB13DFD8L,
430
                                    0x1ECBBA0714E2A97BL, 0x6143459D5CDE5F14L,
431
                                    0x53A8FBF1D5F0AC89L, 0x97EA04D81C5E5B00L,
432
                                    0x622181A8D4FDB3F3L, 0xE9BCD341572A1208L,
433
                                    0x1411258643CCE58AL, 0x9144C5FEA4C6E0A4L,
434
                                    0x0D33D06565CF620FL, 0x54A48D489F219CA1L,
435
                                    0xC43E5EAC6D63C821L, 0xA9728B3A72770DAFL,
436
                                    0xD7934E7B20DF87EFL, 0xE35503B61A3E86E5L,
437
                                    0xCAE321FBC819D504L, 0x129A50B3AC60BFA6L,
438
                                    0xCD5E68EA7E9FB6C3L, 0xB01C90199483B1C7L,
439
                                    0x3DE93CD5C295376CL, 0xAED52EDF2AB9AD13L,
440
                                    0x2E60F512C0A07884L, 0xBC3D86A3E36210C9L,
441
                                    0x35269D9B163951CEL, 0x0C7D6E2AD0CDB5FAL,
442
                                    0x59E86297D87F5733L, 0x298EF221898DB0E7L,
443
                                    0x55000029D1A5AA7EL, 0x8BC08AE1B5061B45L,
444
                                    0xC2C31C2B6C92703AL, 0x94CC596BAF25EF42L,
445
                                    0x0A1D73DB22540456L, 0x04B6A0F9D9C4179AL,
446
                                    0xEFFDAFA2AE3D3C60L, 0xF7C8075BB49496C4L,
447
                                    0x9CC5C7141D1CD4E3L, 0x78BD1638218E5534L,
448
                                    0xB2F11568F850246AL, 0xEDFABCFA9502BC29L,
449
                                    0x796CE5F2DA23051BL, 0xAAE128B0DC93537CL,
450
                                    0x3A493DA0EE4B29AEL, 0xB5DF6B2C416895D7L,
451
                                    0xFCABBD25122D7F37L, 0x70810B58105DC4B1L,
452
                                    0xE10FDD37F7882A90L, 0x524DCAB5518A3F5CL,
453
                                    0x3C9E85878451255BL, 0x4029828119BD34E2L,
454
                                    0x74A05B6F5D3CECCBL, 0xB610021542E13ECAL,
455
                                    0x0FF979D12F59E2ACL, 0x6037DA27E4F9CC50L,
456
                                    0x5E92975A0DF1847DL, 0xD66DE190D3E623FEL,
457
                                    0x5032D6B87B568048L, 0x9A36B7CE8235216EL,
458
                                    0x80272A7A24F64B4AL, 0x93EFED8B8C6916F7L,
459
                                    0x37DDBFF44CCE1555L, 0x4B95DB5D4B99BD25L,
460
                                    0x92D3FDA169812FC0L, 0xFB1A4A9A90660BB6L,
461
                                    0x730C196946A4B9B2L, 0x81E289AA7F49DA68L,
462
                                    0x64669A0F83B1A05FL, 0x27B3FF7D9644F48BL,
463
                                    0xCC6B615C8DB675B3L, 0x674F20B9BCEBBE95L,
464
                                    0x6F31238275655982L, 0x5AE488713E45CF05L,
465
                                    0xBF619F9954C21157L, 0xEABAC46040A8EAE9L,
466
                                    0x454C6FE9F2C0C1CDL, 0x419CF6496412691CL,
467
                                    0xD3DC3BEF265B0F70L, 0x6D0E60F5C3578A9EL };
468
343
469
  /** S-Box T4. */
344
  /** S-Box T4. */
470
  private static final long[] T4 = { 0x5B0E608526323C55L, 0x1A46C1A9FA1B59F5L,
345
  private static final long[] T4 = {
471
                                    0xA9E245A17C4C8FFAL, 0x65CA5159DB2955D7L,
346
      0x5B0E608526323C55L, 0x1A46C1A9FA1B59F5L, 0xA9E245A17C4C8FFAL,
472
                                    0x05DB0A76CE35AFC2L, 0x81EAC77EA9113D45L,
347
      0x65CA5159DB2955D7L, 0x05DB0A76CE35AFC2L, 0x81EAC77EA9113D45L,
473
                                    0x528EF88AB6AC0A0DL, 0xA09EA253597BE3FFL,
348
      0x528EF88AB6AC0A0DL, 0xA09EA253597BE3FFL, 0x430DDFB3AC48CD56L,
474
                                    0x430DDFB3AC48CD56L, 0xC4B3A67AF45CE46FL,
349
      0xC4B3A67AF45CE46FL, 0x4ECECFD8FBE2D05EL, 0x3EF56F10B39935F0L,
475
                                    0x4ECECFD8FBE2D05EL, 0x3EF56F10B39935F0L,
350
      0x0B22D6829CD619C6L, 0x17FD460A74DF2069L, 0x6CF8CC8E8510ED40L,
476
                                    0x0B22D6829CD619C6L, 0x17FD460A74DF2069L,
351
      0xD6C824BF3A6ECAA7L, 0x61243D581A817049L, 0x048BACB6BBC163A2L,
477
                                    0x6CF8CC8E8510ED40L, 0xD6C824BF3A6ECAA7L,
352
      0xD9A38AC27D44CC32L, 0x7FDDFF5BAAF410ABL, 0xAD6D495AA804824BL,
478
                                    0x61243D581A817049L, 0x048BACB6BBC163A2L,
353
      0xE1A6A74F2D8C9F94L, 0xD4F7851235DEE8E3L, 0xFD4B7F886540D893L,
479
                                    0xD9A38AC27D44CC32L, 0x7FDDFF5BAAF410ABL,
354
      0x247C20042AA4BFDAL, 0x096EA1C517D1327CL, 0xD56966B4361A6685L,
480
                                    0xAD6D495AA804824BL, 0xE1A6A74F2D8C9F94L,
355
      0x277DA5C31221057DL, 0x94D59893A43ACFF7L, 0x64F0C51CCDC02281L,
481
                                    0xD4F7851235DEE8E3L, 0xFD4B7F886540D893L,
356
      0x3D33BCC4FF6189DBL, 0xE005CB184CE66AF1L, 0xFF5CCD1D1DB99BEAL,
482
                                    0x247C20042AA4BFDAL, 0x096EA1C517D1327CL,
357
      0xB0B854A7FE42980FL, 0x7BD46A6A718D4B9FL, 0xD10FA8CC22A5FD8CL,
483
                                    0xD56966B4361A6685L, 0x277DA5C31221057DL,
358
      0xD31484952BE4BD31L, 0xC7FA975FCB243847L, 0x4886ED1E5846C407L,
484
                                    0x94D59893A43ACFF7L, 0x64F0C51CCDC02281L,
359
      0x28CDDB791EB70B04L, 0xC2B00BE2F573417FL, 0x5C9590452180F877L,
485
                                    0x3D33BCC4FF6189DBL, 0xE005CB184CE66AF1L,
360
      0x7A6BDDFFF370EB00L, 0xCE509E38D6D9D6A4L, 0xEBEB0F00647FA702L,
486
                                    0xFF5CCD1D1DB99BEAL, 0xB0B854A7FE42980FL,
361
      0x1DCC06CF76606F06L, 0xE4D9F28BA286FF0AL, 0xD85A305DC918C262L,
487
                                    0x7BD46A6A718D4B9FL, 0xD10FA8CC22A5FD8CL,
362
      0x475B1D8732225F54L, 0x2D4FB51668CCB5FEL, 0xA679B9D9D72BBA20L,
488
                                    0xD31484952BE4BD31L, 0xC7FA975FCB243847L,
363
      0x53841C0D912D43A5L, 0x3B7EAA48BF12A4E8L, 0x781E0E47F22F1DDFL,
489
                                    0x4886ED1E5846C407L, 0x28CDDB791EB70B04L,
364
      0xEFF20CE60AB50973L, 0x20D261D19DFFB742L, 0x16A12B03062A2E39L,
490
                                    0xC2B00BE2F573417FL, 0x5C9590452180F877L,
365
      0x1960EB2239650495L, 0x251C16FED50EB8B8L, 0x9AC0C330F826016EL,
491
                                    0x7A6BDDFFF370EB00L, 0xCE509E38D6D9D6A4L,
366
      0xED152665953E7671L, 0x02D63194A6369570L, 0x5074F08394B1C987L,
492
                                    0xEBEB0F00647FA702L, 0x1DCC06CF76606F06L,
367
      0x70BA598C90B25CE1L, 0x794A15810B9742F6L, 0x0D5925E9FCAF8C6CL,
493
                                    0xE4D9F28BA286FF0AL, 0xD85A305DC918C262L,
368
      0x3067716CD868744EL, 0x910AB077E8D7731BL, 0x6A61BBDB5AC42F61L,
494
                                    0x475B1D8732225F54L, 0x2D4FB51668CCB5FEL,
369
      0x93513EFBF0851567L, 0xF494724B9E83E9D5L, 0xE887E1985C09648DL,
495
                                    0xA679B9D9D72BBA20L, 0x53841C0D912D43A5L,
370
      0x34B1D3C675370CFDL, 0xDC35E433BC0D255DL, 0xD0AAB84234131BE0L,
496
                                    0x3B7EAA48BF12A4E8L, 0x781E0E47F22F1DDFL,
371
      0x08042A50B48B7EAFL, 0x9997C4EE44A3AB35L, 0x829A7B49201799D0L,
497
                                    0xEFF20CE60AB50973L, 0x20D261D19DFFB742L,
372
      0x263B8307B7C54441L, 0x752F95F4FD6A6CA6L, 0x927217402C08C6E5L,
498
                                    0x16A12B03062A2E39L, 0x1960EB2239650495L,
373
      0x2A8AB754A795D9EEL, 0xA442F7552F72943DL, 0x2C31334E19781208L,
499
                                    0x251C16FED50EB8B8L, 0x9AC0C330F826016EL,
374
      0x4FA98D7CEAEE6291L, 0x55C3862F665DB309L, 0xBD0610175D53B1F3L,
500
                                    0xED152665953E7671L, 0x02D63194A6369570L,
375
      0x46FE6CB840413F27L, 0x3FE03792DF0CFA59L, 0xCFE700372EB85E8FL,
501
                                    0x5074F08394B1C987L, 0x70BA598C90B25CE1L,
376
      0xA7BE29E7ADBCE118L, 0xE544EE5CDE8431DDL, 0x8A781B1B41F1873EL,
502
                                    0x794A15810B9742F6L, 0x0D5925E9FCAF8C6CL,
377
      0xA5C94C78A0D2F0E7L, 0x39412E2877B60728L, 0xA1265EF3AFC9A62CL,
503
                                    0x3067716CD868744EL, 0x910AB077E8D7731BL,
378
      0xBCC2770C6A2506C5L, 0x3AB66DD5DCE1CE12L, 0xE65499D04A675B37L,
504
                                    0x6A61BBDB5AC42F61L, 0x93513EFBF0851567L,
379
      0x7D8F523481BFD216L, 0x0F6F64FCEC15F389L, 0x74EFBE618B5B13C8L,
505
                                    0xF494724B9E83E9D5L, 0xE887E1985C09648DL,
380
      0xACDC82B714273E1DL, 0xDD40BFE003199D17L, 0x37E99257E7E061F8L,
506
                                    0x34B1D3C675370CFDL, 0xDC35E433BC0D255DL,
381
      0xFA52626904775AAAL, 0x8BBBF63A463D56F9L, 0xF0013F1543A26E64L,
507
                                    0xD0AAB84234131BE0L, 0x08042A50B48B7EAFL,
382
      0xA8307E9F879EC898L, 0xCC4C27A4150177CCL, 0x1B432F2CCA1D3348L,
508
                                    0x9997C4EE44A3AB35L, 0x829A7B49201799D0L,
383
      0xDE1D1F8F9F6FA013L, 0x606602A047A7DDD6L, 0xD237AB64CC1CB2C7L,
509
                                    0x263B8307B7C54441L, 0x752F95F4FD6A6CA6L,
384
      0x9B938E7225FCD1D3L, 0xEC4E03708E0FF476L, 0xFEB2FBDA3D03C12DL,
510
                                    0x927217402C08C6E5L, 0x2A8AB754A795D9EEL,
385
      0xAE0BCED2EE43889AL, 0x22CB8923EBFB4F43L, 0x69360D013CF7396DL,
511
                                    0xA442F7552F72943DL, 0x2C31334E19781208L,
386
      0x855E3602D2D4E022L, 0x073805BAD01F784CL, 0x33E17A133852F546L,
512
                                    0x4FA98D7CEAEE6291L, 0x55C3862F665DB309L,
387
      0xDF4874058AC7B638L, 0xBA92B29C678AA14AL, 0x0CE89FC76CFAADCDL,
513
                                    0xBD0610175D53B1F3L, 0x46FE6CB840413F27L,
388
      0x5F9D4E0908339E34L, 0xF1AFE9291F5923B9L, 0x6E3480F60F4A265FL,
514
                                    0x3FE03792DF0CFA59L, 0xCFE700372EB85E8FL,
389
      0xEEBF3A2AB29B841CL, 0xE21938A88F91B4ADL, 0x57DFEFF845C6D3C3L,
515
                                    0xA7BE29E7ADBCE118L, 0xE544EE5CDE8431DDL,
390
      0x2F006B0BF62CAAF2L, 0x62F479EF6F75EE78L, 0x11A55AD41C8916A9L,
516
                                    0x8A781B1B41F1873EL, 0xA5C94C78A0D2F0E7L,
391
      0xF229D29084FED453L, 0x42F1C27B16B000E6L, 0x2B1F76749823C074L,
517
                                    0x39412E2877B60728L, 0xA1265EF3AFC9A62CL,
392
      0x4B76ECA3C2745360L, 0x8C98F463B91691BDL, 0x14BCC93CF1ADE66AL,
518
                                    0xBCC2770C6A2506C5L, 0x3AB66DD5DCE1CE12L,
393
      0x8885213E6D458397L, 0x8E177DF0274D4711L, 0xB49B73B5503F2951L,
519
                                    0xE65499D04A675B37L, 0x7D8F523481BFD216L,
394
      0x10168168C3F96B6BL, 0x0E3D963B63CAB0AEL, 0x8DFC4B5655A1DB14L,
520
                                    0x0F6F64FCEC15F389L, 0x74EFBE618B5B13C8L,
395
      0xF789F1356E14DE5CL, 0x683E68AF4E51DAC1L, 0xC9A84F9D8D4B0FD9L,
521
                                    0xACDC82B714273E1DL, 0xDD40BFE003199D17L,
396
      0x3691E03F52A0F9D1L, 0x5ED86E46E1878E80L, 0x3C711A0E99D07150L,
522
                                    0x37E99257E7E061F8L, 0xFA52626904775AAAL,
397
      0x5A0865B20C4E9310L, 0x56FBFC1FE4F0682EL, 0xEA8D5DE3105EDF9BL,
523
                                    0x8BBBF63A463D56F9L, 0xF0013F1543A26E64L,
398
      0x71ABFDB12379187AL, 0x2EB99DE1BEE77B9CL, 0x21ECC0EA33CF4523L,
524
                                    0xA8307E9F879EC898L, 0xCC4C27A4150177CCL,
399
      0x59A4D7521805C7A1L, 0x3896F5EB56AE7C72L, 0xAA638F3DB18F75DCL,
525
                                    0x1B432F2CCA1D3348L, 0xDE1D1F8F9F6FA013L,
400
      0x9F39358DABE9808EL, 0xB7DEFA91C00B72ACL, 0x6B5541FD62492D92L,
526
                                    0x606602A047A7DDD6L, 0xD237AB64CC1CB2C7L,
401
      0x6DC6DEE8F92E4D5BL, 0x353F57ABC4BEEA7EL, 0x735769D6DA5690CEL,
527
                                    0x9B938E7225FCD1D3L, 0xEC4E03708E0FF476L,
402
      0x0A234AA642391484L, 0xF6F9508028F80D9DL, 0xB8E319A27AB3F215L,
528
                                    0xFEB2FBDA3D03C12DL, 0xAE0BCED2EE43889AL,
403
      0x31AD9C1151341A4DL, 0x773C22A57BEF5805L, 0x45C7561A07968633L,
529
                                    0x22CB8923EBFB4F43L, 0x69360D013CF7396DL,
404
      0xF913DA9E249DBE36L, 0xDA652D9B78A64C68L, 0x4C27A97F3BC334EFL,
530
                                    0x855E3602D2D4E022L, 0x073805BAD01F784CL,
405
      0x76621220E66B17F4L, 0x967743899ACD7D0BL, 0xF3EE5BCAE0ED6782L,
531
                                    0x33E17A133852F546L, 0xDF4874058AC7B638L,
406
      0x409F753600C879FCL, 0x06D09A39B5926DB6L, 0x6F83AEB0317AC588L,
532
                                    0xBA92B29C678AA14AL, 0x0CE89FC76CFAADCDL,
407
      0x01E6CA4A86381F21L, 0x66FF3462D19F3025L, 0x72207C24DDFD3BFBL,
533
                                    0x5F9D4E0908339E34L, 0xF1AFE9291F5923B9L,
408
      0x4AF6B6D3E2ECE2EBL, 0x9C994DBEC7EA08DEL, 0x49ACE597B09A8BC4L,
534
                                    0x6E3480F60F4A265FL, 0xEEBF3A2AB29B841CL,
409
      0xB38C4766CF0797BAL, 0x131B9373C57C2A75L, 0xB1822CCE61931E58L,
535
                                    0xE21938A88F91B4ADL, 0x57DFEFF845C6D3C3L,
410
      0x9D7555B909BA1C0CL, 0x127FAFDD937D11D2L, 0x29DA3BADC66D92E4L,
536
                                    0x2F006B0BF62CAAF2L, 0x62F479EF6F75EE78L,
411
      0xA2C1D57154C2ECBCL, 0x58C5134D82F6FE24L, 0x1C3AE3515B62274FL,
537
                                    0x11A55AD41C8916A9L, 0xF229D29084FED453L,
412
      0xE907C82E01CB8126L, 0xF8ED091913E37FCBL, 0x3249D8F9C80046C9L,
538
                                    0x42F1C27B16B000E6L, 0x2B1F76749823C074L,
413
      0x80CF9BEDE388FB63L, 0x1881539A116CF19EL, 0x5103F3F76BD52457L,
539
                                    0x4B76ECA3C2745360L, 0x8C98F463B91691BDL,
414
      0x15B7E6F5AE47F7A8L, 0xDBD7C6DED47E9CCFL, 0x44E55C410228BB1AL,
540
                                    0x14BCC93CF1ADE66AL, 0x8885213E6D458397L,
415
      0xB647D4255EDB4E99L, 0x5D11882BB8AAFC30L, 0xF5098BBB29D3212AL,
541
                                    0x8E177DF0274D4711L, 0xB49B73B5503F2951L,
416
      0x8FB5EA14E90296B3L, 0x677B942157DD025AL, 0xFB58E7C0A390ACB5L,
542
                                    0x10168168C3F96B6BL, 0x0E3D963B63CAB0AEL,
417
      0x89D3674C83BD4A01L, 0x9E2DA4DF4BF3B93BL, 0xFCC41E328CAB4829L,
543
                                    0x8DFC4B5655A1DB14L, 0xF789F1356E14DE5CL,
418
      0x03F38C96BA582C52L, 0xCAD1BDBD7FD85DB2L, 0xBBB442C16082AE83L,
544
                                    0x683E68AF4E51DAC1L, 0xC9A84F9D8D4B0FD9L,
419
      0xB95FE86BA5DA9AB0L, 0xB22E04673771A93FL, 0x845358C9493152D8L,
545
                                    0x3691E03F52A0F9D1L, 0x5ED86E46E1878E80L,
420
      0xBE2A488697B4541EL, 0x95A2DC2DD38E6966L, 0xC02C11AC923C852BL,
546
                                    0x3C711A0E99D07150L, 0x5A0865B20C4E9310L,
421
      0x2388B1990DF2A87BL, 0x7C8008FA1B4F37BEL, 0x1F70D0C84D54E503L,
547
                                    0x56FBFC1FE4F0682EL, 0xEA8D5DE3105EDF9BL,
422
      0x5490ADEC7ECE57D4L, 0x002B3C27D9063A3AL, 0x7EAEA3848030A2BFL,
548
                                    0x71ABFDB12379187AL, 0x2EB99DE1BEE77B9CL,
423
      0xC602326DED2003C0L, 0x83A7287D69A94086L, 0xC57A5FCB30F57A8AL,
549
                                    0x21ECC0EA33CF4523L, 0x59A4D7521805C7A1L,
424
      0xB56844E479EBE779L, 0xA373B40F05DCBCE9L, 0xD71A786E88570EE2L,
550
                                    0x3896F5EB56AE7C72L, 0xAA638F3DB18F75DCL,
425
      0x879CBACDBDE8F6A0L, 0x976AD1BCC164A32FL, 0xAB21E25E9666D78BL,
551
                                    0x9F39358DABE9808EL, 0xB7DEFA91C00B72ACL,
426
      0x901063AAE5E5C33CL, 0x9818B34448698D90L, 0xE36487AE3E1E8ABBL,
552
                                    0x6B5541FD62492D92L, 0x6DC6DEE8F92E4D5BL,
427
      0xAFBDF931893BDCB4L, 0x6345A0DC5FBBD519L, 0x8628FE269B9465CAL,
553
                                    0x353F57ABC4BEEA7EL, 0x735769D6DA5690CEL,
428
      0x1E5D01603F9C51ECL, 0x4DE44006A15049B7L, 0xBF6C70E5F776CBB1L,
554
                                    0x0A234AA642391484L, 0xF6F9508028F80D9DL,
429
      0x411218F2EF552BEDL, 0xCB0C0708705A36A3L, 0xE74D14754F986044L,
555
                                    0xB8E319A27AB3F215L, 0x31AD9C1151341A4DL,
430
      0xCD56D9430EA8280EL, 0xC12591D7535F5065L, 0xC83223F1720AEF96L,
556
                                    0x773C22A57BEF5805L, 0x45C7561A07968633L,
431
      0xC3A0396F7363A51FL };
557
                                    0xF913DA9E249DBE36L, 0xDA652D9B78A64C68L,
558
                                    0x4C27A97F3BC334EFL, 0x76621220E66B17F4L,
559
                                    0x967743899ACD7D0BL, 0xF3EE5BCAE0ED6782L,
560
                                    0x409F753600C879FCL, 0x06D09A39B5926DB6L,
561
                                    0x6F83AEB0317AC588L, 0x01E6CA4A86381F21L,
562
                                    0x66FF3462D19F3025L, 0x72207C24DDFD3BFBL,
563
                                    0x4AF6B6D3E2ECE2EBL, 0x9C994DBEC7EA08DEL,
564
                                    0x49ACE597B09A8BC4L, 0xB38C4766CF0797BAL,
565
                                    0x131B9373C57C2A75L, 0xB1822CCE61931E58L,
566
                                    0x9D7555B909BA1C0CL, 0x127FAFDD937D11D2L,
567
                                    0x29DA3BADC66D92E4L, 0xA2C1D57154C2ECBCL,
568
                                    0x58C5134D82F6FE24L, 0x1C3AE3515B62274FL,
569
                                    0xE907C82E01CB8126L, 0xF8ED091913E37FCBL,
570
                                    0x3249D8F9C80046C9L, 0x80CF9BEDE388FB63L,
571
                                    0x1881539A116CF19EL, 0x5103F3F76BD52457L,
572
                                    0x15B7E6F5AE47F7A8L, 0xDBD7C6DED47E9CCFL,
573
                                    0x44E55C410228BB1AL, 0xB647D4255EDB4E99L,
574
                                    0x5D11882BB8AAFC30L, 0xF5098BBB29D3212AL,
575
                                    0x8FB5EA14E90296B3L, 0x677B942157DD025AL,
576
                                    0xFB58E7C0A390ACB5L, 0x89D3674C83BD4A01L,
577
                                    0x9E2DA4DF4BF3B93BL, 0xFCC41E328CAB4829L,
578
                                    0x03F38C96BA582C52L, 0xCAD1BDBD7FD85DB2L,
579
                                    0xBBB442C16082AE83L, 0xB95FE86BA5DA9AB0L,
580
                                    0xB22E04673771A93FL, 0x845358C9493152D8L,
581
                                    0xBE2A488697B4541EL, 0x95A2DC2DD38E6966L,
582
                                    0xC02C11AC923C852BL, 0x2388B1990DF2A87BL,
583
                                    0x7C8008FA1B4F37BEL, 0x1F70D0C84D54E503L,
584
                                    0x5490ADEC7ECE57D4L, 0x002B3C27D9063A3AL,
585
                                    0x7EAEA3848030A2BFL, 0xC602326DED2003C0L,
586
                                    0x83A7287D69A94086L, 0xC57A5FCB30F57A8AL,
587
                                    0xB56844E479EBE779L, 0xA373B40F05DCBCE9L,
588
                                    0xD71A786E88570EE2L, 0x879CBACDBDE8F6A0L,
589
                                    0x976AD1BCC164A32FL, 0xAB21E25E9666D78BL,
590
                                    0x901063AAE5E5C33CL, 0x9818B34448698D90L,
591
                                    0xE36487AE3E1E8ABBL, 0xAFBDF931893BDCB4L,
592
                                    0x6345A0DC5FBBD519L, 0x8628FE269B9465CAL,
593
                                    0x1E5D01603F9C51ECL, 0x4DE44006A15049B7L,
594
                                    0xBF6C70E5F776CBB1L, 0x411218F2EF552BEDL,
595
                                    0xCB0C0708705A36A3L, 0xE74D14754F986044L,
596
                                    0xCD56D9430EA8280EL, 0xC12591D7535F5065L,
597
                                    0xC83223F1720AEF96L, 0xC3A0396F7363A51FL };
598
432
599
  // The cached self-test result.
433
  // The cached self-test result.
600
  private static Boolean valid;
434
  private static Boolean valid;
Lines 602-610 Link Here
602
  // The context.
436
  // The context.
603
  private long a, b, c;
437
  private long a, b, c;
604
438
605
  // Constructors.
606
  // -----------------------------------------------------------------------
607
608
  /**
439
  /**
609
   * Trivial 0-arguments constructor.
440
   * Trivial 0-arguments constructor.
610
   */
441
   */
Lines 628-636 Link Here
628
    this.buffer = (that.buffer != null) ? (byte[]) that.buffer.clone() : null;
459
    this.buffer = (that.buffer != null) ? (byte[]) that.buffer.clone() : null;
629
  }
460
  }
630
461
631
  // Instance methods implementing BaseHash.
632
  // -----------------------------------------------------------------------
633
634
  public Object clone()
462
  public Object clone()
635
  {
463
  {
636
    return new Tiger(this);
464
    return new Tiger(this);
Lines 640-681 Link Here
640
  {
468
  {
641
    if (valid == null)
469
    if (valid == null)
642
      {
470
      {
643
        valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new Tiger().digest())));
471
        String d = Util.toString(new Tiger().digest());
472
        valid = Boolean.valueOf(DIGEST0.equals(d));
644
      }
473
      }
645
    return valid.booleanValue();
474
    return valid.booleanValue();
646
  }
475
  }
647
476
648
  protected byte[] padBuffer()
477
  protected byte[] padBuffer()
649
  {
478
  {
650
    int n = (int) (count % BLOCK_SIZE);
479
    int n = (int)(count % BLOCK_SIZE);
651
    int padding = (n < 56) ? (56 - n) : (120 - n);
480
    int padding = (n < 56) ? (56 - n) : (120 - n);
652
    byte[] pad = new byte[padding + 8];
481
    byte[] pad = new byte[padding + 8];
653
654
    pad[0] = 1;
482
    pad[0] = 1;
655
    long bits = count << 3;
483
    long bits = count << 3;
656
657
    pad[padding++] = (byte) bits;
484
    pad[padding++] = (byte) bits;
658
    pad[padding++] = (byte) (bits >>> 8);
485
    pad[padding++] = (byte)(bits >>> 8);
659
    pad[padding++] = (byte) (bits >>> 16);
486
    pad[padding++] = (byte)(bits >>> 16);
660
    pad[padding++] = (byte) (bits >>> 24);
487
    pad[padding++] = (byte)(bits >>> 24);
661
    pad[padding++] = (byte) (bits >>> 32);
488
    pad[padding++] = (byte)(bits >>> 32);
662
    pad[padding++] = (byte) (bits >>> 40);
489
    pad[padding++] = (byte)(bits >>> 40);
663
    pad[padding++] = (byte) (bits >>> 48);
490
    pad[padding++] = (byte)(bits >>> 48);
664
    pad[padding] = (byte) (bits >>> 56);
491
    pad[padding  ] = (byte)(bits >>> 56);
665
666
    return pad;
492
    return pad;
667
  }
493
  }
668
494
669
  protected byte[] getResult()
495
  protected byte[] getResult()
670
  {
496
  {
671
    return new byte[] { (byte) a, (byte) (a >>> 8), (byte) (a >>> 16),
497
    return new byte[] {
672
                       (byte) (a >>> 24), (byte) (a >>> 32), (byte) (a >>> 40),
498
        (byte) a,         (byte)(a >>>  8), (byte)(a >>> 16), (byte)(a >>> 24),
673
                       (byte) (a >>> 48), (byte) (a >>> 56), (byte) b,
499
        (byte)(a >>> 32), (byte)(a >>> 40), (byte)(a >>> 48), (byte)(a >>> 56),
674
                       (byte) (b >>> 8), (byte) (b >>> 16), (byte) (b >>> 24),
500
        (byte) b,         (byte)(b >>>  8), (byte)(b >>> 16), (byte)(b >>> 24),
675
                       (byte) (b >>> 32), (byte) (b >>> 40), (byte) (b >>> 48),
501
        (byte)(b >>> 32), (byte)(b >>> 40), (byte)(b >>> 48), (byte)(b >>> 56),
676
                       (byte) (b >>> 56), (byte) c, (byte) (c >>> 8),
502
        (byte) c,         (byte)(c >>>  8), (byte)(c >>> 16), (byte)(c >>> 24),
677
                       (byte) (c >>> 16), (byte) (c >>> 24), (byte) (c >>> 32),
503
        (byte)(c >>> 32), (byte)(c >>> 40), (byte)(c >>> 48), (byte)(c >>> 56) };
678
                       (byte) (c >>> 40), (byte) (c >>> 48), (byte) (c >>> 56) };
679
  }
504
  }
680
505
681
  protected void resetContext()
506
  protected void resetContext()
Lines 688-804 Link Here
688
  protected void transform(byte[] in, int offset)
513
  protected void transform(byte[] in, int offset)
689
  {
514
  {
690
    long x0, x1, x2, x3, x4, x5, x6, x7;
515
    long x0, x1, x2, x3, x4, x5, x6, x7;
691
516
    x0 = ((long)  in[offset++] & 0xFF)
692
    x0 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
517
       | ((long) (in[offset++] & 0xFF) << 8)
693
         | ((long) (in[offset++] & 0xFF) << 16)
518
       | ((long) (in[offset++] & 0xFF) << 16)
694
         | ((long) (in[offset++] & 0xFF) << 24)
519
       | ((long) (in[offset++] & 0xFF) << 24)
695
         | ((long) (in[offset++] & 0xFF) << 32)
520
       | ((long) (in[offset++] & 0xFF) << 32)
696
         | ((long) (in[offset++] & 0xFF) << 40)
521
       | ((long) (in[offset++] & 0xFF) << 40)
697
         | ((long) (in[offset++] & 0xFF) << 48)
522
       | ((long) (in[offset++] & 0xFF) << 48)
698
         | ((long) (in[offset++] & 0xFF) << 56);
523
       | ((long) (in[offset++] & 0xFF) << 56);
699
    x1 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
524
    x1 = ((long)  in[offset++] & 0xFF)
700
         | ((long) (in[offset++] & 0xFF) << 16)
525
       | ((long) (in[offset++] & 0xFF) << 8)
701
         | ((long) (in[offset++] & 0xFF) << 24)
526
       | ((long) (in[offset++] & 0xFF) << 16)
702
         | ((long) (in[offset++] & 0xFF) << 32)
527
       | ((long) (in[offset++] & 0xFF) << 24)
703
         | ((long) (in[offset++] & 0xFF) << 40)
528
       | ((long) (in[offset++] & 0xFF) << 32)
704
         | ((long) (in[offset++] & 0xFF) << 48)
529
       | ((long) (in[offset++] & 0xFF) << 40)
705
         | ((long) (in[offset++] & 0xFF) << 56);
530
       | ((long) (in[offset++] & 0xFF) << 48)
706
    x2 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
531
       | ((long) (in[offset++] & 0xFF) << 56);
707
         | ((long) (in[offset++] & 0xFF) << 16)
532
    x2 = ((long)  in[offset++] & 0xFF)
708
         | ((long) (in[offset++] & 0xFF) << 24)
533
       | ((long) (in[offset++] & 0xFF) << 8)
709
         | ((long) (in[offset++] & 0xFF) << 32)
534
       | ((long) (in[offset++] & 0xFF) << 16)
710
         | ((long) (in[offset++] & 0xFF) << 40)
535
       | ((long) (in[offset++] & 0xFF) << 24)
711
         | ((long) (in[offset++] & 0xFF) << 48)
536
       | ((long) (in[offset++] & 0xFF) << 32)
712
         | ((long) (in[offset++] & 0xFF) << 56);
537
       | ((long) (in[offset++] & 0xFF) << 40)
713
    x3 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
538
       | ((long) (in[offset++] & 0xFF) << 48)
714
         | ((long) (in[offset++] & 0xFF) << 16)
539
       | ((long) (in[offset++] & 0xFF) << 56);
715
         | ((long) (in[offset++] & 0xFF) << 24)
540
    x3 = ((long)  in[offset++] & 0xFF)
716
         | ((long) (in[offset++] & 0xFF) << 32)
541
       | ((long) (in[offset++] & 0xFF) << 8)
717
         | ((long) (in[offset++] & 0xFF) << 40)
542
       | ((long) (in[offset++] & 0xFF) << 16)
718
         | ((long) (in[offset++] & 0xFF) << 48)
543
       | ((long) (in[offset++] & 0xFF) << 24)
719
         | ((long) (in[offset++] & 0xFF) << 56);
544
       | ((long) (in[offset++] & 0xFF) << 32)
720
    x4 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
545
       | ((long) (in[offset++] & 0xFF) << 40)
721
         | ((long) (in[offset++] & 0xFF) << 16)
546
       | ((long) (in[offset++] & 0xFF) << 48)
722
         | ((long) (in[offset++] & 0xFF) << 24)
547
       | ((long) (in[offset++] & 0xFF) << 56);
723
         | ((long) (in[offset++] & 0xFF) << 32)
548
    x4 = ((long)  in[offset++] & 0xFF)
724
         | ((long) (in[offset++] & 0xFF) << 40)
549
       | ((long) (in[offset++] & 0xFF) << 8)
725
         | ((long) (in[offset++] & 0xFF) << 48)
550
       | ((long) (in[offset++] & 0xFF) << 16)
726
         | ((long) (in[offset++] & 0xFF) << 56);
551
       | ((long) (in[offset++] & 0xFF) << 24)
727
    x5 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
552
       | ((long) (in[offset++] & 0xFF) << 32)
728
         | ((long) (in[offset++] & 0xFF) << 16)
553
       | ((long) (in[offset++] & 0xFF) << 40)
729
         | ((long) (in[offset++] & 0xFF) << 24)
554
       | ((long) (in[offset++] & 0xFF) << 48)
730
         | ((long) (in[offset++] & 0xFF) << 32)
555
       | ((long) (in[offset++] & 0xFF) << 56);
731
         | ((long) (in[offset++] & 0xFF) << 40)
556
    x5 = ((long)  in[offset++] & 0xFF)
732
         | ((long) (in[offset++] & 0xFF) << 48)
557
       | ((long) (in[offset++] & 0xFF) << 8)
733
         | ((long) (in[offset++] & 0xFF) << 56);
558
       | ((long) (in[offset++] & 0xFF) << 16)
734
    x6 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
559
       | ((long) (in[offset++] & 0xFF) << 24)
735
         | ((long) (in[offset++] & 0xFF) << 16)
560
       | ((long) (in[offset++] & 0xFF) << 32)
736
         | ((long) (in[offset++] & 0xFF) << 24)
561
       | ((long) (in[offset++] & 0xFF) << 40)
737
         | ((long) (in[offset++] & 0xFF) << 32)
562
       | ((long) (in[offset++] & 0xFF) << 48)
738
         | ((long) (in[offset++] & 0xFF) << 40)
563
       | ((long) (in[offset++] & 0xFF) << 56);
739
         | ((long) (in[offset++] & 0xFF) << 48)
564
    x6 = ((long)  in[offset++] & 0xFF)
740
         | ((long) (in[offset++] & 0xFF) << 56);
565
       | ((long) (in[offset++] & 0xFF) << 8)
741
    x7 = ((long) in[offset++] & 0xFF) | ((long) (in[offset++] & 0xFF) << 8)
566
       | ((long) (in[offset++] & 0xFF) << 16)
742
         | ((long) (in[offset++] & 0xFF) << 16)
567
       | ((long) (in[offset++] & 0xFF) << 24)
743
         | ((long) (in[offset++] & 0xFF) << 24)
568
       | ((long) (in[offset++] & 0xFF) << 32)
744
         | ((long) (in[offset++] & 0xFF) << 32)
569
       | ((long) (in[offset++] & 0xFF) << 40)
745
         | ((long) (in[offset++] & 0xFF) << 40)
570
       | ((long) (in[offset++] & 0xFF) << 48)
746
         | ((long) (in[offset++] & 0xFF) << 48)
571
       | ((long) (in[offset++] & 0xFF) << 56);
747
         | ((long) (in[offset] & 0xFF) << 56);
572
    x7 = ((long)  in[offset++] & 0xFF)
748
573
       | ((long) (in[offset++] & 0xFF) << 8)
574
       | ((long) (in[offset++] & 0xFF) << 16)
575
       | ((long) (in[offset++] & 0xFF) << 24)
576
       | ((long) (in[offset++] & 0xFF) << 32)
577
       | ((long) (in[offset++] & 0xFF) << 40)
578
       | ((long) (in[offset++] & 0xFF) << 48)
579
       | ((long) (in[offset  ] & 0xFF) << 56);
749
    // save_abc ::=
580
    // save_abc ::=
750
    long aa = a, bb = b, cc = c;
581
    long aa = a, bb = b, cc = c;
751
752
    // pass(aa, bb, cc, 5) ::=
582
    // pass(aa, bb, cc, 5) ::=
753
    cc ^= x0;
583
    cc ^= x0;
754
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
584
    aa -= T1[(int) cc        & 0xff]
755
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
585
        ^ T2[(int)(cc >> 16) & 0xff]
756
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
586
        ^ T3[(int)(cc >> 32) & 0xff]
757
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
587
        ^ T4[(int)(cc >> 48) & 0xff];
588
    bb += T4[(int)(cc >>  8) & 0xff]
589
        ^ T3[(int)(cc >> 24) & 0xff]
590
        ^ T2[(int)(cc >> 40) & 0xff]
591
        ^ T1[(int)(cc >> 56) & 0xff];
758
    bb *= 5;
592
    bb *= 5;
759
    aa ^= x1;
593
    aa ^= x1;
760
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
594
    bb -= T1[(int) aa        & 0xff]
761
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
595
        ^ T2[(int)(aa >> 16) & 0xff]
762
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
596
        ^ T3[(int)(aa >> 32) & 0xff]
763
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
597
        ^ T4[(int)(aa >> 48) & 0xff];
598
    cc += T4[(int)(aa >>  8) & 0xff]
599
        ^ T3[(int)(aa >> 24) & 0xff]
600
        ^ T2[(int)(aa >> 40) & 0xff]
601
        ^ T1[(int)(aa >> 56) & 0xff];
764
    cc *= 5;
602
    cc *= 5;
765
    bb ^= x2;
603
    bb ^= x2;
766
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
604
    cc -= T1[(int) bb        & 0xff]
767
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
605
        ^ T2[(int)(bb >> 16) & 0xff]
768
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
606
        ^ T3[(int)(bb >> 32) & 0xff]
769
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
607
        ^ T4[(int)(bb >> 48) & 0xff];
608
    aa += T4[(int)(bb >>  8) & 0xff]
609
        ^ T3[(int)(bb >> 24) & 0xff]
610
        ^ T2[(int)(bb >> 40) & 0xff]
611
        ^ T1[(int)(bb >> 56) & 0xff];
770
    aa *= 5;
612
    aa *= 5;
771
    cc ^= x3;
613
    cc ^= x3;
772
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
614
    aa -= T1[(int) cc        & 0xff]
773
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
615
        ^ T2[(int)(cc >> 16) & 0xff]
774
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
616
        ^ T3[(int)(cc >> 32) & 0xff]
775
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
617
        ^ T4[(int)(cc >> 48) & 0xff];
618
    bb += T4[(int)(cc >>  8) & 0xff]
619
        ^ T3[(int)(cc >> 24) & 0xff]
620
        ^ T2[(int)(cc >> 40) & 0xff]
621
        ^ T1[(int)(cc >> 56) & 0xff];
776
    bb *= 5;
622
    bb *= 5;
777
    aa ^= x4;
623
    aa ^= x4;
778
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
624
    bb -= T1[(int) aa        & 0xff]
779
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
625
        ^ T2[(int)(aa >> 16) & 0xff]
780
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
626
        ^ T3[(int)(aa >> 32) & 0xff]
781
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
627
        ^ T4[(int)(aa >> 48) & 0xff];
628
    cc += T4[(int)(aa >>  8) & 0xff]
629
        ^ T3[(int)(aa >> 24) & 0xff]
630
        ^ T2[(int)(aa >> 40) & 0xff]
631
        ^ T1[(int)(aa >> 56) & 0xff];
782
    cc *= 5;
632
    cc *= 5;
783
    bb ^= x5;
633
    bb ^= x5;
784
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
634
    cc -= T1[(int) bb        & 0xff]
785
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
635
        ^ T2[(int)(bb >> 16) & 0xff]
786
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
636
        ^ T3[(int)(bb >> 32) & 0xff]
787
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
637
        ^ T4[(int)(bb >> 48) & 0xff];
638
    aa += T4[(int)(bb >>  8) & 0xff]
639
        ^ T3[(int)(bb >> 24) & 0xff]
640
        ^ T2[(int)(bb >> 40) & 0xff]
641
        ^ T1[(int)(bb >> 56) & 0xff];
788
    aa *= 5;
642
    aa *= 5;
789
    cc ^= x6;
643
    cc ^= x6;
790
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
644
    aa -= T1[(int) cc        & 0xff]
791
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
645
        ^ T2[(int)(cc >> 16) & 0xff]
792
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
646
        ^ T3[(int)(cc >> 32) & 0xff]
793
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
647
        ^ T4[(int)(cc >> 48) & 0xff];
648
    bb += T4[(int)(cc >>  8) & 0xff]
649
        ^ T3[(int)(cc >> 24) & 0xff]
650
        ^ T2[(int)(cc >> 40) & 0xff]
651
        ^ T1[(int)(cc >> 56) & 0xff];
794
    bb *= 5;
652
    bb *= 5;
795
    aa ^= x7;
653
    aa ^= x7;
796
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
654
    bb -= T1[(int) aa        & 0xff]
797
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
655
        ^ T2[(int)(aa >> 16) & 0xff]
798
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
656
        ^ T3[(int)(aa >> 32) & 0xff]
799
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
657
        ^ T4[(int)(aa >> 48) & 0xff];
658
    cc += T4[(int)(aa >>  8) & 0xff]
659
        ^ T3[(int)(aa >> 24) & 0xff]
660
        ^ T2[(int)(aa >> 40) & 0xff]
661
        ^ T1[(int)(aa >> 56) & 0xff];
800
    cc *= 5;
662
    cc *= 5;
801
802
    // key_schedule ::=
663
    // key_schedule ::=
803
    x0 -= x7 ^ 0xA5A5A5A5A5A5A5A5L;
664
    x0 -= x7 ^ 0xA5A5A5A5A5A5A5A5L;
804
    x1 ^= x0;
665
    x1 ^= x0;
Lines 816-872 Link Here
816
    x5 ^= x4;
677
    x5 ^= x4;
817
    x6 += x5;
678
    x6 += x5;
818
    x7 -= x6 ^ 0x0123456789ABCDEFL;
679
    x7 -= x6 ^ 0x0123456789ABCDEFL;
819
820
    // pass(cc, aa, bb, 7) ::=
680
    // pass(cc, aa, bb, 7) ::=
821
    bb ^= x0;
681
    bb ^= x0;
822
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
682
    cc -= T1[(int) bb        & 0xff]
823
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
683
        ^ T2[(int)(bb >> 16) & 0xff]
824
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
684
        ^ T3[(int)(bb >> 32) & 0xff]
825
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
685
        ^ T4[(int)(bb >> 48) & 0xff];
686
    aa += T4[(int)(bb >>  8) & 0xff]
687
        ^ T3[(int)(bb >> 24) & 0xff]
688
        ^ T2[(int)(bb >> 40) & 0xff]
689
        ^ T1[(int)(bb >> 56) & 0xff];
826
    aa *= 7;
690
    aa *= 7;
827
    cc ^= x1;
691
    cc ^= x1;
828
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
692
    aa -= T1[(int) cc        & 0xff]
829
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
693
        ^ T2[(int)(cc >> 16) & 0xff]
830
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
694
        ^ T3[(int)(cc >> 32) & 0xff]
831
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
695
        ^ T4[(int)(cc >> 48) & 0xff];
696
    bb += T4[(int)(cc >>  8) & 0xff]
697
        ^ T3[(int)(cc >> 24) & 0xff]
698
        ^ T2[(int)(cc >> 40) & 0xff]
699
        ^ T1[(int)(cc >> 56) & 0xff];
832
    bb *= 7;
700
    bb *= 7;
833
    aa ^= x2;
701
    aa ^= x2;
834
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
702
    bb -= T1[(int) aa        & 0xff]
835
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
703
        ^ T2[(int)(aa >> 16) & 0xff]
836
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
704
        ^ T3[(int)(aa >> 32) & 0xff]
837
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
705
        ^ T4[(int)(aa >> 48) & 0xff];
706
    cc += T4[(int)(aa >>  8) & 0xff]
707
        ^ T3[(int)(aa >> 24) & 0xff]
708
        ^ T2[(int)(aa >> 40) & 0xff]
709
        ^ T1[(int)(aa >> 56) & 0xff];
838
    cc *= 7;
710
    cc *= 7;
839
    bb ^= x3;
711
    bb ^= x3;
840
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
712
    cc -= T1[(int) bb        & 0xff]
841
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
713
        ^ T2[(int)(bb >> 16) & 0xff]
842
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
714
        ^ T3[(int)(bb >> 32) & 0xff]
843
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
715
        ^ T4[(int)(bb >> 48) & 0xff];
716
    aa += T4[(int)(bb >>  8) & 0xff]
717
        ^ T3[(int)(bb >> 24) & 0xff]
718
        ^ T2[(int)(bb >> 40) & 0xff]
719
        ^ T1[(int)(bb >> 56) & 0xff];
844
    aa *= 7;
720
    aa *= 7;
845
    cc ^= x4;
721
    cc ^= x4;
846
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
722
    aa -= T1[(int) cc        & 0xff]
847
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
723
        ^ T2[(int)(cc >> 16) & 0xff]
848
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
724
        ^ T3[(int)(cc >> 32) & 0xff]
849
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
725
        ^ T4[(int)(cc >> 48) & 0xff];
726
    bb += T4[(int)(cc >>  8) & 0xff]
727
        ^ T3[(int)(cc >> 24) & 0xff]
728
        ^ T2[(int)(cc >> 40) & 0xff]
729
        ^ T1[(int)(cc >> 56) & 0xff];
850
    bb *= 7;
730
    bb *= 7;
851
    aa ^= x5;
731
    aa ^= x5;
852
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
732
    bb -= T1[(int) aa        & 0xff]
853
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
733
        ^ T2[(int)(aa >> 16) & 0xff]
854
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
734
        ^ T3[(int)(aa >> 32) & 0xff]
855
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
735
        ^ T4[(int)(aa >> 48) & 0xff];
736
    cc += T4[(int)(aa >>  8) & 0xff]
737
        ^ T3[(int)(aa >> 24) & 0xff]
738
        ^ T2[(int)(aa >> 40) & 0xff]
739
        ^ T1[(int)(aa >> 56) & 0xff];
856
    cc *= 7;
740
    cc *= 7;
857
    bb ^= x6;
741
    bb ^= x6;
858
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
742
    cc -= T1[(int) bb        & 0xff]
859
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
743
        ^ T2[(int)(bb >> 16) & 0xff]
860
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
744
        ^ T3[(int)(bb >> 32) & 0xff]
861
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
745
        ^ T4[(int)(bb >> 48) & 0xff];
746
    aa += T4[(int)(bb >>  8) & 0xff]
747
        ^ T3[(int)(bb >> 24) & 0xff]
748
        ^ T2[(int)(bb >> 40) & 0xff]
749
        ^ T1[(int)(bb >> 56) & 0xff];
862
    aa *= 7;
750
    aa *= 7;
863
    cc ^= x7;
751
    cc ^= x7;
864
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
752
    aa -= T1[(int) cc        & 0xff]
865
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
753
        ^ T2[(int)(cc >> 16) & 0xff]
866
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
754
        ^ T3[(int)(cc >> 32) & 0xff]
867
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
755
        ^ T4[(int)(cc >> 48) & 0xff];
756
    bb += T4[(int)(cc >>  8) & 0xff]
757
        ^ T3[(int)(cc >> 24) & 0xff]
758
        ^ T2[(int)(cc >> 40) & 0xff]
759
        ^ T1[(int)(cc >> 56) & 0xff];
868
    bb *= 7;
760
    bb *= 7;
869
870
    // key_schedule ::=
761
    // key_schedule ::=
871
    x0 -= x7 ^ 0xA5A5A5A5A5A5A5A5L;
762
    x0 -= x7 ^ 0xA5A5A5A5A5A5A5A5L;
872
    x1 ^= x0;
763
    x1 ^= x0;
Lines 884-940 Link Here
884
    x5 ^= x4;
775
    x5 ^= x4;
885
    x6 += x5;
776
    x6 += x5;
886
    x7 -= x6 ^ 0x0123456789ABCDEFL;
777
    x7 -= x6 ^ 0x0123456789ABCDEFL;
887
888
    // pass(bb,cc,aa,9) ::=
778
    // pass(bb,cc,aa,9) ::=
889
    aa ^= x0;
779
    aa ^= x0;
890
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
780
    bb -= T1[(int) aa        & 0xff]
891
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
781
        ^ T2[(int)(aa >> 16) & 0xff]
892
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
782
        ^ T3[(int)(aa >> 32) & 0xff]
893
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
783
        ^ T4[(int)(aa >> 48) & 0xff];
784
    cc += T4[(int)(aa >>  8) & 0xff]
785
        ^ T3[(int)(aa >> 24) & 0xff]
786
        ^ T2[(int)(aa >> 40) & 0xff]
787
        ^ T1[(int)(aa >> 56) & 0xff];
894
    cc *= 9;
788
    cc *= 9;
895
    bb ^= x1;
789
    bb ^= x1;
896
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
790
    cc -= T1[(int) bb        & 0xff]
897
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
791
        ^ T2[(int)(bb >> 16) & 0xff]
898
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
792
        ^ T3[(int)(bb >> 32) & 0xff]
899
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
793
        ^ T4[(int)(bb >> 48) & 0xff];
794
    aa += T4[(int)(bb >>  8) & 0xff]
795
        ^ T3[(int)(bb >> 24) & 0xff]
796
        ^ T2[(int)(bb >> 40) & 0xff]
797
        ^ T1[(int)(bb >> 56) & 0xff];
900
    aa *= 9;
798
    aa *= 9;
901
    cc ^= x2;
799
    cc ^= x2;
902
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
800
    aa -= T1[(int) cc        & 0xff]
903
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
801
        ^ T2[(int)(cc >> 16) & 0xff]
904
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
802
        ^ T3[(int)(cc >> 32) & 0xff]
905
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
803
        ^ T4[(int)(cc >> 48) & 0xff];
804
    bb += T4[(int)(cc >>  8) & 0xff]
805
        ^ T3[(int)(cc >> 24) & 0xff]
806
        ^ T2[(int)(cc >> 40) & 0xff]
807
        ^ T1[(int)(cc >> 56) & 0xff];
906
    bb *= 9;
808
    bb *= 9;
907
    aa ^= x3;
809
    aa ^= x3;
908
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
810
    bb -= T1[(int) aa        & 0xff]
909
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
811
        ^ T2[(int)(aa >> 16) & 0xff]
910
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
812
        ^ T3[(int)(aa >> 32) & 0xff]
911
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
813
        ^ T4[(int)(aa >> 48) & 0xff];
814
    cc += T4[(int)(aa >>  8) & 0xff]
815
        ^ T3[(int)(aa >> 24) & 0xff]
816
        ^ T2[(int)(aa >> 40) & 0xff]
817
        ^ T1[(int)(aa >> 56) & 0xff];
912
    cc *= 9;
818
    cc *= 9;
913
    bb ^= x4;
819
    bb ^= x4;
914
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
820
    cc -= T1[(int) bb        & 0xff]
915
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
821
        ^ T2[(int)(bb >> 16) & 0xff]
916
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
822
        ^ T3[(int)(bb >> 32) & 0xff]
917
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
823
        ^ T4[(int)(bb >> 48) & 0xff];
824
    aa += T4[(int)(bb >>  8) & 0xff]
825
        ^ T3[(int)(bb >> 24) & 0xff]
826
        ^ T2[(int)(bb >> 40) & 0xff]
827
        ^ T1[(int)(bb >> 56) & 0xff];
918
    aa *= 9;
828
    aa *= 9;
919
    cc ^= x5;
829
    cc ^= x5;
920
    aa -= T1[(int) cc & 0xff] ^ T2[(int) (cc >> 16) & 0xff]
830
    aa -= T1[(int) cc        & 0xff]
921
          ^ T3[(int) (cc >> 32) & 0xff] ^ T4[(int) (cc >> 48) & 0xff];
831
        ^ T2[(int)(cc >> 16) & 0xff]
922
    bb += T4[(int) (cc >> 8) & 0xff] ^ T3[(int) (cc >> 24) & 0xff]
832
        ^ T3[(int)(cc >> 32) & 0xff]
923
          ^ T2[(int) (cc >> 40) & 0xff] ^ T1[(int) (cc >> 56) & 0xff];
833
        ^ T4[(int)(cc >> 48) & 0xff];
834
    bb += T4[(int)(cc >>  8) & 0xff]
835
        ^ T3[(int)(cc >> 24) & 0xff]
836
        ^ T2[(int)(cc >> 40) & 0xff]
837
        ^ T1[(int)(cc >> 56) & 0xff];
924
    bb *= 9;
838
    bb *= 9;
925
    aa ^= x6;
839
    aa ^= x6;
926
    bb -= T1[(int) aa & 0xff] ^ T2[(int) (aa >> 16) & 0xff]
840
    bb -= T1[(int) aa        & 0xff]
927
          ^ T3[(int) (aa >> 32) & 0xff] ^ T4[(int) (aa >> 48) & 0xff];
841
        ^ T2[(int)(aa >> 16) & 0xff]
928
    cc += T4[(int) (aa >> 8) & 0xff] ^ T3[(int) (aa >> 24) & 0xff]
842
        ^ T3[(int)(aa >> 32) & 0xff]
929
          ^ T2[(int) (aa >> 40) & 0xff] ^ T1[(int) (aa >> 56) & 0xff];
843
        ^ T4[(int)(aa >> 48) & 0xff];
844
    cc += T4[(int)(aa >>  8) & 0xff]
845
        ^ T3[(int)(aa >> 24) & 0xff]
846
        ^ T2[(int)(aa >> 40) & 0xff]
847
        ^ T1[(int)(aa >> 56) & 0xff];
930
    cc *= 9;
848
    cc *= 9;
931
    bb ^= x7;
849
    bb ^= x7;
932
    cc -= T1[(int) bb & 0xff] ^ T2[(int) (bb >> 16) & 0xff]
850
    cc -= T1[(int) bb        & 0xff]
933
          ^ T3[(int) (bb >> 32) & 0xff] ^ T4[(int) (bb >> 48) & 0xff];
851
        ^ T2[(int)(bb >> 16) & 0xff]
934
    aa += T4[(int) (bb >> 8) & 0xff] ^ T3[(int) (bb >> 24) & 0xff]
852
        ^ T3[(int)(bb >> 32) & 0xff]
935
          ^ T2[(int) (bb >> 40) & 0xff] ^ T1[(int) (bb >> 56) & 0xff];
853
        ^ T4[(int)(bb >> 48) & 0xff];
854
    aa += T4[(int)(bb >>  8) & 0xff]
855
        ^ T3[(int)(bb >> 24) & 0xff]
856
        ^ T2[(int)(bb >> 40) & 0xff]
857
        ^ T1[(int)(bb >> 56) & 0xff];
936
    aa *= 9;
858
    aa *= 9;
937
938
    // feedforward ::=
859
    // feedforward ::=
939
    a ^= aa;
860
    a ^= aa;
940
    b = bb - b;
861
    b = bb - b;
(-)Whirlpool.java (-54 / +25 lines)
Lines 62-68 Link Here
62
 *    <a href="mailto:vincent.rijmen@iaik.tugraz.at">Vincent Rijmen</a>.</li>
62
 *    <a href="mailto:vincent.rijmen@iaik.tugraz.at">Vincent Rijmen</a>.</li>
63
 * </ol>
63
 * </ol>
64
 */
64
 */
65
public final class Whirlpool extends BaseHash
65
public final class Whirlpool
66
    extends BaseHash
66
{
67
{
67
  private static final Logger log = Logger.getLogger(Whirlpool.class.getName());
68
  private static final Logger log = Logger.getLogger(Whirlpool.class.getName());
68
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
69
  private static final int BLOCK_SIZE = 64; // inner block size in bytes
Lines 77-98 Link Here
77
78
78
  /** Whirlpool S-box; p. 19. */
79
  /** Whirlpool S-box; p. 19. */
79
  private static final String S_box = // p. 19 [WHIRLPOOL]
80
  private static final String S_box = // p. 19 [WHIRLPOOL]
80
      "\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152" +
81
      "\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152"
81
      "\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57" +
82
    + "\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57"
82
      "\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85" +
83
    + "\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85"
83
      "\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8" +
84
    + "\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8"
84
      "\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333" +
85
    + "\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333"
85
      "\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0" +
86
    + "\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0"
86
      "\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE" +
87
    + "\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE"
87
      "\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d" +
88
    + "\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d"
88
      "\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF" +
89
    + "\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF"
89
      "\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A" +
90
    + "\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A"
90
      "\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c" +
91
    + "\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c"
91
      "\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04" +
92
    + "\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04"
92
      "\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB" +
93
    + "\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB"
93
      "\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9" +
94
    + "\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9"
94
      "\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1" +
95
    + "\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1"
95
      "\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
96
    + "\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
96
97
97
  /** The 64-bit lookup tables; section 7.1 p. 13. */
98
  /** The 64-bit lookup tables; section 7.1 p. 13. */
98
  private static final long[] T0 = new long[256];
99
  private static final long[] T0 = new long[256];
Lines 124-135 Link Here
124
  /** work area for holding block cipher's intermediate values. */
125
  /** work area for holding block cipher's intermediate values. */
125
  private long w0, w1, w2, w3, w4, w5, w6, w7;
126
  private long w0, w1, w2, w3, w4, w5, w6, w7;
126
127
127
  // Static code - to intialise lookup tables --------------------------------
128
129
  static
128
  static
130
    {
129
    {
131
      long time = System.currentTimeMillis();
130
      long time = System.currentTimeMillis();
132
133
      int ROOT = 0x11D; // para. 2.1 [WHIRLPOOL]
131
      int ROOT = 0x11D; // para. 2.1 [WHIRLPOOL]
134
      int i, r, j;
132
      int i, r, j;
135
      long s1, s2, s4, s5, s8, s9, t;
133
      long s1, s2, s4, s5, s8, s9, t;
Lines 165-171 Link Here
165
          T6[i] = t >>> 48 | t << 16;
163
          T6[i] = t >>> 48 | t << 16;
166
          T7[i] = t >>> 56 | t <<  8;
164
          T7[i] = t >>> 56 | t <<  8;
167
        }
165
        }
168
169
      for (r = 0, i = 0; r < R; )
166
      for (r = 0, i = 0; r < R; )
170
        rc[r++] = (T0[i++] & 0xFF00000000000000L)
167
        rc[r++] = (T0[i++] & 0xFF00000000000000L)
171
                ^ (T1[i++] & 0x00FF000000000000L)
168
                ^ (T1[i++] & 0x00FF000000000000L)
Lines 175-181 Link Here
175
                ^ (T5[i++] & 0x0000000000FF0000L)
172
                ^ (T5[i++] & 0x0000000000FF0000L)
176
                ^ (T6[i++] & 0x000000000000FF00L)
173
                ^ (T6[i++] & 0x000000000000FF00L)
177
                ^ (T7[i++] & 0x00000000000000FFL);
174
                ^ (T7[i++] & 0x00000000000000FFL);
178
179
      time = System.currentTimeMillis() - time;
175
      time = System.currentTimeMillis() - time;
180
      if (Configuration.DEBUG)
176
      if (Configuration.DEBUG)
181
        {
177
        {
Lines 261-269 Link Here
261
        }
257
        }
262
    }
258
    }
263
259
264
  // Constructor(s)
265
  // -------------------------------------------------------------------------
266
267
  /** Trivial 0-arguments constructor. */
260
  /** Trivial 0-arguments constructor. */
268
  public Whirlpool()
261
  public Whirlpool()
269
  {
262
  {
Lines 271-277 Link Here
271
  }
264
  }
272
265
273
  /**
266
  /**
274
   * <p>Private constructor for cloning purposes.</p>
267
   * Private constructor for cloning purposes.
275
   *
268
   *
276
   * @param md the instance to clone.
269
   * @param md the instance to clone.
277
   */
270
   */
Lines 291-311 Link Here
291
    this.buffer = (byte[]) md.buffer.clone();
284
    this.buffer = (byte[]) md.buffer.clone();
292
  }
285
  }
293
286
294
  // Class methods
295
  // -------------------------------------------------------------------------
296
297
  // Instance methods
298
  // -------------------------------------------------------------------------
299
300
  // java.lang.Cloneable interface implementation ----------------------------
301
302
  public Object clone()
287
  public Object clone()
303
  {
288
  {
304
    return (new Whirlpool(this));
289
    return (new Whirlpool(this));
305
  }
290
  }
306
291
307
  // Implementation of concrete methods in BaseHash --------------------------
308
309
  protected void transform(byte[] in, int offset)
292
  protected void transform(byte[] in, int offset)
310
  {
293
  {
311
    // apply mu to the input
294
    // apply mu to the input
Lines 373-379 Link Here
373
       | (in[offset++] & 0xFFL) << 16
356
       | (in[offset++] & 0xFFL) << 16
374
       | (in[offset++] & 0xFFL) <<  8
357
       | (in[offset++] & 0xFFL) <<  8
375
       | (in[offset++] & 0xFFL);
358
       | (in[offset++] & 0xFFL);
376
377
    // transform K into the key schedule Kr; 0 <= r <= R
359
    // transform K into the key schedule Kr; 0 <= r <= R
378
    k00 = H0;
360
    k00 = H0;
379
    k01 = H1;
361
    k01 = H1;
Lines 383-389 Link Here
383
    k05 = H5;
365
    k05 = H5;
384
    k06 = H6;
366
    k06 = H6;
385
    k07 = H7;
367
    k07 = H7;
386
387
    nn0 = n0 ^ k00;
368
    nn0 = n0 ^ k00;
388
    nn1 = n1 ^ k01;
369
    nn1 = n1 ^ k01;
389
    nn2 = n2 ^ k02;
370
    nn2 = n2 ^ k02;
Lines 392-401 Link Here
392
    nn5 = n5 ^ k05;
373
    nn5 = n5 ^ k05;
393
    nn6 = n6 ^ k06;
374
    nn6 = n6 ^ k06;
394
    nn7 = n7 ^ k07;
375
    nn7 = n7 ^ k07;
395
396
    // intermediate cipher output
376
    // intermediate cipher output
397
    w0 = w1 = w2 = w3 = w4 = w5 = w6 = w7 = 0L;
377
    w0 = w1 = w2 = w3 = w4 = w5 = w6 = w7 = 0L;
398
399
    for (int r = 0; r < R; r++)
378
    for (int r = 0; r < R; r++)
400
      {
379
      {
401
        // 1. compute intermediate round key schedule by applying ro[rc]
380
        // 1. compute intermediate round key schedule by applying ro[rc]
Lines 464-470 Link Here
464
            ^ T5[(int)((k02 >> 16) & 0xFFL)]
443
            ^ T5[(int)((k02 >> 16) & 0xFFL)]
465
            ^ T6[(int)((k01 >>  8) & 0xFFL)]
444
            ^ T6[(int)((k01 >>  8) & 0xFFL)]
466
            ^ T7[(int)( k00        & 0xFFL)];
445
            ^ T7[(int)( k00        & 0xFFL)];
467
468
        k00 = Kr0;
446
        k00 = Kr0;
469
        k01 = Kr1;
447
        k01 = Kr1;
470
        k02 = Kr2;
448
        k02 = Kr2;
Lines 473-479 Link Here
473
        k05 = Kr5;
451
        k05 = Kr5;
474
        k06 = Kr6;
452
        k06 = Kr6;
475
        k07 = Kr7;
453
        k07 = Kr7;
476
477
        // 2. incrementally compute the cipher output
454
        // 2. incrementally compute the cipher output
478
        w0 = T0[(int)((nn0 >> 56) & 0xFFL)]
455
        w0 = T0[(int)((nn0 >> 56) & 0xFFL)]
479
           ^ T1[(int)((nn7 >> 48) & 0xFFL)]
456
           ^ T1[(int)((nn7 >> 48) & 0xFFL)]
Lines 539-545 Link Here
539
           ^ T5[(int)((nn2 >> 16) & 0xFFL)]
516
           ^ T5[(int)((nn2 >> 16) & 0xFFL)]
540
           ^ T6[(int)((nn1 >>  8) & 0xFFL)]
517
           ^ T6[(int)((nn1 >>  8) & 0xFFL)]
541
           ^ T7[(int)( nn0        & 0xFFL)] ^ Kr7;
518
           ^ T7[(int)( nn0        & 0xFFL)] ^ Kr7;
542
543
        nn0 = w0;
519
        nn0 = w0;
544
        nn1 = w1;
520
        nn1 = w1;
545
        nn2 = w2;
521
        nn2 = w2;
Lines 549-555 Link Here
549
        nn6 = w6;
525
        nn6 = w6;
550
        nn7 = w7;
526
        nn7 = w7;
551
      }
527
      }
552
553
    // apply the Miyaguchi-Preneel hash scheme
528
    // apply the Miyaguchi-Preneel hash scheme
554
    H0 ^= w0 ^ n0;
529
    H0 ^= w0 ^ n0;
555
    H1 ^= w1 ^ n1;
530
    H1 ^= w1 ^ n1;
Lines 574-585 Link Here
574
    //		count + 33 + padding = 0 (mod BLOCK_SIZE)
549
    //		count + 33 + padding = 0 (mod BLOCK_SIZE)
575
    int n = (int)((count + 33) % BLOCK_SIZE);
550
    int n = (int)((count + 33) % BLOCK_SIZE);
576
    int padding = n == 0 ? 33 : BLOCK_SIZE - n + 33;
551
    int padding = n == 0 ? 33 : BLOCK_SIZE - n + 33;
577
578
    byte[] result = new byte[padding];
552
    byte[] result = new byte[padding];
579
580
    // padding is always binary 1 followed by binary 0s
553
    // padding is always binary 1 followed by binary 0s
581
    result[0] = (byte) 0x80;
554
    result[0] = (byte) 0x80;
582
583
    // save (right justified) the number of bits hashed
555
    // save (right justified) the number of bits hashed
584
    long bits = count * 8;
556
    long bits = count * 8;
585
    int i = padding - 8;
557
    int i = padding - 8;
Lines 591-604 Link Here
591
    result[i++] = (byte)(bits >>> 16);
563
    result[i++] = (byte)(bits >>> 16);
592
    result[i++] = (byte)(bits >>>  8);
564
    result[i++] = (byte)(bits >>>  8);
593
    result[i  ] = (byte) bits;
565
    result[i  ] = (byte) bits;
594
595
    return result;
566
    return result;
596
  }
567
  }
597
568
598
  protected byte[] getResult()
569
  protected byte[] getResult()
599
  {
570
  {
600
    // apply inverse mu to the context
571
    // apply inverse mu to the context
601
    byte[] result = new byte[] {
572
    return new byte[] {
602
      (byte)(H0 >>> 56), (byte)(H0 >>> 48), (byte)(H0 >>> 40), (byte)(H0 >>> 32),
573
      (byte)(H0 >>> 56), (byte)(H0 >>> 48), (byte)(H0 >>> 40), (byte)(H0 >>> 32),
603
      (byte)(H0 >>> 24), (byte)(H0 >>> 16), (byte)(H0 >>>  8), (byte) H0,
574
      (byte)(H0 >>> 24), (byte)(H0 >>> 16), (byte)(H0 >>>  8), (byte) H0,
604
      (byte)(H1 >>> 56), (byte)(H1 >>> 48), (byte)(H1 >>> 40), (byte)(H1 >>> 32),
575
      (byte)(H1 >>> 56), (byte)(H1 >>> 48), (byte)(H1 >>> 40), (byte)(H1 >>> 32),
Lines 614-623 Link Here
614
      (byte)(H6 >>> 56), (byte)(H6 >>> 48), (byte)(H6 >>> 40), (byte)(H6 >>> 32),
585
      (byte)(H6 >>> 56), (byte)(H6 >>> 48), (byte)(H6 >>> 40), (byte)(H6 >>> 32),
615
      (byte)(H6 >>> 24), (byte)(H6 >>> 16), (byte)(H6 >>>  8), (byte) H6,
586
      (byte)(H6 >>> 24), (byte)(H6 >>> 16), (byte)(H6 >>>  8), (byte) H6,
616
      (byte)(H7 >>> 56), (byte)(H7 >>> 48), (byte)(H7 >>> 40), (byte)(H7 >>> 32),
587
      (byte)(H7 >>> 56), (byte)(H7 >>> 48), (byte)(H7 >>> 40), (byte)(H7 >>> 32),
617
      (byte)(H7 >>> 24), (byte)(H7 >>> 16), (byte)(H7 >>>  8), (byte) H7
588
      (byte)(H7 >>> 24), (byte)(H7 >>> 16), (byte)(H7 >>>  8), (byte) H7 };
618
    };
619
589
620
    return result;
621
  }
590
  }
622
591
623
  protected void resetContext()
592
  protected void resetContext()
Lines 628-635 Link Here
628
  public boolean selfTest()
597
  public boolean selfTest()
629
  {
598
  {
630
    if (valid == null)
599
    if (valid == null)
631
      valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new Whirlpool().digest())));
600
      {
632
601
        String d = Util.toString(new Whirlpool().digest());
602
        valid = Boolean.valueOf(DIGEST0.equals(d));
603
      }
633
    return valid.booleanValue();
604
    return valid.booleanValue();
634
  }
605
  }
635
}
606
}

Return to bug 26067