Coverage Report

Created: 2026-06-18 06:34

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