Coverage Report

Created: 2025-06-13 06:56

/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
#include <openssl/core_dispatch.h>
11
#include <openssl/core_names.h>
12
#include <openssl/params.h>
13
#include <openssl/evp.h>
14
#include <openssl/err.h>
15
#include <openssl/proverr.h>
16
17
#include "crypto/poly1305.h"
18
19
#include "prov/implementations.h"
20
#include "prov/providercommon.h"
21
22
/*
23
 * Forward declaration of everything implemented here.  This is not strictly
24
 * necessary for the compiler, but provides an assurance that the signatures
25
 * of the functions in the dispatch table are correct.
26
 */
27
static OSSL_FUNC_mac_newctx_fn poly1305_new;
28
static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
29
static OSSL_FUNC_mac_freectx_fn poly1305_free;
30
static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
31
static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
32
static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
33
static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
34
static OSSL_FUNC_mac_init_fn poly1305_init;
35
static OSSL_FUNC_mac_update_fn poly1305_update;
36
static OSSL_FUNC_mac_final_fn poly1305_final;
37
38
struct poly1305_data_st {
39
    void *provctx;
40
    int updated;
41
    POLY1305 poly1305;           /* Poly1305 data */
42
};
43
44
static void *poly1305_new(void *provctx)
45
0
{
46
0
    struct poly1305_data_st *ctx;
47
48
0
    if (!ossl_prov_is_running())
49
0
        return NULL;
50
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
51
0
    if (ctx != NULL)
52
0
        ctx->provctx = provctx;
53
0
    return ctx;
54
0
}
55
56
static void poly1305_free(void *vmacctx)
57
0
{
58
0
    OPENSSL_free(vmacctx);
59
0
}
60
61
static void *poly1305_dup(void *vsrc)
62
0
{
63
0
    struct poly1305_data_st *src = vsrc;
64
0
    struct poly1305_data_st *dst;
65
66
0
    if (!ossl_prov_is_running())
67
0
        return NULL;
68
0
    dst = OPENSSL_malloc(sizeof(*dst));
69
0
    if (dst == NULL)
70
0
        return NULL;
71
72
0
    *dst = *src;
73
0
    return dst;
74
0
}
75
76
static size_t poly1305_size(void)
77
0
{
78
0
    return POLY1305_DIGEST_SIZE;
79
0
}
80
81
static int poly1305_setkey(struct poly1305_data_st *ctx,
82
                           const unsigned char *key, size_t keylen)
83
0
{
84
0
    if (keylen != POLY1305_KEY_SIZE) {
85
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
86
0
        return 0;
87
0
    }
88
0
    Poly1305_Init(&ctx->poly1305, key);
89
0
    ctx->updated = 0;
90
0
    return 1;
91
0
}
92
93
static int poly1305_init(void *vmacctx, const unsigned char *key,
94
                         size_t keylen, const OSSL_PARAM params[])
95
0
{
96
0
    struct poly1305_data_st *ctx = vmacctx;
97
98
    /* initialize the context in MAC_ctrl function */
99
0
    if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
100
0
        return 0;
101
0
    if (key != NULL)
102
0
        return poly1305_setkey(ctx, key, keylen);
103
    /* no reinitialization of context with the same key is allowed */
104
0
    return ctx->updated == 0;
105
0
}
106
107
static int poly1305_update(void *vmacctx, const unsigned char *data,
108
                       size_t datalen)
109
0
{
110
0
    struct poly1305_data_st *ctx = vmacctx;
111
112
0
    ctx->updated = 1;
113
0
    if (datalen == 0)
114
0
        return 1;
115
116
    /* poly1305 has nothing to return in its update function */
117
0
    Poly1305_Update(&ctx->poly1305, data, datalen);
118
0
    return 1;
119
0
}
120
121
static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
122
                          size_t outsize)
123
0
{
124
0
    struct poly1305_data_st *ctx = vmacctx;
125
126
0
    if (!ossl_prov_is_running())
127
0
        return 0;
128
0
    ctx->updated = 1;
129
0
    Poly1305_Final(&ctx->poly1305, out);
130
0
    *outl = poly1305_size();
131
0
    return 1;
132
0
}
133
134
static const OSSL_PARAM known_gettable_params[] = {
135
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
136
    OSSL_PARAM_END
137
};
138
static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
139
0
{
140
0
    return known_gettable_params;
141
0
}
142
143
static int poly1305_get_params(OSSL_PARAM params[])
144
0
{
145
0
    OSSL_PARAM *p;
146
147
0
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
148
0
        return OSSL_PARAM_set_size_t(p, poly1305_size());
149
150
0
    return 1;
151
0
}
152
153
static const OSSL_PARAM known_settable_ctx_params[] = {
154
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
155
    OSSL_PARAM_END
156
};
157
static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
158
                                                      ossl_unused void *provctx)
159
0
{
160
0
    return known_settable_ctx_params;
161
0
}
162
163
static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
164
0
{
165
0
    struct poly1305_data_st *ctx = vmacctx;
166
0
    const OSSL_PARAM *p;
167
168
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
169
0
            && !poly1305_setkey(ctx, p->data, p->data_size))
170
0
        return 0;
171
0
    return 1;
172
0
}
173
174
const OSSL_DISPATCH ossl_poly1305_functions[] = {
175
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
176
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
177
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
178
    { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
179
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
180
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
181
    { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
182
    { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
183
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
184
      (void (*)(void))poly1305_settable_ctx_params },
185
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
186
    OSSL_DISPATCH_END
187
};