11#include <botan/assert.h>
12#include <botan/internal/buffer_stuffer.h>
13#include <botan/internal/divide.h>
14#include <botan/internal/dl_scheme.h>
15#include <botan/internal/keypair.h>
16#include <botan/internal/pk_ops_impl.h>
18#if defined(BOTAN_HAS_RFC6979_GENERATOR)
19 #include <botan/internal/rfc6979.h>
25 return m_public_key->group().q_bytes();
29 return m_public_key->estimated_strength();
33 return m_public_key->p_bits();
37 return m_public_key->get_int_field(
algo_name(), field);
45 return m_public_key->public_key_as_bytes();
49 return m_public_key->DER_encode();
53 return m_public_key->check_key(rng, strong);
57 return std::make_unique<DSA_PrivateKey>(rng, m_public_key->group());
63 BOTAN_ARG_CHECK(m_public_key->group().has_q(),
"Q parameter must be set for DSA");
67 m_public_key = std::make_shared<DL_PublicKey>(group, y);
69 BOTAN_ARG_CHECK(m_public_key->group().has_q(),
"Q parameter must be set for DSA");
75 m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
76 m_public_key = m_private_key->public_key();
82 m_private_key = std::make_shared<DL_PrivateKey>(group, x);
83 m_public_key = m_private_key->public_key();
88 m_public_key = m_private_key->public_key();
90 BOTAN_ARG_CHECK(m_private_key->group().has_q(),
"Q parameter must be set for DSA");
94 if(!m_private_key->check_key(rng, strong)) {
98 if(m_private_key->private_key() >= m_private_key->group().get_q()) {
106 return m_private_key->DER_encode();
110 return m_private_key->raw_private_key_bits();
114 return m_private_key->get_int_field(
algo_name(), field);
119 return std::unique_ptr<DSA_PublicKey>(
new DSA_PublicKey(m_public_key));
129 DSA_Signature_Operation(
const std::shared_ptr<const DL_PrivateKey>& key,
130 std::string_view hash_fn,
132 PK_Ops::Signature_with_Hash(hash_fn), m_key(key) {
134 m_b_inv = m_key->group().inverse_mod_q(m_b);
137 size_t signature_length()
const override {
return 2 * m_key->group().q_bytes(); }
139 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng)
override;
141 AlgorithmIdentifier algorithm_identifier()
const override;
144 std::shared_ptr<const DL_PrivateKey> m_key;
149 const std::string full_name =
"DSA/" + hash_function();
150 const OID oid = OID::from_string(full_name);
151 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
154std::vector<uint8_t> DSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
155 const DL_Group& group = m_key->group();
156 const BigInt& q = group.get_q();
158 BigInt m = BigInt::from_bytes_with_max_bits(msg.data(), msg.size(), group.q_bits());
164#if defined(BOTAN_HAS_RFC6979_GENERATOR)
168 const BigInt k = BigInt::random_integer(rng, 1, q);
171 const BigInt k_inv = group.multiply_mod_q(group.inverse_mod_q(group.mod_q(m_b * k)), m_b);
182 const BigInt r =
ct_modulo(group.power_g_p(k, group.q_bits()), group.get_q());
187 m_b = group.square_mod_q(m_b);
188 m_b_inv = group.square_mod_q(m_b_inv);
190 m = group.multiply_mod_q(m_b, m);
191 const BigInt xr = group.multiply_mod_q(m_b, m_key->private_key(), r);
193 const BigInt s = group.multiply_mod_q(m_b_inv, k_inv, group.mod_q(xr + m));
196 if(r.is_zero() || s.is_zero()) {
200 const size_t q_bytes = q.
bytes();
201 std::vector<uint8_t> sig(2 * q_bytes);
202 BufferStuffer stuffer(sig);
203 r.serialize_to(stuffer.next(q_bytes));
204 s.serialize_to(stuffer.next(q_bytes));
211class DSA_Verification_Operation final :
public PK_Ops::Verification_with_Hash {
213 DSA_Verification_Operation(
const std::shared_ptr<const DL_PublicKey>& key, std::string_view hash_fn) :
214 PK_Ops::Verification_with_Hash(hash_fn), m_key(key) {}
216 DSA_Verification_Operation(
const std::shared_ptr<const DL_PublicKey>& key,
const AlgorithmIdentifier& alg_id) :
217 PK_Ops::Verification_with_Hash(alg_id,
"DSA"), m_key(key) {}
219 bool verify(std::span<const uint8_t> input, std::span<const uint8_t> sig)
override;
222 std::shared_ptr<const DL_PublicKey> m_key;
225bool DSA_Verification_Operation::verify(std::span<const uint8_t> input, std::span<const uint8_t> sig) {
226 const auto group = m_key->group();
228 const BigInt& q = group.get_q();
229 const size_t q_bytes = q.bytes();
231 if(sig.size() != 2 * q_bytes) {
235 const BigInt r(sig.first(q_bytes));
236 BigInt s(sig.last(q_bytes));
238 if(r == 0 || r >= q || s == 0 || s >= q) {
242 BigInt i = BigInt::from_bytes_with_max_bits(input.data(), input.size(), group.q_bits());
247 s = group.inverse_mod_q(s);
249 const BigInt sr = group.multiply_mod_q(s, r);
250 const BigInt si = group.multiply_mod_q(s, i);
252 s = group.multi_exponentiate(si, m_key->public_key(), sr);
255 return (s % group.get_q() == r);
261 std::string_view provider)
const {
262 if(provider ==
"base" || provider.empty()) {
263 return std::make_unique<DSA_Verification_Operation>(this->m_public_key, params);
270 if(provider ==
"base" || provider.empty()) {
271 return std::make_unique<DSA_Verification_Operation>(this->m_public_key, signature_algorithm);
278 std::string_view params,
279 std::string_view provider)
const {
280 if(provider ==
"base" || provider.empty()) {
281 return std::make_unique<DSA_Signature_Operation>(this->m_private_key, params, rng);
#define BOTAN_ARG_CHECK(expr, msg)
virtual OID object_identifier() const
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
static BigInt from_s32(int32_t n)
secure_vector< uint8_t > private_key_bits() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
secure_vector< uint8_t > raw_private_key_bits() const override
std::unique_ptr< Public_Key > public_key() const override
const BigInt & get_int_field(std::string_view field) const override
std::optional< size_t > _signature_element_size_for_DER_encoding() const override
std::vector< uint8_t > raw_public_key_bits() const override
DSA_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
size_t estimated_strength() const override
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
friend class DSA_PrivateKey
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::vector< uint8_t > public_key_bits() const override
AlgorithmIdentifier algorithm_identifier() const override
const BigInt & get_int_field(std::string_view field) const override
std::string algo_name() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
size_t key_length() const override
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
BigInt generate_rfc6979_nonce(const BigInt &x, const BigInt &q, const BigInt &h, std::string_view hash)
BigInt ct_modulo(const BigInt &x, const BigInt &y)
std::vector< T, secure_allocator< T > > secure_vector