Coverage Report

Created: 2025-12-04 06:33

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