1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/* MD2.java --
Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
This file is a part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.security.hash;
import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>An implementation of the MD2 message digest algorithm.</p>
*
* <p>MD2 is not widely used. Unless it is needed for compatibility with
* existing systems, it is not recommended for use in new applications.</p>
*
* <p>References:</p>
*
* <ol>
* <li>The <a href="http://www.ietf.org/rfc/rfc1319.txt">MD2</a>
* Message-Digest Algorithm.<br>
* B. Kaliski.</li>
* <li>The <a href="http://www.rfc-editor.org/errata.html">RFC ERRATA PAGE</a>
* under section RFC 1319.</li>
* </ol>
*/
public class MD2 extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
/** An MD2 message digest is always 128-bits long, or 16 bytes. */
private static final int DIGEST_LENGTH = 16;
/** The MD2 algorithm operates on 128-bit blocks, or 16 bytes. */
private static final int BLOCK_LENGTH = 16;
/** 256 byte "random" permutation of the digits of pi. */
private static final byte[] PI = { 41, 46, 67, -55, -94, -40, 124, 1, 61, 54,
84, -95, -20, -16, 6, 19, 98, -89, 5, -13,
-64, -57, 115, -116, -104, -109, 43, -39,
-68, 76, -126, -54, 30, -101, 87, 60, -3,
-44, -32, 22, 103, 66, 111, 24, -118, 23,
-27, 18, -66, 78, -60, -42, -38, -98, -34,
73, -96, -5, -11, -114, -69, 47, -18, 122,
-87, 104, 121, -111, 21, -78, 7, 63, -108,
-62, 16, -119, 11, 34, 95, 33, -128, 127,
93, -102, 90, -112, 50, 39, 53, 62, -52,
-25, -65, -9, -105, 3, -1, 25, 48, -77, 72,
-91, -75, -47, -41, 94, -110, 42, -84, 86,
-86, -58, 79, -72, 56, -46, -106, -92, 125,
-74, 118, -4, 107, -30, -100, 116, 4, -15,
69, -99, 112, 89, 100, 113, -121, 32, -122,
91, -49, 101, -26, 45, -88, 2, 27, 96, 37,
-83, -82, -80, -71, -10, 28, 70, 97, 105,
52, 64, 126, 15, 85, 71, -93, 35, -35, 81,
-81, 58, -61, 92, -7, -50, -70, -59, -22,
38, 44, 83, 13, 110, -123, 40, -124, 9,
-45, -33, -51, -12, 65, -127, 77, 82, 106,
-36, 55, -56, 108, -63, -85, -6, 36, -31,
123, 8, 12, -67, -79, 74, 120, -120, -107,
-117, -29, 99, -24, 109, -23, -53, -43, -2,
59, 0, 29, 57, -14, -17, -73, 14, 102, 88,
-48, -28, -90, 119, 114, -8, -21, 117, 75,
10, 49, 68, 80, -76, -113, -19, 31, 26,
-37, -103, -115, 51, -97, 17, -125, 20 };
/** The output of this message digest when no data has been input. */
private static final String DIGEST0 = "8350E5A3E24C153DF2275C9F80692773";
/** caches the result of the correctness test, once executed. */
private static Boolean valid;
/** The checksum computed so far. */
private byte[] checksum;
/**
* Work array needed by encrypt method. First <code>BLOCK_LENGTH</code> bytes
* are also used to store the running digest.
*/
private byte[] work;
// Constructor(s)
// -------------------------------------------------------------------------
/** Creates a new MD2 digest ready for use. */
public MD2()
{
super(Registry.MD2_HASH, DIGEST_LENGTH, BLOCK_LENGTH);
}
/**
* <p>Private constructor used for cloning.</p>
*
* @param md2 the instance to clone.
*/
private MD2(MD2 md2)
{
this();
// superclass field
this.count = md2.count;
this.buffer = (byte[]) md2.buffer.clone();
// private field
this.checksum = (byte[]) md2.checksum.clone();
this.work = (byte[]) md2.work.clone();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new MD2(this);
}
// Implementation of abstract methods in BaseHash --------------------------
protected byte[] getResult()
{
byte[] result = new byte[DIGEST_LENGTH];
// Encrypt checksum as last block.
encryptBlock(checksum, 0);
for (int i = 0; i < BLOCK_LENGTH; i++)
{
result[i] = work[i];
}
return result;
}
protected void resetContext()
{
checksum = new byte[BLOCK_LENGTH];
work = new byte[BLOCK_LENGTH * 3];
}
public boolean selfTest()
{
if (valid == null)
{
valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD2().digest())));
}
return valid.booleanValue();
}
/**
* <p>Generates an array of padding bytes. The padding is defined as
* <code>i</code> bytes of value <code>i</code>, where <code>i</code> is the
* number of bytes to fill the last block of the message to
* <code>BLOCK_LENGTH</code> bytes (or <code>BLOCK_LENGTH</code> bytes when
* the last block was completely full).</p>
*
* @return the bytes to pad the remaining bytes in the buffer before
* completing a hash operation.
*/
protected byte[] padBuffer()
{
int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH);
if (length == 0)
{
length = BLOCK_LENGTH;
}
byte[] pad = new byte[length];
for (int i = 0; i < length; i++)
{
pad[i] = (byte) length;
}
return pad;
}
/**
* <p>Adds <code>BLOCK_LENGTH</code> bytes to the running digest.</p>
*
* @param in the byte array to take the <code>BLOCK_LENGTH</code> bytes from.
* @param off the offset to start from in the given byte array.
*/
protected void transform(byte[] in, int off)
{
// encryptBlock(in, off);
// updateCheckSum(in, off);
updateCheckSumAndEncryptBlock(in, off);
}
// Private instance methods ------------------------------------------------
/**
* Updates the checksum with the <code>BLOCK_LENGTH</code> bytes from the
* given array starting at <code>off</code>.
*/
/*
private void updateCheckSum(byte[] in, int off) {
byte l = checksum[BLOCK_LENGTH-1];
for (int i = 0; i < BLOCK_LENGTH; i++) {
byte b = in[off+i];
// l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF));
l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]);
checksum[i] = l;
}
}
*/
/**
* Adds a new block (<code>BLOCK_LENGTH</code> bytes) to the running digest
* from the given byte array starting from the given offset.
*/
private void encryptBlock(byte[] in, int off)
{
for (int i = 0; i < BLOCK_LENGTH; i++)
{
byte b = in[off + i];
work[BLOCK_LENGTH + i] = b;
work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b);
}
byte t = 0;
for (int i = 0; i < 18; i++)
{
for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
{
// t = (byte)((work[j] & 0xFF) ^ (PI[t & 0xFF] & 0xFF));
t = (byte) (work[j] ^ PI[t & 0xFF]);
work[j] = t;
}
// t = (byte)((t + i) & 0xFF);
t = (byte) (t + i);
}
}
/**
* Optimized method that combines a checksum update and encrypt of a block.
*/
private void updateCheckSumAndEncryptBlock(byte[] in, int off)
{
byte l = checksum[BLOCK_LENGTH - 1];
for (int i = 0; i < BLOCK_LENGTH; i++)
{
byte b = in[off + i];
work[BLOCK_LENGTH + i] = b;
// work[BLOCK_LENGTH*2+i] = (byte)((work[i] & 0xFF) ^ (b & 0xFF));
work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b);
// l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF));
l = (byte) (checksum[i] ^ PI[(b ^ l) & 0xFF]);
checksum[i] = l;
}
byte t = 0;
for (int i = 0; i < 18; i++)
{
for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
{
// t = (byte)((work[j] & 0xFF) ^ (PI[t & 0xFF] & 0xFF));
t = (byte) (work[j] ^ PI[t & 0xFF]);
work[j] = t;
}
// t = (byte)((t + i) & 0xFF);
t = (byte) (t + i);
}
}
}
|