Coverage Report

Created: 2025-08-25 06:30

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