Coverage Report

Created: 2025-10-28 06:56

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
    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
static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
139
0
{
140
0
    return poly1305_get_params_list;
141
0
}
142
143
static int poly1305_get_params(OSSL_PARAM params[])
144
0
{
145
0
    struct poly1305_get_params_st p;
146
147
0
    if (!poly1305_get_params_decoder(params, &p))
148
0
        return 0;
149
150
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, poly1305_size()))
151
0
        return 0;
152
153
0
    return 1;
154
0
}
155
156
static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
157
                                                      ossl_unused void *provctx)
158
0
{
159
0
    return poly1305_set_ctx_params_list;
160
0
}
161
162
static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
163
0
{
164
0
    struct poly1305_data_st *ctx = vmacctx;
165
0
    struct poly1305_set_ctx_params_st p;
166
167
0
    if (ctx == NULL || !poly1305_set_ctx_params_decoder(params, &p))
168
0
        return 0;
169
170
0
    if (p.key != NULL)
171
0
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
172
0
                || !poly1305_setkey(ctx, p.key->data, p.key->data_size))
173
0
            return 0;
174
0
    return 1;
175
0
}
176
177
const OSSL_DISPATCH ossl_poly1305_functions[] = {
178
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
179
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
180
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
181
    { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
182
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
183
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
184
    { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
185
    { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
186
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
187
      (void (*)(void))poly1305_settable_ctx_params },
188
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
189
    OSSL_DISPATCH_END
190
};