Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/ffc/ffc_params.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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 <string.h> /* memset */
11
#include <openssl/core_names.h>
12
#include "internal/ffc.h"
13
#include "internal/param_build_set.h"
14
#include "internal/nelem.h"
15
16
#ifndef FIPS_MODULE
17
#include <openssl/asn1.h> /* ossl_ffc_params_print */
18
#endif
19
20
void ossl_ffc_params_init(FFC_PARAMS *params)
21
1.77M
{
22
1.77M
    memset(params, 0, sizeof(*params));
23
1.77M
    params->pcounter = -1;
24
1.77M
    params->gindex = FFC_UNVERIFIABLE_GINDEX;
25
1.77M
    params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
26
1.77M
}
27
28
void ossl_ffc_params_cleanup(FFC_PARAMS *params)
29
886k
{
30
886k
    BN_free(params->p);
31
886k
    BN_free(params->q);
32
886k
    BN_free(params->g);
33
886k
    BN_free(params->j);
34
886k
    OPENSSL_free(params->seed);
35
886k
    ossl_ffc_params_init(params);
36
886k
}
37
38
void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
39
149k
{
40
149k
    if (p != NULL && p != d->p) {
41
149k
        BN_free(d->p);
42
149k
        d->p = p;
43
149k
    }
44
149k
    if (q != NULL && q != d->q) {
45
140k
        BN_free(d->q);
46
140k
        d->q = q;
47
140k
    }
48
149k
    if (g != NULL && g != d->g) {
49
149k
        BN_free(d->g);
50
149k
        d->g = g;
51
149k
    }
52
149k
}
53
54
void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
55
    const BIGNUM **q, const BIGNUM **g)
56
85.0k
{
57
85.0k
    if (p != NULL)
58
85.0k
        *p = d->p;
59
85.0k
    if (q != NULL)
60
74.5k
        *q = d->q;
61
85.0k
    if (g != NULL)
62
74.5k
        *g = d->g;
63
85.0k
}
64
65
/* j is the 'cofactor' that is optionally output for ASN1. */
66
void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j)
67
145k
{
68
145k
    BN_free(d->j);
69
145k
    d->j = NULL;
70
145k
    if (j != NULL)
71
21.3k
        d->j = j;
72
145k
}
73
74
int ossl_ffc_params_set_seed(FFC_PARAMS *params,
75
    const unsigned char *seed, size_t seedlen)
76
4.28k
{
77
4.28k
    if (params->seed != NULL) {
78
0
        if (params->seed == seed)
79
0
            return 1;
80
0
        OPENSSL_free(params->seed);
81
0
    }
82
83
4.28k
    if (seed != NULL && seedlen > 0) {
84
344
        params->seed = OPENSSL_memdup(seed, seedlen);
85
344
        if (params->seed == NULL)
86
0
            return 0;
87
344
        params->seedlen = seedlen;
88
3.94k
    } else {
89
3.94k
        params->seed = NULL;
90
3.94k
        params->seedlen = 0;
91
3.94k
    }
92
4.28k
    return 1;
93
4.28k
}
94
95
void ossl_ffc_params_set_gindex(FFC_PARAMS *params, int index)
96
0
{
97
0
    params->gindex = index;
98
0
}
99
100
void ossl_ffc_params_set_pcounter(FFC_PARAMS *params, int index)
101
0
{
102
0
    params->pcounter = index;
103
0
}
104
105
void ossl_ffc_params_set_h(FFC_PARAMS *params, int index)
106
0
{
107
0
    params->h = index;
108
0
}
109
110
void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags)
111
0
{
112
0
    params->flags = flags;
113
0
}
114
115
void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags,
116
    int enable)
117
128k
{
118
128k
    if (enable)
119
82.1k
        params->flags |= flags;
120
45.9k
    else
121
45.9k
        params->flags &= ~flags;
122
128k
}
123
124
void ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props)
125
0
{
126
0
    params->mdname = alg;
127
0
    params->mdprops = props;
128
0
}
129
130
int ossl_ffc_params_set_validate_params(FFC_PARAMS *params,
131
    const unsigned char *seed,
132
    size_t seedlen, int counter)
133
432
{
134
432
    if (!ossl_ffc_params_set_seed(params, seed, seedlen))
135
0
        return 0;
136
432
    params->pcounter = counter;
137
432
    return 1;
138
432
}
139
140
void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
141
    unsigned char **seed, size_t *seedlen,
142
    int *pcounter)
143
381
{
144
381
    if (seed != NULL)
145
381
        *seed = params->seed;
146
381
    if (seedlen != NULL)
147
381
        *seedlen = params->seedlen;
148
381
    if (pcounter != NULL)
149
381
        *pcounter = params->pcounter;
150
381
}
151
152
static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
153
74.4k
{
154
74.4k
    BIGNUM *a;
155
156
    /*
157
     * If source is read only just copy the pointer, so
158
     * we don't have to reallocate it.
159
     */
160
74.4k
    if (src == NULL)
161
24.7k
        a = NULL;
162
49.7k
    else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
163
3.00k
        && !BN_get_flags(src, BN_FLG_MALLOCED))
164
3.00k
        a = (BIGNUM *)src;
165
46.7k
    else if ((a = BN_dup(src)) == NULL)
166
0
        return 0;
167
74.4k
    BN_clear_free(*dst);
168
74.4k
    *dst = a;
169
74.4k
    return 1;
170
74.4k
}
171
172
int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
173
18.6k
{
174
18.6k
    if (!ffc_bn_cpy(&dst->p, src->p)
175
18.6k
        || !ffc_bn_cpy(&dst->g, src->g)
176
18.6k
        || !ffc_bn_cpy(&dst->q, src->q)
177
18.6k
        || !ffc_bn_cpy(&dst->j, src->j))
178
0
        return 0;
179
180
18.6k
    dst->mdname = src->mdname;
181
18.6k
    dst->mdprops = src->mdprops;
182
18.6k
    OPENSSL_free(dst->seed);
183
18.6k
    dst->seedlen = src->seedlen;
184
18.6k
    if (src->seed != NULL) {
185
228
        dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
186
228
        if (dst->seed == NULL)
187
0
            return 0;
188
18.3k
    } else {
189
18.3k
        dst->seed = NULL;
190
18.3k
    }
191
18.6k
    dst->nid = src->nid;
192
18.6k
    dst->pcounter = src->pcounter;
193
18.6k
    dst->h = src->h;
194
18.6k
    dst->gindex = src->gindex;
195
18.6k
    dst->flags = src->flags;
196
18.6k
    dst->keylength = src->keylength;
197
18.6k
    return 1;
198
18.6k
}
199
200
int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
201
107k
{
202
107k
    return BN_cmp(a->p, b->p) == 0
203
107k
        && BN_cmp(a->g, b->g) == 0
204
107k
        && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
205
107k
}
206
207
int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
208
    OSSL_PARAM params[])
209
199k
{
210
199k
    int test_flags;
211
212
199k
    if (ffc->p != NULL
213
199k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
214
0
        return 0;
215
199k
    if (ffc->q != NULL
216
179k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
217
0
        return 0;
218
199k
    if (ffc->g != NULL
219
199k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
220
0
        return 0;
221
199k
    if (ffc->j != NULL
222
18.5k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
223
18.5k
            ffc->j))
224
0
        return 0;
225
199k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
226
199k
            ffc->gindex))
227
0
        return 0;
228
199k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
229
199k
            ffc->pcounter))
230
0
        return 0;
231
199k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
232
0
        return 0;
233
199k
    if (ffc->seed != NULL
234
440
        && !ossl_param_build_set_octet_string(bld, params,
235
440
            OSSL_PKEY_PARAM_FFC_SEED,
236
440
            ffc->seed, ffc->seedlen))
237
0
        return 0;
238
199k
    if (ffc->nid != NID_undef) {
239
3.04k
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
240
3.04k
        const char *name = ossl_ffc_named_group_get_name(group);
241
242
3.04k
        if (name == NULL
243
3.04k
            || !ossl_param_build_set_utf8_string(bld, params,
244
3.04k
                OSSL_PKEY_PARAM_GROUP_NAME,
245
3.04k
                name))
246
0
            return 0;
247
3.04k
    }
248
199k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
249
199k
    if (!ossl_param_build_set_int(bld, params,
250
199k
            OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
251
0
        return 0;
252
199k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
253
199k
    if (!ossl_param_build_set_int(bld, params,
254
199k
            OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
255
0
        return 0;
256
199k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
257
199k
    if (!ossl_param_build_set_int(bld, params,
258
199k
            OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
259
199k
            test_flags))
260
0
        return 0;
261
262
199k
    if (ffc->mdname != NULL
263
0
        && !ossl_param_build_set_utf8_string(bld, params,
264
0
            OSSL_PKEY_PARAM_FFC_DIGEST,
265
0
            ffc->mdname))
266
0
        return 0;
267
199k
    if (ffc->mdprops != NULL
268
0
        && !ossl_param_build_set_utf8_string(bld, params,
269
0
            OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
270
0
            ffc->mdprops))
271
0
        return 0;
272
199k
    return 1;
273
199k
}
274
275
#ifndef FIPS_MODULE
276
int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
277
157
{
278
157
    if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
279
0
        goto err;
280
157
    if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
281
0
        goto err;
282
157
    if (ffc->q != NULL
283
157
        && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
284
0
        goto err;
285
157
    if (ffc->j != NULL
286
0
        && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
287
0
        goto err;
288
157
    if (ffc->seed != NULL) {
289
0
        size_t i;
290
291
0
        if (!BIO_indent(bp, indent, 128)
292
0
            || BIO_puts(bp, "seed:") <= 0)
293
0
            goto err;
294
0
        for (i = 0; i < ffc->seedlen; i++) {
295
0
            if ((i % 15) == 0) {
296
0
                if (BIO_puts(bp, "\n") <= 0
297
0
                    || !BIO_indent(bp, indent + 4, 128))
298
0
                    goto err;
299
0
            }
300
0
            if (BIO_printf(bp, "%02x%s", ffc->seed[i],
301
0
                    ((i + 1) == ffc->seedlen) ? "" : ":")
302
0
                <= 0)
303
0
                goto err;
304
0
        }
305
0
        if (BIO_write(bp, "\n", 1) <= 0)
306
0
            return 0;
307
0
    }
308
157
    if (ffc->pcounter != -1) {
309
0
        if (!BIO_indent(bp, indent, 128)
310
0
            || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
311
0
            goto err;
312
0
    }
313
157
    return 1;
314
0
err:
315
0
    return 0;
316
157
}
317
#endif /* FIPS_MODULE */