Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

dh.cpp
Go to the documentation of this file.
1 /*
2 * Diffie-Hellman
3 * (C) 1999-2007,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/dh.h>
9 #include <botan/internal/pk_ops_impl.h>
10 #include <botan/workfactor.h>
11 #include <botan/pow_mod.h>
12 #include <botan/blinding.h>
13
14 namespace Botan {
15
16 /*
17 * DH_PublicKey Constructor
18 */
19 DH_PublicKey::DH_PublicKey(const DL_Group& grp, const BigInt& y1)
20 {
21 m_group = grp;
22 m_y = y1;
23 }
24
25 /*
26 * Return the public value for key agreement
27 */
28 std::vector<uint8_t> DH_PublicKey::public_value() const
29 {
30 return unlock(BigInt::encode_1363(m_y, group_p().bytes()));
31 }
32
33 /*
34 * Create a DH private key
35 */
36 DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng,
37 const DL_Group& grp,
38 const BigInt& x_arg)
39 {
40 m_group = grp;
41
42 if(x_arg == 0)
43 {
44 const BigInt& p = group_p();
45 m_x.randomize(rng, dl_exponent_size(p.bits()));
46 }
47 else
48 {
49 m_x = x_arg;
50 }
51
52 if(m_y == 0)
53 {
54 m_y = power_mod(group_g(), m_x, group_p());
55 }
56 }
57
58 /*
59 * Load a DH private key
60 */
61 DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id,
62 const secure_vector<uint8_t>& key_bits) :
63 DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
64 {
65 if(m_y == 0)
66 m_y = power_mod(group_g(), m_x, group_p());
67 }
68
69 /*
70 * Return the public value for key agreement
71 */
72 std::vector<uint8_t> DH_PrivateKey::public_value() const
73 {
74 return DH_PublicKey::public_value();
75 }
76
77 namespace {
78
79 /**
80 * DH operation
81 */
82 class DH_KA_Operation : public PK_Ops::Key_Agreement_with_KDF
83 {
84 public:
85
86 DH_KA_Operation(const DH_PrivateKey& key, const std::string& kdf, RandomNumberGenerator& rng) :
87 PK_Ops::Key_Agreement_with_KDF(kdf),
88 m_p(key.group_p()),
89 m_powermod_x_p(key.get_x(), m_p),
90 m_blinder(m_p,
91 rng,
92 [](const BigInt& k) { return k; },
93 [this](const BigInt& k) { return m_powermod_x_p(inverse_mod(k, m_p)); })
94 {}
95
96 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override;
97 private:
98 const BigInt& m_p;
99
100 Fixed_Exponent_Power_Mod m_powermod_x_p;
101 Blinder m_blinder;
102 };
103
104 secure_vector<uint8_t> DH_KA_Operation::raw_agree(const uint8_t w[], size_t w_len)
105 {
106 BigInt input = BigInt::decode(w, w_len);
107
108 if(input <= 1 || input >= m_p - 1)
109 throw Invalid_Argument("DH agreement - invalid key provided");
110
111 BigInt r = m_blinder.unblind(m_powermod_x_p(m_blinder.blind(input)));
112
113 return BigInt::encode_1363(r, m_p.bytes());
114 }
115
116 }
117
118 std::unique_ptr<PK_Ops::Key_Agreement>
119 DH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
120 const std::string& params,
121 const std::string& provider) const
122 {
123 if(provider == "base" || provider.empty())
124 return std::unique_ptr<PK_Ops::Key_Agreement>(new DH_KA_Operation(*this, params, rng));
125 throw Provider_Not_Found(algo_name(), provider);
126 }
127
128 }
Botan::DL_Scheme_PrivateKey
Definition: dl_algo.h:95
Botan::DL_Scheme_PrivateKey::m_x
BigInt m_x
Definition: dl_algo.h:127
Botan::BigInt
Definition: bigint.h:23
Botan::dl_exponent_size
size_t dl_exponent_size(size_t bits)
Definition: workfactor.cpp:40
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::BigInt::randomize
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Definition: big_rand.cpp:17
Botan::DL_Scheme_PublicKey::group_p
const BigInt & group_p() const
Definition: dl_algo.h:43
Botan::DH_PrivateKey::DH_PrivateKey
DH_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: dh.cpp:61
m_blinder
Blinder m_blinder
Definition: dh.cpp:101
Botan::PK_Ops::Key_Agreement_with_KDF
Definition: pk_ops_impl.h:170
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:184
Botan::DH_PublicKey::DH_PublicKey
DH_PublicKey()=default
Botan::DL_Group
Definition: dl_group.h:20
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
m_p
const BigInt & m_p
Definition: dh.cpp:98
Botan::DL_Scheme_PublicKey::m_group
DL_Group m_group
Definition: dl_algo.h:89
Botan::DL_Scheme_PublicKey::m_y
BigInt m_y
Definition: dl_algo.h:84
Botan::Blinder::unblind
BigInt unblind(const BigInt &x) const
Definition: blinding.cpp:59
Botan
Definition: alg_id.cpp:13
Botan::Provider_Not_Found
Definition: exceptn.h:161
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125
Botan::DH_PrivateKey::create_key_agreement_op
std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: dh.cpp:119
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:276
Botan::DH_PublicKey::public_value
std::vector< uint8_t > public_value() const
Definition: dh.cpp:28
Botan::Blinder::blind
BigInt blind(const BigInt &x) const
Definition: blinding.cpp:36
Botan::power_mod
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:373
m_powermod_x_p
Fixed_Exponent_Power_Mod m_powermod_x_p
Definition: dh.cpp:100
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::BigInt::encode_1363
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
Botan::DL_Scheme_PublicKey::group_g
const BigInt & group_g() const
Definition: dl_algo.h:55
Botan::DH_PrivateKey
Definition: dh.h:49
Botan::DH_PrivateKey::public_value
std::vector< uint8_t > public_value() const override
Definition: dh.cpp:72
Botan::BigInt::decode
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
Botan::DH_PublicKey::algo_name
std::string algo_name() const override
Definition: dh.h:21
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:176