Botan  2.4.0
Crypto and TLS for C++11
dl_group.h
Go to the documentation of this file.
1 /*
2 * Discrete Logarithm Group
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_DL_PARAM_H_
9 #define BOTAN_DL_PARAM_H_
10 
11 #include <botan/bigint.h>
12 
13 namespace Botan {
14 
15 /**
16 * This class represents discrete logarithm groups. It holds a prime p,
17 * a prime q = (p-1)/2 and g = x^((p-1)/q) mod p.
18 */
19 class BOTAN_PUBLIC_API(2,0) DL_Group final
20  {
21  public:
22 
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  ANSI_X9_42_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  std::vector<uint8_t> DER_encode(Format format) const;
81 
82  /**
83  * Decode a DER/BER encoded group into this instance.
84  * @param ber a vector containing the DER/BER encoded group
85  * @param format the format of the encoded group
86  */
87  void BER_decode(const std::vector<uint8_t>& ber,
88  Format format);
89 
90  /**
91  * Decode a PEM encoded group into this instance.
92  * @param pem the PEM encoding of the group
93  */
94  void PEM_decode(const std::string& pem);
95 
96  /**
97  * Construct a DL group with uninitialized internal value.
98  * Use this constructor is you wish to set the groups values
99  * from a DER or PEM encoded group.
100  */
101  DL_Group();
102 
103  /**
104  * Construct a DL group that is registered in the configuration.
105  * @param name the name that is configured in the global configuration
106  * for the desired group. If no configuration file is specified,
107  * the default values from the file policy.cpp will be used. For instance,
108  * use "modp/ietf/3072".
109  */
110  DL_Group(const std::string& name);
111 
112  /**
113  * Create a new group randomly.
114  * @param rng the random number generator to use
115  * @param type specifies how the creation of primes p and q shall
116  * be performed. If type=Strong, then p will be determined as a
117  * safe prime, and q will be chosen as (p-1)/2. If
118  * type=Prime_Subgroup and qbits = 0, then the size of q will be
119  * determined according to the estimated difficulty of the DL
120  * problem. If type=DSA_Kosherizer, DSA primes will be created.
121  * @param pbits the number of bits of p
122  * @param qbits the number of bits of q. Leave it as 0 to have
123  * the value determined according to pbits.
124  */
126  size_t pbits, size_t qbits = 0);
127 
128  /**
129  * Create a DSA group with a given seed.
130  * @param rng the random number generator to use
131  * @param seed the seed to use to create the random primes
132  * @param pbits the desired bit size of the prime p
133  * @param qbits the desired bit size of the prime q.
134  */
136  const std::vector<uint8_t>& seed,
137  size_t pbits = 1024, size_t qbits = 0);
138 
139  /**
140  * Create a DL group. The prime q will be determined according to p.
141  * @param p the prime p
142  * @param g the base g
143  */
144  DL_Group(const BigInt& p, const BigInt& g);
145 
146  /**
147  * Create a DL group.
148  * @param p the prime p
149  * @param q the prime q
150  * @param g the base g
151  */
152  DL_Group(const BigInt& p, const BigInt& q, const BigInt& g);
153 
154  /**
155  * Return PEM representation of named DL group
156  */
157  static std::string PEM_for_named_group(const std::string& name);
158  private:
159  static BigInt make_dsa_generator(const BigInt&, const BigInt&);
160 
161  void init_check() const;
162  void initialize(const BigInt&, const BigInt&, const BigInt&);
163  bool m_initialized;
164  BigInt m_p, m_q, m_g;
165  };
166 
167 }
168 
169 #endif
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
const BigInt & m_q
Definition: rsa.cpp:235
MechanismType type
std::string PEM_encode(const Private_Key &key)
Definition: pkcs8.cpp:140
Definition: alg_id.cpp:13