8#include <botan/dl_group.h>
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/numthry.h>
14#include <botan/reducer.h>
15#include <botan/internal/divide.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/monty.h>
18#include <botan/internal/monty_exp.h>
19#include <botan/internal/primality.h>
20#include <botan/internal/workfactor.h>
25class DL_Group_Data
final {
27 DL_Group_Data(
const BigInt& p,
const BigInt& q,
const BigInt& g,
DL_Group_Source source) :
33 m_monty_params(std::make_shared<Montgomery_Params>(m_p, m_mod_p)),
41 ~DL_Group_Data() =
default;
43 DL_Group_Data(
const DL_Group_Data& other) =
delete;
44 DL_Group_Data(DL_Group_Data&& other) =
delete;
45 DL_Group_Data& operator=(
const DL_Group_Data& other) =
delete;
46 DL_Group_Data& operator=(DL_Group_Data&& other) =
delete;
48 const BigInt& p()
const {
return m_p; }
50 const BigInt& q()
const {
return m_q; }
52 const BigInt& g()
const {
return m_g; }
54 BigInt mod_p(
const BigInt& x)
const {
return m_mod_p.
reduce(x); }
56 BigInt multiply_mod_p(
const BigInt& x,
const BigInt& y)
const {
return m_mod_p.
multiply(x, y); }
58 BigInt mod_q(
const BigInt& x)
const {
return m_mod_q.
reduce(x); }
60 BigInt multiply_mod_q(
const BigInt& x,
const BigInt& y)
const {
return m_mod_q.
multiply(x, y); }
62 BigInt square_mod_q(
const BigInt& x)
const {
return m_mod_q.
square(x); }
64 std::shared_ptr<const Montgomery_Params> monty_params_p()
const {
return m_monty_params; }
66 size_t p_bits()
const {
return m_p_bits; }
68 size_t q_bits()
const {
return m_q_bits; }
70 size_t p_bytes()
const {
return (m_p_bits + 7) / 8; }
72 size_t q_bytes()
const {
return (m_q_bits + 7) / 8; }
74 size_t estimated_strength()
const {
return m_estimated_strength; }
76 size_t exponent_bits()
const {
return m_exponent_bits; }
78 BigInt power_g_p(
const BigInt& k,
size_t max_k_bits)
const {
return monty_execute(*m_monty, k, max_k_bits); }
82 BigInt power_b_p(
const BigInt&
b,
const BigInt& k,
size_t max_k_bits)
const {
83 return monty_exp(m_monty_params,
b, k, max_k_bits);
86 BigInt power_b_p_vartime(
const BigInt&
b,
const BigInt& k)
const {
90 bool q_is_set()
const {
return m_q_bits > 0; }
92 void assert_q_is_set(std::string_view function)
const {
93 if(q_is_set() ==
false) {
94 throw Invalid_State(
fmt(
"DL_Group::{}: q is not set for this group", function));
104 Modular_Reducer m_mod_p;
105 Modular_Reducer m_mod_q;
106 std::shared_ptr<const Montgomery_Params> m_monty_params;
107 std::shared_ptr<const Montgomery_Exponentation_State> m_monty;
110 size_t m_estimated_strength;
111 size_t m_exponent_bits;
116std::shared_ptr<DL_Group_Data> DL_Group::BER_decode_DL_group(
const uint8_t data[],
122 BER_Decoder decoder(data, data_len);
123 BER_Decoder ber = decoder.start_sequence();
126 ber.decode(p).decode(q).decode(g).verify_end();
128 ber.decode(p).decode(g).decode(q).discard_remaining();
131 ber.decode(p).decode(g).discard_remaining();
133 throw Invalid_Argument(
"Unknown DL_Group encoding");
136 return std::make_shared<DL_Group_Data>(p, q, g,
source);
140std::shared_ptr<DL_Group_Data> DL_Group::load_DL_group_info(
const char* p_str,
const char* q_str,
const char* g_str) {
141 const BigInt p(p_str);
142 const BigInt q(q_str);
143 const BigInt g(g_str);
149std::shared_ptr<DL_Group_Data> DL_Group::load_DL_group_info(
const char* p_str,
const char* g_str) {
150 const BigInt p(p_str);
151 const BigInt q = (p - 1) / 2;
152 const BigInt g(g_str);
160 if(label ==
"DH PARAMETERS") {
162 }
else if(label ==
"DSA PARAMETERS") {
164 }
else if(label ==
"X942 DH PARAMETERS" || label ==
"X9.42 DH PARAMETERS") {
167 throw Decoding_Error(
fmt(
"DL_Group: Unknown PEM label '{}'", label));
180 if(m_data ==
nullptr) {
190 if(m_data ==
nullptr) {
204 if(e == 0 || r > 0) {
216 throw Internal_Error(
"DL_Group: Couldn't create a suitable generator");
230 throw Invalid_Argument(
fmt(
"DL_Group: requested q size {} is too big for p {}", qbits, pbits));
234 if(qbits != 0 && qbits != pbits - 1) {
235 throw Invalid_Argument(
"Cannot create strong-prime DL_Group with specified q bits");
239 const BigInt q = (p - 1) / 2;
266 while(p.
bits() != pbits || !
is_prime(p, rng, 128,
true)) {
267 X.randomize(rng, pbits);
271 const BigInt g = make_dsa_generator(p, q);
275 qbits = ((pbits <= 1024) ? 160 : 256);
280 const BigInt g = make_dsa_generator(p, q);
294 throw Invalid_Argument(
"DL_Group: The seed given does not generate a DSA group");
297 BigInt g = make_dsa_generator(p, q);
316const DL_Group_Data& DL_Group::data()
const {
321 throw Invalid_State(
"DL_Group uninitialized");
328 if(y <= 1 || y >= p) {
333 if(data().power_b_p_vartime(y, q) != 1) {
345 if(x <= 1 || x >= p) {
359 if(y <= 1 || y >= p || x <= 1 || x >= p) {
376 if(!strong && from_builtin) {
384 if(g < 2 || p < 3 || q < 0) {
388 const size_t test_prob = 128;
391 if(!
is_prime(p, rng, test_prob, is_randomly_generated)) {
396 if((p - 1) % q != 0) {
399 if(data().power_g_p_vartime(q) != 1) {
402 if(!
is_prime(q, rng, test_prob, is_randomly_generated)) {
406 if(!from_builtin && !is_randomly_generated) {
410 const size_t upper_bound = strong ? 1000 : 100;
412 for(
size_t i = 2; i != upper_bound; ++i) {
445 return data().monty_params_p();
449 return data().q_is_set();
453 return data().p_bits();
457 return data().p_bytes();
461 data().assert_q_is_set(
"q_bits");
462 return data().q_bits();
466 data().assert_q_is_set(
"q_bytes");
467 return data().q_bytes();
471 return data().estimated_strength();
475 return data().exponent_bits();
484 return data().mod_p(x);
488 return data().multiply_mod_p(x, y);
492 data().assert_q_is_set(
"inverse_mod_q");
498 data().assert_q_is_set(
"mod_q");
499 return data().mod_q(x);
503 data().assert_q_is_set(
"multiply_mod_q");
504 return data().multiply_mod_q(x, y);
508 data().assert_q_is_set(
"multiply_mod_q");
513 data().assert_q_is_set(
"square_mod_q");
514 return data().square_mod_q(x);
522 return data().power_g_p(x, x.
bits());
526 return data().power_g_p(x, max_x_bits);
534 return data().power_b_p(
b, x, max_x_bits);
538 return data().source();
546 throw Encoding_Error(
"Cannot encode DL_Group in ANSI formats when q param is missing");
549 std::vector<uint8_t> output;
569 const std::vector<uint8_t> encoding =
DER_encode(format);
static BigInt from_word(word n)
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
bool verify_private_element(const BigInt &x) const
std::vector< uint8_t > DER_encode(DL_Group_Format format) const
BigInt power_g_p(const BigInt &x) const
BigInt mod_p(const BigInt &x) const
BigInt multiply_mod_p(const BigInt &x, const BigInt &y) const
std::shared_ptr< const Montgomery_Params > monty_params_p() const
std::string PEM_encode(DL_Group_Format format) const
const BigInt & get_p() const
BigInt multi_exponentiate(const BigInt &x, const BigInt &y, const BigInt &z) const
bool verify_public_element(const BigInt &y) const
BigInt square_mod_q(const BigInt &x) const
void BER_decode(const std::vector< uint8_t > &ber, DL_Group_Format format)
BigInt inverse_mod_q(const BigInt &x) const
bool verify_element_pair(const BigInt &y, const BigInt &x) const
size_t estimated_strength() const
DL_Group_Source source() const
BigInt multiply_mod_q(const BigInt &x, const BigInt &y) const
BigInt inverse_mod_p(const BigInt &x) const
static DL_Group DL_Group_from_PEM(std::string_view pem)
BigInt power_b_p(const BigInt &b, const BigInt &x, size_t max_x_bits) const
size_t exponent_bits() const
BigInt mod_q(const BigInt &x) const
bool verify_group(RandomNumberGenerator &rng, bool strong=true) const
const BigInt & get_g() const
static std::shared_ptr< DL_Group_Data > DL_group_info(std::string_view name)
const BigInt & get_q() const
BigInt square(const BigInt &x) const
BigInt multiply(const BigInt &x, const BigInt &y) const
BigInt reduce(const BigInt &x) const
int(* final)(unsigned char *, CTX *)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
BigInt monty_exp(const std::shared_ptr< const Montgomery_Params > ¶ms_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo, size_t prob)
std::string fmt(std::string_view format, const T &... args)
const size_t PRIME_TABLE_SIZE
size_t dl_work_factor(size_t bits)
BigInt monty_multi_exp(const std::shared_ptr< const Montgomery_Params > ¶ms_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
std::vector< T > unlock(const secure_vector< T > &in)
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)
BigInt monty_exp_vartime(const std::shared_ptr< const Montgomery_Params > ¶ms_p, const BigInt &g, const BigInt &k)
size_t dl_exponent_size(size_t bits)
BigInt monty_execute_vartime(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k)
int32_t jacobi(const BigInt &a, const BigInt &n)
BigInt random_safe_prime(RandomNumberGenerator &rng, size_t bits)
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
BigInt monty_execute(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
std::shared_ptr< const Montgomery_Exponentation_State > monty_precompute(const std::shared_ptr< const Montgomery_Params > ¶ms, const BigInt &g, size_t window_bits, bool const_time)