Botan 1.10.17

elgamal.cpp
Go to the documentation of this file.
1 /*
2 * ElGamal
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/elgamal.h>
9 #include <botan/numthry.h>
10 #include <botan/libstate.h>
11 #include <botan/keypair.h>
12 #include <botan/internal/workfactor.h>
13
14 namespace Botan {
15
16 /*
17 * ElGamal_PublicKey Constructor
18 */
19 ElGamal_PublicKey::ElGamal_PublicKey(const DL_Group& grp, const BigInt& y1)
20 {
21 group = grp;
22 y = y1;
23 }
24
25 /*
26 * ElGamal_PrivateKey Constructor
27 */
28 ElGamal_PrivateKey::ElGamal_PrivateKey(RandomNumberGenerator& rng,
29 const DL_Group& grp,
30 const BigInt& x_arg)
31 {
32 group = grp;
33 x = x_arg;
34
35 if(x == 0)
36 x.randomize(rng, 2 * dl_work_factor(group_p().bits()));
37
38 y = power_mod(group_g(), x, group_p());
39
40 if(x_arg == 0)
41 gen_check(rng);
42 else
43 load_check(rng);
44 }
45
46 ElGamal_PrivateKey::ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id,
47 const MemoryRegion<byte>& key_bits,
48 RandomNumberGenerator& rng) :
49 DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
50 {
51 y = power_mod(group_g(), x, group_p());
52 load_check(rng);
53 }
54
55 /*
56 * Check Private ElGamal Parameters
57 */
58 bool ElGamal_PrivateKey::check_key(RandomNumberGenerator& rng,
59 bool strong) const
60 {
61 if(!DL_Scheme_PrivateKey::check_key(rng, strong))
62 return false;
63
64 if(!strong)
65 return true;
66
67 return KeyPair::encryption_consistency_check(rng, *this, "EME1(SHA-1)");
68 }
69
70 ElGamal_Encryption_Operation::ElGamal_Encryption_Operation(const ElGamal_PublicKey& key)
71 {
72 const BigInt& p = key.group_p();
73
74 powermod_g_p = Fixed_Base_Power_Mod(key.group_g(), p);
75 powermod_y_p = Fixed_Base_Power_Mod(key.get_y(), p);
76 mod_p = Modular_Reducer(p);
77 }
78
79 SecureVector<byte>
80 ElGamal_Encryption_Operation::encrypt(const byte msg[], size_t msg_len,
81 RandomNumberGenerator& rng)
82 {
83 const BigInt& p = mod_p.get_modulus();
84
85 BigInt m(msg, msg_len);
86
87 if(m >= p)
88 throw Invalid_Argument("ElGamal encryption: Input is too large");
89
90 BigInt k(rng, 2 * dl_work_factor(p.bits()));
91
92 BigInt a = powermod_g_p(k);
93 BigInt b = mod_p.multiply(m, powermod_y_p(k));
94
95 SecureVector<byte> output(2*p.bytes());
96 a.binary_encode(&output[p.bytes() - a.bytes()]);
97 b.binary_encode(&output[output.size() / 2 + (p.bytes() - b.bytes())]);
98 return output;
99 }
100
101 ElGamal_Decryption_Operation::ElGamal_Decryption_Operation(const ElGamal_PrivateKey& key)
102 {
103 const BigInt& p = key.group_p();
104
105 powermod_x_p = Fixed_Exponent_Power_Mod(key.get_x(), p);
106 mod_p = Modular_Reducer(p);
107
108 BigInt k(global_state().global_rng(), p.bits() - 1);
109 blinder = Blinder(k, powermod_x_p(k), p);
110 }
111
112 SecureVector<byte>
113 ElGamal_Decryption_Operation::decrypt(const byte msg[], size_t msg_len)
114 {
115 const BigInt& p = mod_p.get_modulus();
116
117 const size_t p_bytes = p.bytes();
118
119 if(msg_len != 2 * p_bytes)
120 throw Invalid_Argument("ElGamal decryption: Invalid message");
121
122 BigInt a(msg, p_bytes);
123 BigInt b(msg + p_bytes, p_bytes);
124
125 if(a >= p || b >= p)
126 throw Invalid_Argument("ElGamal decryption: Invalid message");
127
128 a = blinder.blind(a);
129
130 BigInt r = mod_p.multiply(b, inverse_mod(powermod_x_p(a), p));
131
132 return BigInt::encode(blinder.unblind(r));
133 }
134
135 }
Botan::DL_Scheme_PrivateKey
Definition: dl_algo.h:85
Botan::ElGamal_Encryption_Operation::encrypt
SecureVector< byte > encrypt(const byte msg[], size_t msg_len, RandomNumberGenerator &rng)
Definition: elgamal.cpp:80
Botan::Fixed_Base_Power_Mod
Definition: pow_mod.h:87
Botan::BigInt
Definition: bigint.h:22
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::DL_Scheme_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: dl_algo.cpp:67
Botan::ElGamal_PublicKey
Definition: elgamal.h:22
Botan::DL_Scheme_PublicKey::get_y
const BigInt & get_y() const
Definition: dl_algo.h:38
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::ElGamal_PrivateKey
Definition: elgamal.h:43
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::ElGamal_Decryption_Operation::ElGamal_Decryption_Operation
ElGamal_Decryption_Operation(const ElGamal_PrivateKey &key)
Definition: elgamal.cpp:101
Botan::ElGamal_PublicKey::ElGamal_PublicKey
ElGamal_PublicKey()
Definition: elgamal.h:37
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::ElGamal_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: elgamal.cpp:58
Botan::Blinder::unblind
BigInt unblind(const BigInt &x) const
Definition: blinding.cpp:42
Botan::DL_Group
Definition: dl_group.h:20
Botan::BigInt::randomize
void randomize(RandomNumberGenerator &rng, size_t bitsize=0)
Definition: big_rand.cpp:29
Botan::ElGamal_PrivateKey::ElGamal_PrivateKey
ElGamal_PrivateKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits, RandomNumberGenerator &rng)
Definition: elgamal.cpp:46
Botan::Private_Key::gen_check
void gen_check(RandomNumberGenerator &rng) const
Definition: pk_keys.cpp:49
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::BigInt::binary_encode
void binary_encode(byte buf[]) const
Definition: bigint.cpp:341
Botan::KeyPair::encryption_consistency_check
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.cpp:18
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:246
Botan::Private_Key::load_check
void load_check(RandomNumberGenerator &rng) const
Definition: pk_keys.cpp:40
Botan::ElGamal_Encryption_Operation::ElGamal_Encryption_Operation
ElGamal_Encryption_Operation(const ElGamal_PublicKey &key)
Definition: elgamal.cpp:70
Botan::Blinder
Definition: blinding.h:19
Botan::Modular_Reducer::get_modulus
const BigInt & get_modulus() const
Definition: reducer.h:21
Botan::Blinder::blind
BigInt blind(const BigInt &x) const
Definition: blinding.cpp:29
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:314
Botan::DL_Scheme_PublicKey::group_p
const BigInt & group_p() const
Definition: dl_algo.h:44
Botan::power_mod
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:366
Botan::DL_Scheme_PrivateKey::x
BigInt x
Definition: dl_algo.h:109
Botan::DL_Scheme_PublicKey::group
DL_Group group
Definition: dl_algo.h:79
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::Fixed_Exponent_Power_Mod
Definition: pow_mod.h:73
Botan::SecureVector< byte >
Botan::dl_work_factor
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:14
Botan::DL_Scheme_PrivateKey::get_x
const BigInt & get_x() const
Definition: dl_algo.h:95
Botan::DL_Scheme_PublicKey::group_g
const BigInt & group_g() const
Definition: dl_algo.h:56
Botan::Modular_Reducer
Definition: reducer.h:18
Botan::Modular_Reducer::multiply
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31
Botan::ElGamal_Decryption_Operation::decrypt
SecureVector< byte > decrypt(const byte msg[], size_t msg_len)
Definition: elgamal.cpp:113
Botan::DL_Scheme_PublicKey::y
BigInt y
Definition: dl_algo.h:74