Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/macs/siphash_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2018-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
/* clang-format off */
10
11
/* clang-format on */
12
13
#include <string.h>
14
#include <openssl/core_dispatch.h>
15
#include <openssl/core_names.h>
16
#include <openssl/params.h>
17
#include <openssl/evp.h>
18
#include <openssl/err.h>
19
#include <openssl/proverr.h>
20
21
#include "internal/cryptlib.h"
22
#include "crypto/siphash.h"
23
24
#include "prov/implementations.h"
25
#include "prov/providercommon.h"
26
27
/*
28
 * Forward declaration of everything implemented here.  This is not strictly
29
 * necessary for the compiler, but provides an assurance that the signatures
30
 * of the functions in the dispatch table are correct.
31
 */
32
static OSSL_FUNC_mac_newctx_fn siphash_new;
33
static OSSL_FUNC_mac_dupctx_fn siphash_dup;
34
static OSSL_FUNC_mac_freectx_fn siphash_free;
35
static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
36
static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params;
37
static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_ctx_params;
38
static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
39
static OSSL_FUNC_mac_init_fn siphash_init;
40
static OSSL_FUNC_mac_update_fn siphash_update;
41
static OSSL_FUNC_mac_final_fn siphash_final;
42
43
struct siphash_data_st {
44
    void *provctx;
45
    SIPHASH siphash; /* Siphash data */
46
    SIPHASH sipcopy; /* Siphash data copy for reinitialization */
47
    unsigned int crounds, drounds;
48
};
49
50
static unsigned int crounds(struct siphash_data_st *ctx)
51
3.24k
{
52
3.24k
    return ctx->crounds != 0 ? ctx->crounds : SIPHASH_C_ROUNDS;
53
3.24k
}
54
55
static unsigned int drounds(struct siphash_data_st *ctx)
56
3.24k
{
57
3.24k
    return ctx->drounds != 0 ? ctx->drounds : SIPHASH_D_ROUNDS;
58
3.24k
}
59
60
static void *siphash_new(void *provctx)
61
144
{
62
144
    struct siphash_data_st *ctx;
63
64
144
    if (!ossl_prov_is_running())
65
0
        return NULL;
66
144
    ctx = OPENSSL_zalloc(sizeof(*ctx));
67
144
    if (ctx != NULL)
68
144
        ctx->provctx = provctx;
69
144
    return ctx;
70
144
}
71
72
static void siphash_free(void *vmacctx)
73
144
{
74
144
    OPENSSL_free(vmacctx);
75
144
}
76
77
static void *siphash_dup(void *vsrc)
78
0
{
79
0
    struct siphash_data_st *ssrc = vsrc;
80
0
    struct siphash_data_st *sdst;
81
82
0
    if (!ossl_prov_is_running())
83
0
        return NULL;
84
0
    sdst = OPENSSL_malloc(sizeof(*sdst));
85
0
    if (sdst == NULL)
86
0
        return NULL;
87
88
0
    *sdst = *ssrc;
89
0
    return sdst;
90
0
}
91
92
static size_t siphash_size(void *vmacctx)
93
6.41k
{
94
6.41k
    struct siphash_data_st *ctx = vmacctx;
95
96
6.41k
    return SipHash_hash_size(&ctx->siphash);
97
6.41k
}
98
99
static int siphash_setkey(struct siphash_data_st *ctx,
100
    const unsigned char *key, size_t keylen)
101
3.33k
{
102
3.33k
    int ret;
103
104
3.33k
    if (keylen != SIPHASH_KEY_SIZE)
105
87
        return 0;
106
3.24k
    ret = SipHash_Init(&ctx->siphash, key, crounds(ctx), drounds(ctx));
107
3.24k
    if (ret)
108
3.24k
        ctx->sipcopy = ctx->siphash;
109
3.24k
    return ret;
110
3.33k
}
111
112
static int siphash_init(void *vmacctx, const unsigned char *key, size_t keylen,
113
    const OSSL_PARAM params[])
114
3.33k
{
115
3.33k
    struct siphash_data_st *ctx = vmacctx;
116
117
3.33k
    if (!ossl_prov_is_running() || !siphash_set_params(ctx, params))
118
84
        return 0;
119
    /*
120
     * Without a key, there is not much to do here,
121
     * The actual initialization happens through controls.
122
     */
123
3.24k
    if (key == NULL) {
124
0
        ctx->siphash = ctx->sipcopy;
125
0
        return 1;
126
0
    }
127
3.24k
    return siphash_setkey(ctx, key, keylen);
128
3.24k
}
129
130
static int siphash_update(void *vmacctx, const unsigned char *data,
131
    size_t datalen)
132
3.38k
{
133
3.38k
    struct siphash_data_st *ctx = vmacctx;
134
135
3.38k
    if (datalen == 0)
136
0
        return 1;
137
138
3.38k
    SipHash_Update(&ctx->siphash, data, datalen);
139
3.38k
    return 1;
140
3.38k
}
141
142
static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
143
    size_t outsize)
144
3.20k
{
145
3.20k
    struct siphash_data_st *ctx = vmacctx;
146
3.20k
    size_t hlen = siphash_size(ctx);
147
148
3.20k
    if (!ossl_prov_is_running() || outsize < hlen)
149
0
        return 0;
150
151
3.20k
    *outl = hlen;
152
3.20k
    return SipHash_Final(&ctx->siphash, out, hlen);
153
3.20k
}
154
155
/* clang-format off */
156
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
157
#ifndef siphash_get_ctx_params_list
158
static const OSSL_PARAM siphash_get_ctx_params_list[] = {
159
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
160
    OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
161
    OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
162
    OSSL_PARAM_END
163
};
164
#endif
165
166
#ifndef siphash_get_ctx_params_st
167
struct siphash_get_ctx_params_st {
168
    OSSL_PARAM *c;
169
    OSSL_PARAM *d;
170
    OSSL_PARAM *size;
171
};
172
#endif
173
174
#ifndef siphash_get_ctx_params_decoder
175
static int siphash_get_ctx_params_decoder
176
    (const OSSL_PARAM *p, struct siphash_get_ctx_params_st *r)
177
1.33k
{
178
1.33k
    const char *s;
179
180
1.33k
    memset(r, 0, sizeof(*r));
181
1.33k
    if (p != NULL)
182
2.66k
        for (; (s = p->key) != NULL; p++)
183
1.33k
            switch(s[0]) {
184
0
            default:
185
0
                break;
186
0
            case 'c':
187
0
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
188
                    /* OSSL_MAC_PARAM_C_ROUNDS */
189
0
                    if (ossl_unlikely(r->c != NULL)) {
190
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
191
0
                                       "param %s is repeated", s);
192
0
                        return 0;
193
0
                    }
194
0
                    r->c = (OSSL_PARAM *)p;
195
0
                }
196
0
                break;
197
0
            case 'd':
198
0
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
199
                    /* OSSL_MAC_PARAM_D_ROUNDS */
200
0
                    if (ossl_unlikely(r->d != NULL)) {
201
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
202
0
                                       "param %s is repeated", s);
203
0
                        return 0;
204
0
                    }
205
0
                    r->d = (OSSL_PARAM *)p;
206
0
                }
207
0
                break;
208
1.33k
            case 's':
209
1.33k
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
210
                    /* OSSL_MAC_PARAM_SIZE */
211
1.33k
                    if (ossl_unlikely(r->size != NULL)) {
212
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
213
0
                                       "param %s is repeated", s);
214
0
                        return 0;
215
0
                    }
216
1.33k
                    r->size = (OSSL_PARAM *)p;
217
1.33k
                }
218
1.33k
            }
219
1.33k
    return 1;
220
1.33k
}
221
#endif
222
/* End of machine generated */
223
/* clang-format on */
224
225
static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx,
226
    ossl_unused void *provctx)
227
0
{
228
0
    return siphash_get_ctx_params_list;
229
0
}
230
231
static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
232
1.33k
{
233
1.33k
    struct siphash_data_st *ctx = vmacctx;
234
1.33k
    struct siphash_get_ctx_params_st p;
235
236
1.33k
    if (ctx == NULL || !siphash_get_ctx_params_decoder(params, &p))
237
0
        return 0;
238
239
1.33k
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, siphash_size(vmacctx)))
240
0
        return 0;
241
1.33k
    if (p.c != NULL && !OSSL_PARAM_set_uint(p.c, crounds(ctx)))
242
0
        return 0;
243
1.33k
    if (p.d != NULL && !OSSL_PARAM_set_uint(p.d, drounds(ctx)))
244
0
        return 0;
245
1.33k
    return 1;
246
1.33k
}
247
248
/* clang-format off */
249
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
250
#ifndef siphash_set_params_list
251
static const OSSL_PARAM siphash_set_params_list[] = {
252
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
253
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
254
    OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
255
    OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
256
    OSSL_PARAM_END
257
};
258
#endif
259
260
#ifndef siphash_set_params_st
261
struct siphash_set_params_st {
262
    OSSL_PARAM *c;
263
    OSSL_PARAM *d;
264
    OSSL_PARAM *key;
265
    OSSL_PARAM *size;
266
};
267
#endif
268
269
#ifndef siphash_set_params_decoder
270
static int siphash_set_params_decoder
271
    (const OSSL_PARAM *p, struct siphash_set_params_st *r)
272
1.39k
{
273
1.39k
    const char *s;
274
275
1.39k
    memset(r, 0, sizeof(*r));
276
1.39k
    if (p != NULL)
277
292
        for (; (s = p->key) != NULL; p++)
278
230
            switch(s[0]) {
279
10
            default:
280
10
                break;
281
54
            case 'c':
282
54
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
283
                    /* OSSL_MAC_PARAM_C_ROUNDS */
284
52
                    if (ossl_unlikely(r->c != NULL)) {
285
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
286
0
                                       "param %s is repeated", s);
287
0
                        return 0;
288
0
                    }
289
52
                    r->c = (OSSL_PARAM *)p;
290
52
                }
291
54
                break;
292
62
            case 'd':
293
62
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
294
                    /* OSSL_MAC_PARAM_D_ROUNDS */
295
52
                    if (ossl_unlikely(r->d != NULL)) {
296
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
297
0
                                       "param %s is repeated", s);
298
0
                        return 0;
299
0
                    }
300
52
                    r->d = (OSSL_PARAM *)p;
301
52
                }
302
62
                break;
303
62
            case 'k':
304
52
                if (ossl_likely(strcmp("ey", s + 1) == 0)) {
305
                    /* OSSL_MAC_PARAM_KEY */
306
52
                    if (ossl_unlikely(r->key != NULL)) {
307
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
308
0
                                       "param %s is repeated", s);
309
0
                        return 0;
310
0
                    }
311
52
                    r->key = (OSSL_PARAM *)p;
312
52
                }
313
52
                break;
314
52
            case 's':
315
52
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
316
                    /* OSSL_MAC_PARAM_SIZE */
317
52
                    if (ossl_unlikely(r->size != NULL)) {
318
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
319
0
                                       "param %s is repeated", s);
320
0
                        return 0;
321
0
                    }
322
52
                    r->size = (OSSL_PARAM *)p;
323
52
                }
324
230
            }
325
1.39k
    return 1;
326
1.39k
}
327
#endif
328
/* End of machine generated */
329
/* clang-format on */
330
331
static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx,
332
    void *provctx)
333
128
{
334
128
    return siphash_set_params_list;
335
128
}
336
337
static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
338
1.39k
{
339
1.39k
    struct siphash_data_st *ctx = vmacctx;
340
1.39k
    struct siphash_set_params_st p;
341
1.39k
    size_t size;
342
343
1.39k
    if (ctx == NULL || !siphash_set_params_decoder(params, &p))
344
0
        return 0;
345
346
1.39k
    if (p.size != NULL) {
347
52
        if (!OSSL_PARAM_get_size_t(p.size, &size)
348
52
            || !SipHash_set_hash_size(&ctx->siphash, size)
349
35
            || !SipHash_set_hash_size(&ctx->sipcopy, size))
350
17
            return 0;
351
52
    }
352
1.38k
    if (p.c != NULL && !OSSL_PARAM_get_uint(p.c, &ctx->crounds))
353
0
        return 0;
354
1.38k
    if (p.d != NULL && !OSSL_PARAM_get_uint(p.d, &ctx->drounds))
355
0
        return 0;
356
1.38k
    if (p.key != NULL)
357
35
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
358
35
            || !siphash_setkey(ctx, p.key->data, p.key->data_size))
359
19
            return 0;
360
1.36k
    return 1;
361
1.38k
}
362
363
const OSSL_DISPATCH ossl_siphash_functions[] = {
364
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
365
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
366
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
367
    { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
368
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
369
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
370
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
371
        (void (*)(void))siphash_gettable_ctx_params },
372
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
373
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
374
        (void (*)(void))siphash_settable_ctx_params },
375
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
376
    OSSL_DISPATCH_END
377
};