Coverage Report

Created: 2025-07-23 06:49

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