aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/elgamal/elg_op.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-10-01 15:17:52 +0000
committerlloyd <[email protected]>2008-10-01 15:17:52 +0000
commit92ddf6f0f8f0ef6f5584889481e4a098e280ee40 (patch)
tree4178ad93250da66d0eb18f9dfcce221bbb57bcd7 /src/pubkey/elgamal/elg_op.cpp
parent1034cf44b4ee0948312c11a1b079b8b04c5828e2 (diff)
Move last pieces of algorithm-specific code from general 'pubkey' module
into algorithm-specific directories. (Dependencies still remain on these in core/libstate, though).
Diffstat (limited to 'src/pubkey/elgamal/elg_op.cpp')
-rw-r--r--src/pubkey/elgamal/elg_op.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/pubkey/elgamal/elg_op.cpp b/src/pubkey/elgamal/elg_op.cpp
new file mode 100644
index 000000000..3f276de09
--- /dev/null
+++ b/src/pubkey/elgamal/elg_op.cpp
@@ -0,0 +1,64 @@
+/*************************************************
+* ElGamal Operations Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/elg_op.h>
+#include <botan/eng_def.h>
+
+namespace Botan {
+
+/*************************************************
+* Default_ELG_Op Constructor *
+*************************************************/
+Default_ELG_Op::Default_ELG_Op(const DL_Group& group, const BigInt& y,
+ const BigInt& x) : p(group.get_p())
+ {
+ powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), p);
+ powermod_y_p = Fixed_Base_Power_Mod(y, p);
+ mod_p = Modular_Reducer(p);
+
+ if(x != 0)
+ powermod_x_p = Fixed_Exponent_Power_Mod(x, p);
+ }
+
+/*************************************************
+* Default ElGamal Encrypt Operation *
+*************************************************/
+SecureVector<byte> Default_ELG_Op::encrypt(const byte in[], u32bit length,
+ const BigInt& k) const
+ {
+ BigInt m(in, length);
+ if(m >= p)
+ throw Invalid_Argument("Default_ELG_Op::encrypt: Input is too large");
+
+ BigInt a = powermod_g_p(k);
+ BigInt b = mod_p.multiply(m, powermod_y_p(k));
+
+ SecureVector<byte> output(2*p.bytes());
+ a.binary_encode(output + (p.bytes() - a.bytes()));
+ b.binary_encode(output + output.size() / 2 + (p.bytes() - b.bytes()));
+ return output;
+ }
+
+/*************************************************
+* Default ElGamal Decrypt Operation *
+*************************************************/
+BigInt Default_ELG_Op::decrypt(const BigInt& a, const BigInt& b) const
+ {
+ if(a >= p || b >= p)
+ throw Invalid_Argument("Default_ELG_Op: Invalid message");
+
+ return mod_p.multiply(b, inverse_mod(powermod_x_p(a), p));
+ }
+
+/*************************************************
+* Acquire an ElGamal op *
+*************************************************/
+ELG_Operation* Default_Engine::elg_op(const DL_Group& group, const BigInt& y,
+ const BigInt& x) const
+ {
+ return new Default_ELG_Op(group, y, x);
+ }
+
+}