Path: blob/master/src/java.base/share/classes/sun/security/provider/MD5.java
41159 views
/*1* Copyright (c) 1996, 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.lang.invoke.MethodHandles;28import java.lang.invoke.VarHandle;29import java.nio.ByteOrder;30import java.util.Arrays;31import java.util.Objects;3233import static sun.security.provider.ByteArrayAccess.*;34import jdk.internal.vm.annotation.IntrinsicCandidate;3536/**37* The MD5 class is used to compute an MD5 message digest over a given38* buffer of bytes. It is an implementation of the RSA Data Security Inc39* MD5 algorithim as described in internet RFC 1321.40*41* @author Chuck McManis42* @author Benjamin Renaud43* @author Andreas Sterbenz44*/45public final class MD5 extends DigestBase {4647// state of this object48private int[] state;4950// rotation constants51private static final int S11 = 7;52private static final int S12 = 12;53private static final int S13 = 17;54private static final int S14 = 22;55private static final int S21 = 5;56private static final int S22 = 9;57private static final int S23 = 14;58private static final int S24 = 20;59private static final int S31 = 4;60private static final int S32 = 11;61private static final int S33 = 16;62private static final int S34 = 23;63private static final int S41 = 6;64private static final int S42 = 10;65private static final int S43 = 15;66private static final int S44 = 21;6768// Standard constructor, creates a new MD5 instance.69public MD5() {70super("MD5", 16, 64);71state = new int[4];72implReset();73}7475// clone this object76public Object clone() throws CloneNotSupportedException {77MD5 copy = (MD5) super.clone();78copy.state = copy.state.clone();79return copy;80}8182/**83* Reset the state of this object.84*/85void implReset() {86// Load magic initialization constants.87state[0] = 0x67452301;88state[1] = 0xefcdab89;89state[2] = 0x98badcfe;90state[3] = 0x10325476;91}9293/**94* Perform the final computations, any buffered bytes are added95* to the digest, the count is added to the digest, and the resulting96* digest is stored.97*/98void implDigest(byte[] out, int ofs) {99long bitsProcessed = bytesProcessed << 3;100101int index = (int)bytesProcessed & 0x3f;102int padLen = (index < 56) ? (56 - index) : (120 - index);103engineUpdate(padding, 0, padLen);104105i2bLittle4((int)bitsProcessed, buffer, 56);106i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);107implCompress(buffer, 0);108109i2bLittle(state, 0, out, ofs, 16);110}111112/* **********************************************************113* The MD5 Functions. The results of this114* implementation were checked against the RSADSI version.115* **********************************************************116*/117118private static int FF(int a, int b, int c, int d, int x, int s, int ac) {119a += ((b & c) | ((~b) & d)) + x + ac;120return ((a << s) | (a >>> (32 - s))) + b;121}122123private static int GG(int a, int b, int c, int d, int x, int s, int ac) {124a += ((b & d) | (c & (~d))) + x + ac;125return ((a << s) | (a >>> (32 - s))) + b;126}127128private static int HH(int a, int b, int c, int d, int x, int s, int ac) {129a += ((b ^ c) ^ d) + x + ac;130return ((a << s) | (a >>> (32 - s))) + b;131}132133private static int II(int a, int b, int c, int d, int x, int s, int ac) {134a += (c ^ (b | (~d))) + x + ac;135return ((a << s) | (a >>> (32 - s))) + b;136}137138/**139* This is where the functions come together as the generic MD5140* transformation operation. It consumes sixteen141* bytes from the buffer, beginning at the specified offset.142*/143void implCompress(byte[] buf, int ofs) {144implCompressCheck(buf, ofs);145implCompress0(buf, ofs);146}147148private void implCompressCheck(byte[] buf, int ofs) {149Objects.requireNonNull(buf);150151// These checks are sufficient for the case when the method152// 'implCompressImpl' is replaced with a compiler153// intrinsic.154if ((ofs < 0) || ((buf.length - ofs) < 64)) {155throw new ArrayIndexOutOfBoundsException();156}157}158159// The method 'implCompress0 seems not to use its parameters.160// The method can, however, be replaced with a compiler intrinsic161// that operates directly on the array 'buf' (starting from162// offset 'ofs') and not on array 'x', therefore 'buf' and 'ofs'163// must be passed as parameter to the method.164@IntrinsicCandidate165void implCompress0(byte[] buf, int ofs) {166int a = state[0];167int b = state[1];168int c = state[2];169int d = state[3];170171int x0 = (int) LE.INT_ARRAY.get(buf, ofs);172int x1 = (int) LE.INT_ARRAY.get(buf, ofs + 4);173int x2 = (int) LE.INT_ARRAY.get(buf, ofs + 8);174int x3 = (int) LE.INT_ARRAY.get(buf, ofs + 12);175int x4 = (int) LE.INT_ARRAY.get(buf, ofs + 16);176int x5 = (int) LE.INT_ARRAY.get(buf, ofs + 20);177int x6 = (int) LE.INT_ARRAY.get(buf, ofs + 24);178int x7 = (int) LE.INT_ARRAY.get(buf, ofs + 28);179int x8 = (int) LE.INT_ARRAY.get(buf, ofs + 32);180int x9 = (int) LE.INT_ARRAY.get(buf, ofs + 36);181int x10 = (int) LE.INT_ARRAY.get(buf, ofs + 40);182int x11 = (int) LE.INT_ARRAY.get(buf, ofs + 44);183int x12 = (int) LE.INT_ARRAY.get(buf, ofs + 48);184int x13 = (int) LE.INT_ARRAY.get(buf, ofs + 52);185int x14 = (int) LE.INT_ARRAY.get(buf, ofs + 56);186int x15 = (int) LE.INT_ARRAY.get(buf, ofs + 60);187188/* Round 1 */189a = FF ( a, b, c, d, x0, S11, 0xd76aa478); /* 1 */190d = FF ( d, a, b, c, x1, S12, 0xe8c7b756); /* 2 */191c = FF ( c, d, a, b, x2, S13, 0x242070db); /* 3 */192b = FF ( b, c, d, a, x3, S14, 0xc1bdceee); /* 4 */193a = FF ( a, b, c, d, x4, S11, 0xf57c0faf); /* 5 */194d = FF ( d, a, b, c, x5, S12, 0x4787c62a); /* 6 */195c = FF ( c, d, a, b, x6, S13, 0xa8304613); /* 7 */196b = FF ( b, c, d, a, x7, S14, 0xfd469501); /* 8 */197a = FF ( a, b, c, d, x8, S11, 0x698098d8); /* 9 */198d = FF ( d, a, b, c, x9, S12, 0x8b44f7af); /* 10 */199c = FF ( c, d, a, b, x10, S13, 0xffff5bb1); /* 11 */200b = FF ( b, c, d, a, x11, S14, 0x895cd7be); /* 12 */201a = FF ( a, b, c, d, x12, S11, 0x6b901122); /* 13 */202d = FF ( d, a, b, c, x13, S12, 0xfd987193); /* 14 */203c = FF ( c, d, a, b, x14, S13, 0xa679438e); /* 15 */204b = FF ( b, c, d, a, x15, S14, 0x49b40821); /* 16 */205206/* Round 2 */207a = GG ( a, b, c, d, x1, S21, 0xf61e2562); /* 17 */208d = GG ( d, a, b, c, x6, S22, 0xc040b340); /* 18 */209c = GG ( c, d, a, b, x11, S23, 0x265e5a51); /* 19 */210b = GG ( b, c, d, a, x0, S24, 0xe9b6c7aa); /* 20 */211a = GG ( a, b, c, d, x5, S21, 0xd62f105d); /* 21 */212d = GG ( d, a, b, c, x10, S22, 0x2441453); /* 22 */213c = GG ( c, d, a, b, x15, S23, 0xd8a1e681); /* 23 */214b = GG ( b, c, d, a, x4, S24, 0xe7d3fbc8); /* 24 */215a = GG ( a, b, c, d, x9, S21, 0x21e1cde6); /* 25 */216d = GG ( d, a, b, c, x14, S22, 0xc33707d6); /* 26 */217c = GG ( c, d, a, b, x3, S23, 0xf4d50d87); /* 27 */218b = GG ( b, c, d, a, x8, S24, 0x455a14ed); /* 28 */219a = GG ( a, b, c, d, x13, S21, 0xa9e3e905); /* 29 */220d = GG ( d, a, b, c, x2, S22, 0xfcefa3f8); /* 30 */221c = GG ( c, d, a, b, x7, S23, 0x676f02d9); /* 31 */222b = GG ( b, c, d, a, x12, S24, 0x8d2a4c8a); /* 32 */223224/* Round 3 */225a = HH ( a, b, c, d, x5, S31, 0xfffa3942); /* 33 */226d = HH ( d, a, b, c, x8, S32, 0x8771f681); /* 34 */227c = HH ( c, d, a, b, x11, S33, 0x6d9d6122); /* 35 */228b = HH ( b, c, d, a, x14, S34, 0xfde5380c); /* 36 */229a = HH ( a, b, c, d, x1, S31, 0xa4beea44); /* 37 */230d = HH ( d, a, b, c, x4, S32, 0x4bdecfa9); /* 38 */231c = HH ( c, d, a, b, x7, S33, 0xf6bb4b60); /* 39 */232b = HH ( b, c, d, a, x10, S34, 0xbebfbc70); /* 40 */233a = HH ( a, b, c, d, x13, S31, 0x289b7ec6); /* 41 */234d = HH ( d, a, b, c, x0, S32, 0xeaa127fa); /* 42 */235c = HH ( c, d, a, b, x3, S33, 0xd4ef3085); /* 43 */236b = HH ( b, c, d, a, x6, S34, 0x4881d05); /* 44 */237a = HH ( a, b, c, d, x9, S31, 0xd9d4d039); /* 45 */238d = HH ( d, a, b, c, x12, S32, 0xe6db99e5); /* 46 */239c = HH ( c, d, a, b, x15, S33, 0x1fa27cf8); /* 47 */240b = HH ( b, c, d, a, x2, S34, 0xc4ac5665); /* 48 */241242/* Round 4 */243a = II ( a, b, c, d, x0, S41, 0xf4292244); /* 49 */244d = II ( d, a, b, c, x7, S42, 0x432aff97); /* 50 */245c = II ( c, d, a, b, x14, S43, 0xab9423a7); /* 51 */246b = II ( b, c, d, a, x5, S44, 0xfc93a039); /* 52 */247a = II ( a, b, c, d, x12, S41, 0x655b59c3); /* 53 */248d = II ( d, a, b, c, x3, S42, 0x8f0ccc92); /* 54 */249c = II ( c, d, a, b, x10, S43, 0xffeff47d); /* 55 */250b = II ( b, c, d, a, x1, S44, 0x85845dd1); /* 56 */251a = II ( a, b, c, d, x8, S41, 0x6fa87e4f); /* 57 */252d = II ( d, a, b, c, x15, S42, 0xfe2ce6e0); /* 58 */253c = II ( c, d, a, b, x6, S43, 0xa3014314); /* 59 */254b = II ( b, c, d, a, x13, S44, 0x4e0811a1); /* 60 */255a = II ( a, b, c, d, x4, S41, 0xf7537e82); /* 61 */256d = II ( d, a, b, c, x11, S42, 0xbd3af235); /* 62 */257c = II ( c, d, a, b, x2, S43, 0x2ad7d2bb); /* 63 */258b = II ( b, c, d, a, x9, S44, 0xeb86d391); /* 64 */259260state[0] += a;261state[1] += b;262state[2] += c;263state[3] += d;264}265266}267268269