diff options
author | David Clark <david.a.clark@nokia.com> | 2010-11-18 16:20:48 +1000 |
---|---|---|
committer | David Clark <david.a.clark@nokia.com> | 2010-11-18 16:20:48 +1000 |
commit | c223232bc15106750da632598047a35ad3762723 (patch) | |
tree | 403f7aa2c3a5a912edce6feae869046c89d29178 /old/botan/src/pubkey/dh | |
parent | b984b0b62076067f1f75db5a7eda5aaa2cdaad2a (diff) |
Diffstat (limited to 'old/botan/src/pubkey/dh')
-rw-r--r-- | old/botan/src/pubkey/dh/dh.cpp | 119 | ||||
-rw-r--r-- | old/botan/src/pubkey/dh/dh.h | 80 | ||||
-rw-r--r-- | old/botan/src/pubkey/dh/dh_core.cpp | 69 | ||||
-rw-r--r-- | old/botan/src/pubkey/dh/dh_core.h | 38 | ||||
-rw-r--r-- | old/botan/src/pubkey/dh/dh_op.h | 45 | ||||
-rw-r--r-- | old/botan/src/pubkey/dh/info.txt | 20 |
6 files changed, 371 insertions, 0 deletions
diff --git a/old/botan/src/pubkey/dh/dh.cpp b/old/botan/src/pubkey/dh/dh.cpp new file mode 100644 index 0000000..0c9d02f --- /dev/null +++ b/old/botan/src/pubkey/dh/dh.cpp @@ -0,0 +1,119 @@ +/* +* Diffie-Hellman +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#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/old/botan/src/pubkey/dh/dh.h b/old/botan/src/pubkey/dh/dh.h new file mode 100644 index 0000000..fa558bc --- /dev/null +++ b/old/botan/src/pubkey/dh/dh.h @@ -0,0 +1,80 @@ +/* +* Diffie-Hellman +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DIFFIE_HELLMAN_H__ +#define BOTAN_DIFFIE_HELLMAN_H__ + +#include <botan/dl_algo.h> +#include <botan/dh_core.h> + +namespace Botan { + +/** +* This class represents Diffie-Hellman public keys. +*/ +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; } + + /** + * Construct an uninitialized key. Use this constructor if you wish + * to decode an encoded key into the new instance. + */ + DH_PublicKey() {} + + /** + * Construct a public key with the specified parameters. + * @param grp the DL group to use in the key + * @param y the public value y + */ + DH_PublicKey(const DL_Group& grp, const BigInt& y); + private: + void X509_load_hook(); + }; + +/** +* This class represents Diffie-Hellman private keys. +*/ +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 other[], u32bit length) const; + SecureVector<byte> derive_key(const DH_PublicKey& other) const; + SecureVector<byte> derive_key(const BigInt& other) const; + + MemoryVector<byte> public_value() const; + + /** + * Construct an uninitialized key. Use this constructor if you wish + * to decode an encoded key into the new instance. + */ + DH_PrivateKey() {} + + /** + * Construct a private key with predetermined value. + * @param rng random number generator to use + * @param grp the group to be used in the key + * @param x the key's secret value (or if zero, generate a new key) + */ + DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, + const BigInt& x = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false); + DH_Core core; + }; + +} + +#endif diff --git a/old/botan/src/pubkey/dh/dh_core.cpp b/old/botan/src/pubkey/dh/dh_core.cpp new file mode 100644 index 0000000..78a26a8 --- /dev/null +++ b/old/botan/src/pubkey/dh/dh_core.cpp @@ -0,0 +1,69 @@ +/* +* PK Algorithm Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dh_core.h> +#include <botan/numthry.h> +#include <botan/pk_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/old/botan/src/pubkey/dh/dh_core.h b/old/botan/src/pubkey/dh/dh_core.h new file mode 100644 index 0000000..91b50a2 --- /dev/null +++ b/old/botan/src/pubkey/dh/dh_core.h @@ -0,0 +1,38 @@ +/* +* DH Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DH_CORE_H__ +#define BOTAN_DH_CORE_H__ + +#include <botan/dh_op.h> +#include <botan/blinding.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/old/botan/src/pubkey/dh/dh_op.h b/old/botan/src/pubkey/dh/dh_op.h new file mode 100644 index 0000000..50f3d78 --- /dev/null +++ b/old/botan/src/pubkey/dh/dh_op.h @@ -0,0 +1,45 @@ +/* +* DH Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DH_OPS_H__ +#define BOTAN_DH_OPS_H__ + +#include <botan/dl_group.h> +#include <botan/reducer.h> +#include <botan/pow_mod.h> + +namespace Botan { + +/* +* DH Operation Interface +*/ +class BOTAN_DLL DH_Operation + { + public: + virtual BigInt agree(const BigInt&) const = 0; + virtual DH_Operation* clone() const = 0; + virtual ~DH_Operation() {} + }; + +/* +* Botan's Default DH Operation +*/ +class BOTAN_DLL Default_DH_Op : public DH_Operation + { + public: + BigInt agree(const BigInt& i) const { return powermod_x_p(i); } + DH_Operation* clone() const { return new Default_DH_Op(*this); } + + Default_DH_Op(const DL_Group& group, const BigInt& x) : + powermod_x_p(x, group.get_p()) {} + private: + Fixed_Exponent_Power_Mod powermod_x_p; + }; + +} + +#endif diff --git a/old/botan/src/pubkey/dh/info.txt b/old/botan/src/pubkey/dh/info.txt new file mode 100644 index 0000000..33af9a8 --- /dev/null +++ b/old/botan/src/pubkey/dh/info.txt @@ -0,0 +1,20 @@ +realname "Diffie-Hellman Key Agreement" + +define DIFFIE_HELLMAN + +load_on auto + +<add> +dh.cpp +dh.h +dh_core.cpp +dh_core.h +dh_op.h +</add> + +<requires> +dl_algo +dl_group +libstate +numbertheory +</requires> |