aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/mac/cbc_mac/cbc_mac.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-10-26 20:31:30 -0400
committerJack Lloyd <[email protected]>2017-10-26 22:26:15 -0400
commite6d45052efedfe49e99adb6318aaf56e0a9e8d7b (patch)
treec6c3ccd3cff3d04285940bf1d518c809e0653947 /src/lib/mac/cbc_mac/cbc_mac.cpp
parent315b002ecf00f6b6bb0f0d5200d1f39a83527e8f (diff)
Add checks that keyed algorithms are actually keyed before use
Previously calling update or encrypt without calling set_key first would result in invalid outputs or else crashing.
Diffstat (limited to 'src/lib/mac/cbc_mac/cbc_mac.cpp')
-rw-r--r--src/lib/mac/cbc_mac/cbc_mac.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/lib/mac/cbc_mac/cbc_mac.cpp b/src/lib/mac/cbc_mac/cbc_mac.cpp
index b272fe3bc..ba403b564 100644
--- a/src/lib/mac/cbc_mac/cbc_mac.cpp
+++ b/src/lib/mac/cbc_mac/cbc_mac.cpp
@@ -14,6 +14,8 @@ namespace Botan {
*/
void CBC_MAC::add_data(const uint8_t input[], size_t length)
{
+ verify_key_set(m_state.empty() == false);
+
size_t xored = std::min(output_length() - m_position, length);
xor_buf(&m_state[m_position], input, xored);
m_position += xored;
@@ -41,6 +43,8 @@ void CBC_MAC::add_data(const uint8_t input[], size_t length)
*/
void CBC_MAC::final_result(uint8_t mac[])
{
+ verify_key_set(m_state.empty() == false);
+
if(m_position)
m_cipher->encrypt(m_state);
@@ -54,6 +58,7 @@ void CBC_MAC::final_result(uint8_t mac[])
*/
void CBC_MAC::key_schedule(const uint8_t key[], size_t length)
{
+ m_state.resize(m_cipher->block_size());
m_cipher->set_key(key, length);
}
@@ -63,7 +68,7 @@ void CBC_MAC::key_schedule(const uint8_t key[], size_t length)
void CBC_MAC::clear()
{
m_cipher->clear();
- zeroise(m_state);
+ zap(m_state);
m_position = 0;
}
@@ -87,7 +92,7 @@ MessageAuthenticationCode* CBC_MAC::clone() const
* CBC-MAC Constructor
*/
CBC_MAC::CBC_MAC(BlockCipher* cipher) :
- m_cipher(cipher), m_state(cipher->block_size())
+ m_cipher(cipher)
{
}