Coverage Report

Created: 2024-08-18 16:21

/home/liu/actions-runner/_work/ccv/ccv/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
76.9M
#define cROUNDS 2
23
31.1M
#define dROUNDS 4
24
25
457M
#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.23M
  U32TO8_LE((p), (uint32_t)((v)));                                             \
35
6.23M
  U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
36
37
#define U8TO64_LE(p)                                                           \
38
31.8M
  (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) |                          \
39
31.8M
   ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) |                   \
40
31.8M
   ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) |                   \
41
31.8M
   ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
42
43
#define SIPROUND                                                               \
44
88.6M
  do {                                                                         \
45
76.2M
    v0 += v1;                                                                  \
46
76.2M
    v1 = ROTL(v1, 13);                                                         \
47
76.2M
    v1 ^= v0;                                                                  \
48
76.2M
    v0 = ROTL(v0, 32);                                                         \
49
76.2M
    v2 += v3;                                                                  \
50
76.2M
    v3 = ROTL(v3, 16);                                                         \
51
76.2M
    v3 ^= v2;                                                                  \
52
76.2M
    v0 += v3;                                                                  \
53
76.2M
    v3 = ROTL(v3, 21);                                                         \
54
76.2M
    v3 ^= v0;                                                                  \
55
76.2M
    v2 += v1;                                                                  \
56
76.2M
    v1 = ROTL(v1, 17);                                                         \
57
76.2M
    v1 ^= v2;                                                                  \
58
76.2M
    v2 = ROTL(v2, 32);                                                         \
59
76.2M
  } 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.23M
int siphash(uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k) {
78
  /* "somepseudorandomlygeneratedbytes" */
79
6.23M
  uint64_t v0 = 0x736f6d6570736575ULL;
80
6.23M
  uint64_t v1 = 0x646f72616e646f6dULL;
81
6.23M
  uint64_t v2 = 0x6c7967656e657261ULL;
82
6.23M
  uint64_t v3 = 0x7465646279746573ULL;
83
6.23M
  uint64_t b;
84
6.23M
  uint64_t k0 = U8TO64_LE(k);
85
6.23M
  uint64_t k1 = U8TO64_LE(k + 8);
86
6.23M
  uint64_t m;
87
6.23M
  int i;
88
6.23M
  const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
89
6.23M
  const int left = inlen & 7;
90
6.23M
  b = ((uint64_t)inlen) << 56;
91
6.23M
  v3 ^= k1;
92
6.23M
  v2 ^= k0;
93
6.23M
  v1 ^= k1;
94
6.23M
  v0 ^= k0;
95
96
#ifdef DOUBLE
97
  v1 ^= 0xee;
98
#endif
99
100
25.6M
  for (; in != end; 
in += 819.4M
) {
101
19.4M
    m = U8TO64_LE(in);
102
19.4M
    v3 ^= m;
103
104
19.4M
    TRACE;
105
58.2M
    for (i = 0; i < cROUNDS; 
++i38.8M
)
106
38.8M
      SIPROUND;
107
108
19.4M
    v0 ^= m;
109
19.4M
  }
110
111
6.23M
  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.20M
  case 4:
119
6.20M
    b |= ((uint64_t)in[3]) << 24;
120
6.20M
  case 3:
121
6.20M
    b |= ((uint64_t)in[2]) << 16;
122
6.20M
  case 2:
123
6.20M
    b |= ((uint64_t)in[1]) << 8;
124
6.20M
  case 1:
125
6.20M
    b |= ((uint64_t)in[0]);
126
6.20M
    break;
127
22.0k
  case 0:
128
22.0k
    break;
129
6.23M
  }
130
131
6.23M
  v3 ^= b;
132
133
6.23M
  TRACE;
134
18.6M
  for (i = 0; i < cROUNDS; 
++i12.4M
)
135
12.4M
    SIPROUND;
136
137
6.23M
  v0 ^= b;
138
139
6.23M
#ifndef DOUBLE
140
6.23M
  v2 ^= 0xff;
141
#else
142
  v2 ^= 0xee;
143
#endif
144
145
6.23M
  TRACE;
146
31.1M
  for (i = 0; i < dROUNDS; 
++i24.9M
)
147
24.9M
    SIPROUND;
148
149
6.23M
  b = v0 ^ v1 ^ v2 ^ v3;
150
6.23M
  U64TO8_LE(out, b);
151
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
163
6.23M
  return 0;
164
6.23M
}
165