Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/ffc/ffc_params.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2024 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.90M
{
22
1.90M
    memset(params, 0, sizeof(*params));
23
1.90M
    params->pcounter = -1;
24
1.90M
    params->gindex = FFC_UNVERIFIABLE_GINDEX;
25
1.90M
    params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
26
1.90M
}
27
28
void ossl_ffc_params_cleanup(FFC_PARAMS *params)
29
954k
{
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
954k
    BN_free(params->p);
38
954k
    BN_free(params->q);
39
954k
    BN_free(params->g);
40
954k
    BN_free(params->j);
41
954k
    OPENSSL_free(params->seed);
42
954k
#endif
43
954k
    ossl_ffc_params_init(params);
44
954k
}
45
46
void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
47
150k
{
48
150k
    if (p != NULL && p != d->p) {
49
150k
        BN_free(d->p);
50
150k
        d->p = p;
51
150k
    }
52
150k
    if (q != NULL && q != d->q) {
53
140k
        BN_free(d->q);
54
140k
        d->q = q;
55
140k
    }
56
150k
    if (g != NULL && g != d->g) {
57
150k
        BN_free(d->g);
58
150k
        d->g = g;
59
150k
    }
60
150k
}
61
62
void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
63
    const BIGNUM **q, const BIGNUM **g)
64
83.5k
{
65
83.5k
    if (p != NULL)
66
83.5k
        *p = d->p;
67
83.5k
    if (q != NULL)
68
72.3k
        *q = d->q;
69
83.5k
    if (g != NULL)
70
72.3k
        *g = d->g;
71
83.5k
}
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
146k
{
76
146k
    BN_free(d->j);
77
146k
    d->j = NULL;
78
146k
    if (j != NULL)
79
19.0k
        d->j = j;
80
146k
}
81
82
int ossl_ffc_params_set_seed(FFC_PARAMS *params,
83
    const unsigned char *seed, size_t seedlen)
84
4.54k
{
85
4.54k
    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.54k
    if (seed != NULL && seedlen > 0) {
92
360
        params->seed = OPENSSL_memdup(seed, seedlen);
93
360
        if (params->seed == NULL)
94
0
            return 0;
95
360
        params->seedlen = seedlen;
96
4.18k
    } else {
97
4.18k
        params->seed = NULL;
98
4.18k
        params->seedlen = 0;
99
4.18k
    }
100
4.54k
    return 1;
101
4.54k
}
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
154k
{
126
154k
    if (enable)
127
99.8k
        params->flags |= flags;
128
55.0k
    else
129
55.0k
        params->flags &= ~flags;
130
154k
}
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
446
{
142
446
    if (!ossl_ffc_params_set_seed(params, seed, seedlen))
143
0
        return 0;
144
446
    params->pcounter = counter;
145
446
    return 1;
146
446
}
147
148
void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
149
    unsigned char **seed, size_t *seedlen,
150
    int *pcounter)
151
400
{
152
400
    if (seed != NULL)
153
400
        *seed = params->seed;
154
400
    if (seedlen != NULL)
155
400
        *seedlen = params->seedlen;
156
400
    if (pcounter != NULL)
157
400
        *pcounter = params->pcounter;
158
400
}
159
160
static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
161
78.4k
{
162
78.4k
    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
78.4k
    if (src == NULL)
169
25.6k
        a = NULL;
170
52.7k
    else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
171
3.01k
        && !BN_get_flags(src, BN_FLG_MALLOCED))
172
3.01k
        a = (BIGNUM *)src;
173
49.7k
    else if ((a = BN_dup(src)) == NULL)
174
0
        return 0;
175
78.4k
    BN_clear_free(*dst);
176
78.4k
    *dst = a;
177
78.4k
    return 1;
178
78.4k
}
179
180
int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
181
19.6k
{
182
19.6k
    if (!ffc_bn_cpy(&dst->p, src->p)
183
19.6k
        || !ffc_bn_cpy(&dst->g, src->g)
184
19.6k
        || !ffc_bn_cpy(&dst->q, src->q)
185
19.6k
        || !ffc_bn_cpy(&dst->j, src->j))
186
0
        return 0;
187
188
19.6k
    dst->mdname = src->mdname;
189
19.6k
    dst->mdprops = src->mdprops;
190
19.6k
    OPENSSL_free(dst->seed);
191
19.6k
    dst->seedlen = src->seedlen;
192
19.6k
    if (src->seed != NULL) {
193
219
        dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
194
219
        if (dst->seed == NULL)
195
0
            return 0;
196
19.3k
    } else {
197
19.3k
        dst->seed = NULL;
198
19.3k
    }
199
19.6k
    dst->nid = src->nid;
200
19.6k
    dst->pcounter = src->pcounter;
201
19.6k
    dst->h = src->h;
202
19.6k
    dst->gindex = src->gindex;
203
19.6k
    dst->flags = src->flags;
204
19.6k
    dst->keylength = src->keylength;
205
19.6k
    return 1;
206
19.6k
}
207
208
int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
209
108k
{
210
108k
    return BN_cmp(a->p, b->p) == 0
211
108k
        && BN_cmp(a->g, b->g) == 0
212
108k
        && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
213
108k
}
214
215
int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
216
    OSSL_PARAM params[])
217
197k
{
218
197k
    int test_flags;
219
220
197k
    if (ffc->p != NULL
221
197k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
222
0
        return 0;
223
197k
    if (ffc->q != NULL
224
176k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
225
0
        return 0;
226
197k
    if (ffc->g != NULL
227
197k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
228
0
        return 0;
229
197k
    if (ffc->j != NULL
230
17.5k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
231
17.5k
            ffc->j))
232
0
        return 0;
233
197k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
234
197k
            ffc->gindex))
235
0
        return 0;
236
197k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
237
197k
            ffc->pcounter))
238
0
        return 0;
239
197k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
240
0
        return 0;
241
197k
    if (ffc->seed != NULL
242
422
        && !ossl_param_build_set_octet_string(bld, params,
243
422
            OSSL_PKEY_PARAM_FFC_SEED,
244
422
            ffc->seed, ffc->seedlen))
245
0
        return 0;
246
197k
    if (ffc->nid != NID_undef) {
247
2.90k
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
248
2.90k
        const char *name = ossl_ffc_named_group_get_name(group);
249
250
2.90k
        if (name == NULL
251
2.90k
            || !ossl_param_build_set_utf8_string(bld, params,
252
2.90k
                OSSL_PKEY_PARAM_GROUP_NAME,
253
2.90k
                name))
254
0
            return 0;
255
2.90k
    }
256
197k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
257
197k
    if (!ossl_param_build_set_int(bld, params,
258
197k
            OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
259
0
        return 0;
260
197k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
261
197k
    if (!ossl_param_build_set_int(bld, params,
262
197k
            OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
263
0
        return 0;
264
197k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
265
197k
    if (!ossl_param_build_set_int(bld, params,
266
197k
            OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
267
197k
            test_flags))
268
0
        return 0;
269
270
197k
    if (ffc->mdname != NULL
271
0
        && !ossl_param_build_set_utf8_string(bld, params,
272
0
            OSSL_PKEY_PARAM_FFC_DIGEST,
273
0
            ffc->mdname))
274
0
        return 0;
275
197k
    if (ffc->mdprops != NULL
276
0
        && !ossl_param_build_set_utf8_string(bld, params,
277
0
            OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
278
0
            ffc->mdprops))
279
0
        return 0;
280
197k
    return 1;
281
197k
}
282
283
#ifndef FIPS_MODULE
284
int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
285
123
{
286
123
    if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
287
0
        goto err;
288
123
    if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
289
0
        goto err;
290
123
    if (ffc->q != NULL
291
123
        && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
292
0
        goto err;
293
123
    if (ffc->j != NULL
294
0
        && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
295
0
        goto err;
296
123
    if (ffc->seed != NULL) {
297
0
        size_t i;
298
299
0
        if (!BIO_indent(bp, indent, 128)
300
0
            || BIO_puts(bp, "seed:") <= 0)
301
0
            goto err;
302
0
        for (i = 0; i < ffc->seedlen; i++) {
303
0
            if ((i % 15) == 0) {
304
0
                if (BIO_puts(bp, "\n") <= 0
305
0
                    || !BIO_indent(bp, indent + 4, 128))
306
0
                    goto err;
307
0
            }
308
0
            if (BIO_printf(bp, "%02x%s", ffc->seed[i],
309
0
                    ((i + 1) == ffc->seedlen) ? "" : ":")
310
0
                <= 0)
311
0
                goto err;
312
0
        }
313
0
        if (BIO_write(bp, "\n", 1) <= 0)
314
0
            return 0;
315
0
    }
316
123
    if (ffc->pcounter != -1) {
317
0
        if (!BIO_indent(bp, indent, 128)
318
0
            || BIO_printf(bp, "counter: %d\n", ffc->pcounter) <= 0)
319
0
            goto err;
320
0
    }
321
123
    return 1;
322
0
err:
323
0
    return 0;
324
123
}
325
#endif /* FIPS_MODULE */