2 * Worldvisions Weaver Software:
3 * Copyright (C) 2003 Net Integration Technologies, Inc.
5 * Diffie-Hellman shared secret handshake.
8 #include "wvautoconf.h"
10 # define alloca __builtin_alloca
14 # define alloca _alloca
22 # ifndef alloca /* predefined by HP cc +Olibcalls */
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
);
46 // info->p->dmax = _keylen * 8 / BN_BITS2;
48 // info->p->flags = 0;
51 BN_set_word(info
->g
, generator
);
52 // info->g->d = &generator;
56 // info->g->flags = 0;
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
);
84 // alloca is stack allocated, don't free it.
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
),
96 hexdump_buffer(inbuf
.peek(0, in_len
), in_len
, false));
97 int len
= DH_compute_key (foo
, BN_bin2bn(inbuf
.get(in_len
), in_len
, NULL
),
100 outbuf
.put(foo
, len
);
102 log("Shared secret\n%s\n",hexdump_buffer(outbuf
.peek(0, len
), len
, false));