diff options
author | lloyd <[email protected]> | 2010-03-05 01:09:28 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-03-05 01:09:28 +0000 |
commit | 1c7dbb21d19702872379421e6ae44a15caf67da2 (patch) | |
tree | bd4ee9e01f8cfd9631655d0e0b0991d49c0a7e8e /src/pubkey/dsa | |
parent | 78b5b103291ee668185dc71d138a50e8e7e54808 (diff) |
Add signature generation operation classes. Remove sign() from
PK_Signing_Key, though for the moment the class remains because there
are a few pieces of code that use it to detect if signatures are
supported, or for passing to functions in look_pk
Diffstat (limited to 'src/pubkey/dsa')
-rw-r--r-- | src/pubkey/dsa/dsa.cpp | 33 | ||||
-rw-r--r-- | src/pubkey/dsa/dsa.h | 31 |
2 files changed, 59 insertions, 5 deletions
diff --git a/src/pubkey/dsa/dsa.cpp b/src/pubkey/dsa/dsa.cpp index 91adb83d4..403243a97 100644 --- a/src/pubkey/dsa/dsa.cpp +++ b/src/pubkey/dsa/dsa.cpp @@ -109,4 +109,37 @@ bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const return true; } +DSA_Signature_Operation::DSA_Signature_Operation(const DSA_PrivateKey& dsa) : + q(dsa.group_q()), + x(dsa.get_x()), + powermod_g_p(dsa.group_g(), dsa.group_p()), + mod_q(dsa.group_q()) + { + } + +SecureVector<byte> DSA_Signature_Operation::sign(const byte msg[], + u32bit msg_len, + RandomNumberGenerator& rng) + { + rng.add_entropy(msg, msg_len); + + BigInt k; + do + k.randomize(rng, q.bits()); + while(k >= q); + + BigInt i(msg, msg_len); + + 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("DSA signature gen failure: 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/src/pubkey/dsa/dsa.h b/src/pubkey/dsa/dsa.h index 8caa24b6b..444b3a825 100644 --- a/src/pubkey/dsa/dsa.h +++ b/src/pubkey/dsa/dsa.h @@ -9,6 +9,9 @@ #define BOTAN_DSA_H__ #include <botan/dl_algo.h> +#include <botan/pk_ops.h> +#include <botan/reducer.h> +#include <botan/pow_mod.h> #include <botan/dsa_core.h> namespace Botan { @@ -51,11 +54,6 @@ class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey, public virtual DL_Scheme_PrivateKey { public: - SecureVector<byte> sign(const byte hash[], u32bit hash_len, - RandomNumberGenerator& rng) const; - - bool check_key(RandomNumberGenerator& rng, bool strong) const; - DSA_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng); @@ -63,6 +61,29 @@ class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey, DSA_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group, const BigInt& private_key = 0); + + bool check_key(RandomNumberGenerator& rng, bool strong) const; + + SecureVector<byte> sign(const byte hash[], u32bit hash_len, + RandomNumberGenerator& rng) const; + }; + +class BOTAN_DLL DSA_Signature_Operation : public PK_Ops::Signature_Operation + { + public: + DSA_Signature_Operation(const DSA_PrivateKey& dsa); + + u32bit message_parts() const { return 2; } + u32bit message_part_size() const { return q.bytes(); } + u32bit max_input_bits() const { return q.bits(); } + + SecureVector<byte> sign(const byte msg[], u32bit msg_len, + RandomNumberGenerator& rng); + private: + const BigInt& q; + const BigInt& x; + Fixed_Base_Power_Mod powermod_g_p; + Modular_Reducer mod_q; }; } |