8 #include "wvautoconf.h"
10 # define alloca __builtin_alloca
14 # define alloca _alloca
30 #include <openssl/bn.h>
33 #include "wvdiffiehellman.h"
36 WvDiffieHellman::WvDiffieHellman(
const unsigned char *_key,
int _keylen,
37 BN_ULONG _generator) :
38 generator(_generator), log(
"Diffie-Hellman",
WvLog::Debug)
44 info->p = BN_bin2bn(_key, _keylen, NULL);
51 BN_set_word(info->g, generator);
59 check = BN_mod_word(info->p, 24);
60 DH_check(info, &problems);
61 if (problems & DH_CHECK_P_NOT_PRIME)
62 log(WvLog::Error,
"Using a composite number for authentication.\n");
63 if (problems & DH_CHECK_P_NOT_SAFE_PRIME)
64 log(WvLog::Error,
"Using an unsafe prime number for authentication.\n");
65 if (problems & DH_NOT_SUITABLE_GENERATOR)
66 log(WvLog::Error,
"Can you just use 2 instead of %s (%s)!!\n",
67 BN_bn2hex(info->g), check);
68 if (problems & DH_UNABLE_TO_CHECK_GENERATOR)
69 log(WvLog::Notice,
"Using a strange argument for diffie-hellman.\n");
70 DH_generate_key(info);
73 int WvDiffieHellman::pub_key_len()
75 return BN_num_bytes(info->pub_key);
78 int WvDiffieHellman::get_public_value(
WvBuf &outbuf,
int len)
80 int key_len = BN_num_bytes(info->pub_key);
85 unsigned char *foo = (
unsigned char*)alloca(key_len);
86 BN_bn2bin(info->pub_key, foo);
92 bool WvDiffieHellman::create_secret(
WvBuf &inbuf,
size_t in_len,
WvBuf& outbuf)
94 unsigned char *foo = (
unsigned char *)alloca(DH_size(info));
95 log(
"My public value\n%s\nYour public value\n%s\n",BN_bn2hex(info->pub_key),
97 int len = DH_compute_key (foo, BN_bin2bn(inbuf.
get(in_len), in_len, NULL),
100 outbuf.put(foo, len);
WvString hexdump_buffer(const void *buf, size_t len, bool charRep=true)
Produce a hexadecimal dump of the data buffer in 'buf' of length 'len'.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers...
const T * peek(int offset, size_t count)
Returns a const pointer into the buffer at the specified offset to the specified number of elements w...
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's...