Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 531e2533dd
Fetching contributors…

Cannot retrieve contributors at this time

file 775 lines (660 sloc) 25.094 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <pollita@php.net> |
+----------------------------------------------------------------------+
*/

/* $Id$ */

/* Heavily borrowed from md5.c & sha1.c of PHP archival fame
Note that ripemd laughs in the face of logic and uses
little endian byte ordering */

#include "php_hash.h"
#include "php_hash_ripemd.h"

const php_hash_ops php_hash_ripemd128_ops = {
(php_hash_init_func_t) PHP_RIPEMD128Init,
(php_hash_update_func_t) PHP_RIPEMD128Update,
(php_hash_final_func_t) PHP_RIPEMD128Final,
(php_hash_copy_func_t) php_hash_copy,
16,
64,
sizeof(PHP_RIPEMD128_CTX)
};

const php_hash_ops php_hash_ripemd160_ops = {
(php_hash_init_func_t) PHP_RIPEMD160Init,
(php_hash_update_func_t) PHP_RIPEMD160Update,
(php_hash_final_func_t) PHP_RIPEMD160Final,
(php_hash_copy_func_t) php_hash_copy,
20,
64,
sizeof(PHP_RIPEMD160_CTX)
};

const php_hash_ops php_hash_ripemd256_ops = {
(php_hash_init_func_t) PHP_RIPEMD256Init,
(php_hash_update_func_t) PHP_RIPEMD256Update,
(php_hash_final_func_t) PHP_RIPEMD256Final,
(php_hash_copy_func_t) php_hash_copy,
32,
64,
sizeof(PHP_RIPEMD256_CTX)
};

const php_hash_ops php_hash_ripemd320_ops = {
(php_hash_init_func_t) PHP_RIPEMD320Init,
(php_hash_update_func_t) PHP_RIPEMD320Update,
(php_hash_final_func_t) PHP_RIPEMD320Final,
(php_hash_copy_func_t) php_hash_copy,
40,
64,
sizeof(PHP_RIPEMD320_CTX)
};

/* {{{ PHP_RIPEMD128Init
* ripemd128 initialization. Begins a ripemd128 operation, writing a new context.
*/
PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX * context)
{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
*/
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
}
/* }}} */

/* {{{ PHP_RIPEMD256Init
* ripemd256 initialization. Begins a ripemd256 operation, writing a new context.
*/
PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX * context)
{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
*/
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0x76543210;
context->state[5] = 0xFEDCBA98;
context->state[6] = 0x89ABCDEF;
context->state[7] = 0x01234567;
}
/* }}} */

/* {{{ PHP_RIPEMD160Init
* ripemd160 initialization. Begins a ripemd160 operation, writing a new context.
*/
PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context)
{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
*/
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0xC3D2E1F0;
}
/* }}} */

/* {{{ PHP_RIPEMD320Init
* ripemd320 initialization. Begins a ripemd320 operation, writing a new context.
*/
PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX * context)
{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
*/
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0xC3D2E1F0;
context->state[5] = 0x76543210;
context->state[6] = 0xFEDCBA98;
context->state[7] = 0x89ABCDEF;
context->state[8] = 0x01234567;
context->state[9] = 0x3C2D1E0F;
}
/* }}} */

/* Basic ripemd function */
#define F0(x,y,z) ((x) ^ (y) ^ (z))
#define F1(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
#define F2(x,y,z) (((x) | (~(y))) ^ (z))
#define F3(x,y,z) (((x) & (z)) | ((y) & (~(z))))
#define F4(x,y,z) ((x) ^ ((y) | (~(z))))

static const php_hash_uint32 K_values[5] = { 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E }; /* 128, 256, 160, 320 */
static const php_hash_uint32 KK_values[4] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x00000000 }; /* 128 & 256 */
static const php_hash_uint32 KK160_values[5] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000 }; /* 160 & 320 */

#define K(n) K_values[ (n) >> 4]
#define KK(n) KK_values[(n) >> 4]
#define KK160(n) KK160_values[(n) >> 4]

static const unsigned char R[80] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 };

static const unsigned char RR[80] = {
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 };

static const unsigned char S[80] = {
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 };

static const unsigned char SS[80] = {
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 };

#define ROLS(j, x) (((x) << S[j]) | ((x) >> (32 - S[j])))
#define ROLSS(j, x) (((x) << SS[j]) | ((x) >> (32 - SS[j])))
#define ROL(n, x) (((x) << n) | ((x) >> (32 - n)))

/* {{{ RIPEMDDecode
Decodes input (unsigned char) into output (php_hash_uint32). Assumes len is
a multiple of 4.
*/
static void RIPEMDDecode(php_hash_uint32 *output, const unsigned char *input, unsigned int len)
{
unsigned int i, j;

for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((php_hash_uint32) input[j + 0]) | (((php_hash_uint32) input[j + 1]) << 8) |
(((php_hash_uint32) input[j + 2]) << 16) | (((php_hash_uint32) input[j + 3]) << 24);
}
/* }}} */

/* {{{ RIPEMD128Transform
* ripemd128 basic transformation. Transforms state based on block.
*/
static void RIPEMD128Transform(php_hash_uint32 state[4], const unsigned char block[64])
{
php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3];
php_hash_uint32 aa = state[0], bb = state[1], cc = state[2], dd = state[3];
php_hash_uint32 tmp, x[16];
int j;

RIPEMDDecode(x, block, 64);

for(j = 0; j < 16; j++) {
tmp = ROLS( j, a + F0(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}

for(j = 16; j < 32; j++) {
tmp = ROLS( j, a + F1(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}

for(j = 32; j < 48; j++) {
tmp = ROLS( j, a + F2(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}

for(j = 48; j < 64; j++) {
tmp = ROLS( j, a + F3(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}

tmp = state[1] + c + dd;
state[1] = state[2] + d + aa;
state[2] = state[3] + a + bb;
state[3] = state[0] + b + cc;
state[0] = tmp;

tmp = 0;
memset(x, 0, sizeof(x));
}
/* }}} */

/* {{{ PHP_RIPEMD128Update
ripemd128 block update operation. Continues a ripemd128 message-digest
operation, processing another message block, and updating the
context.
*/
PHP_HASH_API void PHP_RIPEMD128Update(PHP_RIPEMD128_CTX * context, const unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen;

/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);

/* Update number of bits */
if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
context->count[1]++;
}
context->count[1] += ((php_hash_uint32) inputLen >> 29);

partLen = 64 - index;

/* Transform as many times as possible.
*/
if (inputLen >= partLen) {
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
RIPEMD128Transform(context->state, context->buffer);

for (i = partLen; i + 63 < inputLen; i += 64) {
RIPEMD128Transform(context->state, &input[i]);
}

index = 0;
} else {
i = 0;
}

/* Buffer remaining input */
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
}
/* }}} */

/* {{{ RIPEMD256Transform
* ripemd256 basic transformation. Transforms state based on block.
*/
static void RIPEMD256Transform(php_hash_uint32 state[8], const unsigned char block[64])
{
php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3];
php_hash_uint32 aa = state[4], bb = state[5], cc = state[6], dd = state[7];
php_hash_uint32 tmp, x[16];
int j;

RIPEMDDecode(x, block, 64);

for(j = 0; j < 16; j++) {
tmp = ROLS( j, a + F0(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}
tmp = a; a = aa; aa = tmp;

for(j = 16; j < 32; j++) {
tmp = ROLS( j, a + F1(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}
tmp = b; b = bb; bb = tmp;

for(j = 32; j < 48; j++) {
tmp = ROLS( j, a + F2(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}
tmp = c; c = cc; cc = tmp;

for(j = 48; j < 64; j++) {
tmp = ROLS( j, a + F3(b, c, d) + x[R[j]] + K(j));
a = d; d = c; c = b; b = tmp;
tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK(j));
aa = dd; dd = cc; cc = bb; bb = tmp;
}
tmp = d; d = dd; dd = tmp;

state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += aa;
state[5] += bb;
state[6] += cc;
state[7] += dd;

tmp = 0;
memset(x, 0, sizeof(x));
}
/* }}} */

/* {{{ PHP_RIPEMD256Update
ripemd256 block update operation. Continues a ripemd256 message-digest
operation, processing another message block, and updating the
context.
*/
PHP_HASH_API void PHP_RIPEMD256Update(PHP_RIPEMD256_CTX * context, const unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen;

/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);

/* Update number of bits */
if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
context->count[1]++;
}
context->count[1] += ((php_hash_uint32) inputLen >> 29);

partLen = 64 - index;

/* Transform as many times as possible.
*/
if (inputLen >= partLen) {
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
RIPEMD256Transform(context->state, context->buffer);

for (i = partLen; i + 63 < inputLen; i += 64) {
RIPEMD256Transform(context->state, &input[i]);
}

index = 0;
} else {
i = 0;
}

/* Buffer remaining input */
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
}
/* }}} */

/* {{{ RIPEMD160Transform
* ripemd160 basic transformation. Transforms state based on block.
*/
static void RIPEMD160Transform(php_hash_uint32 state[5], const unsigned char block[64])
{
php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
php_hash_uint32 aa = state[0], bb = state[1], cc = state[2], dd = state[3], ee = state[4];
php_hash_uint32 tmp, x[16];
int j;

RIPEMDDecode(x, block, 64);

for(j = 0; j < 16; j++) {
tmp = ROLS( j, a + F0(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F4(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}

for(j = 16; j < 32; j++) {
tmp = ROLS( j, a + F1(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}

for(j = 32; j < 48; j++) {
tmp = ROLS( j, a + F2(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}

for(j = 48; j < 64; j++) {
tmp = ROLS( j, a + F3(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}

for(j = 64; j < 80; j++) {
tmp = ROLS( j, a + F4(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}

tmp = state[1] + c + dd;
state[1] = state[2] + d + ee;
state[2] = state[3] + e + aa;
state[3] = state[4] + a + bb;
state[4] = state[0] + b + cc;
state[0] = tmp;

tmp = 0;
memset(x, 0, sizeof(x));
}
/* }}} */

/* {{{ PHP_RIPEMD160Update
ripemd160 block update operation. Continues a ripemd160 message-digest
operation, processing another message block, and updating the
context.
*/
PHP_HASH_API void PHP_RIPEMD160Update(PHP_RIPEMD160_CTX * context, const unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen;

/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);

/* Update number of bits */
if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
context->count[1]++;
}
context->count[1] += ((php_hash_uint32) inputLen >> 29);

partLen = 64 - index;

/* Transform as many times as possible.
*/
if (inputLen >= partLen) {
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
RIPEMD160Transform(context->state, context->buffer);

for (i = partLen; i + 63 < inputLen; i += 64) {
RIPEMD160Transform(context->state, &input[i]);
}

index = 0;
} else {
i = 0;
}

/* Buffer remaining input */
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
}
/* }}} */

/* {{{ RIPEMD320Transform
* ripemd320 basic transformation. Transforms state based on block.
*/
static void RIPEMD320Transform(php_hash_uint32 state[10], const unsigned char block[64])
{
php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
php_hash_uint32 aa = state[5], bb = state[6], cc = state[7], dd = state[8], ee = state[9];
php_hash_uint32 tmp, x[16];
int j;

RIPEMDDecode(x, block, 64);

for(j = 0; j < 16; j++) {
tmp = ROLS( j, a + F0(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F4(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}
tmp = b; b = bb; bb = tmp;

for(j = 16; j < 32; j++) {
tmp = ROLS( j, a + F1(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}
tmp = d; d = dd; dd = tmp;

for(j = 32; j < 48; j++) {
tmp = ROLS( j, a + F2(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}
tmp = a; a = aa; aa = tmp;

for(j = 48; j < 64; j++) {
tmp = ROLS( j, a + F3(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}
tmp = c; c = cc; cc = tmp;

for(j = 64; j < 80; j++) {
tmp = ROLS( j, a + F4(b, c, d) + x[R[j]] + K(j)) + e;
a = e; e = d; d = ROL(10, c); c = b; b = tmp;
tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
}
tmp = e; e = ee; ee = tmp;

state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
state[5] += aa;
state[6] += bb;
state[7] += cc;
state[8] += dd;
state[9] += ee;

tmp = 0;
memset(x, 0, sizeof(x));
}
/* }}} */

/* {{{ PHP_RIPEMD320Update
ripemd320 block update operation. Continues a ripemd320 message-digest
operation, processing another message block, and updating the
context.
*/
PHP_HASH_API void PHP_RIPEMD320Update(PHP_RIPEMD320_CTX * context, const unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen;

/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);

/* Update number of bits */
if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
context->count[1]++;
}
context->count[1] += ((php_hash_uint32) inputLen >> 29);

partLen = 64 - index;

/* Transform as many times as possible.
*/
if (inputLen >= partLen) {
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
RIPEMD320Transform(context->state, context->buffer);

for (i = partLen; i + 63 < inputLen; i += 64) {
RIPEMD320Transform(context->state, &input[i]);
}

index = 0;
} else {
i = 0;
}

/* Buffer remaining input */
memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
}
/* }}} */

static const unsigned char PADDING[64] =
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

/* {{{ RIPEMDEncode
Encodes input (php_hash_uint32) into output (unsigned char). Assumes len is
a multiple of 4.
*/
static void RIPEMDEncode(unsigned char *output, php_hash_uint32 *input, unsigned int len)
{
unsigned int i, j;

for (i = 0, j = 0; j < len; i++, j += 4) {
output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
output[j + 0] = (unsigned char) (input[i] & 0xff);
}
}
/* }}} */

/* {{{ PHP_RIPEMD128Final
ripemd128 finalization. Ends a ripemd128 message-digest operation, writing the
the message digest and zeroizing the context.
*/
PHP_HASH_API void PHP_RIPEMD128Final(unsigned char digest[16], PHP_RIPEMD128_CTX * context)
{
unsigned char bits[8];
unsigned int index, padLen;

/* Save number of bits */
bits[0] = (unsigned char) (context->count[0] & 0xFF);
bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
bits[4] = (unsigned char) (context->count[1] & 0xFF);
bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);

/* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
PHP_RIPEMD128Update(context, PADDING, padLen);

/* Append length (before padding) */
PHP_RIPEMD128Update(context, bits, 8);

/* Store state in digest */
RIPEMDEncode(digest, context->state, 16);

/* Zeroize sensitive information.
*/
memset((unsigned char*) context, 0, sizeof(*context));
}
/* }}} */

/* {{{ PHP_RIPEMD256Final
ripemd256 finalization. Ends a ripemd256 message-digest operation, writing the
the message digest and zeroizing the context.
*/
PHP_HASH_API void PHP_RIPEMD256Final(unsigned char digest[32], PHP_RIPEMD256_CTX * context)
{
unsigned char bits[8];
unsigned int index, padLen;

/* Save number of bits */
bits[0] = (unsigned char) (context->count[0] & 0xFF);
bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
bits[4] = (unsigned char) (context->count[1] & 0xFF);
bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);

/* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
PHP_RIPEMD256Update(context, PADDING, padLen);

/* Append length (before padding) */
PHP_RIPEMD256Update(context, bits, 8);

/* Store state in digest */
RIPEMDEncode(digest, context->state, 32);

/* Zeroize sensitive information.
*/
memset((unsigned char*) context, 0, sizeof(*context));
}
/* }}} */

/* {{{ PHP_RIPEMD160Final
ripemd160 finalization. Ends a ripemd160 message-digest operation, writing the
the message digest and zeroizing the context.
*/
PHP_HASH_API void PHP_RIPEMD160Final(unsigned char digest[20], PHP_RIPEMD160_CTX * context)
{
unsigned char bits[8];
unsigned int index, padLen;

/* Save number of bits */
bits[0] = (unsigned char) (context->count[0] & 0xFF);
bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
bits[4] = (unsigned char) (context->count[1] & 0xFF);
bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);

/* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
PHP_RIPEMD160Update(context, PADDING, padLen);

/* Append length (before padding) */
PHP_RIPEMD160Update(context, bits, 8);

/* Store state in digest */
RIPEMDEncode(digest, context->state, 20);

/* Zeroize sensitive information.
*/
memset((unsigned char*) context, 0, sizeof(*context));
}
/* }}} */

/* {{{ PHP_RIPEMD320Final
ripemd320 finalization. Ends a ripemd320 message-digest operation, writing the
the message digest and zeroizing the context.
*/
PHP_HASH_API void PHP_RIPEMD320Final(unsigned char digest[40], PHP_RIPEMD320_CTX * context)
{
unsigned char bits[8];
unsigned int index, padLen;

/* Save number of bits */
bits[0] = (unsigned char) (context->count[0] & 0xFF);
bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
bits[4] = (unsigned char) (context->count[1] & 0xFF);
bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);

/* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
PHP_RIPEMD320Update(context, PADDING, padLen);

/* Append length (before padding) */
PHP_RIPEMD320Update(context, bits, 8);

/* Store state in digest */
RIPEMDEncode(digest, context->state, 40);

/* Zeroize sensitive information.
*/
memset((unsigned char*) context, 0, sizeof(*context));
}
/* }}} */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
Something went wrong with that request. Please try again.