diff options
Diffstat (limited to 'botan/src/pubkey/dsa')
-rw-r--r-- | botan/src/pubkey/dsa/dsa.cpp | 134 | ||||
-rw-r--r-- | botan/src/pubkey/dsa/dsa.h | 62 | ||||
-rw-r--r-- | botan/src/pubkey/dsa/dsa_core.cpp | 63 | ||||
-rw-r--r-- | botan/src/pubkey/dsa/dsa_core.h | 37 | ||||
-rw-r--r-- | botan/src/pubkey/dsa/dsa_op.cpp | 73 | ||||
-rw-r--r-- | botan/src/pubkey/dsa/dsa_op.h | 53 | ||||
-rw-r--r-- | botan/src/pubkey/dsa/info.txt | 22 |
7 files changed, 444 insertions, 0 deletions
diff --git a/botan/src/pubkey/dsa/dsa.cpp b/botan/src/pubkey/dsa/dsa.cpp new file mode 100644 index 0000000..b0688ae --- /dev/null +++ b/botan/src/pubkey/dsa/dsa.cpp @@ -0,0 +1,134 @@ +/* +* DSA +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dsa.h> +#include <botan/numthry.h> +#include <botan/keypair.h> +#include <botan/look_pk.h> + +namespace Botan { + +/* +* DSA_PublicKey Constructor +*/ +DSA_PublicKey::DSA_PublicKey(const DL_Group& grp, const BigInt& y1) + { + group = grp; + y = y1; + X509_load_hook(); + } + +/* +* Algorithm Specific X.509 Initialization Code +*/ +void DSA_PublicKey::X509_load_hook() + { + core = DSA_Core(group, y); + } + +/* +* DSA Verification Function +*/ +bool DSA_PublicKey::verify(const byte msg[], u32bit msg_len, + const byte sig[], u32bit sig_len) const + { + return core.verify(msg, msg_len, sig, sig_len); + } + +/* +* Return the maximum input size in bits +*/ +u32bit DSA_PublicKey::max_input_bits() const + { + return group_q().bits(); + } + +/* +* Return the size of each portion of the sig +*/ +u32bit DSA_PublicKey::message_part_size() const + { + return group_q().bytes(); + } + +/* +* Create a DSA private key +*/ +DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng, + const DL_Group& grp, + const BigInt& x_arg) + { + group = grp; + x = x_arg; + + if(x == 0) + { + x = BigInt::random_integer(rng, 2, group_q() - 1); + PKCS8_load_hook(rng, true); + } + else + PKCS8_load_hook(rng, false); + } + +/* +* Algorithm Specific PKCS #8 Initialization Code +*/ +void DSA_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, + bool generated) + { + y = power_mod(group_g(), x, group_p()); + core = DSA_Core(group, y, x); + + if(generated) + gen_check(rng); + else + load_check(rng); + } + +/* +* DSA Signature Operation +*/ +SecureVector<byte> DSA_PrivateKey::sign(const byte in[], u32bit length, + RandomNumberGenerator& rng) const + { + const BigInt& q = group_q(); + + BigInt k; + do + k.randomize(rng, q.bits()); + while(k >= q); + + return core.sign(in, length, k); + } + +/* +* Check Private DSA Parameters +*/ +bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const + { + if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q()) + return false; + + if(!strong) + return true; + + try + { + KeyPair::check_key(rng, + get_pk_signer(*this, "EMSA1(SHA-1)"), + get_pk_verifier(*this, "EMSA1(SHA-1)") + ); + } + catch(Self_Test_Failure) + { + return false; + } + + return true; + } + +} diff --git a/botan/src/pubkey/dsa/dsa.h b/botan/src/pubkey/dsa/dsa.h new file mode 100644 index 0000000..4c9b708 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa.h @@ -0,0 +1,62 @@ +/* +* DSA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DSA_H__ +#define BOTAN_DSA_H__ + +#include <botan/dl_algo.h> +#include <botan/dsa_core.h> + +namespace Botan { + +/* +* DSA Public Key +*/ +class BOTAN_DLL DSA_PublicKey : public PK_Verifying_wo_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "DSA"; } + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + + bool verify(const byte[], u32bit, const byte[], u32bit) const; + u32bit max_input_bits() const; + + DSA_PublicKey() {} + DSA_PublicKey(const DL_Group&, const BigInt&); + protected: + DSA_Core core; + private: + void X509_load_hook(); + }; + +/* +* DSA Private Key +*/ +class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + DSA_PrivateKey() {} + DSA_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false); + }; + +} + +#endif diff --git a/botan/src/pubkey/dsa/dsa_core.cpp b/botan/src/pubkey/dsa/dsa_core.cpp new file mode 100644 index 0000000..e5a23a5 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_core.cpp @@ -0,0 +1,63 @@ +/* +* DSA Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dsa_core.h> +#include <botan/numthry.h> +#include <botan/pk_engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/* +* DSA_Core Constructor +*/ +DSA_Core::DSA_Core(const DL_Group& group, const BigInt& y, const BigInt& x) + { + op = Engine_Core::dsa_op(group, y, x); + } + +/* +* DSA_Core Copy Constructor +*/ +DSA_Core::DSA_Core(const DSA_Core& core) + { + op = 0; + if(core.op) + op = core.op->clone(); + } + +/* +* DSA_Core Assignment Operator +*/ +DSA_Core& DSA_Core::operator=(const DSA_Core& core) + { + delete op; + if(core.op) + op = core.op->clone(); + return (*this); + } + +/* +* DSA Verification Operation +*/ +bool DSA_Core::verify(const byte msg[], u32bit msg_length, + const byte sig[], u32bit sig_length) const + { + return op->verify(msg, msg_length, sig, sig_length); + } + +/* +* DSA Signature Operation +*/ +SecureVector<byte> DSA_Core::sign(const byte in[], u32bit length, + const BigInt& k) const + { + return op->sign(in, length, k); + } + +} diff --git a/botan/src/pubkey/dsa/dsa_core.h b/botan/src/pubkey/dsa/dsa_core.h new file mode 100644 index 0000000..8bb1621 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_core.h @@ -0,0 +1,37 @@ +/* +* DSA Core +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DSA_CORE_H__ +#define BOTAN_DSA_CORE_H__ + +#include <botan/dsa_op.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* DSA Core +*/ +class BOTAN_DLL DSA_Core + { + public: + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + bool verify(const byte[], u32bit, const byte[], u32bit) const; + + DSA_Core& operator=(const DSA_Core&); + + DSA_Core() { op = 0; } + DSA_Core(const DSA_Core&); + DSA_Core(const DL_Group&, const BigInt&, const BigInt& = 0); + ~DSA_Core() { delete op; } + private: + DSA_Operation* op; + }; + +} + +#endif diff --git a/botan/src/pubkey/dsa/dsa_op.cpp b/botan/src/pubkey/dsa/dsa_op.cpp new file mode 100644 index 0000000..5b92144 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_op.cpp @@ -0,0 +1,73 @@ +/* +* DSA Operations +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/dsa_op.h> + +namespace Botan { + +/* +* Default_DSA_Op Constructor +*/ +Default_DSA_Op::Default_DSA_Op(const DL_Group& grp, const BigInt& y1, + const BigInt& x1) : x(x1), y(y1), group(grp) + { + powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), group.get_p()); + powermod_y_p = Fixed_Base_Power_Mod(y, group.get_p()); + mod_p = Modular_Reducer(group.get_p()); + mod_q = Modular_Reducer(group.get_q()); + } + +/* +* Default DSA Verify Operation +*/ +bool Default_DSA_Op::verify(const byte msg[], u32bit msg_len, + const byte sig[], u32bit sig_len) const + { + const BigInt& q = group.get_q(); + + if(sig_len != 2*q.bytes() || msg_len > q.bytes()) + return false; + + BigInt r(sig, q.bytes()); + BigInt s(sig + q.bytes(), q.bytes()); + BigInt i(msg, msg_len); + + if(r <= 0 || r >= q || s <= 0 || s >= q) + return false; + + s = inverse_mod(s, q); + s = mod_p.multiply(powermod_g_p(mod_q.multiply(s, i)), + powermod_y_p(mod_q.multiply(s, r))); + + return (mod_q.reduce(s) == r); + } + +/* +* Default DSA Sign Operation +*/ +SecureVector<byte> Default_DSA_Op::sign(const byte in[], u32bit length, + const BigInt& k) const + { + if(x == 0) + throw Internal_Error("Default_DSA_Op::sign: No private key"); + + const BigInt& q = group.get_q(); + BigInt i(in, length); + + BigInt r = mod_q.reduce(powermod_g_p(k)); + BigInt s = mod_q.multiply(inverse_mod(k, q), mul_add(x, r, i)); + + if(r.is_zero() || s.is_zero()) + throw Internal_Error("Default_DSA_Op::sign: r or s was zero"); + + SecureVector<byte> output(2*q.bytes()); + r.binary_encode(output + (output.size() / 2 - r.bytes())); + s.binary_encode(output + (output.size() - s.bytes())); + return output; + } + +} diff --git a/botan/src/pubkey/dsa/dsa_op.h b/botan/src/pubkey/dsa/dsa_op.h new file mode 100644 index 0000000..0b112c6 --- /dev/null +++ b/botan/src/pubkey/dsa/dsa_op.h @@ -0,0 +1,53 @@ +/* +* DSA Operations +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_DSA_OPS_H__ +#define BOTAN_DSA_OPS_H__ + +#include <botan/numthry.h> +#include <botan/pow_mod.h> +#include <botan/reducer.h> +#include <botan/dl_group.h> + +namespace Botan { + +/* +* DSA Operation +*/ +class BOTAN_DLL DSA_Operation + { + public: + virtual bool verify(const byte[], u32bit, + const byte[], u32bit) const = 0; + virtual SecureVector<byte> sign(const byte[], u32bit, + const BigInt&) const = 0; + virtual DSA_Operation* clone() const = 0; + virtual ~DSA_Operation() {} + }; + +/* +* Botan's Default DSA Operation +*/ +class BOTAN_DLL Default_DSA_Op : public DSA_Operation + { + public: + bool verify(const byte[], u32bit, const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + DSA_Operation* clone() const { return new Default_DSA_Op(*this); } + + Default_DSA_Op(const DL_Group&, const BigInt&, const BigInt&); + private: + const BigInt x, y; + const DL_Group group; + Fixed_Base_Power_Mod powermod_g_p, powermod_y_p; + Modular_Reducer mod_p, mod_q; + }; + +} + +#endif diff --git a/botan/src/pubkey/dsa/info.txt b/botan/src/pubkey/dsa/info.txt new file mode 100644 index 0000000..c70e02d --- /dev/null +++ b/botan/src/pubkey/dsa/info.txt @@ -0,0 +1,22 @@ +realname "DSA" + +define DSA + +load_on auto + +<add> +dsa.cpp +dsa.h +dsa_core.cpp +dsa_core.h +dsa_op.cpp +dsa_op.h +</add> + +<requires> +dl_algo +dl_group +keypair +libstate +numbertheory +</requires> |