aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey/if_algo
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-10 03:41:59 +0000
committerlloyd <[email protected]>2014-01-10 03:41:59 +0000
commit6894dca64c04936d07048c0e8cbf7e25858548c3 (patch)
tree5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/pubkey/if_algo
parent9efa3be92442afb3d0b69890a36c7f122df18eda (diff)
Move lib into src
Diffstat (limited to 'src/lib/pubkey/if_algo')
-rw-r--r--src/lib/pubkey/if_algo/if_algo.cpp143
-rw-r--r--src/lib/pubkey/if_algo/if_algo.h108
-rw-r--r--src/lib/pubkey/if_algo/info.txt10
3 files changed, 261 insertions, 0 deletions
diff --git a/src/lib/pubkey/if_algo/if_algo.cpp b/src/lib/pubkey/if_algo/if_algo.cpp
new file mode 100644
index 000000000..f6aeb69db
--- /dev/null
+++ b/src/lib/pubkey/if_algo/if_algo.cpp
@@ -0,0 +1,143 @@
+/*
+* IF Scheme
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/if_algo.h>
+#include <botan/numthry.h>
+#include <botan/workfactor.h>
+#include <botan/der_enc.h>
+#include <botan/ber_dec.h>
+
+namespace Botan {
+
+size_t IF_Scheme_PublicKey::estimated_strength() const
+ {
+ return dl_work_factor(n.bits());
+ }
+
+AlgorithmIdentifier IF_Scheme_PublicKey::algorithm_identifier() const
+ {
+ return AlgorithmIdentifier(get_oid(),
+ AlgorithmIdentifier::USE_NULL_PARAM);
+ }
+
+std::vector<byte> IF_Scheme_PublicKey::x509_subject_public_key() const
+ {
+ return DER_Encoder()
+ .start_cons(SEQUENCE)
+ .encode(n)
+ .encode(e)
+ .end_cons()
+ .get_contents_unlocked();
+ }
+
+IF_Scheme_PublicKey::IF_Scheme_PublicKey(const AlgorithmIdentifier&,
+ const secure_vector<byte>& key_bits)
+ {
+ BER_Decoder(key_bits)
+ .start_cons(SEQUENCE)
+ .decode(n)
+ .decode(e)
+ .verify_end()
+ .end_cons();
+ }
+
+/*
+* 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;
+ }
+
+secure_vector<byte> IF_Scheme_PrivateKey::pkcs8_private_key() const
+ {
+ return DER_Encoder()
+ .start_cons(SEQUENCE)
+ .encode(static_cast<size_t>(0))
+ .encode(n)
+ .encode(e)
+ .encode(d)
+ .encode(p)
+ .encode(q)
+ .encode(d1)
+ .encode(d2)
+ .encode(c)
+ .end_cons()
+ .get_contents();
+ }
+
+IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng,
+ const AlgorithmIdentifier&,
+ const secure_vector<byte>& key_bits)
+ {
+ BER_Decoder(key_bits)
+ .start_cons(SEQUENCE)
+ .decode_and_check<size_t>(0, "Unknown PKCS #1 key format version")
+ .decode(n)
+ .decode(e)
+ .decode(d)
+ .decode(p)
+ .decode(q)
+ .decode(d1)
+ .decode(d2)
+ .decode(c)
+ .end_cons();
+
+ load_check(rng);
+ }
+
+IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng,
+ const BigInt& prime1,
+ const BigInt& prime2,
+ const BigInt& exp,
+ const BigInt& d_exp,
+ const BigInt& mod)
+ {
+ p = prime1;
+ q = prime2;
+ e = exp;
+ d = d_exp;
+ n = mod.is_nonzero() ? mod : p * q;
+
+ if(d == 0)
+ {
+ BigInt inv_for_d = lcm(p - 1, q - 1);
+ if(e.is_even())
+ inv_for_d >>= 1;
+
+ d = inverse_mod(e, inv_for_d);
+ }
+
+ d1 = d % (p - 1);
+ d2 = d % (q - 1);
+ c = inverse_mod(q, p);
+
+ load_check(rng);
+ }
+
+/*
+* 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/lib/pubkey/if_algo/if_algo.h b/src/lib/pubkey/if_algo/if_algo.h
new file mode 100644
index 000000000..7dd6d19f0
--- /dev/null
+++ b/src/lib/pubkey/if_algo/if_algo.h
@@ -0,0 +1,108 @@
+/*
+* IF Scheme
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_IF_ALGO_H__
+#define BOTAN_IF_ALGO_H__
+
+#include <botan/bigint.h>
+#include <botan/x509_key.h>
+#include <botan/pkcs8.h>
+
+namespace Botan {
+
+/**
+* This class represents public keys
+* of integer factorization based (IF) public key schemes.
+*/
+class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key
+ {
+ public:
+ IF_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
+ const secure_vector<byte>& key_bits);
+
+ IF_Scheme_PublicKey(const BigInt& n, const BigInt& e) :
+ n(n), e(e) {}
+
+ bool check_key(RandomNumberGenerator& rng, bool) const;
+
+ AlgorithmIdentifier algorithm_identifier() const;
+
+ std::vector<byte> x509_subject_public_key() const;
+
+ /**
+ * @return public modulus
+ */
+ const BigInt& get_n() const { return n; }
+
+ /**
+ * @return public exponent
+ */
+ const BigInt& get_e() const { return e; }
+
+ size_t max_input_bits() const { return (n.bits() - 1); }
+
+ size_t estimated_strength() const override;
+
+ protected:
+ IF_Scheme_PublicKey() {}
+
+ BigInt n, e;
+ };
+
+/**
+* This class represents public keys
+* of integer factorization based (IF) public key schemes.
+*/
+class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey,
+ public virtual Private_Key
+ {
+ public:
+
+ IF_Scheme_PrivateKey(RandomNumberGenerator& rng,
+ const BigInt& prime1, const BigInt& prime2,
+ const BigInt& exp, const BigInt& d_exp,
+ const BigInt& mod);
+
+ IF_Scheme_PrivateKey(RandomNumberGenerator& rng,
+ const AlgorithmIdentifier& alg_id,
+ const secure_vector<byte>& key_bits);
+
+ bool check_key(RandomNumberGenerator& rng, bool) const;
+
+ /**
+ * Get the first prime p.
+ * @return prime p
+ */
+ const BigInt& get_p() const { return p; }
+
+ /**
+ * Get the second prime q.
+ * @return prime q
+ */
+ const BigInt& get_q() const { return q; }
+
+ /**
+ * Get d with exp * d = 1 mod (p - 1, q - 1).
+ * @return d
+ */
+ const BigInt& get_d() const { return d; }
+
+ const BigInt& get_c() const { return c; }
+ const BigInt& get_d1() const { return d1; }
+ const BigInt& get_d2() const { return d2; }
+
+ secure_vector<byte> pkcs8_private_key() const;
+
+ protected:
+ IF_Scheme_PrivateKey() {}
+
+ BigInt d, p, q, d1, d2, c;
+ };
+
+}
+
+#endif
diff --git a/src/lib/pubkey/if_algo/info.txt b/src/lib/pubkey/if_algo/info.txt
new file mode 100644
index 000000000..e4d2dbb5e
--- /dev/null
+++ b/src/lib/pubkey/if_algo/info.txt
@@ -0,0 +1,10 @@
+define IF_PUBLIC_KEY_FAMILY 20131128
+
+load_on dep
+
+<requires>
+asn1
+bigint
+libstate
+numbertheory
+</requires>