8#include <botan/internal/primality.h>
9#include <botan/internal/fmt.h>
10#include <botan/numthry.h>
11#include <botan/hash.h>
12#include <botan/reducer.h>
22bool fips186_3_valid_size(
size_t pbits,
size_t qbits)
25 return (pbits == 1024);
28 return (pbits == 2048);
31 return (pbits == 2048 || pbits == 3072);
37std::string hash_function_for(
size_t qbits)
42 return "SHA-" + std::to_string(qbits);
52 size_t pbits,
size_t qbits,
53 const std::vector<uint8_t>& seed_c,
56 if(!fips186_3_valid_size(pbits, qbits))
59 fmt(
"FIPS 186-3 does not allow DSA domain parameters of {}/{} bits long",
63 if(seed_c.size() * 8 < qbits)
66 fmt(
"Generating a DSA parameter set with a {} bit long q requires a seed at least as many bits long", qbits));
69 const std::string hash_name = hash_function_for(qbits);
72 const size_t HASH_SIZE = hash->output_length();
77 explicit Seed(
const std::vector<uint8_t>& s) : m_seed(s) {}
79 const std::vector<uint8_t>& value()
const {
return m_seed; }
83 for(
size_t j = m_seed.size(); j > 0; --j)
89 std::vector<uint8_t> m_seed;
101 const size_t n = (pbits-1) / (HASH_SIZE * 8),
102 b = (pbits-1) % (HASH_SIZE * 8);
105 std::vector<uint8_t> V(HASH_SIZE * (n+1));
109 for(
size_t j = 0; j != 4*pbits; ++j)
111 for(
size_t k = 0; k <= n; ++k)
114 hash->update(seed.value());
115 hash->final(&V[HASH_SIZE * (n-k)]);
120 X.binary_decode(&V[HASH_SIZE - 1 - b/8],
121 V.size() - (HASH_SIZE - 1 - b/8));
138 size_t pbits,
size_t qbits)
142 std::vector<uint8_t> seed(qbits / 8);
void binary_decode(const uint8_t buf[], size_t length)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
BigInt reduce(const BigInt &x) const
void randomize(std::span< uint8_t > output)
int(* final)(unsigned char *, CTX *)
std::string fmt(std::string_view format, const T &... args)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
bool generate_dsa_primes(RandomNumberGenerator &rng, BigInt &p, BigInt &q, size_t pbits, size_t qbits, const std::vector< uint8_t > &seed_c, size_t offset)