/src/Botan-3.4.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/internal/ct_utils.h> |
12 | | #include <botan/internal/fmt.h> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | /* |
17 | | * Update a HMAC Calculation |
18 | | */ |
19 | 2.63M | void HMAC::add_data(std::span<const uint8_t> input) { |
20 | 2.63M | assert_key_material_set(); |
21 | 2.63M | m_hash->update(input); |
22 | 2.63M | } |
23 | | |
24 | | /* |
25 | | * Finalize a HMAC Calculation |
26 | | */ |
27 | 1.41M | void HMAC::final_result(std::span<uint8_t> mac) { |
28 | 1.41M | assert_key_material_set(); |
29 | 1.41M | m_hash->final(mac); |
30 | 1.41M | m_hash->update(m_okey); |
31 | 1.41M | m_hash->update(mac.first(m_hash_output_length)); |
32 | 1.41M | m_hash->final(mac); |
33 | 1.41M | m_hash->update(m_ikey); |
34 | 1.41M | } |
35 | | |
36 | 810k | Key_Length_Specification HMAC::key_spec() const { |
37 | | // Support very long lengths for things like PBKDF2 and the TLS PRF |
38 | 810k | return Key_Length_Specification(0, 4096); |
39 | 810k | } |
40 | | |
41 | 1.82M | size_t HMAC::output_length() const { |
42 | 1.82M | return m_hash_output_length; |
43 | 1.82M | } |
44 | | |
45 | 4.05M | bool HMAC::has_keying_material() const { |
46 | 4.05M | return !m_okey.empty(); |
47 | 4.05M | } |
48 | | |
49 | | /* |
50 | | * HMAC Key Schedule |
51 | | */ |
52 | 810k | void HMAC::key_schedule(std::span<const uint8_t> key) { |
53 | 810k | const uint8_t ipad = 0x36; |
54 | 810k | const uint8_t opad = 0x5C; |
55 | | |
56 | 810k | m_hash->clear(); |
57 | | |
58 | 810k | m_ikey.resize(m_hash_block_size); |
59 | 810k | m_okey.resize(m_hash_block_size); |
60 | | |
61 | 810k | clear_mem(m_ikey.data(), m_ikey.size()); |
62 | 810k | 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 | 810k | if(key.size() > m_hash_block_size) { |
82 | 0 | m_hash->update(key); |
83 | 0 | m_hash->final(m_ikey.data()); |
84 | 810k | } else if(!key.empty()) { |
85 | 104M | for(size_t i = 0, i_mod_length = 0; i != m_hash_block_size; ++i) { |
86 | | /* |
87 | | access key[i % length] but avoiding division due to variable |
88 | | time computation on some processors. |
89 | | */ |
90 | 103M | auto needs_reduction = CT::Mask<size_t>::is_lte(key.size(), i_mod_length); |
91 | 103M | i_mod_length = needs_reduction.select(0, i_mod_length); |
92 | 103M | const uint8_t kb = key[i_mod_length]; |
93 | | |
94 | 103M | auto in_range = CT::Mask<size_t>::is_lt(i, key.size()); |
95 | 103M | m_ikey[i] = static_cast<uint8_t>(in_range.if_set_return(kb)); |
96 | 103M | i_mod_length += 1; |
97 | 103M | } |
98 | 810k | } |
99 | | |
100 | 104M | for(size_t i = 0; i != m_hash_block_size; ++i) { |
101 | 103M | m_ikey[i] ^= ipad; |
102 | 103M | m_okey[i] = m_ikey[i] ^ ipad ^ opad; |
103 | 103M | } |
104 | | |
105 | 810k | m_hash->update(m_ikey); |
106 | 810k | } |
107 | | |
108 | | /* |
109 | | * Clear memory of sensitive data |
110 | | */ |
111 | 0 | void HMAC::clear() { |
112 | 0 | m_hash->clear(); |
113 | 0 | zap(m_ikey); |
114 | 0 | zap(m_okey); |
115 | 0 | } |
116 | | |
117 | | /* |
118 | | * Return the name of this type |
119 | | */ |
120 | 0 | std::string HMAC::name() const { |
121 | 0 | return fmt("HMAC({})", m_hash->name()); |
122 | 0 | } |
123 | | |
124 | | /* |
125 | | * Return a new_object of this object |
126 | | */ |
127 | 0 | std::unique_ptr<MessageAuthenticationCode> HMAC::new_object() const { |
128 | 0 | return std::make_unique<HMAC>(m_hash->new_object()); |
129 | 0 | } |
130 | | |
131 | | /* |
132 | | * HMAC Constructor |
133 | | */ |
134 | | HMAC::HMAC(std::unique_ptr<HashFunction> hash) : |
135 | 202k | m_hash(std::move(hash)), |
136 | 202k | m_hash_output_length(m_hash->output_length()), |
137 | 202k | m_hash_block_size(m_hash->hash_block_size()) { |
138 | 202k | BOTAN_ARG_CHECK(m_hash_block_size >= m_hash_output_length, "HMAC is not compatible with this hash function"); |
139 | 202k | } |
140 | | |
141 | | } // namespace Botan |