| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/dh.h> |
| 16 |
| 17 | #include <openssl/bn.h> |
| 18 | #include <openssl/err.h> |
| 19 |
| 20 | #include "../bn/internal.h" |
| 21 | #include "internal.h" |
| 22 |
| 23 |
| 24 | static_assert(OPENSSL_DH_MAX_MODULUS_BITS <= |
| 25 | BN_MONTGOMERY_MAX_WORDS * BN_BITS2, |
| 26 | "Max DH size too big for Montgomery arithmetic"); |
| 27 |
| 28 | int dh_check_params_fast(const DH *dh) { |
| 29 | // Most operations scale with p and q. |
| 30 | if (BN_is_negative(dh->p) || !BN_is_odd(dh->p) || |
| 31 | BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
| 32 | OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PARAMETERS); |
| 33 | return 0; |
| 34 | } |
| 35 |
| 36 | // q must be bounded by p. |
| 37 | if (dh->q != NULL && (BN_is_negative(dh->q) || BN_ucmp(dh->q, dh->p) > 0)) { |
| 38 | OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PARAMETERS); |
| 39 | return 0; |
| 40 | } |
| 41 |
| 42 | // g must be an element of p's multiplicative group. |
| 43 | if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || |
| 44 | BN_ucmp(dh->g, dh->p) >= 0) { |
| 45 | OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PARAMETERS); |
| 46 | return 0; |
| 47 | } |
| 48 |
| 49 | return 1; |
| 50 | } |
| 51 |
| 52 | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *out_flags) { |
| 53 | *out_flags = 0; |
| 54 | if (!dh_check_params_fast(dh)) { |
| 55 | return 0; |
| 56 | } |
| 57 |
| 58 | BN_CTX *ctx = BN_CTX_new(); |
| 59 | if (ctx == NULL) { |
| 60 | return 0; |
| 61 | } |
| 62 | BN_CTX_start(ctx); |
| 63 |
| 64 | int ok = 0; |
| 65 |
| 66 | // Check |pub_key| is greater than 1. |
| 67 | if (BN_cmp(pub_key, BN_value_one()) <= 0) { |
| 68 | *out_flags |= DH_CHECK_PUBKEY_TOO_SMALL; |
| 69 | } |
| 70 |
| 71 | // Check |pub_key| is less than |dh->p| - 1. |
| 72 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 73 | if (tmp == NULL || |
| 74 | !BN_copy(tmp, dh->p) || |
| 75 | !BN_sub_word(tmp, 1)) { |
| 76 | goto err; |
| 77 | } |
| 78 | if (BN_cmp(pub_key, tmp) >= 0) { |
| 79 | *out_flags |= DH_CHECK_PUBKEY_TOO_LARGE; |
| 80 | } |
| 81 |
| 82 | if (dh->q != NULL) { |
| 83 | // Check |pub_key|^|dh->q| is 1 mod |dh->p|. This is necessary for RFC 5114 |
| 84 | // groups which are not safe primes but pick a generator on a prime-order |
| 85 | // subgroup of size |dh->q|. |
| 86 | if (!BN_mod_exp_mont(tmp, pub_key, dh->q, dh->p, ctx, NULL)) { |
| 87 | goto err; |
| 88 | } |
| 89 | if (!BN_is_one(tmp)) { |
| 90 | *out_flags |= DH_CHECK_PUBKEY_INVALID; |
| 91 | } |
| 92 | } |
| 93 |
| 94 | ok = 1; |
| 95 |
| 96 | err: |
| 97 | BN_CTX_end(ctx); |
| 98 | BN_CTX_free(ctx); |
| 99 | return ok; |
| 100 | } |
| 101 |
| 102 |
| 103 | int DH_check(const DH *dh, int *out_flags) { |
| 104 | *out_flags = 0; |
| 105 | if (!dh_check_params_fast(dh)) { |
| 106 | return 0; |
| 107 | } |
| 108 |
| 109 | // Check that p is a safe prime and if g is 2, 3 or 5, check that it is a |
| 110 | // suitable generator where: |
| 111 | // for 2, p mod 24 == 11 |
| 112 | // for 3, p mod 12 == 5 |
| 113 | // for 5, p mod 10 == 3 or 7 |
| 114 | // should hold. |
| 115 | int ok = 0, r; |
| 116 | BN_CTX *ctx = NULL; |
| 117 | BN_ULONG l; |
| 118 | BIGNUM *t1 = NULL, *t2 = NULL; |
| 119 |
| 120 | ctx = BN_CTX_new(); |
| 121 | if (ctx == NULL) { |
| 122 | goto err; |
| 123 | } |
| 124 | BN_CTX_start(ctx); |
| 125 | t1 = BN_CTX_get(ctx); |
| 126 | if (t1 == NULL) { |
| 127 | goto err; |
| 128 | } |
| 129 | t2 = BN_CTX_get(ctx); |
| 130 | if (t2 == NULL) { |
| 131 | goto err; |
| 132 | } |
| 133 |
| 134 | if (dh->q) { |
| 135 | if (BN_cmp(dh->g, BN_value_one()) <= 0) { |
| 136 | *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR; |
| 137 | } else if (BN_cmp(dh->g, dh->p) >= 0) { |
| 138 | *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR; |
| 139 | } else { |
| 140 | // Check g^q == 1 mod p |
| 141 | if (!BN_mod_exp_mont(t1, dh->g, dh->q, dh->p, ctx, NULL)) { |
| 142 | goto err; |
| 143 | } |
| 144 | if (!BN_is_one(t1)) { |
| 145 | *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR; |
| 146 | } |
| 147 | } |
| 148 | r = BN_is_prime_ex(dh->q, BN_prime_checks_for_validation, ctx, NULL); |
| 149 | if (r < 0) { |
| 150 | goto err; |
| 151 | } |
| 152 | if (!r) { |
| 153 | *out_flags |= DH_CHECK_Q_NOT_PRIME; |
| 154 | } |
| 155 | // Check p == 1 mod q i.e. q divides p - 1 |
| 156 | if (!BN_div(t1, t2, dh->p, dh->q, ctx)) { |
| 157 | goto err; |
| 158 | } |
| 159 | if (!BN_is_one(t2)) { |
| 160 | *out_flags |= DH_CHECK_INVALID_Q_VALUE; |
| 161 | } |
| 162 | } else if (BN_is_word(dh->g, DH_GENERATOR_2)) { |
| 163 | l = BN_mod_word(dh->p, 24); |
| 164 | if (l == (BN_ULONG)-1) { |
| 165 | goto err; |
| 166 | } |
| 167 | if (l != 11) { |
| 168 | *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR; |
| 169 | } |
| 170 | } else if (BN_is_word(dh->g, DH_GENERATOR_5)) { |
| 171 | l = BN_mod_word(dh->p, 10); |
| 172 | if (l == (BN_ULONG)-1) { |
| 173 | goto err; |
| 174 | } |
| 175 | if (l != 3 && l != 7) { |
| 176 | *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR; |
| 177 | } |
| 178 | } else { |
| 179 | *out_flags |= DH_CHECK_UNABLE_TO_CHECK_GENERATOR; |
| 180 | } |
| 181 |
| 182 | r = BN_is_prime_ex(dh->p, BN_prime_checks_for_validation, ctx, NULL); |
| 183 | if (r < 0) { |
| 184 | goto err; |
| 185 | } |
| 186 | if (!r) { |
| 187 | *out_flags |= DH_CHECK_P_NOT_PRIME; |
| 188 | } else if (!dh->q) { |
| 189 | if (!BN_rshift1(t1, dh->p)) { |
| 190 | goto err; |
| 191 | } |
| 192 | r = BN_is_prime_ex(t1, BN_prime_checks_for_validation, ctx, NULL); |
| 193 | if (r < 0) { |
| 194 | goto err; |
| 195 | } |
| 196 | if (!r) { |
| 197 | *out_flags |= DH_CHECK_P_NOT_SAFE_PRIME; |
| 198 | } |
| 199 | } |
| 200 | ok = 1; |
| 201 |
| 202 | err: |
| 203 | if (ctx != NULL) { |
| 204 | BN_CTX_end(ctx); |
| 205 | BN_CTX_free(ctx); |
| 206 | } |
| 207 | return ok; |
| 208 | } |
| 209 |