1 /* $OpenBSD: dh_check.c,v 1.22 2021/12/05 13:45:26 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/bn.h> 62 #include <openssl/dh.h> 63 64 #include "bn_lcl.h" 65 66 #define DH_NUMBER_ITERATIONS_FOR_PRIME 64 67 68 int 69 DH_check_params(const DH *dh, int *flags) 70 { 71 BIGNUM *max_g = NULL; 72 int ok = 0; 73 74 *flags = 0; 75 76 if (!BN_is_odd(dh->p)) 77 *flags |= DH_CHECK_P_NOT_PRIME; 78 79 /* 80 * Check that 1 < dh->g < p - 1 81 */ 82 83 if (BN_cmp(dh->g, BN_value_one()) <= 0) 84 *flags |= DH_NOT_SUITABLE_GENERATOR; 85 /* max_g = p - 1 */ 86 if ((max_g = BN_dup(dh->p)) == NULL) 87 goto err; 88 if (!BN_sub_word(max_g, 1)) 89 goto err; 90 /* check that g < max_g */ 91 if (BN_cmp(dh->g, max_g) >= 0) 92 *flags |= DH_NOT_SUITABLE_GENERATOR; 93 94 ok = 1; 95 96 err: 97 BN_free(max_g); 98 99 return ok; 100 } 101 102 /* 103 * Check that p is a safe prime and that g is a suitable generator. 104 */ 105 106 int 107 DH_check(const DH *dh, int *flags) 108 { 109 BN_CTX *ctx = NULL; 110 int is_prime; 111 int ok = 0; 112 113 *flags = 0; 114 115 if (!DH_check_params(dh, flags)) 116 goto err; 117 118 ctx = BN_CTX_new(); 119 if (ctx == NULL) 120 goto err; 121 BN_CTX_start(ctx); 122 123 if (dh->q != NULL) { 124 BIGNUM *quotient, *residue; 125 126 if ((quotient = BN_CTX_get(ctx)) == NULL) 127 goto err; 128 if ((residue = BN_CTX_get(ctx)) == NULL) 129 goto err; 130 if ((*flags & DH_NOT_SUITABLE_GENERATOR) == 0) { 131 /* Check g^q == 1 mod p */ 132 if (!BN_mod_exp_ct(residue, dh->g, dh->q, dh->p, ctx)) 133 goto err; 134 if (!BN_is_one(residue)) 135 *flags |= DH_NOT_SUITABLE_GENERATOR; 136 } 137 is_prime = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, 138 ctx, NULL); 139 if (is_prime < 0) 140 goto err; 141 if (is_prime == 0) 142 *flags |= DH_CHECK_Q_NOT_PRIME; 143 /* Check p == 1 mod q, i.e., q divides p - 1 */ 144 if (!BN_div_ct(quotient, residue, dh->p, dh->q, ctx)) 145 goto err; 146 if (!BN_is_one(residue)) 147 *flags |= DH_CHECK_INVALID_Q_VALUE; 148 if (dh->j != NULL && BN_cmp(dh->j, quotient) != 0) 149 *flags |= DH_CHECK_INVALID_J_VALUE; 150 } 151 152 is_prime = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, 153 ctx, NULL); 154 if (is_prime < 0) 155 goto err; 156 if (is_prime == 0) 157 *flags |= DH_CHECK_P_NOT_PRIME; 158 else if (dh->q == NULL) { 159 BIGNUM *q; 160 161 if ((q = BN_CTX_get(ctx)) == NULL) 162 goto err; 163 if (!BN_rshift1(q, dh->p)) 164 goto err; 165 is_prime = BN_is_prime_ex(q, DH_NUMBER_ITERATIONS_FOR_PRIME, 166 ctx, NULL); 167 if (is_prime < 0) 168 goto err; 169 if (is_prime == 0) 170 *flags |= DH_CHECK_P_NOT_SAFE_PRIME; 171 } 172 173 ok = 1; 174 175 err: 176 BN_CTX_end(ctx); 177 BN_CTX_free(ctx); 178 return ok; 179 } 180 181 int 182 DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *flags) 183 { 184 BN_CTX *ctx = NULL; 185 BIGNUM *max_pub_key; 186 int ok = 0; 187 188 *flags = 0; 189 190 if ((ctx = BN_CTX_new()) == NULL) 191 goto err; 192 BN_CTX_start(ctx); 193 if ((max_pub_key = BN_CTX_get(ctx)) == NULL) 194 goto err; 195 196 /* 197 * Check that 1 < pub_key < dh->p - 1 198 */ 199 200 if (BN_cmp(pub_key, BN_value_one()) <= 0) 201 *flags |= DH_CHECK_PUBKEY_TOO_SMALL; 202 203 /* max_pub_key = dh->p - 1 */ 204 if (BN_copy(max_pub_key, dh->p) == NULL) 205 goto err; 206 if (!BN_sub_word(max_pub_key, 1)) 207 goto err; 208 209 if (BN_cmp(pub_key, max_pub_key) >= 0) 210 *flags |= DH_CHECK_PUBKEY_TOO_LARGE; 211 212 /* 213 * If dh->q is set, check that pub_key^q == 1 mod p 214 */ 215 216 if (dh->q != NULL) { 217 BIGNUM *residue; 218 219 if ((residue = BN_CTX_get(ctx)) == NULL) 220 goto err; 221 222 if (!BN_mod_exp_ct(residue, pub_key, dh->q, dh->p, ctx)) 223 goto err; 224 if (!BN_is_one(residue)) 225 *flags = DH_CHECK_PUBKEY_INVALID; 226 } 227 228 ok = 1; 229 230 err: 231 BN_CTX_end(ctx); 232 BN_CTX_free(ctx); 233 234 return ok; 235 } 236