Coverage Report

Created: 2018-08-29 13:53

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