Botan  2.6.0
Crypto and TLS for C++11
elgamal.cpp
Go to the documentation of this file.
1 /*
2 * ElGamal
3 * (C) 1999-2007,2018 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/elgamal.h>
9 #include <botan/internal/pk_ops_impl.h>
10 #include <botan/keypair.h>
11 #include <botan/reducer.h>
12 #include <botan/blinding.h>
13 #include <botan/pow_mod.h>
14 
15 namespace Botan {
16 
17 /*
18 * ElGamal_PublicKey Constructor
19 */
21  DL_Scheme_PublicKey(group, y)
22  {
23  }
24 
25 /*
26 * ElGamal_PrivateKey Constructor
27 */
29  const DL_Group& group,
30  const BigInt& x)
31  {
32  m_x = x;
33  m_group = group;
34 
35  if(m_x.is_zero())
36  {
37  m_x.randomize(rng, group.exponent_bits());
38  }
39 
41  }
42 
44  const secure_vector<uint8_t>& key_bits) :
45  DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
46  {
48  }
49 
50 /*
51 * Check Private ElGamal Parameters
52 */
54  bool strong) const
55  {
56  if(!DL_Scheme_PrivateKey::check_key(rng, strong))
57  return false;
58 
59  if(!strong)
60  return true;
61 
62  return KeyPair::encryption_consistency_check(rng, *this, "EME1(SHA-256)");
63  }
64 
65 namespace {
66 
67 /**
68 * ElGamal encryption operation
69 */
70 class ElGamal_Encryption_Operation final : public PK_Ops::Encryption_with_EME
71  {
72  public:
73 
74  size_t max_raw_input_bits() const override { return m_group.p_bits() - 1; }
75 
76  ElGamal_Encryption_Operation(const ElGamal_PublicKey& key, const std::string& eme);
77 
78  secure_vector<uint8_t> raw_encrypt(const uint8_t msg[], size_t msg_len,
79  RandomNumberGenerator& rng) override;
80 
81  private:
82  const DL_Group m_group;
83  Fixed_Base_Power_Mod m_powermod_y_p;
84  };
85 
86 ElGamal_Encryption_Operation::ElGamal_Encryption_Operation(const ElGamal_PublicKey& key,
87  const std::string& eme) :
88  PK_Ops::Encryption_with_EME(eme),
89  m_group(key.get_group()),
90  m_powermod_y_p(key.get_y(), m_group.get_p())
91  {
92  }
93 
94 secure_vector<uint8_t>
95 ElGamal_Encryption_Operation::raw_encrypt(const uint8_t msg[], size_t msg_len,
96  RandomNumberGenerator& rng)
97  {
98  BigInt m(msg, msg_len);
99 
100  if(m >= m_group.get_p())
101  throw Invalid_Argument("ElGamal encryption: Input is too large");
102 
103  const size_t k_bits = m_group.exponent_bits();
104  const BigInt k(rng, k_bits);
105 
106  const BigInt a = m_group.power_g_p(k);
107  const BigInt b = m_group.multiply_mod_p(m, m_powermod_y_p(k));
108 
109  return BigInt::encode_fixed_length_int_pair(a, b, m_group.p_bytes());
110  }
111 
112 /**
113 * ElGamal decryption operation
114 */
115 class ElGamal_Decryption_Operation final : public PK_Ops::Decryption_with_EME
116  {
117  public:
118 
119  ElGamal_Decryption_Operation(const ElGamal_PrivateKey& key,
120  const std::string& eme,
121  RandomNumberGenerator& rng);
122 
123  secure_vector<uint8_t> raw_decrypt(const uint8_t msg[], size_t msg_len) override;
124  private:
125  const DL_Group m_group;
126  Fixed_Exponent_Power_Mod m_powermod_x_p;
127  Blinder m_blinder;
128  };
129 
130 ElGamal_Decryption_Operation::ElGamal_Decryption_Operation(const ElGamal_PrivateKey& key,
131  const std::string& eme,
132  RandomNumberGenerator& rng) :
133  PK_Ops::Decryption_with_EME(eme),
134  m_group(key.get_group()),
135  m_powermod_x_p(key.get_x(), m_group.get_p()),
136  m_blinder(m_group.get_p(),
137  rng,
138  [](const BigInt& k) { return k; },
139  [this](const BigInt& k) { return m_powermod_x_p(k); })
140  {
141  }
142 
143 secure_vector<uint8_t>
144 ElGamal_Decryption_Operation::raw_decrypt(const uint8_t msg[], size_t msg_len)
145  {
146  const size_t p_bytes = m_group.p_bytes();
147 
148  if(msg_len != 2 * p_bytes)
149  throw Invalid_Argument("ElGamal decryption: Invalid message");
150 
151  BigInt a(msg, p_bytes);
152  const BigInt b(msg + p_bytes, p_bytes);
153 
154  if(a >= m_group.get_p() || b >= m_group.get_p())
155  throw Invalid_Argument("ElGamal decryption: Invalid message");
156 
157  a = m_blinder.blind(a);
158 
159  const BigInt r = m_group.multiply_mod_p(m_group.inverse_mod_p(m_powermod_x_p(a)), b);
160 
161  return BigInt::encode_1363(m_blinder.unblind(r), p_bytes);
162  }
163 
164 }
165 
166 std::unique_ptr<PK_Ops::Encryption>
168  const std::string& params,
169  const std::string& provider) const
170  {
171  if(provider == "base" || provider.empty())
172  return std::unique_ptr<PK_Ops::Encryption>(new ElGamal_Encryption_Operation(*this, params));
173  throw Provider_Not_Found(algo_name(), provider);
174  }
175 
176 std::unique_ptr<PK_Ops::Decryption>
178  const std::string& params,
179  const std::string& provider) const
180  {
181  if(provider == "base" || provider.empty())
182  return std::unique_ptr<PK_Ops::Decryption>(new ElGamal_Decryption_Operation(*this, params, rng));
183  throw Provider_Not_Found(algo_name(), provider);
184  }
185 
186 }
size_t p_bytes() const
Definition: dl_group.cpp:411
BigInt multiply_mod_p(const BigInt &x, const BigInt &y) const
Definition: dl_group.cpp:437
ElGamal_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: elgamal.cpp:43
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Definition: big_rand.cpp:17
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: elgamal.cpp:53
BigInt power_g_p(const BigInt &x) const
Definition: dl_group.cpp:447
bool is_zero() const
Definition: bigint.h:314
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:19
BigInt unblind(const BigInt &x) const
Definition: blinding.cpp:58
const BigInt & get_p() const
Definition: dl_group.cpp:380
size_t exponent_bits() const
Definition: dl_group.cpp:421
std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: elgamal.cpp:167
std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: elgamal.cpp:177
Definition: alg_id.cpp:13
BigInt blind(const BigInt &x) const
Definition: blinding.cpp:35
Blinder m_blinder
Definition: rsa.cpp:264
BigInt inverse_mod_p(const BigInt &x) const
Definition: dl_group.cpp:426
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: dl_algo.cpp:76
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
static secure_vector< uint8_t > encode_fixed_length_int_pair(const BigInt &n1, const BigInt &n2, size_t bytes)
Definition: big_code.cpp:103
std::string algo_name() const override
Definition: elgamal.h:21