00001
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #include "linden_common.h"
00081
00082 #include "llmd5.h"
00083
00084 #include <cassert>
00085
00086
00087 const int LLMD5::BLOCK_LEN = 4096;
00088
00089
00090
00091
00092 LLMD5::LLMD5()
00093 {
00094 init();
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 void LLMD5::update (const uint1 *input, const uint4 input_length) {
00105
00106 uint4 input_index, buffer_index;
00107 uint4 buffer_space;
00108
00109 if (finalized){
00110 std::cerr << "LLMD5::update: Can't update a finalized digest!" << std::endl;
00111 return;
00112 }
00113
00114
00115 buffer_index = (unsigned int)((count[0] >> 3) & 0x3F);
00116
00117
00118 if ( (count[0] += ((uint4) input_length << 3))<((uint4) input_length << 3) )
00119 count[1]++;
00120
00121 count[1] += ((uint4)input_length >> 29);
00122
00123
00124 buffer_space = 64 - buffer_index;
00125
00126
00127 if (input_length >= buffer_space) {
00128
00129 memcpy(
00130 buffer + buffer_index,
00131 input,
00132 buffer_space);
00133 transform (buffer);
00134
00135
00136 if (input == NULL || input_length == 0){
00137 std::cerr << "LLMD5::update: Invalid input!" << std::endl;
00138 return;
00139 }
00140
00141 for (input_index = buffer_space; input_index + 63 < input_length;
00142 input_index += 64)
00143 transform (input+input_index);
00144
00145 buffer_index = 0;
00146 }
00147 else
00148 input_index=0;
00149
00150
00151
00152 memcpy(buffer+buffer_index, input+input_index, input_length-input_index);
00153 }
00154
00155
00156
00157
00158
00159
00160 void LLMD5::update(FILE* file){
00161
00162 unsigned char buffer[BLOCK_LEN];
00163 int len;
00164
00165 while ( (len=(int)fread(buffer, 1, BLOCK_LEN, file)) )
00166 update(buffer, len);
00167
00168 fclose (file);
00169
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 void LLMD5::update(std::istream& stream){
00181
00182 unsigned char buffer[BLOCK_LEN];
00183 int len;
00184
00185 while (stream.good()){
00186 stream.read( (char*)buffer, BLOCK_LEN);
00187 len=stream.gcount();
00188 update(buffer, len);
00189 }
00190
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 void LLMD5::finalize (){
00202
00203 unsigned char bits[8];
00204 unsigned int index, padLen;
00205 static uint1 PADDING[64]={
00206 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00209 };
00210
00211 if (finalized){
00212 std::cerr << "LLMD5::finalize: Already finalized this digest!" << std::endl;
00213 return;
00214 }
00215
00216
00217 encode (bits, count, 8);
00218
00219
00220 index = (uint4) ((count[0] >> 3) & 0x3f);
00221 padLen = (index < 56) ? (56 - index) : (120 - index);
00222 update (PADDING, padLen);
00223
00224
00225 update (bits, 8);
00226
00227
00228 encode (digest, state, 16);
00229
00230
00231 memset (buffer, 0, sizeof(*buffer));
00232
00233 finalized=1;
00234
00235 }
00236
00237
00238
00239
00240 LLMD5::LLMD5(FILE *file){
00241
00242 init();
00243 update(file);
00244 finalize ();
00245 }
00246
00247
00248
00249
00250 LLMD5::LLMD5(std::istream& stream){
00251
00252 init();
00253 update (stream);
00254 finalize();
00255 }
00256
00257
00258 LLMD5::LLMD5(const unsigned char *string, const unsigned int number)
00259 {
00260 const char *colon = ":";
00261 char tbuf[16];
00262 init();
00263 update(string, (U32)strlen((const char *) string));
00264 update((const unsigned char *) colon, (U32)strlen(colon));
00265 snprintf(tbuf, sizeof(tbuf), "%i", number);
00266 update((const unsigned char *) tbuf, (U32)strlen(tbuf));
00267 finalize();
00268 }
00269
00270
00271 LLMD5::LLMD5(const unsigned char *s)
00272 {
00273 init();
00274 update(s, (U32)strlen((const char *) s));
00275 finalize();
00276 }
00277
00278 void LLMD5::raw_digest(unsigned char *s)
00279 {
00280 if (!finalized)
00281 {
00282 std::cerr << "LLMD5::raw_digest: Can't get digest if you haven't "<<
00283 "finalized the digest!" << std::endl;
00284 s[0] = '\0';
00285 return;
00286 }
00287
00288 memcpy(s, digest, 16);
00289 return;
00290 }
00291
00292
00293
00294 void LLMD5::hex_digest(char *s)
00295 {
00296 int i;
00297
00298 if (!finalized)
00299 {
00300 std::cerr << "LLMD5::hex_digest: Can't get digest if you haven't "<<
00301 "finalized the digest!" <<std::endl;
00302 s[0] = '\0';
00303 return;
00304 }
00305
00306 for (i=0; i<16; i++)
00307 {
00308 sprintf(s+i*2, "%02x", digest[i]);
00309 }
00310
00311 s[32]='\0';
00312
00313 return;
00314 }
00315
00316
00317
00318
00319
00320 std::ostream& operator<<(std::ostream &stream, LLMD5 context)
00321 {
00322 char s[33];
00323 context.hex_digest(s);
00324 stream << s;
00325 return stream;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335 void LLMD5::init(){
00336 finalized=0;
00337
00338
00339 count[0] = 0;
00340 count[1] = 0;
00341
00342
00343 state[0] = 0x67452301;
00344 state[1] = 0xefcdab89;
00345 state[2] = 0x98badcfe;
00346 state[3] = 0x10325476;
00347 }
00348
00349
00350
00351
00352
00353
00354
00355 #define S11 7
00356 #define S12 12
00357 #define S13 17
00358 #define S14 22
00359 #define S21 5
00360 #define S22 9
00361 #define S23 14
00362 #define S24 20
00363 #define S31 4
00364 #define S32 11
00365 #define S33 16
00366 #define S34 23
00367 #define S41 6
00368 #define S42 10
00369 #define S43 15
00370 #define S44 21
00371
00372
00373
00374
00375
00376
00377 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
00378 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
00379 #define H(x, y, z) ((x) ^ (y) ^ (z))
00380 #define I(x, y, z) ((y) ^ ((x) | (~z)))
00381
00382
00383
00384 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
00385
00386
00387
00388
00389 #define FF(a, b, c, d, x, s, ac) { \
00390 (a) += F ((b), (c), (d)) + (x) + (U32)(ac); \
00391 (a) = ROTATE_LEFT ((a), (s)); \
00392 (a) += (b); \
00393 }
00394 #define GG(a, b, c, d, x, s, ac) { \
00395 (a) += G ((b), (c), (d)) + (x) + (U32)(ac); \
00396 (a) = ROTATE_LEFT ((a), (s)); \
00397 (a) += (b); \
00398 }
00399 #define HH(a, b, c, d, x, s, ac) { \
00400 (a) += H ((b), (c), (d)) + (x) + (U32)(ac); \
00401 (a) = ROTATE_LEFT ((a), (s)); \
00402 (a) += (b); \
00403 }
00404 #define II(a, b, c, d, x, s, ac) { \
00405 (a) += I ((b), (c), (d)) + (x) + (U32)(ac); \
00406 (a) = ROTATE_LEFT ((a), (s)); \
00407 (a) += (b); \
00408 }
00409
00410
00411
00412
00413 void LLMD5::transform (const U8 block[64]){
00414
00415 uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
00416
00417 decode (x, block, 64);
00418
00419 assert(!finalized);
00420
00421
00422 FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
00423 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
00424 FF (c, d, a, b, x[ 2], S13, 0x242070db);
00425 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
00426 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
00427 FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
00428 FF (c, d, a, b, x[ 6], S13, 0xa8304613);
00429 FF (b, c, d, a, x[ 7], S14, 0xfd469501);
00430 FF (a, b, c, d, x[ 8], S11, 0x698098d8);
00431 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
00432 FF (c, d, a, b, x[10], S13, 0xffff5bb1);
00433 FF (b, c, d, a, x[11], S14, 0x895cd7be);
00434 FF (a, b, c, d, x[12], S11, 0x6b901122);
00435 FF (d, a, b, c, x[13], S12, 0xfd987193);
00436 FF (c, d, a, b, x[14], S13, 0xa679438e);
00437 FF (b, c, d, a, x[15], S14, 0x49b40821);
00438
00439
00440 GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
00441 GG (d, a, b, c, x[ 6], S22, 0xc040b340);
00442 GG (c, d, a, b, x[11], S23, 0x265e5a51);
00443 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
00444 GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
00445 GG (d, a, b, c, x[10], S22, 0x2441453);
00446 GG (c, d, a, b, x[15], S23, 0xd8a1e681);
00447 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
00448 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
00449 GG (d, a, b, c, x[14], S22, 0xc33707d6);
00450 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
00451 GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
00452 GG (a, b, c, d, x[13], S21, 0xa9e3e905);
00453 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
00454 GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
00455 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
00456
00457
00458 HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
00459 HH (d, a, b, c, x[ 8], S32, 0x8771f681);
00460 HH (c, d, a, b, x[11], S33, 0x6d9d6122);
00461 HH (b, c, d, a, x[14], S34, 0xfde5380c);
00462 HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
00463 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
00464 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
00465 HH (b, c, d, a, x[10], S34, 0xbebfbc70);
00466 HH (a, b, c, d, x[13], S31, 0x289b7ec6);
00467 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
00468 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
00469 HH (b, c, d, a, x[ 6], S34, 0x4881d05);
00470 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
00471 HH (d, a, b, c, x[12], S32, 0xe6db99e5);
00472 HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
00473 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
00474
00475
00476 II (a, b, c, d, x[ 0], S41, 0xf4292244);
00477 II (d, a, b, c, x[ 7], S42, 0x432aff97);
00478 II (c, d, a, b, x[14], S43, 0xab9423a7);
00479 II (b, c, d, a, x[ 5], S44, 0xfc93a039);
00480 II (a, b, c, d, x[12], S41, 0x655b59c3);
00481 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
00482 II (c, d, a, b, x[10], S43, 0xffeff47d);
00483 II (b, c, d, a, x[ 1], S44, 0x85845dd1);
00484 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
00485 II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
00486 II (c, d, a, b, x[ 6], S43, 0xa3014314);
00487 II (b, c, d, a, x[13], S44, 0x4e0811a1);
00488 II (a, b, c, d, x[ 4], S41, 0xf7537e82);
00489 II (d, a, b, c, x[11], S42, 0xbd3af235);
00490 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
00491 II (b, c, d, a, x[ 9], S44, 0xeb86d391);
00492
00493 state[0] += a;
00494 state[1] += b;
00495 state[2] += c;
00496 state[3] += d;
00497
00498
00499 memset ( (uint1 *) x, 0, sizeof(x));
00500
00501 }
00502
00503
00504
00505
00506
00507 void LLMD5::encode (uint1 *output, const uint4 *input, const uint4 len) {
00508
00509 unsigned int i, j;
00510
00511 for (i = 0, j = 0; j < len; i++, j += 4) {
00512 output[j] = (uint1) (input[i] & 0xff);
00513 output[j+1] = (uint1) ((input[i] >> 8) & 0xff);
00514 output[j+2] = (uint1) ((input[i] >> 16) & 0xff);
00515 output[j+3] = (uint1) ((input[i] >> 24) & 0xff);
00516 }
00517 }
00518
00519
00520
00521
00522
00523
00524 void LLMD5::decode (uint4 *output, const uint1 *input, const uint4 len){
00525
00526 unsigned int i, j;
00527
00528 for (i = 0, j = 0; j < len; i++, j += 4)
00529 output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
00530 (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
00531 }