Botan 3.9.0
Crypto and TLS for C&
Botan::RSA_PublicKey Class Reference

#include <rsa.h>

Inheritance diagram for Botan::RSA_PublicKey:
Botan::Public_Key Botan::Asymmetric_Key Botan::RSA_PrivateKey Botan::TPM2::RSA_PrivateKey Botan::TPM2::RSA_PublicKey

Public Member Functions

virtual Signature_Format _default_x509_signature_format () const
virtual std::optional< size_t > _signature_element_size_for_DER_encoding () const
std::string algo_name () const override
AlgorithmIdentifier algorithm_identifier () const override
bool check_key (RandomNumberGenerator &rng, bool strong) const override
std::unique_ptr< PK_Ops::Encryptioncreate_encryption_op (RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
std::unique_ptr< PK_Ops::KEM_Encryptioncreate_kem_encryption_op (std::string_view params, std::string_view provider) const override
std::unique_ptr< PK_Ops::Verificationcreate_verification_op (std::string_view params, std::string_view provider) const override
std::unique_ptr< PK_Ops::Verificationcreate_x509_verification_op (const AlgorithmIdentifier &alg_id, std::string_view provider) const override
Signature_Format default_x509_signature_format () const
size_t estimated_strength () const override
std::string fingerprint_public (std::string_view alg="SHA-256") const
std::unique_ptr< Private_Keygenerate_another (RandomNumberGenerator &rng) const override
const BigIntget_e () const
const BigIntget_int_field (std::string_view field) const override
const BigIntget_n () const
OID get_oid () const
size_t key_length () const override
size_t message_part_size () const
size_t message_parts () const
virtual OID object_identifier () const
std::shared_ptr< const RSA_Public_Data > public_data () const
std::vector< uint8_t > public_key_bits () const override
std::vector< uint8_t > raw_public_key_bits () const override
 RSA_PublicKey (const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
 RSA_PublicKey (const BigInt &n, const BigInt &e)
std::vector< uint8_t > subject_public_key () const
bool supports_operation (PublicKeyOperation op) const override

Protected Member Functions

void init (BigInt &&n, BigInt &&e)
 RSA_PublicKey ()=default

Protected Attributes

std::shared_ptr< const RSA_Public_Data > m_public

Detailed Description

RSA Public Key

Definition at line 25 of file rsa.h.

Constructor & Destructor Documentation

◆ RSA_PublicKey() [1/3]

Botan::RSA_PublicKey::RSA_PublicKey ( const AlgorithmIdentifier & alg_id,
std::span< const uint8_t > key_bits )

Load a public key.

Parameters
alg_idthe X.509 algorithm identifier
key_bitsDER encoded public key bits

Definition at line 159 of file rsa.cpp.

159 {
160 BigInt n;
161 BigInt e;
162 BER_Decoder(key_bits).start_sequence().decode(n).decode(e).end_cons();
163
164 init(std::move(n), std::move(e));
165}
void init(BigInt &&n, BigInt &&e)
Definition rsa.cpp:152

References Botan::BER_Decoder::decode(), Botan::BER_Decoder::end_cons(), init(), and Botan::BER_Decoder::start_sequence().

◆ RSA_PublicKey() [2/3]

Botan::RSA_PublicKey::RSA_PublicKey ( const BigInt & n,
const BigInt & e )

Create a public key.

  • n the modulus
  • e the exponent

Definition at line 172 of file rsa.cpp.

172 {
173 BigInt n = modulus;
174 BigInt e = exponent;
175 init(std::move(n), std::move(e));
176}

References init().

◆ RSA_PublicKey() [3/3]

Botan::RSA_PublicKey::RSA_PublicKey ( )
protecteddefault

References init().

Member Function Documentation

◆ _default_x509_signature_format()

Signature_Format Botan::Asymmetric_Key::_default_x509_signature_format ( ) const
virtualinherited

◆ _signature_element_size_for_DER_encoding()

virtual std::optional< size_t > Botan::Asymmetric_Key::_signature_element_size_for_DER_encoding ( ) const
inlinevirtualinherited

Certain signatures schemes such as ECDSA have more than one element, and certain unfortunate protocols decided the thing to do was not concatenate them as normally done, but instead DER encode each of the elements as independent values.

If this returns a value x then the signature is checked to be exactly 2*x bytes and split in half for DER encoding.

Reimplemented in Botan::DSA_PublicKey, Botan::ECDSA_PublicKey, Botan::ECGDSA_PublicKey, Botan::ECKCDSA_PublicKey, Botan::GOST_3410_PublicKey, and Botan::SM2_PublicKey.

Definition at line 137 of file pk_keys.h.

137{ return {}; }

Referenced by _default_x509_signature_format(), Botan::Public_Key::message_part_size(), Botan::Public_Key::message_parts(), Botan::PK_Verifier::PK_Verifier(), and Botan::PK_Verifier::PK_Verifier().

◆ algo_name()

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

Get the name of the underlying public key scheme.

Returns
name of the public key scheme

Implements Botan::Asymmetric_Key.

Definition at line 41 of file rsa.h.

41{ return "RSA"; }

Referenced by Botan::RSA_PrivateKey::create_decryption_op(), create_encryption_op(), Botan::RSA_PrivateKey::create_kem_decryption_op(), create_kem_encryption_op(), Botan::RSA_PrivateKey::create_signature_op(), create_verification_op(), and create_x509_verification_op().

◆ algorithm_identifier()

AlgorithmIdentifier Botan::RSA_PublicKey::algorithm_identifier ( ) const
overridevirtual
Returns
X.509 AlgorithmIdentifier for this key

Implements Botan::Public_Key.

Definition at line 186 of file rsa.cpp.

186 {
187 return AlgorithmIdentifier(object_identifier(), AlgorithmIdentifier::USE_NULL_PARAM);
188}
virtual OID object_identifier() const
Definition pk_keys.cpp:22

References Botan::Asymmetric_Key::object_identifier(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

Referenced by Botan::TPM2::RSA_PrivateKey::public_key().

◆ check_key()

bool Botan::RSA_PublicKey::check_key ( RandomNumberGenerator & rng,
bool strong ) const
overridevirtual

Implements Botan::Asymmetric_Key.

Definition at line 205 of file rsa.cpp.

205 {
206 if(get_n() < 35 || get_n().is_even() || get_e() < 3 || get_e().is_even()) {
207 return false;
208 }
209 return true;
210}
const BigInt & get_n() const
Definition rsa.cpp:144
const BigInt & get_e() const
Definition rsa.cpp:148

References get_e(), and get_n().

◆ create_encryption_op()

std::unique_ptr< PK_Ops::Encryption > Botan::RSA_PublicKey::create_encryption_op ( RandomNumberGenerator & rng,
std::string_view params,
std::string_view provider ) const
overridevirtual

This is an internal library function exposed on key types. In almost all cases applications should use wrappers in pubkey.h

Return an encryption operation for this key/params or throw

Parameters
rnga random number generator. The PK_Op may maintain a reference to the RNG and use it many times. The rng must outlive any operations which reference it.
paramsadditional parameters
providerthe provider to use

Reimplemented from Botan::Public_Key.

Reimplemented in Botan::TPM2::RSA_PublicKey.

Definition at line 781 of file rsa.cpp.

783 {
784 if(provider == "base" || provider.empty()) {
785 return std::make_unique<RSA_Encryption_Operation>(*this, params);
786 }
787 throw Provider_Not_Found(algo_name(), provider);
788}
std::string algo_name() const override
Definition rsa.h:41

References algo_name().

◆ create_kem_encryption_op()

std::unique_ptr< PK_Ops::KEM_Encryption > Botan::RSA_PublicKey::create_kem_encryption_op ( std::string_view params,
std::string_view provider ) const
overridevirtual

This is an internal library function exposed on key types. In almost all cases applications should use wrappers in pubkey.h

Return a KEM encryption operation for this key/params or throw

Parameters
paramsadditional parameters
providerthe provider to use

Reimplemented from Botan::Public_Key.

Definition at line 790 of file rsa.cpp.

791 {
792 if(provider == "base" || provider.empty()) {
793 return std::make_unique<RSA_KEM_Encryption_Operation>(*this, params);
794 }
795 throw Provider_Not_Found(algo_name(), provider);
796}

References algo_name().

◆ create_verification_op()

std::unique_ptr< PK_Ops::Verification > Botan::RSA_PublicKey::create_verification_op ( std::string_view params,
std::string_view provider ) const
overridevirtual

This is an internal library function exposed on key types. In all cases applications should use wrappers in pubkey.h

Return a verification operation for this key/params or throw

Parameters
paramsadditional parameters
providerthe provider to use

Reimplemented from Botan::Public_Key.

Reimplemented in Botan::TPM2::RSA_PublicKey.

Definition at line 798 of file rsa.cpp.

799 {
800 if(provider == "base" || provider.empty()) {
801 return std::make_unique<RSA_Verify_Operation>(*this, params);
802 }
803
804 throw Provider_Not_Found(algo_name(), provider);
805}

References algo_name().

◆ create_x509_verification_op()

std::unique_ptr< PK_Ops::Verification > Botan::RSA_PublicKey::create_x509_verification_op ( const AlgorithmIdentifier & signature_algorithm,
std::string_view provider ) const
overridevirtual

This is an internal library function exposed on key types. In all cases applications should use wrappers in pubkey.h

Return a verification operation for this combination of key and signature algorithm or throw.

Parameters
signature_algorithmis the X.509 algorithm identifier encoding the padding scheme and hash hash function used in the signature if applicable.
providerthe provider to use

Reimplemented from Botan::Public_Key.

Definition at line 857 of file rsa.cpp.

858 {
859 if(provider == "base" || provider.empty()) {
860 return std::make_unique<RSA_Verify_Operation>(*this, parse_rsa_signature_algorithm(alg_id));
861 }
862
863 throw Provider_Not_Found(algo_name(), provider);
864}

References algo_name().

◆ default_x509_signature_format()

Signature_Format Botan::Public_Key::default_x509_signature_format ( ) const
inlineinherited

Definition at line 229 of file pk_keys.h.

229 {
231 }
virtual Signature_Format _default_x509_signature_format() const
Definition pk_keys.cpp:30

References Botan::Asymmetric_Key::_default_x509_signature_format(), and default_x509_signature_format().

Referenced by default_x509_signature_format().

◆ estimated_strength()

size_t Botan::RSA_PublicKey::estimated_strength ( ) const
overridevirtual

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::Asymmetric_Key.

Definition at line 182 of file rsa.cpp.

182 {
183 return if_work_factor(key_length());
184}
size_t key_length() const override
Definition rsa.cpp:178
size_t if_work_factor(size_t bits)

References Botan::if_work_factor(), and key_length().

◆ fingerprint_public()

std::string Botan::Public_Key::fingerprint_public ( std::string_view alg = "SHA-256") const
inherited
Returns
Hash of the subject public key

Definition at line 87 of file pk_keys.cpp.

87 {
88 return create_hex_fingerprint(subject_public_key(), hash_algo);
89}
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:56
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, std::string_view hash_name)
Definition pk_keys.cpp:38

References Botan::create_hex_fingerprint(), and subject_public_key().

Referenced by public_key_bits().

◆ generate_another()

std::unique_ptr< Private_Key > Botan::RSA_PublicKey::generate_another ( RandomNumberGenerator & rng) const
overridevirtual

Generate another (cryptographically independent) key pair using the same algorithm parameters as this key. This is most useful for algorithms that support PublicKeyOperation::KeyAgreement to generate a fitting ephemeral key pair. For other key types it might throw Not_Implemented.

Implements Botan::Asymmetric_Key.

Reimplemented in Botan::TPM2::RSA_PublicKey.

Definition at line 140 of file rsa.cpp.

140 {
141 return std::make_unique<RSA_PrivateKey>(rng, m_public->public_modulus_bits(), m_public->get_e().to_u32bit());
142}
std::shared_ptr< const RSA_Public_Data > m_public
Definition rsa.h:91

References m_public.

◆ get_e()

const BigInt & Botan::RSA_PublicKey::get_e ( ) const
Returns
public exponent

Definition at line 148 of file rsa.cpp.

148 {
149 return m_public->get_e();
150}

References m_public.

Referenced by Botan::RSA_PrivateKey::check_key(), check_key(), Botan::RSA_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::public_key(), and public_key_bits().

◆ get_int_field()

const BigInt & Botan::RSA_PublicKey::get_int_field ( std::string_view field) const
overridevirtual

Access an algorithm specific field

If the field is not known for this algorithm, an Invalid_Argument is thrown. The interpretation of the result requires knowledge of which algorithm is involved. For instance for RSA "p" represents one of the secret primes, while for DSA "p" is the public prime.

Some algorithms may not implement this method at all.

This is primarily used to implement the FFI botan_pubkey_get_field and botan_privkey_get_field functions.

TODO(Botan4) Change this to return by value

Reimplemented from Botan::Asymmetric_Key.

Definition at line 130 of file rsa.cpp.

130 {
131 if(field == "n") {
132 return m_public->get_n();
133 } else if(field == "e") {
134 return m_public->get_e();
135 } else {
136 return Public_Key::get_int_field(field);
137 }
138}
virtual const BigInt & get_int_field(std::string_view field) const
Definition pk_keys.cpp:18

References Botan::Asymmetric_Key::get_int_field(), and m_public.

Referenced by Botan::RSA_PrivateKey::get_int_field().

◆ get_n()

const BigInt & Botan::RSA_PublicKey::get_n ( ) const
Returns
public modulus

Definition at line 144 of file rsa.cpp.

144 {
145 return m_public->get_n();
146}

References m_public.

Referenced by Botan::RSA_PrivateKey::check_key(), check_key(), Botan::RSA_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::public_key(), and public_key_bits().

◆ get_oid()

OID Botan::Public_Key::get_oid ( ) const
inlineinherited

Deprecated version of object_identifier

Definition at line 161 of file pk_keys.h.

161{ return this->object_identifier(); }

References get_oid(), and Botan::Asymmetric_Key::object_identifier().

Referenced by get_oid().

◆ init()

void Botan::RSA_PublicKey::init ( BigInt && n,
BigInt && e )
protected

Definition at line 152 of file rsa.cpp.

152 {
153 if(n.is_negative() || n.is_even() || n.bits() < 5 /* n >= 3*5 */ || e.is_negative() || e.is_even()) {
154 throw Decoding_Error("Invalid RSA public key parameters");
155 }
156 m_public = std::make_shared<RSA_Public_Data>(std::move(n), std::move(e));
157}

References m_public.

Referenced by Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::RSA_PrivateKey::RSA_PrivateKey(), RSA_PublicKey(), RSA_PublicKey(), and RSA_PublicKey().

◆ key_length()

size_t Botan::RSA_PublicKey::key_length ( ) const
overridevirtual

Return an integer value best approximating the length of the primary security parameter. For example for RSA this will be the size of the modulus, for ECDSA the size of the ECC group, and for McEliece the size of the code will be returned.

Implements Botan::Public_Key.

Definition at line 178 of file rsa.cpp.

178 {
179 return m_public->public_modulus_bits();
180}

References m_public.

Referenced by estimated_strength().

◆ message_part_size()

size_t Botan::Public_Key::message_part_size ( ) const
inlineinherited

Returns how large each of the message parts refered to by message_parts() is

This function is public but applications should have few reasons to ever call this.

Returns
size of the message parts in bits

Definition at line 221 of file pk_keys.h.

221 {
222 return _signature_element_size_for_DER_encoding().value_or(0);
223 }

References Botan::Asymmetric_Key::_signature_element_size_for_DER_encoding(), and message_part_size().

Referenced by message_part_size().

◆ message_parts()

size_t Botan::Public_Key::message_parts ( ) const
inlineinherited

Returns more than 1 if the output of this algorithm (ciphertext, signature) should be treated as more than one value. This is used for algorithms like DSA and ECDSA, where the (r,s) output pair can be encoded as either a plain binary list or a TLV tagged DER encoding depending on the protocol.

This function is public but applications should have few reasons to ever call this.

Returns
number of message parts

Definition at line 208 of file pk_keys.h.

208 {
210 }

References Botan::Asymmetric_Key::_signature_element_size_for_DER_encoding(), and message_parts().

Referenced by message_parts().

◆ object_identifier()

OID Botan::Asymmetric_Key::object_identifier ( ) const
virtualinherited

Get the OID of the underlying public key scheme.

Returns
OID of the public key scheme

Reimplemented in Botan::Classic_McEliece_PublicKey, Botan::Dilithium_PublicKey, Botan::FrodoKEM_PublicKey, Botan::HSS_LMS_PublicKey, Botan::Kyber_PublicKey, and Botan::SphincsPlus_PublicKey.

Definition at line 22 of file pk_keys.cpp.

22 {
23 try {
25 } catch(Lookup_Error&) {
26 throw Lookup_Error(fmt("Public key algorithm {} has no defined OIDs", algo_name()));
27 }
28}
virtual std::string algo_name() const =0
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:86
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References algo_name(), Botan::fmt(), and Botan::OID::from_string().

Referenced by Botan::DH_PublicKey::algorithm_identifier(), Botan::DSA_PublicKey::algorithm_identifier(), Botan::EC_PublicKey::algorithm_identifier(), Botan::Ed25519_PublicKey::algorithm_identifier(), Botan::Ed448_PublicKey::algorithm_identifier(), Botan::ElGamal_PublicKey::algorithm_identifier(), Botan::GOST_3410_PublicKey::algorithm_identifier(), Botan::McEliece_PublicKey::algorithm_identifier(), Botan::RSA_PublicKey::algorithm_identifier(), Botan::TPM_PrivateKey::algorithm_identifier(), Botan::X25519_PublicKey::algorithm_identifier(), Botan::X448_PublicKey::algorithm_identifier(), Botan::XMSS_PublicKey::algorithm_identifier(), estimated_strength(), and Botan::Public_Key::get_oid().

◆ public_data()

std::shared_ptr< const RSA_Public_Data > Botan::RSA_PublicKey::public_data ( ) const

Definition at line 126 of file rsa.cpp.

126 {
127 return m_public;
128}

References m_public.

◆ public_key_bits()

std::vector< uint8_t > Botan::RSA_PublicKey::public_key_bits ( ) const
overridevirtual
Returns
BER encoded public key bits

Implements Botan::Public_Key.

Definition at line 194 of file rsa.cpp.

194 {
195 std::vector<uint8_t> output;
196 DER_Encoder der(output);
197 der.start_sequence().encode(get_n()).encode(get_e()).end_cons();
198
199 return output;
200}

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), get_e(), get_n(), and Botan::DER_Encoder::start_sequence().

Referenced by Botan::TPM2::RSA_PrivateKey::public_key().

◆ raw_public_key_bits()

std::vector< uint8_t > Botan::RSA_PublicKey::raw_public_key_bits ( ) const
overridevirtual
Returns
binary public key bits, with no additional encoding

For key agreements this is an alias for PK_Key_Agreement_Key::public_value.

Note: some algorithms (for example RSA) do not have an obvious encoding for this value due to having many different values, and thus throw Not_Implemented when invoking this method.

Implements Botan::Public_Key.

Reimplemented in Botan::TPM2::RSA_PublicKey.

Definition at line 190 of file rsa.cpp.

190 {
191 throw Not_Implemented("an RSA public key does not provide a raw binary representation.");
192}

◆ subject_public_key()

std::vector< uint8_t > Botan::Public_Key::subject_public_key ( ) const
inherited
Returns
X.509 subject key encoding for this key object

Definition at line 56 of file pk_keys.cpp.

56 {
57 std::vector<uint8_t> output;
58
59 DER_Encoder(output)
60 .start_sequence()
61 .encode(algorithm_identifier())
63 .end_cons();
64
65 return output;
66}
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::vector< uint8_t > public_key_bits() const =0

References algorithm_identifier(), Botan::BitString, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), public_key_bits(), and Botan::DER_Encoder::start_sequence().

Referenced by Botan::X509::BER_encode(), Botan::PKCS10_Request::create(), fingerprint_public(), Botan::X509::PEM_encode(), and public_key_bits().

◆ supports_operation()

bool Botan::RSA_PublicKey::supports_operation ( PublicKeyOperation op) const
overridevirtual

Return true if this key could be used for the specified type of operation.

Implements Botan::Asymmetric_Key.

Reimplemented in Botan::TPM2::RSA_PublicKey.

Definition at line 167 of file rsa.cpp.

References Botan::Encryption, Botan::KeyEncapsulation, and Botan::Signature.

Member Data Documentation

◆ m_public

std::shared_ptr<const RSA_Public_Data> Botan::RSA_PublicKey::m_public
protected

Definition at line 91 of file rsa.h.

Referenced by generate_another(), get_e(), get_int_field(), get_n(), init(), key_length(), and public_data().


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