Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/src/lib/mac/hmac/hmac.cpp
Line
Count
Source
1
/*
2
* HMAC
3
* (C) 1999-2007,2014,2020 Jack Lloyd
4
*     2007 Yves Jerschow
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#include <botan/internal/hmac.h>
10
11
#include <botan/exceptn.h>
12
#include <botan/mem_ops.h>
13
#include <botan/internal/ct_utils.h>
14
#include <botan/internal/fmt.h>
15
16
namespace Botan {
17
18
/*
19
* Update a HMAC Calculation
20
*/
21
2.89M
void HMAC::add_data(std::span<const uint8_t> input) {
22
2.89M
   assert_key_material_set();
23
2.89M
   m_hash->update(input);
24
2.89M
}
25
26
/*
27
* Finalize a HMAC Calculation
28
*/
29
1.97M
void HMAC::final_result(std::span<uint8_t> mac) {
30
1.97M
   assert_key_material_set();
31
1.97M
   m_hash->final(mac);
32
1.97M
   m_hash->update(m_okey);
33
1.97M
   m_hash->update(mac.first(m_hash_output_length));
34
1.97M
   m_hash->final(mac);
35
1.97M
   m_hash->update(m_ikey);
36
1.97M
}
37
38
0
void HMAC::start_msg(std::span<const uint8_t> nonce) {
39
0
   if(!nonce.empty()) {
40
0
      throw Invalid_IV_Length(name(), nonce.size());
41
0
   }
42
0
   assert_key_material_set();
43
44
0
   m_hash->clear();
45
0
   m_hash->update(m_ikey);
46
0
}
47
48
652k
Key_Length_Specification HMAC::key_spec() const {
49
   // Support very long lengths for things like PBKDF2 and the TLS PRF
50
652k
   return Key_Length_Specification(0, 8192);
51
652k
}
52
53
2.26M
size_t HMAC::output_length() const {
54
2.26M
   return m_hash_output_length;
55
2.26M
}
56
57
4.87M
bool HMAC::has_keying_material() const {
58
4.87M
   return !m_okey.empty();
59
4.87M
}
60
61
/*
62
* HMAC Key Schedule
63
*/
64
652k
void HMAC::key_schedule(std::span<const uint8_t> key) {
65
652k
   const uint8_t ipad = 0x36;
66
652k
   const uint8_t opad = 0x5C;
67
68
652k
   m_hash->clear();
69
70
652k
   m_ikey.resize(m_hash_block_size);
71
652k
   m_okey.resize(m_hash_block_size);
72
73
652k
   clear_mem(m_ikey.data(), m_ikey.size());
74
652k
   clear_mem(m_okey.data(), m_okey.size());
75
76
   /*
77
   * Sometimes the HMAC key length itself is sensitive, as with PBKDF2 where it
78
   * reveals the length of the passphrase. Make some attempt to hide this to
79
   * side channels. Clearly if the secret is longer than the block size then the
80
   * branch to hash first reveals that. In addition, counting the number of
81
   * compression functions executed reveals the size at the granularity of the
82
   * hash function's block size.
83
   *
84
   * The greater concern is for smaller keys; being able to detect when a
85
   * passphrase is say 4 bytes may assist choosing weaker targets. Even though
86
   * the loop bounds are constant, we can only actually read key[0..length] so
87
   * it doesn't seem possible to make this computation truly constant time.
88
   *
89
   * We don't mind leaking if the length is exactly zero since that's
90
   * trivial to simply check.
91
   */
92
93
652k
   if(key.size() > m_hash_block_size) {
94
0
      m_hash->update(key);
95
0
      m_hash->final(m_ikey.data());
96
652k
   } else if(key.size() >= 20) {
97
      // For long keys we just leak the length either it is a cryptovariable
98
      // or a long enough password that just the length is not a useful signal
99
652k
      copy_mem(std::span{m_ikey}.first(key.size()), key);
100
652k
   } else if(!key.empty()) {
101
0
      for(size_t i = 0, i_mod_length = 0; i != m_hash_block_size; ++i) {
102
         /*
103
         access key[i % length] but avoiding division due to variable
104
         time computation on some processors.
105
         */
106
0
         auto needs_reduction = CT::Mask<size_t>::is_lte(key.size(), i_mod_length);
107
0
         i_mod_length = needs_reduction.select(0, i_mod_length);
108
0
         const uint8_t kb = key[i_mod_length];
109
110
0
         auto in_range = CT::Mask<size_t>::is_lt(i, key.size());
111
0
         m_ikey[i] = static_cast<uint8_t>(in_range.if_set_return(kb));
112
0
         i_mod_length += 1;
113
0
      }
114
0
   }
115
116
83.2M
   for(size_t i = 0; i != m_hash_block_size; ++i) {
117
82.6M
      m_ikey[i] ^= ipad;
118
82.6M
      m_okey[i] = m_ikey[i] ^ ipad ^ opad;
119
82.6M
   }
120
121
652k
   m_hash->update(m_ikey);
122
652k
}
123
124
/*
125
* Clear memory of sensitive data
126
*/
127
0
void HMAC::clear() {
128
0
   m_hash->clear();
129
0
   zap(m_ikey);
130
0
   zap(m_okey);
131
0
}
132
133
/*
134
* Return the name of this type
135
*/
136
0
std::string HMAC::name() const {
137
0
   return fmt("HMAC({})", m_hash->name());
138
0
}
139
140
/*
141
* Return a new_object of this object
142
*/
143
13.5k
std::unique_ptr<MessageAuthenticationCode> HMAC::new_object() const {
144
13.5k
   return std::make_unique<HMAC>(m_hash->new_object());
145
13.5k
}
146
147
/*
148
* HMAC Constructor
149
*/
150
HMAC::HMAC(std::unique_ptr<HashFunction> hash) :
151
149k
      m_hash(std::move(hash)),
152
149k
      m_hash_output_length(m_hash->output_length()),
153
149k
      m_hash_block_size(m_hash->hash_block_size()) {
154
149k
   BOTAN_ARG_CHECK(m_hash_output_length >= 8, "HMAC is not compatible with this hash function");
155
149k
   BOTAN_ARG_CHECK(m_hash_block_size >= m_hash_output_length, "HMAC is not compatible with this hash function");
156
149k
}
157
158
}  // namespace Botan