1 /*
   2  * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import static sun.security.provider.ByteArrayAccess.*;
  29 
  30 /**
  31  * The MD5 class is used to compute an MD5 message digest over a given
  32  * buffer of bytes. It is an implementation of the RSA Data Security Inc
  33  * MD5 algorithim as described in internet RFC 1321.
  34  *
  35  * @author      Chuck McManis
  36  * @author      Benjamin Renaud
  37  * @author      Andreas Sterbenz
  38  */
  39 public final class MD5 extends DigestBase {
  40 
  41     // state of this object
  42     private int[] state;
  43     // temporary buffer, used by implCompress()
  44     private int[] x;
  45 
  46     // rotation constants
  47     private static final int S11 = 7;
  48     private static final int S12 = 12;
  49     private static final int S13 = 17;
  50     private static final int S14 = 22;
  51     private static final int S21 = 5;
  52     private static final int S22 = 9;
  53     private static final int S23 = 14;
  54     private static final int S24 = 20;
  55     private static final int S31 = 4;
  56     private static final int S32 = 11;
  57     private static final int S33 = 16;
  58     private static final int S34 = 23;
  59     private static final int S41 = 6;
  60     private static final int S42 = 10;
  61     private static final int S43 = 15;
  62     private static final int S44 = 21;
  63 
  64     // Standard constructor, creates a new MD5 instance.
  65     public MD5() {
  66         super("MD5", 16, 64);
  67         state = new int[4];
  68         x = new int[16];
  69         implReset();
  70     }
  71 
  72     // clone this object
  73     public Object clone() throws CloneNotSupportedException {
  74         MD5 copy = (MD5) super.clone();
  75         copy.state = copy.state.clone();
  76         copy.x = new int[16];
  77         return copy;
  78     }
  79 
  80     /**
  81      * Reset the state of this object.
  82      */
  83     void implReset() {
  84         // Load magic initialization constants.
  85         state[0] = 0x67452301;
  86         state[1] = 0xefcdab89;
  87         state[2] = 0x98badcfe;
  88         state[3] = 0x10325476;
  89     }
  90 
  91     /**
  92      * Perform the final computations, any buffered bytes are added
  93      * to the digest, the count is added to the digest, and the resulting
  94      * digest is stored.
  95      */
  96     void implDigest(byte[] out, int ofs) {
  97         long bitsProcessed = bytesProcessed << 3;
  98 
  99         int index = (int)bytesProcessed & 0x3f;
 100         int padLen = (index < 56) ? (56 - index) : (120 - index);
 101         engineUpdate(padding, 0, padLen);
 102 
 103         i2bLittle4((int)bitsProcessed, buffer, 56);
 104         i2bLittle4((int)(bitsProcessed >>> 32), buffer, 60);
 105         implCompress(buffer, 0);
 106 
 107         i2bLittle(state, 0, out, ofs, 16);
 108     }
 109 
 110     /* **********************************************************
 111      * The MD5 Functions. The results of this
 112      * implementation were checked against the RSADSI version.
 113      * **********************************************************
 114      */
 115 
 116     private static int FF(int a, int b, int c, int d, int x, int s, int ac) {
 117         a += ((b & c) | ((~b) & d)) + x + ac;
 118         return ((a << s) | (a >>> (32 - s))) + b;
 119     }
 120 
 121     private static int GG(int a, int b, int c, int d, int x, int s, int ac) {
 122         a += ((b & d) | (c & (~d))) + x + ac;
 123         return ((a << s) | (a >>> (32 - s))) + b;
 124     }
 125 
 126     private static int HH(int a, int b, int c, int d, int x, int s, int ac) {
 127         a += ((b ^ c) ^ d) + x + ac;
 128         return ((a << s) | (a >>> (32 - s))) + b;
 129     }
 130 
 131     private static int II(int a, int b, int c, int d, int x, int s, int ac) {
 132         a += (c ^ (b | (~d))) + x + ac;
 133         return ((a << s) | (a >>> (32 - s))) + b;
 134     }
 135 
 136     /**
 137      * This is where the functions come together as the generic MD5
 138      * transformation operation. It consumes sixteen
 139      * bytes from the buffer, beginning at the specified offset.
 140      */
 141     void implCompress(byte[] buf, int ofs) {
 142         b2iLittle64(buf, ofs, x);
 143 
 144         int a = state[0];
 145         int b = state[1];
 146         int c = state[2];
 147         int d = state[3];
 148 
 149         /* Round 1 */
 150         a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
 151         d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
 152         c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
 153         b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
 154         a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
 155         d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
 156         c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
 157         b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
 158         a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
 159         d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
 160         c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
 161         b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
 162         a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
 163         d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
 164         c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
 165         b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
 166 
 167         /* Round 2 */
 168         a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
 169         d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
 170         c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
 171         b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
 172         a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
 173         d = GG ( d, a, b, c, x[10], S22,  0x2441453); /* 22 */
 174         c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
 175         b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
 176         a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
 177         d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
 178         c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
 179         b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
 180         a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
 181         d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
 182         c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
 183         b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
 184 
 185         /* Round 3 */
 186         a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
 187         d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
 188         c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
 189         b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
 190         a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
 191         d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
 192         c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
 193         b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
 194         a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
 195         d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
 196         c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
 197         b = HH ( b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
 198         a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
 199         d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
 200         c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
 201         b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
 202 
 203         /* Round 4 */
 204         a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
 205         d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
 206         c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
 207         b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
 208         a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
 209         d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
 210         c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
 211         b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
 212         a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
 213         d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
 214         c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
 215         b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
 216         a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
 217         d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
 218         c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
 219         b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
 220 
 221         state[0] += a;
 222         state[1] += b;
 223         state[2] += c;
 224         state[3] += d;
 225     }
 226 
 227 }