aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/dh
diff options
context:
space:
mode:
Diffstat (limited to 'src/pubkey/dh')
-rw-r--r--src/pubkey/dh/dh.cpp117
-rw-r--r--src/pubkey/dh/dh.h57
-rw-r--r--src/pubkey/dh/dh_core.cpp67
-rw-r--r--src/pubkey/dh/dh_core.h38
-rw-r--r--src/pubkey/dh/info.txt20
5 files changed, 299 insertions, 0 deletions
diff --git a/src/pubkey/dh/dh.cpp b/src/pubkey/dh/dh.cpp
new file mode 100644
index 000000000..8d2059936
--- /dev/null
+++ b/src/pubkey/dh/dh.cpp
@@ -0,0 +1,117 @@
+/*************************************************
+* Diffie-Hellman Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/dh.h>
+#include <botan/numthry.h>
+#include <botan/util.h>
+
+namespace Botan {
+
+/*************************************************
+* DH_PublicKey Constructor *
+*************************************************/
+DH_PublicKey::DH_PublicKey(const DL_Group& grp, const BigInt& y1)
+ {
+ group = grp;
+ y = y1;
+ X509_load_hook();
+ }
+
+/*************************************************
+* Algorithm Specific X.509 Initialization Code *
+*************************************************/
+void DH_PublicKey::X509_load_hook()
+ {
+ }
+
+/*************************************************
+* Return the maximum input size in bits *
+*************************************************/
+u32bit DH_PublicKey::max_input_bits() const
+ {
+ return group_p().bits();
+ }
+
+/*************************************************
+* Return the public value for key agreement *
+*************************************************/
+MemoryVector<byte> DH_PublicKey::public_value() const
+ {
+ return BigInt::encode_1363(y, group_p().bytes());
+ }
+
+/*************************************************
+* Create a DH private key *
+*************************************************/
+DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng,
+ const DL_Group& grp,
+ const BigInt& x_arg)
+ {
+ group = grp;
+ x = x_arg;
+
+ if(x == 0)
+ {
+ const BigInt& p = group_p();
+ x.randomize(rng, 2 * dl_work_factor(p.bits()));
+ PKCS8_load_hook(rng, true);
+ }
+ else
+ PKCS8_load_hook(rng, false);
+ }
+
+/*************************************************
+* Algorithm Specific PKCS #8 Initialization Code *
+*************************************************/
+void DH_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng,
+ bool generated)
+ {
+ if(y == 0)
+ y = power_mod(group_g(), x, group_p());
+ core = DH_Core(rng, group, x);
+
+ if(generated)
+ gen_check(rng);
+ else
+ load_check(rng);
+ }
+
+/*************************************************
+* Return the public value for key agreement *
+*************************************************/
+MemoryVector<byte> DH_PrivateKey::public_value() const
+ {
+ return DH_PublicKey::public_value();
+ }
+
+/*************************************************
+* Derive a key *
+*************************************************/
+SecureVector<byte> DH_PrivateKey::derive_key(const byte w[],
+ u32bit w_len) const
+ {
+ return derive_key(BigInt::decode(w, w_len));
+ }
+
+/*************************************************
+* Derive a key *
+*************************************************/
+SecureVector<byte> DH_PrivateKey::derive_key(const DH_PublicKey& key) const
+ {
+ return derive_key(key.get_y());
+ }
+
+/*************************************************
+* Derive a key *
+*************************************************/
+SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const
+ {
+ const BigInt& p = group_p();
+ if(w <= 1 || w >= p-1)
+ throw Invalid_Argument(algo_name() + "::derive_key: Invalid key input");
+ return BigInt::encode_1363(core.agree(w), p.bytes());
+ }
+
+}
diff --git a/src/pubkey/dh/dh.h b/src/pubkey/dh/dh.h
new file mode 100644
index 000000000..2a567fb70
--- /dev/null
+++ b/src/pubkey/dh/dh.h
@@ -0,0 +1,57 @@
+/*************************************************
+* Diffie-Hellman Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_DIFFIE_HELLMAN_H__
+#define BOTAN_DIFFIE_HELLMAN_H__
+
+#include <botan/dl_algo.h>
+#include <botan/dh_core.h>
+
+namespace Botan {
+
+/*************************************************
+* Diffie-Hellman Public Key *
+*************************************************/
+class BOTAN_DLL DH_PublicKey : public virtual DL_Scheme_PublicKey
+ {
+ public:
+ std::string algo_name() const { return "DH"; }
+
+ MemoryVector<byte> public_value() const;
+ u32bit max_input_bits() const;
+
+ DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; }
+
+ DH_PublicKey() {}
+ DH_PublicKey(const DL_Group&, const BigInt&);
+ private:
+ void X509_load_hook();
+ };
+
+/*************************************************
+* Diffie-Hellman Private Key *
+*************************************************/
+class BOTAN_DLL DH_PrivateKey : public DH_PublicKey,
+ public PK_Key_Agreement_Key,
+ public virtual DL_Scheme_PrivateKey
+ {
+ public:
+ SecureVector<byte> derive_key(const byte[], u32bit) const;
+ SecureVector<byte> derive_key(const DH_PublicKey&) const;
+ SecureVector<byte> derive_key(const BigInt&) const;
+
+ MemoryVector<byte> public_value() const;
+
+ DH_PrivateKey() {}
+ DH_PrivateKey(RandomNumberGenerator&, const DL_Group&,
+ const BigInt& = 0);
+ private:
+ void PKCS8_load_hook(RandomNumberGenerator&, bool = false);
+ DH_Core core;
+ };
+
+}
+
+#endif
diff --git a/src/pubkey/dh/dh_core.cpp b/src/pubkey/dh/dh_core.cpp
new file mode 100644
index 000000000..a0586c444
--- /dev/null
+++ b/src/pubkey/dh/dh_core.cpp
@@ -0,0 +1,67 @@
+/*************************************************
+* PK Algorithm Core Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/dh_core.h>
+#include <botan/numthry.h>
+#include <botan/engine.h>
+#include <botan/parsing.h>
+#include <algorithm>
+
+namespace Botan {
+
+namespace {
+
+const u32bit BLINDING_BITS = BOTAN_PRIVATE_KEY_OP_BLINDING_BITS;
+
+}
+
+/*************************************************
+* DH_Core Constructor *
+*************************************************/
+DH_Core::DH_Core(RandomNumberGenerator& rng,
+ const DL_Group& group, const BigInt& x)
+ {
+ op = Engine_Core::dh_op(group, x);
+
+ const BigInt& p = group.get_p();
+
+ BigInt k(rng, std::min(p.bits()-1, BLINDING_BITS));
+
+ if(k != 0)
+ blinder = Blinder(k, power_mod(inverse_mod(k, p), x, p), p);
+ }
+
+/*************************************************
+* DH_Core Copy Constructor *
+*************************************************/
+DH_Core::DH_Core(const DH_Core& core)
+ {
+ op = 0;
+ if(core.op)
+ op = core.op->clone();
+ blinder = core.blinder;
+ }
+
+/*************************************************
+* DH_Core Assignment Operator *
+*************************************************/
+DH_Core& DH_Core::operator=(const DH_Core& core)
+ {
+ delete op;
+ if(core.op)
+ op = core.op->clone();
+ blinder = core.blinder;
+ return (*this);
+ }
+
+/*************************************************
+* DH Operation *
+*************************************************/
+BigInt DH_Core::agree(const BigInt& i) const
+ {
+ return blinder.unblind(op->agree(blinder.blind(i)));
+ }
+
+}
diff --git a/src/pubkey/dh/dh_core.h b/src/pubkey/dh/dh_core.h
new file mode 100644
index 000000000..3735f31e1
--- /dev/null
+++ b/src/pubkey/dh/dh_core.h
@@ -0,0 +1,38 @@
+/*************************************************
+* DH Core Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_DH_CORE_H__
+#define BOTAN_DH_CORE_H__
+
+#include <botan/bigint.h>
+#include <botan/blinding.h>
+#include <botan/pk_ops.h>
+#include <botan/dl_group.h>
+
+namespace Botan {
+
+/*************************************************
+* DH Core *
+*************************************************/
+class BOTAN_DLL DH_Core
+ {
+ public:
+ BigInt agree(const BigInt&) const;
+
+ DH_Core& operator=(const DH_Core&);
+
+ DH_Core() { op = 0; }
+ DH_Core(const DH_Core&);
+ DH_Core(RandomNumberGenerator& rng,
+ const DL_Group&, const BigInt&);
+ ~DH_Core() { delete op; }
+ private:
+ DH_Operation* op;
+ Blinder blinder;
+ };
+
+}
+
+#endif
diff --git a/src/pubkey/dh/info.txt b/src/pubkey/dh/info.txt
new file mode 100644
index 000000000..3765644c8
--- /dev/null
+++ b/src/pubkey/dh/info.txt
@@ -0,0 +1,20 @@
+realname "DH"
+
+define DH
+
+load_on auto
+
+<add>
+dh.cpp
+dh.h
+dh_core.cpp
+dh_core.h
+</add>
+
+<requires>
+asn1
+bigint
+dl_algo
+numbertheory
+pubkey
+</requires>