8 #include <botan/dl_group.h>
9 #include <botan/parsing.h>
10 #include <botan/numthry.h>
11 #include <botan/der_enc.h>
12 #include <botan/ber_dec.h>
13 #include <botan/pem.h>
14 #include <botan/workfactor.h>
15 #include <botan/pow_mod.h>
24 m_initialized =
false;
81 m_p = X - (X % (2*m_q) - 1);
84 m_g = make_dsa_generator(m_p, m_q);
88 qbits = qbits ? qbits : ((pbits <= 1024) ? 160 : 256);
92 m_g = make_dsa_generator(m_p, m_q);
102 const std::vector<uint8_t>& seed,
103 size_t pbits,
size_t qbits)
107 "generate a DSA group");
109 m_g = make_dsa_generator(m_p, m_q);
111 m_initialized =
true;
119 initialize(p1, 0, g1);
127 initialize(p1, q1, g1);
137 if(g1 < 2 || g1 >= p1)
139 if(q1 < 0 || q1 >= p1)
146 m_initialized =
true;
152 void DL_Group::init_check()
const
155 throw Invalid_State(
"DLP group cannot be used uninitialized");
166 if(m_g < 2 || m_p < 3 || m_q < 0)
169 const size_t prob = (strong) ? 128 : 10;
173 if((m_p - 1) % m_q != 0)
229 if((m_q == 0) && (format !=
PKCS_3))
230 throw Encoding_Error(
"The ANSI DL parameter formats require a subgroup");
270 const std::vector<uint8_t> encoding =
DER_encode(format);
288 BigInt new_p, new_q, new_g;
316 initialize(new_p, new_q, new_g);
328 if(label ==
"DH PARAMETERS")
330 else if(label ==
"DSA PARAMETERS")
332 else if(label ==
"X942 DH PARAMETERS" || label ==
"X9.42 DH PARAMETERS")
343 const BigInt e = (p - 1) / q;
345 if(e == 0 || (p - 1) % q > 0)
355 throw Internal_Error(
"DL_Group: Couldn't create a suitable generator");
const size_t PRIME_TABLE_SIZE
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
const uint16_t BOTAN_DLL PRIMES[]
std::string PEM_encode(Format format) const
size_t dl_exponent_size(size_t bits)
std::vector< uint8_t > get_contents_unlocked()
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
bool verify_group(RandomNumberGenerator &rng, bool strong) const
const BigInt & get_q() const
BER_Decoder & decode(bool &v)
const BigInt & get_p() const
std::string to_string(const BER_Object &obj)
void PEM_decode(const std::string &pem)
std::vector< uint8_t > DER_encode(Format format) const
bool generate_dsa_primes(RandomNumberGenerator &rng, BigInt &p, BigInt &q, size_t pbits, size_t qbits, const std::vector< uint8_t > &seed_c, size_t offset)
DER_Encoder & encode(bool b)
const BigInt & get_g() const
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
BER_Decoder & discard_remaining()
BigInt random_safe_prime(RandomNumberGenerator &rng, size_t bits)
std::vector< T > unlock(const secure_vector< T > &in)
BER_Decoder & verify_end()
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
void BER_decode(const std::vector< uint8_t > &ber, Format format)
int32_t jacobi(const BigInt &a, const BigInt &n)
static std::string PEM_for_named_group(const std::string &name)
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo)