Botan  1.11.15
src/lib/mac/x919_mac/x919_mac.cpp
Go to the documentation of this file.
00001 /*
00002 * ANSI X9.19 MAC
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/mac_utils.h>
00009 #include <botan/x919_mac.h>
00010 
00011 namespace Botan {
00012 
00013 BOTAN_REGISTER_MAC_NAMED_NOARGS(ANSI_X919_MAC, "X9.19-MAC");
00014 
00015 /*
00016 * Update an ANSI X9.19 MAC Calculation
00017 */
00018 void ANSI_X919_MAC::add_data(const byte input[], size_t length)
00019    {
00020    size_t xored = std::min(8 - m_position, length);
00021    xor_buf(&m_state[m_position], input, xored);
00022    m_position += xored;
00023 
00024    if(m_position < 8) return;
00025 
00026    m_des1->encrypt(m_state);
00027    input += xored;
00028    length -= xored;
00029    while(length >= 8)
00030       {
00031       xor_buf(m_state, input, 8);
00032       m_des1->encrypt(m_state);
00033       input += 8;
00034       length -= 8;
00035       }
00036 
00037    xor_buf(m_state, input, length);
00038    m_position = length;
00039    }
00040 
00041 /*
00042 * Finalize an ANSI X9.19 MAC Calculation
00043 */
00044 void ANSI_X919_MAC::final_result(byte mac[])
00045    {
00046    if(m_position)
00047       m_des1->encrypt(m_state);
00048    m_des2->decrypt(&m_state[0], mac);
00049    m_des1->encrypt(mac);
00050    zeroise(m_state);
00051    m_position = 0;
00052    }
00053 
00054 /*
00055 * ANSI X9.19 MAC Key Schedule
00056 */
00057 void ANSI_X919_MAC::key_schedule(const byte key[], size_t length)
00058    {
00059    m_des1->set_key(key, 8);
00060 
00061    if(length == 16)
00062       key += 8;
00063 
00064    m_des2->set_key(key, 8);
00065    }
00066 
00067 /*
00068 * Clear memory of sensitive data
00069 */
00070 void ANSI_X919_MAC::clear()
00071    {
00072    m_des1->clear();
00073    m_des2->clear();
00074    zeroise(m_state);
00075    m_position = 0;
00076    }
00077 
00078 std::string ANSI_X919_MAC::name() const
00079    {
00080    return "X9.19-MAC";
00081    }
00082 
00083 MessageAuthenticationCode* ANSI_X919_MAC::clone() const
00084    {
00085    return new ANSI_X919_MAC;
00086    }
00087 
00088 /*
00089 * ANSI X9.19 MAC Constructor
00090 */
00091 ANSI_X919_MAC::ANSI_X919_MAC() : m_state(8), m_position(0)
00092    {
00093    auto& ciphers = Algo_Registry<BlockCipher>::global_registry();
00094    m_des1.reset(ciphers.make(BlockCipher::Spec("DES"), ""));
00095    m_des2.reset(m_des1->clone());
00096    }
00097 
00098 }