Coverage Report

Created: 2026-02-14 07:20

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