Botan 3.3.0
Crypto and TLS for C&
rfc6979.cpp
Go to the documentation of this file.
1/*
2* RFC 6979 Deterministic Nonce Generator
3* (C) 2014,2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/rfc6979.h>
9
10#include <botan/hmac_drbg.h>
11#include <botan/mac.h>
12#include <botan/internal/fmt.h>
13
14namespace Botan {
15
16RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(std::string_view hash, const BigInt& order, const BigInt& x) :
17 m_order(order),
18 m_qlen(m_order.bits()),
19 m_rlen(m_qlen / 8 + (m_qlen % 8 ? 1 : 0)),
20 m_rng_in(m_rlen * 2),
21 m_rng_out(m_rlen) {
22 m_hmac_drbg = std::make_unique<HMAC_DRBG>(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash)));
23
24 BigInt::encode_1363(m_rng_in.data(), m_rlen, x);
25}
26
28
30 BigInt::encode_1363(&m_rng_in[m_rlen], m_rlen, m);
31 m_hmac_drbg->clear();
32 m_hmac_drbg->initialize_with(m_rng_in.data(), m_rng_in.size());
33
34 do {
35 m_hmac_drbg->randomize(m_rng_out.data(), m_rng_out.size());
36 m_k.binary_decode(m_rng_out.data(), m_rng_out.size());
37 m_k >>= (8 * m_rlen - m_qlen);
38 } while(m_k == 0 || m_k >= m_order);
39
40 return m_k;
41}
42
43BigInt generate_rfc6979_nonce(const BigInt& x, const BigInt& q, const BigInt& h, std::string_view hash) {
44 RFC6979_Nonce_Generator gen(hash, q, x);
45 BigInt k = gen.nonce_for(h);
46 return k;
47}
48
49} // namespace Botan
void binary_decode(const uint8_t buf[], size_t length)
Definition bigint.cpp:403
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:105
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:148
RFC6979_Nonce_Generator(std::string_view hash, const BigInt &order, const BigInt &x)
Definition rfc6979.cpp:16
const BigInt & nonce_for(const BigInt &m)
Definition rfc6979.cpp:29
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
BigInt generate_rfc6979_nonce(const BigInt &x, const BigInt &q, const BigInt &h, std::string_view hash)
Definition rfc6979.cpp:43