1/***************************************************************************** 2* 3* "derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm". 4* 5* This program is free software; you can redistribute it and/or modify 6* it under the terms of the GNU General Public License as published by 7* the Free Software Foundation; either version 2 of the License, or 8* (at your option) any later version. 9* 10* This program is distributed in the hope that it will be useful, 11* but WITHOUT ANY WARRANTY; without even the implied warranty of 12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13* GNU General Public License for more details. 14* 15* You should have received a copy of the GNU General Public License 16* along with this program; if not, write to the Free Software 17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18* 19*****************************************************************************/ 20 21#include "All.h" 22#include <string.h> 23#include "MD5.h" 24 25 26#if __BYTE_ORDER == __BIG_ENDIAN 27/* 28 * Block copy and convert byte order to little-endian. 29 * dst must be 32bit aligned. 30 * Length is the number of 32bit words 31 */ 32static void 33CopyToLittleEndian ( uint32_t* dst, 34 const uint8_t* src, 35 size_t length ) 36{ 37 for ( ; length--; src += 4, dst++ ) { 38 *dst = (( (uint32_t) src [3] ) << 24) | 39 (( (uint32_t) src [2] ) << 16) | 40 (( (uint32_t) src [1] ) << 8) | 41 (( (uint32_t) src [0] ) << 0); 42 43 44 } 45} 46#endif 47 48 49/* 50 Assembler versions of __MD5Transform, MD5Init and MD5Update 51 currently exist for x86 and little-endian ARM. 52 For other targets, we need to use the C versions below. 53*/ 54 55//#if !(defined (__i386__) || ((defined (__arm__) && (__BYTE_ORDER == __LITTLE_ENDIAN)))) 56#if 1 /* SHINTA */ 57 58/* 59 Initialise the MD5 context. 60*/ 61void 62MD5Init ( MD5_CTX* context ) 63{ 64 context -> count [0] = 0; 65 context -> count [1] = 0; 66 67 context -> state [0] = 0x67452301; /* Load magic constants. */ 68 context -> state [1] = 0xefcdab89; 69 context -> state [2] = 0x98badcfe; 70 context -> state [3] = 0x10325476; 71} 72 73#define ROTATE_LEFT(x, n) ((x << n) | (x >> (32-n))) 74 75#define F(x, y, z) (z ^ (x & (y ^ z))) 76#define G(x, y, z) (y ^ (z & (x ^ y))) 77#define H(x, y, z) (x ^ y ^ z) 78#define I(x, y, z) (y ^ (x | ~z)) 79 80#define FF(a, b, c, d, x, s, ac) { (a) += F (b, c, d) + (x) + (uint32_t)(ac); (a) = ROTATE_LEFT (a, s); (a) += (b); } 81#define GG(a, b, c, d, x, s, ac) { (a) += G (b, c, d) + (x) + (uint32_t)(ac); (a) = ROTATE_LEFT (a, s); (a) += (b); } 82#define HH(a, b, c, d, x, s, ac) { (a) += H (b, c, d) + (x) + (uint32_t)(ac); (a) = ROTATE_LEFT (a, s); (a) += (b); } 83#define II(a, b, c, d, x, s, ac) { (a) += I (b, c, d) + (x) + (uint32_t)(ac); (a) = ROTATE_LEFT (a, s); (a) += (b); } 84 85static void 86__MD5Transform ( uint32_t state [4], 87 const uint8_t* in, 88 int repeat ) 89{ 90 const uint32_t* x; 91 uint32_t a = state [0]; 92 uint32_t b = state [1]; 93 uint32_t c = state [2]; 94 uint32_t d = state [3]; 95 96 for ( ; repeat; repeat-- ) { 97 uint32_t tempBuffer [16]; 98#if __BYTE_ORDER == __BIG_ENDIAN 99 100 CopyToLittleEndian (tempBuffer, in, 16); 101 x = tempBuffer; 102#else 103 if ( (unsigned int)in & 3 ) { 104 memcpy ( tempBuffer, in, 64 ); 105 x = tempBuffer; 106 } 107 else { 108 x = (const uint32_t*) in; 109 } 110#endif 111 112 FF (a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */ /* Round 1 */ 113 FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */ 114 FF (c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */ 115 FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */ 116 FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */ 117 FF (d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */ 118 FF (c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */ 119 FF (b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */ 120 FF (a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */ 121 FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */ 122 FF (c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */ 123 FF (b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */ 124 FF (a, b, c, d, x[12], 7, 0x6b901122); /* 13 */ 125 FF (d, a, b, c, x[13], 12, 0xfd987193); /* 14 */ 126 FF (c, d, a, b, x[14], 17, 0xa679438e); /* 15 */ 127 FF (b, c, d, a, x[15], 22, 0x49b40821); /* 16 */ 128 129 GG (a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */ /* Round 2 */ 130 GG (d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */ 131 GG (c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */ 132 GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */ 133 GG (a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */ 134 GG (d, a, b, c, x[10], 9, 0x02441453); /* 22 */ 135 GG (c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */ 136 GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */ 137 GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */ 138 GG (d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */ 139 GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */ 140 GG (b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */ 141 GG (a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */ 142 GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */ 143 GG (c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */ 144 GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */ 145 146 HH (a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */ /* Round 3 */ 147 HH (d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */ 148 HH (c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */ 149 HH (b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */ 150 HH (a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */ 151 HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */ 152 HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */ 153 HH (b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */ 154 HH (a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */ 155 HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */ 156 HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */ 157 HH (b, c, d, a, x[ 6], 23, 0x04881d05); /* 44 */ 158 HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */ 159 HH (d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */ 160 HH (c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */ 161 HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */ 162 163 II (a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */ /* Round 4 */ 164 II (d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */ 165 II (c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */ 166 II (b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */ 167 II (a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */ 168 II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */ 169 II (c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */ 170 II (b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */ 171 II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */ 172 II (d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */ 173 II (c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */ 174 II (b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */ 175 II (a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */ 176 II (d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */ 177 II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */ 178 II (b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */ 179 180 state [0] = a = a + state [0]; 181 state [1] = b = b + state [1]; 182 state [2] = c = c + state [2]; 183 state [3] = d = d + state [3]; 184 185 in += 64; 186 } 187} 188 189 190/* 191 MD5 block update operation: 192 Process another sub-string of the message and update the context. 193*/ 194 195void 196MD5Update ( MD5_CTX* context, 197 const uint8_t* input, 198 size_t inputBytes ) 199{ 200 int byteIndex; 201 unsigned int partLen; 202 int len; 203 int i; 204 205 /* Compute number of bytes mod 64 */ 206 byteIndex = (context -> count[0] >> 3) & 0x3F; 207 208 /* Update number of bits: count += 8 * inputBytes */ 209 if ( (context -> count [0] += inputBytes << 3) < (inputBytes << 3) ) 210 context -> count [1]++; 211 context -> count [1] += inputBytes >> (32 - 3); 212 213 partLen = (64 - byteIndex); 214 215 /* Transform as many times as possible. */ 216 if ( inputBytes >= partLen ) { 217 memcpy ( context -> buffer + byteIndex, input, partLen ); 218 __MD5Transform ( context -> state, (const uint8_t*) context -> buffer, 1 ); 219 len = ( inputBytes - partLen ) >> 6; 220 __MD5Transform ( context -> state, input + partLen, len ); 221 i = partLen + (len << 6); 222 byteIndex = 0; 223 } 224 else { 225 i = 0; 226 } 227 228 /* Buffer remaining input */ 229 memcpy ( (context -> buffer) + byteIndex, input + i, inputBytes - i ); 230} 231 232#endif 233 234 235void 236MD5Final ( uint8_t digest [16], 237 MD5_CTX* context ) 238{ 239 static uint8_t finalBlock [64]; 240 uint32_t bits [2]; 241 int byteIndex; 242 int finalBlockLength; 243 244 byteIndex = (context -> count[0] >> 3) & 0x3F; 245 finalBlockLength = (byteIndex < 56 ? 56 : 120) - byteIndex; 246 finalBlock[0] = 0x80; 247 248#if __BYTE_ORDER == __BIG_ENDIAN 249 CopyToLittleEndian ( bits, (const uint8_t*) context -> count, 2 ); 250#else 251 memcpy ( bits, context->count, 8 ); 252#endif 253 254 MD5Update ( context, finalBlock, finalBlockLength ); 255 MD5Update ( context, (const uint8_t*) bits, 8 ); 256 257#if __BYTE_ORDER == __BIG_ENDIAN 258 CopyToLittleEndian ( (uint32_t*) digest, (const uint8_t*) context -> state, 4 ); 259#else 260 memcpy ( digest, context -> state, 16 ); 261#endif 262 263 memset ( context, 0, sizeof (*context) ); 264} 265 266