Botan  1.11.15
Public Member Functions | Protected Member Functions | Protected Attributes
Botan::IF_Scheme_PrivateKey Class Reference

#include <if_algo.h>

Inheritance diagram for Botan::IF_Scheme_PrivateKey:
Botan::IF_Scheme_PublicKey Botan::Private_Key Botan::Public_Key Botan::Public_Key Botan::RSA_PrivateKey Botan::RW_PrivateKey

List of all members.

Public Member Functions

virtual std::string algo_name () const =0
AlgorithmIdentifier algorithm_identifier () const
bool check_key (RandomNumberGenerator &rng, bool) const
size_t estimated_strength () const override
const BigIntget_c () const
const BigIntget_d () const
const BigIntget_d1 () const
const BigIntget_d2 () const
const BigIntget_e () const
const BigIntget_n () const
virtual OID get_oid () const
const BigIntget_p () const
const BigIntget_q () const
 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)
size_t max_input_bits () const
virtual size_t message_part_size () const
virtual size_t message_parts () const
virtual AlgorithmIdentifier pkcs8_algorithm_identifier () const
secure_vector< bytepkcs8_private_key () const
std::vector< bytex509_subject_public_key () const

Protected Member Functions

void gen_check (RandomNumberGenerator &rng) const
 IF_Scheme_PrivateKey ()
virtual void load_check (RandomNumberGenerator &rng) const
void load_check (RandomNumberGenerator &rng) const

Protected Attributes

BigInt c
BigInt d
BigInt d1
BigInt d2
BigInt e
BigInt n
BigInt p
BigInt q

Detailed Description

This class represents public keys of integer factorization based (IF) public key schemes.

Definition at line 60 of file if_algo.h.


Constructor & Destructor Documentation

Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey ( RandomNumberGenerator rng,
const BigInt prime1,
const BigInt prime2,
const BigInt exp,
const BigInt d_exp,
const BigInt mod 
)

Definition at line 95 of file if_algo.cpp.

References c, d, d1, d2, Botan::IF_Scheme_PublicKey::e, Botan::inverse_mod(), Botan::BigInt::is_even(), Botan::BigInt::is_nonzero(), Botan::lcm(), Botan::Private_Key::load_check(), Botan::IF_Scheme_PublicKey::n, p, and q.

   {
   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);
   }
Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey ( RandomNumberGenerator rng,
const AlgorithmIdentifier alg_id,
const secure_vector< byte > &  key_bits 
)

Definition at line 75 of file if_algo.cpp.

References c, d, d1, d2, Botan::BER::decode(), Botan::BER_Decoder::decode_and_check(), Botan::IF_Scheme_PublicKey::e, Botan::Private_Key::load_check(), Botan::IF_Scheme_PublicKey::n, p, q, Botan::SEQUENCE, and Botan::BER_Decoder::start_cons().

   {
   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);
   }

Definition at line 101 of file if_algo.h.

{}

Member Function Documentation

virtual std::string Botan::Public_Key::algo_name ( ) const [pure virtual, inherited]
Returns:
X.509 AlgorithmIdentifier for this key

Implements Botan::Public_Key.

Definition at line 21 of file if_algo.cpp.

References Botan::Public_Key::get_oid(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

   {
   return AlgorithmIdentifier(get_oid(),
                              AlgorithmIdentifier::USE_NULL_PARAM);
   }
bool Botan::IF_Scheme_PrivateKey::check_key ( RandomNumberGenerator rng,
bool  strong 
) const [virtual]

Test the key values for consistency.

Parameters:
rngrng to use
strongwhether to perform strong and lengthy version of the test
Returns:
true if the test is passed

Reimplemented from Botan::IF_Scheme_PublicKey.

Reimplemented in Botan::RW_PrivateKey, and Botan::RSA_PrivateKey.

Definition at line 127 of file if_algo.cpp.

References c, d, d1, d2, Botan::IF_Scheme_PublicKey::e, Botan::inverse_mod(), Botan::BigInt::is_even(), Botan::is_prime(), Botan::IF_Scheme_PublicKey::n, p, and q.

Referenced by Botan::RSA_PrivateKey::check_key(), and Botan::RW_PrivateKey::check_key().

   {
   if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
      return false;

   if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
      return false;

   const size_t prob = (strong) ? 56 : 12;

   if(!is_prime(p, rng, prob) || !is_prime(q, rng, prob))
      return false;
   return true;
   }
size_t Botan::IF_Scheme_PublicKey::estimated_strength ( ) const [override, virtual, inherited]

Return the estimated strength of the underlying key against the best currently known attack. Note that this ignores anything but pure attacks against the key itself and do not take into account padding schemes, usage mistakes, etc which might reduce the strength. However it does suffice to provide an upper bound.

Returns:
estimated strength in bits

Implements Botan::Public_Key.

Definition at line 16 of file if_algo.cpp.

References Botan::BigInt::bits(), Botan::dl_work_factor(), and Botan::IF_Scheme_PublicKey::n.

   {
   return dl_work_factor(n.bits());
   }
void Botan::Private_Key::gen_check ( RandomNumberGenerator rng) const [protected, inherited]

Self-test after generating a key

Parameters:
rnga random number generator

Definition at line 49 of file pk_keys.cpp.

References Botan::Public_Key::algo_name(), and Botan::Public_Key::check_key().

Referenced by Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), Botan::NR_PrivateKey::NR_PrivateKey(), Botan::RSA_PrivateKey::RSA_PrivateKey(), and Botan::RW_PrivateKey::RW_PrivateKey().

   {
   if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE))
      throw Self_Test_Failure(algo_name() + " private key generation failed");
   }
const BigInt& Botan::IF_Scheme_PrivateKey::get_c ( ) const [inline]

Definition at line 94 of file if_algo.h.

References c.

{ return c; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_d ( ) const [inline]

Get d with exp * d = 1 mod (p - 1, q - 1).

Returns:
d

Definition at line 92 of file if_algo.h.

{ return d; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_d1 ( ) const [inline]

Definition at line 95 of file if_algo.h.

{ return d1; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_d2 ( ) const [inline]

Definition at line 96 of file if_algo.h.

{ return d2; }
const BigInt& Botan::IF_Scheme_PublicKey::get_e ( ) const [inline, inherited]
Returns:
public exponent

Definition at line 44 of file if_algo.h.

References e.

{ return e; }
const BigInt& Botan::IF_Scheme_PublicKey::get_n ( ) const [inline, inherited]
Returns:
public modulus

Definition at line 39 of file if_algo.h.

References n.

{ return n; }
OID Botan::Public_Key::get_oid ( ) const [virtual, inherited]

Get the OID of the underlying public key scheme.

Returns:
OID of the public key scheme

Definition at line 17 of file pk_keys.cpp.

References Botan::Public_Key::algo_name(), and Botan::OIDS::lookup().

Referenced by Botan::DL_Scheme_PublicKey::algorithm_identifier(), Botan::Curve25519_PublicKey::algorithm_identifier(), Botan::IF_Scheme_PublicKey::algorithm_identifier(), Botan::GOST_3410_PublicKey::algorithm_identifier(), Botan::EC_PublicKey::algorithm_identifier(), and Botan::McEliece_PublicKey::algorithm_identifier().

   {
   try {
      return OIDS::lookup(algo_name());
      }
   catch(Lookup_Error)
      {
      throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs");
      }
   }
const BigInt& Botan::IF_Scheme_PrivateKey::get_p ( ) const [inline]

Get the first prime p.

Returns:
prime p

Definition at line 80 of file if_algo.h.

{ return p; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_q ( ) const [inline]

Get the second prime q.

Returns:
prime q

Definition at line 86 of file if_algo.h.

References q.

{ return q; }
void Botan::Public_Key::load_check ( RandomNumberGenerator rng) const [protected, virtual, inherited]

Self-test after loading a key

Parameters:
rnga random number generator

Reimplemented in Botan::Private_Key.

Definition at line 31 of file pk_keys.cpp.

References Botan::Public_Key::algo_name(), and Botan::Public_Key::check_key().

   {
   if(!check_key(rng, BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD))
      throw Invalid_Argument(algo_name() + ": Invalid public key");
   }
void Botan::Private_Key::load_check ( RandomNumberGenerator rng) const [protected, virtual, inherited]

Self-test after loading a key

Parameters:
rnga random number generator

Reimplemented from Botan::Public_Key.

Definition at line 40 of file pk_keys.cpp.

References Botan::Public_Key::algo_name(), and Botan::Public_Key::check_key().

Referenced by Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), IF_Scheme_PrivateKey(), and Botan::NR_PrivateKey::NR_PrivateKey().

   {
   if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD))
      throw Invalid_Argument(algo_name() + ": Invalid private key");
   }
size_t Botan::IF_Scheme_PublicKey::max_input_bits ( ) const [inline, virtual, inherited]

Get the maximum message size in bits supported by this public key.

Returns:
maximum message size in bits

Implements Botan::Public_Key.

Definition at line 46 of file if_algo.h.

References n.

{ return (n.bits() - 1); }
virtual size_t Botan::Public_Key::message_part_size ( ) const [inline, virtual, inherited]

Find out the message part size supported by this scheme/key.

Returns:
size of the message parts in bits

Reimplemented in Botan::GOST_3410_PublicKey, Botan::ECDSA_PublicKey, Botan::NR_PublicKey, and Botan::DSA_PublicKey.

Definition at line 67 of file pk_keys.h.

{ return 0; }
virtual size_t Botan::Public_Key::message_parts ( ) const [inline, virtual, inherited]

Find out the number of message parts supported by this scheme.

Returns:
number of message parts

Reimplemented in Botan::GOST_3410_PublicKey, Botan::ECDSA_PublicKey, Botan::NR_PublicKey, and Botan::DSA_PublicKey.

Definition at line 61 of file pk_keys.h.

Referenced by Botan::EAC_Signed_Object::check_signature(), Botan::X509_Object::check_signature(), and Botan::choose_sig_format().

{ return 1; }
virtual AlgorithmIdentifier Botan::Private_Key::pkcs8_algorithm_identifier ( ) const [inline, virtual, inherited]
Returns:
PKCS #8 AlgorithmIdentifier for this key Might be different from the X.509 identifier, but normally is not

Reimplemented in Botan::GOST_3410_PrivateKey.

Definition at line 109 of file pk_keys.h.

Referenced by Botan::PKCS8::BER_encode().

         { return algorithm_identifier(); }
secure_vector< byte > Botan::IF_Scheme_PrivateKey::pkcs8_private_key ( ) const [virtual]
Returns:
PKCS #8 private key encoding for this key object

Implements Botan::Private_Key.

Definition at line 58 of file if_algo.cpp.

References c, d, d1, d2, Botan::IF_Scheme_PublicKey::e, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents(), Botan::IF_Scheme_PublicKey::n, p, q, Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

   {
   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();
   }
std::vector< byte > Botan::IF_Scheme_PublicKey::x509_subject_public_key ( ) const [virtual, inherited]
Returns:
X.509 subject key encoding for this key object

Implements Botan::Public_Key.

Definition at line 27 of file if_algo.cpp.

References Botan::IF_Scheme_PublicKey::e, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents_unlocked(), Botan::IF_Scheme_PublicKey::n, Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

   {
   return DER_Encoder()
      .start_cons(SEQUENCE)
         .encode(n)
         .encode(e)
      .end_cons()
      .get_contents_unlocked();
   }

Member Data Documentation

BigInt Botan::IF_Scheme_PublicKey::e [protected, inherited]
BigInt Botan::IF_Scheme_PublicKey::n [protected, inherited]

The documentation for this class was generated from the following files: