]> granicus.if.org Git - php/commitdiff
Add HAVAL algorithm
authorSara Golemon <pollita@php.net>
Sun, 20 Nov 2005 20:14:22 +0000 (20:14 +0000)
committerSara Golemon <pollita@php.net>
Sun, 20 Nov 2005 20:14:22 +0000 (20:14 +0000)
Bit widths: 128, 160, 192, 224, 256
Passes: 3, 4, 5

ext/hash/config.m4
ext/hash/config.w32
ext/hash/hash.c
ext/hash/hash_haval.c [new file with mode: 0644]
ext/hash/package.xml
ext/hash/php_hash.h
ext/hash/php_hash_haval.h [new file with mode: 0644]
ext/hash/tests/haval.phpt [new file with mode: 0644]

index 582efb7be9b303589a53dbab4eaacd42b82c0b6f..e2deffaff0fb01cb0dbb00cdff89b9dbc60b8c94 100644 (file)
@@ -6,5 +6,5 @@ PHP_ARG_ENABLE(hash, whether to enable hash support,
 
 if test "$PHP_HASH" != "no"; then
   AC_DEFINE(HAVE_HASH_EXT,1,[Have HASH Extension])
-  PHP_NEW_EXTENSION(hash, hash.c hash_md.c hash_sha.c hash_ripemd.c , $ext_shared)
+  PHP_NEW_EXTENSION(hash, hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c, $ext_shared)
 fi
index b2dc934b0f0010e3081982a1313308752ea7a6bb..8ae9d360c78a2d10c5721397bb235774cfcb8cf9 100644 (file)
@@ -5,6 +5,6 @@ ARG_ENABLE("hash", "enable hash support", "no");
 
 if (PHP_HASH != "no") {
        AC_DEFINE('HAVE_HASH_EXT', 1);
-       EXTENSION("hash", "hash.c hash_md.c hash_sha.c hash_ripemd.c");
+       EXTENSION("hash", "hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c");
 }
 
index 1e4e1d93b1c8442992b909cd90d2fbc4c76aadce..648b77e5a9b1f0599e151256db9c36979b8faa63 100644 (file)
@@ -389,6 +389,8 @@ static void php_hash_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
        efree(hash);
 }
 
+#define PHP_HASH_HAVAL_REGISTER(p,b)   php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
+
 /* {{{ PHP_MINIT_FUNCTION
  */
 PHP_MINIT_FUNCTION(hash)
@@ -405,6 +407,24 @@ PHP_MINIT_FUNCTION(hash)
        php_hash_register_algo("ripemd128",             &php_hash_ripemd128_ops);
        php_hash_register_algo("ripemd160",             &php_hash_ripemd160_ops);
 
+       PHP_HASH_HAVAL_REGISTER(3,128);
+       PHP_HASH_HAVAL_REGISTER(3,160);
+       PHP_HASH_HAVAL_REGISTER(3,192);
+       PHP_HASH_HAVAL_REGISTER(3,224);
+       PHP_HASH_HAVAL_REGISTER(3,256);
+
+       PHP_HASH_HAVAL_REGISTER(4,128);
+       PHP_HASH_HAVAL_REGISTER(4,160);
+       PHP_HASH_HAVAL_REGISTER(4,192);
+       PHP_HASH_HAVAL_REGISTER(4,224);
+       PHP_HASH_HAVAL_REGISTER(4,256);
+
+       PHP_HASH_HAVAL_REGISTER(5,128);
+       PHP_HASH_HAVAL_REGISTER(5,160);
+       PHP_HASH_HAVAL_REGISTER(5,192);
+       PHP_HASH_HAVAL_REGISTER(5,224);
+       PHP_HASH_HAVAL_REGISTER(5,256);
+
        REGISTER_LONG_CONSTANT("HASH_HMAC",             PHP_HASH_HMAC,  CONST_CS | CONST_PERSISTENT);
 
        return SUCCESS;
@@ -433,10 +453,8 @@ PHP_MINFO_FUNCTION(hash)
        for(zend_hash_internal_pointer_reset_ex(&php_hash_hashtable, &pos);
                (type = zend_hash_get_current_key_ex(&php_hash_hashtable, &str, NULL, &idx, 0, &pos)) != HASH_KEY_NON_EXISTANT;
                zend_hash_move_forward_ex(&php_hash_hashtable, &pos)) {
-               s += snprintf(s, e - s, "%s, ", str);
+               s += snprintf(s, e - s, "%s ", str);
        }
-
-       while (s > buffer && *(--s) == ' ');
        *s = 0;
 
        php_info_print_table_start();
diff --git a/ext/hash/hash_haval.c b/ext/hash/hash_haval.c
new file mode 100644 (file)
index 0000000..62d4a29
--- /dev/null
@@ -0,0 +1,546 @@
+/*
+  +----------------------------------------------------------------------+
+  | PHP Version 5                                                        |
+  +----------------------------------------------------------------------+
+  | Copyright (c) 1997-2005 The PHP Group                                |
+  +----------------------------------------------------------------------+
+  | This source file is subject to version 3.0 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_0.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$ */
+
+#include "php_hash.h"
+#include "php_hash_haval.h"
+
+static unsigned char PADDING[128] ={
+       1, 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,
+       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, 0 };
+
+static php_uint32 D0[8] = {
+       0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, 0x082EFA98, 0xEC4E6C89 };
+
+static php_uint32 K2[32] = {
+       0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917,
+       0x9216D5D9, 0x8979FB1B, 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96,
+       0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0x636920D8, 0x71574E69,
+       0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5 };
+
+static php_uint32 K3[32] = {
+       0x9C30D539, 0x2AF26013, 0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF, 0x8E79DCB0, 0x603A180E,
+       0x6C9E0E8B, 0xB01E8A3E, 0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60, 0xE65525F3, 0xAA55AB94,
+       0x57489862, 0x63E81440, 0x55CA396A, 0x2AAB10B6, 0xB4CC5C34, 0x1141E8CE, 0xA15486AF, 0x7C72E993,
+       0xB3EE1411, 0x636FBC2A, 0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E, 0xAFD6BA33, 0x6C24CF5C };
+
+static php_uint32 K4[32] = {
+       0x7A325381, 0x28958677, 0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193, 0x61D809CC, 0xFB21A991,
+       0x487CAC60, 0x5DEC8032, 0xEF845D5D, 0xE98575B1, 0xDC262302, 0xEB651B88, 0x23893E81, 0xD396ACC5,
+       0x0F6D6FF3, 0x83F44239, 0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E, 0x21C66842, 0xF6E96C9A,
+       0x670C9C61, 0xABD388F0, 0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3, 0x6EEF0B6C, 0x137A3BE4 };
+
+static php_uint32 K5[32] = {
+       0xBA3BF050, 0x7EFB2A98, 0xA1F1651D, 0x39AF0176, 0x66CA593E, 0x82430E88, 0x8CEE8619, 0x456F9FB4,
+       0x7D84A5C3, 0x3B8B5EBE, 0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6, 0x4ED3AA62, 0x363F7706,
+       0x1BFEDF72, 0x429B023D, 0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B, 0x075372C9, 0x80991B7B,
+       0x25D479D8, 0xF6E8DEF7, 0xE3FE501A, 0xB6794C3B, 0x976CE0BD, 0x04C006BA, 0xC1A94FB6, 0x409F60C4 };
+
+static short I2[32] = {         5, 14, 26, 18, 11, 28,  7, 16,  0, 23, 20, 22,  1, 10,  4,  8,
+                                               30,  3, 21,  9, 17, 24, 29,  6, 19, 12, 15, 13,  2, 25, 31, 27 };
+
+static short I3[32] = {        19,  9,  4, 20, 28, 17,  8, 22, 29, 14, 25, 12, 24, 30, 16, 26,
+                                               31, 15,  7,  3,  1,  0, 18, 27, 13,  6, 21, 10, 23, 11,  5,  2 };
+
+static short I4[32] = {        24,  4,  0, 14,  2,  7, 28, 23, 26,  6, 30, 20, 18, 25, 19,  3,
+                                               22, 11, 31, 21,  8, 27, 12,  9,  1, 29,  5, 15, 17, 10, 16, 13 };
+
+static short I5[32] = {        27,  3, 21, 26, 17, 11, 20, 29, 19,  0, 12,  7, 13,  8, 31, 10,
+                                                5,  9, 14, 30, 18,  6, 28, 24,  2, 23, 16, 22,  4,  1, 25, 15 };
+
+static short M0[32] = {        0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1,
+                                               0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1 };
+
+static short M1[32] = {        1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2,
+                                               1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2 };
+
+static short M2[32] = {        2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3,
+                                               2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3 };
+
+static short M3[32] = {        3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4,
+                                               3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4 };
+
+static short M4[32] = {        4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5,
+                                               4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5 };
+
+static short M5[32] = {        5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6,
+                                               5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6 };
+
+static short M6[32] = {        6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7,
+                                               6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7 };
+
+static short M7[32] = {        7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0,
+                                               7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0 };
+
+/* {{{ Encode
+   Encodes input (php_uint32) into output (unsigned char). Assumes len is
+   a multiple of 4.
+ */
+static void Encode(unsigned char *output, php_uint32 *input, unsigned int len)
+{
+       unsigned int i, j;
+
+       for (i = 0, j = 0; j < len; i++, j += 4) {
+               output[j] = (unsigned char) (input[i] & 0xff);
+               output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
+               output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
+               output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
+       }
+}
+/* }}} */
+
+/* {{{ Decode
+   Decodes input (unsigned char) into output (php_uint32). Assumes len is
+   a multiple of 4.
+ */
+static void Decode(php_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_uint32) input[j]) | (((php_uint32) input[j + 1]) << 8) |
+                       (((php_uint32) input[j + 2]) << 16) | (((php_uint32) input[j + 3]) << 24);
+       }
+}
+/* }}} */
+
+#define F1(x6,x5,x4,x3,x2,x1,x0)       ( ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ ((x0) & (x1)) ^ (x0) )
+#define F2(x6,x5,x4,x3,x2,x1,x0)       ( ((x1) & (x2) & (x3)) ^ ((x2) & (x4) & (x5)) ^ ((x1) & (x2)) ^ ((x1) & (x4)) ^ \
+                                                                         ((x2) & (x6)) ^ ((x3) & (x5)) ^ ((x4) & (x5)) ^ ((x0) & (x2)) ^ (x0) )
+#define F3(x6,x5,x4,x3,x2,x1,x0)       ( ((x1) & (x2) & (x3)) ^ ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ ((x0) & (x3)) ^ (x0) )
+#define F4(x6,x5,x4,x3,x2,x1,x0)       ( ((x1) & (x2) & (x3)) ^ ((x2) & (x4) & (x5)) ^ ((x3) & (x4) & (x6)) ^ \
+                                                                         ((x1) & (x4)) ^ ((x2) & (x6)) ^ ((x3) & (x4)) ^ ((x3) & (x5)) ^ \
+                                                                         ((x3) & (x6)) ^ ((x4) & (x5)) ^ ((x4) & (x6)) ^ ((x0) & (x4)) ^ (x0) )
+#define F5(x6,x5,x4,x3,x2,x1,x0)       ( ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ \
+                                                                         ((x0) & (x1) & (x2) & (x3)) ^ ((x0) & (x5)) ^ (x0) )
+
+#define ROTR(x,n)      (((x) >> (n)) | ((x) << (32 - (n))))
+
+
+/* {{{ PHP_3HAVALTransform
+ */
+static void PHP_3HAVALTransform(php_uint32 state[8], const unsigned char block[128])
+{
+       php_uint32 E[8];
+       php_uint32 x[32];
+       int i;
+
+       Decode(x, block, 128);
+
+       for(i = 0; i < 8; i++) {
+               E[i] = state[i];
+       }
+
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F1(E[M1[i]],E[M0[i]],E[M3[i]],E[M5[i]],E[M6[i]],E[M2[i]],E[M4[i]]),7) + ROTR(E[M7[i]],11) + x[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F2(E[M4[i]],E[M2[i]],E[M1[i]],E[M0[i]],E[M5[i]],E[M3[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F3(E[M6[i]],E[M1[i]],E[M2[i]],E[M3[i]],E[M4[i]],E[M5[i]],E[M0[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
+       }
+
+       /* Update digest */
+       for(i = 0; i < 8; i++) {
+               state[i] += E[i];
+       }
+
+       /* Zeroize sensitive information. */
+       memset((unsigned char*) x, 0, sizeof(x));
+}
+/* }}} */
+
+/* {{{ PHP_4HAVALTransform
+ */
+static void PHP_4HAVALTransform(php_uint32 state[8], const unsigned char block[128])
+{
+       php_uint32 E[8];
+       php_uint32 x[32];
+       int i;
+
+       Decode(x, block, 128);
+
+       for(i = 0; i < 8; i++) {
+               E[i] = state[i];
+       }
+
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F1(E[M2[i]],E[M6[i]],E[M1[i]],E[M4[i]],E[M5[i]],E[M3[i]],E[M0[i]]),7) + ROTR(E[M7[i]],11) + x[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F2(E[M3[i]],E[M5[i]],E[M2[i]],E[M0[i]],E[M1[i]],E[M6[i]],E[M4[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F3(E[M1[i]],E[M4[i]],E[M3[i]],E[M6[i]],E[M0[i]],E[M2[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F4(E[M6[i]],E[M4[i]],E[M0[i]],E[M5[i]],E[M2[i]],E[M1[i]],E[M3[i]]),7) + ROTR(E[M7[i]],11) + x[I4[i]] + K4[i];
+       }
+
+       /* Update digest */
+       for(i = 0; i < 8; i++) {
+               state[i] += E[i];
+       }
+
+       /* Zeroize sensitive information. */
+       memset((unsigned char*) x, 0, sizeof(x));
+}
+/* }}} */
+
+/* {{{ PHP_5HAVALTransform
+ */
+static void PHP_5HAVALTransform(php_uint32 state[8], const unsigned char block[128])
+{
+       php_uint32 E[8];
+       php_uint32 x[32];
+       int i;
+
+       Decode(x, block, 128);
+
+       for(i = 0; i < 8; i++) {
+               E[i] = state[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F1(E[M3[i]],E[M4[i]],E[M1[i]],E[M0[i]],E[M5[i]],E[M2[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F2(E[M6[i]],E[M2[i]],E[M1[i]],E[M0[i]],E[M3[i]],E[M4[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F3(E[M2[i]],E[M6[i]],E[M0[i]],E[M4[i]],E[M3[i]],E[M1[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F4(E[M1[i]],E[M5[i]],E[M3[i]],E[M2[i]],E[M0[i]],E[M4[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[I4[i]] + K4[i];
+       }
+       for(i = 0; i < 32; i++) {
+               E[7 - (i % 8)] = ROTR(F5(E[M2[i]],E[M5[i]],E[M0[i]],E[M6[i]],E[M4[i]],E[M3[i]],E[M1[i]]),7) + ROTR(E[M7[i]],11) + x[I5[i]] + K5[i];
+       }
+
+       /* Update digest */
+       for(i = 0; i < 8; i++) {
+               state[i] += E[i];
+       }
+
+       /* Zeroize sensitive information. */
+       memset((unsigned char*) x, 0, sizeof(x));
+}
+/* }}} */
+
+#define PHP_HASH_HAVAL_INIT(p,b) \
+php_hash_ops php_hash_##p##haval##b##_ops = { PHP_##p##HAVAL##b##Init, PHP_HAVALUpdate, PHP_HAVAL##b##Final, ((b) / 8), 128, sizeof(PHP_HAVAL_CTX) }; \
+PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *context) \
+{      int i; context->count[0] =      context->count[1] =     0; \
+       for(i = 0; i < 8; i++) context->state[i] = D0[i]; \
+       context->passes = p;    context->output = b; \
+       context->Transform = PHP_##p##HAVALTransform; }
+
+PHP_HASH_HAVAL_INIT(3,128)
+PHP_HASH_HAVAL_INIT(3,160)
+PHP_HASH_HAVAL_INIT(3,192)
+PHP_HASH_HAVAL_INIT(3,224)
+PHP_HASH_HAVAL_INIT(3,256)
+
+PHP_HASH_HAVAL_INIT(4,128)
+PHP_HASH_HAVAL_INIT(4,160)
+PHP_HASH_HAVAL_INIT(4,192)
+PHP_HASH_HAVAL_INIT(4,224)
+PHP_HASH_HAVAL_INIT(4,256)
+
+PHP_HASH_HAVAL_INIT(5,128)
+PHP_HASH_HAVAL_INIT(5,160)
+PHP_HASH_HAVAL_INIT(5,192)
+PHP_HASH_HAVAL_INIT(5,224)
+PHP_HASH_HAVAL_INIT(5,256)
+
+/* {{{ PHP_HAVALUpdate
+ */
+PHP_HASH_API void PHP_HAVALUpdate(PHP_HAVAL_CTX *context, const unsigned char *input, unsigned int inputLen)
+{
+       unsigned int i, index, partLen;
+
+       /* Compute number of bytes mod 128 */
+       index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
+       /* Update number of bits */
+       if ((context->count[0] += ((php_uint32) inputLen << 3)) < ((php_uint32) inputLen << 3)) {
+               context->count[1]++;
+       }
+       context->count[1] += ((php_uint32) inputLen >> 29);
+
+       partLen = 128 - index;
+
+       /* Transform as many times as possible.
+        */
+       if (inputLen >= partLen) {
+               memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
+               context->Transform(context->state, context->buffer);
+
+               for (i = partLen; i + 127 < inputLen; i += 128) {
+                       context->Transform(context->state, &input[i]);
+               }
+
+               index = 0;
+       } else {
+               i = 0;
+       }
+
+       /* Buffer remaining input */
+       memcpy((unsigned char*) &context->buffer[index], (unsigned char*) &input[i], inputLen - i);
+}
+/* }}} */
+
+#define PHP_HASH_HAVAL_VERSION 0x01
+
+/* {{{ PHP_HAVAL128Final
+ */
+PHP_HASH_API void PHP_HAVAL128Final(unsigned char *digest, PHP_HAVAL_CTX * context)
+{
+       unsigned char bits[8];
+       unsigned int index, padLen;
+
+       /* Version, Passes, and Digest Length */
+       bits[0] =       (PHP_HASH_HAVAL_VERSION & 0x07) |
+                               ((context->passes & 0x07) << 3) |
+                               ((context->output & 0x03) << 6);
+       bits[1] = (context->output >> 2);
+
+       /* Save number of bits */
+       Encode(bits + 2, context->count, 8);
+
+       /* Pad out to 118 mod 128.
+        */
+       index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+       padLen = (index < 118) ? (118 - index) : (246 - index);
+       PHP_HAVALUpdate(context, PADDING, padLen);
+
+       /* Append version, passes, digest length, and message length */
+       PHP_HAVALUpdate(context, bits, 10);
+
+       /* Store state in digest */
+       context->state[3] += (context->state[7] & 0xFF000000) |
+                                                (context->state[6] & 0x00FF0000) |
+                                                (context->state[5] & 0x0000FF00) |
+                                                (context->state[4] & 0x000000FF);
+
+       context->state[2] += (((context->state[7] & 0x00FF0000) |
+                                                  (context->state[6] & 0x0000FF00) |
+                                                  (context->state[5] & 0x000000FF)) << 8) |
+                                                 ((context->state[4] & 0xFF000000) >> 24);
+
+       context->state[1] += (((context->state[7] & 0x0000FF00) |
+                                                  (context->state[6] & 0x000000FF)) << 16) |
+                                                (((context->state[5] & 0xFF000000) |
+                                                  (context->state[4] & 0x00FF0000)) >> 16);
+
+       context->state[0] +=  ((context->state[7] & 0x000000FF) << 24) |
+                                                (((context->state[6] & 0xFF000000) |
+                                                  (context->state[5] & 0x00FF0000) |
+                                                  (context->state[4] & 0x0000FF00)) >> 8);
+
+       Encode(digest, context->state, 16);
+
+       /* Zeroize sensitive information.
+        */
+       memset((unsigned char*) context, 0, sizeof(*context));
+}
+/* }}} */
+
+/* {{{ PHP_HAVAL160Final
+ */
+PHP_HASH_API void PHP_HAVAL160Final(unsigned char *digest, PHP_HAVAL_CTX * context)
+{
+       unsigned char bits[8];
+       unsigned int index, padLen;
+
+       /* Version, Passes, and Digest Length */
+       bits[0] =       (PHP_HASH_HAVAL_VERSION & 0x07) |
+                               ((context->passes & 0x07) << 3) |
+                               ((context->output & 0x03) << 6);
+       bits[1] = (context->output >> 2);
+
+       /* Save number of bits */
+       Encode(bits + 2, context->count, 8);
+
+       /* Pad out to 118 mod 128.
+        */
+       index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+       padLen = (index < 118) ? (118 - index) : (246 - index);
+       PHP_HAVALUpdate(context, PADDING, padLen);
+
+       /* Append version, passes, digest length, and message length */
+       PHP_HAVALUpdate(context, bits, 10);
+
+       /* Store state in digest */
+       context->state[4] += ((context->state[7] & 0xFE000000) |
+                                                 (context->state[6] & 0x01F80000) |
+                                                 (context->state[5] & 0x0007F000)) >> 12;
+
+       context->state[3] += ((context->state[7] & 0x01F80000) |
+                                                 (context->state[6] & 0x0007F000) |
+                                                 (context->state[5] & 0x00000FC0)) >> 6;
+
+       context->state[2] +=  (context->state[7] & 0x0007F000) |
+                                                 (context->state[6] & 0x00000FC0) |
+                                                 (context->state[5] & 0x0000003F);
+
+       context->state[1] += ROTR((context->state[7] & 0x00000FC0) |
+                                                         (context->state[6] & 0x0000003F) |
+                                                         (context->state[5] & 0xFE000000), 25);
+
+       context->state[0] += ROTR((context->state[7] & 0x0000003F) |
+                                                         (context->state[6] & 0xFE000000) |
+                                                         (context->state[5] & 0x01F80000), 19);
+
+       Encode(digest, context->state, 20);
+
+       /* Zeroize sensitive information.
+        */
+       memset((unsigned char*) context, 0, sizeof(*context));
+}
+/* }}} */
+
+/* {{{ PHP_HAVAL192Final
+ */
+PHP_HASH_API void PHP_HAVAL192Final(unsigned char *digest, PHP_HAVAL_CTX * context)
+{
+       unsigned char bits[8];
+       unsigned int index, padLen;
+
+       /* Version, Passes, and Digest Length */
+       bits[0] =       (PHP_HASH_HAVAL_VERSION & 0x07) |
+                               ((context->passes & 0x07) << 3) |
+                               ((context->output & 0x03) << 6);
+       bits[1] = (context->output >> 2);
+
+       /* Save number of bits */
+       Encode(bits + 2, context->count, 8);
+
+       /* Pad out to 118 mod 128.
+        */
+       index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+       padLen = (index < 118) ? (118 - index) : (246 - index);
+       PHP_HAVALUpdate(context, PADDING, padLen);
+
+       /* Append version, passes, digest length, and message length */
+       PHP_HAVALUpdate(context, bits, 10);
+
+       /* Store state in digest */
+       context->state[5] += ((context->state[7] & 0xFC000000) | (context->state[6] & 0x03E00000)) >> 21;
+       context->state[4] += ((context->state[7] & 0x03E00000) | (context->state[6] & 0x001F0000)) >> 16;
+       context->state[3] += ((context->state[7] & 0x001F0000) | (context->state[6] & 0x0000FC00)) >> 10;
+       context->state[2] += ((context->state[7] & 0x0000FC00) | (context->state[6] & 0x000003E0)) >>  5;
+       context->state[1] +=  (context->state[7] & 0x000003E0) | (context->state[6] & 0x0000001F);
+       context->state[0] += ROTR((context->state[7] & 0x0000001F) | (context->state[6] & 0xFC000000), 26);
+       Encode(digest, context->state, 24);
+
+       /* Zeroize sensitive information.
+        */
+       memset((unsigned char*) context, 0, sizeof(*context));
+}
+/* }}} */
+
+/* {{{ PHP_HAVAL224Final
+ */
+PHP_HASH_API void PHP_HAVAL224Final(unsigned char *digest, PHP_HAVAL_CTX * context)
+{
+       unsigned char bits[8];
+       unsigned int index, padLen;
+
+       /* Version, Passes, and Digest Length */
+       bits[0] =       (PHP_HASH_HAVAL_VERSION & 0x07) |
+                               ((context->passes & 0x07) << 3) |
+                               ((context->output & 0x03) << 6);
+       bits[1] = (context->output >> 2);
+
+       /* Save number of bits */
+       Encode(bits + 2, context->count, 8);
+
+       /* Pad out to 118 mod 128.
+        */
+       index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+       padLen = (index < 118) ? (118 - index) : (246 - index);
+       PHP_HAVALUpdate(context, PADDING, padLen);
+
+       /* Append version, passes, digest length, and message length */
+       PHP_HAVALUpdate(context, bits, 10);
+
+       /* Store state in digest */
+       context->state[6] +=  context->state[7]        & 0x0000000F;
+       context->state[5] += (context->state[7] >>  4) & 0x0000001F;
+       context->state[4] += (context->state[7] >>  9) & 0x0000000F;
+       context->state[3] += (context->state[7] >> 13) & 0x0000001F;
+       context->state[2] += (context->state[7] >> 18) & 0x0000000F;
+       context->state[1] += (context->state[7] >> 22) & 0x0000001F;
+       context->state[0] += (context->state[7] >> 27) & 0x0000001F;
+       Encode(digest, context->state, 28);
+
+       /* Zeroize sensitive information.
+        */
+       memset((unsigned char*) context, 0, sizeof(*context));
+}
+/* }}} */
+
+/* {{{ PHP_HAVAL256Final
+ */
+PHP_HASH_API void PHP_HAVAL256Final(unsigned char *digest, PHP_HAVAL_CTX * context)
+{
+       unsigned char bits[8];
+       unsigned int index, padLen;
+
+       /* Version, Passes, and Digest Length */
+       bits[0] =       (PHP_HASH_HAVAL_VERSION & 0x07) |
+                               ((context->passes & 0x07) << 3) |
+                               ((context->output & 0x03) << 6);
+       bits[1] = (context->output >> 2);
+
+       /* Save number of bits */
+       Encode(bits + 2, context->count, 8);
+
+       /* Pad out to 118 mod 128.
+        */
+       index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+       padLen = (index < 118) ? (118 - index) : (246 - index);
+       PHP_HAVALUpdate(context, PADDING, padLen);
+
+       /* Append version, passes, digest length, and message length */
+       PHP_HAVALUpdate(context, bits, 10);
+
+       /* Store state in digest */
+       Encode(digest, context->state, 32);
+
+       /* 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
+ */
index 525c3470e31c2d794165f04d7460bd31f90b9014..1e8d541922b125dc5da6e4c3f284c023c123687b 100644 (file)
@@ -42,6 +42,7 @@
     <file role="test" name="sha512.phpt"/>
     <file role="test" name="ripemd128.phpt"/>
     <file role="test" name="ripemd160.phpt"/>
+    <file role="test" name="haval.phpt"/>
    </dir>
   </filelist>
 
index 6651fdde43e502904db503743a3fe65f22a5d6cc..76ce694830405ef99c07e945aa260c5dd7ba657f 100644 (file)
@@ -55,6 +55,26 @@ extern php_hash_ops php_hash_sha512_ops;
 extern php_hash_ops php_hash_ripemd128_ops;
 extern php_hash_ops php_hash_ripemd160_ops;
 
+#define PHP_HASH_HAVAL_OPS(p,b)        extern php_hash_ops php_hash_##p##haval##b##_ops;
+
+PHP_HASH_HAVAL_OPS(3,128)
+PHP_HASH_HAVAL_OPS(3,160)
+PHP_HASH_HAVAL_OPS(3,192)
+PHP_HASH_HAVAL_OPS(3,224)
+PHP_HASH_HAVAL_OPS(3,256)
+
+PHP_HASH_HAVAL_OPS(4,128)
+PHP_HASH_HAVAL_OPS(4,160)
+PHP_HASH_HAVAL_OPS(4,192)
+PHP_HASH_HAVAL_OPS(4,224)
+PHP_HASH_HAVAL_OPS(4,256)
+
+PHP_HASH_HAVAL_OPS(5,128)
+PHP_HASH_HAVAL_OPS(5,160)
+PHP_HASH_HAVAL_OPS(5,192)
+PHP_HASH_HAVAL_OPS(5,224)
+PHP_HASH_HAVAL_OPS(5,256)
+
 extern zend_module_entry hash_module_entry;
 #define phpext_hash_ptr &hash_module_entry
 
diff --git a/ext/hash/php_hash_haval.h b/ext/hash/php_hash_haval.h
new file mode 100644 (file)
index 0000000..67b640c
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2005 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.0 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_0.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$ */
+
+#ifndef PHP_HASH_HAVAL_H
+#define PHP_HASH_HAVAL_H
+
+#include "ext/standard/basic_functions.h"
+/* HAVAL context. */
+typedef struct {
+       php_uint32 state[8];
+       php_uint32 count[2];
+       unsigned char buffer[128];
+
+       char passes;
+       short output;
+       void (*Transform)(php_uint32 state[8], const unsigned char block[128]);
+} PHP_HAVAL_CTX;
+
+#define PHP_HASH_HAVAL_INIT_DECL(p,b)  PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *); \
+                                                                               PHP_HASH_API void PHP_HAVAL##b##Final(unsigned char*, PHP_HAVAL_CTX *);
+
+PHP_HASH_API void PHP_HAVALUpdate(PHP_HAVAL_CTX *, const unsigned char *, unsigned int);
+
+PHP_HASH_HAVAL_INIT_DECL(3,128)
+PHP_HASH_HAVAL_INIT_DECL(3,160)
+PHP_HASH_HAVAL_INIT_DECL(3,192)
+PHP_HASH_HAVAL_INIT_DECL(3,224)
+PHP_HASH_HAVAL_INIT_DECL(3,256)
+
+PHP_HASH_HAVAL_INIT_DECL(4,128)
+PHP_HASH_HAVAL_INIT_DECL(4,160)
+PHP_HASH_HAVAL_INIT_DECL(4,192)
+PHP_HASH_HAVAL_INIT_DECL(4,224)
+PHP_HASH_HAVAL_INIT_DECL(4,256)
+
+PHP_HASH_HAVAL_INIT_DECL(5,128)
+PHP_HASH_HAVAL_INIT_DECL(5,160)
+PHP_HASH_HAVAL_INIT_DECL(5,192)
+PHP_HASH_HAVAL_INIT_DECL(5,224)
+PHP_HASH_HAVAL_INIT_DECL(5,256)
+
+#endif
diff --git a/ext/hash/tests/haval.phpt b/ext/hash/tests/haval.phpt
new file mode 100644 (file)
index 0000000..5a034c7
--- /dev/null
@@ -0,0 +1,76 @@
+--TEST--
+haval algorithm (multi-vector, multi-pass, multi-width)
+--SKIPIF--
+<?php if(!extension_loaded("hash")) print "skip"; ?>
+--FILE--
+<?php
+echo "Empty String\n";
+for($pass=3; $pass<=5; $pass++)
+       for($bits=128; $bits <= 256; $bits += 32) {
+               $algo = sprintf('haval%d,%d',$bits,$pass);
+               echo $algo . ': ' . hash($algo,'') . "\n";
+       }
+
+echo "\"abc\"\n";
+for($pass=3; $pass<=5; $pass++)
+       for($bits=128; $bits <= 256; $bits += 32) {
+               $algo = sprintf('haval%d,%d',$bits,$pass);
+               echo $algo . ': ' . hash($algo,'abc') . "\n";
+       }
+
+echo "\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789\"\n";
+for($pass=3; $pass<=5; $pass++)
+       for($bits=128; $bits <= 256; $bits += 32) {
+               $algo = sprintf('haval%d,%d',$bits,$pass);
+               echo $algo . ': ' . hash($algo,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789') . "\n";
+       }
+
+--EXPECTF--
+Empty String
+haval128,3: c68f39913f901f3ddf44c707357a7d70
+haval160,3: d353c3ae22a25401d257643836d7231a9a95f953
+haval192,3: e9c48d7903eaf2a91c5b350151efcb175c0fc82de2289a4e
+haval224,3: c5aae9d47bffcaaf84a8c6e7ccacd60a0dd1932be7b1a192b9214b6d
+haval256,3: 4f6938531f0bc8991f62da7bbd6f7de3fad44562b8c6f4ebf146d5b4e46f7c17
+haval128,4: ee6bbf4d6a46a679b3a856c88538bb98
+haval160,4: 1d33aae1be4146dbaaca0b6e70d7a11f10801525
+haval192,4: 4a8372945afa55c7dead800311272523ca19d42ea47b72da
+haval224,4: 3e56243275b3b81561750550e36fcd676ad2f5dd9e15f2e89e6ed78e
+haval256,4: c92b2e23091e80e375dadce26982482d197b1a2521be82da819f8ca2c579b99b
+haval128,5: 184b8482a0c050dca54b59c7f05bf5dd
+haval160,5: 255158cfc1eed1a7be7c55ddd64d9790415b933b
+haval192,5: 4839d0626f95935e17ee2fc4509387bbe2cc46cb382ffe85
+haval224,5: 4a0513c032754f5582a758d35917ac9adf3854219b39e3ac77d1837e
+haval256,5: be417bb4dd5cfb76c7126f4f8eeb1553a449039307b1a3cd451dbfdc0fbbe330
+"abc"
+haval128,3: 9e40ed883fb63e985d299b40cda2b8f2
+haval160,3: b21e876c4d391e2a897661149d83576b5530a089
+haval192,3: a7b14c9ef3092319b0e75e3b20b957d180bf20745629e8de
+haval224,3: 5bc955220ba2346a948d2848eca37bdd5eca6ecca7b594bd32923fab
+haval256,3: 8699f1e3384d05b2a84b032693e2b6f46df85a13a50d93808d6874bb8fb9e86c
+haval128,4: 6f2132867c9648419adcd5013e532fa2
+haval160,4: 77aca22f5b12cc09010afc9c0797308638b1cb9b
+haval192,4: 7e29881ed05c915903dd5e24a8e81cde5d910142ae66207c
+haval224,4: 124c43d2ba4884599d013e8c872bfea4c88b0b6bf6303974cbe04e68
+haval256,4: 8f409f1bb6b30c5016fdce55f652642261575bedca0b9533f32f5455459142b5
+haval128,5: d054232fe874d9c6c6dc8e6a853519ea
+haval160,5: ae646b04845e3351f00c5161d138940e1fa0c11c
+haval192,5: d12091104555b00119a8d07808a3380bf9e60018915b9025
+haval224,5: 8081027a500147c512e5f1055986674d746d92af4841abeb89da64ad
+haval256,5: 976cd6254c337969e5913b158392a2921af16fca51f5601d486e0a9de01156e7
+"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789"
+haval128,3: ddf4304cc5ffa3db8aab60d4f8fc2a00
+haval160,3: e709559359b15917623050e41d27a306c6c3a9db
+haval192,3: 51e25280ad356c06f4b913b3cdb3abaaac5879dda0a4fea4
+haval224,3: 28aa2c164e10bb3076574cc8aa8584fd6d04f6d82c37ea5c21e451b3
+haval256,3: 5537364e3d75174b846d21adf9b113f9d8f97e4750df64d428c01e782f9ade4d
+haval128,4: c7d981e8270e39888ba96cafe8745636
+haval160,4: 3444e38cc2a132b818b554ced8f7d9592df28f57
+haval192,4: 0ca58f140ed92828a27913ce5636611abcada220fccf3af7
+haval224,4: a9d0571d0857773e71363e4e9dfcca4696dba3e5019e7225e65e0cb1
+haval256,4: 1858d106bdc2fc787445364a163cfc6027597a45a58a2490d14203c8b9bdd268
+haval128,5: d41e927ea041d2f0c255352b1a9f6195
+haval160,5: f3245e222e6581d0c3077bd7af322af4b4fedab7
+haval192,5: fc45dc17a7b19adfed2a6485921f7af7951d70703b9357c1
+haval224,5: 29687958a6f0d54d495105df00dbda0153ee0f5708408db68a5bbea5
+haval256,5: f93421623f852ac877584d1e4bba5d9345a95f81bfd277fe36dfeed1815f83d5