aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/mac
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-18 19:45:16 +0000
committerlloyd <[email protected]>2014-01-18 19:45:16 +0000
commitef465af87d61c0cfbba17b86a3e1cc48b90ab391 (patch)
tree151aafc54f2a57c1ca037653b647398616221060 /src/lib/mac
parent1822ba0d828d2c7bec51313597a9a64a54ccc559 (diff)
Use unique_ptr instead of bare pointers and explicit delete in block, mac, hash.
m_ namespaced everything while I'm in there. Changed CMAC poly_double signature.
Diffstat (limited to 'src/lib/mac')
-rw-r--r--src/lib/mac/cbc_mac/cbc_mac.cpp53
-rw-r--r--src/lib/mac/cbc_mac/cbc_mac.h13
-rw-r--r--src/lib/mac/cmac/cmac.cpp104
-rw-r--r--src/lib/mac/cmac/cmac.h19
-rw-r--r--src/lib/mac/hmac/hmac.cpp54
-rw-r--r--src/lib/mac/hmac/hmac.h12
-rw-r--r--src/lib/mac/ssl3mac/ssl3_mac.cpp46
-rw-r--r--src/lib/mac/ssl3mac/ssl3_mac.h5
-rw-r--r--src/lib/mac/x919_mac/info.txt2
-rw-r--r--src/lib/mac/x919_mac/x919_mac.cpp64
-rw-r--r--src/lib/mac/x919_mac/x919_mac.h13
11 files changed, 178 insertions, 207 deletions
diff --git a/src/lib/mac/cbc_mac/cbc_mac.cpp b/src/lib/mac/cbc_mac/cbc_mac.cpp
index 118570e72..7d9a55e28 100644
--- a/src/lib/mac/cbc_mac/cbc_mac.cpp
+++ b/src/lib/mac/cbc_mac/cbc_mac.cpp
@@ -16,26 +16,26 @@ namespace Botan {
*/
void CBC_MAC::add_data(const byte input[], size_t length)
{
- size_t xored = std::min(output_length() - position, length);
- xor_buf(&state[position], input, xored);
- position += xored;
+ size_t xored = std::min(output_length() - m_position, length);
+ xor_buf(&m_state[m_position], input, xored);
+ m_position += xored;
- if(position < output_length())
+ if(m_position < output_length())
return;
- e->encrypt(state);
+ m_cipher->encrypt(m_state);
input += xored;
length -= xored;
while(length >= output_length())
{
- xor_buf(state, input, output_length());
- e->encrypt(state);
+ xor_buf(m_state, input, output_length());
+ m_cipher->encrypt(m_state);
input += output_length();
length -= output_length();
}
- xor_buf(state, input, length);
- position = length;
+ xor_buf(m_state, input, length);
+ m_position = length;
}
/*
@@ -43,12 +43,12 @@ void CBC_MAC::add_data(const byte input[], size_t length)
*/
void CBC_MAC::final_result(byte mac[])
{
- if(position)
- e->encrypt(state);
+ if(m_position)
+ m_cipher->encrypt(m_state);
- copy_mem(mac, &state[0], state.size());
- zeroise(state);
- position = 0;
+ copy_mem(mac, &m_state[0], m_state.size());
+ zeroise(m_state);
+ m_position = 0;
}
/*
@@ -56,7 +56,7 @@ void CBC_MAC::final_result(byte mac[])
*/
void CBC_MAC::key_schedule(const byte key[], size_t length)
{
- e->set_key(key, length);
+ m_cipher->set_key(key, length);
}
/*
@@ -64,9 +64,9 @@ void CBC_MAC::key_schedule(const byte key[], size_t length)
*/
void CBC_MAC::clear()
{
- e->clear();
- zeroise(state);
- position = 0;
+ m_cipher->clear();
+ zeroise(m_state);
+ m_position = 0;
}
/*
@@ -74,7 +74,7 @@ void CBC_MAC::clear()
*/
std::string CBC_MAC::name() const
{
- return "CBC-MAC(" + e->name() + ")";
+ return "CBC-MAC(" + m_cipher->name() + ")";
}
/*
@@ -82,24 +82,15 @@ std::string CBC_MAC::name() const
*/
MessageAuthenticationCode* CBC_MAC::clone() const
{
- return new CBC_MAC(e->clone());
+ return new CBC_MAC(m_cipher->clone());
}
/*
* CBC-MAC Constructor
*/
-CBC_MAC::CBC_MAC(BlockCipher* e_in) :
- e(e_in), state(e->block_size())
+CBC_MAC::CBC_MAC(BlockCipher* cipher) :
+ m_cipher(cipher), m_state(cipher->block_size())
{
- position = 0;
- }
-
-/*
-* CBC-MAC Destructor
-*/
-CBC_MAC::~CBC_MAC()
- {
- delete e;
}
}
diff --git a/src/lib/mac/cbc_mac/cbc_mac.h b/src/lib/mac/cbc_mac/cbc_mac.h
index be25718d9..e7285d0cb 100644
--- a/src/lib/mac/cbc_mac/cbc_mac.h
+++ b/src/lib/mac/cbc_mac/cbc_mac.h
@@ -10,6 +10,7 @@
#include <botan/mac.h>
#include <botan/block_cipher.h>
+#include <memory>
namespace Botan {
@@ -21,27 +22,27 @@ class BOTAN_DLL CBC_MAC : public MessageAuthenticationCode
public:
std::string name() const;
MessageAuthenticationCode* clone() const;
- size_t output_length() const { return e->block_size(); }
+ size_t output_length() const { return m_cipher->block_size(); }
void clear();
Key_Length_Specification key_spec() const
{
- return e->key_spec();
+ return m_cipher->key_spec();
}
/**
* @param cipher the underlying block cipher to use
*/
CBC_MAC(BlockCipher* cipher);
- ~CBC_MAC();
+
private:
void add_data(const byte[], size_t);
void final_result(byte[]);
void key_schedule(const byte[], size_t);
- BlockCipher* e;
- secure_vector<byte> state;
- size_t position;
+ std::unique_ptr<BlockCipher> m_cipher;
+ secure_vector<byte> m_state;
+ size_t m_position = 0;
};
}
diff --git a/src/lib/mac/cmac/cmac.cpp b/src/lib/mac/cmac/cmac.cpp
index 00120cf14..16524faec 100644
--- a/src/lib/mac/cmac/cmac.cpp
+++ b/src/lib/mac/cmac/cmac.cpp
@@ -1,11 +1,12 @@
/*
* CMAC
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/cmac.h>
+#include <botan/loadstor.h>
#include <botan/internal/xor_buf.h>
namespace Botan {
@@ -13,9 +14,10 @@ namespace Botan {
/*
* Perform CMAC's multiplication in GF(2^n)
*/
-secure_vector<byte> CMAC::poly_double(const secure_vector<byte>& in,
- byte polynomial)
+secure_vector<byte> CMAC::poly_double(const secure_vector<byte>& in)
{
+ const byte polynomial = (in.size() == 16) ? 0x87 : 0x1B;
+
const byte poly_xor = (in[0] & 0x80) ? polynomial : 0;
secure_vector<byte> out = in;
@@ -38,24 +40,24 @@ secure_vector<byte> CMAC::poly_double(const secure_vector<byte>& in,
*/
void CMAC::add_data(const byte input[], size_t length)
{
- buffer_insert(buffer, position, input, length);
- if(position + length > output_length())
+ buffer_insert(m_buffer, m_position, input, length);
+ if(m_position + length > output_length())
{
- xor_buf(state, buffer, output_length());
- e->encrypt(state);
- input += (output_length() - position);
- length -= (output_length() - position);
+ xor_buf(m_state, m_buffer, output_length());
+ m_cipher->encrypt(m_state);
+ input += (output_length() - m_position);
+ length -= (output_length() - m_position);
while(length > output_length())
{
- xor_buf(state, input, output_length());
- e->encrypt(state);
+ xor_buf(m_state, input, output_length());
+ m_cipher->encrypt(m_state);
input += output_length();
length -= output_length();
}
- copy_mem(&buffer[0], input, length);
- position = 0;
+ copy_mem(&m_buffer[0], input, length);
+ m_position = 0;
}
- position += length;
+ m_position += length;
}
/*
@@ -63,26 +65,26 @@ void CMAC::add_data(const byte input[], size_t length)
*/
void CMAC::final_result(byte mac[])
{
- xor_buf(state, buffer, position);
+ xor_buf(m_state, m_buffer, m_position);
- if(position == output_length())
+ if(m_position == output_length())
{
- xor_buf(state, B, output_length());
+ xor_buf(m_state, m_B, output_length());
}
else
{
- state[position] ^= 0x80;
- xor_buf(state, P, output_length());
+ m_state[m_position] ^= 0x80;
+ xor_buf(m_state, m_P, output_length());
}
- e->encrypt(state);
+ m_cipher->encrypt(m_state);
for(size_t i = 0; i != output_length(); ++i)
- mac[i] = state[i];
+ mac[i] = m_state[i];
- zeroise(state);
- zeroise(buffer);
- position = 0;
+ zeroise(m_state);
+ zeroise(m_buffer);
+ m_position = 0;
}
/*
@@ -91,10 +93,10 @@ void CMAC::final_result(byte mac[])
void CMAC::key_schedule(const byte key[], size_t length)
{
clear();
- e->set_key(key, length);
- e->encrypt(B);
- B = poly_double(B, polynomial);
- P = poly_double(B, polynomial);
+ m_cipher->set_key(key, length);
+ m_cipher->encrypt(m_B);
+ m_B = poly_double(m_B);
+ m_P = poly_double(m_B);
}
/*
@@ -102,12 +104,12 @@ void CMAC::key_schedule(const byte key[], size_t length)
*/
void CMAC::clear()
{
- e->clear();
- zeroise(state);
- zeroise(buffer);
- zeroise(B);
- zeroise(P);
- position = 0;
+ m_cipher->clear();
+ zeroise(m_state);
+ zeroise(m_buffer);
+ zeroise(m_B);
+ zeroise(m_P);
+ m_position = 0;
}
/*
@@ -115,7 +117,7 @@ void CMAC::clear()
*/
std::string CMAC::name() const
{
- return "CMAC(" + e->name() + ")";
+ return "CMAC(" + m_cipher->name() + ")";
}
/*
@@ -123,34 +125,22 @@ std::string CMAC::name() const
*/
MessageAuthenticationCode* CMAC::clone() const
{
- return new CMAC(e->clone());
+ return new CMAC(m_cipher->clone());
}
/*
* CMAC Constructor
*/
-CMAC::CMAC(BlockCipher* e_in) : e(e_in)
- {
- if(e->block_size() == 16)
- polynomial = 0x87;
- else if(e->block_size() == 8)
- polynomial = 0x1B;
- else
- throw Invalid_Argument("CMAC cannot use the cipher " + e->name());
-
- state.resize(output_length());
- buffer.resize(output_length());
- B.resize(output_length());
- P.resize(output_length());
- position = 0;
- }
-
-/*
-* CMAC Destructor
-*/
-CMAC::~CMAC()
+CMAC::CMAC(BlockCipher* cipher) : m_cipher(cipher)
{
- delete e;
+ if(m_cipher->block_size() != 8 && m_cipher->block_size() != 16)
+ throw Invalid_Argument("CMAC cannot use the cipher " + m_cipher->name());
+
+ m_state.resize(output_length());
+ m_buffer.resize(output_length());
+ m_B.resize(output_length());
+ m_P.resize(output_length());
+ m_position = 0;
}
}
diff --git a/src/lib/mac/cmac/cmac.h b/src/lib/mac/cmac/cmac.h
index c1b75cfa5..f363eade5 100644
--- a/src/lib/mac/cmac/cmac.h
+++ b/src/lib/mac/cmac/cmac.h
@@ -1,6 +1,6 @@
/*
* CMAC
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -10,6 +10,7 @@
#include <botan/mac.h>
#include <botan/block_cipher.h>
+#include <memory>
namespace Botan {
@@ -20,14 +21,14 @@ class BOTAN_DLL CMAC : public MessageAuthenticationCode
{
public:
std::string name() const;
- size_t output_length() const { return e->block_size(); }
+ size_t output_length() const { return m_cipher->block_size(); }
MessageAuthenticationCode* clone() const;
void clear();
Key_Length_Specification key_spec() const
{
- return e->key_spec();
+ return m_cipher->key_spec();
}
/**
@@ -35,8 +36,7 @@ class BOTAN_DLL CMAC : public MessageAuthenticationCode
* @param in the input
* @param polynomial the byte value of the polynomial
*/
- static secure_vector<byte> poly_double(const secure_vector<byte>& in,
- byte polynomial);
+ static secure_vector<byte> poly_double(const secure_vector<byte>& in);
/**
* @param cipher the underlying block cipher to use
@@ -45,17 +45,14 @@ class BOTAN_DLL CMAC : public MessageAuthenticationCode
CMAC(const CMAC&) = delete;
CMAC& operator=(const CMAC&) = delete;
-
- ~CMAC();
private:
void add_data(const byte[], size_t);
void final_result(byte[]);
void key_schedule(const byte[], size_t);
- BlockCipher* e;
- secure_vector<byte> buffer, state, B, P;
- size_t position;
- byte polynomial;
+ std::unique_ptr<BlockCipher> m_cipher;
+ secure_vector<byte> m_buffer, m_state, m_B, m_P;
+ size_t m_position;
};
}
diff --git a/src/lib/mac/hmac/hmac.cpp b/src/lib/mac/hmac/hmac.cpp
index 9e9a643db..6d8e393ae 100644
--- a/src/lib/mac/hmac/hmac.cpp
+++ b/src/lib/mac/hmac/hmac.cpp
@@ -1,6 +1,6 @@
/*
* HMAC
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2014 Jack Lloyd
* 2007 Yves Jerschow
*
* Distributed under the terms of the Botan license
@@ -16,7 +16,7 @@ namespace Botan {
*/
void HMAC::add_data(const byte input[], size_t length)
{
- hash->update(input, length);
+ m_hash->update(input, length);
}
/*
@@ -24,11 +24,11 @@ void HMAC::add_data(const byte input[], size_t length)
*/
void HMAC::final_result(byte mac[])
{
- hash->final(mac);
- hash->update(o_key);
- hash->update(mac, output_length());
- hash->final(mac);
- hash->update(i_key);
+ m_hash->final(mac);
+ m_hash->update(m_okey);
+ m_hash->update(mac, output_length());
+ m_hash->final(mac);
+ m_hash->update(m_ikey);
}
/*
@@ -36,27 +36,27 @@ void HMAC::final_result(byte mac[])
*/
void HMAC::key_schedule(const byte key[], size_t length)
{
- hash->clear();
+ m_hash->clear();
- i_key.resize(hash->hash_block_size());
- o_key.resize(hash->hash_block_size());
+ m_ikey.resize(m_hash->hash_block_size());
+ m_okey.resize(m_hash->hash_block_size());
- std::fill(i_key.begin(), i_key.end(), 0x36);
- std::fill(o_key.begin(), o_key.end(), 0x5C);
+ std::fill(m_ikey.begin(), m_ikey.end(), 0x36);
+ std::fill(m_okey.begin(), m_okey.end(), 0x5C);
- if(length > hash->hash_block_size())
+ if(length > m_hash->hash_block_size())
{
- secure_vector<byte> hmac_key = hash->process(key, length);
- xor_buf(i_key, hmac_key, hmac_key.size());
- xor_buf(o_key, hmac_key, hmac_key.size());
+ secure_vector<byte> hmac_key = m_hash->process(key, length);
+ xor_buf(m_ikey, hmac_key, hmac_key.size());
+ xor_buf(m_okey, hmac_key, hmac_key.size());
}
else
{
- xor_buf(i_key, key, length);
- xor_buf(o_key, key, length);
+ xor_buf(m_ikey, key, length);
+ xor_buf(m_okey, key, length);
}
- hash->update(i_key);
+ m_hash->update(m_ikey);
}
/*
@@ -64,9 +64,9 @@ void HMAC::key_schedule(const byte key[], size_t length)
*/
void HMAC::clear()
{
- hash->clear();
- zap(i_key);
- zap(o_key);
+ m_hash->clear();
+ zap(m_ikey);
+ zap(m_okey);
}
/*
@@ -74,7 +74,7 @@ void HMAC::clear()
*/
std::string HMAC::name() const
{
- return "HMAC(" + hash->name() + ")";
+ return "HMAC(" + m_hash->name() + ")";
}
/*
@@ -82,16 +82,16 @@ std::string HMAC::name() const
*/
MessageAuthenticationCode* HMAC::clone() const
{
- return new HMAC(hash->clone());
+ return new HMAC(m_hash->clone());
}
/*
* HMAC Constructor
*/
-HMAC::HMAC(HashFunction* hash_in) : hash(hash_in)
+HMAC::HMAC(HashFunction* hash) : m_hash(hash)
{
- if(hash->hash_block_size() == 0)
- throw Invalid_Argument("HMAC cannot be used with " + hash->name());
+ if(m_hash->hash_block_size() == 0)
+ throw Invalid_Argument("HMAC cannot be used with " + m_hash->name());
}
}
diff --git a/src/lib/mac/hmac/hmac.h b/src/lib/mac/hmac/hmac.h
index 39a084874..359d4e6f3 100644
--- a/src/lib/mac/hmac/hmac.h
+++ b/src/lib/mac/hmac/hmac.h
@@ -1,6 +1,6 @@
/*
* HMAC
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -10,6 +10,7 @@
#include <botan/mac.h>
#include <botan/hash.h>
+#include <memory>
namespace Botan {
@@ -23,10 +24,11 @@ class BOTAN_DLL HMAC : public MessageAuthenticationCode
std::string name() const;
MessageAuthenticationCode* clone() const;
- size_t output_length() const { return hash->output_length(); }
+ size_t output_length() const { return m_hash->output_length(); }
Key_Length_Specification key_spec() const
{
+ // Absurd max length here is to support PBKDF2
return Key_Length_Specification(0, 512);
}
@@ -37,15 +39,13 @@ class BOTAN_DLL HMAC : public MessageAuthenticationCode
HMAC(const HMAC&) = delete;
HMAC& operator=(const HMAC&) = delete;
-
- ~HMAC() { delete hash; }
private:
void add_data(const byte[], size_t);
void final_result(byte[]);
void key_schedule(const byte[], size_t);
- HashFunction* hash;
- secure_vector<byte> i_key, o_key;
+ std::unique_ptr<HashFunction> m_hash;
+ secure_vector<byte> m_ikey, m_okey;
};
}
diff --git a/src/lib/mac/ssl3mac/ssl3_mac.cpp b/src/lib/mac/ssl3mac/ssl3_mac.cpp
index 64f3103ef..82a26cfaf 100644
--- a/src/lib/mac/ssl3mac/ssl3_mac.cpp
+++ b/src/lib/mac/ssl3mac/ssl3_mac.cpp
@@ -14,7 +14,7 @@ namespace Botan {
*/
void SSL3_MAC::add_data(const byte input[], size_t length)
{
- hash->update(input, length);
+ m_hash->update(input, length);
}
/*
@@ -22,11 +22,11 @@ void SSL3_MAC::add_data(const byte input[], size_t length)
*/
void SSL3_MAC::final_result(byte mac[])
{
- hash->final(mac);
- hash->update(o_key);
- hash->update(mac, output_length());
- hash->final(mac);
- hash->update(i_key);
+ m_hash->final(mac);
+ m_hash->update(m_okey);
+ m_hash->update(mac, output_length());
+ m_hash->final(mac);
+ m_hash->update(m_ikey);
}
/*
@@ -34,22 +34,22 @@ void SSL3_MAC::final_result(byte mac[])
*/
void SSL3_MAC::key_schedule(const byte key[], size_t length)
{
- hash->clear();
+ m_hash->clear();
// Quirk to deal with specification bug
const size_t inner_hash_length =
- (hash->name() == "SHA-160") ? 60 : hash->hash_block_size();
+ (m_hash->name() == "SHA-160") ? 60 : m_hash->hash_block_size();
- i_key.resize(inner_hash_length);
- o_key.resize(inner_hash_length);
+ m_ikey.resize(inner_hash_length);
+ m_okey.resize(inner_hash_length);
- std::fill(i_key.begin(), i_key.end(), 0x36);
- std::fill(o_key.begin(), o_key.end(), 0x5C);
+ std::fill(m_ikey.begin(), m_ikey.end(), 0x36);
+ std::fill(m_okey.begin(), m_okey.end(), 0x5C);
- copy_mem(&i_key[0], key, length);
- copy_mem(&o_key[0], key, length);
+ copy_mem(&m_ikey[0], key, length);
+ copy_mem(&m_okey[0], key, length);
- hash->update(i_key);
+ m_hash->update(m_ikey);
}
/*
@@ -57,9 +57,9 @@ void SSL3_MAC::key_schedule(const byte key[], size_t length)
*/
void SSL3_MAC::clear()
{
- hash->clear();
- zap(i_key);
- zap(o_key);
+ m_hash->clear();
+ zap(m_ikey);
+ zap(m_okey);
}
/*
@@ -67,7 +67,7 @@ void SSL3_MAC::clear()
*/
std::string SSL3_MAC::name() const
{
- return "SSL3-MAC(" + hash->name() + ")";
+ return "SSL3-MAC(" + m_hash->name() + ")";
}
/*
@@ -75,16 +75,16 @@ std::string SSL3_MAC::name() const
*/
MessageAuthenticationCode* SSL3_MAC::clone() const
{
- return new SSL3_MAC(hash->clone());
+ return new SSL3_MAC(m_hash->clone());
}
/*
* SSL3-MAC Constructor
*/
-SSL3_MAC::SSL3_MAC(HashFunction* hash_in) : hash(hash_in)
+SSL3_MAC::SSL3_MAC(HashFunction* hash) : m_hash(hash)
{
- if(hash->hash_block_size() == 0)
- throw Invalid_Argument("SSL3-MAC cannot be used with " + hash->name());
+ if(m_hash->hash_block_size() == 0)
+ throw Invalid_Argument("SSL3-MAC cannot be used with " + m_hash->name());
}
}
diff --git a/src/lib/mac/ssl3mac/ssl3_mac.h b/src/lib/mac/ssl3mac/ssl3_mac.h
index d23ac023c..8ddb13ce8 100644
--- a/src/lib/mac/ssl3mac/ssl3_mac.h
+++ b/src/lib/mac/ssl3mac/ssl3_mac.h
@@ -34,14 +34,13 @@ class BOTAN_DLL SSL3_MAC : public MessageAuthenticationCode
* @param hash the underlying hash to use
*/
SSL3_MAC(HashFunction* hash);
- ~SSL3_MAC() { delete hash; }
private:
void add_data(const byte[], size_t);
void final_result(byte[]);
void key_schedule(const byte[], size_t);
- HashFunction* hash;
- secure_vector<byte> i_key, o_key;
+ std::unique_ptr<HashFunction> m_hash;
+ secure_vector<byte> m_ikey, m_okey;
};
}
diff --git a/src/lib/mac/x919_mac/info.txt b/src/lib/mac/x919_mac/info.txt
index 63bf40790..90d849803 100644
--- a/src/lib/mac/x919_mac/info.txt
+++ b/src/lib/mac/x919_mac/info.txt
@@ -1,5 +1,5 @@
define ANSI_X919_MAC 20131128
<requires>
-block
+des
</requires>
diff --git a/src/lib/mac/x919_mac/x919_mac.cpp b/src/lib/mac/x919_mac/x919_mac.cpp
index faf6138ef..1a6d03761 100644
--- a/src/lib/mac/x919_mac/x919_mac.cpp
+++ b/src/lib/mac/x919_mac/x919_mac.cpp
@@ -16,25 +16,25 @@ namespace Botan {
*/
void ANSI_X919_MAC::add_data(const byte input[], size_t length)
{
- size_t xored = std::min(8 - position, length);
- xor_buf(&state[position], input, xored);
- position += xored;
+ size_t xored = std::min(8 - m_position, length);
+ xor_buf(&m_state[m_position], input, xored);
+ m_position += xored;
- if(position < 8) return;
+ if(m_position < 8) return;
- e->encrypt(state);
+ m_des1->encrypt(m_state);
input += xored;
length -= xored;
while(length >= 8)
{
- xor_buf(state, input, 8);
- e->encrypt(state);
+ xor_buf(m_state, input, 8);
+ m_des1->encrypt(m_state);
input += 8;
length -= 8;
}
- xor_buf(state, input, length);
- position = length;
+ xor_buf(m_state, input, length);
+ m_position = length;
}
/*
@@ -42,12 +42,12 @@ void ANSI_X919_MAC::add_data(const byte input[], size_t length)
*/
void ANSI_X919_MAC::final_result(byte mac[])
{
- if(position)
- e->encrypt(state);
- d->decrypt(&state[0], mac);
- e->encrypt(mac);
- zeroise(state);
- position = 0;
+ if(m_position)
+ m_des1->encrypt(m_state);
+ m_des2->decrypt(&m_state[0], mac);
+ m_des1->encrypt(mac);
+ zeroise(m_state);
+ m_position = 0;
}
/*
@@ -55,9 +55,12 @@ void ANSI_X919_MAC::final_result(byte mac[])
*/
void ANSI_X919_MAC::key_schedule(const byte key[], size_t length)
{
- e->set_key(key, 8);
- if(length == 8) d->set_key(key, 8);
- else d->set_key(key + 8, 8);
+ m_des1->set_key(key, 8);
+
+ if(length == 16)
+ key += 8;
+
+ m_des2->set_key(key, 8);
}
/*
@@ -65,10 +68,10 @@ void ANSI_X919_MAC::key_schedule(const byte key[], size_t length)
*/
void ANSI_X919_MAC::clear()
{
- e->clear();
- d->clear();
- zeroise(state);
- position = 0;
+ m_des1->clear();
+ m_des2->clear();
+ zeroise(m_state);
+ m_position = 0;
}
std::string ANSI_X919_MAC::name() const
@@ -78,26 +81,17 @@ std::string ANSI_X919_MAC::name() const
MessageAuthenticationCode* ANSI_X919_MAC::clone() const
{
- return new ANSI_X919_MAC(e->clone());
+ return new ANSI_X919_MAC(m_des1->clone());
}
/*
* ANSI X9.19 MAC Constructor
*/
-ANSI_X919_MAC::ANSI_X919_MAC(BlockCipher* e_in) :
- e(e_in), d(e->clone()), state(e->block_size()), position(0)
+ANSI_X919_MAC::ANSI_X919_MAC(BlockCipher* cipher) :
+ m_des1(cipher), m_des2(m_des1->clone()), m_state(8), m_position(0)
{
- if(e->name() != "DES")
+ if(cipher->name() != "DES")
throw Invalid_Argument("ANSI X9.19 MAC only supports DES");
}
-/*
-* ANSI X9.19 MAC Destructor
-le*/
-ANSI_X919_MAC::~ANSI_X919_MAC()
- {
- delete e;
- delete d;
- }
-
}
diff --git a/src/lib/mac/x919_mac/x919_mac.h b/src/lib/mac/x919_mac/x919_mac.h
index b7b7d685e..38993af62 100644
--- a/src/lib/mac/x919_mac/x919_mac.h
+++ b/src/lib/mac/x919_mac/x919_mac.h
@@ -10,6 +10,7 @@
#include <botan/mac.h>
#include <botan/block_cipher.h>
+#include <memory>
namespace Botan {
@@ -21,7 +22,8 @@ class BOTAN_DLL ANSI_X919_MAC : public MessageAuthenticationCode
public:
void clear();
std::string name() const;
- size_t output_length() const { return e->block_size(); }
+ size_t output_length() const { return 8; }
+
MessageAuthenticationCode* clone() const;
Key_Length_Specification key_spec() const
@@ -36,17 +38,14 @@ class BOTAN_DLL ANSI_X919_MAC : public MessageAuthenticationCode
ANSI_X919_MAC(const ANSI_X919_MAC&) = delete;
ANSI_X919_MAC& operator=(const ANSI_X919_MAC&) = delete;
-
- ~ANSI_X919_MAC();
private:
void add_data(const byte[], size_t);
void final_result(byte[]);
void key_schedule(const byte[], size_t);
- BlockCipher* e;
- BlockCipher* d;
- secure_vector<byte> state;
- size_t position;
+ std::unique_ptr<BlockCipher> m_des1, m_des2;
+ secure_vector<byte> m_state;
+ size_t m_position;
};
}