Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/siphash/siphash.c
Line
Count
Source
1
/*
2
 * Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/* Based on https://131002.net/siphash C reference implementation */
11
/*
12
   SipHash reference C implementation
13
14
   Copyright (c) 2012-2016 Jean-Philippe Aumasson
15
   Copyright (c) 2012-2014 Daniel J. Bernstein
16
17
   To the extent possible under law, the author(s) have dedicated all copyright
18
   and related and neighboring rights to this software to the public domain
19
   worldwide. This software is distributed without any warranty.
20
21
   You should have received a copy of the CC0 Public Domain Dedication along
22
   with this software. If not, see
23
   <http://creativecommons.org/publicdomain/zero/1.0/>.
24
 */
25
26
#include <stdlib.h>
27
#include <string.h>
28
#include <openssl/crypto.h>
29
30
#include "crypto/siphash.h"
31
32
311M
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
33
34
#define U32TO8_LE(p, v)            \
35
15.2M
    (p)[0] = (uint8_t)((v));       \
36
15.2M
    (p)[1] = (uint8_t)((v) >> 8);  \
37
15.2M
    (p)[2] = (uint8_t)((v) >> 16); \
38
15.2M
    (p)[3] = (uint8_t)((v) >> 24);
39
40
#define U64TO8_LE(p, v)              \
41
7.64M
    U32TO8_LE((p), (uint32_t)((v))); \
42
7.64M
    U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
43
44
#define U8TO64_LE(p) \
45
18.3M
    (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
46
47
#define SIPROUND           \
48
67.2M
    do {                   \
49
51.9M
        v0 += v1;          \
50
51.9M
        v1 = ROTL(v1, 13); \
51
51.9M
        v1 ^= v0;          \
52
51.9M
        v0 = ROTL(v0, 32); \
53
51.9M
        v2 += v3;          \
54
51.9M
        v3 = ROTL(v3, 16); \
55
51.9M
        v3 ^= v2;          \
56
51.9M
        v0 += v3;          \
57
51.9M
        v3 = ROTL(v3, 21); \
58
51.9M
        v3 ^= v0;          \
59
51.9M
        v2 += v1;          \
60
51.9M
        v1 = ROTL(v1, 17); \
61
51.9M
        v1 ^= v2;          \
62
51.9M
        v2 = ROTL(v2, 32); \
63
51.9M
    } while (0)
64
65
size_t SipHash_ctx_size(void)
66
0
{
67
0
    return sizeof(SIPHASH);
68
0
}
69
70
size_t SipHash_hash_size(SIPHASH *ctx)
71
5.35k
{
72
5.35k
    return ctx->hash_size;
73
5.35k
}
74
75
static size_t siphash_adjust_hash_size(size_t hash_size)
76
22.9M
{
77
22.9M
    if (hash_size == 0)
78
7.64M
        hash_size = SIPHASH_MAX_DIGEST_SIZE;
79
22.9M
    return hash_size;
80
22.9M
}
81
82
int SipHash_set_hash_size(SIPHASH *ctx, size_t hash_size)
83
7.64M
{
84
7.64M
    hash_size = siphash_adjust_hash_size(hash_size);
85
7.64M
    if (hash_size != SIPHASH_MIN_DIGEST_SIZE
86
173
        && hash_size != SIPHASH_MAX_DIGEST_SIZE)
87
49
        return 0;
88
89
    /*
90
     * It's possible that the key was set first.  If the hash size changes,
91
     * we need to adjust v1 (see SipHash_Init().
92
     */
93
94
    /* Start by adjusting the stored size, to make things easier */
95
7.64M
    ctx->hash_size = (unsigned int)siphash_adjust_hash_size(ctx->hash_size);
96
97
    /* Now, adjust ctx->v1 if the old and the new size differ */
98
7.64M
    if ((size_t)ctx->hash_size != hash_size) {
99
7.64M
        ctx->v1 ^= 0xee;
100
7.64M
        ctx->hash_size = (unsigned int)hash_size;
101
7.64M
    }
102
7.64M
    return 1;
103
7.64M
}
104
105
/* hash_size = crounds = drounds = 0 means SipHash24 with 16-byte output */
106
int SipHash_Init(SIPHASH *ctx, const unsigned char *k, int crounds, int drounds)
107
7.64M
{
108
7.64M
    uint64_t k0 = U8TO64_LE(k);
109
7.64M
    uint64_t k1 = U8TO64_LE(k + 8);
110
111
    /* If the hash size wasn't set, i.e. is zero */
112
7.64M
    ctx->hash_size = (unsigned int)siphash_adjust_hash_size(ctx->hash_size);
113
114
7.64M
    if (drounds == 0)
115
7.64M
        drounds = SIPHASH_D_ROUNDS;
116
7.64M
    if (crounds == 0)
117
7.64M
        crounds = SIPHASH_C_ROUNDS;
118
119
7.64M
    ctx->crounds = crounds;
120
7.64M
    ctx->drounds = drounds;
121
122
7.64M
    ctx->len = 0;
123
7.64M
    ctx->total_inlen = 0;
124
125
7.64M
    ctx->v0 = 0x736f6d6570736575ULL ^ k0;
126
7.64M
    ctx->v1 = 0x646f72616e646f6dULL ^ k1;
127
7.64M
    ctx->v2 = 0x6c7967656e657261ULL ^ k0;
128
7.64M
    ctx->v3 = 0x7465646279746573ULL ^ k1;
129
130
7.64M
    if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
131
2.70k
        ctx->v1 ^= 0xee;
132
133
7.64M
    return 1;
134
7.64M
}
135
136
void SipHash_Update(SIPHASH *ctx, const unsigned char *in, size_t inlen)
137
7.64M
{
138
7.64M
    uint64_t m;
139
7.64M
    const uint8_t *end;
140
7.64M
    int left;
141
7.64M
    unsigned int i;
142
7.64M
    uint64_t v0 = ctx->v0;
143
7.64M
    uint64_t v1 = ctx->v1;
144
7.64M
    uint64_t v2 = ctx->v2;
145
7.64M
    uint64_t v3 = ctx->v3;
146
147
7.64M
    ctx->total_inlen += inlen;
148
149
7.64M
    if (ctx->len) {
150
        /* deal with leavings */
151
88
        size_t available = SIPHASH_BLOCK_SIZE - ctx->len;
152
153
        /* not enough to fill leavings */
154
88
        if (inlen < available) {
155
0
            memcpy(&ctx->leavings[ctx->len], in, inlen);
156
0
            ctx->len += (unsigned int)inlen;
157
0
            return;
158
0
        }
159
160
        /* copy data into leavings and reduce input */
161
88
        memcpy(&ctx->leavings[ctx->len], in, available);
162
88
        inlen -= available;
163
88
        in += available;
164
165
        /* process leavings */
166
88
        m = U8TO64_LE(ctx->leavings);
167
88
        v3 ^= m;
168
264
        for (i = 0; i < ctx->crounds; ++i)
169
176
            SIPROUND;
170
88
        v0 ^= m;
171
88
    }
172
7.64M
    left = inlen & (SIPHASH_BLOCK_SIZE - 1); /* gets put into leavings */
173
7.64M
    end = in + inlen - left;
174
175
10.6M
    for (; in != end; in += 8) {
176
3.02M
        m = U8TO64_LE(in);
177
3.02M
        v3 ^= m;
178
9.08M
        for (i = 0; i < ctx->crounds; ++i)
179
6.05M
            SIPROUND;
180
3.02M
        v0 ^= m;
181
3.02M
    }
182
183
    /* save leavings and other ctx */
184
7.64M
    if (left)
185
3.92M
        memcpy(ctx->leavings, end, left);
186
7.64M
    ctx->len = left;
187
188
7.64M
    ctx->v0 = v0;
189
7.64M
    ctx->v1 = v1;
190
7.64M
    ctx->v2 = v2;
191
7.64M
    ctx->v3 = v3;
192
7.64M
}
193
194
int SipHash_Final(SIPHASH *ctx, unsigned char *out, size_t outlen)
195
7.64M
{
196
    /* finalize hash */
197
7.64M
    unsigned int i;
198
7.64M
    uint64_t b = ctx->total_inlen << 56;
199
7.64M
    uint64_t v0 = ctx->v0;
200
7.64M
    uint64_t v1 = ctx->v1;
201
7.64M
    uint64_t v2 = ctx->v2;
202
7.64M
    uint64_t v3 = ctx->v3;
203
204
7.64M
    if (ctx->crounds == 0 || outlen == 0 || outlen != (size_t)ctx->hash_size)
205
0
        return 0;
206
207
7.64M
    switch (ctx->len) {
208
135k
    case 7:
209
135k
        b |= ((uint64_t)ctx->leavings[6]) << 48;
210
        /* fall through */
211
999k
    case 6:
212
999k
        b |= ((uint64_t)ctx->leavings[5]) << 40;
213
        /* fall through */
214
1.16M
    case 5:
215
1.16M
        b |= ((uint64_t)ctx->leavings[4]) << 32;
216
        /* fall through */
217
1.48M
    case 4:
218
1.48M
        b |= ((uint64_t)ctx->leavings[3]) << 24;
219
        /* fall through */
220
2.94M
    case 3:
221
2.94M
        b |= ((uint64_t)ctx->leavings[2]) << 16;
222
        /* fall through */
223
3.11M
    case 2:
224
3.11M
        b |= ((uint64_t)ctx->leavings[1]) << 8;
225
        /* fall through */
226
3.92M
    case 1:
227
3.92M
        b |= ((uint64_t)ctx->leavings[0]);
228
7.64M
    case 0:
229
7.64M
        break;
230
7.64M
    }
231
232
7.64M
    v3 ^= b;
233
22.9M
    for (i = 0; i < ctx->crounds; ++i)
234
15.2M
        SIPROUND;
235
7.64M
    v0 ^= b;
236
7.64M
    if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
237
2.67k
        v2 ^= 0xee;
238
7.64M
    else
239
7.64M
        v2 ^= 0xff;
240
38.2M
    for (i = 0; i < ctx->drounds; ++i)
241
30.5M
        SIPROUND;
242
7.64M
    b = v0 ^ v1 ^ v2 ^ v3;
243
7.64M
    U64TO8_LE(out, b);
244
7.64M
    if (ctx->hash_size == SIPHASH_MIN_DIGEST_SIZE)
245
7.64M
        return 1;
246
2.67k
    v1 ^= 0xdd;
247
13.3k
    for (i = 0; i < ctx->drounds; ++i)
248
10.7k
        SIPROUND;
249
2.67k
    b = v0 ^ v1 ^ v2 ^ v3;
250
2.67k
    U64TO8_LE(out + 8, b);
251
2.67k
    return 1;
252
7.64M
}