Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/macs/poly1305_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2018-2026 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
15
#include <openssl/core_dispatch.h>
16
#include <openssl/core_names.h>
17
#include <openssl/params.h>
18
#include <openssl/evp.h>
19
#include <openssl/err.h>
20
#include <openssl/proverr.h>
21
22
#include "internal/cryptlib.h"
23
#include "crypto/poly1305.h"
24
25
#include "prov/implementations.h"
26
#include "prov/providercommon.h"
27
28
/*
29
 * Forward declaration of everything implemented here.  This is not strictly
30
 * necessary for the compiler, but provides an assurance that the signatures
31
 * of the functions in the dispatch table are correct.
32
 */
33
static OSSL_FUNC_mac_newctx_fn poly1305_new;
34
static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
35
static OSSL_FUNC_mac_freectx_fn poly1305_free;
36
static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
37
static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
38
static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
39
static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
40
static OSSL_FUNC_mac_init_fn poly1305_init;
41
static OSSL_FUNC_mac_update_fn poly1305_update;
42
static OSSL_FUNC_mac_final_fn poly1305_final;
43
44
struct poly1305_data_st {
45
    void *provctx;
46
    int updated;
47
    int key_set;
48
    POLY1305 poly1305; /* Poly1305 data */
49
};
50
51
static void *poly1305_new(void *provctx)
52
54
{
53
54
    struct poly1305_data_st *ctx;
54
55
54
    if (!ossl_prov_is_running())
56
0
        return NULL;
57
54
    ctx = OPENSSL_zalloc(sizeof(*ctx));
58
54
    if (ctx != NULL)
59
54
        ctx->provctx = provctx;
60
54
    return ctx;
61
54
}
62
63
static void poly1305_free(void *vmacctx)
64
54
{
65
54
    OPENSSL_free(vmacctx);
66
54
}
67
68
static void *poly1305_dup(void *vsrc)
69
0
{
70
0
    struct poly1305_data_st *src = vsrc;
71
0
    struct poly1305_data_st *dst;
72
73
0
    if (!ossl_prov_is_running())
74
0
        return NULL;
75
0
    dst = OPENSSL_malloc(sizeof(*dst));
76
0
    if (dst == NULL)
77
0
        return NULL;
78
79
0
    *dst = *src;
80
0
    return dst;
81
0
}
82
83
static size_t poly1305_size(void)
84
560
{
85
560
    return POLY1305_DIGEST_SIZE;
86
560
}
87
88
static int poly1305_setkey(struct poly1305_data_st *ctx,
89
    const unsigned char *key, size_t keylen)
90
336
{
91
336
    if (key == NULL || keylen != POLY1305_KEY_SIZE) {
92
52
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
93
52
        return 0;
94
52
    }
95
284
    Poly1305_Init(&ctx->poly1305, key);
96
284
    ctx->updated = 0;
97
284
    ctx->key_set = 1;
98
284
    return 1;
99
336
}
100
101
static int poly1305_init(void *vmacctx, const unsigned char *key,
102
    size_t keylen, const OSSL_PARAM params[])
103
332
{
104
332
    struct poly1305_data_st *ctx = vmacctx;
105
106
    /* initialize the context in MAC_ctrl function */
107
332
    if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
108
47
        return 0;
109
285
    if (key != NULL)
110
285
        return poly1305_setkey(ctx, key, keylen);
111
    /* no reinitialization of context with the same key is allowed */
112
0
    return ctx->updated == 0;
113
285
}
114
115
static int poly1305_update(void *vmacctx, const unsigned char *data,
116
    size_t datalen)
117
310
{
118
310
    struct poly1305_data_st *ctx = vmacctx;
119
120
310
    if (!ctx->key_set) {
121
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
122
0
        return 0;
123
0
    }
124
310
    ctx->updated = 1;
125
310
    if (datalen == 0)
126
0
        return 1;
127
128
    /* poly1305 has nothing to return in its update function */
129
310
    Poly1305_Update(&ctx->poly1305, data, datalen);
130
310
    return 1;
131
310
}
132
133
static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
134
    size_t outsize)
135
280
{
136
280
    struct poly1305_data_st *ctx = vmacctx;
137
138
280
    if (!ossl_prov_is_running())
139
0
        return 0;
140
280
    if (!ctx->key_set) {
141
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
142
0
        return 0;
143
0
    }
144
280
    ctx->updated = 1;
145
280
    Poly1305_Final(&ctx->poly1305, out);
146
280
    *outl = poly1305_size();
147
280
    return 1;
148
280
}
149
150
/* clang-format off */
151
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
152
#ifndef poly1305_get_params_list
153
static const OSSL_PARAM poly1305_get_params_list[] = {
154
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
155
    OSSL_PARAM_END
156
};
157
#endif
158
159
#ifndef poly1305_get_params_st
160
struct poly1305_get_params_st {
161
    OSSL_PARAM *size;
162
};
163
#endif
164
165
#ifndef poly1305_get_params_decoder
166
static int poly1305_get_params_decoder
167
    (const OSSL_PARAM *p, struct poly1305_get_params_st *r)
168
0
{
169
0
    const char *s;
170
171
0
    memset(r, 0, sizeof(*r));
172
0
    if (p != NULL)
173
0
        for (; (s = p->key) != NULL; p++)
174
0
            if (ossl_likely(strcmp("size", s + 0) == 0)) {
175
                /* OSSL_MAC_PARAM_SIZE */
176
0
                if (ossl_unlikely(r->size != NULL)) {
177
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
178
0
                                   "param %s is repeated", s);
179
0
                    return 0;
180
0
                }
181
0
                r->size = (OSSL_PARAM *)p;
182
0
            }
183
0
    return 1;
184
0
}
185
#endif
186
/* End of machine generated */
187
/* clang-format on */
188
189
static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
190
0
{
191
0
    return poly1305_get_params_list;
192
0
}
193
194
static int poly1305_get_params(OSSL_PARAM params[])
195
0
{
196
0
    struct poly1305_get_params_st p;
197
198
0
    if (!poly1305_get_params_decoder(params, &p))
199
0
        return 0;
200
201
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, poly1305_size()))
202
0
        return 0;
203
204
0
    return 1;
205
0
}
206
207
/* clang-format off */
208
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
209
#ifndef poly1305_set_ctx_params_list
210
static const OSSL_PARAM poly1305_set_ctx_params_list[] = {
211
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
212
    OSSL_PARAM_END
213
};
214
#endif
215
216
#ifndef poly1305_set_ctx_params_st
217
struct poly1305_set_ctx_params_st {
218
    OSSL_PARAM *key;
219
};
220
#endif
221
222
#ifndef poly1305_set_ctx_params_decoder
223
static int poly1305_set_ctx_params_decoder
224
    (const OSSL_PARAM *p, struct poly1305_set_ctx_params_st *r)
225
26
{
226
26
    const char *s;
227
228
26
    memset(r, 0, sizeof(*r));
229
26
    if (p != NULL)
230
52
        for (; (s = p->key) != NULL; p++)
231
26
            if (ossl_likely(strcmp("key", s + 0) == 0)) {
232
                /* OSSL_MAC_PARAM_KEY */
233
26
                if (ossl_unlikely(r->key != NULL)) {
234
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
235
0
                                   "param %s is repeated", s);
236
0
                    return 0;
237
0
                }
238
26
                r->key = (OSSL_PARAM *)p;
239
26
            }
240
26
    return 1;
241
26
}
242
#endif
243
/* End of machine generated */
244
/* clang-format on */
245
246
static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
247
    ossl_unused void *provctx)
248
63
{
249
63
    return poly1305_set_ctx_params_list;
250
63
}
251
252
static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
253
26
{
254
26
    struct poly1305_data_st *ctx = vmacctx;
255
26
    struct poly1305_set_ctx_params_st p;
256
257
26
    if (ctx == NULL || !poly1305_set_ctx_params_decoder(params, &p))
258
0
        return 0;
259
260
26
    if (p.key != NULL)
261
26
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
262
26
            || !poly1305_setkey(ctx, p.key->data, p.key->data_size))
263
24
            return 0;
264
2
    return 1;
265
26
}
266
267
const OSSL_DISPATCH ossl_poly1305_functions[] = {
268
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
269
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
270
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
271
    { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
272
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
273
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
274
    { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
275
    { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
276
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
277
        (void (*)(void))poly1305_settable_ctx_params },
278
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
279
    OSSL_DISPATCH_END
280
};