/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.27M | { |
19 | 1.27M | assert_key_material_set(); |
20 | 1.27M | m_hash->update(input, length); |
21 | 1.27M | } |
22 | | |
23 | | /* |
24 | | * Finalize a HMAC Calculation |
25 | | */ |
26 | | void HMAC::final_result(uint8_t mac[]) |
27 | 322k | { |
28 | 322k | assert_key_material_set(); |
29 | 322k | m_hash->final(mac); |
30 | 322k | m_hash->update(m_okey); |
31 | 322k | m_hash->update(mac, m_hash_output_length); |
32 | 322k | m_hash->final(mac); |
33 | 322k | m_hash->update(m_ikey); |
34 | 322k | } |
35 | | |
36 | | Key_Length_Specification HMAC::key_spec() const |
37 | 4.91k | { |
38 | | // Support very long lengths for things like PBKDF2 and the TLS PRF |
39 | 4.91k | return Key_Length_Specification(0, 4096); |
40 | 4.91k | } |
41 | | |
42 | | size_t HMAC::output_length() const |
43 | 298k | { |
44 | 298k | return m_hash_output_length; |
45 | 298k | } |
46 | | |
47 | | bool HMAC::has_keying_material() const |
48 | 1.59M | { |
49 | 1.59M | return !m_okey.empty(); |
50 | 1.59M | } |
51 | | |
52 | | /* |
53 | | * HMAC Key Schedule |
54 | | */ |
55 | | void HMAC::key_schedule(const uint8_t key[], size_t length) |
56 | 4.58k | { |
57 | 4.58k | const uint8_t ipad = 0x36; |
58 | 4.58k | const uint8_t opad = 0x5C; |
59 | | |
60 | 4.58k | m_hash->clear(); |
61 | | |
62 | 4.58k | m_ikey.resize(m_hash_block_size); |
63 | 4.58k | m_okey.resize(m_hash_block_size); |
64 | | |
65 | 4.58k | clear_mem(m_ikey.data(), m_ikey.size()); |
66 | 4.58k | 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 | 4.58k | if(length > m_hash_block_size) |
86 | 143 | { |
87 | 143 | m_hash->update(key, length); |
88 | 143 | m_hash->final(m_ikey.data()); |
89 | 143 | } |
90 | 4.44k | else if(length > 0) |
91 | 3.72k | { |
92 | 278k | for(size_t i = 0, i_mod_length = 0; i != m_hash_block_size; ++i) |
93 | 274k | { |
94 | | /* |
95 | | access key[i % length] but avoiding division due to variable |
96 | | time computation on some processors. |
97 | | */ |
98 | 274k | auto needs_reduction = CT::Mask<size_t>::is_lte(length, i_mod_length); |
99 | 274k | i_mod_length = needs_reduction.select(0, i_mod_length); |
100 | 274k | const uint8_t kb = key[i_mod_length]; |
101 | | |
102 | 274k | auto in_range = CT::Mask<size_t>::is_lt(i, length); |
103 | 274k | m_ikey[i] = static_cast<uint8_t>(in_range.if_set_return(kb)); |
104 | 274k | i_mod_length += 1; |
105 | 274k | } |
106 | 3.72k | } |
107 | | |
108 | 352k | for(size_t i = 0; i != m_hash_block_size; ++i) |
109 | 347k | { |
110 | 347k | m_ikey[i] ^= ipad; |
111 | 347k | m_okey[i] = m_ikey[i] ^ ipad ^ opad; |
112 | 347k | } |
113 | | |
114 | 4.58k | m_hash->update(m_ikey); |
115 | 4.58k | } |
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 | 327 | { |
132 | 327 | return "HMAC(" + m_hash->name() + ")"; |
133 | 327 | } |
134 | | |
135 | | /* |
136 | | * Return a new_object of this object |
137 | | */ |
138 | | std::unique_ptr<MessageAuthenticationCode> HMAC::new_object() const |
139 | 3.19k | { |
140 | 3.19k | return std::make_unique<HMAC>(m_hash->new_object()); |
141 | 3.19k | } |
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 | 6.58k | { |
151 | 6.58k | BOTAN_ARG_CHECK(m_hash_block_size >= m_hash_output_length, |
152 | 6.58k | "HMAC is not compatible with this hash function"); |
153 | 6.58k | } |
154 | | |
155 | | } |