Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/dh/dh_check.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/bn.h>
13
#include "dh_local.h"
14
15
0
# define DH_NUMBER_ITERATIONS_FOR_PRIME 64
16
17
/*-
18
 * Check that p and g are suitable enough
19
 *
20
 * p is odd
21
 * 1 < g < p - 1
22
 */
23
int DH_check_params_ex(const DH *dh)
24
0
{
25
0
    int errflags = 0;
26
27
0
    if (!DH_check_params(dh, &errflags))
28
0
        return 0;
29
30
0
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
31
0
        DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_CHECK_P_NOT_PRIME);
32
0
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
33
0
        DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_NOT_SUITABLE_GENERATOR);
34
35
0
    return errflags == 0;
36
0
}
37
38
int DH_check_params(const DH *dh, int *ret)
39
0
{
40
0
    int ok = 0;
41
0
    BIGNUM *tmp = NULL;
42
0
    BN_CTX *ctx = NULL;
43
44
0
    *ret = 0;
45
0
    ctx = BN_CTX_new();
46
0
    if (ctx == NULL)
47
0
        goto err;
48
0
    BN_CTX_start(ctx);
49
0
    tmp = BN_CTX_get(ctx);
50
0
    if (tmp == NULL)
51
0
        goto err;
52
53
0
    if (!BN_is_odd(dh->p))
54
0
        *ret |= DH_CHECK_P_NOT_PRIME;
55
0
    if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || BN_is_one(dh->g))
56
0
        *ret |= DH_NOT_SUITABLE_GENERATOR;
57
0
    if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
58
0
        goto err;
59
0
    if (BN_cmp(dh->g, tmp) >= 0)
60
0
        *ret |= DH_NOT_SUITABLE_GENERATOR;
61
62
0
    ok = 1;
63
0
 err:
64
0
    BN_CTX_end(ctx);
65
0
    BN_CTX_free(ctx);
66
0
    return ok;
67
0
}
68
69
/*-
70
 * Check that p is a safe prime and
71
 * g is a suitable generator.
72
 */
73
int DH_check_ex(const DH *dh)
74
0
{
75
0
    int errflags = 0;
76
77
0
    if (!DH_check(dh, &errflags))
78
0
        return 0;
79
80
0
    if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
81
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_NOT_SUITABLE_GENERATOR);
82
0
    if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
83
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_Q_NOT_PRIME);
84
0
    if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
85
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_Q_VALUE);
86
0
    if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
87
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_J_VALUE);
88
0
    if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
89
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_UNABLE_TO_CHECK_GENERATOR);
90
0
    if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
91
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_PRIME);
92
0
    if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
93
0
        DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_SAFE_PRIME);
94
95
0
    return errflags == 0;
96
0
}
97
98
int DH_check(const DH *dh, int *ret)
99
0
{
100
0
    int ok = 0, r;
101
0
    BN_CTX *ctx = NULL;
102
0
    BIGNUM *t1 = NULL, *t2 = NULL;
103
104
0
    if (!DH_check_params(dh, ret))
105
0
        return 0;
106
107
0
    ctx = BN_CTX_new();
108
0
    if (ctx == NULL)
109
0
        goto err;
110
0
    BN_CTX_start(ctx);
111
0
    t1 = BN_CTX_get(ctx);
112
0
    t2 = BN_CTX_get(ctx);
113
0
    if (t2 == NULL)
114
0
        goto err;
115
116
0
    if (dh->q) {
117
0
        if (BN_cmp(dh->g, BN_value_one()) <= 0)
118
0
            *ret |= DH_NOT_SUITABLE_GENERATOR;
119
0
        else if (BN_cmp(dh->g, dh->p) >= 0)
120
0
            *ret |= DH_NOT_SUITABLE_GENERATOR;
121
0
        else {
122
            /* Check g^q == 1 mod p */
123
0
            if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
124
0
                goto err;
125
0
            if (!BN_is_one(t1))
126
0
                *ret |= DH_NOT_SUITABLE_GENERATOR;
127
0
        }
128
0
        r = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
129
0
        if (r < 0)
130
0
            goto err;
131
0
        if (!r)
132
0
            *ret |= DH_CHECK_Q_NOT_PRIME;
133
        /* Check p == 1 mod q  i.e. q divides p - 1 */
134
0
        if (!BN_div(t1, t2, dh->p, dh->q, ctx))
135
0
            goto err;
136
0
        if (!BN_is_one(t2))
137
0
            *ret |= DH_CHECK_INVALID_Q_VALUE;
138
0
        if (dh->j && BN_cmp(dh->j, t1))
139
0
            *ret |= DH_CHECK_INVALID_J_VALUE;
140
0
    }
141
142
0
    r = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
143
0
    if (r < 0)
144
0
        goto err;
145
0
    if (!r)
146
0
        *ret |= DH_CHECK_P_NOT_PRIME;
147
0
    else if (!dh->q) {
148
0
        if (!BN_rshift1(t1, dh->p))
149
0
            goto err;
150
0
        r = BN_is_prime_ex(t1, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
151
0
        if (r < 0)
152
0
            goto err;
153
0
        if (!r)
154
0
            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
155
0
    }
156
0
    ok = 1;
157
0
 err:
158
0
    BN_CTX_end(ctx);
159
0
    BN_CTX_free(ctx);
160
0
    return ok;
161
0
}
162
163
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
164
0
{
165
0
    int errflags = 0;
166
167
0
    if (!DH_check_pub_key(dh, pub_key, &errflags))
168
0
        return 0;
169
170
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
171
0
        DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL);
172
0
    if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
173
0
        DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE);
174
0
    if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
175
0
        DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID);
176
177
0
    return errflags == 0;
178
0
}
179
180
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
181
0
{
182
0
    int ok = 0;
183
0
    BIGNUM *tmp = NULL;
184
0
    BN_CTX *ctx = NULL;
185
186
0
    *ret = 0;
187
0
    ctx = BN_CTX_new();
188
0
    if (ctx == NULL)
189
0
        goto err;
190
0
    BN_CTX_start(ctx);
191
0
    tmp = BN_CTX_get(ctx);
192
0
    if (tmp == NULL || !BN_set_word(tmp, 1))
193
0
        goto err;
194
0
    if (BN_cmp(pub_key, tmp) <= 0)
195
0
        *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
196
0
    if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
197
0
        goto err;
198
0
    if (BN_cmp(pub_key, tmp) >= 0)
199
0
        *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
200
201
0
    if (dh->q != NULL) {
202
        /* Check pub_key^q == 1 mod p */
203
0
        if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
204
0
            goto err;
205
0
        if (!BN_is_one(tmp))
206
0
            *ret |= DH_CHECK_PUBKEY_INVALID;
207
0
    }
208
209
0
    ok = 1;
210
0
 err:
211
0
    BN_CTX_end(ctx);
212
0
    BN_CTX_free(ctx);
213
0
    return ok;
214
0
}