Coverage Report

Created: 2025-12-31 06:58

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.86M
{
22
1.86M
    memset(params, 0, sizeof(*params));
23
1.86M
    params->pcounter = -1;
24
1.86M
    params->gindex = FFC_UNVERIFIABLE_GINDEX;
25
1.86M
    params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
26
1.86M
}
27
28
void ossl_ffc_params_cleanup(FFC_PARAMS *params)
29
935k
{
30
935k
    BN_free(params->p);
31
935k
    BN_free(params->q);
32
935k
    BN_free(params->g);
33
935k
    BN_free(params->j);
34
935k
    OPENSSL_free(params->seed);
35
935k
    ossl_ffc_params_init(params);
36
935k
}
37
38
void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
39
148k
{
40
148k
    if (p != NULL && p != d->p) {
41
148k
        BN_free(d->p);
42
148k
        d->p = p;
43
148k
    }
44
148k
    if (q != NULL && q != d->q) {
45
137k
        BN_free(d->q);
46
137k
        d->q = q;
47
137k
    }
48
148k
    if (g != NULL && g != d->g) {
49
148k
        BN_free(d->g);
50
148k
        d->g = g;
51
148k
    }
52
148k
}
53
54
void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
55
    const BIGNUM **q, const BIGNUM **g)
56
83.8k
{
57
83.8k
    if (p != NULL)
58
83.8k
        *p = d->p;
59
83.8k
    if (q != NULL)
60
72.3k
        *q = d->q;
61
83.8k
    if (g != NULL)
62
72.3k
        *g = d->g;
63
83.8k
}
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
144k
{
68
144k
    BN_free(d->j);
69
144k
    d->j = NULL;
70
144k
    if (j != NULL)
71
7.86k
        d->j = j;
72
144k
}
73
74
int ossl_ffc_params_set_seed(FFC_PARAMS *params,
75
    const unsigned char *seed, size_t seedlen)
76
870
{
77
870
    if (params == NULL)
78
0
        return 0;
79
80
870
    if (params->seed != NULL) {
81
0
        if (params->seed == seed)
82
0
            return 1;
83
0
        OPENSSL_free(params->seed);
84
0
    }
85
86
870
    if (seed != NULL && seedlen > 0) {
87
8
        params->seed = OPENSSL_memdup(seed, seedlen);
88
8
        if (params->seed == NULL)
89
0
            return 0;
90
8
        params->seedlen = seedlen;
91
862
    } else {
92
862
        params->seed = NULL;
93
862
        params->seedlen = 0;
94
862
    }
95
870
    return 1;
96
870
}
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
159k
{
121
159k
    if (enable)
122
102k
        params->flags |= flags;
123
56.6k
    else
124
56.6k
        params->flags &= ~flags;
125
159k
}
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
437
{
138
437
    if (!ossl_ffc_params_set_seed(params, seed, seedlen))
139
0
        return 0;
140
437
    params->pcounter = counter;
141
437
    return 1;
142
437
}
143
144
void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params,
145
    unsigned char **seed, size_t *seedlen,
146
    int *pcounter)
147
399
{
148
399
    if (seed != NULL)
149
399
        *seed = params->seed;
150
399
    if (seedlen != NULL)
151
399
        *seedlen = params->seedlen;
152
399
    if (pcounter != NULL)
153
399
        *pcounter = params->pcounter;
154
399
}
155
156
static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src)
157
78.2k
{
158
78.2k
    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.2k
    if (src == NULL)
165
25.3k
        a = NULL;
166
52.9k
    else if (BN_get_flags(src, BN_FLG_STATIC_DATA)
167
3.08k
        && !BN_get_flags(src, BN_FLG_MALLOCED))
168
3.08k
        a = (BIGNUM *)src;
169
49.8k
    else if ((a = BN_dup(src)) == NULL)
170
0
        return 0;
171
78.2k
    BN_clear_free(*dst);
172
78.2k
    *dst = a;
173
78.2k
    return 1;
174
78.2k
}
175
176
int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src)
177
19.5k
{
178
19.5k
    if (!ffc_bn_cpy(&dst->p, src->p)
179
19.5k
        || !ffc_bn_cpy(&dst->g, src->g)
180
19.5k
        || !ffc_bn_cpy(&dst->q, src->q)
181
19.5k
        || !ffc_bn_cpy(&dst->j, src->j))
182
0
        return 0;
183
184
19.5k
    dst->mdname = src->mdname;
185
19.5k
    dst->mdprops = src->mdprops;
186
19.5k
    OPENSSL_free(dst->seed);
187
19.5k
    dst->seedlen = src->seedlen;
188
19.5k
    if (src->seed != NULL) {
189
220
        dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
190
220
        if (dst->seed == NULL)
191
0
            return 0;
192
19.3k
    } else {
193
19.3k
        dst->seed = NULL;
194
19.3k
    }
195
19.5k
    dst->nid = src->nid;
196
19.5k
    dst->pcounter = src->pcounter;
197
19.5k
    dst->h = src->h;
198
19.5k
    dst->gindex = src->gindex;
199
19.5k
    dst->flags = src->flags;
200
19.5k
    dst->keylength = src->keylength;
201
19.5k
    return 1;
202
19.5k
}
203
204
int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
205
111k
{
206
111k
    return BN_cmp(a->p, b->p) == 0
207
111k
        && BN_cmp(a->g, b->g) == 0
208
111k
        && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
209
111k
}
210
211
int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
212
    OSSL_PARAM params[])
213
29.1k
{
214
29.1k
    int test_flags;
215
216
29.1k
    if (ffc == NULL)
217
0
        return 0;
218
219
29.1k
    if (ffc->p != NULL
220
29.1k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
221
0
        return 0;
222
29.1k
    if (ffc->q != NULL
223
22.2k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
224
0
        return 0;
225
29.1k
    if (ffc->g != NULL
226
29.1k
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
227
0
        return 0;
228
29.1k
    if (ffc->j != NULL
229
970
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
230
970
            ffc->j))
231
0
        return 0;
232
29.1k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
233
29.1k
            ffc->gindex))
234
0
        return 0;
235
29.1k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
236
29.1k
            ffc->pcounter))
237
0
        return 0;
238
29.1k
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
239
0
        return 0;
240
29.1k
    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
29.1k
    if (ffc->nid != NID_undef) {
246
824
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
247
824
        const char *name = ossl_ffc_named_group_get_name(group);
248
249
824
        if (name == NULL
250
824
            || !ossl_param_build_set_utf8_string(bld, params,
251
824
                OSSL_PKEY_PARAM_GROUP_NAME,
252
824
                name))
253
0
            return 0;
254
824
    }
255
29.1k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
256
29.1k
    if (!ossl_param_build_set_int(bld, params,
257
29.1k
            OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
258
0
        return 0;
259
29.1k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
260
29.1k
    if (!ossl_param_build_set_int(bld, params,
261
29.1k
            OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
262
0
        return 0;
263
29.1k
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
264
29.1k
    if (!ossl_param_build_set_int(bld, params,
265
29.1k
            OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
266
29.1k
            test_flags))
267
0
        return 0;
268
269
29.1k
    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
29.1k
    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
29.1k
    return 1;
280
29.1k
}
281
282
#ifndef FIPS_MODULE
283
int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent)
284
115
{
285
115
    if (!ASN1_bn_print(bp, "prime P:", ffc->p, NULL, indent))
286
0
        goto err;
287
115
    if (!ASN1_bn_print(bp, "generator G:", ffc->g, NULL, indent))
288
0
        goto err;
289
115
    if (ffc->q != NULL
290
115
        && !ASN1_bn_print(bp, "subgroup order Q:", ffc->q, NULL, indent))
291
0
        goto err;
292
115
    if (ffc->j != NULL
293
0
        && !ASN1_bn_print(bp, "subgroup factor:", ffc->j, NULL, indent))
294
0
        goto err;
295
115
    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
115
    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
115
    return 1;
321
0
err:
322
0
    return 0;
323
115
}
324
#endif /* FIPS_MODULE */