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_dh.c
Line
Count
Source
1
/*
2
 * Copyright 2020-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 "internal/ffc.h"
11
#include "internal/nelem.h"
12
#include "crypto/bn_dh.h"
13
14
#ifndef OPENSSL_NO_DH
15
16
#define FFDHE(sz, keylength) {  \
17
    SN_ffdhe##sz,               \
18
    NID_ffdhe##sz,              \
19
    sz,                         \
20
    keylength,                  \
21
    &ossl_bignum_ffdhe##sz##_p, \
22
    &ossl_bignum_ffdhe##sz##_q, \
23
    &ossl_bignum_const_2,       \
24
}
25
26
#define MODP(sz, keylength) {                               \
27
    SN_modp_##sz, NID_modp_##sz,                            \
28
    sz,                                                     \
29
    keylength,                                              \
30
    &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \
31
    &ossl_bignum_const_2                                    \
32
}
33
34
#define RFC5114(name, uid, sz, tag) {                   \
35
    name, uid,                                          \
36
    sz,                                                 \
37
    0,                                                  \
38
    &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \
39
    &ossl_bignum_dh##tag##_g                            \
40
}
41
42
#else
43
44
#define FFDHE(sz, keylength) { SN_ffdhe##sz, NID_ffdhe##sz }
45
#define MODP(sz, keylength) { SN_modp_##sz, NID_modp_##sz }
46
#define RFC5114(name, uid, sz, tag) { name, uid }
47
48
#endif
49
50
struct dh_named_group_st {
51
    const char *name;
52
    int uid;
53
#ifndef OPENSSL_NO_DH
54
    int32_t nbits;
55
    int keylength;
56
    const BIGNUM *p;
57
    const BIGNUM *q;
58
    const BIGNUM *g;
59
#endif
60
};
61
62
/*
63
 * The private key length values are taken from RFC7919 with the values for
64
 * MODP primes given the same lengths as the equivalent FFDHE.
65
 * The MODP 1536 value is approximated.
66
 */
67
static const DH_NAMED_GROUP dh_named_groups[] = {
68
    FFDHE(2048, 225),
69
    FFDHE(3072, 275),
70
    FFDHE(4096, 325),
71
    FFDHE(6144, 375),
72
    FFDHE(8192, 400),
73
#ifndef FIPS_MODULE
74
    MODP(1536, 200),
75
#endif
76
    MODP(2048, 225),
77
    MODP(3072, 275),
78
    MODP(4096, 325),
79
    MODP(6144, 375),
80
    MODP(8192, 400),
81
/*
82
 * Additional dh named groups from RFC 5114 that have a different g.
83
 * The uid can be any unique identifier.
84
 */
85
#ifndef FIPS_MODULE
86
    RFC5114("dh_1024_160", 1, 1024, 1024_160),
87
    RFC5114("dh_2048_224", 2, 2048, 2048_224),
88
    RFC5114("dh_2048_256", 3, 2048, 2048_256),
89
#endif
90
};
91
92
const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name)
93
0
{
94
0
    size_t i;
95
96
0
    for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
97
0
        if (OPENSSL_strcasecmp(dh_named_groups[i].name, name) == 0)
98
0
            return &dh_named_groups[i];
99
0
    }
100
0
    return NULL;
101
0
}
102
103
const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid)
104
0
{
105
0
    size_t i;
106
107
0
    for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
108
0
        if (dh_named_groups[i].uid == uid)
109
0
            return &dh_named_groups[i];
110
0
    }
111
0
    return NULL;
112
0
}
113
114
#ifndef OPENSSL_NO_DH
115
const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
116
    const BIGNUM *q,
117
    const BIGNUM *g)
118
0
{
119
0
    size_t i;
120
121
0
    for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
122
        /* Keep searching until a matching p and g is found */
123
0
        if (BN_cmp(p, dh_named_groups[i].p) == 0
124
0
            && BN_cmp(g, dh_named_groups[i].g) == 0
125
            /* Verify q is correct if it exists */
126
0
            && (q == NULL || BN_cmp(q, dh_named_groups[i].q) == 0))
127
0
            return &dh_named_groups[i];
128
0
    }
129
0
    return NULL;
130
0
}
131
#endif
132
133
int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group)
134
0
{
135
0
    if (group == NULL)
136
0
        return NID_undef;
137
0
    return group->uid;
138
0
}
139
140
const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *group)
141
0
{
142
0
    if (group == NULL)
143
0
        return NULL;
144
0
    return group->name;
145
0
}
146
147
#ifndef OPENSSL_NO_DH
148
int ossl_ffc_named_group_get_keylength(const DH_NAMED_GROUP *group)
149
0
{
150
0
    if (group == NULL)
151
0
        return 0;
152
0
    return group->keylength;
153
0
}
154
155
const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group)
156
0
{
157
0
    if (group == NULL)
158
0
        return NULL;
159
0
    return group->q;
160
0
}
161
162
int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group)
163
0
{
164
0
    if (ffc == NULL || group == NULL)
165
0
        return 0;
166
167
0
    ossl_ffc_params_set0_pqg(ffc, (BIGNUM *)group->p, (BIGNUM *)group->q,
168
0
        (BIGNUM *)group->g);
169
0
    ffc->keylength = group->keylength;
170
171
    /* flush the cached nid, The DH layer is responsible for caching */
172
0
    ffc->nid = NID_undef;
173
0
    return 1;
174
0
}
175
#endif