12#include "lib/crypt_ops/compat_openssl.h"
20DISABLE_GCC_WARNING(
"-Wredundant-decls")
22#include <openssl/dh.h>
24ENABLE_GCC_WARNING(
"-Wredundant-decls")
26#include <openssl/bn.h>
56crypto_validate_dh_params(
const BIGNUM *p,
const BIGNUM *g)
66 if (!(dh_p = BN_dup(p)))
68 if (!(dh_g = BN_dup(g)))
70 if (!DH_set0_pqg(dh, dh_p, NULL, dh_g))
75 if (!DH_check(dh, &codes))
77 if (BN_is_word(g, DH_GENERATOR_2)) {
83 BN_ULONG residue = BN_mod_word(p, 24);
84 if (residue == 11 || residue == 23)
85 codes &= ~DH_NOT_SUITABLE_GENERATOR;
107 BIGNUM *result = BN_new();
110 int r = BN_hex2bn(&result, hex);
128 generator = BN_new();
163#define DH_PRIVATE_KEY_BITS 320
182 tor_assert(dh_type == DH_TYPE_CIRCUIT || dh_type == DH_TYPE_TLS ||
183 dh_type == DH_TYPE_REND);
189 if (dh_type == DH_TYPE_TLS) {
207 if (!(res_dh = DH_new()))
210 BIGNUM *dh_p = NULL, *dh_g = NULL;
221 if (!DH_set0_pqg(res_dh, dh_p, NULL, dh_g)) {
234 if (res_dh) DH_free(res_dh);
258 return DH_size(dh->
dh);
267 if (!DH_generate_key(dh->
dh)) {
279 const BIGNUM *pub_key, *priv_key;
280 DH_get0_key(dh->
dh, &pub_key, &priv_key);
282 log_warn(
LD_CRYPTO,
"Weird! Our own DH key was invalid. I guess once-in-"
283 "the-universe chances really do happen. Treating as a failure.");
300 const BIGNUM *dh_pub;
302 const BIGNUM *dh_priv;
303 DH_get0_key(dh->
dh, &dh_pub, &dh_priv);
309 DH_get0_key(dh->
dh, &dh_pub, &dh_priv);
314 bytes = BN_num_bytes(dh_pub);
316 if (pubkey_len < (
size_t)bytes) {
318 "Weird! pubkey_len (%d) was smaller than key length (%d)",
319 (
int) pubkey_len, bytes);
323 memset(pubkey, 0, pubkey_len);
324 BN_bn2bin(dh_pub, (
unsigned char*)(pubkey+(pubkey_len-bytes)));
342 if (BN_cmp(bn,x)<=0) {
348 if (BN_cmp(bn,x)>=0) {
372 const char *pubkey,
size_t pubkey_len,
373 unsigned char *secret_out,
size_t secret_bytes_out)
375 BIGNUM *pubkey_bn = NULL;
387 if (!(pubkey_bn = BN_bin2bn((
const unsigned char*)pubkey,
388 (
int)pubkey_len, NULL)))
395 result = DH_compute_key(secret_out, pubkey_bn, dh->
dh);
397 log_warn(
LD_CRYPTO,
"DH_compute_key() failed.");
408 BN_clear_free(pubkey_bn);
429crypto_dh_free_all_openssl(
void)
const char OAKLEY_PRIME_2[]
const char TLS_DH_PRIME[]
const unsigned DH_GENERATOR
#define DH_PRIVATE_KEY_BITS
int crypto_dh_generate_public(crypto_dh_t *dh)
void crypto_dh_free_(crypto_dh_t *dh)
crypto_dh_t * crypto_dh_new(int dh_type)
DH * crypto_dh_new_openssl_tls(void)
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
static BIGNUM * bignum_from_hex(const char *hex)
static void crypto_set_dh_generator(void)
static int tor_check_dh_key(int severity, const BIGNUM *bn, const BIGNUM *dh_p)
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)
crypto_dh_t * crypto_dh_dup(const crypto_dh_t *dh)
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.