Botan 3.10.0
Crypto and TLS for C&
dsa_gen.cpp
Go to the documentation of this file.
1/*
2* DSA Parameter Generation
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/primality.h>
9
10#include <botan/bigint.h>
11#include <botan/hash.h>
12#include <botan/numthry.h>
13#include <botan/rng.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
18namespace {
19
20/*
21* Check if this size is allowed by FIPS 186-3
22*/
23bool fips186_3_valid_size(size_t pbits, size_t qbits) {
24 if(qbits == 160) {
25 return (pbits == 1024);
26 }
27
28 if(qbits == 224) {
29 return (pbits == 2048);
30 }
31
32 if(qbits == 256) {
33 return (pbits == 2048 || pbits == 3072);
34 }
35
36 return false;
37}
38
39// qbits assumed to be a valid size for FIPS param gen
40std::string hash_function_for(size_t qbits) {
41 if(qbits == 160) {
42 return "SHA-1";
43 }
44
45 return "SHA-" + std::to_string(qbits);
46}
47
48} // namespace
49
50/*
51* Attempt DSA prime generation with given seed
52*/
54 BigInt& p,
55 BigInt& q,
56 size_t pbits,
57 size_t qbits,
58 const std::vector<uint8_t>& seed_c,
59 size_t offset) {
60 if(!fips186_3_valid_size(pbits, qbits)) {
61 throw Invalid_Argument(fmt("FIPS 186-3 does not allow DSA domain parameters of {}/{} bits long", pbits, qbits));
62 }
63
64 if(seed_c.size() * 8 < qbits) {
65 throw Invalid_Argument(
66 fmt("Generating a DSA parameter set with a {} bit long q requires a seed at least as many bits long", qbits));
67 }
68
69 const std::string hash_name = hash_function_for(qbits);
70 auto hash = HashFunction::create_or_throw(hash_name);
71
72 const size_t HASH_SIZE = hash->output_length();
73
74 class Seed final {
75 public:
76 explicit Seed(const std::vector<uint8_t>& s) : m_seed(s) {}
77
78 const std::vector<uint8_t>& value() const { return m_seed; }
79
80 Seed& operator++() {
81 for(size_t j = m_seed.size(); j > 0; --j) {
82 m_seed[j - 1] += 1;
83 if(m_seed[j - 1] != 0) {
84 break;
85 }
86 }
87 return (*this);
88 }
89
90 private:
91 std::vector<uint8_t> m_seed;
92 };
93
94 Seed seed(seed_c);
95
96 q._assign_from_bytes(hash->process(seed.value()));
97 q.set_bit(qbits - 1);
98 q.set_bit(0);
99
100 if(!is_prime(q, rng, 128, true)) {
101 return false;
102 }
103
104 const size_t n = (pbits - 1) / (HASH_SIZE * 8);
105 const size_t b = (pbits - 1) % (HASH_SIZE * 8);
106
107 BigInt X;
108 std::vector<uint8_t> V(HASH_SIZE * (n + 1));
109
110 const BigInt q2 = 2 * q;
111
112 for(size_t j = 0; j != 4 * pbits; ++j) {
113 for(size_t k = 0; k <= n; ++k) {
114 ++seed;
115 hash->update(seed.value());
116 hash->final(&V[HASH_SIZE * (n - k)]);
117 }
118
119 if(j >= offset) {
120 X._assign_from_bytes(std::span{V}.subspan(HASH_SIZE - 1 - b / 8));
121 X.set_bit(pbits - 1);
122
123 // Variable time division is OK here since DSA primes are public anyway
124 p = X - ((X % q2) - 1);
125
126 if(p.bits() == pbits && is_prime(p, rng, 128, true)) {
127 return true;
128 }
129 }
130 }
131 return false;
132}
133
134/*
135* Generate DSA Primes
136*/
137std::vector<uint8_t> generate_dsa_primes(RandomNumberGenerator& rng, BigInt& p, BigInt& q, size_t pbits, size_t qbits) {
138 while(true) {
139 std::vector<uint8_t> seed(qbits / 8);
140 rng.randomize(seed.data(), seed.size());
141
142 if(generate_dsa_primes(rng, p, q, pbits, qbits, seed)) {
143 return seed;
144 }
145 }
146}
147
148} // namespace Botan
void set_bit(size_t n)
Definition bigint.h:463
size_t bits() const
Definition bigint.cpp:311
void _assign_from_bytes(std::span< const uint8_t > bytes)
Definition bigint.h:947
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
void randomize(std::span< uint8_t > output)
Definition rng.h:71
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr auto operator++(Strong< T, Tags... > &a, int)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:380
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)
Definition dsa_gen.cpp:53