Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/providers/implementations/macs/blake2_mac_impl.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/proverr.h>
14
15
#include "prov/blake2.h"
16
#include "internal/cryptlib.h"
17
#include "prov/implementations.h"
18
#include "prov/providercommon.h"
19
20
/*
21
 * Forward declaration of everything implemented here.  This is not strictly
22
 * necessary for the compiler, but provides an assurance that the signatures
23
 * of the functions in the dispatch table are correct.
24
 */
25
static OSSL_FUNC_mac_newctx_fn blake2_mac_new;
26
static OSSL_FUNC_mac_dupctx_fn blake2_mac_dup;
27
static OSSL_FUNC_mac_freectx_fn blake2_mac_free;
28
static OSSL_FUNC_mac_gettable_ctx_params_fn blake2_gettable_ctx_params;
29
static OSSL_FUNC_mac_get_ctx_params_fn blake2_get_ctx_params;
30
static OSSL_FUNC_mac_settable_ctx_params_fn blake2_mac_settable_ctx_params;
31
static OSSL_FUNC_mac_set_ctx_params_fn blake2_mac_set_ctx_params;
32
static OSSL_FUNC_mac_init_fn blake2_mac_init;
33
static OSSL_FUNC_mac_update_fn blake2_mac_update;
34
static OSSL_FUNC_mac_final_fn blake2_mac_final;
35
36
struct blake2_mac_data_st {
37
    BLAKE2_CTX ctx;
38
    BLAKE2_PARAM params;
39
    unsigned char key[BLAKE2_KEYBYTES];
40
};
41
42
static void *blake2_mac_new(void *unused_provctx)
43
106
{
44
106
    struct blake2_mac_data_st *macctx;
45
46
106
    if (!ossl_prov_is_running())
47
0
        return NULL;
48
49
106
    macctx = OPENSSL_zalloc(sizeof(*macctx));
50
106
    if (macctx != NULL) {
51
106
        BLAKE2_PARAM_INIT(&macctx->params);
52
        /* ctx initialization is deferred to BLAKE2b_Init() */
53
106
    }
54
106
    return macctx;
55
106
}
blake2b_mac.c:blake2_mac_new
Line
Count
Source
43
70
{
44
70
    struct blake2_mac_data_st *macctx;
45
46
70
    if (!ossl_prov_is_running())
47
0
        return NULL;
48
49
70
    macctx = OPENSSL_zalloc(sizeof(*macctx));
50
70
    if (macctx != NULL) {
51
70
        BLAKE2_PARAM_INIT(&macctx->params);
52
        /* ctx initialization is deferred to BLAKE2b_Init() */
53
70
    }
54
70
    return macctx;
55
70
}
blake2s_mac.c:blake2_mac_new
Line
Count
Source
43
36
{
44
36
    struct blake2_mac_data_st *macctx;
45
46
36
    if (!ossl_prov_is_running())
47
0
        return NULL;
48
49
36
    macctx = OPENSSL_zalloc(sizeof(*macctx));
50
36
    if (macctx != NULL) {
51
36
        BLAKE2_PARAM_INIT(&macctx->params);
52
        /* ctx initialization is deferred to BLAKE2b_Init() */
53
36
    }
54
36
    return macctx;
55
36
}
56
57
static void *blake2_mac_dup(void *vsrc)
58
0
{
59
0
    struct blake2_mac_data_st *dst;
60
0
    struct blake2_mac_data_st *src = vsrc;
61
62
0
    if (!ossl_prov_is_running())
63
0
        return NULL;
64
65
0
    dst = OPENSSL_zalloc(sizeof(*dst));
66
0
    if (dst == NULL)
67
0
        return NULL;
68
69
0
    *dst = *src;
70
0
    return dst;
71
0
}
Unexecuted instantiation: blake2b_mac.c:blake2_mac_dup
Unexecuted instantiation: blake2s_mac.c:blake2_mac_dup
72
73
static void blake2_mac_free(void *vmacctx)
74
106
{
75
106
    struct blake2_mac_data_st *macctx = vmacctx;
76
77
106
    if (macctx != NULL) {
78
106
        OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
79
106
        OPENSSL_free(macctx);
80
106
    }
81
106
}
blake2b_mac.c:blake2_mac_free
Line
Count
Source
74
70
{
75
70
    struct blake2_mac_data_st *macctx = vmacctx;
76
77
70
    if (macctx != NULL) {
78
70
        OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
79
70
        OPENSSL_free(macctx);
80
70
    }
81
70
}
blake2s_mac.c:blake2_mac_free
Line
Count
Source
74
36
{
75
36
    struct blake2_mac_data_st *macctx = vmacctx;
76
77
36
    if (macctx != NULL) {
78
36
        OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
79
36
        OPENSSL_free(macctx);
80
36
    }
81
36
}
82
83
static size_t blake2_mac_size(void *vmacctx)
84
3.63k
{
85
3.63k
    struct blake2_mac_data_st *macctx = vmacctx;
86
87
3.63k
    return macctx->params.digest_length;
88
3.63k
}
blake2b_mac.c:blake2_mac_size
Line
Count
Source
84
1.83k
{
85
1.83k
    struct blake2_mac_data_st *macctx = vmacctx;
86
87
1.83k
    return macctx->params.digest_length;
88
1.83k
}
blake2s_mac.c:blake2_mac_size
Line
Count
Source
84
1.80k
{
85
1.80k
    struct blake2_mac_data_st *macctx = vmacctx;
86
87
1.80k
    return macctx->params.digest_length;
88
1.80k
}
89
90
static int blake2_setkey(struct blake2_mac_data_st *macctx,
91
                         const unsigned char *key, size_t keylen)
92
1.93k
{
93
1.93k
    if (keylen > BLAKE2_KEYBYTES || keylen == 0) {
94
24
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
95
24
        return 0;
96
24
    }
97
1.91k
    memcpy(macctx->key, key, keylen);
98
    /* Pad with zeroes at the end if required */
99
1.91k
    if (keylen < BLAKE2_KEYBYTES)
100
1.69k
        memset(macctx->key + keylen, 0, BLAKE2_KEYBYTES - keylen);
101
1.91k
    BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)keylen);
102
1.91k
    return 1;
103
1.93k
}
blake2b_mac.c:blake2_setkey
Line
Count
Source
92
996
{
93
996
    if (keylen > BLAKE2_KEYBYTES || keylen == 0) {
94
13
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
95
13
        return 0;
96
13
    }
97
983
    memcpy(macctx->key, key, keylen);
98
    /* Pad with zeroes at the end if required */
99
983
    if (keylen < BLAKE2_KEYBYTES)
100
906
        memset(macctx->key + keylen, 0, BLAKE2_KEYBYTES - keylen);
101
983
    BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)keylen);
102
983
    return 1;
103
996
}
blake2s_mac.c:blake2_setkey
Line
Count
Source
92
940
{
93
940
    if (keylen > BLAKE2_KEYBYTES || keylen == 0) {
94
11
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
95
11
        return 0;
96
11
    }
97
929
    memcpy(macctx->key, key, keylen);
98
    /* Pad with zeroes at the end if required */
99
929
    if (keylen < BLAKE2_KEYBYTES)
100
788
        memset(macctx->key + keylen, 0, BLAKE2_KEYBYTES - keylen);
101
929
    BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)keylen);
102
929
    return 1;
103
940
}
104
105
static int blake2_mac_init(void *vmacctx, const unsigned char *key,
106
                           size_t keylen, const OSSL_PARAM params[])
107
1.87k
{
108
1.87k
    struct blake2_mac_data_st *macctx = vmacctx;
109
110
1.87k
    if (!ossl_prov_is_running() || !blake2_mac_set_ctx_params(macctx, params))
111
57
        return 0;
112
1.82k
    if (key != NULL) {
113
1.82k
        if (!blake2_setkey(macctx, key, keylen))
114
1
            return 0;
115
1.82k
    } else if (macctx->params.key_length == 0) {
116
        /* Check key has been set */
117
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
118
0
        return 0;
119
0
    }
120
1.82k
    return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);
121
1.82k
}
blake2b_mac.c:blake2_mac_init
Line
Count
Source
107
957
{
108
957
    struct blake2_mac_data_st *macctx = vmacctx;
109
110
957
    if (!ossl_prov_is_running() || !blake2_mac_set_ctx_params(macctx, params))
111
38
        return 0;
112
919
    if (key != NULL) {
113
919
        if (!blake2_setkey(macctx, key, keylen))
114
0
            return 0;
115
919
    } else if (macctx->params.key_length == 0) {
116
        /* Check key has been set */
117
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
118
0
        return 0;
119
0
    }
120
919
    return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);
121
919
}
blake2s_mac.c:blake2_mac_init
Line
Count
Source
107
921
{
108
921
    struct blake2_mac_data_st *macctx = vmacctx;
109
110
921
    if (!ossl_prov_is_running() || !blake2_mac_set_ctx_params(macctx, params))
111
19
        return 0;
112
902
    if (key != NULL) {
113
902
        if (!blake2_setkey(macctx, key, keylen))
114
1
            return 0;
115
902
    } else if (macctx->params.key_length == 0) {
116
        /* Check key has been set */
117
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
118
0
        return 0;
119
0
    }
120
901
    return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);
121
902
}
122
123
static int blake2_mac_update(void *vmacctx,
124
                             const unsigned char *data, size_t datalen)
125
1.93k
{
126
1.93k
    struct blake2_mac_data_st *macctx = vmacctx;
127
128
1.93k
    if (datalen == 0)
129
0
        return 1;
130
131
1.93k
    return BLAKE2_UPDATE(&macctx->ctx, data, datalen);
132
1.93k
}
blake2b_mac.c:blake2_mac_update
Line
Count
Source
125
973
{
126
973
    struct blake2_mac_data_st *macctx = vmacctx;
127
128
973
    if (datalen == 0)
129
0
        return 1;
130
131
973
    return BLAKE2_UPDATE(&macctx->ctx, data, datalen);
132
973
}
blake2s_mac.c:blake2_mac_update
Line
Count
Source
125
957
{
126
957
    struct blake2_mac_data_st *macctx = vmacctx;
127
128
957
    if (datalen == 0)
129
0
        return 1;
130
131
957
    return BLAKE2_UPDATE(&macctx->ctx, data, datalen);
132
957
}
133
134
static int blake2_mac_final(void *vmacctx,
135
                            unsigned char *out, size_t *outl,
136
                            size_t outsize)
137
1.81k
{
138
1.81k
    struct blake2_mac_data_st *macctx = vmacctx;
139
140
1.81k
    if (!ossl_prov_is_running())
141
0
        return 0;
142
143
1.81k
    *outl = blake2_mac_size(macctx);
144
1.81k
    return BLAKE2_FINAL(out, &macctx->ctx);
145
1.81k
}
blake2b_mac.c:blake2_mac_final
Line
Count
Source
137
916
{
138
916
    struct blake2_mac_data_st *macctx = vmacctx;
139
140
916
    if (!ossl_prov_is_running())
141
0
        return 0;
142
143
916
    *outl = blake2_mac_size(macctx);
144
916
    return BLAKE2_FINAL(out, &macctx->ctx);
145
916
}
blake2s_mac.c:blake2_mac_final
Line
Count
Source
137
901
{
138
901
    struct blake2_mac_data_st *macctx = vmacctx;
139
140
901
    if (!ossl_prov_is_running())
141
0
        return 0;
142
143
901
    *outl = blake2_mac_size(macctx);
144
901
    return BLAKE2_FINAL(out, &macctx->ctx);
145
901
}
146
147
static const OSSL_PARAM known_gettable_ctx_params[] = {
148
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
149
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
150
    OSSL_PARAM_END
151
};
152
static const OSSL_PARAM *blake2_gettable_ctx_params(ossl_unused void *ctx,
153
                                                    ossl_unused void *provctx)
154
0
{
155
0
    return known_gettable_ctx_params;
156
0
}
Unexecuted instantiation: blake2b_mac.c:blake2_gettable_ctx_params
Unexecuted instantiation: blake2s_mac.c:blake2_gettable_ctx_params
157
158
static int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
159
1.82k
{
160
1.82k
    OSSL_PARAM *p;
161
162
1.82k
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
163
1.82k
            && !OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx)))
164
0
        return 0;
165
166
1.82k
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
167
1.82k
            && !OSSL_PARAM_set_size_t(p, BLAKE2_BLOCKBYTES))
168
0
        return 0;
169
170
1.82k
    return 1;
171
1.82k
}
blake2b_mac.c:blake2_get_ctx_params
Line
Count
Source
159
919
{
160
919
    OSSL_PARAM *p;
161
162
919
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
163
919
            && !OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx)))
164
0
        return 0;
165
166
919
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
167
919
            && !OSSL_PARAM_set_size_t(p, BLAKE2_BLOCKBYTES))
168
0
        return 0;
169
170
919
    return 1;
171
919
}
blake2s_mac.c:blake2_get_ctx_params
Line
Count
Source
159
901
{
160
901
    OSSL_PARAM *p;
161
162
901
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
163
901
            && !OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx)))
164
0
        return 0;
165
166
901
    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
167
901
            && !OSSL_PARAM_set_size_t(p, BLAKE2_BLOCKBYTES))
168
0
        return 0;
169
170
901
    return 1;
171
901
}
172
173
static const OSSL_PARAM known_settable_ctx_params[] = {
174
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
175
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
176
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),
177
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0),
178
    OSSL_PARAM_END
179
};
180
static const OSSL_PARAM *blake2_mac_settable_ctx_params(
181
            ossl_unused void *ctx, ossl_unused void *p_ctx)
182
91
{
183
91
    return known_settable_ctx_params;
184
91
}
blake2b_mac.c:blake2_mac_settable_ctx_params
Line
Count
Source
182
60
{
183
60
    return known_settable_ctx_params;
184
60
}
blake2s_mac.c:blake2_mac_settable_ctx_params
Line
Count
Source
182
31
{
183
31
    return known_settable_ctx_params;
184
31
}
185
186
/*
187
 * ALL parameters should be set before init().
188
 */
189
static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
190
0
{
191
0
    struct blake2_mac_data_st *macctx = vmacctx;
192
0
    const OSSL_PARAM *p;
193
194
0
    if (params == NULL)
195
0
        return 1;
196
197
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
198
0
        size_t size;
199
200
0
        if (!OSSL_PARAM_get_size_t(p, &size)
201
0
            || size < 1
202
0
            || size > BLAKE2_OUTBYTES) {
203
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH);
204
0
            return 0;
205
0
        }
206
0
        BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size);
207
0
    }
208
209
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
210
0
            && !blake2_setkey(macctx, p->data, p->data_size))
211
0
        return 0;
212
213
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM))
214
0
        != NULL) {
215
        /*
216
         * The OSSL_PARAM API doesn't provide direct pointer use, so we
217
         * must handle the OSSL_PARAM structure ourselves here
218
         */
219
0
        if (p->data_size > BLAKE2_PERSONALBYTES) {
220
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
221
0
            return 0;
222
0
        }
223
0
        BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size);
224
0
    }
225
226
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) {
227
        /*
228
         * The OSSL_PARAM API doesn't provide direct pointer use, so we
229
         * must handle the OSSL_PARAM structure ourselves here as well
230
         */
231
0
        if (p->data_size > BLAKE2_SALTBYTES) {
232
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
233
0
            return 0;
234
0
        }
235
0
        BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size);
236
0
    }
237
0
    return 1;
238
0
}
Unexecuted instantiation: blake2b_mac.c:blake2_mac_set_ctx_params
Unexecuted instantiation: blake2s_mac.c:blake2_mac_set_ctx_params
239
240
const OSSL_DISPATCH BLAKE2_FUNCTIONS[] = {
241
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))blake2_mac_new },
242
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))blake2_mac_dup },
243
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))blake2_mac_free },
244
    { OSSL_FUNC_MAC_INIT, (void (*)(void))blake2_mac_init },
245
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))blake2_mac_update },
246
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))blake2_mac_final },
247
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
248
      (void (*)(void))blake2_gettable_ctx_params },
249
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))blake2_get_ctx_params },
250
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
251
      (void (*)(void))blake2_mac_settable_ctx_params },
252
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))blake2_mac_set_ctx_params },
253
    OSSL_DISPATCH_END
254
};