Fix BN_is_prime* calls.
[openssl.git] / crypto / dh / dh_check.c
1 /*
2  * Copyright 1995-2016 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 is a safe prime and
17  * if g is 2, 3 or 5, check that it is a suitable generator
18  * where
19  * for 2, p mod 24 == 11
20  * for 3, p mod 12 == 5
21  * for 5, p mod 10 == 3 or 7
22  * should hold.
23  */
24
25 int DH_check(const DH *dh, int *ret)
26 {
27     int ok = 0, r;
28     BN_CTX *ctx = NULL;
29     BN_ULONG l;
30     BIGNUM *t1 = NULL, *t2 = NULL;
31
32     *ret = 0;
33     ctx = BN_CTX_new();
34     if (ctx == NULL)
35         goto err;
36     BN_CTX_start(ctx);
37     t1 = BN_CTX_get(ctx);
38     if (t1 == NULL)
39         goto err;
40     t2 = BN_CTX_get(ctx);
41     if (t2 == NULL)
42         goto err;
43
44     if (dh->q) {
45         if (BN_cmp(dh->g, BN_value_one()) <= 0)
46             *ret |= DH_NOT_SUITABLE_GENERATOR;
47         else if (BN_cmp(dh->g, dh->p) >= 0)
48             *ret |= DH_NOT_SUITABLE_GENERATOR;
49         else {
50             /* Check g^q == 1 mod p */
51             if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
52                 goto err;
53             if (!BN_is_one(t1))
54                 *ret |= DH_NOT_SUITABLE_GENERATOR;
55         }
56         r = BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL);
57         if (r < 0)
58             goto err;
59         if (!r)
60             *ret |= DH_CHECK_Q_NOT_PRIME;
61         /* Check p == 1 mod q  i.e. q divides p - 1 */
62         if (!BN_div(t1, t2, dh->p, dh->q, ctx))
63             goto err;
64         if (!BN_is_one(t2))
65             *ret |= DH_CHECK_INVALID_Q_VALUE;
66         if (dh->j && BN_cmp(dh->j, t1))
67             *ret |= DH_CHECK_INVALID_J_VALUE;
68
69     } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
70         l = BN_mod_word(dh->p, 24);
71         if (l != 11)
72             *ret |= DH_NOT_SUITABLE_GENERATOR;
73     } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
74         l = BN_mod_word(dh->p, 10);
75         if ((l != 3) && (l != 7))
76             *ret |= DH_NOT_SUITABLE_GENERATOR;
77     } else
78         *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
79
80     r = BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL);
81     if (r < 0)
82         goto err;
83     if (!r)
84         *ret |= DH_CHECK_P_NOT_PRIME;
85     else if (!dh->q) {
86         if (!BN_rshift1(t1, dh->p))
87             goto err;
88         r = BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL);
89         if (r < 0)
90             goto err;
91         if (!r)
92             *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
93     }
94     ok = 1;
95  err:
96     if (ctx != NULL) {
97         BN_CTX_end(ctx);
98         BN_CTX_free(ctx);
99     }
100     return (ok);
101 }
102
103 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
104 {
105     int ok = 0;
106     BIGNUM *tmp = NULL;
107     BN_CTX *ctx = NULL;
108
109     *ret = 0;
110     ctx = BN_CTX_new();
111     if (ctx == NULL)
112         goto err;
113     BN_CTX_start(ctx);
114     tmp = BN_CTX_get(ctx);
115     if (tmp == NULL || !BN_set_word(tmp, 1))
116         goto err;
117     if (BN_cmp(pub_key, tmp) <= 0)
118         *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
119     if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
120         goto err;
121     if (BN_cmp(pub_key, tmp) >= 0)
122         *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
123
124     if (dh->q != NULL) {
125         /* Check pub_key^q == 1 mod p */
126         if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
127             goto err;
128         if (!BN_is_one(tmp))
129             *ret |= DH_CHECK_PUBKEY_INVALID;
130     }
131
132     ok = 1;
133  err:
134     if (ctx != NULL) {
135         BN_CTX_end(ctx);
136         BN_CTX_free(ctx);
137     }
138     return (ok);
139 }