00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "config.h"
00038
00039 #include "Hash.h"
00040
00041
00042
00043
00044
00045
00046 int hash_cnt_all = 0, hash_cnt_uhash = 0;
00047
00048 #if defined(USE_UHASH) || defined(USE_UMAC_NH)
00049 uint8 uhash_key[UHASH_KEY_SIZE];
00050 #endif
00051
00052 #ifdef USE_DIETZFELBINGER
00053 #include "TwoWise.h"
00054 const TwoWise* two_wise;
00055 #endif
00056
00057 #ifdef USE_H3
00058 #include "h3.h"
00059 const H3<hash_t, UHASH_KEY_SIZE>* h3;
00060 #endif
00061
00062 void init_hash_function()
00063 {
00064 if ( UHASH_KEY_SIZE % 16 != 0 )
00065 internal_error("uhash key size must be multiply of 16");
00066
00067
00068
00069 #ifdef USE_DIETZFELBINGER
00070 two_wise = new TwoWise((UHASH_KEY_SIZE + 3) >> 2);
00071 #endif
00072
00073 #ifdef USE_H3
00074 h3 = new H3<hash_t, UHASH_KEY_SIZE>();
00075 #endif
00076
00077 #if defined(USE_UHASH) || defined(USE_UMAC_NH)
00078 uint32 buf2[5];
00079 memcpy(buf2, shared_hmac_md5_key, sizeof(shared_hmac_md5_key));
00080
00081 for ( int i = 0; i < UHASH_KEY_SIZE; i += 16 )
00082 {
00083 buf2[4] = i;
00084 hmac_md5(sizeof(buf2), (const unsigned char*) buf2, uhash_key + i);
00085 }
00086 #endif
00087 }
00088
00089 HashKey::HashKey(bro_int_t i)
00090 {
00091 key_u.i = i;
00092 key = (void*) &key_u;
00093 size = sizeof(i);
00094 hash = HashBytes(key, size);
00095 is_our_dynamic = 0;
00096 }
00097
00098 HashKey::HashKey(bro_uint_t u)
00099 {
00100 key_u.i = bro_int_t(u);
00101 key = (void*) &key_u;
00102 size = sizeof(u);
00103 hash = HashBytes(key, size);
00104 is_our_dynamic = 0;
00105 }
00106
00107 #ifdef USE_INT64
00108
00109 HashKey::HashKey(uint32 u)
00110 {
00111 key_u.u32 = u;
00112 key = (void*) &key_u;
00113 size = sizeof(u);
00114 hash = HashBytes(key, size);
00115 is_our_dynamic = 0;
00116 }
00117
00118 #endif // USE_INT64
00119
00120 HashKey::HashKey(const uint32 u[], int n)
00121 {
00122 size = n * sizeof(u[0]);
00123 key = (void*) u;
00124 hash = HashBytes(key, size);
00125 is_our_dynamic = 0;
00126 }
00127
00128 HashKey::HashKey(double d)
00129 {
00130 union {
00131 double d;
00132 int i[2];
00133 } u;
00134
00135 key_u.d = u.d = d;
00136 key = (void*) &key_u;
00137 size = sizeof(d);
00138 hash = HashBytes(key, size);
00139 is_our_dynamic = 0;
00140 }
00141
00142 HashKey::HashKey(const void* p)
00143 {
00144 key_u.p = p;
00145 key = (void*) &key_u;
00146 size = sizeof(p);
00147 hash = HashBytes(key, size);
00148 is_our_dynamic = 0;
00149 }
00150
00151 HashKey::HashKey(const char* s)
00152 {
00153 size = strlen(s);
00154 key = (void*) s;
00155 hash = HashBytes(key, size);
00156 is_our_dynamic = 0;
00157 }
00158
00159 HashKey::HashKey(const BroString* s)
00160 {
00161 size = s->Len();
00162 key = (void*) s->Bytes();
00163 hash = HashBytes(key, size);
00164 is_our_dynamic = 0;
00165 }
00166
00167 HashKey::HashKey(int copy_key, void* arg_key, int arg_size)
00168 {
00169 size = arg_size;
00170 is_our_dynamic = 1;
00171
00172 if ( copy_key )
00173 {
00174 key = (void*) new char[size];
00175 memcpy(key, arg_key, size);
00176 }
00177 else
00178 key = arg_key;
00179
00180 hash = HashBytes(key, size);
00181 }
00182
00183 HashKey::HashKey(const void* arg_key, int arg_size, hash_t arg_hash)
00184 {
00185 size = arg_size;
00186 hash = arg_hash;
00187 key = CopyKey(arg_key, size);
00188 is_our_dynamic = 1;
00189 }
00190
00191 HashKey::HashKey(const void* arg_key, int arg_size, hash_t arg_hash,
00192 bool )
00193 {
00194 size = arg_size;
00195 hash = arg_hash;
00196 key = const_cast<void*>(arg_key);
00197 is_our_dynamic = 0;
00198 }
00199
00200 HashKey::HashKey(const void* bytes, int arg_size)
00201 {
00202 size = arg_size;
00203 key = CopyKey(bytes, size);
00204 hash = HashBytes(key, size);
00205 is_our_dynamic = 1;
00206 }
00207
00208 void* HashKey::TakeKey()
00209 {
00210 if ( is_our_dynamic )
00211 {
00212 is_our_dynamic = 0;
00213 return key;
00214 }
00215 else
00216 return CopyKey(key, size);
00217 }
00218
00219 void* HashKey::CopyKey(const void* k, int s) const
00220 {
00221 void* k_copy = (void*) new char[s];
00222 memcpy(k_copy, k, s);
00223 return k_copy;
00224 }
00225
00226 hash_t HashKey::HashBytes(const void* bytes, int size)
00227 {
00228 #ifdef USE_OLD_BRO_HASH
00229
00230 const unsigned char* cp = (const unsigned char*) bytes;
00231 hash_t h = 0;
00232
00233 for ( int i = 0; i < size; ++i )
00234
00235 h = (h >> 31) + (h << 1) + cp[i];
00236
00237 return h;
00238 #endif
00239
00240 ++hash_cnt_all;
00241
00242 #ifdef USE_H3
00243 if ( size == 0 )
00244 return 0;
00245
00246 if ( size <= UHASH_KEY_SIZE )
00247 {
00248 ++hash_cnt_uhash;
00249 return (*h3)(bytes, size);
00250 }
00251 #endif
00252
00253 #ifdef USE_DIETZFELBINGER
00254 if ( size <= UHASH_KEY_SIZE )
00255 {
00256 ++hash_cnt_uhash;
00257 return two_wise->Hash(size, bytes);
00258 }
00259 #endif
00260
00261 #ifdef USE_UMAC_NH
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 typedef uint16 uhash_word_t;
00275 typedef uint32 uhash_dword_t;
00276
00277 if ( (unsigned int) size <= UHASH_KEY_SIZE - sizeof(uhash_word_t) )
00278 {
00279 ++hash_cnt_uhash;
00280
00281 const uhash_word_t* uhash = (const uhash_word_t *) uhash_key;
00282 const uint8* data = (const uint8 *) bytes;
00283 uhash_dword_t sum = 0;
00284
00285 int i, j, k;
00286 uhash_word_t x, y;
00287 k = 0;
00288
00289 for ( i = 0; i + sizeof(uhash_word_t) < size; )
00290 {
00291 x = 0;
00292
00293 for ( j = 0; j < sizeof(uhash_word_t) && i + j < size; ++j )
00294 x = (x << 8) | data[i+j];
00295
00296 i += j;
00297 x += uhash[k++];
00298 y = 0;
00299
00300 for ( j = 0; j < sizeof(uhash_word_t) && i + j < size; ++j )
00301 y = (y << 8) | data[i+j];
00302
00303 i += j;
00304 y += uhash[k++];
00305
00306 sum += ((uhash_dword_t) x) * ((uhash_dword_t) y);
00307 }
00308
00309 x = 0;
00310 for ( j = 0; j < sizeof(uhash_word_t) && i + j < size; ++j )
00311 x = (x << 8) | data[i+j];
00312 x += uhash[k++];
00313
00314 y = size + uhash[k++];
00315 sum += ((uhash_dword_t) x) * ((uhash_dword_t) y);
00316
00317 return hash_t(sum);
00318 }
00319 #endif
00320
00321 #ifdef USE_UHASH
00322
00323 typedef uint32 uhash_word_t;
00324
00325
00326
00327
00328
00329 typedef unsigned long long uint64;
00330
00331 typedef uint64 uhash_dword_t;
00332
00333 if ( (unsigned int) size <= UHASH_KEY_SIZE - sizeof(uhash_word_t) )
00334 {
00335 ++hash_cnt_uhash;
00336
00337 const uhash_word_t* uhash = (const uhash_word_t *) uhash_key;
00338 static const uhash_dword_t uhash_prime = (uint64) 1 << 32 + 15;
00339
00340 uhash_dword_t sum =
00341 (uhash_dword_t) (size + 1) *
00342 ((uhash_dword_t) uhash[0] + 1);
00343
00344 int i, j, k;
00345 const uint8* data = (const uint8*) bytes;
00346 for ( i = 0, k = 1; i < size; i += sizeof(uhash_word_t), ++k )
00347 {
00348 uhash_dword_t x = 0;
00349 for ( j = 0; unsigned(j) < sizeof(uhash_word_t) && i + j < size; ++j )
00350 x = (x << 8) | (uhash_dword_t) data[i+j];
00351
00352 sum += ((x + 1) * ((uhash_dword_t) uhash[k] + 1)) %
00353 uhash_prime;
00354 }
00355
00356 return hash_t((sum % uhash_prime) & 0xffffffff);
00357 }
00358
00359 #endif
00360
00361
00362 hash_t digest[16];
00363 hmac_md5(size, (unsigned char*) bytes, (unsigned char*) digest);
00364 return digest[0];
00365 }