2
* DSA Parameter Generation
3
* (C) 1999-2007 Jack Lloyd
5
* Botan is released under the Simplified BSD License (see license.txt)
8
#include <botan/numthry.h>
9
#include <botan/hash.h>
10
#include <botan/rng.h>
17
* Check if this size is allowed by FIPS 186-3
19
bool fips186_3_valid_size(size_t pbits, size_t qbits)
22
return (pbits == 1024);
25
return (pbits == 2048);
28
return (pbits == 2048 || pbits == 3072);
36
* Attempt DSA prime generation with given seed
38
bool generate_dsa_primes(RandomNumberGenerator& rng,
40
size_t pbits, size_t qbits,
41
const std::vector<uint8_t>& seed_c,
44
if(!fips186_3_valid_size(pbits, qbits))
45
throw Invalid_Argument(
46
"FIPS 186-3 does not allow DSA domain parameters of " +
47
std::to_string(pbits) + "/" + std::to_string(qbits) + " bits long");
49
if(seed_c.size() * 8 < qbits)
50
throw Invalid_Argument(
51
"Generating a DSA parameter set with a " + std::to_string(qbits) +
52
"long q requires a seed at least as many bits long");
54
const std::string hash_name = "SHA-" + std::to_string(qbits);
55
std::unique_ptr<HashFunction> hash(HashFunction::create_or_throw(hash_name));
57
const size_t HASH_SIZE = hash->output_length();
62
explicit Seed(const std::vector<uint8_t>& s) : m_seed(s) {}
64
const std::vector<uint8_t>& value() const { return m_seed; }
68
for(size_t j = m_seed.size(); j > 0; --j)
74
std::vector<uint8_t> m_seed;
79
q.binary_decode(hash->process(seed.value()));
83
if(!is_prime(q, rng, 126))
86
const size_t n = (pbits-1) / (HASH_SIZE * 8),
87
b = (pbits-1) % (HASH_SIZE * 8);
90
std::vector<uint8_t> V(HASH_SIZE * (n+1));
92
for(size_t j = 0; j != 4*pbits; ++j)
94
for(size_t k = 0; k <= n; ++k)
97
hash->update(seed.value());
98
hash->final(&V[HASH_SIZE * (n-k)]);
103
X.binary_decode(&V[HASH_SIZE - 1 - b/8],
104
V.size() - (HASH_SIZE - 1 - b/8));
107
p = X - (X % (2*q) - 1);
109
if(p.bits() == pbits && is_prime(p, rng, 126))
117
* Generate DSA Primes
119
std::vector<uint8_t> generate_dsa_primes(RandomNumberGenerator& rng,
120
BigInt& p, BigInt& q,
121
size_t pbits, size_t qbits)
125
std::vector<uint8_t> seed(qbits / 8);
126
rng.randomize(seed.data(), seed.size());
128
if(generate_dsa_primes(rng, p, q, pbits, qbits, seed))