aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/dsa
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-10-01 14:36:27 +0000
committerlloyd <[email protected]>2008-10-01 14:36:27 +0000
commit06e35ed5375028f246f57b99ccf639e8ed317b35 (patch)
tree32021a79d1617a147f7fa7ac29c1eae60f9024ce /src/pubkey/dsa
parent9320b5e5c1b64894a6ff8797f392b57dfd72dea3 (diff)
Rename pk dir to pubkey, avoids tab-completion collision with pk_pad
Diffstat (limited to 'src/pubkey/dsa')
-rw-r--r--src/pubkey/dsa/dsa.cpp131
-rw-r--r--src/pubkey/dsa/dsa.h60
-rw-r--r--src/pubkey/dsa/dsa_core.cpp67
-rw-r--r--src/pubkey/dsa/dsa_core.h36
-rw-r--r--src/pubkey/dsa/info.txt21
5 files changed, 315 insertions, 0 deletions
diff --git a/src/pubkey/dsa/dsa.cpp b/src/pubkey/dsa/dsa.cpp
new file mode 100644
index 000000000..ef69a1ee5
--- /dev/null
+++ b/src/pubkey/dsa/dsa.cpp
@@ -0,0 +1,131 @@
+/*************************************************
+* DSA Source File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#include <botan/dsa.h>
+#include <botan/numthry.h>
+#include <botan/keypair.h>
+
+namespace Botan {
+
+/*************************************************
+* DSA_PublicKey Constructor *
+*************************************************/
+DSA_PublicKey::DSA_PublicKey(const DL_Group& grp, const BigInt& y1)
+ {
+ group = grp;
+ y = y1;
+ X509_load_hook();
+ }
+
+/*************************************************
+* Algorithm Specific X.509 Initialization Code *
+*************************************************/
+void DSA_PublicKey::X509_load_hook()
+ {
+ core = DSA_Core(group, y);
+ }
+
+/*************************************************
+* DSA Verification Function *
+*************************************************/
+bool DSA_PublicKey::verify(const byte msg[], u32bit msg_len,
+ const byte sig[], u32bit sig_len) const
+ {
+ return core.verify(msg, msg_len, sig, sig_len);
+ }
+
+/*************************************************
+* Return the maximum input size in bits *
+*************************************************/
+u32bit DSA_PublicKey::max_input_bits() const
+ {
+ return group_q().bits();
+ }
+
+/*************************************************
+* Return the size of each portion of the sig *
+*************************************************/
+u32bit DSA_PublicKey::message_part_size() const
+ {
+ return group_q().bytes();
+ }
+
+/*************************************************
+* Create a DSA private key *
+*************************************************/
+DSA_PrivateKey::DSA_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 DSA_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng,
+ bool generated)
+ {
+ y = power_mod(group_g(), x, group_p());
+ core = DSA_Core(group, y, x);
+
+ if(generated)
+ gen_check(rng);
+ else
+ load_check(rng);
+ }
+
+/*************************************************
+* DSA Signature Operation *
+*************************************************/
+SecureVector<byte> DSA_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 DSA Parameters *
+*************************************************/
+bool DSA_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/src/pubkey/dsa/dsa.h b/src/pubkey/dsa/dsa.h
new file mode 100644
index 000000000..4175c19ad
--- /dev/null
+++ b/src/pubkey/dsa/dsa.h
@@ -0,0 +1,60 @@
+/*************************************************
+* DSA Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_DSA_H__
+#define BOTAN_DSA_H__
+
+#include <botan/dl_algo.h>
+#include <botan/dsa_core.h>
+
+namespace Botan {
+
+/*************************************************
+* DSA Public Key *
+*************************************************/
+class BOTAN_DLL DSA_PublicKey : public PK_Verifying_wo_MR_Key,
+ public virtual DL_Scheme_PublicKey
+ {
+ public:
+ std::string algo_name() const { return "DSA"; }
+
+ DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; }
+ u32bit message_parts() const { return 2; }
+ u32bit message_part_size() const;
+
+ bool verify(const byte[], u32bit, const byte[], u32bit) const;
+ u32bit max_input_bits() const;
+
+ DSA_PublicKey() {}
+ DSA_PublicKey(const DL_Group&, const BigInt&);
+ protected:
+ DSA_Core core;
+ private:
+ void X509_load_hook();
+ };
+
+/*************************************************
+* DSA Private Key *
+*************************************************/
+class BOTAN_DLL DSA_PrivateKey : public DSA_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;
+
+ DSA_PrivateKey() {}
+ DSA_PrivateKey(RandomNumberGenerator&, const DL_Group&,
+ const BigInt& = 0);
+ private:
+ void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false);
+ };
+
+}
+
+#endif
diff --git a/src/pubkey/dsa/dsa_core.cpp b/src/pubkey/dsa/dsa_core.cpp
new file mode 100644
index 000000000..aba1e61fb
--- /dev/null
+++ b/src/pubkey/dsa/dsa_core.cpp
@@ -0,0 +1,67 @@
+/*************************************************
+* DSA Core Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/dsa_core.h>
+#include <botan/numthry.h>
+#include <botan/engine.h>
+#include <botan/parsing.h>
+#include <algorithm>
+
+namespace Botan {
+
+namespace {
+
+const u32bit BLINDING_BITS = BOTAN_PRIVATE_KEY_OP_BLINDING_BITS;
+
+}
+
+/*************************************************
+* DSA_Core Constructor *
+*************************************************/
+DSA_Core::DSA_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
+ {
+ op = Engine_Core::dsa_op(group, y, x);
+ }
+
+/*************************************************
+* DSA_Core Copy Constructor *
+*************************************************/
+DSA_Core::DSA_Core(const DSA_Core& core)
+ {
+ op = 0;
+ if(core.op)
+ op = core.op->clone();
+ }
+
+/*************************************************
+* DSA_Core Assignment Operator *
+*************************************************/
+DSA_Core& DSA_Core::operator=(const DSA_Core& core)
+ {
+ delete op;
+ if(core.op)
+ op = core.op->clone();
+ return (*this);
+ }
+
+/*************************************************
+* DSA Verification Operation *
+*************************************************/
+bool DSA_Core::verify(const byte msg[], u32bit msg_length,
+ const byte sig[], u32bit sig_length) const
+ {
+ return op->verify(msg, msg_length, sig, sig_length);
+ }
+
+/*************************************************
+* DSA Signature Operation *
+*************************************************/
+SecureVector<byte> DSA_Core::sign(const byte in[], u32bit length,
+ const BigInt& k) const
+ {
+ return op->sign(in, length, k);
+ }
+
+}
diff --git a/src/pubkey/dsa/dsa_core.h b/src/pubkey/dsa/dsa_core.h
new file mode 100644
index 000000000..467f3c23f
--- /dev/null
+++ b/src/pubkey/dsa/dsa_core.h
@@ -0,0 +1,36 @@
+/*************************************************
+* DSA Core Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_DSA_CORE_H__
+#define BOTAN_DSA_CORE_H__
+
+#include <botan/bigint.h>
+#include <botan/pk_ops.h>
+#include <botan/dl_group.h>
+
+namespace Botan {
+
+/*************************************************
+* DSA Core *
+*************************************************/
+class BOTAN_DLL DSA_Core
+ {
+ public:
+ SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const;
+ bool verify(const byte[], u32bit, const byte[], u32bit) const;
+
+ DSA_Core& operator=(const DSA_Core&);
+
+ DSA_Core() { op = 0; }
+ DSA_Core(const DSA_Core&);
+ DSA_Core(const DL_Group&, const BigInt&, const BigInt& = 0);
+ ~DSA_Core() { delete op; }
+ private:
+ DSA_Operation* op;
+ };
+
+}
+
+#endif
diff --git a/src/pubkey/dsa/info.txt b/src/pubkey/dsa/info.txt
new file mode 100644
index 000000000..e98f33ca9
--- /dev/null
+++ b/src/pubkey/dsa/info.txt
@@ -0,0 +1,21 @@
+realname "DSA"
+
+define DSA
+
+load_on auto
+
+<add>
+dsa.cpp
+dsa.h
+dsa_core.cpp
+dsa_core.h
+</add>
+
+<requires>
+asn1
+bigint
+dl_algo
+keypair
+numbertheory
+pubkey
+</requires>