Fix coding style and building with !TR_LIGHTWEIGHT

This commit is contained in:
Mike Gelfand 2016-09-07 01:09:04 +03:00
parent f91cf5ad8c
commit 8c8386a7f3
1 changed files with 32 additions and 26 deletions

View File

@ -14,6 +14,7 @@
#include <assert.h> #include <assert.h>
#include <openssl/bn.h> #include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/dh.h> #include <openssl/dh.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/evp.h> #include <openssl/evp.h>
@ -48,7 +49,12 @@ log_openssl_error (const char * file,
static bool strings_loaded = false; static bool strings_loaded = false;
if (!strings_loaded) if (!strings_loaded)
{ {
#if OPENSSL_VERSION_NUMBER < 0x10100000
ERR_load_crypto_strings (); ERR_load_crypto_strings ();
#else
OPENSSL_init_crypto (OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
#endif
strings_loaded = true; strings_loaded = true;
} }
#endif #endif
@ -230,6 +236,7 @@ tr_rc4_process (tr_rc4_ctx_t handle,
***/ ***/
#if OPENSSL_VERSION_NUMBER < 0x10100000 #if OPENSSL_VERSION_NUMBER < 0x10100000
static inline int static inline int
DH_set0_pqg (DH * dh, DH_set0_pqg (DH * dh,
BIGNUM * p, BIGNUM * p,
@ -237,28 +244,29 @@ DH_set0_pqg (DH * dh,
BIGNUM * g) BIGNUM * g)
{ {
/* If the fields p and g in d are NULL, the corresponding input /* If the fields p and g in d are NULL, the corresponding input
* parameters MUST be non-NULL. q may remain NULL. * parameters MUST be non-NULL. q may remain NULL.
*/ */
if ((dh->p == NULL && p == NULL) if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
|| (dh->g == NULL && g == NULL))
return 0; return 0;
if (p != NULL) { if (p != NULL)
BN_free (dh->p); {
dh->p = p; BN_free (dh->p);
} dh->p = p;
if (q != NULL) { }
BN_free (dh->q); if (q != NULL)
dh->q = q; {
} BN_free (dh->q);
if (g != NULL) { dh->q = q;
BN_free (dh->g); }
dh->g = g; if (g != NULL)
} {
BN_free (dh->g);
dh->g = g;
}
if (q != NULL) { if (q != NULL)
dh->length = BN_num_bits (q); dh->length = BN_num_bits (q);
}
return 1; return 1;
} }
@ -267,8 +275,8 @@ static inline int
DH_set_length (DH * dh, DH_set_length (DH * dh,
long length) long length)
{ {
dh->length = length; dh->length = length;
return 1; return 1;
} }
static inline void static inline void
@ -295,12 +303,11 @@ tr_dh_new (const uint8_t * prime_num,
assert (prime_num != NULL); assert (prime_num != NULL);
assert (generator_num != NULL); assert (generator_num != NULL);
p = BN_bin2bn (prime_num, prime_num_length, NULL); p = BN_bin2bn (prime_num, prime_num_length, NULL);
g = BN_bin2bn (generator_num, generator_num_length, NULL); g = BN_bin2bn (generator_num, generator_num_length, NULL);
if (!check_pointer (p) || if (!check_pointer (p) || !check_pointer (g) || !DH_set0_pqg (handle, p, NULL, g))
!check_pointer (g) ||
!DH_set0_pqg (handle, p, NULL, g))
{ {
BN_free (p); BN_free (p);
BN_free (g); BN_free (g);
@ -328,20 +335,19 @@ tr_dh_make_key (tr_dh_ctx_t raw_handle,
{ {
DH * handle = raw_handle; DH * handle = raw_handle;
int dh_size, my_public_key_length; int dh_size, my_public_key_length;
const BIGNUM * hand_pub_key; const BIGNUM * my_public_key;
assert (handle != NULL); assert (handle != NULL);
assert (public_key != NULL); assert (public_key != NULL);
DH_set_length(handle, private_key_length * 8); DH_set_length(handle, private_key_length * 8);
if (!check_result (DH_generate_key (handle))) if (!check_result (DH_generate_key (handle)))
return false; return false;
DH_get0_key (handle, &hand_pub_key, NULL); DH_get0_key (handle, &my_public_key, NULL);
my_public_key_length = BN_bn2bin (hand_pub_key, public_key); my_public_key_length = BN_bn2bin (my_public_key, public_key);
dh_size = DH_size (handle); dh_size = DH_size (handle);
tr_dh_align_key (public_key, my_public_key_length, dh_size); tr_dh_align_key (public_key, my_public_key_length, dh_size);