0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport static sun.security.provider.ByteArrayAccess.*;
0N/A
0N/A/**
0N/A * The MD4 class is used to compute an MD4 message digest over a given
0N/A * buffer of bytes. It is an implementation of the RSA Data Security Inc
0N/A * MD4 algorithim as described in internet RFC 1320.
0N/A *
0N/A * <p>The MD4 algorithm is very weak and should not be used unless it is
0N/A * unavoidable. Therefore, it is not registered in our standard providers. To
0N/A * obtain an implementation, call the static getInstance() method in this
0N/A * class.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A */
0N/Apublic final class MD4 extends DigestBase {
0N/A
0N/A // state of this object
0N/A private final int[] state;
0N/A // temporary buffer, used by implCompress()
0N/A private final int[] x;
0N/A
0N/A // rotation constants
0N/A private static final int S11 = 3;
0N/A private static final int S12 = 7;
0N/A private static final int S13 = 11;
0N/A private static final int S14 = 19;
0N/A private static final int S21 = 3;
0N/A private static final int S22 = 5;
0N/A private static final int S23 = 9;
0N/A private static final int S24 = 13;
0N/A private static final int S31 = 3;
0N/A private static final int S32 = 9;
0N/A private static final int S33 = 11;
0N/A private static final int S34 = 15;
0N/A
0N/A private final static Provider md4Provider;
0N/A
0N/A static {
0N/A md4Provider = new Provider("MD4Provider", 1.0d, "MD4 MessageDigest") {};
0N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
0N/A public Void run() {
0N/A md4Provider.put("MessageDigest.MD4", "sun.security.provider.MD4");
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A
0N/A public static MessageDigest getInstance() {
0N/A try {
0N/A return MessageDigest.getInstance("MD4", md4Provider);
0N/A } catch (NoSuchAlgorithmException e) {
0N/A // should never occur
0N/A throw new ProviderException(e);
0N/A }
0N/A }
0N/A
0N/A // Standard constructor, creates a new MD4 instance.
0N/A public MD4() {
0N/A super("MD4", 16, 64);
0N/A state = new int[4];
0N/A x = new int[16];
0N/A implReset();
0N/A }
0N/A
0N/A // Cloning constructor
0N/A private MD4(MD4 base) {
0N/A super(base);
0N/A this.state = base.state.clone();
0N/A this.x = new int[16];
0N/A }
0N/A
0N/A // clone this object
0N/A public Object clone() {
0N/A return new MD4(this);
0N/A }
0N/A
0N/A /**
0N/A * Reset the state of this object.
0N/A */
0N/A void implReset() {
0N/A // Load magic initialization constants.
0N/A state[0] = 0x67452301;
0N/A state[1] = 0xefcdab89;
0N/A state[2] = 0x98badcfe;
0N/A state[3] = 0x10325476;
0N/A }
0N/A
0N/A /**
0N/A * Perform the final computations, any buffered bytes are added
0N/A * to the digest, the count is added to the digest, and the resulting
0N/A * digest is stored.
0N/A */
0N/A void implDigest(byte[] out, int ofs) {
0N/A long bitsProcessed = bytesProcessed << 3;
0N/A
0N/A int index = (int)bytesProcessed & 0x3f;
0N/A int padLen = (index < 56) ? (56 - index) : (120 - index);
0N/A engineUpdate(padding, 0, padLen);
0N/A
0N/A i2bLittle4((int)bitsProcessed, buffer, 56);
0N/A i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);
0N/A implCompress(buffer, 0);
0N/A
0N/A i2bLittle(state, 0, out, ofs, 16);
0N/A }
0N/A
0N/A private static int FF(int a, int b, int c, int d, int x, int s) {
0N/A a += ((b & c) | ((~b) & d)) + x;
0N/A return ((a << s) | (a >>> (32 - s)));
0N/A }
0N/A
0N/A private static int GG(int a, int b, int c, int d, int x, int s) {
0N/A a += ((b & c) | (b & d) | (c & d)) + x + 0x5a827999;
0N/A return ((a << s) | (a >>> (32 - s)));
0N/A }
0N/A
0N/A private static int HH(int a, int b, int c, int d, int x, int s) {
0N/A a += ((b ^ c) ^ d) + x + 0x6ed9eba1;
0N/A return ((a << s) | (a >>> (32 - s)));
0N/A }
0N/A
0N/A /**
0N/A * This is where the functions come together as the generic MD4
0N/A * transformation operation. It consumes sixteen
0N/A * bytes from the buffer, beginning at the specified offset.
0N/A */
0N/A void implCompress(byte[] buf, int ofs) {
0N/A b2iLittle64(buf, ofs, x);
0N/A
0N/A int a = state[0];
0N/A int b = state[1];
0N/A int c = state[2];
0N/A int d = state[3];
0N/A
0N/A /* Round 1 */
0N/A a = FF (a, b, c, d, x[ 0], S11); /* 1 */
0N/A d = FF (d, a, b, c, x[ 1], S12); /* 2 */
0N/A c = FF (c, d, a, b, x[ 2], S13); /* 3 */
0N/A b = FF (b, c, d, a, x[ 3], S14); /* 4 */
0N/A a = FF (a, b, c, d, x[ 4], S11); /* 5 */
0N/A d = FF (d, a, b, c, x[ 5], S12); /* 6 */
0N/A c = FF (c, d, a, b, x[ 6], S13); /* 7 */
0N/A b = FF (b, c, d, a, x[ 7], S14); /* 8 */
0N/A a = FF (a, b, c, d, x[ 8], S11); /* 9 */
0N/A d = FF (d, a, b, c, x[ 9], S12); /* 10 */
0N/A c = FF (c, d, a, b, x[10], S13); /* 11 */
0N/A b = FF (b, c, d, a, x[11], S14); /* 12 */
0N/A a = FF (a, b, c, d, x[12], S11); /* 13 */
0N/A d = FF (d, a, b, c, x[13], S12); /* 14 */
0N/A c = FF (c, d, a, b, x[14], S13); /* 15 */
0N/A b = FF (b, c, d, a, x[15], S14); /* 16 */
0N/A
0N/A /* Round 2 */
0N/A a = GG (a, b, c, d, x[ 0], S21); /* 17 */
0N/A d = GG (d, a, b, c, x[ 4], S22); /* 18 */
0N/A c = GG (c, d, a, b, x[ 8], S23); /* 19 */
0N/A b = GG (b, c, d, a, x[12], S24); /* 20 */
0N/A a = GG (a, b, c, d, x[ 1], S21); /* 21 */
0N/A d = GG (d, a, b, c, x[ 5], S22); /* 22 */
0N/A c = GG (c, d, a, b, x[ 9], S23); /* 23 */
0N/A b = GG (b, c, d, a, x[13], S24); /* 24 */
0N/A a = GG (a, b, c, d, x[ 2], S21); /* 25 */
0N/A d = GG (d, a, b, c, x[ 6], S22); /* 26 */
0N/A c = GG (c, d, a, b, x[10], S23); /* 27 */
0N/A b = GG (b, c, d, a, x[14], S24); /* 28 */
0N/A a = GG (a, b, c, d, x[ 3], S21); /* 29 */
0N/A d = GG (d, a, b, c, x[ 7], S22); /* 30 */
0N/A c = GG (c, d, a, b, x[11], S23); /* 31 */
0N/A b = GG (b, c, d, a, x[15], S24); /* 32 */
0N/A
0N/A /* Round 3 */
0N/A a = HH (a, b, c, d, x[ 0], S31); /* 33 */
0N/A d = HH (d, a, b, c, x[ 8], S32); /* 34 */
0N/A c = HH (c, d, a, b, x[ 4], S33); /* 35 */
0N/A b = HH (b, c, d, a, x[12], S34); /* 36 */
0N/A a = HH (a, b, c, d, x[ 2], S31); /* 37 */
0N/A d = HH (d, a, b, c, x[10], S32); /* 38 */
0N/A c = HH (c, d, a, b, x[ 6], S33); /* 39 */
0N/A b = HH (b, c, d, a, x[14], S34); /* 40 */
0N/A a = HH (a, b, c, d, x[ 1], S31); /* 41 */
0N/A d = HH (d, a, b, c, x[ 9], S32); /* 42 */
0N/A c = HH (c, d, a, b, x[ 5], S33); /* 43 */
0N/A b = HH (b, c, d, a, x[13], S34); /* 44 */
0N/A a = HH (a, b, c, d, x[ 3], S31); /* 45 */
0N/A d = HH (d, a, b, c, x[11], S32); /* 46 */
0N/A c = HH (c, d, a, b, x[ 7], S33); /* 47 */
0N/A b = HH (b, c, d, a, x[15], S34); /* 48 */
0N/A
0N/A state[0] += a;
0N/A state[1] += b;
0N/A state[2] += c;
0N/A state[3] += d;
0N/A }
0N/A
0N/A}