aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-11-02 13:47:26 -0400
committerJack Lloyd <[email protected]>2017-11-02 13:47:26 -0400
commitd9a456e0b0fe2c3a3f8197f2a3daf7325495010f (patch)
treea34d249299f2f77d36184f85eab733cf95bfb028
parentc9f3da32344479d43c221d035d14ecdd45f6b320 (diff)
Minor SIV/CMAC optimizations
-rw-r--r--src/lib/mac/cmac/cmac.cpp21
-rw-r--r--src/lib/modes/aead/siv/siv.cpp9
2 files changed, 17 insertions, 13 deletions
diff --git a/src/lib/mac/cmac/cmac.cpp b/src/lib/mac/cmac/cmac.cpp
index 9e0a01853..18f7c151c 100644
--- a/src/lib/mac/cmac/cmac.cpp
+++ b/src/lib/mac/cmac/cmac.cpp
@@ -25,19 +25,21 @@ secure_vector<uint8_t> CMAC::poly_double(const secure_vector<uint8_t>& in)
*/
void CMAC::add_data(const uint8_t input[], size_t length)
{
+ const size_t bs = output_length();
+
buffer_insert(m_buffer, m_position, input, length);
- if(m_position + length > output_length())
+ if(m_position + length > bs)
{
- xor_buf(m_state, m_buffer, output_length());
+ xor_buf(m_state, m_buffer, bs);
m_cipher->encrypt(m_state);
- input += (output_length() - m_position);
- length -= (output_length() - m_position);
- while(length > output_length())
+ input += (bs - m_position);
+ length -= (bs - m_position);
+ while(length > bs)
{
- xor_buf(m_state, input, output_length());
+ xor_buf(m_state, input, bs);
m_cipher->encrypt(m_state);
- input += output_length();
- length -= output_length();
+ input += bs;
+ length -= bs;
}
copy_mem(m_buffer.data(), input, length);
m_position = 0;
@@ -64,8 +66,7 @@ void CMAC::final_result(uint8_t mac[])
m_cipher->encrypt(m_state);
- for(size_t i = 0; i != output_length(); ++i)
- mac[i] = m_state[i];
+ copy_mem(mac, m_state.data(), output_length());
zeroise(m_state);
zeroise(m_buffer);
diff --git a/src/lib/modes/aead/siv/siv.cpp b/src/lib/modes/aead/siv/siv.cpp
index df9a0ef37..3a960e0af 100644
--- a/src/lib/modes/aead/siv/siv.cpp
+++ b/src/lib/modes/aead/siv/siv.cpp
@@ -16,7 +16,7 @@ namespace Botan {
SIV_Mode::SIV_Mode(BlockCipher* cipher) :
m_name(cipher->name() + "/SIV"),
- m_ctr(new CTR_BE(cipher->clone())),
+ m_ctr(new CTR_BE(cipher->clone(), 8)),
m_mac(new CMAC(cipher)),
m_bs(cipher->block_size())
{
@@ -173,8 +173,11 @@ void SIV_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset)
{
BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
- buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
- msg_buf().clear();
+ if(msg_buf().size() > 0)
+ {
+ buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
+ msg_buf().clear();
+ }
const size_t sz = buffer.size() - offset;