Coverage Report

Created: 2023-09-25 06:41

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