Test siphash using a test vector. Returns 0 if the function passed * all the tests, otherwise 1 is returned. * * IMPORTANT: The test vector is for SipHash 2-4. Before running * the test revert back the siphash() function to 2-4 rounds since * now it uses 1-2 rounds. */
| 312 | * the test revert back the siphash() function to 2-4 rounds since |
| 313 | * now it uses 1-2 rounds. */ |
| 314 | int siphash_test(void) { |
| 315 | uint8_t in[64], k[16]; |
| 316 | int i; |
| 317 | int fails = 0; |
| 318 | |
| 319 | for (i = 0; i < 16; ++i) |
| 320 | k[i] = i; |
| 321 | |
| 322 | for (i = 0; i < 64; ++i) { |
| 323 | in[i] = i; |
| 324 | uint64_t hash = siphash(in, i, k); |
| 325 | const uint8_t *v = NULL; |
| 326 | v = (uint8_t *)vectors_sip64; |
| 327 | if (memcmp(&hash, v + (i * 8), 8)) { |
| 328 | /* printf("fail for %d bytes\n", i); */ |
| 329 | fails++; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* Run a few basic tests with the case insensitive version. */ |
| 334 | uint64_t h1, h2; |
| 335 | h1 = siphash((uint8_t*)"hello world",11,(uint8_t*)"1234567812345678"); |
| 336 | h2 = siphash_nocase((uint8_t*)"hello world",11,(uint8_t*)"1234567812345678"); |
| 337 | if (h1 != h2) fails++; |
| 338 | |
| 339 | h1 = siphash((uint8_t*)"hello world",11,(uint8_t*)"1234567812345678"); |
| 340 | h2 = siphash_nocase((uint8_t*)"HELLO world",11,(uint8_t*)"1234567812345678"); |
| 341 | if (h1 != h2) fails++; |
| 342 | |
| 343 | h1 = siphash((uint8_t*)"HELLO world",11,(uint8_t*)"1234567812345678"); |
| 344 | h2 = siphash_nocase((uint8_t*)"HELLO world",11,(uint8_t*)"1234567812345678"); |
| 345 | if (h1 == h2) fails++; |
| 346 | |
| 347 | if (!fails) return 0; |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | int main(void) { |
| 352 | if (siphash_test() == 0) { |
no test coverage detected