mirror of
https://github.com/php/php-src.git
synced 2026-03-24 08:12:21 +01:00
Backport md2, ripemd256, and ripemd320 algos from HEAD
This commit is contained in:
@@ -517,6 +517,7 @@ PHP_MINIT_FUNCTION(hash)
|
||||
|
||||
zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
|
||||
|
||||
php_hash_register_algo("md2", &php_hash_md2_ops);
|
||||
php_hash_register_algo("md4", &php_hash_md4_ops);
|
||||
php_hash_register_algo("md5", &php_hash_md5_ops);
|
||||
php_hash_register_algo("sha1", &php_hash_sha1_ops);
|
||||
@@ -525,6 +526,8 @@ PHP_MINIT_FUNCTION(hash)
|
||||
php_hash_register_algo("sha512", &php_hash_sha512_ops);
|
||||
php_hash_register_algo("ripemd128", &php_hash_ripemd128_ops);
|
||||
php_hash_register_algo("ripemd160", &php_hash_ripemd160_ops);
|
||||
php_hash_register_algo("ripemd256", &php_hash_ripemd256_ops);
|
||||
php_hash_register_algo("ripemd320", &php_hash_ripemd320_ops);
|
||||
php_hash_register_algo("whirlpool", &php_hash_whirlpool_ops);
|
||||
php_hash_register_algo("tiger128,3", &php_hash_3tiger128_ops);
|
||||
php_hash_register_algo("tiger160,3", &php_hash_3tiger160_ops);
|
||||
|
||||
@@ -39,6 +39,15 @@ php_hash_ops php_hash_md4_ops = {
|
||||
sizeof(PHP_MD4_CTX)
|
||||
};
|
||||
|
||||
php_hash_ops php_hash_md2_ops = {
|
||||
(php_hash_init_func_t) PHP_MD2Init,
|
||||
(php_hash_update_func_t) PHP_MD2Update,
|
||||
(php_hash_final_func_t) PHP_MD2Final,
|
||||
16,
|
||||
16,
|
||||
sizeof(PHP_MD2_CTX)
|
||||
};
|
||||
|
||||
/* MD common stuff */
|
||||
|
||||
static unsigned char PADDING[64] =
|
||||
@@ -579,6 +588,95 @@ PHP_HASH_API void PHP_MD4Final(unsigned char digest[16], PHP_MD4_CTX * context)
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* MD2 */
|
||||
|
||||
static unsigned char MD2_S[256] = {
|
||||
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
|
||||
98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
|
||||
30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
|
||||
190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
|
||||
169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
|
||||
128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
|
||||
255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
|
||||
79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
|
||||
69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
|
||||
27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
|
||||
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
|
||||
44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
|
||||
106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
|
||||
120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
|
||||
242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
|
||||
49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 };
|
||||
|
||||
PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context)
|
||||
{
|
||||
memset(context, 0, sizeof(PHP_MD2_CTX));
|
||||
}
|
||||
|
||||
static void MD2_Transform(PHP_MD2_CTX *context, const unsigned char *block)
|
||||
{
|
||||
unsigned char i,j,t = 0;
|
||||
|
||||
for(i = 0; i < 16; i++) {
|
||||
context->state[16+i] = block[i];
|
||||
context->state[32+i] = (context->state[16+i] ^ context->state[i]);
|
||||
}
|
||||
|
||||
for(i = 0; i < 18; i++) {
|
||||
for(j = 0; j < 48; j++) {
|
||||
t = context->state[j] = context->state[j] ^ MD2_S[t];
|
||||
}
|
||||
t += i;
|
||||
}
|
||||
|
||||
/* Update checksum -- must be after transform to avoid fouling up last message block */
|
||||
t = context->checksum[15];
|
||||
for(i = 0; i < 16; i++) {
|
||||
t = context->checksum[i] ^= MD2_S[block[i] ^ t];
|
||||
}
|
||||
}
|
||||
|
||||
PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *buf, unsigned int len)
|
||||
{
|
||||
const unsigned char *p = buf, *e = buf + len;
|
||||
|
||||
if (context->in_buffer) {
|
||||
if (context->in_buffer + len < 16) {
|
||||
/* Not enough for block, just pass into buffer */
|
||||
memcpy(context->buffer + context->in_buffer, p, len);
|
||||
context->in_buffer += len;
|
||||
return;
|
||||
}
|
||||
/* Put buffered data together with inbound for a single block */
|
||||
memcpy(context->buffer + context->in_buffer, p, 16 - context->in_buffer);
|
||||
MD2_Transform(context, context->buffer);
|
||||
p += 16 - context->in_buffer;
|
||||
context->in_buffer = 0;
|
||||
}
|
||||
|
||||
/* Process as many whole blocks as remain */
|
||||
while ((p + 16) <= e) {
|
||||
MD2_Transform(context, p);
|
||||
p += 16;
|
||||
}
|
||||
|
||||
/* Copy remaining data to buffer */
|
||||
if (p < e) {
|
||||
memcpy(context->buffer, p, e - p);
|
||||
context->in_buffer = e - p;
|
||||
}
|
||||
}
|
||||
|
||||
PHP_HASH_API void PHP_MD2Final(unsigned char output[16], PHP_MD2_CTX *context)
|
||||
{
|
||||
memset(context->buffer + context->in_buffer, 16 - context->in_buffer, 16 - context->in_buffer);
|
||||
MD2_Transform(context, context->buffer);
|
||||
MD2_Transform(context, context->checksum);
|
||||
|
||||
memcpy(output, context->state, 16);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
|
||||
@@ -43,6 +43,24 @@ php_hash_ops php_hash_ripemd160_ops = {
|
||||
sizeof(PHP_RIPEMD160_CTX)
|
||||
};
|
||||
|
||||
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,
|
||||
32,
|
||||
64,
|
||||
sizeof(PHP_RIPEMD256_CTX)
|
||||
};
|
||||
|
||||
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,
|
||||
40,
|
||||
64,
|
||||
sizeof(PHP_RIPEMD320_CTX)
|
||||
};
|
||||
|
||||
/* {{{ PHP_RIPEMD128Init
|
||||
* ripemd128 initialization. Begins a ripemd128 operation, writing a new context.
|
||||
*/
|
||||
@@ -58,8 +76,27 @@ PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX * context)
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ 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
|
||||
* ripemd128 initialization. Begins a ripemd128 operation, writing a new context.
|
||||
* ripemd160 initialization. Begins a ripemd160 operation, writing a new context.
|
||||
*/
|
||||
PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context)
|
||||
{
|
||||
@@ -74,6 +111,27 @@ PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context)
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ 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)))
|
||||
@@ -81,9 +139,9 @@ PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context)
|
||||
#define F3(x,y,z) (((x) & (z)) | ((y) & (~(z))))
|
||||
#define F4(x,y,z) ((x) ^ ((y) | (~(z))))
|
||||
|
||||
static php_hash_uint32 K_values[5] = { 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E };
|
||||
static php_hash_uint32 KK_values[4] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x00000000 };
|
||||
static php_hash_uint32 KK160_values[5] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000 };
|
||||
static php_hash_uint32 K_values[5] = { 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E }; /* 128, 256, 160, 320 */
|
||||
static php_hash_uint32 KK_values[4] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x00000000 }; /* 128 & 256 */
|
||||
static 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]
|
||||
@@ -226,6 +284,104 @@ PHP_HASH_API void PHP_RIPEMD128Update(PHP_RIPEMD128_CTX * context, const unsigne
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ 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.
|
||||
*/
|
||||
@@ -286,7 +442,7 @@ static void RIPEMD160Transform(php_hash_uint32 state[5], const unsigned char blo
|
||||
/* }}} */
|
||||
|
||||
/* {{{ PHP_RIPEMD160Update
|
||||
ripemd160 block update operation. Continues a ripemd128 message-digest
|
||||
ripemd160 block update operation. Continues a ripemd160 message-digest
|
||||
operation, processing another message block, and updating the
|
||||
context.
|
||||
*/
|
||||
@@ -325,6 +481,114 @@ PHP_HASH_API void PHP_RIPEMD160Update(PHP_RIPEMD160_CTX * context, const unsigne
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ 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 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,
|
||||
@@ -386,6 +650,43 @@ PHP_HASH_API void PHP_RIPEMD128Final(unsigned char digest[16], PHP_RIPEMD128_CTX
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ 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.
|
||||
@@ -423,6 +724,43 @@ PHP_HASH_API void PHP_RIPEMD160Final(unsigned char digest[20], PHP_RIPEMD160_CTX
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ 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
|
||||
|
||||
@@ -52,6 +52,7 @@ typedef struct _php_hash_data {
|
||||
unsigned char *key;
|
||||
} php_hash_data;
|
||||
|
||||
extern php_hash_ops php_hash_md2_ops;
|
||||
extern php_hash_ops php_hash_md4_ops;
|
||||
extern php_hash_ops php_hash_md5_ops;
|
||||
extern php_hash_ops php_hash_sha1_ops;
|
||||
@@ -60,6 +61,8 @@ extern php_hash_ops php_hash_sha384_ops;
|
||||
extern php_hash_ops php_hash_sha512_ops;
|
||||
extern php_hash_ops php_hash_ripemd128_ops;
|
||||
extern php_hash_ops php_hash_ripemd160_ops;
|
||||
extern php_hash_ops php_hash_ripemd256_ops;
|
||||
extern php_hash_ops php_hash_ripemd320_ops;
|
||||
extern php_hash_ops php_hash_whirlpool_ops;
|
||||
extern php_hash_ops php_hash_3tiger128_ops;
|
||||
extern php_hash_ops php_hash_3tiger160_ops;
|
||||
|
||||
@@ -85,4 +85,16 @@ typedef struct {
|
||||
PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX *context, const unsigned char *, unsigned int);
|
||||
PHP_HASH_API void PHP_MD4Final(unsigned char[16], PHP_MD4_CTX *);
|
||||
|
||||
/* MD2 context */
|
||||
typedef struct {
|
||||
unsigned char state[48];
|
||||
unsigned char checksum[16];
|
||||
unsigned char buffer[16];
|
||||
char in_buffer;
|
||||
} PHP_MD2_CTX;
|
||||
|
||||
PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context);
|
||||
PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *, unsigned int);
|
||||
PHP_HASH_API void PHP_MD2Final(unsigned char[16], PHP_MD2_CTX *);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -35,6 +35,18 @@ typedef struct {
|
||||
unsigned char buffer[64]; /* input buffer */
|
||||
} PHP_RIPEMD160_CTX;
|
||||
|
||||
typedef struct {
|
||||
php_hash_uint32 state[8]; /* state (ABCD) */
|
||||
php_hash_uint32 count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
||||
unsigned char buffer[64]; /* input buffer */
|
||||
} PHP_RIPEMD256_CTX;
|
||||
|
||||
typedef struct {
|
||||
php_hash_uint32 state[10]; /* state (ABCD) */
|
||||
php_hash_uint32 count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
||||
unsigned char buffer[64]; /* input buffer */
|
||||
} PHP_RIPEMD320_CTX;
|
||||
|
||||
PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX *);
|
||||
PHP_HASH_API void PHP_RIPEMD128Update(PHP_RIPEMD128_CTX *, const unsigned char *, unsigned int);
|
||||
PHP_HASH_API void PHP_RIPEMD128Final(unsigned char[16], PHP_RIPEMD128_CTX *);
|
||||
@@ -43,4 +55,12 @@ PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX *);
|
||||
PHP_HASH_API void PHP_RIPEMD160Update(PHP_RIPEMD160_CTX *, const unsigned char *, unsigned int);
|
||||
PHP_HASH_API void PHP_RIPEMD160Final(unsigned char[20], PHP_RIPEMD160_CTX *);
|
||||
|
||||
PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX *);
|
||||
PHP_HASH_API void PHP_RIPEMD256Update(PHP_RIPEMD256_CTX *, const unsigned char *, unsigned int);
|
||||
PHP_HASH_API void PHP_RIPEMD256Final(unsigned char[32], PHP_RIPEMD256_CTX *);
|
||||
|
||||
PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX *);
|
||||
PHP_HASH_API void PHP_RIPEMD320Update(PHP_RIPEMD320_CTX *, const unsigned char *, unsigned int);
|
||||
PHP_HASH_API void PHP_RIPEMD320Final(unsigned char[40], PHP_RIPEMD320_CTX *);
|
||||
|
||||
#endif /* PHP_HASH_RIPEMD_H */
|
||||
|
||||
21
ext/hash/tests/md2.phpt
Normal file
21
ext/hash/tests/md2.phpt
Normal file
@@ -0,0 +1,21 @@
|
||||
--TEST--
|
||||
md2 algorithm
|
||||
--SKIPIF--
|
||||
<?php if(!extension_loaded("hash")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
echo hash('md2', '') . "\n";
|
||||
echo hash('md2', 'a') . "\n";
|
||||
echo hash('md2', 'abc') . "\n";
|
||||
echo hash('md2', 'message digest') . "\n";
|
||||
echo hash('md2', 'abcdefghijklmnopqrstuvwxyz') . "\n";
|
||||
echo hash('md2', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') . "\n";
|
||||
echo hash('md2', '12345678901234567890123456789012345678901234567890123456789012345678901234567890') . "\n";
|
||||
--EXPECT--
|
||||
8350e5a3e24c153df2275c9f80692773
|
||||
32ec01ec4a6dac72c0ab96fb34c0b5d1
|
||||
da853b0d3f88d99b30283a69e6ded6bb
|
||||
ab4f496bfb2a530b219ff33031fe06b0
|
||||
4e8ddff3650292ab5a4108c3aa47940b
|
||||
da33def2a42df13975352846c30338cd
|
||||
d5976f79d83d3a0dc9806c3c66f3efd8
|
||||
25
ext/hash/tests/ripemd256.phpt
Normal file
25
ext/hash/tests/ripemd256.phpt
Normal file
@@ -0,0 +1,25 @@
|
||||
--TEST--
|
||||
ripemd256 algorithm
|
||||
--SKIPIF--
|
||||
<?php if(!extension_loaded("hash")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
echo hash('ripemd256', '') . "\n";
|
||||
echo hash('ripemd256', 'a') . "\n";
|
||||
echo hash('ripemd256', 'abc') . "\n";
|
||||
echo hash('ripemd256', 'message digest') . "\n";
|
||||
echo hash('ripemd256', 'abcdefghijklmnopqrstuvwxyz') . "\n";
|
||||
echo hash('ripemd256', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') . "\n";
|
||||
echo hash('ripemd256', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') . "\n";
|
||||
echo hash('ripemd256', '12345678901234567890123456789012345678901234567890123456789012345678901234567890') . "\n";
|
||||
echo hash('ripemd256', str_repeat('a', 1000000)) . "\n";
|
||||
--EXPECT--
|
||||
02ba4c4e5f8ecd1877fc52d64d30e37a2d9774fb1e5d026380ae0168e3c5522d
|
||||
f9333e45d857f5d90a91bab70a1eba0cfb1be4b0783c9acfcd883a9134692925
|
||||
afbd6e228b9d8cbbcef5ca2d03e6dba10ac0bc7dcbe4680e1e42d2e975459b65
|
||||
87e971759a1ce47a514d5c914c392c9018c7c46bc14465554afcdf54a5070c0e
|
||||
649d3034751ea216776bf9a18acc81bc7896118a5197968782dd1fd97d8d5133
|
||||
3843045583aac6c8c8d9128573e7a9809afb2a0f34ccc36ea9e72f16f6368e3f
|
||||
5740a408ac16b720b84424ae931cbb1fe363d1d0bf4017f1a89f7ea6de77a0b8
|
||||
06fdcc7a409548aaf91368c06a6275b553e3f099bf0ea4edfd6778df89a890dd
|
||||
ac953744e10e31514c150d4d8d7b677342e33399788296e43ae4850ce4f97978
|
||||
25
ext/hash/tests/ripemd320.phpt
Normal file
25
ext/hash/tests/ripemd320.phpt
Normal file
@@ -0,0 +1,25 @@
|
||||
--TEST--
|
||||
ripemd320 algorithm
|
||||
--SKIPIF--
|
||||
<?php if(!extension_loaded("hash")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
echo hash('ripemd320', '') . "\n";
|
||||
echo hash('ripemd320', 'a') . "\n";
|
||||
echo hash('ripemd320', 'abc') . "\n";
|
||||
echo hash('ripemd320', 'message digest') . "\n";
|
||||
echo hash('ripemd320', 'abcdefghijklmnopqrstuvwxyz') . "\n";
|
||||
echo hash('ripemd320', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') . "\n";
|
||||
echo hash('ripemd320', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') . "\n";
|
||||
echo hash('ripemd320', '12345678901234567890123456789012345678901234567890123456789012345678901234567890') . "\n";
|
||||
echo hash('ripemd320', str_repeat('a', 1000000)) . "\n";
|
||||
--EXPECT--
|
||||
22d65d5661536cdc75c1fdf5c6de7b41b9f27325ebc61e8557177d705a0ec880151c3a32a00899b8
|
||||
ce78850638f92658a5a585097579926dda667a5716562cfcf6fbe77f63542f99b04705d6970dff5d
|
||||
de4c01b3054f8930a79d09ae738e92301e5a17085beffdc1b8d116713e74f82fa942d64cdbc4682d
|
||||
3a8e28502ed45d422f68844f9dd316e7b98533fa3f2a91d29f84d425c88d6b4eff727df66a7c0197
|
||||
cabdb1810b92470a2093aa6bce05952c28348cf43ff60841975166bb40ed234004b8824463e6b009
|
||||
d034a7950cf722021ba4b84df769a5de2060e259df4c9bb4a4268c0e935bbc7470a969c9d072a1ac
|
||||
ed544940c86d67f250d232c30b7b3e5770e0c60c8cb9a4cafe3b11388af9920e1b99230b843c86a4
|
||||
557888af5f6d8ed62ab66945c6d2a0a47ecd5341e915eb8fea1d0524955f825dc717e4a008ab2d42
|
||||
bdee37f4371e20646b8b0d862dda16292ae36f40965e8c8509e63d1dbddecc503e2b63eb9245bb66
|
||||
Reference in New Issue
Block a user