md5block.c (5015B)
1 #include "os.h" 2 #include "libsec.h" 3 4 /* 5 * rfc1321 requires that I include this. The code is new. The constants 6 * all come from the rfc (hence the copyright). We trade a table for the 7 * macros in rfc. The total size is a lot less. -- presotto 8 * 9 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 10 * rights reserved. 11 * 12 * License to copy and use this software is granted provided that it 13 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest 14 * Algorithm" in all material mentioning or referencing this software 15 * or this function. 16 * 17 * License is also granted to make and use derivative works provided 18 * that such works are identified as "derived from the RSA Data 19 * Security, Inc. MD5 Message-Digest Algorithm" in all material 20 * mentioning or referencing the derived work. 21 * 22 * RSA Data Security, Inc. makes no representations concerning either 23 * the merchantability of this software or the suitability of this 24 * software forany particular purpose. It is provided "as is" 25 * without express or implied warranty of any kind. 26 * These notices must be retained in any copies of any part of this 27 * documentation and/or software. 28 */ 29 30 /* 31 * Rotate ammounts used in the algorithm 32 */ 33 enum 34 { 35 S11= 7, 36 S12= 12, 37 S13= 17, 38 S14= 22, 39 40 S21= 5, 41 S22= 9, 42 S23= 14, 43 S24= 20, 44 45 S31= 4, 46 S32= 11, 47 S33= 16, 48 S34= 23, 49 50 S41= 6, 51 S42= 10, 52 S43= 15, 53 S44= 21, 54 }; 55 56 static uint32 md5tab[] = 57 { 58 /* round 1 */ 59 /*[0]*/ 0xd76aa478, 60 0xe8c7b756, 61 0x242070db, 62 0xc1bdceee, 63 0xf57c0faf, 64 0x4787c62a, 65 0xa8304613, 66 0xfd469501, 67 0x698098d8, 68 0x8b44f7af, 69 0xffff5bb1, 70 0x895cd7be, 71 0x6b901122, 72 0xfd987193, 73 0xa679438e, 74 0x49b40821, 75 76 /* round 2 */ 77 /*[16]*/0xf61e2562, 78 0xc040b340, 79 0x265e5a51, 80 0xe9b6c7aa, 81 0xd62f105d, 82 0x2441453, 83 0xd8a1e681, 84 0xe7d3fbc8, 85 0x21e1cde6, 86 0xc33707d6, 87 0xf4d50d87, 88 0x455a14ed, 89 0xa9e3e905, 90 0xfcefa3f8, 91 0x676f02d9, 92 0x8d2a4c8a, 93 94 /* round 3 */ 95 /*[32]*/0xfffa3942, 96 0x8771f681, 97 0x6d9d6122, 98 0xfde5380c, 99 0xa4beea44, 100 0x4bdecfa9, 101 0xf6bb4b60, 102 0xbebfbc70, 103 0x289b7ec6, 104 0xeaa127fa, 105 0xd4ef3085, 106 0x4881d05, 107 0xd9d4d039, 108 0xe6db99e5, 109 0x1fa27cf8, 110 0xc4ac5665, 111 112 /* round 4 */ 113 /*[48]*/0xf4292244, 114 0x432aff97, 115 0xab9423a7, 116 0xfc93a039, 117 0x655b59c3, 118 0x8f0ccc92, 119 0xffeff47d, 120 0x85845dd1, 121 0x6fa87e4f, 122 0xfe2ce6e0, 123 0xa3014314, 124 0x4e0811a1, 125 0xf7537e82, 126 0xbd3af235, 127 0x2ad7d2bb, 128 0xeb86d391, 129 }; 130 131 static void decode(uint32*, uchar*, ulong); 132 extern void _md5block(uchar *p, ulong len, uint32 *s); 133 134 void 135 _md5block(uchar *p, ulong len, uint32 *s) 136 { 137 uint32 a, b, c, d, sh; 138 uint32 *t; 139 uchar *end; 140 uint32 x[16]; 141 142 for(end = p+len; p < end; p += 64){ 143 a = s[0]; 144 b = s[1]; 145 c = s[2]; 146 d = s[3]; 147 148 decode(x, p, 64); 149 150 t = md5tab; 151 sh = 0; 152 for(; sh != 16; t += 4){ 153 a += ((c ^ d) & b) ^ d; 154 a += x[sh] + t[0]; 155 a = (a << S11) | (a >> (32 - S11)); 156 a += b; 157 158 d += ((b ^ c) & a) ^ c; 159 d += x[sh + 1] + t[1]; 160 d = (d << S12) | (d >> (32 - S12)); 161 d += a; 162 163 c += ((a ^ b) & d) ^ b; 164 c += x[sh + 2] + t[2]; 165 c = (c << S13) | (c >> (32 - S13)); 166 c += d; 167 168 b += ((d ^ a) & c) ^ a; 169 b += x[sh + 3] + t[3]; 170 b = (b << S14) | (b >> (32 - S14)); 171 b += c; 172 173 sh += 4; 174 } 175 sh = 1; 176 for(; sh != 1+20*4; t += 4){ 177 a += ((b ^ c) & d) ^ c; 178 a += x[sh & 0xf] + t[0]; 179 a = (a << S21) | (a >> (32 - S21)); 180 a += b; 181 182 d += ((a ^ b) & c) ^ b; 183 d += x[(sh + 5) & 0xf] + t[1]; 184 d = (d << S22) | (d >> (32 - S22)); 185 d += a; 186 187 c += ((d ^ a) & b) ^ a; 188 c += x[(sh + 10) & 0xf] + t[2]; 189 c = (c << S23) | (c >> (32 - S23)); 190 c += d; 191 192 b += ((c ^ d) & a) ^ d; 193 b += x[(sh + 15) & 0xf] + t[3]; 194 b = (b << S24) | (b >> (32 - S24)); 195 b += c; 196 197 sh += 20; 198 } 199 sh = 5; 200 for(; sh != 5+12*4; t += 4){ 201 a += b ^ c ^ d; 202 a += x[sh & 0xf] + t[0]; 203 a = (a << S31) | (a >> (32 - S31)); 204 a += b; 205 206 d += a ^ b ^ c; 207 d += x[(sh + 3) & 0xf] + t[1]; 208 d = (d << S32) | (d >> (32 - S32)); 209 d += a; 210 211 c += d ^ a ^ b; 212 c += x[(sh + 6) & 0xf] + t[2]; 213 c = (c << S33) | (c >> (32 - S33)); 214 c += d; 215 216 b += c ^ d ^ a; 217 b += x[(sh + 9) & 0xf] + t[3]; 218 b = (b << S34) | (b >> (32 - S34)); 219 b += c; 220 221 sh += 12; 222 } 223 sh = 0; 224 for(; sh != 28*4; t += 4){ 225 a += c ^ (b | ~d); 226 a += x[sh & 0xf] + t[0]; 227 a = (a << S41) | (a >> (32 - S41)); 228 a += b; 229 230 d += b ^ (a | ~c); 231 d += x[(sh + 7) & 0xf] + t[1]; 232 d = (d << S42) | (d >> (32 - S42)); 233 d += a; 234 235 c += a ^ (d | ~b); 236 c += x[(sh + 14) & 0xf] + t[2]; 237 c = (c << S43) | (c >> (32 - S43)); 238 c += d; 239 240 b += d ^ (c | ~a); 241 b += x[(sh + 21) & 0xf] + t[3]; 242 b = (b << S44) | (b >> (32 - S44)); 243 b += c; 244 245 sh += 28; 246 } 247 248 s[0] += a; 249 s[1] += b; 250 s[2] += c; 251 s[3] += d; 252 } 253 } 254 255 /* 256 * decodes input (uchar) into output (uint32). Assumes len is 257 * a multiple of 4. 258 */ 259 static void 260 decode(uint32 *output, uchar *input, ulong len) 261 { 262 uchar *e; 263 264 for(e = input+len; input < e; input += 4) 265 *output++ = input[0] | (input[1] << 8) | 266 (input[2] << 16) | (input[3] << 24); 267 }