diff options
author | lloyd <[email protected]> | 2008-10-01 15:17:52 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-01 15:17:52 +0000 |
commit | 92ddf6f0f8f0ef6f5584889481e4a098e280ee40 (patch) | |
tree | 4178ad93250da66d0eb18f9dfcce221bbb57bcd7 /src/pubkey/dsa | |
parent | 1034cf44b4ee0948312c11a1b079b8b04c5828e2 (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/dsa')
-rw-r--r-- | src/pubkey/dsa/dsa_core.h | 3 | ||||
-rw-r--r-- | src/pubkey/dsa/dsa_op.cpp | 81 | ||||
-rw-r--r-- | src/pubkey/dsa/dsa_op.h | 51 | ||||
-rw-r--r-- | src/pubkey/dsa/info.txt | 16 |
4 files changed, 142 insertions, 9 deletions
diff --git a/src/pubkey/dsa/dsa_core.h b/src/pubkey/dsa/dsa_core.h index 467f3c23f..d1aa413e5 100644 --- a/src/pubkey/dsa/dsa_core.h +++ b/src/pubkey/dsa/dsa_core.h @@ -6,8 +6,7 @@ #ifndef BOTAN_DSA_CORE_H__ #define BOTAN_DSA_CORE_H__ -#include <botan/bigint.h> -#include <botan/pk_ops.h> +#include <botan/dsa_op.h> #include <botan/dl_group.h> namespace Botan { diff --git a/src/pubkey/dsa/dsa_op.cpp b/src/pubkey/dsa/dsa_op.cpp new file mode 100644 index 000000000..8b6e5403b --- /dev/null +++ b/src/pubkey/dsa/dsa_op.cpp @@ -0,0 +1,81 @@ +/************************************************* +* DSA Operations Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/dsa_op.h> +#include <botan/eng_def.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; + } + +/************************************************* +* Acquire a DSA op * +*************************************************/ +DSA_Operation* Default_Engine::dsa_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new Default_DSA_Op(group, y, x); + } + +} diff --git a/src/pubkey/dsa/dsa_op.h b/src/pubkey/dsa/dsa_op.h new file mode 100644 index 000000000..98e671bf0 --- /dev/null +++ b/src/pubkey/dsa/dsa_op.h @@ -0,0 +1,51 @@ +/************************************************* +* DSA Operations Header File * +* (C) 1999-2008 Jack Lloyd * +*************************************************/ + +#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/src/pubkey/dsa/info.txt b/src/pubkey/dsa/info.txt index e98f33ca9..f1b505800 100644 --- a/src/pubkey/dsa/info.txt +++ b/src/pubkey/dsa/info.txt @@ -4,13 +4,6 @@ define DSA load_on auto -<add> -dsa.cpp -dsa.h -dsa_core.cpp -dsa_core.h -</add> - <requires> asn1 bigint @@ -19,3 +12,12 @@ keypair numbertheory pubkey </requires> + +<add> +dsa.cpp +dsa.h +dsa_core.cpp +dsa_core.h +dsa_op.cpp +dsa_op.h +</add> |