Coverage Report

Created: 2026-05-24 07:14

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