diff options
Diffstat (limited to 'src/pubkey/nr')
-rw-r--r-- | src/pubkey/nr/info.txt | 2 | ||||
-rw-r--r-- | src/pubkey/nr/nr_core.h | 3 | ||||
-rw-r--r-- | src/pubkey/nr/nr_op.cpp | 79 | ||||
-rw-r--r-- | src/pubkey/nr/nr_op.h | 51 |
4 files changed, 133 insertions, 2 deletions
diff --git a/src/pubkey/nr/info.txt b/src/pubkey/nr/info.txt index f7325d984..b574af0e7 100644 --- a/src/pubkey/nr/info.txt +++ b/src/pubkey/nr/info.txt @@ -9,6 +9,8 @@ nr.cpp nr.h nr_core.cpp nr_core.h +nr_op.cpp +nr_op.h </add> <requires> diff --git a/src/pubkey/nr/nr_core.h b/src/pubkey/nr/nr_core.h index 416e31619..69a605c8d 100644 --- a/src/pubkey/nr/nr_core.h +++ b/src/pubkey/nr/nr_core.h @@ -6,8 +6,7 @@ #ifndef BOTAN_NR_CORE_H__ #define BOTAN_NR_CORE_H__ -#include <botan/bigint.h> -#include <botan/pk_ops.h> +#include <botan/nr_op.h> #include <botan/dl_group.h> namespace Botan { diff --git a/src/pubkey/nr/nr_op.cpp b/src/pubkey/nr/nr_op.cpp new file mode 100644 index 000000000..dfb44d617 --- /dev/null +++ b/src/pubkey/nr/nr_op.cpp @@ -0,0 +1,79 @@ +/************************************************* +* NR Operations Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/nr_op.h> +#include <botan/eng_def.h> + +namespace Botan { + +/************************************************* +* Default_NR_Op Constructor * +*************************************************/ +Default_NR_Op::Default_NR_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 NR Verify Operation * +*************************************************/ +SecureVector<byte> Default_NR_Op::verify(const byte in[], u32bit length) const + { + const BigInt& q = group.get_q(); + + if(length != 2*q.bytes()) + return false; + + BigInt c(in, q.bytes()); + BigInt d(in + q.bytes(), q.bytes()); + + if(c.is_zero() || c >= q || d >= q) + throw Invalid_Argument("Default_NR_Op::verify: Invalid signature"); + + BigInt i = mod_p.multiply(powermod_g_p(d), powermod_y_p(c)); + return BigInt::encode(mod_q.reduce(c - i)); + } + +/************************************************* +* Default NR Sign Operation * +*************************************************/ +SecureVector<byte> Default_NR_Op::sign(const byte in[], u32bit length, + const BigInt& k) const + { + if(x == 0) + throw Internal_Error("Default_NR_Op::sign: No private key"); + + const BigInt& q = group.get_q(); + + BigInt f(in, length); + + if(f >= q) + throw Invalid_Argument("Default_NR_Op::sign: 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; + } + +/************************************************* +* Acquire a NR op * +*************************************************/ +NR_Operation* Default_Engine::nr_op(const DL_Group& group, const BigInt& y, + const BigInt& x) const + { + return new Default_NR_Op(group, y, x); + } + +} diff --git a/src/pubkey/nr/nr_op.h b/src/pubkey/nr/nr_op.h new file mode 100644 index 000000000..11c2cd26a --- /dev/null +++ b/src/pubkey/nr/nr_op.h @@ -0,0 +1,51 @@ +/************************************************* +* NR Operations Header File * +* (C) 1999-2008 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_NR_OPS_H__ +#define BOTAN_NR_OPS_H__ + +#include <botan/pow_mod.h> +#include <botan/numthry.h> +#include <botan/reducer.h> +#include <botan/dl_group.h> + +namespace Botan { + +/************************************************* +* NR Operation * +*************************************************/ +class BOTAN_DLL NR_Operation + { + public: + virtual SecureVector<byte> verify(const byte[], u32bit) const = 0; + virtual SecureVector<byte> sign(const byte[], u32bit, + const BigInt&) const = 0; + virtual NR_Operation* clone() const = 0; + virtual ~NR_Operation() {} + }; + +/************************************************* +* Botan's Default NR Operation * +*************************************************/ +class BOTAN_DLL Default_NR_Op : public NR_Operation + { + public: + SecureVector<byte> verify(const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + + NR_Operation* clone() const { return new Default_NR_Op(*this); } + + Default_NR_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 |