Coverage Report

Created: 2026-05-24 07:14

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