Coverage Report

Created: 2025-12-10 06:24

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