diff options
author | Jack Lloyd <[email protected]> | 2018-02-19 11:43:40 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-19 11:48:04 -0500 |
commit | 6d4affbbc27f021c6e87f74c5db420b75ca96581 (patch) | |
tree | a5685a6b05a49ffe55e798a90bcf6c75ca05d886 /src/lib/pubkey/dl_group/dl_group.h | |
parent | 0c730407a73d26eb5a5c9cb2a5fdb6b6042081ed (diff) |
Add shared_ptr for DL_Group state
Add precomputations for mod-p math and g^x%p calcualations.
Diffstat (limited to 'src/lib/pubkey/dl_group/dl_group.h')
-rw-r--r-- | src/lib/pubkey/dl_group/dl_group.h | 187 |
1 files changed, 119 insertions, 68 deletions
diff --git a/src/lib/pubkey/dl_group/dl_group.h b/src/lib/pubkey/dl_group/dl_group.h index 24b829bd9..823c0ba95 100644 --- a/src/lib/pubkey/dl_group/dl_group.h +++ b/src/lib/pubkey/dl_group/dl_group.h @@ -1,6 +1,6 @@ /* * Discrete Logarithm Group -* (C) 1999-2008 Jack Lloyd +* (C) 1999-2008,2018 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -12,31 +12,20 @@ namespace Botan { +class DL_Group_Data; + /** -* 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. +* This class represents discrete logarithm groups. It holds a prime +* modulus p, a generator g, and (optionally) a prime q which is a +* factor of (p-1). In most cases g generates the order-q subgroup. */ class BOTAN_PUBLIC_API(2,0) DL_Group final { public: - - /** - * Get the prime p. - * @return prime p - */ - const BigInt& get_p() const; - /** - * Get the prime q. - * @return prime q - */ - const BigInt& get_q() const; - - /** - * Get the base g. - * @return base g + * Determine the prime creation for DL groups. */ - const BigInt& get_g() const; + enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer }; /** * The DL group encoding format variants. @@ -53,52 +42,11 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final }; /** - * Determine the prime creation for DL groups. - */ - enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer }; - - /** - * Perform validity checks on the group. - * @param rng the rng to use - * @param strong whether to perform stronger by lengthier tests - * @return true if the object is consistent, false otherwise - */ - bool verify_group(RandomNumberGenerator& rng, bool strong) const; - - /** - * Encode this group into a string using PEM encoding. - * @param format the encoding format - * @return string holding the PEM encoded group - */ - std::string PEM_encode(Format format) const; - - /** - * Encode this group into a string using DER encoding. - * @param format the encoding format - * @return string holding the DER encoded group - */ - std::vector<uint8_t> DER_encode(Format format) const; - - /** - * Decode a DER/BER encoded group into this instance. - * @param ber a vector containing the DER/BER encoded group - * @param format the format of the encoded group - */ - void BER_decode(const std::vector<uint8_t>& ber, - Format format); - - /** - * Decode a PEM encoded group into this instance. - * @param pem the PEM encoding of the group - */ - void PEM_decode(const std::string& pem); - - /** * 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. */ - DL_Group(); + DL_Group() = default; /** * Construct a DL group that is registered in the configuration. @@ -137,7 +85,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final size_t pbits = 1024, size_t qbits = 0); /** - * Create a DL group. The prime q will be determined according to p. + * Create a DL group. * @param p the prime p * @param g the base g */ @@ -152,16 +100,119 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final DL_Group(const BigInt& p, const BigInt& q, const BigInt& g); /** + * Decode a BER-encoded DL group param + */ + DL_Group(const uint8_t ber[], size_t ber_len, Format format); + + /** + * Get the prime p. + * @return prime p + */ + const BigInt& get_p() const; + + /** + * Get the prime q, returns zero if q is not used + * @return prime q + */ + const BigInt& get_q() const; + + /** + * Get the base g. + * @return base g + */ + const BigInt& get_g() const; + + /** + * Perform validity checks on the group. + * @param rng the rng to use + * @param strong whether to perform stronger by lengthier tests + * @return true if the object is consistent, false otherwise + */ + bool verify_group(RandomNumberGenerator& rng, bool strong) const; + + /** + * Encode this group into a string using PEM encoding. + * @param format the encoding format + * @return string holding the PEM encoded group + */ + std::string PEM_encode(Format format) const; + + /** + * Encode this group into a string using DER encoding. + * @param format the encoding format + * @return string holding the DER encoded group + */ + std::vector<uint8_t> DER_encode(Format format) const; + + /* + * Reduce an integer modulo p + * @return x % p + */ + BigInt mod_p(const BigInt& x) const; + + /* + * Multiply and reduce an integer modulo p + * @return (x*y) % p + */ + BigInt multiply_mod_p(const BigInt& x, const BigInt& y) const; + + BigInt inverse_mod_p(const BigInt& x) const; + + /* + * Modular exponentiation + * @return (g^x) % p + */ + BigInt power_g_p(const BigInt& x) const; + + /** + * Return the size of p in bits + * Same as get_p().bits() + */ + size_t p_bits() const; + + /** + * Return the size of p in bytes + * Same as get_p().bytes() + */ + size_t p_bytes() const; + + /** + * Decode a DER/BER encoded group into this instance. + * @param ber a vector containing the DER/BER encoded group + * @param format the format of the encoded group + */ + void BOTAN_DEPRECATED("Use DL_Group(ber, Format)") BER_decode(const std::vector<uint8_t>& ber, Format format); + + /** + * Decode a PEM encoded group into this instance. + * @param pem the PEM encoding of the group + */ + void BOTAN_DEPRECATED("Use DL_Group(std::string)") PEM_decode(const std::string& pem); + + /** * Return PEM representation of named DL group */ - static std::string PEM_for_named_group(const std::string& name); + static std::string BOTAN_DEPRECATED("Use DL_Group(name).PEM_encode()") + PEM_for_named_group(const std::string& name); + + /* + * For internal use only + */ + static std::shared_ptr<DL_Group_Data> DL_group_info(const std::string& name); + private: - static BigInt make_dsa_generator(const BigInt&, const BigInt&); + static std::shared_ptr<DL_Group_Data> load_DL_group_info(const char* p_str, + const char* q_str, + const char* g_str); + + static std::shared_ptr<DL_Group_Data> load_DL_group_info(const char* p_str, + const char* g_str); + + static std::shared_ptr<DL_Group_Data> + BER_decode_DL_group(const uint8_t data[], size_t data_len, DL_Group::Format format); - void init_check() const; - void initialize(const BigInt&, const BigInt&, const BigInt&); - bool m_initialized; - BigInt m_p, m_q, m_g; + const DL_Group_Data& data() const; + std::shared_ptr<DL_Group_Data> m_data; }; } |