Botan  2.1.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 #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  /**
25  * Get the prime p.
26  * @return prime p
27  */
28  const BigInt& get_p() const;
29 
30  /**
31  * Get the prime q.
32  * @return prime q
33  */
34  const BigInt& get_q() const;
35 
36  /**
37  * Get the base g.
38  * @return base g
39  */
40  const BigInt& get_g() const;
41 
42  /**
43  * The DL group encoding format variants.
44  */
45  enum Format {
49 
50  DSA_PARAMETERS = ANSI_X9_57,
51  DH_PARAMETERS = ANSI_X9_42,
52  ANSI_X9_42_DH_PARAMETERS = ANSI_X9_42,
53  PKCS3_DH_PARAMETERS = PKCS_3
54  };
55 
56  /**
57  * Determine the prime creation for DL groups.
58  */
59  enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer };
60 
61  /**
62  * Perform validity checks on the group.
63  * @param rng the rng to use
64  * @param strong whether to perform stronger by lengthier tests
65  * @return true if the object is consistent, false otherwise
66  */
67  bool verify_group(RandomNumberGenerator& rng, bool strong) const;
68 
69  /**
70  * Encode this group into a string using PEM encoding.
71  * @param format the encoding format
72  * @return string holding the PEM encoded group
73  */
74  std::string PEM_encode(Format format) const;
75 
76  /**
77  * Encode this group into a string using DER encoding.
78  * @param format the encoding format
79  * @return string holding the DER encoded group
80  */
81  std::vector<uint8_t> DER_encode(Format format) const;
82 
83  /**
84  * Decode a DER/BER encoded group into this instance.
85  * @param ber a vector containing the DER/BER encoded group
86  * @param format the format of the encoded group
87  */
88  void BER_decode(const std::vector<uint8_t>& ber,
89  Format format);
90 
91  /**
92  * Decode a PEM encoded group into this instance.
93  * @param pem the PEM encoding of the group
94  */
95  void PEM_decode(const std::string& pem);
96 
97  /**
98  * Construct a DL group with uninitialized internal value.
99  * Use this constructor is you wish to set the groups values
100  * from a DER or PEM encoded group.
101  */
102  DL_Group();
103 
104  /**
105  * Construct a DL group that is registered in the configuration.
106  * @param name the name that is configured in the global configuration
107  * for the desired group. If no configuration file is specified,
108  * the default values from the file policy.cpp will be used. For instance,
109  * use "modp/ietf/3072".
110  */
111  DL_Group(const std::string& name);
112 
113  /**
114  * Create a new group randomly.
115  * @param rng the random number generator to use
116  * @param type specifies how the creation of primes p and q shall
117  * be performed. If type=Strong, then p will be determined as a
118  * safe prime, and q will be chosen as (p-1)/2. If
119  * type=Prime_Subgroup and qbits = 0, then the size of q will be
120  * determined according to the estimated difficulty of the DL
121  * problem. If type=DSA_Kosherizer, DSA primes will be created.
122  * @param pbits the number of bits of p
123  * @param qbits the number of bits of q. Leave it as 0 to have
124  * the value determined according to pbits.
125  */
126  DL_Group(RandomNumberGenerator& rng, PrimeType type,
127  size_t pbits, size_t qbits = 0);
128 
129  /**
130  * Create a DSA group with a given seed.
131  * @param rng the random number generator to use
132  * @param seed the seed to use to create the random primes
133  * @param pbits the desired bit size of the prime p
134  * @param qbits the desired bit size of the prime q.
135  */
137  const std::vector<uint8_t>& seed,
138  size_t pbits = 1024, size_t qbits = 0);
139 
140  /**
141  * Create a DL group. The prime q will be determined according to p.
142  * @param p the prime p
143  * @param g the base g
144  */
145  DL_Group(const BigInt& p, const BigInt& g);
146 
147  /**
148  * Create a DL group.
149  * @param p the prime p
150  * @param q the prime q
151  * @param g the base g
152  */
153  DL_Group(const BigInt& p, const BigInt& q, const BigInt& g);
154 
155  /**
156  * Return PEM representation of named DL group
157  */
158  static std::string PEM_for_named_group(const std::string& name);
159  private:
160  static BigInt make_dsa_generator(const BigInt&, const BigInt&);
161 
162  void init_check() const;
163  void initialize(const BigInt&, const BigInt&, const BigInt&);
164  bool m_initialized;
165  BigInt m_p, m_q, m_g;
166  };
167 
168 }
169 
170 #endif
MechanismType type
std::string PEM_encode(const Private_Key &key)
Definition: pkcs8.cpp:139
Definition: alg_id.cpp:13
BigInt m_p
Definition: curve_gfp.cpp:55
const BigInt & m_q
Definition: dsa.cpp:96