Botan  2.4.0
Crypto and TLS for C++11
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 */
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 */
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 */
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  {
75  }
76 
77 namespace {
78 
79 /**
80 * DH operation
81 */
82 class DH_KA_Operation final : 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) :
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;
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>
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 }
std::vector< uint8_t > public_value() const
Definition: dh.cpp:28
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:374
size_t bits() const
Definition: bigint.cpp:183
DH_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: dh.cpp:61
DH_PublicKey()=default
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:277
Definition: alg_id.cpp:13
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95
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
const BigInt & group_p() const
Definition: dl_algo.h:43
Blinder m_blinder
Definition: rsa.cpp:239
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
size_t dl_exponent_size(size_t bits)
Definition: workfactor.cpp:40
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
const BigInt & get_x() const
Definition: dl_algo.h:105
const BigInt & group_g() const
Definition: dl_algo.h:55
std::vector< uint8_t > public_value() const override
Definition: dh.cpp:72
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
std::string algo_name() const override
Definition: dh.h:21