Coverage Report

Created: 2024-06-28 06:39

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