aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/if_algo
diff options
context:
space:
mode:
authorlloyd <lloyd@randombit.net>2008-10-01 14:36:27 +0000
committerlloyd <lloyd@randombit.net>2008-10-01 14:36:27 +0000
commit06e35ed5375028f246f57b99ccf639e8ed317b35 (patch)
tree32021a79d1617a147f7fa7ac29c1eae60f9024ce /src/pubkey/if_algo
parent9320b5e5c1b64894a6ff8797f392b57dfd72dea3 (diff)
Rename pk dir to pubkey, avoids tab-completion collision with pk_pad
Diffstat (limited to 'src/pubkey/if_algo')
-rw-r--r--src/pubkey/if_algo/if_algo.cpp213
-rw-r--r--src/pubkey/if_algo/if_algo.h58
-rw-r--r--src/pubkey/if_algo/if_core.cpp85
-rw-r--r--src/pubkey/if_algo/if_core.h44
-rw-r--r--src/pubkey/if_algo/info.txt18
5 files changed, 418 insertions, 0 deletions
diff --git a/src/pubkey/if_algo/if_algo.cpp b/src/pubkey/if_algo/if_algo.cpp
new file mode 100644
index 000000000..929f488fd
--- /dev/null
+++ b/src/pubkey/if_algo/if_algo.cpp
@@ -0,0 +1,213 @@
+/*************************************************
+* IF Scheme Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/if_algo.h>
+#include <botan/numthry.h>
+#include <botan/der_enc.h>
+#include <botan/ber_dec.h>
+
+namespace Botan {
+
+/*************************************************
+* Return the X.509 public key encoder *
+*************************************************/
+X509_Encoder* IF_Scheme_PublicKey::x509_encoder() const
+ {
+ class IF_Scheme_Encoder : public X509_Encoder
+ {
+ public:
+ AlgorithmIdentifier alg_id() const
+ {
+ return AlgorithmIdentifier(key->get_oid(),
+ AlgorithmIdentifier::USE_NULL_PARAM);
+ }
+
+ MemoryVector<byte> key_bits() const
+ {
+ return DER_Encoder()
+ .start_cons(SEQUENCE)
+ .encode(key->n)
+ .encode(key->e)
+ .end_cons()
+ .get_contents();
+ }
+
+ IF_Scheme_Encoder(const IF_Scheme_PublicKey* k) : key(k) {}
+ private:
+ const IF_Scheme_PublicKey* key;
+ };
+
+ return new IF_Scheme_Encoder(this);
+ }
+
+/*************************************************
+* Return the X.509 public key decoder *
+*************************************************/
+X509_Decoder* IF_Scheme_PublicKey::x509_decoder()
+ {
+ class IF_Scheme_Decoder : public X509_Decoder
+ {
+ public:
+ void alg_id(const AlgorithmIdentifier&) {}
+
+ void key_bits(const MemoryRegion<byte>& bits)
+ {
+ BER_Decoder(bits)
+ .start_cons(SEQUENCE)
+ .decode(key->n)
+ .decode(key->e)
+ .verify_end()
+ .end_cons();
+
+ key->X509_load_hook();
+ }
+
+ IF_Scheme_Decoder(IF_Scheme_PublicKey* k) : key(k) {}
+ private:
+ IF_Scheme_PublicKey* key;
+ };
+
+ return new IF_Scheme_Decoder(this);
+ }
+
+/*************************************************
+* Return the PKCS #8 public key encoder *
+*************************************************/
+PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const
+ {
+ class IF_Scheme_Encoder : public PKCS8_Encoder
+ {
+ public:
+ AlgorithmIdentifier alg_id() const
+ {
+ return AlgorithmIdentifier(key->get_oid(),
+ AlgorithmIdentifier::USE_NULL_PARAM);
+ }
+
+ MemoryVector<byte> key_bits() const
+ {
+ return DER_Encoder()
+ .start_cons(SEQUENCE)
+ .encode(static_cast<u32bit>(0))
+ .encode(key->n)
+ .encode(key->e)
+ .encode(key->d)
+ .encode(key->p)
+ .encode(key->q)
+ .encode(key->d1)
+ .encode(key->d2)
+ .encode(key->c)
+ .end_cons()
+ .get_contents();
+ }
+
+ IF_Scheme_Encoder(const IF_Scheme_PrivateKey* k) : key(k) {}
+ private:
+ const IF_Scheme_PrivateKey* key;
+ };
+
+ return new IF_Scheme_Encoder(this);
+ }
+
+/*************************************************
+* Return the PKCS #8 public key decoder *
+*************************************************/
+PKCS8_Decoder* IF_Scheme_PrivateKey::pkcs8_decoder(RandomNumberGenerator& rng)
+ {
+ class IF_Scheme_Decoder : public PKCS8_Decoder
+ {
+ public:
+ void alg_id(const AlgorithmIdentifier&) {}
+
+ void key_bits(const MemoryRegion<byte>& bits)
+ {
+ u32bit version;
+
+ BER_Decoder(bits)
+ .start_cons(SEQUENCE)
+ .decode(version)
+ .decode(key->n)
+ .decode(key->e)
+ .decode(key->d)
+ .decode(key->p)
+ .decode(key->q)
+ .decode(key->d1)
+ .decode(key->d2)
+ .decode(key->c)
+ .end_cons();
+
+ if(version != 0)
+ throw Decoding_Error("Unknown PKCS #1 key format version");
+
+ key->PKCS8_load_hook(rng);
+ }
+
+ IF_Scheme_Decoder(IF_Scheme_PrivateKey* k, RandomNumberGenerator& r) :
+ key(k), rng(r) {}
+ private:
+ IF_Scheme_PrivateKey* key;
+ RandomNumberGenerator& rng;
+ };
+
+ return new IF_Scheme_Decoder(this, rng);
+ }
+
+/*************************************************
+* Algorithm Specific X.509 Initialization Code *
+*************************************************/
+void IF_Scheme_PublicKey::X509_load_hook()
+ {
+ core = IF_Core(e, n);
+ }
+
+/*************************************************
+* Algorithm Specific PKCS #8 Initialization Code *
+*************************************************/
+void IF_Scheme_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng,
+ bool generated)
+ {
+ if(n == 0) n = p * q;
+ if(d1 == 0) d1 = d % (p - 1);
+ if(d2 == 0) d2 = d % (q - 1);
+ if(c == 0) c = inverse_mod(q, p);
+
+ core = IF_Core(rng, e, n, d, p, q, d1, d2, c);
+
+ if(generated)
+ gen_check(rng);
+ else
+ load_check(rng);
+ }
+
+/*************************************************
+* Check IF Scheme Public Parameters *
+*************************************************/
+bool IF_Scheme_PublicKey::check_key(RandomNumberGenerator&, bool) const
+ {
+ if(n < 35 || n.is_even() || e < 2)
+ return false;
+ return true;
+ }
+
+/*************************************************
+* Check IF Scheme Private Parameters *
+*************************************************/
+bool IF_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng,
+ bool strong) const
+ {
+ if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
+ return false;
+
+ if(!strong)
+ return true;
+
+ if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
+ return false;
+ if(!check_prime(p, rng) || !check_prime(q, rng))
+ return false;
+ return true;
+ }
+
+}
diff --git a/src/pubkey/if_algo/if_algo.h b/src/pubkey/if_algo/if_algo.h
new file mode 100644
index 000000000..c58b2cb8f
--- /dev/null
+++ b/src/pubkey/if_algo/if_algo.h
@@ -0,0 +1,58 @@
+/*************************************************
+* IF Scheme Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_IF_ALGO_H__
+#define BOTAN_IF_ALGO_H__
+
+#include <botan/if_core.h>
+#include <botan/x509_key.h>
+#include <botan/pkcs8.h>
+
+namespace Botan {
+
+/*************************************************
+* IF Public Key *
+*************************************************/
+class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key
+ {
+ public:
+ bool check_key(RandomNumberGenerator& rng, bool) const;
+
+ const BigInt& get_n() const { return n; }
+ const BigInt& get_e() const { return e; }
+
+ u32bit max_input_bits() const { return (n.bits() - 1); }
+
+ X509_Encoder* x509_encoder() const;
+ X509_Decoder* x509_decoder();
+ protected:
+ virtual void X509_load_hook();
+ BigInt n, e;
+ IF_Core core;
+ };
+
+/*************************************************
+* IF Private Key *
+*************************************************/
+class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey,
+ public virtual Private_Key
+ {
+ public:
+ bool check_key(RandomNumberGenerator& rng, bool) const;
+
+ const BigInt& get_p() const { return p; }
+ const BigInt& get_q() const { return q; }
+ const BigInt& get_d() const { return d; }
+
+ PKCS8_Encoder* pkcs8_encoder() const;
+ PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&);
+ protected:
+ virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false);
+ BigInt d, p, q, d1, d2, c;
+ };
+
+}
+
+#endif
diff --git a/src/pubkey/if_algo/if_core.cpp b/src/pubkey/if_algo/if_core.cpp
new file mode 100644
index 000000000..97cacf9d8
--- /dev/null
+++ b/src/pubkey/if_algo/if_core.cpp
@@ -0,0 +1,85 @@
+/*************************************************
+* IF Algorithm Core Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/if_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;
+
+}
+
+/*************************************************
+* IF_Core Constructor *
+*************************************************/
+IF_Core::IF_Core(const BigInt& e, const BigInt& n)
+ {
+ op = Engine_Core::if_op(e, n, 0, 0, 0, 0, 0, 0);
+ }
+
+
+/*************************************************
+* IF_Core Constructor *
+*************************************************/
+IF_Core::IF_Core(RandomNumberGenerator& rng,
+ const BigInt& e, const BigInt& n, const BigInt& d,
+ const BigInt& p, const BigInt& q,
+ const BigInt& d1, const BigInt& d2, const BigInt& c)
+ {
+ op = Engine_Core::if_op(e, n, d, p, q, d1, d2, c);
+
+ if(BLINDING_BITS)
+ {
+ BigInt k(rng, std::min(n.bits()-1, BLINDING_BITS));
+ blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n);
+ }
+ }
+
+/*************************************************
+* IF_Core Copy Constructor *
+*************************************************/
+IF_Core::IF_Core(const IF_Core& core)
+ {
+ op = 0;
+ if(core.op)
+ op = core.op->clone();
+ blinder = core.blinder;
+ }
+
+/*************************************************
+* IF_Core Assignment Operator *
+*************************************************/
+IF_Core& IF_Core::operator=(const IF_Core& core)
+ {
+ delete op;
+ if(core.op)
+ op = core.op->clone();
+ blinder = core.blinder;
+ return (*this);
+ }
+
+/*************************************************
+* IF Public Operation *
+*************************************************/
+BigInt IF_Core::public_op(const BigInt& i) const
+ {
+ return op->public_op(i);
+ }
+
+/*************************************************
+* IF Private Operation *
+*************************************************/
+BigInt IF_Core::private_op(const BigInt& i) const
+ {
+ return blinder.unblind(op->private_op(blinder.blind(i)));
+ }
+
+}
diff --git a/src/pubkey/if_algo/if_core.h b/src/pubkey/if_algo/if_core.h
new file mode 100644
index 000000000..b6afad950
--- /dev/null
+++ b/src/pubkey/if_algo/if_core.h
@@ -0,0 +1,44 @@
+/*************************************************
+* IF Algorithm Core Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_IF_CORE_H__
+#define BOTAN_IF_CORE_H__
+
+#include <botan/bigint.h>
+#include <botan/blinding.h>
+#include <botan/pk_ops.h>
+
+namespace Botan {
+
+/*************************************************
+* IF Core *
+*************************************************/
+class BOTAN_DLL IF_Core
+ {
+ public:
+ BigInt public_op(const BigInt&) const;
+ BigInt private_op(const BigInt&) const;
+
+ IF_Core& operator=(const IF_Core&);
+
+ IF_Core() { op = 0; }
+ IF_Core(const IF_Core&);
+
+ IF_Core(const BigInt&, const BigInt&);
+
+ IF_Core(RandomNumberGenerator& rng,
+ const BigInt&, const BigInt&,
+ const BigInt&, const BigInt&, const BigInt&,
+ const BigInt&, const BigInt&, const BigInt&);
+
+ ~IF_Core() { delete op; }
+ private:
+ IF_Operation* op;
+ Blinder blinder;
+ };
+
+}
+
+#endif
diff --git a/src/pubkey/if_algo/info.txt b/src/pubkey/if_algo/info.txt
new file mode 100644
index 000000000..af1726414
--- /dev/null
+++ b/src/pubkey/if_algo/info.txt
@@ -0,0 +1,18 @@
+realname "Integer Factorization Algorithms"
+
+define IF_PUBLIC_KEY_FAMILY
+
+load_on required
+
+<add>
+if_algo.cpp
+if_algo.h
+if_core.cpp
+if_core.h
+</add>
+
+<requires>
+asn1
+bigint
+filters
+</requires>