Botan 3.14.1
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,2024 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/assert.h>
11#include <botan/hmac_drbg.h>
12#include <botan/mac.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
20
22
23RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(std::string_view hash, size_t order_bits, const BigInt& x) :
24 m_qlen(order_bits), m_rlen((m_qlen + 7) / 8), m_rng_in(m_rlen * 2), m_rng_out(m_rlen) {
25 m_hmac_drbg = std::make_unique<HMAC_DRBG>(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash)));
26
27 x.serialize_to(std::span{m_rng_in}.first(m_rlen));
28}
29
31 BOTAN_DEBUG_ASSERT(order.bits() == m_qlen);
32
33 m.serialize_to(std::span{m_rng_in}.last(m_rlen));
34
35 // m_rng_in contains the private key
36 const auto scope = CT::scoped_poison(m_rng_in);
37 m_hmac_drbg->initialize_with(m_rng_in);
38
39 const size_t shift = 8 * m_rlen - m_qlen;
40 BOTAN_ASSERT_NOMSG(shift < 8);
41
42 BigInt k;
43
44 for(;;) {
45 m_hmac_drbg->randomize(m_rng_out);
46 k._assign_from_bytes(m_rng_out);
47
48 if(shift > 0) {
49 k >>= shift;
50 }
51
52 if(k > 0 && k < order) {
53 break;
54 }
55 }
56
57 return k;
58}
59
60#if defined(BOTAN_HAS_ECC_GROUP)
61RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(std::string_view hash, size_t order_bits, const EC_Scalar& scalar) :
62 m_qlen(order_bits), m_rlen((m_qlen + 7) / 8), m_rng_in(m_rlen * 2), m_rng_out(m_rlen) {
63 m_hmac_drbg = std::make_unique<HMAC_DRBG>(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash)));
64
65 scalar.serialize_to(std::span{m_rng_in}.first(m_rlen));
66}
67
69 m.serialize_to(std::span{m_rng_in}.last(m_rlen));
70
71 // m_rng_in contains the private key
72 const auto scope = CT::scoped_poison(m_rng_in);
73 m_hmac_drbg->initialize_with(m_rng_in);
74
75 const size_t shift = 8 * m_rlen - m_qlen;
76 BOTAN_ASSERT_NOMSG(shift < 8);
77
78 for(;;) {
79 m_hmac_drbg->randomize(m_rng_out);
80
81 if(shift > 0) {
82 uint8_t carry = 0;
83 for(uint8_t& b : m_rng_out) {
84 const uint8_t w = b;
85 b = (w >> shift) | carry;
86 carry = w << (8 - shift);
87 }
88 }
89
90 if(auto k = EC_Scalar::deserialize(group, m_rng_out)) {
91 return *k;
92 }
93 }
94}
95#endif
96
97} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
void serialize_to(std::span< uint8_t > out) const
Definition bigint.cpp:396
size_t bits() const
Definition bigint.cpp:308
void _assign_from_bytes(std::span< const uint8_t > bytes)
Definition bigint.h:1044
static std::optional< EC_Scalar > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
void serialize_to(std::span< uint8_t > bytes) const
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:142
RFC6979_Nonce_Generator & operator=(const RFC6979_Nonce_Generator &other)=delete
RFC6979_Nonce_Generator(std::string_view hash, size_t order_bits, const BigInt &x)
Definition rfc6979.cpp:23
BigInt nonce_for(const BigInt &group_order, const BigInt &m)
Definition rfc6979.cpp:30
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53