Coverage Report

Created: 2026-10-28 06:28

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