Botan 2.19.1
Crypto and TLS for C&
|
#include <dl_group.h>
Public Types | |
enum | Format { ANSI_X9_42 , ANSI_X9_57 , PKCS_3 , DSA_PARAMETERS = ANSI_X9_57 , DH_PARAMETERS = ANSI_X9_42 , ANSI_X9_42_DH_PARAMETERS = ANSI_X9_42 , PKCS3_DH_PARAMETERS = PKCS_3 } |
enum | PrimeType { Strong , Prime_Subgroup , DSA_Kosherizer } |
Public Member Functions | |
void | BER_decode (const std::vector< uint8_t > &ber, Format format) |
std::vector< uint8_t > | DER_encode (Format format) const |
DL_Group ()=default | |
DL_Group (const BigInt &p, const BigInt &g) | |
DL_Group (const BigInt &p, const BigInt &q, const BigInt &g) | |
DL_Group (const std::string &name) | |
template<typename Alloc > | |
DL_Group (const std::vector< uint8_t, Alloc > &ber, Format format) | |
DL_Group (const uint8_t ber[], size_t ber_len, Format format) | |
DL_Group (RandomNumberGenerator &rng, const std::vector< uint8_t > &seed, size_t pbits=1024, size_t qbits=0) | |
DL_Group (RandomNumberGenerator &rng, PrimeType type, size_t pbits, size_t qbits=0) | |
size_t | estimated_strength () const |
size_t | exponent_bits () const |
const BigInt & | get_g () const |
const BigInt & | get_p () const |
const BigInt & | get_q () const |
BigInt | inverse_mod_p (const BigInt &x) const |
BigInt | inverse_mod_q (const BigInt &x) const |
BigInt | mod_p (const BigInt &x) const |
BigInt | mod_q (const BigInt &x) const |
std::shared_ptr< const Montgomery_Params > | monty_params_p () const |
BigInt | multi_exponentiate (const BigInt &x, const BigInt &y, const BigInt &z) const |
BigInt | multiply_mod_p (const BigInt &x, const BigInt &y) const |
BigInt | multiply_mod_q (const BigInt &x, const BigInt &y) const |
BigInt | multiply_mod_q (const BigInt &x, const BigInt &y, const BigInt &z) const |
size_t | p_bits () const |
size_t | p_bytes () const |
void | PEM_decode (const std::string &pem) |
std::string | PEM_encode (Format format) const |
BigInt | power_g_p (const BigInt &x) const |
BigInt | power_g_p (const BigInt &x, size_t max_x_bits) const |
size_t | q_bits () const |
size_t | q_bytes () const |
DL_Group_Source | source () const |
BigInt | square_mod_q (const BigInt &x) const |
bool | verify_element_pair (const BigInt &y, const BigInt &x) const |
bool | verify_group (RandomNumberGenerator &rng, bool strong=true) const |
bool | verify_public_element (const BigInt &y) const |
Static Public Member Functions | |
static DL_Group | DL_Group_from_PEM (const std::string &pem) |
static std::shared_ptr< DL_Group_Data > | DL_group_info (const std::string &name) |
static std::string | PEM_for_named_group (const std::string &name) |
This class represents discrete logarithm groups. It holds a prime modulus p, a generator g, and (optionally) a prime q which is a factor of (p-1). In most cases g generates the order-q subgroup.
Definition at line 29 of file dl_group.h.
The DL group encoding format variants.
Enumerator | |
---|---|
ANSI_X9_42 | |
ANSI_X9_57 | |
PKCS_3 | |
DSA_PARAMETERS | |
DH_PARAMETERS | |
ANSI_X9_42_DH_PARAMETERS | |
PKCS3_DH_PARAMETERS |
Definition at line 40 of file dl_group.h.
Determine the prime creation for DL groups.
Enumerator | |
---|---|
Strong | |
Prime_Subgroup | |
DSA_Kosherizer |
Definition at line 35 of file dl_group.h.
|
default |
Construct a DL group with uninitialized internal value. Use this constructor is you wish to set the groups values from a DER or PEM encoded group.
Referenced by DL_Group_from_PEM().
|
explicit |
Construct a DL group that is registered in the configuration.
name | the name of the group, for example "modp/ietf/3072" |
Definition at line 189 of file dl_group.cpp.
References Botan::PEM_Code::decode(), DL_group_info(), Botan::ExternalSource, and Botan::unlock().
Botan::DL_Group::DL_Group | ( | RandomNumberGenerator & | rng, |
PrimeType | type, | ||
size_t | pbits, | ||
size_t | qbits = 0 |
||
) |
Create a new group randomly.
rng | the random number generator to use |
type | specifies how the creation of primes p and q shall be performed. If type=Strong, then p will be determined as a safe prime, and q will be chosen as (p-1)/2. If type=Prime_Subgroup and qbits = 0, then the size of q will be determined according to the estimated difficulty of the DL problem. If type=DSA_Kosherizer, DSA primes will be created. |
pbits | the number of bits of p |
qbits | the number of bits of q. Leave it as 0 to have the value determined according to pbits. |
Definition at line 240 of file dl_group.cpp.
References Botan::BigInt::bits(), Botan::dl_exponent_size(), DSA_Kosherizer, Botan::generate_dsa_primes(), Botan::is_prime(), Botan::jacobi(), Prime_Subgroup, Botan::PRIME_TABLE_SIZE, Botan::PRIMES, Botan::random_prime(), Botan::random_safe_prime(), Botan::RandomlyGenerated, Botan::Modular_Reducer::reduce(), Strong, Botan::ASN1::to_string(), type, and X.
Botan::DL_Group::DL_Group | ( | RandomNumberGenerator & | rng, |
const std::vector< uint8_t > & | seed, | ||
size_t | pbits = 1024 , |
||
size_t | qbits = 0 |
||
) |
Create a DSA group with a given seed.
rng | the random number generator to use |
seed | the seed to use to create the random primes |
pbits | the desired bit size of the prime p |
qbits | the desired bit size of the prime q. |
Definition at line 309 of file dl_group.cpp.
References Botan::generate_dsa_primes(), and Botan::RandomlyGenerated.
Create a DL group.
p | the prime p |
g | the base g |
Definition at line 326 of file dl_group.cpp.
References Botan::ExternalSource.
Create a DL group.
p | the prime p |
q | the prime q |
g | the base g |
Definition at line 334 of file dl_group.cpp.
References Botan::ExternalSource.
Botan::DL_Group::DL_Group | ( | const uint8_t | ber[], |
size_t | ber_len, | ||
Format | format | ||
) |
Decode a BER-encoded DL group param
Definition at line 607 of file dl_group.cpp.
References Botan::ExternalSource.
|
inline |
Decode a BER-encoded DL group param
Definition at line 124 of file dl_group.h.
void Botan::DL_Group::BER_decode | ( | const std::vector< uint8_t > & | ber, |
Format | format | ||
) |
Decode a DER/BER encoded group into this instance.
ber | a vector containing the DER/BER encoded group |
format | the format of the encoded group |
Definition at line 612 of file dl_group.cpp.
References Botan::ExternalSource.
Referenced by Botan::DL_Scheme_PrivateKey::DL_Scheme_PrivateKey().
std::vector< uint8_t > Botan::DL_Group::DER_encode | ( | Format | format | ) | const |
Encode this group into a string using DER encoding.
format | the encoding format |
Definition at line 553 of file dl_group.cpp.
References ANSI_X9_42, ANSI_X9_57, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), get_g(), get_p(), get_q(), PKCS_3, Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), and Botan::ASN1::to_string().
Referenced by Botan::DL_Scheme_PublicKey::algorithm_identifier(), and PEM_encode().
|
static |
Definition at line 618 of file dl_group.cpp.
References Botan::PEM_Code::decode(), DL_Group(), and Botan::unlock().
|
static |
Definition at line 13 of file dl_named.cpp.
References name.
Referenced by DL_Group().
size_t Botan::DL_Group::estimated_strength | ( | ) | const |
Return an estimate of the strength of this group against discrete logarithm attacks (eg NFS). Warning: since this only takes into account known attacks it is by necessity an overestimate of the actual strength.
Definition at line 473 of file dl_group.cpp.
Referenced by Botan::DL_Scheme_PublicKey::estimated_strength().
size_t Botan::DL_Group::exponent_bits | ( | ) | const |
Return size in bits of a secret exponent
This attempts to balance between the attack costs of NFS (which depends on the size of the modulus) and Pollard's rho (which depends on the size of the exponent).
It may vary over time for a particular group, if the attack costs change.
Definition at line 478 of file dl_group.cpp.
Referenced by Botan::DH_PrivateKey::DH_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step1().
const BigInt & Botan::DL_Group::get_g | ( | ) | const |
Get the base g.
Definition at line 433 of file dl_group.cpp.
Referenced by DER_encode(), multi_exponentiate(), Botan::TLS::Server_Key_Exchange::Server_Key_Exchange(), Botan::srp6_client_agree(), Botan::srp6_group_identifier(), Botan::SRP6_Server_Session::step1(), and verify_group().
const BigInt & Botan::DL_Group::get_p | ( | ) | const |
Get the prime p.
Definition at line 425 of file dl_group.cpp.
Referenced by DER_encode(), inverse_mod_p(), Botan::TLS::Server_Key_Exchange::Server_Key_Exchange(), Botan::srp6_client_agree(), Botan::srp6_group_identifier(), Botan::SRP6_Server_Session::step1(), verify_element_pair(), verify_group(), and verify_public_element().
const BigInt & Botan::DL_Group::get_q | ( | ) | const |
Get the prime q, returns zero if q is not used
Definition at line 441 of file dl_group.cpp.
Referenced by DER_encode(), inverse_mod_q(), PEM_for_named_group(), verify_group(), and verify_public_element().
Return the inverse of x mod p
Definition at line 483 of file dl_group.cpp.
References get_p(), and Botan::inverse_mod().
Return the inverse of x mod q Throws if q is unset on this DL_Group
Definition at line 499 of file dl_group.cpp.
References get_q(), and Botan::inverse_mod().
Reduce an integer modulo p
Definition at line 489 of file dl_group.cpp.
Referenced by Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step1().
Reduce an integer modulo q Throws if q is unset on this DL_Group
Definition at line 506 of file dl_group.cpp.
std::shared_ptr< const Montgomery_Params > Botan::DL_Group::monty_params_p | ( | ) | const |
Return parameters for Montgomery reduction/exponentiation mod p
Definition at line 446 of file dl_group.cpp.
Referenced by multi_exponentiate().
BigInt Botan::DL_Group::multi_exponentiate | ( | const BigInt & | x, |
const BigInt & | y, | ||
const BigInt & | z | ||
) | const |
Multi-exponentiate Return (g^x * y^z) % p
Definition at line 530 of file dl_group.cpp.
References get_g(), Botan::monty_multi_exp(), and monty_params_p().
Multiply and reduce an integer modulo p
Definition at line 494 of file dl_group.cpp.
Multiply and reduce an integer modulo q Throws if q is unset on this DL_Group
Definition at line 512 of file dl_group.cpp.
Referenced by multiply_mod_q().
BigInt Botan::DL_Group::multiply_mod_q | ( | const BigInt & | x, |
const BigInt & | y, | ||
const BigInt & | z | ||
) | const |
Multiply and reduce an integer modulo q Throws if q is unset on this DL_Group
Definition at line 518 of file dl_group.cpp.
References multiply_mod_q().
size_t Botan::DL_Group::p_bits | ( | ) | const |
Return the size of p in bits Same as get_p().bits()
Definition at line 451 of file dl_group.cpp.
Referenced by Botan::DH_PrivateKey::DH_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), and Botan::DL_Scheme_PublicKey::key_length().
size_t Botan::DL_Group::p_bytes | ( | ) | const |
Return the size of p in bytes Same as get_p().bytes()
Definition at line 456 of file dl_group.cpp.
Referenced by Botan::srp6_client_agree().
void Botan::DL_Group::PEM_decode | ( | const std::string & | pem | ) |
Decode a PEM encoded group into this instance.
pem | the PEM encoding of the group |
Definition at line 629 of file dl_group.cpp.
References Botan::PEM_Code::decode(), Botan::ExternalSource, and Botan::unlock().
std::string Botan::DL_Group::PEM_encode | ( | Format | format | ) | const |
Encode this group into a string using PEM encoding.
format | the encoding format |
Definition at line 593 of file dl_group.cpp.
References ANSI_X9_42, ANSI_X9_57, DER_encode(), Botan::PEM_Code::encode(), PKCS_3, and Botan::ASN1::to_string().
Referenced by PEM_for_named_group().
|
static |
Return PEM representation of named DL group
Definition at line 639 of file dl_group.cpp.
References ANSI_X9_42, get_q(), Botan::BigInt::is_zero(), name, PEM_encode(), and PKCS_3.
Modular exponentiation
Definition at line 535 of file dl_group.cpp.
References Botan::BigInt::bits().
Referenced by Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), Botan::generate_srp6_verifier(), Botan::srp6_client_agree(), Botan::SRP6_Server_Session::step1(), verify_element_pair(), and verify_group().
Modular exponentiation
x | the exponent |
max_x_bits | x is assumed to be at most this many bits long. |
Definition at line 540 of file dl_group.cpp.
size_t Botan::DL_Group::q_bits | ( | ) | const |
Return the size of q in bits Same as get_q().bits() Throws if q is unset
Definition at line 461 of file dl_group.cpp.
Referenced by Botan::DSA_PrivateKey::DSA_PrivateKey().
size_t Botan::DL_Group::q_bytes | ( | ) | const |
Return the size of q in bytes Same as get_q().bytes() Throws if q is unset
Definition at line 467 of file dl_group.cpp.
DL_Group_Source Botan::DL_Group::source | ( | ) | const |
Definition at line 545 of file dl_group.cpp.
Referenced by verify_group().
Square and reduce an integer modulo q Throws if q is unset on this DL_Group
Definition at line 524 of file dl_group.cpp.
Verify a pair of elements y = g^x
This verifies that 1 < x,y < p and that y=g^x mod p
Definition at line 364 of file dl_group.cpp.
References get_p(), and power_g_p().
Referenced by Botan::DL_Scheme_PrivateKey::check_key().
bool Botan::DL_Group::verify_group | ( | RandomNumberGenerator & | rng, |
bool | strong = true |
||
) | const |
Perform validity checks on the group.
rng | the rng to use |
strong | whether to perform stronger by lengthier tests |
Definition at line 380 of file dl_group.cpp.
References Botan::Builtin, Botan::ExternalSource, get_g(), get_p(), get_q(), Botan::is_prime(), power_g_p(), and source().
Referenced by Botan::DL_Scheme_PublicKey::check_key(), Botan::DL_Scheme_PrivateKey::check_key(), and Botan::TLS::Callbacks::tls_dh_agree().
bool Botan::DL_Group::verify_public_element | ( | const BigInt & | y | ) | const |
Verify a public element, ie check if y = g^x for some x.
This is not a perfect test. It verifies that 1 < y < p and (if q is set) that y is in the subgroup of size q.
Definition at line 347 of file dl_group.cpp.
References get_p(), get_q(), Botan::BigInt::is_zero(), and Botan::power_mod().
Referenced by Botan::DL_Scheme_PublicKey::check_key().