LCOV - code coverage report
Current view: top level - test - test-siphash24.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 63 63 100.0 %
Date: 2019-08-22 15:41:25 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include "memory-util.h"
       4             : #include "siphash24.h"
       5             : 
       6             : #define ITERATIONS 10000000ULL
       7             : 
       8           4 : static void do_test(const uint8_t *in, size_t len, const uint8_t *key) {
       9           4 :         struct siphash state = {};
      10             :         uint64_t out;
      11             :         unsigned i, j;
      12             : 
      13           4 :         out = siphash24(in, len, key);
      14           4 :         assert_se(out == 0xa129ca6149be45e5);
      15             : 
      16             :         /* verify the internal state as given in the above paper */
      17           4 :         siphash24_init(&state, key);
      18           4 :         assert_se(state.v0 == 0x7469686173716475);
      19           4 :         assert_se(state.v1 == 0x6b617f6d656e6665);
      20           4 :         assert_se(state.v2 == 0x6b7f62616d677361);
      21           4 :         assert_se(state.v3 == 0x7b6b696e727e6c7b);
      22           4 :         siphash24_compress(in, len, &state);
      23           4 :         assert_se(state.v0 == 0x4a017198de0a59e0);
      24           4 :         assert_se(state.v1 == 0x0d52f6f62a4f59a4);
      25           4 :         assert_se(state.v2 == 0x634cb3577b01fd3d);
      26           4 :         assert_se(state.v3 == 0xa5224d6f55c7d9c8);
      27           4 :         out = siphash24_finalize(&state);
      28           4 :         assert_se(out == 0xa129ca6149be45e5);
      29           4 :         assert_se(state.v0 == 0xf6bcd53893fecff1);
      30           4 :         assert_se(state.v1 == 0x54b9964c7ea0d937);
      31           4 :         assert_se(state.v2 == 0x1b38329c099bb55a);
      32           4 :         assert_se(state.v3 == 0x1814bb89ad7be679);
      33             : 
      34             :         /* verify that decomposing the input in three chunks gives the
      35             :            same result */
      36          64 :         for (i = 0; i < len; i++) {
      37         540 :                 for (j = i; j < len; j++) {
      38         480 :                         siphash24_init(&state, key);
      39         480 :                         siphash24_compress(in, i, &state);
      40         480 :                         siphash24_compress(&in[i], j - i, &state);
      41         480 :                         siphash24_compress(&in[j], len - j, &state);
      42         480 :                         out = siphash24_finalize(&state);
      43         480 :                         assert_se(out == 0xa129ca6149be45e5);
      44             :                 }
      45             :         }
      46           4 : }
      47             : 
      48           1 : static void test_short_hashes(void) {
      49           1 :         const uint8_t one[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
      50             :                                 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
      51           1 :         const uint8_t  key[16] = { 0x22, 0x24, 0x41, 0x22, 0x55, 0x77, 0x88, 0x07,
      52             :                                    0x23, 0x09, 0x23, 0x14, 0x0c, 0x33, 0x0e, 0x0f};
      53           1 :         uint8_t two[sizeof one] = {};
      54             : 
      55           1 :         struct siphash state1 = {}, state2 = {};
      56             :         unsigned i, j;
      57             : 
      58           1 :         siphash24_init(&state1, key);
      59           1 :         siphash24_init(&state2, key);
      60             : 
      61             :         /* hashing 1, 2, 3, 4, 5, ..., 16 bytes, with the byte after the buffer different */
      62          17 :         for (i = 1; i <= sizeof one; i++) {
      63          16 :                 siphash24_compress(one, i, &state1);
      64             : 
      65          16 :                 two[i-1] = one[i-1];
      66          16 :                 siphash24_compress(two, i, &state2);
      67             : 
      68          16 :                 assert_se(memcmp(&state1, &state2, sizeof state1) == 0);
      69             :         }
      70             : 
      71             :         /* hashing n and 1, n and 2, n and 3, ..., n-1 and 1, n-2 and 2, ... */
      72          17 :         for (i = sizeof one; i > 0; i--) {
      73          16 :                 zero(two);
      74             : 
      75         272 :                 for (j = 1; j <= sizeof one; j++) {
      76         256 :                         siphash24_compress(one, i, &state1);
      77         256 :                         siphash24_compress(one, j, &state1);
      78             : 
      79         256 :                         siphash24_compress(one, i, &state2);
      80         256 :                         two[j-1] = one[j-1];
      81         256 :                         siphash24_compress(two, j, &state2);
      82             : 
      83         256 :                         assert_se(memcmp(&state1, &state2, sizeof state1) == 0);
      84             :                 }
      85             :         }
      86           1 : }
      87             : 
      88             : /* see https://131002.net/siphash/siphash.pdf, Appendix A */
      89           1 : int main(int argc, char *argv[]) {
      90           1 :         const uint8_t in[15]  = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      91             :                                   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
      92           1 :         const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      93             :                                   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
      94             :         uint8_t in_buf[20];
      95             : 
      96             :         /* Test with same input but different alignments. */
      97           1 :         memcpy(in_buf, in, sizeof(in));
      98           1 :         do_test(in_buf, sizeof(in), key);
      99           1 :         memcpy(in_buf + 1, in, sizeof(in));
     100           1 :         do_test(in_buf + 1, sizeof(in), key);
     101           1 :         memcpy(in_buf + 2, in, sizeof(in));
     102           1 :         do_test(in_buf + 2, sizeof(in), key);
     103           1 :         memcpy(in_buf + 4, in, sizeof(in));
     104           1 :         do_test(in_buf + 4, sizeof(in), key);
     105             : 
     106           1 :         test_short_hashes();
     107             : }

Generated by: LCOV version 1.14