Botan  1.11.10
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Botan::RSA_PrivateKey Class Reference

#include <rsa.h>

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

Public Member Functions

std::string algo_name () const
 
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
 
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
 
 RSA_PrivateKey (const AlgorithmIdentifier &alg_id, const secure_vector< byte > &key_bits, RandomNumberGenerator &rng)
 
 RSA_PrivateKey (RandomNumberGenerator &rng, const BigInt &p, const BigInt &q, const BigInt &e, const BigInt &d=0, const BigInt &n=0)
 
 RSA_PrivateKey (RandomNumberGenerator &rng, size_t bits, size_t exp=65537)
 
std::vector< bytex509_subject_public_key () const
 

Protected Member Functions

void gen_check (RandomNumberGenerator &rng) const
 
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

RSA Private Key

Definition at line 47 of file rsa.h.

Constructor & Destructor Documentation

Botan::RSA_PrivateKey::RSA_PrivateKey ( const AlgorithmIdentifier alg_id,
const secure_vector< byte > &  key_bits,
RandomNumberGenerator rng 
)
inline

Definition at line 53 of file rsa.h.

55  :
56  IF_Scheme_PrivateKey(rng, alg_id, key_bits) {}
Botan::RSA_PrivateKey::RSA_PrivateKey ( RandomNumberGenerator rng,
const BigInt p,
const BigInt q,
const BigInt e,
const BigInt d = 0,
const BigInt n = 0 
)
inline

Construct a private key from the specified parameters.

Parameters
rnga random number generator
pthe first prime
qthe second prime
ethe exponent
dif specified, this has to be d with exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to the constructor to calculate it.
nif specified, this must be n = p * q. Leave it as 0 if you wish to the constructor to calculate it.

Definition at line 70 of file rsa.h.

Botan::RSA_PrivateKey::RSA_PrivateKey ( RandomNumberGenerator rng,
size_t  bits,
size_t  exp = 65537 
)

Create a new private key with the specified bit length

Parameters
rngthe random number generator to use
bitsthe desired bit length of the private key
expthe public exponent to be used

Definition at line 19 of file rsa.cpp.

References Botan::RSA_PublicKey::algo_name(), Botan::BigInt::bits(), Botan::IF_Scheme_PrivateKey::c, Botan::IF_Scheme_PrivateKey::d, Botan::IF_Scheme_PrivateKey::d1, Botan::IF_Scheme_PrivateKey::d2, Botan::IF_Scheme_PublicKey::e, exp, Botan::Private_Key::gen_check(), Botan::inverse_mod(), Botan::lcm(), Botan::IF_Scheme_PublicKey::n, Botan::IF_Scheme_PrivateKey::p, Botan::IF_Scheme_PrivateKey::q, Botan::random_prime(), and Botan::ASN1::to_string().

21  {
22  if(bits < 1024)
23  throw Invalid_Argument(algo_name() + ": Can't make a key that is only " +
24  std::to_string(bits) + " bits long");
25  if(exp < 3 || exp % 2 == 0)
26  throw Invalid_Argument(algo_name() + ": Invalid encryption exponent");
27 
28  e = exp;
29 
30  do
31  {
32  p = random_prime(rng, (bits + 1) / 2, e);
33  q = random_prime(rng, bits - p.bits(), e);
34  n = p * q;
35  } while(n.bits() != bits);
36 
37  d = inverse_mod(e, lcm(p - 1, q - 1));
38  d1 = d % (p - 1);
39  d2 = d % (q - 1);
40  c = inverse_mod(q, p);
41 
42  gen_check(rng);
43  }
std::string algo_name() const
Definition: rsa.h:24
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:46
size_t bits() const
Definition: bigint.cpp:233
GMP_MPZ exp
Definition: gmp_powm.cpp:29
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:128
void gen_check(RandomNumberGenerator &rng) const
Definition: pk_keys.cpp:49
BigInt lcm(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:72
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo)
Definition: make_prm.cpp:17

Member Function Documentation

std::string Botan::RSA_PublicKey::algo_name ( ) const
inlinevirtualinherited

Get the name of the underlying public key scheme.

Returns
name of the public key scheme

Implements Botan::Public_Key.

Definition at line 24 of file rsa.h.

Referenced by RSA_PrivateKey().

24 { return "RSA"; }
AlgorithmIdentifier Botan::IF_Scheme_PublicKey::algorithm_identifier ( ) const
virtualinherited
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.

22  {
23  return AlgorithmIdentifier(get_oid(),
25  }
virtual OID get_oid() const
Definition: pk_keys.cpp:17
bool Botan::RSA_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_PrivateKey.

Definition at line 48 of file rsa.cpp.

References Botan::IF_Scheme_PrivateKey::check_key(), Botan::IF_Scheme_PrivateKey::d, Botan::IF_Scheme_PublicKey::e, Botan::lcm(), Botan::IF_Scheme_PrivateKey::p, Botan::IF_Scheme_PrivateKey::q, and Botan::KeyPair::signature_consistency_check().

49  {
50  if(!IF_Scheme_PrivateKey::check_key(rng, strong))
51  return false;
52 
53  if(!strong)
54  return true;
55 
56  if((e * d) % lcm(p - 1, q - 1) != 1)
57  return false;
58 
59  return KeyPair::signature_consistency_check(rng, *this, "EMSA4(SHA-1)");
60  }
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.cpp:47
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: if_algo.cpp:127
BigInt lcm(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:72
size_t Botan::IF_Scheme_PublicKey::estimated_strength ( ) const
overridevirtualinherited

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.

17  {
18  return dl_work_factor(n.bits());
19  }
size_t bits() const
Definition: bigint.cpp:233
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:14
void Botan::Private_Key::gen_check ( RandomNumberGenerator rng) const
protectedinherited

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(), RSA_PrivateKey(), and Botan::RW_PrivateKey::RW_PrivateKey().

50  {
51  if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE))
52  throw Self_Test_Failure(algo_name() + " private key generation failed");
53  }
virtual bool check_key(RandomNumberGenerator &rng, bool strong) const =0
virtual std::string algo_name() const =0
const BigInt& Botan::IF_Scheme_PrivateKey::get_c ( ) const
inlineinherited

Definition at line 94 of file if_algo.h.

94 { return c; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_d ( ) const
inlineinherited

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

Returns
d

Definition at line 92 of file if_algo.h.

92 { return d; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_d1 ( ) const
inlineinherited

Definition at line 95 of file if_algo.h.

95 { return d1; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_d2 ( ) const
inlineinherited

Definition at line 96 of file if_algo.h.

96 { return d2; }
const BigInt& Botan::IF_Scheme_PublicKey::get_e ( ) const
inlineinherited
Returns
public exponent

Definition at line 44 of file if_algo.h.

44 { return e; }
const BigInt& Botan::IF_Scheme_PublicKey::get_n ( ) const
inlineinherited
Returns
public modulus

Definition at line 39 of file if_algo.h.

39 { return n; }
OID Botan::Public_Key::get_oid ( ) const
virtualinherited

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::IF_Scheme_PublicKey::algorithm_identifier(), Botan::GOST_3410_PublicKey::algorithm_identifier(), Botan::EC_PublicKey::algorithm_identifier(), and Botan::McEliece_PublicKey::algorithm_identifier().

18  {
19  try {
20  return OIDS::lookup(algo_name());
21  }
22  catch(Lookup_Error)
23  {
24  throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs");
25  }
26  }
virtual std::string algo_name() const =0
std::string lookup(const OID &oid)
Definition: oids.cpp:111
const BigInt& Botan::IF_Scheme_PrivateKey::get_p ( ) const
inlineinherited

Get the first prime p.

Returns
prime p

Definition at line 80 of file if_algo.h.

80 { return p; }
const BigInt& Botan::IF_Scheme_PrivateKey::get_q ( ) const
inlineinherited

Get the second prime q.

Returns
prime q

Definition at line 86 of file if_algo.h.

86 { return q; }
void Botan::Public_Key::load_check ( RandomNumberGenerator rng) const
protectedvirtualinherited

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().

32  {
33  if(!check_key(rng, BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD))
34  throw Invalid_Argument(algo_name() + ": Invalid public key");
35  }
virtual bool check_key(RandomNumberGenerator &rng, bool strong) const =0
virtual std::string algo_name() const =0
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
void Botan::Private_Key::load_check ( RandomNumberGenerator rng) const
protectedvirtualinherited

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::DH_PrivateKey::DH_PrivateKey(), Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(), and Botan::NR_PrivateKey::NR_PrivateKey().

41  {
42  if(!check_key(rng, BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD))
43  throw Invalid_Argument(algo_name() + ": Invalid private key");
44  }
virtual bool check_key(RandomNumberGenerator &rng, bool strong) const =0
virtual std::string algo_name() const =0
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
size_t Botan::IF_Scheme_PublicKey::max_input_bits ( ) const
inlinevirtualinherited

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.

46 { return (n.bits() - 1); }
size_t bits() const
Definition: bigint.cpp:233
virtual size_t Botan::Public_Key::message_part_size ( ) const
inlinevirtualinherited

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.

67 { return 0; }
virtual size_t Botan::Public_Key::message_parts ( ) const
inlinevirtualinherited

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().

61 { return 1; }
virtual AlgorithmIdentifier Botan::Private_Key::pkcs8_algorithm_identifier ( ) const
inlinevirtualinherited
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().

110  { return algorithm_identifier(); }
virtual AlgorithmIdentifier algorithm_identifier() const =0
secure_vector< byte > Botan::IF_Scheme_PrivateKey::pkcs8_private_key ( ) const
virtualinherited
Returns
PKCS #8 private key encoding for this key object

Implements Botan::Private_Key.

Definition at line 58 of file if_algo.cpp.

References Botan::IF_Scheme_PrivateKey::c, Botan::IF_Scheme_PrivateKey::d, Botan::IF_Scheme_PrivateKey::d1, Botan::IF_Scheme_PrivateKey::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, Botan::IF_Scheme_PrivateKey::p, Botan::IF_Scheme_PrivateKey::q, Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

59  {
60  return DER_Encoder()
61  .start_cons(SEQUENCE)
62  .encode(static_cast<size_t>(0))
63  .encode(n)
64  .encode(e)
65  .encode(d)
66  .encode(p)
67  .encode(q)
68  .encode(d1)
69  .encode(d2)
70  .encode(c)
71  .end_cons()
72  .get_contents();
73  }
std::vector< byte > Botan::IF_Scheme_PublicKey::x509_subject_public_key ( ) const
virtualinherited
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().

28  {
29  return DER_Encoder()
30  .start_cons(SEQUENCE)
31  .encode(n)
32  .encode(e)
33  .end_cons()
34  .get_contents_unlocked();
35  }

Member Data Documentation

BigInt Botan::IF_Scheme_PrivateKey::c
protectedinherited
BigInt Botan::IF_Scheme_PrivateKey::d
protectedinherited
BigInt Botan::IF_Scheme_PrivateKey::d1
protectedinherited
BigInt Botan::IF_Scheme_PrivateKey::d2
protectedinherited
BigInt Botan::IF_Scheme_PublicKey::e
protectedinherited
BigInt Botan::IF_Scheme_PublicKey::n
protectedinherited
BigInt Botan::IF_Scheme_PrivateKey::p
protectedinherited
BigInt Botan::IF_Scheme_PrivateKey::q
protectedinherited

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