Botan 3.7.1
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/reducer.h>
14#include <botan/rng.h>
15#include <botan/internal/fmt.h>
16
17namespace Botan {
18
19namespace {
20
21/*
22* Check if this size is allowed by FIPS 186-3
23*/
24bool fips186_3_valid_size(size_t pbits, size_t qbits) {
25 if(qbits == 160) {
26 return (pbits == 1024);
27 }
28
29 if(qbits == 224) {
30 return (pbits == 2048);
31 }
32
33 if(qbits == 256) {
34 return (pbits == 2048 || pbits == 3072);
35 }
36
37 return false;
38}
39
40// qbits assumed to be a valid size for FIPS param gen
41std::string hash_function_for(size_t qbits) {
42 if(qbits == 160) {
43 return "SHA-1";
44 }
45
46 return "SHA-" + std::to_string(qbits);
47}
48
49} // namespace
50
51/*
52* Attempt DSA prime generation with given seed
53*/
55 BigInt& p,
56 BigInt& q,
57 size_t pbits,
58 size_t qbits,
59 const std::vector<uint8_t>& seed_c,
60 size_t offset) {
61 if(!fips186_3_valid_size(pbits, qbits)) {
62 throw Invalid_Argument(fmt("FIPS 186-3 does not allow DSA domain parameters of {}/{} bits long", pbits, qbits));
63 }
64
65 if(seed_c.size() * 8 < qbits) {
66 throw Invalid_Argument(
67 fmt("Generating a DSA parameter set with a {} bit long q requires a seed at least as many bits long", qbits));
68 }
69
70 const std::string hash_name = hash_function_for(qbits);
71 auto hash = HashFunction::create_or_throw(hash_name);
72
73 const size_t HASH_SIZE = hash->output_length();
74
75 class Seed final {
76 public:
77 explicit Seed(const std::vector<uint8_t>& s) : m_seed(s) {}
78
79 const std::vector<uint8_t>& value() const { return m_seed; }
80
81 Seed& operator++() {
82 for(size_t j = m_seed.size(); j > 0; --j) {
83 if(++m_seed[j - 1]) {
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), b = (pbits - 1) % (HASH_SIZE * 8);
105
106 BigInt X;
107 std::vector<uint8_t> V(HASH_SIZE * (n + 1));
108
109 auto mod_2q = Modular_Reducer::for_public_modulus(2 * q);
110
111 for(size_t j = 0; j != 4 * pbits; ++j) {
112 for(size_t k = 0; k <= n; ++k) {
113 ++seed;
114 hash->update(seed.value());
115 hash->final(&V[HASH_SIZE * (n - k)]);
116 }
117
118 if(j >= offset) {
119 X._assign_from_bytes(std::span{V}.subspan(HASH_SIZE - 1 - b / 8));
120 X.set_bit(pbits - 1);
121
122 p = X - (mod_2q.reduce(X) - 1);
123
124 if(p.bits() == pbits && is_prime(p, rng, 128, true)) {
125 return true;
126 }
127 }
128 }
129 return false;
130}
131
132/*
133* Generate DSA Primes
134*/
135std::vector<uint8_t> generate_dsa_primes(RandomNumberGenerator& rng, BigInt& p, BigInt& q, size_t pbits, size_t qbits) {
136 while(true) {
137 std::vector<uint8_t> seed(qbits / 8);
138 rng.randomize(seed.data(), seed.size());
139
140 if(generate_dsa_primes(rng, p, q, pbits, qbits, seed)) {
141 return seed;
142 }
143 }
144}
145
146} // namespace Botan
void set_bit(size_t n)
Definition bigint.h:464
size_t bits() const
Definition bigint.cpp:295
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:298
static Modular_Reducer for_public_modulus(const BigInt &m)
Definition reducer.cpp:43
void randomize(std::span< uint8_t > output)
Definition rng.h:53
int(* final)(unsigned char *, CTX *)
FE_25519 X
Definition ge.cpp:25
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:355
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:54
const SIMD_8x32 & b