Coverage Report

Created: 2021-09-30 20:21

/home/liu/buildslave/linux-x64-runtests/build/lib/3rdparty/siphash/siphash24.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   SipHash reference C implementation
3
4
   Copyright (c) 2012-2014 Jean-Philippe Aumasson
5
   <jeanphilippe.aumasson@gmail.com>
6
   Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
7
8
   To the extent possible under law, the author(s) have dedicated all copyright
9
   and related and neighboring rights to this software to the public domain
10
   worldwide. This software is distributed without any warranty.
11
12
   You should have received a copy of the CC0 Public Domain Dedication along
13
   with
14
   this software. If not, see
15
   <http://creativecommons.org/publicdomain/zero/1.0/>.
16
 */
17
#include <stdint.h>
18
#include <stdio.h>
19
#include <string.h>
20
21
/* default: SipHash-2-4 */
22
68.7M
#define cROUNDS 2
23
31.1M
#define dROUNDS 4
24
25
424M
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
26
27
#define U32TO8_LE(p, v)                                                        \
28
12.4M
  (p)[0] = (uint8_t)((v));                                                     \
29
12.4M
  (p)[1] = (uint8_t)((v) >> 8);                                                \
30
12.4M
  (p)[2] = (uint8_t)((v) >> 16);                                               \
31
12.4M
  (p)[3] = (uint8_t)((v) >> 24);
32
33
#define U64TO8_LE(p, v)                                                        \
34
6.22M
  U32TO8_LE((p), (uint32_t)((v)));                                             \
35
6.22M
  U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
36
37
#define U8TO64_LE(p)                                                           \
38
29.1M
  (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) |                          \
39
29.1M
   ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) |                   \
40
29.1M
   ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) |                   \
41
29.1M
   ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
42
43
#define SIPROUND                                                               \
44
70.7M
  do {                                                                         \
45
70.7M
    v0 += v1;                                                                  \
46
70.7M
    v1 = ROTL(v1, 13);                                                         \
47
70.7M
    v1 ^= v0;                                                                  \
48
70.7M
    v0 = ROTL(v0, 32);                                                         \
49
70.7M
    v2 += v3;                                                                  \
50
70.7M
    v3 = ROTL(v3, 16);                                                         \
51
70.7M
    v3 ^= v2;                                                                  \
52
70.7M
    v0 += v3;                                                                  \
53
70.7M
    v3 = ROTL(v3, 21);                                                         \
54
70.7M
    v3 ^= v0;                                                                  \
55
70.7M
    v2 += v1;                                                                  \
56
70.7M
    v1 = ROTL(v1, 17);                                                         \
57
70.7M
    v1 ^= v2;                                                                  \
58
70.7M
    v2 = ROTL(v2, 32);                                                         \
59
70.7M
  } while (0)
60
61
#ifdef DEBUG
62
#define TRACE                                                                  \
63
  do {                                                                         \
64
    printf("(%3d) v0 %08x %08x\n", (int)inlen, (uint32_t)(v0 >> 32),           \
65
           (uint32_t)v0);                                                      \
66
    printf("(%3d) v1 %08x %08x\n", (int)inlen, (uint32_t)(v1 >> 32),           \
67
           (uint32_t)v1);                                                      \
68
    printf("(%3d) v2 %08x %08x\n", (int)inlen, (uint32_t)(v2 >> 32),           \
69
           (uint32_t)v2);                                                      \
70
    printf("(%3d) v3 %08x %08x\n", (int)inlen, (uint32_t)(v3 >> 32),           \
71
           (uint32_t)v3);                                                      \
72
  } while (0)
73
#else
74
#define TRACE
75
#endif
76
77
6.22M
int siphash(uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k) {
78
6.22M
  /* "somepseudorandomlygeneratedbytes" */
79
6.22M
  uint64_t v0 = 0x736f6d6570736575ULL;
80
6.22M
  uint64_t v1 = 0x646f72616e646f6dULL;
81
6.22M
  uint64_t v2 = 0x6c7967656e657261ULL;
82
6.22M
  uint64_t v3 = 0x7465646279746573ULL;
83
6.22M
  uint64_t b;
84
6.22M
  uint64_t k0 = U8TO64_LE(k);
85
6.22M
  uint64_t k1 = U8TO64_LE(k + 8);
86
6.22M
  uint64_t m;
87
6.22M
  int i;
88
6.22M
  const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
89
6.22M
  const int left = inlen & 7;
90
6.22M
  b = ((uint64_t)inlen) << 56;
91
6.22M
  v3 ^= k1;
92
6.22M
  v2 ^= k0;
93
6.22M
  v1 ^= k1;
94
6.22M
  v0 ^= k0;
95
6.22M
96
#ifdef DOUBLE
97
  v1 ^= 0xee;
98
#endif
99
6.22M
100
22.9M
  for (; in != end; 
in += 816.6M
) {
101
16.6M
    m = U8TO64_LE(in);
102
16.6M
    v3 ^= m;
103
16.6M
104
16.6M
    TRACE;
105
50.0M
    for (i = 0; i < cROUNDS; 
++i33.3M
)
106
33.3M
      SIPROUND;
107
16.6M
108
16.6M
    v0 ^= m;
109
16.6M
  }
110
6.22M
111
6.22M
  switch (left) {
112
0
  case 7:
113
0
    b |= ((uint64_t)in[6]) << 48;
114
0
  case 6:
115
0
    b |= ((uint64_t)in[5]) << 40;
116
1
  case 5:
117
1
    b |= ((uint64_t)in[4]) << 32;
118
6.21M
  case 4:
119
6.21M
    b |= ((uint64_t)in[3]) << 24;
120
6.21M
  case 3:
121
6.21M
    b |= ((uint64_t)in[2]) << 16;
122
6.21M
  case 2:
123
6.21M
    b |= ((uint64_t)in[1]) << 8;
124
6.21M
  case 1:
125
6.21M
    b |= ((uint64_t)in[0]);
126
6.21M
    break;
127
6.21M
  case 0:
128
18.3k
    break;
129
6.22M
  }
130
6.22M
131
6.22M
  v3 ^= b;
132
6.22M
133
6.22M
  TRACE;
134
18.6M
  for (i = 0; i < cROUNDS; 
++i12.4M
)
135
12.4M
    SIPROUND;
136
6.22M
137
6.22M
  v0 ^= b;
138
6.22M
139
6.22M
#ifndef DOUBLE
140
6.22M
  v2 ^= 0xff;
141
#else
142
  v2 ^= 0xee;
143
#endif
144
6.22M
145
6.22M
  TRACE;
146
31.1M
  for (i = 0; i < dROUNDS; 
++i24.9M
)
147
24.9M
    SIPROUND;
148
6.22M
149
6.22M
  b = v0 ^ v1 ^ v2 ^ v3;
150
6.22M
  U64TO8_LE(out, b);
151
6.22M
152
#ifdef DOUBLE
153
  v1 ^= 0xdd;
154
155
  TRACE;
156
  for (i = 0; i < dROUNDS; ++i)
157
    SIPROUND;
158
159
  b = v0 ^ v1 ^ v2 ^ v3;
160
  U64TO8_LE(out + 8, b);
161
#endif
162
6.22M
163
6.22M
  return 0;
164
6.22M
}
165