summaryrefslogtreecommitdiffstats
path: root/botan/src/pubkey/nr
diff options
context:
space:
mode:
Diffstat (limited to 'botan/src/pubkey/nr')
-rw-r--r--botan/src/pubkey/nr/info.txt22
-rw-r--r--botan/src/pubkey/nr/nr.cpp134
-rw-r--r--botan/src/pubkey/nr/nr.h63
-rw-r--r--botan/src/pubkey/nr/nr_core.cpp62
-rw-r--r--botan/src/pubkey/nr/nr_core.h37
-rw-r--r--botan/src/pubkey/nr/nr_op.cpp71
-rw-r--r--botan/src/pubkey/nr/nr_op.h53
7 files changed, 442 insertions, 0 deletions
diff --git a/botan/src/pubkey/nr/info.txt b/botan/src/pubkey/nr/info.txt
new file mode 100644
index 0000000..c89820a
--- /dev/null
+++ b/botan/src/pubkey/nr/info.txt
@@ -0,0 +1,22 @@
+realname "Nyberg-Rueppel"
+
+define NYBERG_RUEPPEL
+
+load_on auto
+
+<add>
+nr.cpp
+nr.h
+nr_core.cpp
+nr_core.h
+nr_op.cpp
+nr_op.h
+</add>
+
+<requires>
+dl_algo
+dl_group
+keypair
+libstate
+numbertheory
+</requires>
diff --git a/botan/src/pubkey/nr/nr.cpp b/botan/src/pubkey/nr/nr.cpp
new file mode 100644
index 0000000..ad4ae78
--- /dev/null
+++ b/botan/src/pubkey/nr/nr.cpp
@@ -0,0 +1,134 @@
+/*
+* Nyberg-Rueppel
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/nr.h>
+#include <botan/numthry.h>
+#include <botan/keypair.h>
+#include <botan/look_pk.h>
+
+namespace Botan {
+
+/*
+* NR_PublicKey Constructor
+*/
+NR_PublicKey::NR_PublicKey(const DL_Group& grp, const BigInt& y1)
+ {
+ group = grp;
+ y = y1;
+ X509_load_hook();
+ }
+
+/*
+* Algorithm Specific X.509 Initialization Code
+*/
+void NR_PublicKey::X509_load_hook()
+ {
+ core = NR_Core(group, y);
+ }
+
+/*
+* Nyberg-Rueppel Verification Function
+*/
+SecureVector<byte> NR_PublicKey::verify(const byte sig[], u32bit sig_len) const
+ {
+ return core.verify(sig, sig_len);
+ }
+
+/*
+* Return the maximum input size in bits
+*/
+u32bit NR_PublicKey::max_input_bits() const
+ {
+ return (group_q().bits() - 1);
+ }
+
+/*
+* Return the size of each portion of the sig
+*/
+u32bit NR_PublicKey::message_part_size() const
+ {
+ return group_q().bytes();
+ }
+
+/*
+* Create a NR private key
+*/
+NR_PrivateKey::NR_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 NR_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng,
+ bool generated)
+ {
+ if(y == 0)
+ y = power_mod(group_g(), x, group_p());
+ core = NR_Core(group, y, x);
+
+ if(generated)
+ gen_check(rng);
+ else
+ load_check(rng);
+ }
+
+/*
+* Nyberg-Rueppel Signature Operation
+*/
+SecureVector<byte> NR_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 Nyberg-Rueppel Parameters
+*/
+bool NR_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/nr/nr.h b/botan/src/pubkey/nr/nr.h
new file mode 100644
index 0000000..144c5ec
--- /dev/null
+++ b/botan/src/pubkey/nr/nr.h
@@ -0,0 +1,63 @@
+/*
+* Nyberg-Rueppel
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_NYBERG_RUEPPEL_H__
+#define BOTAN_NYBERG_RUEPPEL_H__
+
+#include <botan/dl_algo.h>
+#include <botan/nr_core.h>
+
+namespace Botan {
+
+/*
+* Nyberg-Rueppel Public Key
+*/
+class BOTAN_DLL NR_PublicKey : public PK_Verifying_with_MR_Key,
+ public virtual DL_Scheme_PublicKey
+ {
+ public:
+ std::string algo_name() const { return "NR"; }
+
+ SecureVector<byte> verify(const byte[], u32bit) const;
+ u32bit max_input_bits() const;
+
+ DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; }
+ u32bit message_parts() const { return 2; }
+ u32bit message_part_size() const;
+
+ NR_PublicKey() {}
+ NR_PublicKey(const DL_Group&, const BigInt&);
+ protected:
+ NR_Core core;
+ private:
+ void X509_load_hook();
+ };
+
+/*
+* Nyberg-Rueppel Private Key
+*/
+class BOTAN_DLL NR_PrivateKey : public NR_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;
+
+ NR_PrivateKey() {}
+
+ NR_PrivateKey(RandomNumberGenerator&, const DL_Group&,
+ const BigInt& = 0);
+ private:
+ void PKCS8_load_hook(RandomNumberGenerator&, bool = false);
+ };
+
+}
+
+#endif
diff --git a/botan/src/pubkey/nr/nr_core.cpp b/botan/src/pubkey/nr/nr_core.cpp
new file mode 100644
index 0000000..afa1115
--- /dev/null
+++ b/botan/src/pubkey/nr/nr_core.cpp
@@ -0,0 +1,62 @@
+/*
+* NR Core
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/nr_core.h>
+#include <botan/numthry.h>
+#include <botan/pk_engine.h>
+#include <botan/parsing.h>
+#include <algorithm>
+
+namespace Botan {
+
+/*
+* NR_Core Constructor
+*/
+NR_Core::NR_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
+ {
+ op = Engine_Core::nr_op(group, y, x);
+ }
+
+/*
+* NR_Core Copy Constructor
+*/
+NR_Core::NR_Core(const NR_Core& core)
+ {
+ op = 0;
+ if(core.op)
+ op = core.op->clone();
+ }
+
+/*
+* NR_Core Assignment Operator
+*/
+NR_Core& NR_Core::operator=(const NR_Core& core)
+ {
+ delete op;
+ if(core.op)
+ op = core.op->clone();
+ return (*this);
+ }
+
+/*
+* NR Verification Operation
+*/
+SecureVector<byte> NR_Core::verify(const byte in[], u32bit length) const
+ {
+ return op->verify(in, length);
+ }
+
+/*
+* NR Signature Operation
+*/
+SecureVector<byte> NR_Core::sign(const byte in[], u32bit length,
+ const BigInt& k) const
+ {
+ return op->sign(in, length, k);
+ }
+
+}
diff --git a/botan/src/pubkey/nr/nr_core.h b/botan/src/pubkey/nr/nr_core.h
new file mode 100644
index 0000000..4837736
--- /dev/null
+++ b/botan/src/pubkey/nr/nr_core.h
@@ -0,0 +1,37 @@
+/*
+* NR Core
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_NR_CORE_H__
+#define BOTAN_NR_CORE_H__
+
+#include <botan/nr_op.h>
+#include <botan/dl_group.h>
+
+namespace Botan {
+
+/*
+* NR Core
+*/
+class BOTAN_DLL NR_Core
+ {
+ public:
+ SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const;
+ SecureVector<byte> verify(const byte[], u32bit) const;
+
+ NR_Core& operator=(const NR_Core&);
+
+ NR_Core() { op = 0; }
+ NR_Core(const NR_Core&);
+ NR_Core(const DL_Group&, const BigInt&, const BigInt& = 0);
+ ~NR_Core() { delete op; }
+ private:
+ NR_Operation* op;
+ };
+
+}
+
+#endif
diff --git a/botan/src/pubkey/nr/nr_op.cpp b/botan/src/pubkey/nr/nr_op.cpp
new file mode 100644
index 0000000..b5efa3d
--- /dev/null
+++ b/botan/src/pubkey/nr/nr_op.cpp
@@ -0,0 +1,71 @@
+/*
+* NR Operations
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/nr_op.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;
+ }
+
+}
diff --git a/botan/src/pubkey/nr/nr_op.h b/botan/src/pubkey/nr/nr_op.h
new file mode 100644
index 0000000..cba1465
--- /dev/null
+++ b/botan/src/pubkey/nr/nr_op.h
@@ -0,0 +1,53 @@
+/*
+* NR Operations
+* (C) 1999-2008 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#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