Coverage Report

Created: 2026-06-08 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/macs/poly1305_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
#include <string.h>
11
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/poly1305.h"
21
22
#include "prov/implementations.h"
23
#include "prov/providercommon.h"
24
#include "providers/implementations/macs/poly1305_prov.inc"
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
    int key_set;
46
    POLY1305 poly1305; /* Poly1305 data */
47
};
48
49
static void *poly1305_new(void *provctx)
50
0
{
51
0
    struct poly1305_data_st *ctx;
52
53
0
    if (!ossl_prov_is_running())
54
0
        return NULL;
55
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
56
0
    if (ctx != NULL)
57
0
        ctx->provctx = provctx;
58
0
    return ctx;
59
0
}
60
61
static void poly1305_free(void *vmacctx)
62
0
{
63
0
    OPENSSL_free(vmacctx);
64
0
}
65
66
static void *poly1305_dup(void *vsrc)
67
0
{
68
0
    struct poly1305_data_st *src = vsrc;
69
0
    struct poly1305_data_st *dst;
70
71
0
    if (!ossl_prov_is_running())
72
0
        return NULL;
73
0
    dst = OPENSSL_malloc(sizeof(*dst));
74
0
    if (dst == NULL)
75
0
        return NULL;
76
77
0
    *dst = *src;
78
0
    return dst;
79
0
}
80
81
static size_t poly1305_size(void)
82
0
{
83
0
    return POLY1305_DIGEST_SIZE;
84
0
}
85
86
static int poly1305_setkey(struct poly1305_data_st *ctx,
87
    const unsigned char *key, size_t keylen)
88
0
{
89
0
    if (keylen != POLY1305_KEY_SIZE) {
90
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
91
0
        return 0;
92
0
    }
93
0
    Poly1305_Init(&ctx->poly1305, key);
94
0
    ctx->updated = 0;
95
0
    ctx->key_set = 1;
96
0
    return 1;
97
0
}
98
99
static int poly1305_init(void *vmacctx, const unsigned char *key,
100
    size_t keylen, const OSSL_PARAM params[])
101
0
{
102
0
    struct poly1305_data_st *ctx = vmacctx;
103
104
    /* initialize the context in MAC_ctrl function */
105
0
    if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
106
0
        return 0;
107
0
    if (key != NULL)
108
0
        return poly1305_setkey(ctx, key, keylen);
109
    /* no reinitialization of context with the same key is allowed */
110
0
    return ctx->updated == 0;
111
0
}
112
113
static int poly1305_update(void *vmacctx, const unsigned char *data,
114
    size_t datalen)
115
0
{
116
0
    struct poly1305_data_st *ctx = vmacctx;
117
118
0
    ctx->updated = 1;
119
0
    if (datalen == 0)
120
0
        return 1;
121
122
    /* poly1305 has nothing to return in its update function */
123
0
    Poly1305_Update(&ctx->poly1305, data, datalen);
124
0
    return 1;
125
0
}
126
127
static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
128
    size_t outsize)
129
0
{
130
0
    struct poly1305_data_st *ctx = vmacctx;
131
132
0
    if (!ossl_prov_is_running())
133
0
        return 0;
134
0
    if (!ctx->key_set) {
135
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
136
0
        return 0;
137
0
    }
138
0
    ctx->updated = 1;
139
0
    Poly1305_Final(&ctx->poly1305, out);
140
0
    *outl = poly1305_size();
141
0
    return 1;
142
0
}
143
144
static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
145
0
{
146
0
    return poly1305_get_params_list;
147
0
}
148
149
static int poly1305_get_params(OSSL_PARAM params[])
150
0
{
151
0
    struct poly1305_get_params_st p;
152
153
0
    if (!poly1305_get_params_decoder(params, &p))
154
0
        return 0;
155
156
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, poly1305_size()))
157
0
        return 0;
158
159
0
    return 1;
160
0
}
161
162
static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
163
    ossl_unused void *provctx)
164
0
{
165
0
    return poly1305_set_ctx_params_list;
166
0
}
167
168
static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
169
0
{
170
0
    struct poly1305_data_st *ctx = vmacctx;
171
0
    struct poly1305_set_ctx_params_st p;
172
173
0
    if (ctx == NULL || !poly1305_set_ctx_params_decoder(params, &p))
174
0
        return 0;
175
176
0
    if (p.key != NULL)
177
0
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
178
0
            || !poly1305_setkey(ctx, p.key->data, p.key->data_size))
179
0
            return 0;
180
0
    return 1;
181
0
}
182
183
const OSSL_DISPATCH ossl_poly1305_functions[] = {
184
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
185
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
186
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
187
    { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
188
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
189
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
190
    { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
191
    { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
192
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
193
        (void (*)(void))poly1305_settable_ctx_params },
194
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
195
    OSSL_DISPATCH_END
196
};