1
/*************************************************
2
* Diffie-Hellman Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
7
#include <botan/numthry.h>
8
#include <botan/util.h>
12
/*************************************************
13
* DH_PublicKey Constructor *
14
*************************************************/
15
DH_PublicKey::DH_PublicKey(const DL_Group& grp, const BigInt& y1)
22
/*************************************************
23
* Algorithm Specific X.509 Initialization Code *
24
*************************************************/
25
void DH_PublicKey::X509_load_hook()
30
/*************************************************
31
* Return the maximum input size in bits *
32
*************************************************/
33
u32bit DH_PublicKey::max_input_bits() const
35
return group_p().bits();
38
/*************************************************
39
* Return the public value for key agreement *
40
*************************************************/
41
MemoryVector<byte> DH_PublicKey::public_value() const
43
return BigInt::encode_1363(y, group_p().bytes());
46
/*************************************************
47
* Create a DH private key *
48
*************************************************/
49
DH_PrivateKey::DH_PrivateKey(const DL_Group& grp)
53
const BigInt& p = group_p();
54
x = random_integer(2 * dl_work_factor(p.bits()));
56
PKCS8_load_hook(true);
59
/*************************************************
60
* DH_PrivateKey Constructor *
61
*************************************************/
62
DH_PrivateKey::DH_PrivateKey(const DL_Group& grp, const BigInt& x1,
72
/*************************************************
73
* Algorithm Specific PKCS #8 Initialization Code *
74
*************************************************/
75
void DH_PrivateKey::PKCS8_load_hook(bool generated)
78
y = power_mod(group_g(), x, group_p());
79
core = DH_Core(group, x);
87
/*************************************************
88
* Return the public value for key agreement *
89
*************************************************/
90
MemoryVector<byte> DH_PrivateKey::public_value() const
92
return DH_PublicKey::public_value();
95
/*************************************************
97
*************************************************/
98
SecureVector<byte> DH_PrivateKey::derive_key(const byte w[],
101
return derive_key(BigInt::decode(w, w_len));
104
/*************************************************
106
*************************************************/
107
SecureVector<byte> DH_PrivateKey::derive_key(const DH_PublicKey& key) const
109
return derive_key(key.get_y());
112
/*************************************************
114
*************************************************/
115
SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const
117
const BigInt& p = group_p();
118
if(w <= 1 || w >= p-1)
119
throw Invalid_Argument(algo_name() + "::derive_key: Invalid key input");
120
return BigInt::encode_1363(core.agree(w), p.bytes());