Back to index

php5  5.3.10
hash_tiger.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Authors: Michael Wallner <mike@php.net>                              |
00016   |          Sara Golemon <pollita@php.net>                              |
00017   +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: hash_tiger.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "php_hash.h"
00023 #include "php_hash_tiger.h"
00024 #include "php_hash_tiger_tables.h"
00025 
00026 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
00027 # if defined(__LITTLE_ENDIAN__)
00028 #  undef WORDS_BIGENDIAN
00029 # else 
00030 #  if defined(__BIG_ENDIAN__)
00031 #   define WORDS_BIGENDIAN
00032 #  endif
00033 # endif
00034 #endif
00035 
00036 /* {{{ */
00037 #define save_abc \
00038        aa = a; \
00039        bb = b; \
00040        cc = c;
00041 
00042 #define round(a,b,c,x,mul) \
00043        c ^= x; \
00044        a -= t1[(unsigned char)(c)] ^ \
00045               t2[(unsigned char)(((php_hash_uint32)(c))>>(2*8))] ^ \
00046               t3[(unsigned char)((c)>>(4*8))] ^ \
00047               t4[(unsigned char)(((php_hash_uint32)((c)>>(4*8)))>>(2*8))] ; \
00048        b += t4[(unsigned char)(((php_hash_uint32)(c))>>(1*8))] ^ \
00049               t3[(unsigned char)(((php_hash_uint32)(c))>>(3*8))] ^ \
00050               t2[(unsigned char)(((php_hash_uint32)((c)>>(4*8)))>>(1*8))] ^ \
00051               t1[(unsigned char)(((php_hash_uint32)((c)>>(4*8)))>>(3*8))]; \
00052        b *= mul;
00053 
00054 #define pass(a,b,c,mul) \
00055        round(a,b,c,x0,mul) \
00056        round(b,c,a,x1,mul) \
00057        round(c,a,b,x2,mul) \
00058        round(a,b,c,x3,mul) \
00059        round(b,c,a,x4,mul) \
00060        round(c,a,b,x5,mul) \
00061        round(a,b,c,x6,mul) \
00062        round(b,c,a,x7,mul)
00063 
00064 #define key_schedule \
00065        x0 -= x7 ^ L64(0xA5A5A5A5A5A5A5A5); \
00066        x1 ^= x0; \
00067        x2 += x1; \
00068        x3 -= x2 ^ ((~x1)<<19); \
00069        x4 ^= x3; \
00070        x5 += x4; \
00071        x6 -= x5 ^ ((~x4)>>23); \
00072        x7 ^= x6; \
00073        x0 += x7; \
00074        x1 -= x0 ^ ((~x7)<<19); \
00075        x2 ^= x1; \
00076        x3 += x2; \
00077        x4 -= x3 ^ ((~x2)>>23); \
00078        x5 ^= x4; \
00079        x6 += x5; \
00080        x7 -= x6 ^ L64(0x0123456789ABCDEF);
00081 
00082 #define feedforward \
00083        a ^= aa; \
00084        b -= bb; \
00085        c += cc;
00086 
00087 #define compress(passes) \
00088        save_abc \
00089        pass(a,b,c,5) \
00090        key_schedule \
00091        pass(c,a,b,7) \
00092        key_schedule \
00093        pass(b,c,a,9) \
00094        for(pass_no=0; pass_no<passes; pass_no++) { \
00095               key_schedule \
00096               pass(a,b,c,9) \
00097               tmpa=a; a=c; c=b; b=tmpa; \
00098        } \
00099        feedforward
00100 
00101 #define split_ex(str) \
00102        x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3]; \
00103        x4=str[4]; x5=str[5]; x6=str[6]; x7=str[7];
00104 #ifdef WORDS_BIGENDIAN
00105 #      define split(str) \
00106        { \
00107               int i; \
00108               php_hash_uint64 tmp[8]; \
00109                \
00110               for (i = 0; i < 64; ++i) { \
00111                      ((unsigned char *) tmp)[i^7] = ((unsigned char *) str)[i]; \
00112               } \
00113               split_ex(tmp); \
00114        }
00115 #else
00116 #      define split split_ex
00117 #endif
00118 
00119 #define tiger_compress(passes, str, state) \
00120 { \
00121        register php_hash_uint64 a, b, c, tmpa, x0, x1, x2, x3, x4, x5, x6, x7; \
00122        php_hash_uint64 aa, bb, cc; \
00123        int pass_no; \
00124        \
00125        a = state[0]; \
00126        b = state[1]; \
00127        c = state[2]; \
00128        \
00129        split(str); \
00130        \
00131        compress(passes); \
00132        \
00133        state[0] = a; \
00134        state[1] = b; \
00135        state[2] = c; \
00136 }
00137 /* }}} */
00138 
00139 static inline void TigerFinalize(PHP_TIGER_CTX *context)
00140 {
00141        context->passed += (php_hash_uint64) context->length << 3;
00142        
00143        context->buffer[context->length++] = 0x1;
00144        if (context->length % 8) {
00145               memset(&context->buffer[context->length], 0, 8-context->length%8);
00146               context->length += 8-context->length%8;
00147        }
00148        
00149        if (context->length > 56) {
00150               memset(&context->buffer[context->length], 0, 64 - context->length);
00151               tiger_compress(context->passes, ((php_hash_uint64 *) context->buffer), context->state);
00152               memset(context->buffer, 0, 56);
00153        } else {
00154               memset(&context->buffer[context->length], 0, 56 - context->length);
00155        }
00156 
00157 #ifndef WORDS_BIGENDIAN     
00158        memcpy(&context->buffer[56], &context->passed, sizeof(php_hash_uint64));
00159 #else
00160        context->buffer[56] = (unsigned char) (context->passed & 0xff);
00161        context->buffer[57] = (unsigned char) ((context->passed >> 8) & 0xff);
00162        context->buffer[58] = (unsigned char) ((context->passed >> 16) & 0xff);
00163        context->buffer[59] = (unsigned char) ((context->passed >> 24) & 0xff);
00164        context->buffer[60] = (unsigned char) ((context->passed >> 32) & 0xff);
00165        context->buffer[61] = (unsigned char) ((context->passed >> 40) & 0xff);
00166        context->buffer[62] = (unsigned char) ((context->passed >> 48) & 0xff);
00167        context->buffer[63] = (unsigned char) ((context->passed >> 56) & 0xff);
00168 #endif
00169        tiger_compress(context->passes, ((php_hash_uint64 *) context->buffer), context->state);
00170 }
00171 
00172 PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context)
00173 {
00174        memset(context, 0, sizeof(*context));
00175        context->state[0] = L64(0x0123456789ABCDEF);
00176        context->state[1] = L64(0xFEDCBA9876543210);
00177        context->state[2] = L64(0xF096A5B4C3B2E187);
00178 }
00179 
00180 PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context)
00181 {
00182        memset(context, 0, sizeof(*context));
00183        context->passes = 1;
00184        context->state[0] = L64(0x0123456789ABCDEF);
00185        context->state[1] = L64(0xFEDCBA9876543210);
00186        context->state[2] = L64(0xF096A5B4C3B2E187);
00187 }
00188 
00189 PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len)
00190 {
00191        if (context->length + len < 64) {
00192               memcpy(&context->buffer[context->length], input, len);
00193               context->length += len;
00194        } else {
00195               size_t i = 0, r = (context->length + len) % 64;
00196               
00197               if (context->length) {
00198                      i = 64 - context->length;
00199                      memcpy(&context->buffer[context->length], input, i);
00200                      tiger_compress(context->passes, ((const php_hash_uint64 *) context->buffer), context->state);
00201                      memset(context->buffer, 0, 64);
00202                      context->passed += 512;
00203               }
00204               
00205               for (; i + 64 <= len; i += 64) {
00206                      memcpy(context->buffer, &input[i], 64);
00207                      tiger_compress(context->passes, ((const php_hash_uint64 *) context->buffer), context->state);
00208                      context->passed += 512;
00209               }
00210               memset(&context->buffer[r], 0, 64-r);
00211               memcpy(context->buffer, &input[i], r);
00212               context->length = r;
00213        }
00214 }
00215 
00216 PHP_HASH_API void PHP_TIGER128Final(unsigned char digest[16], PHP_TIGER_CTX *context)
00217 {
00218        TigerFinalize(context);
00219        
00220        digest[0] = (unsigned char) ((context->state[0] >> 56) & 0xff);
00221        digest[1] = (unsigned char) ((context->state[0] >> 48) & 0xff);
00222        digest[2] = (unsigned char) ((context->state[0] >> 40) & 0xff);
00223        digest[3] = (unsigned char) ((context->state[0] >> 32) & 0xff);
00224        digest[4] = (unsigned char) ((context->state[0] >> 24) & 0xff);
00225        digest[5] = (unsigned char) ((context->state[0] >> 16) & 0xff);
00226        digest[6] = (unsigned char) ((context->state[0] >> 8) & 0xff);
00227        digest[7] = (unsigned char) (context->state[0] & 0xff);
00228        digest[8] = (unsigned char) ((context->state[1] >> 56) & 0xff);
00229        digest[9] = (unsigned char) ((context->state[1] >> 48) & 0xff);
00230        digest[10] = (unsigned char) ((context->state[1] >> 40) & 0xff);
00231        digest[11] = (unsigned char) ((context->state[1] >> 32) & 0xff);
00232        digest[12] = (unsigned char) ((context->state[1] >> 24) & 0xff);
00233        digest[13] = (unsigned char) ((context->state[1] >> 16) & 0xff);
00234        digest[14] = (unsigned char) ((context->state[1] >> 8) & 0xff);
00235        digest[15] = (unsigned char) (context->state[1] & 0xff);
00236        
00237        memset(context, 0, sizeof(*context));
00238 }
00239 
00240 PHP_HASH_API void PHP_TIGER160Final(unsigned char digest[20], PHP_TIGER_CTX *context)
00241 {
00242        TigerFinalize(context);
00243        
00244        digest[0] = (unsigned char) ((context->state[0] >> 56) & 0xff);
00245        digest[1] = (unsigned char) ((context->state[0] >> 48) & 0xff);
00246        digest[2] = (unsigned char) ((context->state[0] >> 40) & 0xff);
00247        digest[3] = (unsigned char) ((context->state[0] >> 32) & 0xff);
00248        digest[4] = (unsigned char) ((context->state[0] >> 24) & 0xff);
00249        digest[5] = (unsigned char) ((context->state[0] >> 16) & 0xff);
00250        digest[6] = (unsigned char) ((context->state[0] >> 8) & 0xff);
00251        digest[7] = (unsigned char) (context->state[0] & 0xff);
00252        digest[8] = (unsigned char) ((context->state[1] >> 56) & 0xff);
00253        digest[9] = (unsigned char) ((context->state[1] >> 48) & 0xff);
00254        digest[10] = (unsigned char) ((context->state[1] >> 40) & 0xff);
00255        digest[11] = (unsigned char) ((context->state[1] >> 32) & 0xff);
00256        digest[12] = (unsigned char) ((context->state[1] >> 24) & 0xff);
00257        digest[13] = (unsigned char) ((context->state[1] >> 16) & 0xff);
00258        digest[14] = (unsigned char) ((context->state[1] >> 8) & 0xff);
00259        digest[15] = (unsigned char) (context->state[1] & 0xff);
00260        digest[16] = (unsigned char) ((context->state[2] >> 56) & 0xff);
00261        digest[17] = (unsigned char) ((context->state[2] >> 48) & 0xff);
00262        digest[18] = (unsigned char) ((context->state[2] >> 40) & 0xff);
00263        digest[19] = (unsigned char) ((context->state[2] >> 32) & 0xff);
00264        
00265        memset(context, 0, sizeof(*context));
00266 }
00267 
00268 PHP_HASH_API void PHP_TIGER192Final(unsigned char digest[24], PHP_TIGER_CTX *context)
00269 {
00270        TigerFinalize(context);
00271        
00272        digest[0] = (unsigned char) ((context->state[0] >> 56) & 0xff);
00273        digest[1] = (unsigned char) ((context->state[0] >> 48) & 0xff);
00274        digest[2] = (unsigned char) ((context->state[0] >> 40) & 0xff);
00275        digest[3] = (unsigned char) ((context->state[0] >> 32) & 0xff);
00276        digest[4] = (unsigned char) ((context->state[0] >> 24) & 0xff);
00277        digest[5] = (unsigned char) ((context->state[0] >> 16) & 0xff);
00278        digest[6] = (unsigned char) ((context->state[0] >> 8) & 0xff);
00279        digest[7] = (unsigned char) (context->state[0] & 0xff);
00280        digest[8] = (unsigned char) ((context->state[1] >> 56) & 0xff);
00281        digest[9] = (unsigned char) ((context->state[1] >> 48) & 0xff);
00282        digest[10] = (unsigned char) ((context->state[1] >> 40) & 0xff);
00283        digest[11] = (unsigned char) ((context->state[1] >> 32) & 0xff);
00284        digest[12] = (unsigned char) ((context->state[1] >> 24) & 0xff);
00285        digest[13] = (unsigned char) ((context->state[1] >> 16) & 0xff);
00286        digest[14] = (unsigned char) ((context->state[1] >> 8) & 0xff);
00287        digest[15] = (unsigned char) (context->state[1] & 0xff);
00288        digest[16] = (unsigned char) ((context->state[2] >> 56) & 0xff);
00289        digest[17] = (unsigned char) ((context->state[2] >> 48) & 0xff);
00290        digest[18] = (unsigned char) ((context->state[2] >> 40) & 0xff);
00291        digest[19] = (unsigned char) ((context->state[2] >> 32) & 0xff);
00292        digest[20] = (unsigned char) ((context->state[2] >> 24) & 0xff);
00293        digest[21] = (unsigned char) ((context->state[2] >> 16) & 0xff);
00294        digest[22] = (unsigned char) ((context->state[2] >> 8) & 0xff);
00295        digest[23] = (unsigned char) (context->state[2] & 0xff);
00296        
00297        memset(context, 0, sizeof(*context));
00298 }
00299 
00300 #define PHP_HASH_TIGER_OPS(p, b) \
00301        const php_hash_ops php_hash_##p##tiger##b##_ops = { \
00302               (php_hash_init_func_t) PHP_##p##TIGERInit, \
00303               (php_hash_update_func_t) PHP_TIGERUpdate, \
00304               (php_hash_final_func_t) PHP_TIGER##b##Final, \
00305               (php_hash_copy_func_t) php_hash_copy, \
00306               b/8, \
00307               64, \
00308               sizeof(PHP_TIGER_CTX) \
00309        }
00310 
00311 PHP_HASH_TIGER_OPS(3, 128);
00312 PHP_HASH_TIGER_OPS(3, 160);
00313 PHP_HASH_TIGER_OPS(3, 192);
00314 PHP_HASH_TIGER_OPS(4, 128);
00315 PHP_HASH_TIGER_OPS(4, 160);
00316 PHP_HASH_TIGER_OPS(4, 192);
00317 
00318 /*
00319  * Local variables:
00320  * tab-width: 4
00321  * c-basic-offset: 4
00322  * End:
00323  * vim600: sw=4 ts=4 fdm=marker
00324  * vim<600: sw=4 ts=4
00325  */