/src/botan/src/lib/pubkey/rfc6979/rfc6979.cpp
Line | Count | Source |
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/fmt.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | 0 | RFC6979_Nonce_Generator::~RFC6979_Nonce_Generator() = default; |
18 | | |
19 | | RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(std::string_view hash, size_t order_bits, const BigInt& x) : |
20 | 0 | m_qlen(order_bits), m_rlen((m_qlen + 7) / 8), m_rng_in(m_rlen * 2), m_rng_out(m_rlen) { |
21 | 0 | m_hmac_drbg = std::make_unique<HMAC_DRBG>(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash))); |
22 | |
|
23 | 0 | x.serialize_to(std::span{m_rng_in}.first(m_rlen)); |
24 | 0 | } |
25 | | |
26 | 0 | BigInt RFC6979_Nonce_Generator::nonce_for(const BigInt& order, const BigInt& m) { |
27 | 0 | BOTAN_DEBUG_ASSERT(order.bits() == m_qlen); |
28 | |
|
29 | 0 | m.serialize_to(std::span{m_rng_in}.last(m_rlen)); |
30 | |
|
31 | 0 | m_hmac_drbg->initialize_with(m_rng_in); |
32 | |
|
33 | 0 | const size_t shift = 8 * m_rlen - m_qlen; |
34 | 0 | BOTAN_ASSERT_NOMSG(shift < 8); |
35 | |
|
36 | 0 | BigInt k; |
37 | |
|
38 | 0 | do { |
39 | 0 | m_hmac_drbg->randomize(m_rng_out); |
40 | 0 | k._assign_from_bytes(m_rng_out); |
41 | |
|
42 | 0 | if(shift > 0) { |
43 | 0 | k >>= shift; |
44 | 0 | } |
45 | 0 | } while(k == 0 || k >= order); |
46 | |
|
47 | 0 | return k; |
48 | 0 | } |
49 | | |
50 | | #if defined(BOTAN_HAS_ECC_GROUP) |
51 | | RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(std::string_view hash, size_t order_bits, const EC_Scalar& scalar) : |
52 | 0 | m_qlen(order_bits), m_rlen((m_qlen + 7) / 8), m_rng_in(m_rlen * 2), m_rng_out(m_rlen) { |
53 | 0 | m_hmac_drbg = std::make_unique<HMAC_DRBG>(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash))); |
54 | |
|
55 | 0 | scalar.serialize_to(std::span{m_rng_in}.first(m_rlen)); |
56 | 0 | } |
57 | | |
58 | 0 | EC_Scalar RFC6979_Nonce_Generator::nonce_for(const EC_Group& group, const EC_Scalar& m) { |
59 | 0 | m.serialize_to(std::span{m_rng_in}.last(m_rlen)); |
60 | |
|
61 | 0 | m_hmac_drbg->initialize_with(m_rng_in); |
62 | |
|
63 | 0 | const size_t shift = 8 * m_rlen - m_qlen; |
64 | 0 | BOTAN_ASSERT_NOMSG(shift < 8); |
65 | |
|
66 | 0 | for(;;) { |
67 | 0 | m_hmac_drbg->randomize(m_rng_out); |
68 | |
|
69 | 0 | if(shift > 0) { |
70 | 0 | uint8_t carry = 0; |
71 | 0 | for(uint8_t& b : m_rng_out) { |
72 | 0 | const uint8_t w = b; |
73 | 0 | b = (w >> shift) | carry; |
74 | 0 | carry = w << (8 - shift); |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | 0 | if(auto k = EC_Scalar::deserialize(group, m_rng_out)) { |
79 | 0 | return *k; |
80 | 0 | } |
81 | 0 | } |
82 | 0 | } |
83 | | #endif |
84 | | |
85 | | } // namespace Botan |