10# define alloca __builtin_alloca
14# define alloca _alloca
30#include <openssl/bn.h>
33#include "wvdiffiehellman.h"
36WvDiffieHellman::WvDiffieHellman(
const unsigned char *_key,
int _keylen,
37 BN_ULONG _generator) :
38 generator(_generator), log(
"Diffie-Hellman",
WvLog::Debug)
44 BIGNUM *p = BN_bin2bn(_key, _keylen, NULL);
51 BN_set_word(g, generator);
58 DH_set0_pqg(info, p, NULL, g);
60 check = BN_mod_word(p, 24);
61 DH_check(info, &problems);
62 if (problems & DH_CHECK_P_NOT_PRIME)
63 log(WvLog::Error,
"Using a composite number for authentication.\n");
64 if (problems & DH_CHECK_P_NOT_SAFE_PRIME)
65 log(WvLog::Error,
"Using an unsafe prime number for authentication.\n");
66 if (problems & DH_NOT_SUITABLE_GENERATOR)
67 log(WvLog::Error,
"Can you just use 2 instead of %s (%s)!!\n",
69 if (problems & DH_UNABLE_TO_CHECK_GENERATOR)
70 log(WvLog::Notice,
"Using a strange argument for diffie-hellman.\n");
71 DH_generate_key(info);
74int WvDiffieHellman::pub_key_len()
76 const BIGNUM *pub_key = NULL;
77 DH_get0_key(info, &pub_key, NULL);
78 return BN_num_bytes(pub_key);
81int WvDiffieHellman::get_public_value(
WvBuf &outbuf,
int len)
83 const BIGNUM *pub_key = NULL;
84 DH_get0_key(info, &pub_key, NULL);
86 int key_len = BN_num_bytes(pub_key);
91 unsigned char *foo = (
unsigned char*)alloca(key_len);
92 BN_bn2bin(pub_key, foo);
98bool WvDiffieHellman::create_secret(
WvBuf &inbuf,
size_t in_len,
WvBuf& outbuf)
100 const BIGNUM *pub_key = NULL;
101 DH_get0_key(info, &pub_key, NULL);
102 unsigned char *foo = (
unsigned char *)alloca(DH_size(info));
103 log(
"My public value\n%s\nYour public value\n%s\n",BN_bn2hex(pub_key),
105 int len = DH_compute_key (foo, BN_bin2bn(inbuf.
get(in_len), in_len, NULL),
108 outbuf.put(foo, len);
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
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...
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
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'.