]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_check.c
Copyright consolidation 09/10
[thirdparty/openssl.git] / crypto / dh / dh_check.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58#include <stdio.h>
b39fc560 59#include "internal/cryptlib.h"
ec577822 60#include <openssl/bn.h>
0aeddcfa 61#include "dh_locl.h"
d02b48c6 62
1d97c843
TH
63/*-
64 * Check that p is a safe prime and
5f0477f4 65 * if g is 2, 3 or 5, check that it is a suitable generator
d02b48c6
RE
66 * where
67 * for 2, p mod 24 == 11
68 * for 3, p mod 12 == 5
69 * for 5, p mod 10 == 3 or 7
70 * should hold.
71 */
72
f971ccb2 73int DH_check(const DH *dh, int *ret)
0f113f3e
MC
74{
75 int ok = 0;
76 BN_CTX *ctx = NULL;
77 BN_ULONG l;
78 BIGNUM *t1 = NULL, *t2 = NULL;
79
80 *ret = 0;
81 ctx = BN_CTX_new();
82 if (ctx == NULL)
83 goto err;
84 BN_CTX_start(ctx);
85 t1 = BN_CTX_get(ctx);
86 if (t1 == NULL)
87 goto err;
88 t2 = BN_CTX_get(ctx);
89 if (t2 == NULL)
90 goto err;
d02b48c6 91
0f113f3e
MC
92 if (dh->q) {
93 if (BN_cmp(dh->g, BN_value_one()) <= 0)
94 *ret |= DH_NOT_SUITABLE_GENERATOR;
95 else if (BN_cmp(dh->g, dh->p) >= 0)
96 *ret |= DH_NOT_SUITABLE_GENERATOR;
97 else {
98 /* Check g^q == 1 mod p */
99 if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
100 goto err;
101 if (!BN_is_one(t1))
102 *ret |= DH_NOT_SUITABLE_GENERATOR;
103 }
104 if (!BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL))
105 *ret |= DH_CHECK_Q_NOT_PRIME;
106 /* Check p == 1 mod q i.e. q divides p - 1 */
107 if (!BN_div(t1, t2, dh->p, dh->q, ctx))
108 goto err;
109 if (!BN_is_one(t2))
110 *ret |= DH_CHECK_INVALID_Q_VALUE;
111 if (dh->j && BN_cmp(dh->j, t1))
112 *ret |= DH_CHECK_INVALID_J_VALUE;
d02b48c6 113
0f113f3e
MC
114 } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
115 l = BN_mod_word(dh->p, 24);
116 if (l != 11)
117 *ret |= DH_NOT_SUITABLE_GENERATOR;
dfb56425 118 } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
0f113f3e
MC
119 l = BN_mod_word(dh->p, 10);
120 if ((l != 3) && (l != 7))
121 *ret |= DH_NOT_SUITABLE_GENERATOR;
122 } else
123 *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
d02b48c6 124
0f113f3e
MC
125 if (!BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL))
126 *ret |= DH_CHECK_P_NOT_PRIME;
127 else if (!dh->q) {
128 if (!BN_rshift1(t1, dh->p))
129 goto err;
130 if (!BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL))
131 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
132 }
133 ok = 1;
134 err:
135 if (ctx != NULL) {
136 BN_CTX_end(ctx);
137 BN_CTX_free(ctx);
138 }
139 return (ok);
140}
bf3d6c0c
BL
141
142int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
0f113f3e
MC
143{
144 int ok = 0;
b128abc3
MC
145 BIGNUM *tmp = NULL;
146 BN_CTX *ctx = NULL;
bf3d6c0c 147
0f113f3e 148 *ret = 0;
b128abc3
MC
149 ctx = BN_CTX_new();
150 if (ctx == NULL)
0f113f3e 151 goto err;
b128abc3
MC
152 BN_CTX_start(ctx);
153 tmp = BN_CTX_get(ctx);
f5a12207 154 if (tmp == NULL || !BN_set_word(tmp, 1))
b128abc3 155 goto err;
b128abc3 156 if (BN_cmp(pub_key, tmp) <= 0)
0f113f3e 157 *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
f5a12207
MC
158 if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
159 goto err;
b128abc3 160 if (BN_cmp(pub_key, tmp) >= 0)
0f113f3e 161 *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
bf3d6c0c 162
b128abc3
MC
163 if (dh->q != NULL) {
164 /* Check pub_key^q == 1 mod p */
165 if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
166 goto err;
167 if (!BN_is_one(tmp))
168 *ret |= DH_CHECK_PUBKEY_INVALID;
169 }
170
0f113f3e
MC
171 ok = 1;
172 err:
b128abc3
MC
173 if (ctx != NULL) {
174 BN_CTX_end(ctx);
175 BN_CTX_free(ctx);
176 }
0f113f3e
MC
177 return (ok);
178}