2016-05-17 19:38:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 19:38:09 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/bn.h>
|
2016-04-06 16:49:48 +00:00
|
|
|
#include "dh_locl.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2014-12-28 02:48:40 +00:00
|
|
|
/*-
|
|
|
|
* Check that p is a safe prime and
|
2007-09-24 11:22:58 +00:00
|
|
|
* if g is 2, 3 or 5, check that it is a suitable generator
|
1998-12-21 10:52:47 +00:00
|
|
|
* where
|
|
|
|
* for 2, p mod 24 == 11
|
|
|
|
* for 3, p mod 12 == 5
|
|
|
|
* for 5, p mod 10 == 3 or 7
|
|
|
|
* should hold.
|
|
|
|
*/
|
|
|
|
|
2000-11-07 14:30:37 +00:00
|
|
|
int DH_check(const DH *dh, int *ret)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BN_ULONG l;
|
|
|
|
BIGNUM *t1 = NULL, *t2 = NULL;
|
|
|
|
|
|
|
|
*ret = 0;
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
t1 = BN_CTX_get(ctx);
|
|
|
|
if (t1 == NULL)
|
|
|
|
goto err;
|
|
|
|
t2 = BN_CTX_get(ctx);
|
|
|
|
if (t2 == NULL)
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dh->q) {
|
|
|
|
if (BN_cmp(dh->g, BN_value_one()) <= 0)
|
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
|
|
|
else if (BN_cmp(dh->g, dh->p) >= 0)
|
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
|
|
|
else {
|
|
|
|
/* Check g^q == 1 mod p */
|
|
|
|
if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
|
|
|
|
goto err;
|
|
|
|
if (!BN_is_one(t1))
|
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
|
|
|
}
|
|
|
|
if (!BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL))
|
|
|
|
*ret |= DH_CHECK_Q_NOT_PRIME;
|
|
|
|
/* Check p == 1 mod q i.e. q divides p - 1 */
|
|
|
|
if (!BN_div(t1, t2, dh->p, dh->q, ctx))
|
|
|
|
goto err;
|
|
|
|
if (!BN_is_one(t2))
|
|
|
|
*ret |= DH_CHECK_INVALID_Q_VALUE;
|
|
|
|
if (dh->j && BN_cmp(dh->j, t1))
|
|
|
|
*ret |= DH_CHECK_INVALID_J_VALUE;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
|
|
|
|
l = BN_mod_word(dh->p, 24);
|
|
|
|
if (l != 11)
|
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
2015-02-03 16:20:56 +00:00
|
|
|
} else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
l = BN_mod_word(dh->p, 10);
|
|
|
|
if ((l != 3) && (l != 7))
|
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
|
|
|
} else
|
|
|
|
*ret |= DH_UNABLE_TO_CHECK_GENERATOR;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL))
|
|
|
|
*ret |= DH_CHECK_P_NOT_PRIME;
|
|
|
|
else if (!dh->q) {
|
|
|
|
if (!BN_rshift1(t1, dh->p))
|
|
|
|
goto err;
|
|
|
|
if (!BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL))
|
|
|
|
*ret |= DH_CHECK_P_NOT_SAFE_PRIME;
|
|
|
|
}
|
|
|
|
ok = 1;
|
|
|
|
err:
|
|
|
|
if (ctx != NULL) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
return (ok);
|
|
|
|
}
|
2005-08-21 16:00:17 +00:00
|
|
|
|
|
|
|
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ok = 0;
|
2016-01-18 11:31:58 +00:00
|
|
|
BIGNUM *tmp = NULL;
|
|
|
|
BN_CTX *ctx = NULL;
|
2005-08-21 16:00:17 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
*ret = 0;
|
2016-01-18 11:31:58 +00:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-01-18 11:31:58 +00:00
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
2016-01-29 09:40:03 +00:00
|
|
|
if (tmp == NULL || !BN_set_word(tmp, 1))
|
2016-01-18 11:31:58 +00:00
|
|
|
goto err;
|
|
|
|
if (BN_cmp(pub_key, tmp) <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
*ret |= DH_CHECK_PUBKEY_TOO_SMALL;
|
2016-01-29 09:40:03 +00:00
|
|
|
if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
|
|
|
|
goto err;
|
2016-01-18 11:31:58 +00:00
|
|
|
if (BN_cmp(pub_key, tmp) >= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
*ret |= DH_CHECK_PUBKEY_TOO_LARGE;
|
2005-08-21 16:00:17 +00:00
|
|
|
|
2016-01-18 11:31:58 +00:00
|
|
|
if (dh->q != NULL) {
|
|
|
|
/* Check pub_key^q == 1 mod p */
|
|
|
|
if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
|
|
|
|
goto err;
|
|
|
|
if (!BN_is_one(tmp))
|
|
|
|
*ret |= DH_CHECK_PUBKEY_INVALID;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ok = 1;
|
|
|
|
err:
|
2016-01-18 11:31:58 +00:00
|
|
|
if (ctx != NULL) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
return (ok);
|
|
|
|
}
|