12 #include "lib/crypt_ops/compat_openssl.h"
20 DISABLE_GCC_WARNING(
"-Wredundant-decls")
22 #include <openssl/dh.h>
24 ENABLE_GCC_WARNING(
"-Wredundant-decls")
26 #include <openssl/bn.h>
55 crypto_validate_dh_params(
const BIGNUM *p,
const BIGNUM *g)
63 #ifdef OPENSSL_1_1_API
65 if (!(dh_p = BN_dup(p)))
67 if (!(dh_g = BN_dup(g)))
69 if (!DH_set0_pqg(dh, dh_p, NULL, dh_g))
72 if (!(dh->p = BN_dup(p)))
74 if (!(dh->g = BN_dup(g)))
80 if (!DH_check(dh, &codes))
82 if (BN_is_word(g, DH_GENERATOR_2)) {
88 BN_ULONG residue = BN_mod_word(p, 24);
89 if (residue == 11 || residue == 23)
90 codes &= ~DH_NOT_SUITABLE_GENERATOR;
112 BIGNUM *result = BN_new();
115 int r = BN_hex2bn(&result, hex);
133 generator = BN_new();
168 #define DH_PRIVATE_KEY_BITS 320
187 tor_assert(dh_type == DH_TYPE_CIRCUIT || dh_type == DH_TYPE_TLS ||
188 dh_type == DH_TYPE_REND);
194 if (dh_type == DH_TYPE_TLS) {
212 if (!(res_dh = DH_new()))
215 BIGNUM *dh_p = NULL, *dh_g = NULL;
226 #ifdef OPENSSL_1_1_API
228 if (!DH_set0_pqg(res_dh, dh_p, NULL, dh_g)) {
246 if (res_dh) DH_free(res_dh);
270 return DH_size(dh->
dh);
279 #ifndef OPENSSL_1_1_API
282 if (!DH_generate_key(dh->
dh)) {
289 #ifdef OPENSSL_1_1_API
294 const BIGNUM *pub_key, *priv_key;
295 DH_get0_key(dh->
dh, &pub_key, &priv_key);
297 log_warn(
LD_CRYPTO,
"Weird! Our own DH key was invalid. I guess once-in-"
298 "the-universe chances really do happen. Treating as a failure.");
305 log_warn(
LD_CRYPTO,
"Weird! Our own DH key was invalid. I guess once-in-"
306 "the-universe chances really do happen. Trying again.");
308 BN_clear_free(dh->
dh->pub_key);
309 BN_clear_free(dh->
dh->priv_key);
310 dh->
dh->pub_key = dh->
dh->priv_key = NULL;
328 const BIGNUM *dh_pub;
330 #ifdef OPENSSL_1_1_API
331 const BIGNUM *dh_priv;
332 DH_get0_key(dh->
dh, &dh_pub, &dh_priv);
334 dh_pub = dh->
dh->pub_key;
341 #ifdef OPENSSL_1_1_API
342 DH_get0_key(dh->
dh, &dh_pub, &dh_priv);
344 dh_pub = dh->
dh->pub_key;
350 bytes = BN_num_bytes(dh_pub);
352 if (pubkey_len < (
size_t)bytes) {
354 "Weird! pubkey_len (%d) was smaller than DH1024_KEY_LEN (%d)",
355 (
int) pubkey_len, bytes);
359 memset(pubkey, 0, pubkey_len);
360 BN_bn2bin(dh_pub, (
unsigned char*)(pubkey+(pubkey_len-bytes)));
380 if (BN_cmp(bn,x)<=0) {
386 if (BN_cmp(bn,x)>=0) {
410 const char *pubkey,
size_t pubkey_len,
411 unsigned char *secret_out,
size_t secret_bytes_out)
413 BIGNUM *pubkey_bn = NULL;
425 if (!(pubkey_bn = BN_bin2bn((
const unsigned char*)pubkey,
426 (
int)pubkey_len, NULL)))
433 result = DH_compute_key(secret_out, pubkey_bn, dh->
dh);
435 log_warn(
LD_CRYPTO,
"DH_compute_key() failed.");
446 BN_clear_free(pubkey_bn);
467 crypto_dh_free_all_openssl(
void)
const char OAKLEY_PRIME_2[]
const char TLS_DH_PRIME[]
const unsigned DH_GENERATOR
#define DH_PRIVATE_KEY_BITS
static BIGNUM * bignum_from_hex(const char *hex)
int crypto_dh_generate_public(crypto_dh_t *dh)
crypto_dh_t * crypto_dh_dup(const crypto_dh_t *dh)
DH * crypto_dh_new_openssl_tls(void)
static int tor_check_dh_key(int severity, const BIGNUM *bn)
void crypto_dh_free_(crypto_dh_t *dh)
int crypto_dh_get_bytes(crypto_dh_t *dh)
static BIGNUM * dh_param_p
void crypto_dh_init_openssl(void)
static BIGNUM * dh_param_p_tls
static BIGNUM * dh_param_g
crypto_dh_t * crypto_dh_new(int dh_type)
static void crypto_set_dh_generator(void)
ssize_t crypto_dh_handshake(int severity, crypto_dh_t *dh, const char *pubkey, size_t pubkey_len, unsigned char *secret_out, size_t secret_bytes_out)
int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey, size_t pubkey_len)
static DH * new_openssl_dh_from_params(BIGNUM *p, BIGNUM *g)
Headers for crypto_digest.c.
Headers for crypto_hkdf.h.
void crypto_openssl_log_errors(int severity, const char *doing)
Common functions for cryptographic routines.
#define log_fn(severity, domain, args,...)
Macros to manage assertions, fatal and non-fatal.