Botan  1.10.9
Public Types | Public Member Functions | List of all members
Botan::DL_Group Class Reference

#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, X942_DH_PARAMETERS = ANSI_X9_42, PKCS3_DH_PARAMETERS = PKCS_3
}
 
enum  PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer }
 

Public Member Functions

void BER_decode (DataSource &src, Format format)
 
SecureVector< byteDER_encode (Format format) const
 
 DL_Group ()
 
 DL_Group (const std::string &name)
 
 DL_Group (RandomNumberGenerator &rng, PrimeType type, size_t pbits, size_t qbits=0)
 
 DL_Group (RandomNumberGenerator &rng, const MemoryRegion< byte > &seed, size_t pbits=1024, size_t qbits=0)
 
 DL_Group (const BigInt &p, const BigInt &g)
 
 DL_Group (const BigInt &p, const BigInt &q, const BigInt &g)
 
const BigIntget_g () const
 
const BigIntget_p () const
 
const BigIntget_q () const
 
void PEM_decode (DataSource &src)
 
std::string PEM_encode (Format format) const
 
bool verify_group (RandomNumberGenerator &rng, bool strong) const
 

Detailed Description

This class represents discrete logarithm groups. It holds a prime p, a prime q = (p-1)/2 and g = x^((p-1)/q) mod p.

Definition at line 20 of file dl_group.h.

Member Enumeration Documentation

The DL group encoding format variants.

Enumerator
ANSI_X9_42 
ANSI_X9_57 
PKCS_3 
DSA_PARAMETERS 
DH_PARAMETERS 
X942_DH_PARAMETERS 
PKCS3_DH_PARAMETERS 

Definition at line 44 of file dl_group.h.

Determine the prime creation for DL groups.

Enumerator
Strong 
Prime_Subgroup 
DSA_Kosherizer 

Definition at line 58 of file dl_group.h.

Constructor & Destructor Documentation

Botan::DL_Group::DL_Group ( )

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.

Definition at line 24 of file dl_group.cpp.

25  {
26  initialized = false;
27  }
Botan::DL_Group::DL_Group ( const std::string &  name)

Construct a DL group that is registered in the configuration.

Parameters
namethe name that is configured in the global configuration for the desired group. If no configuration file is specified, the default values from the file policy.cpp will be used. For instance, use "modp/ietf/768" as name.

Definition at line 32 of file dl_group.cpp.

References Botan::Library_State::get(), Botan::Global_State_Management::global_state(), and PEM_decode().

33  {
34  std::string grp_contents = global_state().get("dl", type);
35 
36  if(grp_contents == "")
37  throw Invalid_Argument("DL_Group: Unknown group " + type);
38 
39  DataSource_Memory pem(grp_contents);
40  PEM_decode(pem);
41  }
void PEM_decode(DataSource &src)
Definition: dl_group.cpp:300
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Library_State & global_state()
std::string get(const std::string &section, const std::string &key) const
Definition: libstate.cpp:114
Botan::DL_Group::DL_Group ( RandomNumberGenerator rng,
PrimeType  type,
size_t  pbits,
size_t  qbits = 0 
)

Create a new group randomly.

Parameters
rngthe random number generator to use
typespecifies 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.
pbitsthe number of bits of p
qbitsthe number of bits of q. Leave it as 0 to have the value determined according to pbits.

Definition at line 46 of file dl_group.cpp.

References Botan::BigInt::bits(), Botan::check_prime(), Botan::dl_work_factor(), DSA_Kosherizer, Botan::generate_dsa_primes(), Botan::Global_State_Management::global_state(), Prime_Subgroup, Botan::random_prime(), Botan::random_safe_prime(), Botan::BigInt::randomize(), Strong, and Botan::to_string().

48  {
49  if(pbits < 512)
50  throw Invalid_Argument("DL_Group: prime size " + to_string(pbits) +
51  " is too small");
52 
53  if(type == Strong)
54  {
55  p = random_safe_prime(rng, pbits);
56  q = (p - 1) / 2;
57  g = 2;
58  }
59  else if(type == Prime_Subgroup)
60  {
61  if(!qbits)
62  qbits = 2 * dl_work_factor(pbits);
63 
64  q = random_prime(rng, qbits);
65  BigInt X;
66  while(p.bits() != pbits || !check_prime(p, rng))
67  {
68  X.randomize(rng, pbits);
69  p = X - (X % (2*q) - 1);
70  }
71 
72  g = make_dsa_generator(p, q);
73  }
74  else if(type == DSA_Kosherizer)
75  {
76  qbits = qbits ? qbits : ((pbits <= 1024) ? 160 : 256);
77 
79  global_state().algorithm_factory(),
80  p, q,
81  pbits, qbits);
82 
83  g = make_dsa_generator(p, q);
84  }
85 
86  initialized = true;
87  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
size_t bits() const
Definition: bigint.cpp:253
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
Library_State & global_state()
BigInt random_safe_prime(RandomNumberGenerator &rng, size_t bits)
Definition: make_prm.cpp:87
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:14
bool generate_dsa_primes(RandomNumberGenerator &rng, Algorithm_Factory &af, BigInt &p, BigInt &q, size_t pbits, size_t qbits, const MemoryRegion< byte > &seed_c)
Definition: dsa_gen.cpp:41
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo)
Definition: make_prm.cpp:17
bool check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:143
Botan::DL_Group::DL_Group ( RandomNumberGenerator rng,
const MemoryRegion< byte > &  seed,
size_t  pbits = 1024,
size_t  qbits = 0 
)

Create a DSA group with a given seed.

Parameters
rngthe random number generator to use
seedthe seed to use to create the random primes
pbitsthe desired bit size of the prime p
qbitsthe desired bit size of the prime q.

Definition at line 92 of file dl_group.cpp.

References Botan::generate_dsa_primes(), and Botan::Global_State_Management::global_state().

94  {
96  global_state().algorithm_factory(),
97  p, q, pbits, qbits, seed))
98  throw Invalid_Argument("DL_Group: The seed given does not "
99  "generate a DSA group");
100 
101  g = make_dsa_generator(p, q);
102 
103  initialized = true;
104  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
Library_State & global_state()
bool generate_dsa_primes(RandomNumberGenerator &rng, Algorithm_Factory &af, BigInt &p, BigInt &q, size_t pbits, size_t qbits, const MemoryRegion< byte > &seed_c)
Definition: dsa_gen.cpp:41
Botan::DL_Group::DL_Group ( const BigInt p,
const BigInt g 
)

Create a DL group. The prime q will be determined according to p.

Parameters
pthe prime p
gthe base g

Definition at line 109 of file dl_group.cpp.

110  {
111  initialize(p1, 0, g1);
112  }
Botan::DL_Group::DL_Group ( const BigInt p,
const BigInt q,
const BigInt g 
)

Create a DL group.

Parameters
pthe prime p
qthe prime q
gthe base g

Definition at line 117 of file dl_group.cpp.

118  {
119  initialize(p1, q1, g1);
120  }

Member Function Documentation

void Botan::DL_Group::BER_decode ( DataSource src,
Format  format 
)

Decode a DER/BER encoded group into this instance.

Parameters
srca DataSource providing the encoded group
formatthe format of the encoded group

Definition at line 264 of file dl_group.cpp.

References ANSI_X9_42, ANSI_X9_57, Botan::BER_Decoder::decode(), Botan::BER_Decoder::discard_remaining(), PKCS_3, Botan::SEQUENCE, Botan::BER_Decoder::start_cons(), Botan::to_string(), and Botan::BER_Decoder::verify_end().

Referenced by Botan::DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(), Botan::DL_Scheme_PublicKey::DL_Scheme_PublicKey(), and PEM_decode().

265  {
266  BigInt new_p, new_q, new_g;
267 
268  BER_Decoder decoder(source);
269  BER_Decoder ber = decoder.start_cons(SEQUENCE);
270 
271  if(format == ANSI_X9_57)
272  {
273  ber.decode(new_p)
274  .decode(new_q)
275  .decode(new_g)
276  .verify_end();
277  }
278  else if(format == ANSI_X9_42)
279  {
280  ber.decode(new_p)
281  .decode(new_g)
282  .decode(new_q)
283  .discard_remaining();
284  }
285  else if(format == PKCS_3)
286  {
287  ber.decode(new_p)
288  .decode(new_g)
289  .discard_remaining();
290  }
291  else
292  throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format));
293 
294  initialize(new_p, new_q, new_g);
295  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
SecureVector< byte > Botan::DL_Group::DER_encode ( Format  format) const

Encode this group into a string using DER encoding.

Parameters
formatthe encoding format
Returns
string holding the DER encoded group

Definition at line 205 of file dl_group.cpp.

References ANSI_X9_42, ANSI_X9_57, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents(), PKCS_3, Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), and Botan::to_string().

Referenced by Botan::DL_Scheme_PublicKey::algorithm_identifier(), and PEM_encode().

206  {
207  init_check();
208 
209  if((q == 0) && (format != PKCS_3))
210  throw Encoding_Error("The ANSI DL parameter formats require a subgroup");
211 
212  if(format == ANSI_X9_57)
213  {
214  return DER_Encoder()
215  .start_cons(SEQUENCE)
216  .encode(p)
217  .encode(q)
218  .encode(g)
219  .end_cons()
220  .get_contents();
221  }
222  else if(format == ANSI_X9_42)
223  {
224  return DER_Encoder()
225  .start_cons(SEQUENCE)
226  .encode(p)
227  .encode(g)
228  .encode(q)
229  .end_cons()
230  .get_contents();
231  }
232  else if(format == PKCS_3)
233  {
234  return DER_Encoder()
235  .start_cons(SEQUENCE)
236  .encode(p)
237  .encode(g)
238  .end_cons()
239  .get_contents();
240  }
241 
242  throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format));
243  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
const BigInt & Botan::DL_Group::get_g ( ) const

Get the base g.

Returns
base g

Definition at line 185 of file dl_group.cpp.

Referenced by Botan::generate_srp6_verifier(), Botan::Server_Key_Exchange::Server_Key_Exchange(), Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step1().

186  {
187  init_check();
188  return g;
189  }
const BigInt & Botan::DL_Group::get_p ( ) const

Get the prime p.

Returns
prime p

Definition at line 176 of file dl_group.cpp.

Referenced by Botan::generate_srp6_verifier(), Botan::Server_Key_Exchange::Server_Key_Exchange(), Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step1().

177  {
178  init_check();
179  return p;
180  }
const BigInt & Botan::DL_Group::get_q ( ) const

Get the prime q.

Returns
prime q

Definition at line 194 of file dl_group.cpp.

195  {
196  init_check();
197  if(q == 0)
198  throw Invalid_State("DLP group has no q prime specified");
199  return q;
200  }
void Botan::DL_Group::PEM_decode ( DataSource src)

Decode a PEM encoded group into this instance.

Parameters
srca DataSource providing the encoded group

Definition at line 300 of file dl_group.cpp.

References ANSI_X9_42, ANSI_X9_57, BER_decode(), Botan::PEM_Code::decode(), and PKCS_3.

Referenced by DL_Group().

301  {
302  std::string label;
303  DataSource_Memory ber(PEM_Code::decode(source, label));
304 
305  if(label == "DH PARAMETERS")
306  BER_decode(ber, PKCS_3);
307  else if(label == "DSA PARAMETERS")
308  BER_decode(ber, ANSI_X9_57);
309  else if(label == "X942 DH PARAMETERS")
310  BER_decode(ber, ANSI_X9_42);
311  else
312  throw Decoding_Error("DL_Group: Invalid PEM label " + label);
313  }
void BER_decode(DataSource &src, Format format)
Definition: dl_group.cpp:264
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:56
std::string Botan::DL_Group::PEM_encode ( Format  format) const

Encode this group into a string using PEM encoding.

Parameters
formatthe encoding format
Returns
string holding the PEM encoded group

Definition at line 248 of file dl_group.cpp.

References ANSI_X9_42, ANSI_X9_57, DER_encode(), Botan::PEM_Code::encode(), PKCS_3, and Botan::to_string().

249  {
250  SecureVector<byte> encoding = DER_encode(format);
251  if(format == PKCS_3)
252  return PEM_Code::encode(encoding, "DH PARAMETERS");
253  else if(format == ANSI_X9_57)
254  return PEM_Code::encode(encoding, "DSA PARAMETERS");
255  else if(format == ANSI_X9_42)
256  return PEM_Code::encode(encoding, "X942 DH PARAMETERS");
257  else
258  throw Invalid_Argument("Unknown DL_Group encoding " + to_string(format));
259  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
SecureVector< byte > DER_encode(Format format) const
Definition: dl_group.cpp:205
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
bool Botan::DL_Group::verify_group ( RandomNumberGenerator rng,
bool  strong 
) const

Perform validity checks on the group.

Parameters
rngthe rng to use
strongwhether to perform stronger by lengthier tests
Returns
true if the object is consistent, false otherwise

Definition at line 153 of file dl_group.cpp.

References Botan::check_prime().

Referenced by Botan::DL_Scheme_PublicKey::check_key(), and Botan::DL_Scheme_PrivateKey::check_key().

155  {
156  init_check();
157 
158  if(g < 2 || p < 3 || q < 0)
159  return false;
160  if((q != 0) && ((p - 1) % q != 0))
161  return false;
162 
163  if(!strong)
164  return true;
165 
166  if(!check_prime(p, rng))
167  return false;
168  if((q > 0) && !check_prime(q, rng))
169  return false;
170  return true;
171  }
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
bool check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:143

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