Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/MD4.java
38830 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import java.security.*;28import java.util.Arrays;2930import static sun.security.provider.ByteArrayAccess.*;3132/**33* The MD4 class is used to compute an MD4 message digest over a given34* buffer of bytes. It is an implementation of the RSA Data Security Inc35* MD4 algorithim as described in internet RFC 1320.36*37* <p>The MD4 algorithm is very weak and should not be used unless it is38* unavoidable. Therefore, it is not registered in our standard providers. To39* obtain an implementation, call the static getInstance() method in this40* class.41*42* @author Andreas Sterbenz43*/44public final class MD4 extends DigestBase {4546// state of this object47private int[] state;48// temporary buffer, used by implCompress()49private int[] x;5051// rotation constants52private static final int S11 = 3;53private static final int S12 = 7;54private static final int S13 = 11;55private static final int S14 = 19;56private static final int S21 = 3;57private static final int S22 = 5;58private static final int S23 = 9;59private static final int S24 = 13;60private static final int S31 = 3;61private static final int S32 = 9;62private static final int S33 = 11;63private static final int S34 = 15;6465private final static Provider md4Provider;6667static {68md4Provider = new Provider("MD4Provider", 1.8d, "MD4 MessageDigest") {69private static final long serialVersionUID = -8850464997518327965L;70};71AccessController.doPrivileged(new PrivilegedAction<Void>() {72public Void run() {73md4Provider.put("MessageDigest.MD4", "sun.security.provider.MD4");74return null;75}76});77}7879public static MessageDigest getInstance() {80try {81return MessageDigest.getInstance("MD4", md4Provider);82} catch (NoSuchAlgorithmException e) {83// should never occur84throw new ProviderException(e);85}86}8788// Standard constructor, creates a new MD4 instance.89public MD4() {90super("MD4", 16, 64);91state = new int[4];92x = new int[16];93resetHashes();94}9596// clone this object97public Object clone() throws CloneNotSupportedException {98MD4 copy = (MD4) super.clone();99copy.state = copy.state.clone();100copy.x = new int[16];101return copy;102}103104/**105* Reset the state of this object.106*/107void implReset() {108// Load magic initialization constants.109resetHashes();110// clear out old data111Arrays.fill(x, 0);112}113114private void resetHashes() {115state[0] = 0x67452301;116state[1] = 0xefcdab89;117state[2] = 0x98badcfe;118state[3] = 0x10325476;119}120121/**122* Perform the final computations, any buffered bytes are added123* to the digest, the count is added to the digest, and the resulting124* digest is stored.125*/126void implDigest(byte[] out, int ofs) {127long bitsProcessed = bytesProcessed << 3;128129int index = (int)bytesProcessed & 0x3f;130int padLen = (index < 56) ? (56 - index) : (120 - index);131engineUpdate(padding, 0, padLen);132133i2bLittle4((int)bitsProcessed, buffer, 56);134i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);135implCompress(buffer, 0);136137i2bLittle(state, 0, out, ofs, 16);138}139140private static int FF(int a, int b, int c, int d, int x, int s) {141a += ((b & c) | ((~b) & d)) + x;142return ((a << s) | (a >>> (32 - s)));143}144145private static int GG(int a, int b, int c, int d, int x, int s) {146a += ((b & c) | (b & d) | (c & d)) + x + 0x5a827999;147return ((a << s) | (a >>> (32 - s)));148}149150private static int HH(int a, int b, int c, int d, int x, int s) {151a += ((b ^ c) ^ d) + x + 0x6ed9eba1;152return ((a << s) | (a >>> (32 - s)));153}154155/**156* This is where the functions come together as the generic MD4157* transformation operation. It consumes sixteen158* bytes from the buffer, beginning at the specified offset.159*/160void implCompress(byte[] buf, int ofs) {161b2iLittle64(buf, ofs, x);162163int a = state[0];164int b = state[1];165int c = state[2];166int d = state[3];167168/* Round 1 */169a = FF (a, b, c, d, x[ 0], S11); /* 1 */170d = FF (d, a, b, c, x[ 1], S12); /* 2 */171c = FF (c, d, a, b, x[ 2], S13); /* 3 */172b = FF (b, c, d, a, x[ 3], S14); /* 4 */173a = FF (a, b, c, d, x[ 4], S11); /* 5 */174d = FF (d, a, b, c, x[ 5], S12); /* 6 */175c = FF (c, d, a, b, x[ 6], S13); /* 7 */176b = FF (b, c, d, a, x[ 7], S14); /* 8 */177a = FF (a, b, c, d, x[ 8], S11); /* 9 */178d = FF (d, a, b, c, x[ 9], S12); /* 10 */179c = FF (c, d, a, b, x[10], S13); /* 11 */180b = FF (b, c, d, a, x[11], S14); /* 12 */181a = FF (a, b, c, d, x[12], S11); /* 13 */182d = FF (d, a, b, c, x[13], S12); /* 14 */183c = FF (c, d, a, b, x[14], S13); /* 15 */184b = FF (b, c, d, a, x[15], S14); /* 16 */185186/* Round 2 */187a = GG (a, b, c, d, x[ 0], S21); /* 17 */188d = GG (d, a, b, c, x[ 4], S22); /* 18 */189c = GG (c, d, a, b, x[ 8], S23); /* 19 */190b = GG (b, c, d, a, x[12], S24); /* 20 */191a = GG (a, b, c, d, x[ 1], S21); /* 21 */192d = GG (d, a, b, c, x[ 5], S22); /* 22 */193c = GG (c, d, a, b, x[ 9], S23); /* 23 */194b = GG (b, c, d, a, x[13], S24); /* 24 */195a = GG (a, b, c, d, x[ 2], S21); /* 25 */196d = GG (d, a, b, c, x[ 6], S22); /* 26 */197c = GG (c, d, a, b, x[10], S23); /* 27 */198b = GG (b, c, d, a, x[14], S24); /* 28 */199a = GG (a, b, c, d, x[ 3], S21); /* 29 */200d = GG (d, a, b, c, x[ 7], S22); /* 30 */201c = GG (c, d, a, b, x[11], S23); /* 31 */202b = GG (b, c, d, a, x[15], S24); /* 32 */203204/* Round 3 */205a = HH (a, b, c, d, x[ 0], S31); /* 33 */206d = HH (d, a, b, c, x[ 8], S32); /* 34 */207c = HH (c, d, a, b, x[ 4], S33); /* 35 */208b = HH (b, c, d, a, x[12], S34); /* 36 */209a = HH (a, b, c, d, x[ 2], S31); /* 37 */210d = HH (d, a, b, c, x[10], S32); /* 38 */211c = HH (c, d, a, b, x[ 6], S33); /* 39 */212b = HH (b, c, d, a, x[14], S34); /* 40 */213a = HH (a, b, c, d, x[ 1], S31); /* 41 */214d = HH (d, a, b, c, x[ 9], S32); /* 42 */215c = HH (c, d, a, b, x[ 5], S33); /* 43 */216b = HH (b, c, d, a, x[13], S34); /* 44 */217a = HH (a, b, c, d, x[ 3], S31); /* 45 */218d = HH (d, a, b, c, x[11], S32); /* 46 */219c = HH (c, d, a, b, x[ 7], S33); /* 47 */220b = HH (b, c, d, a, x[15], S34); /* 48 */221222state[0] += a;223state[1] += b;224state[2] += c;225state[3] += d;226}227228}229230231