Path: blob/master/src/java.base/share/classes/sun/security/provider/MD4.java
41159 views
/*1* Copyright (c) 2005, 2021, 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.*;31import static sun.security.util.SecurityConstants.PROVIDER_VER;3233/**34* The MD4 class is used to compute an MD4 message digest over a given35* buffer of bytes. It is an implementation of the RSA Data Security Inc36* MD4 algorithim as described in internet RFC 1320.37*38* <p>The MD4 algorithm is very weak and should not be used unless it is39* unavoidable. Therefore, it is not registered in our standard providers. To40* obtain an implementation, call the static getInstance() method in this41* class.42*43* @author Andreas Sterbenz44*/45public final class MD4 extends DigestBase {4647// state of this object48private int[] state;4950// rotation constants51private static final int S11 = 3;52private static final int S12 = 7;53private static final int S13 = 11;54private static final int S14 = 19;55private static final int S21 = 3;56private static final int S22 = 5;57private static final int S23 = 9;58private static final int S24 = 13;59private static final int S31 = 3;60private static final int S32 = 9;61private static final int S33 = 11;62private static final int S34 = 15;6364private static final Provider md4Provider;6566static {67md4Provider = new Provider("MD4Provider", PROVIDER_VER,68"MD4 MessageDigest") {69@java.io.Serial70private static final long serialVersionUID = -8850464997518327965L;71};72@SuppressWarnings("removal")73var dummy = AccessController.doPrivileged(new PrivilegedAction<Void>() {74public Void run() {75md4Provider.put("MessageDigest.MD4", "sun.security.provider.MD4");76return null;77}78});79}8081public static MessageDigest getInstance() {82try {83return MessageDigest.getInstance("MD4", md4Provider);84} catch (NoSuchAlgorithmException e) {85// should never occur86throw new ProviderException(e);87}88}8990// Standard constructor, creates a new MD4 instance.91public MD4() {92super("MD4", 16, 64);93state = new int[4];94resetHashes();95}9697// clone this object98public Object clone() throws CloneNotSupportedException {99MD4 copy = (MD4) super.clone();100copy.state = copy.state.clone();101return copy;102}103104/**105* Reset the state of this object.106*/107void implReset() {108// Load magic initialization constants.109resetHashes();110}111112private void resetHashes() {113state[0] = 0x67452301;114state[1] = 0xefcdab89;115state[2] = 0x98badcfe;116state[3] = 0x10325476;117}118119/**120* Perform the final computations, any buffered bytes are added121* to the digest, the count is added to the digest, and the resulting122* digest is stored.123*/124void implDigest(byte[] out, int ofs) {125long bitsProcessed = bytesProcessed << 3;126127int index = (int)bytesProcessed & 0x3f;128int padLen = (index < 56) ? (56 - index) : (120 - index);129engineUpdate(padding, 0, padLen);130131i2bLittle4((int)bitsProcessed, buffer, 56);132i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);133implCompress(buffer, 0);134135i2bLittle(state, 0, out, ofs, 16);136}137138private static int FF(int a, int b, int c, int d, int x, int s) {139a += ((b & c) | ((~b) & d)) + x;140return ((a << s) | (a >>> (32 - s)));141}142143private static int GG(int a, int b, int c, int d, int x, int s) {144a += ((b & c) | (b & d) | (c & d)) + x + 0x5a827999;145return ((a << s) | (a >>> (32 - s)));146}147148private static int HH(int a, int b, int c, int d, int x, int s) {149a += ((b ^ c) ^ d) + x + 0x6ed9eba1;150return ((a << s) | (a >>> (32 - s)));151}152153/**154* This is where the functions come together as the generic MD4155* transformation operation. It consumes sixteen156* bytes from the buffer, beginning at the specified offset.157*/158void implCompress(byte[] buf, int ofs) {159int x0 = (int) LE.INT_ARRAY.get(buf, ofs);160int x1 = (int) LE.INT_ARRAY.get(buf, ofs + 4);161int x2 = (int) LE.INT_ARRAY.get(buf, ofs + 8);162int x3 = (int) LE.INT_ARRAY.get(buf, ofs + 12);163int x4 = (int) LE.INT_ARRAY.get(buf, ofs + 16);164int x5 = (int) LE.INT_ARRAY.get(buf, ofs + 20);165int x6 = (int) LE.INT_ARRAY.get(buf, ofs + 24);166int x7 = (int) LE.INT_ARRAY.get(buf, ofs + 28);167int x8 = (int) LE.INT_ARRAY.get(buf, ofs + 32);168int x9 = (int) LE.INT_ARRAY.get(buf, ofs + 36);169int x10 = (int) LE.INT_ARRAY.get(buf, ofs + 40);170int x11 = (int) LE.INT_ARRAY.get(buf, ofs + 44);171int x12 = (int) LE.INT_ARRAY.get(buf, ofs + 48);172int x13 = (int) LE.INT_ARRAY.get(buf, ofs + 52);173int x14 = (int) LE.INT_ARRAY.get(buf, ofs + 56);174int x15 = (int) LE.INT_ARRAY.get(buf, ofs + 60);175176int a = state[0];177int b = state[1];178int c = state[2];179int d = state[3];180181/* Round 1 */182a = FF (a, b, c, d, x0, S11); /* 1 */183d = FF (d, a, b, c, x1, S12); /* 2 */184c = FF (c, d, a, b, x2, S13); /* 3 */185b = FF (b, c, d, a, x3, S14); /* 4 */186a = FF (a, b, c, d, x4, S11); /* 5 */187d = FF (d, a, b, c, x5, S12); /* 6 */188c = FF (c, d, a, b, x6, S13); /* 7 */189b = FF (b, c, d, a, x7, S14); /* 8 */190a = FF (a, b, c, d, x8, S11); /* 9 */191d = FF (d, a, b, c, x9, S12); /* 10 */192c = FF (c, d, a, b, x10, S13); /* 11 */193b = FF (b, c, d, a, x11, S14); /* 12 */194a = FF (a, b, c, d, x12, S11); /* 13 */195d = FF (d, a, b, c, x13, S12); /* 14 */196c = FF (c, d, a, b, x14, S13); /* 15 */197b = FF (b, c, d, a, x15, S14); /* 16 */198199/* Round 2 */200a = GG (a, b, c, d, x0, S21); /* 17 */201d = GG (d, a, b, c, x4, S22); /* 18 */202c = GG (c, d, a, b, x8, S23); /* 19 */203b = GG (b, c, d, a, x12, S24); /* 20 */204a = GG (a, b, c, d, x1, S21); /* 21 */205d = GG (d, a, b, c, x5, S22); /* 22 */206c = GG (c, d, a, b, x9, S23); /* 23 */207b = GG (b, c, d, a, x13, S24); /* 24 */208a = GG (a, b, c, d, x2, S21); /* 25 */209d = GG (d, a, b, c, x6, S22); /* 26 */210c = GG (c, d, a, b, x10, S23); /* 27 */211b = GG (b, c, d, a, x14, S24); /* 28 */212a = GG (a, b, c, d, x3, S21); /* 29 */213d = GG (d, a, b, c, x7, S22); /* 30 */214c = GG (c, d, a, b, x11, S23); /* 31 */215b = GG (b, c, d, a, x15, S24); /* 32 */216217/* Round 3 */218a = HH (a, b, c, d, x0, S31); /* 33 */219d = HH (d, a, b, c, x8, S32); /* 34 */220c = HH (c, d, a, b, x4, S33); /* 35 */221b = HH (b, c, d, a, x12, S34); /* 36 */222a = HH (a, b, c, d, x2, S31); /* 37 */223d = HH (d, a, b, c, x10, S32); /* 38 */224c = HH (c, d, a, b, x6, S33); /* 39 */225b = HH (b, c, d, a, x14, S34); /* 40 */226a = HH (a, b, c, d, x1, S31); /* 41 */227d = HH (d, a, b, c, x9, S32); /* 42 */228c = HH (c, d, a, b, x5, S33); /* 43 */229b = HH (b, c, d, a, x13, S34); /* 44 */230a = HH (a, b, c, d, x3, S31); /* 45 */231d = HH (d, a, b, c, x11, S32); /* 46 */232c = HH (c, d, a, b, x7, S33); /* 47 */233b = HH (b, c, d, a, x15, S34); /* 48 */234235state[0] += a;236state[1] += b;237state[2] += c;238state[3] += d;239}240241}242243244