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/nr | |
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/nr')
-rw-r--r-- | src/pubkey/nr/nr.cpp | 37 | ||||
-rw-r--r-- | src/pubkey/nr/nr.h | 23 |
2 files changed, 57 insertions, 3 deletions
diff --git a/src/pubkey/nr/nr.cpp b/src/pubkey/nr/nr.cpp index cdcaf3af3..08ed6b376 100644 --- a/src/pubkey/nr/nr.cpp +++ b/src/pubkey/nr/nr.cpp @@ -1,6 +1,6 @@ /* * Nyberg-Rueppel -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -115,4 +115,39 @@ bool NR_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const return true; } +NR_Signature_Operation::NR_Signature_Operation(const NR_PrivateKey& nr) : + q(nr.group_q()), + x(nr.get_x()), + powermod_g_p(nr.group_g(), nr.group_p()), + mod_q(nr.group_q()) + { + } + +SecureVector<byte> NR_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 f(msg, msg_len); + + if(f >= q) + throw Invalid_Argument("NR_Signature_Operation: Input is out of range"); + + BigInt c = mod_q.reduce(powermod_g_p(k) + f); + if(c.is_zero()) + throw Internal_Error("Default_NR_Op::sign: c was zero"); + BigInt d = mod_q.reduce(k - x * c); + + SecureVector<byte> output(2*q.bytes()); + c.binary_encode(output + (output.size() / 2 - c.bytes())); + d.binary_encode(output + (output.size() - d.bytes())); + return output; + } + } diff --git a/src/pubkey/nr/nr.h b/src/pubkey/nr/nr.h index 1479ef064..013f3d42b 100644 --- a/src/pubkey/nr/nr.h +++ b/src/pubkey/nr/nr.h @@ -9,6 +9,7 @@ #define BOTAN_NYBERG_RUEPPEL_H__ #include <botan/dl_algo.h> +#include <botan/pk_ops.h> #include <botan/nr_core.h> namespace Botan { @@ -24,11 +25,11 @@ class BOTAN_DLL NR_PublicKey : public PK_Verifying_with_MR_Key, SecureVector<byte> verify(const byte sig[], u32bit sig_len) const; - u32bit max_input_bits() const { return (group_q().bits() - 1); } - DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } u32bit message_part_size() const { return group_q().bytes(); } + u32bit max_input_bits() const { return (group_q().bits() - 1); } NR_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits); @@ -61,6 +62,24 @@ class BOTAN_DLL NR_PrivateKey : public NR_PublicKey, const BigInt& x = 0); }; +class BOTAN_DLL NR_Signature_Operation : public PK_Ops::Signature_Operation + { + public: + NR_Signature_Operation(const NR_PrivateKey& nr); + + u32bit message_parts() const { return 2; } + u32bit message_part_size() const { return q.bytes(); } + u32bit max_input_bits() const { return (q.bits() - 1); } + + 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; + }; + } #endif |