Coverage Report

Created: 2023-06-08 06:41

/src/openssl30/crypto/ffc/ffc_params.c
Line
Count
Source (jump to first uncovered line)
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
12.0k
{
22
12.0k
    memset(params, 0, sizeof(*params));
23
12.0k
    params->pcounter = -1;
24
12.0k
    params->gindex = FFC_UNVERIFIABLE_GINDEX;
25
12.0k
    params->flags = FFC_PARAM_FLAG_VALIDATE_PQG;
26
12.0k
}
27
28
void ossl_ffc_params_cleanup(FFC_PARAMS *params)
29
6.01k
{
30
6.01k
    BN_free(params->p);
31
6.01k
    BN_free(params->q);
32
6.01k
    BN_free(params->g);
33
6.01k
    BN_free(params->j);
34
6.01k
    OPENSSL_free(params->seed);
35
6.01k
    ossl_ffc_params_init(params);
36
6.01k
}
37
38
void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
39
5
{
40
5
    if (p != NULL && p != d->p) {
41
5
        BN_free(d->p);
42
5
        d->p = p;
43
5
    }
44
5
    if (q != NULL && q != d->q) {
45
5
        BN_free(d->q);
46
5
        d->q = q;
47
5
    }
48
5
    if (g != NULL && g != d->g) {
49
5
        BN_free(d->g);
50
5
        d->g = g;
51
5
    }
52
5
}
53
54
void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p,
55
                              const BIGNUM **q, const BIGNUM **g)
56
69
{
57
69
    if (p != NULL)
58
69
        *p = d->p;
59
69
    if (q != NULL)
60
69
        *q = d->q;
61
69
    if (g != NULL)
62
69
        *g = d->g;
63
69
}
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
5
{
69
5
    BN_free(d->j);
70
5
    d->j = NULL;
71
5
    if (j != NULL)
72
1
        d->j = j;
73
5
}
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
    dst->mdname = src->mdname;
186
0
    dst->mdprops = src->mdprops;
187
0
    OPENSSL_free(dst->seed);
188
0
    dst->seedlen = src->seedlen;
189
0
    if (src->seed != NULL) {
190
0
        dst->seed = OPENSSL_memdup(src->seed, src->seedlen);
191
0
        if  (dst->seed == NULL)
192
0
            return 0;
193
0
    } else {
194
0
        dst->seed = NULL;
195
0
    }
196
0
    dst->nid = src->nid;
197
0
    dst->pcounter = src->pcounter;
198
0
    dst->h = src->h;
199
0
    dst->gindex = src->gindex;
200
0
    dst->flags = src->flags;
201
0
    dst->keylength = src->keylength;
202
0
    return 1;
203
0
}
204
205
int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q)
206
0
{
207
0
    return BN_cmp(a->p, b->p) == 0
208
0
           && BN_cmp(a->g, b->g) == 0
209
0
           && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */
210
0
}
211
212
int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld,
213
                      OSSL_PARAM params[])
214
4
{
215
4
    int test_flags;
216
217
4
    if (ffc == NULL)
218
0
        return 0;
219
220
4
    if (ffc->p != NULL
221
4
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_P, ffc->p))
222
0
        return 0;
223
4
    if (ffc->q != NULL
224
4
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_Q, ffc->q))
225
0
        return 0;
226
4
    if (ffc->g != NULL
227
4
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_G, ffc->g))
228
0
        return 0;
229
4
    if (ffc->j != NULL
230
4
        && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR,
231
1
                                    ffc->j))
232
0
        return 0;
233
4
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX,
234
4
                                  ffc->gindex))
235
0
        return 0;
236
4
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER,
237
4
                                  ffc->pcounter))
238
0
        return 0;
239
4
    if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h))
240
0
        return 0;
241
4
    if (ffc->seed != NULL
242
4
        && !ossl_param_build_set_octet_string(bld, params,
243
0
                                              OSSL_PKEY_PARAM_FFC_SEED,
244
0
                                              ffc->seed, ffc->seedlen))
245
0
        return 0;
246
4
    if (ffc->nid != NID_undef) {
247
0
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
248
0
        const char *name = ossl_ffc_named_group_get_name(group);
249
250
0
        if (name == NULL
251
0
            || !ossl_param_build_set_utf8_string(bld, params,
252
0
                                                 OSSL_PKEY_PARAM_GROUP_NAME,
253
0
                                                 name))
254
0
            return 0;
255
0
    }
256
4
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0);
257
4
    if (!ossl_param_build_set_int(bld, params,
258
4
                                  OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags))
259
0
        return 0;
260
4
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0);
261
4
    if (!ossl_param_build_set_int(bld, params,
262
4
                                  OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags))
263
0
        return 0;
264
4
    test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0);
265
4
    if (!ossl_param_build_set_int(bld, params,
266
4
                                  OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY,
267
4
                                  test_flags))
268
0
        return 0;
269
270
4
    if (ffc->mdname != NULL
271
4
        && !ossl_param_build_set_utf8_string(bld, params,
272
0
                                             OSSL_PKEY_PARAM_FFC_DIGEST,
273
0
                                             ffc->mdname))
274
0
       return 0;
275
4
    if (ffc->mdprops != NULL
276
4
        && !ossl_param_build_set_utf8_string(bld, params,
277
0
                                             OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
278
0
                                             ffc->mdprops))
279
0
        return 0;
280
4
    return 1;
281
4
}
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 % 15) == 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) ? "" : ":") <= 0)
310
0
                goto err;
311
0
        }
312
0
        if (BIO_write(bp, "\n", 1) <= 0)
313
0
            return 0;
314
0
    }
315
0
    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
0
    return 1;
321
0
err:
322
0
    return 0;
323
0
}
324
#endif /* FIPS_MODULE */