Coverage Report

Created: 2023-02-22 06:14

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