Botan  1.10.9
dl_group.h
Go to the documentation of this file.
1 /*
2 * Discrete Logarithm Group
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_DL_PARAM_H__
9 #define BOTAN_DL_PARAM_H__
10 
11 #include <botan/bigint.h>
12 #include <botan/data_src.h>
13 
14 namespace Botan {
15 
16 /**
17 * This class represents discrete logarithm groups. It holds a prime p,
18 * a prime q = (p-1)/2 and g = x^((p-1)/q) mod p.
19 */
20 class BOTAN_DLL DL_Group
21  {
22  public:
23  /**
24  * Get the prime p.
25  * @return prime p
26  */
27  const BigInt& get_p() const;
28 
29  /**
30  * Get the prime q.
31  * @return prime q
32  */
33  const BigInt& get_q() const;
34 
35  /**
36  * Get the base g.
37  * @return base g
38  */
39  const BigInt& get_g() const;
40 
41  /**
42  * The DL group encoding format variants.
43  */
44  enum Format {
48 
49  DSA_PARAMETERS = ANSI_X9_57,
50  DH_PARAMETERS = ANSI_X9_42,
51  X942_DH_PARAMETERS = ANSI_X9_42,
52  PKCS3_DH_PARAMETERS = PKCS_3
53  };
54 
55  /**
56  * Determine the prime creation for DL groups.
57  */
58  enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer };
59 
60  /**
61  * Perform validity checks on the group.
62  * @param rng the rng to use
63  * @param strong whether to perform stronger by lengthier tests
64  * @return true if the object is consistent, false otherwise
65  */
66  bool verify_group(RandomNumberGenerator& rng, bool strong) const;
67 
68  /**
69  * Encode this group into a string using PEM encoding.
70  * @param format the encoding format
71  * @return string holding the PEM encoded group
72  */
73  std::string PEM_encode(Format format) const;
74 
75  /**
76  * Encode this group into a string using DER encoding.
77  * @param format the encoding format
78  * @return string holding the DER encoded group
79  */
80  SecureVector<byte> DER_encode(Format format) const;
81 
82  /**
83  * Decode a DER/BER encoded group into this instance.
84  * @param src a DataSource providing the encoded group
85  * @param format the format of the encoded group
86  */
87  void BER_decode(DataSource& src, Format format);
88 
89  /**
90  * Decode a PEM encoded group into this instance.
91  * @param src a DataSource providing the encoded group
92  */
93  void PEM_decode(DataSource& src);
94 
95  /**
96  * Construct a DL group with uninitialized internal value.
97  * Use this constructor is you wish to set the groups values
98  * from a DER or PEM encoded group.
99  */
100  DL_Group();
101 
102  /**
103  * Construct a DL group that is registered in the configuration.
104  * @param name the name that is configured in the global configuration
105  * for the desired group. If no configuration file is specified,
106  * the default values from the file policy.cpp will be used. For instance,
107  * use "modp/ietf/768" as name.
108  */
109  DL_Group(const std::string& name);
110 
111  /**
112  * Create a new group randomly.
113  * @param rng the random number generator to use
114  * @param type specifies how the creation of primes p and q shall
115  * be performed. If type=Strong, then p will be determined as a
116  * safe prime, and q will be chosen as (p-1)/2. If
117  * type=Prime_Subgroup and qbits = 0, then the size of q will be
118  * determined according to the estimated difficulty of the DL
119  * problem. If type=DSA_Kosherizer, DSA primes will be created.
120  * @param pbits the number of bits of p
121  * @param qbits the number of bits of q. Leave it as 0 to have
122  * the value determined according to pbits.
123  */
124  DL_Group(RandomNumberGenerator& rng, PrimeType type,
125  size_t pbits, size_t qbits = 0);
126 
127  /**
128  * Create a DSA group with a given seed.
129  * @param rng the random number generator to use
130  * @param seed the seed to use to create the random primes
131  * @param pbits the desired bit size of the prime p
132  * @param qbits the desired bit size of the prime q.
133  */
135  size_t pbits = 1024, size_t qbits = 0);
136 
137  /**
138  * Create a DL group. The prime q will be determined according to p.
139  * @param p the prime p
140  * @param g the base g
141  */
142  DL_Group(const BigInt& p, const BigInt& g);
143 
144  /**
145  * Create a DL group.
146  * @param p the prime p
147  * @param q the prime q
148  * @param g the base g
149  */
150  DL_Group(const BigInt& p, const BigInt& q, const BigInt& g);
151  private:
152  static BigInt make_dsa_generator(const BigInt&, const BigInt&);
153 
154  void init_check() const;
155  void initialize(const BigInt&, const BigInt&, const BigInt&);
156  bool initialized;
157  BigInt p, q, g;
158  };
159 
160 }
161 
162 #endif
std::string PEM_encode(const Private_Key &key)
Definition: pkcs8.cpp:150
RandomNumberGenerator * rng
Definition: global_rng.cpp:165