Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/macs/siphash_prov.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2018-2023 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
0
{
50
0
    return ctx->crounds != 0 ? ctx->crounds : SIPHASH_C_ROUNDS;
51
0
}
52
53
static unsigned int drounds(struct siphash_data_st *ctx)
54
0
{
55
0
    return ctx->drounds != 0 ? ctx->drounds : SIPHASH_D_ROUNDS;
56
0
}
57
58
static void *siphash_new(void *provctx)
59
0
{
60
0
    struct siphash_data_st *ctx;
61
62
0
    if (!ossl_prov_is_running())
63
0
        return NULL;
64
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
65
0
    if (ctx != NULL)
66
0
        ctx->provctx = provctx;
67
0
    return ctx;
68
0
}
69
70
static void siphash_free(void *vmacctx)
71
0
{
72
0
    OPENSSL_free(vmacctx);
73
0
}
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
0
{
92
0
    struct siphash_data_st *ctx = vmacctx;
93
94
0
    return SipHash_hash_size(&ctx->siphash);
95
0
}
96
97
static int siphash_setkey(struct siphash_data_st *ctx,
98
                          const unsigned char *key, size_t keylen)
99
0
{
100
0
    int ret;
101
102
0
    if (keylen != SIPHASH_KEY_SIZE)
103
0
        return 0;
104
0
    ret = SipHash_Init(&ctx->siphash, key, crounds(ctx), drounds(ctx));
105
0
    if (ret)
106
0
        ctx->sipcopy = ctx->siphash;
107
0
    return ret;
108
0
}
109
110
static int siphash_init(void *vmacctx, const unsigned char *key, size_t keylen,
111
                        const OSSL_PARAM params[])
112
0
{
113
0
    struct siphash_data_st *ctx = vmacctx;
114
115
0
    if (!ossl_prov_is_running() || !siphash_set_params(ctx, params))
116
0
        return 0;
117
    /*
118
     * Without a key, there is not much to do here,
119
     * The actual initialization happens through controls.
120
     */
121
0
    if (key == NULL) {
122
0
        ctx->siphash = ctx->sipcopy;
123
0
        return 1;
124
0
    }
125
0
    return siphash_setkey(ctx, key, keylen);
126
0
}
127
128
static int siphash_update(void *vmacctx, const unsigned char *data,
129
                          size_t datalen)
130
0
{
131
0
    struct siphash_data_st *ctx = vmacctx;
132
133
0
    if (datalen == 0)
134
0
        return 1;
135
136
0
    SipHash_Update(&ctx->siphash, data, datalen);
137
0
    return 1;
138
0
}
139
140
static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
141
                         size_t outsize)
142
0
{
143
0
    struct siphash_data_st *ctx = vmacctx;
144
0
    size_t hlen = siphash_size(ctx);
145
146
0
    if (!ossl_prov_is_running() || outsize < hlen)
147
0
        return 0;
148
149
0
    *outl = hlen;
150
0
    return SipHash_Final(&ctx->siphash, out, hlen);
151
0
}
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
0
{
175
0
    const char *s;
176
177
0
    memset(r, 0, sizeof(*r));
178
0
    if (p != NULL)
179
0
        for (; (s = p->key) != NULL; p++)
180
0
            switch(s[0]) {
181
0
            default:
182
0
                break;
183
0
            case 'c':
184
0
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
185
                    /* 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
                    /* 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
0
            case 's':
206
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
207
                    /* MAC_PARAM_SIZE */
208
0
                    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
0
                    r->size = (OSSL_PARAM *)p;
214
0
                }
215
0
            }
216
0
    return 1;
217
0
}
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
0
{
229
0
    struct siphash_data_st *ctx = vmacctx;
230
0
    struct siphash_get_ctx_params_st p;
231
232
0
    if (ctx == NULL || !siphash_get_ctx_params_decoder(params, &p))
233
0
        return 0;
234
235
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, siphash_size(vmacctx)))
236
0
        return 0;
237
0
    if (p.c != NULL && !OSSL_PARAM_set_uint(p.c, crounds(ctx)))
238
0
        return 0;
239
0
    if (p.d != NULL && !OSSL_PARAM_set_uint(p.d, drounds(ctx)))
240
0
        return 0;
241
0
    return 1;
242
0
}
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
0
{
268
0
    const char *s;
269
270
0
    memset(r, 0, sizeof(*r));
271
0
    if (p != NULL)
272
0
        for (; (s = p->key) != NULL; p++)
273
0
            switch(s[0]) {
274
0
            default:
275
0
                break;
276
0
            case 'c':
277
0
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
278
                    /* MAC_PARAM_C_ROUNDS */
279
0
                    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
0
                    r->c = (OSSL_PARAM *)p;
285
0
                }
286
0
                break;
287
0
            case 'd':
288
0
                if (ossl_likely(strcmp("-rounds", s + 1) == 0)) {
289
                    /* MAC_PARAM_D_ROUNDS */
290
0
                    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
0
                    r->d = (OSSL_PARAM *)p;
296
0
                }
297
0
                break;
298
0
            case 'k':
299
0
                if (ossl_likely(strcmp("ey", s + 1) == 0)) {
300
                    /* MAC_PARAM_KEY */
301
0
                    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
0
                    r->key = (OSSL_PARAM *)p;
307
0
                }
308
0
                break;
309
0
            case 's':
310
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
311
                    /* MAC_PARAM_SIZE */
312
0
                    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
0
                    r->size = (OSSL_PARAM *)p;
318
0
                }
319
0
            }
320
0
    return 1;
321
0
}
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
0
{
328
0
    return siphash_set_params_list;
329
0
}
330
331
static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
332
0
{
333
0
    struct siphash_data_st *ctx = vmacctx;
334
0
    struct siphash_set_params_st p;
335
0
    size_t size;
336
337
0
    if (ctx == NULL || !siphash_set_params_decoder(params, &p))
338
0
        return 0;
339
340
0
    if (p.size != NULL) {
341
0
        if (!OSSL_PARAM_get_size_t(p.size, &size)
342
0
            || !SipHash_set_hash_size(&ctx->siphash, size)
343
0
            || !SipHash_set_hash_size(&ctx->sipcopy, size))
344
0
            return 0;
345
0
    }
346
0
    if (p.c != NULL && !OSSL_PARAM_get_uint(p.c, &ctx->crounds))
347
0
        return 0;
348
0
    if (p.d != NULL && !OSSL_PARAM_get_uint(p.d, &ctx->drounds))
349
0
        return 0;
350
0
    if (p.key != NULL)
351
0
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
352
0
            || !siphash_setkey(ctx, p.key->data, p.key->data_size))
353
0
            return 0;
354
0
    return 1;
355
0
}
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
};