Coverage Report

Created: 2026-05-24 07:14

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