diff options
author | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
commit | 9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch) | |
tree | fe5e8ae9813b853549558b59833022e87e83981b /src/mac | |
parent | 9822a701516396b7de4e41339faecd48ff8dc8ff (diff) |
Move all modules into src/ directory
Diffstat (limited to 'src/mac')
-rw-r--r-- | src/mac/cbc_mac/cbc_mac.cpp | 109 | ||||
-rw-r--r-- | src/mac/cbc_mac/cbc_mac.h | 36 | ||||
-rw-r--r-- | src/mac/cbc_mac/modinfo.txt | 10 | ||||
-rw-r--r-- | src/mac/cmac/cmac.cpp | 152 | ||||
-rw-r--r-- | src/mac/cmac/cmac.h | 41 | ||||
-rw-r--r-- | src/mac/cmac/modinfo.txt | 10 | ||||
-rw-r--r-- | src/mac/hmac/hmac.cpp | 97 | ||||
-rw-r--r-- | src/mac/hmac/hmac.h | 34 | ||||
-rw-r--r-- | src/mac/hmac/modinfo.txt | 10 | ||||
-rw-r--r-- | src/mac/ssl3mac/modinfo.txt | 10 | ||||
-rw-r--r-- | src/mac/ssl3mac/ssl3_mac.cpp | 88 | ||||
-rw-r--r-- | src/mac/ssl3mac/ssl3_mac.h | 34 | ||||
-rw-r--r-- | src/mac/x919_mac/modinfo.txt | 10 | ||||
-rw-r--r-- | src/mac/x919_mac/x919_mac.cpp | 92 | ||||
-rw-r--r-- | src/mac/x919_mac/x919_mac.h | 36 |
15 files changed, 769 insertions, 0 deletions
diff --git a/src/mac/cbc_mac/cbc_mac.cpp b/src/mac/cbc_mac/cbc_mac.cpp new file mode 100644 index 000000000..d5275b0ed --- /dev/null +++ b/src/mac/cbc_mac/cbc_mac.cpp @@ -0,0 +1,109 @@ +/************************************************* +* CBC-MAC Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/cbc_mac.h> +#include <botan/lookup.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/************************************************* +* Update an CBC-MAC Calculation * +*************************************************/ +void CBC_MAC::add_data(const byte input[], u32bit length) + { + u32bit xored = std::min(OUTPUT_LENGTH - position, length); + xor_buf(state + position, input, xored); + position += xored; + + if(position < OUTPUT_LENGTH) + return; + + e->encrypt(state); + input += xored; + length -= xored; + while(length >= OUTPUT_LENGTH) + { + xor_buf(state, input, OUTPUT_LENGTH); + e->encrypt(state); + input += OUTPUT_LENGTH; + length -= OUTPUT_LENGTH; + } + + xor_buf(state, input, length); + position = length; + } + +/************************************************* +* Finalize an CBC-MAC Calculation * +*************************************************/ +void CBC_MAC::final_result(byte mac[]) + { + if(position) + e->encrypt(state); + + copy_mem(mac, state.begin(), state.size()); + state.clear(); + position = 0; + } + +/************************************************* +* CBC-MAC Key Schedule * +*************************************************/ +void CBC_MAC::key(const byte key[], u32bit length) + { + e->set_key(key, length); + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void CBC_MAC::clear() throw() + { + e->clear(); + state.clear(); + position = 0; + } + +/************************************************* +* Return the name of this type * +*************************************************/ +std::string CBC_MAC::name() const + { + return "CBC-MAC(" + e->name() + ")"; + } + +/************************************************* +* Return a clone of this object * +*************************************************/ +MessageAuthenticationCode* CBC_MAC::clone() const + { + return new CBC_MAC(e->name()); + } + +/************************************************* +* CBC-MAC Constructor * +*************************************************/ +CBC_MAC::CBC_MAC(const std::string& cipher) : + MessageAuthenticationCode(block_size_of(cipher), + min_keylength_of(cipher), + max_keylength_of(cipher), + keylength_multiple_of(cipher)), + state(block_size_of(cipher)) + { + e = get_block_cipher(cipher); + position = 0; + } + +/************************************************* +* CBC-MAC Destructor * +*************************************************/ +CBC_MAC::~CBC_MAC() + { + delete e; + } + +} diff --git a/src/mac/cbc_mac/cbc_mac.h b/src/mac/cbc_mac/cbc_mac.h new file mode 100644 index 000000000..a5646d07a --- /dev/null +++ b/src/mac/cbc_mac/cbc_mac.h @@ -0,0 +1,36 @@ +/************************************************* +* CBC-MAC Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_CBC_MAC__ +#define BOTAN_CBC_MAC__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CBC-MAC * +*************************************************/ +class BOTAN_DLL CBC_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + CBC_MAC(const std::string&); + ~CBC_MAC(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + + BlockCipher* e; + SecureVector<byte> state; + u32bit position; + }; + +} + +#endif diff --git a/src/mac/cbc_mac/modinfo.txt b/src/mac/cbc_mac/modinfo.txt new file mode 100644 index 000000000..3a7a6e781 --- /dev/null +++ b/src/mac/cbc_mac/modinfo.txt @@ -0,0 +1,10 @@ +realname "CBC-MAC" + +define CBC_MAC + +load_on auto + +<add> +cbc_mac.cpp +cbc_mac.h +</add> diff --git a/src/mac/cmac/cmac.cpp b/src/mac/cmac/cmac.cpp new file mode 100644 index 000000000..5a99f93b1 --- /dev/null +++ b/src/mac/cmac/cmac.cpp @@ -0,0 +1,152 @@ +/************************************************* +* CMAC Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/cmac.h> +#include <botan/lookup.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/************************************************* +* Perform CMAC's multiplication in GF(2^n) * +*************************************************/ +SecureVector<byte> CMAC::poly_double(const MemoryRegion<byte>& in, + byte polynomial) + { + const bool do_xor = (in[0] & 0x80) ? true : false; + + SecureVector<byte> out = in; + + byte carry = 0; + for(u32bit j = out.size(); j != 0; --j) + { + byte temp = out[j-1]; + out[j-1] = (temp << 1) | carry; + carry = (temp >> 7); + } + + if(do_xor) + out[out.size()-1] ^= polynomial; + + return out; + } + +/************************************************* +* Update an CMAC Calculation * +*************************************************/ +void CMAC::add_data(const byte input[], u32bit length) + { + buffer.copy(position, input, length); + if(position + length > OUTPUT_LENGTH) + { + xor_buf(state, buffer, OUTPUT_LENGTH); + e->encrypt(state); + input += (OUTPUT_LENGTH - position); + length -= (OUTPUT_LENGTH - position); + while(length > OUTPUT_LENGTH) + { + xor_buf(state, input, OUTPUT_LENGTH); + e->encrypt(state); + input += OUTPUT_LENGTH; + length -= OUTPUT_LENGTH; + } + buffer.copy(input, length); + position = 0; + } + position += length; + } + +/************************************************* +* Finalize an CMAC Calculation * +*************************************************/ +void CMAC::final_result(byte mac[]) + { + xor_buf(state, buffer, position); + + if(position == OUTPUT_LENGTH) + { + xor_buf(state, B, OUTPUT_LENGTH); + } + else + { + state[position] ^= 0x80; + xor_buf(state, P, OUTPUT_LENGTH); + } + + e->encrypt(state); + + for(u32bit j = 0; j != OUTPUT_LENGTH; ++j) + mac[j] = state[j]; + + state.clear(); + buffer.clear(); + position = 0; + } + +/************************************************* +* CMAC Key Schedule * +*************************************************/ +void CMAC::key(const byte key[], u32bit length) + { + clear(); + e->set_key(key, length); + e->encrypt(B); + B = poly_double(B, polynomial); + P = poly_double(B, polynomial); + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void CMAC::clear() throw() + { + e->clear(); + state.clear(); + buffer.clear(); + B.clear(); + P.clear(); + position = 0; + } + +/************************************************* +* Return the name of this type * +*************************************************/ +std::string CMAC::name() const + { + return "CMAC(" + e->name() + ")"; + } + +/************************************************* +* Return a clone of this object * +*************************************************/ +MessageAuthenticationCode* CMAC::clone() const + { + return new CMAC(e->name()); + } + +/************************************************* +* CMAC Constructor * +*************************************************/ +CMAC::CMAC(const std::string& bc_name) : + MessageAuthenticationCode(block_size_of(bc_name), + min_keylength_of(bc_name), + max_keylength_of(bc_name), + keylength_multiple_of(bc_name)) + { + e = get_block_cipher(bc_name); + + 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.create(OUTPUT_LENGTH); + buffer.create(OUTPUT_LENGTH); + B.create(OUTPUT_LENGTH); + P.create(OUTPUT_LENGTH); + position = 0; + } + +} diff --git a/src/mac/cmac/cmac.h b/src/mac/cmac/cmac.h new file mode 100644 index 000000000..c7f107258 --- /dev/null +++ b/src/mac/cmac/cmac.h @@ -0,0 +1,41 @@ +/************************************************* +* CMAC Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_CMAC_H__ +#define BOTAN_CMAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CMAC * +*************************************************/ +class BOTAN_DLL CMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + + static SecureVector<byte> poly_double(const MemoryRegion<byte>& in, + byte polynomial); + + CMAC(const std::string&); + ~CMAC() { delete e; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + + BlockCipher* e; + SecureVector<byte> buffer, state, B, P; + u32bit position; + byte polynomial; + }; + +} + +#endif diff --git a/src/mac/cmac/modinfo.txt b/src/mac/cmac/modinfo.txt new file mode 100644 index 000000000..5188af0c0 --- /dev/null +++ b/src/mac/cmac/modinfo.txt @@ -0,0 +1,10 @@ +realname "CMAC" + +define CMAC + +load_on auto + +<add> +cmac.cpp +cmac.h +</add> diff --git a/src/mac/hmac/hmac.cpp b/src/mac/hmac/hmac.cpp new file mode 100644 index 000000000..b8c76e8f6 --- /dev/null +++ b/src/mac/hmac/hmac.cpp @@ -0,0 +1,97 @@ +/************************************************* +* HMAC Source File * +* (C) 1999-2007 Jack Lloyd * +* 2007 Yves Jerschow * +*************************************************/ + +#include <botan/hmac.h> +#include <botan/lookup.h> +#include <botan/xor_buf.h> + +namespace Botan { + +/************************************************* +* Update a HMAC Calculation * +*************************************************/ +void HMAC::add_data(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/************************************************* +* Finalize a HMAC Calculation * +*************************************************/ +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); + } + +/************************************************* +* HMAC Key Schedule * +*************************************************/ +void HMAC::key(const byte key[], u32bit length) + { + hash->clear(); + std::fill(i_key.begin(), i_key.end(), 0x36); + std::fill(o_key.begin(), o_key.end(), 0x5C); + + if(length > hash->HASH_BLOCK_SIZE) + { + SecureVector<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()); + } + else + { + xor_buf(i_key, key, length); + xor_buf(o_key, key, length); + } + + hash->update(i_key); + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void HMAC::clear() throw() + { + hash->clear(); + i_key.clear(); + o_key.clear(); + } + +/************************************************* +* Return the name of this type * +*************************************************/ +std::string HMAC::name() const + { + return "HMAC(" + hash->name() + ")"; + } + +/************************************************* +* Return a clone of this object * +*************************************************/ +MessageAuthenticationCode* HMAC::clone() const + { + return new HMAC(hash->name()); + } + +/************************************************* +* HMAC Constructor * +*************************************************/ +HMAC::HMAC(const std::string& hash_name) : + MessageAuthenticationCode(output_length_of(hash_name), + 1, 2*block_size_of(hash_name)), + hash(get_hash(hash_name)) + { + if(hash->HASH_BLOCK_SIZE == 0) + throw Invalid_Argument("HMAC cannot be used with " + hash->name()); + i_key.create(hash->HASH_BLOCK_SIZE); + o_key.create(hash->HASH_BLOCK_SIZE); + } + +} diff --git a/src/mac/hmac/hmac.h b/src/mac/hmac/hmac.h new file mode 100644 index 000000000..62529cf13 --- /dev/null +++ b/src/mac/hmac/hmac.h @@ -0,0 +1,34 @@ +/************************************************* +* HMAC Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_HMAC_H__ +#define BOTAN_HMAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* HMAC * +*************************************************/ +class BOTAN_DLL HMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + HMAC(const std::string&); + ~HMAC() { delete hash; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/src/mac/hmac/modinfo.txt b/src/mac/hmac/modinfo.txt new file mode 100644 index 000000000..534d2e036 --- /dev/null +++ b/src/mac/hmac/modinfo.txt @@ -0,0 +1,10 @@ +realname "HMAC" + +define HMAC + +load_on auto + +<add> +hmac.cpp +hmac.h +</add> diff --git a/src/mac/ssl3mac/modinfo.txt b/src/mac/ssl3mac/modinfo.txt new file mode 100644 index 000000000..d7a86e571 --- /dev/null +++ b/src/mac/ssl3mac/modinfo.txt @@ -0,0 +1,10 @@ +realname "SSLv3 MAC" + +define SSL3_MAC + +load_on auto + +<add> +ssl3_mac.cpp +ssl3_mac.h +</add> diff --git a/src/mac/ssl3mac/ssl3_mac.cpp b/src/mac/ssl3mac/ssl3_mac.cpp new file mode 100644 index 000000000..ceb04bf44 --- /dev/null +++ b/src/mac/ssl3mac/ssl3_mac.cpp @@ -0,0 +1,88 @@ +/************************************************* +* SSL3-MAC Source File * +* (C) 1999-2004 Jack Lloyd * +*************************************************/ + +#include <botan/ssl3_mac.h> +#include <botan/lookup.h> + +namespace Botan { + +/************************************************* +* Update a SSL3-MAC Calculation * +*************************************************/ +void SSL3_MAC::add_data(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/************************************************* +* Finalize a SSL3-MAC Calculation * +*************************************************/ +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); + } + +/************************************************* +* SSL3-MAC Key Schedule * +*************************************************/ +void SSL3_MAC::key(const byte key[], u32bit length) + { + hash->clear(); + std::fill(i_key.begin(), i_key.end(), 0x36); + std::fill(o_key.begin(), o_key.end(), 0x5C); + + i_key.copy(key, length); + o_key.copy(key, length); + hash->update(i_key); + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void SSL3_MAC::clear() throw() + { + hash->clear(); + i_key.clear(); + o_key.clear(); + } + +/************************************************* +* Return the name of this type * +*************************************************/ +std::string SSL3_MAC::name() const + { + return "SSL3-MAC(" + hash->name() + ")"; + } + +/************************************************* +* Return a clone of this object * +*************************************************/ +MessageAuthenticationCode* SSL3_MAC::clone() const + { + return new SSL3_MAC(hash->name()); + } + +/************************************************* +* SSL3-MAC Constructor * +*************************************************/ +SSL3_MAC::SSL3_MAC(const std::string& hash_name) : + MessageAuthenticationCode(output_length_of(hash_name), + output_length_of(hash_name)), + hash(get_hash(hash_name)) + { + if(hash->name() != "MD5" && hash->name() != "SHA-160") + throw Invalid_Argument("SSL3-MAC cannot be used with " + hash->name()); + + const u32bit INNER_HASH_LENGTH = (hash->name() == "MD5") ? 64 : 60; + + i_key.create(INNER_HASH_LENGTH); + o_key.create(INNER_HASH_LENGTH); + } + +} diff --git a/src/mac/ssl3mac/ssl3_mac.h b/src/mac/ssl3mac/ssl3_mac.h new file mode 100644 index 000000000..8ab08c97d --- /dev/null +++ b/src/mac/ssl3mac/ssl3_mac.h @@ -0,0 +1,34 @@ +/************************************************* +* SSL3-MAC Header File * +* (C) 1999-2004 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_SSL3_MAC_H__ +#define BOTAN_SSL3_MAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* SSL3-MAC * +*************************************************/ +class SSL3_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + SSL3_MAC(const std::string&); + ~SSL3_MAC() { delete hash; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/src/mac/x919_mac/modinfo.txt b/src/mac/x919_mac/modinfo.txt new file mode 100644 index 000000000..24c78b1c6 --- /dev/null +++ b/src/mac/x919_mac/modinfo.txt @@ -0,0 +1,10 @@ +realname "ANSI X9.19 MAC" + +define ANSI_X919_MAC + +load_on auto + +<add> +x919_mac.cpp +x919_mac.h +</add> diff --git a/src/mac/x919_mac/x919_mac.cpp b/src/mac/x919_mac/x919_mac.cpp new file mode 100644 index 000000000..92ec7b7b8 --- /dev/null +++ b/src/mac/x919_mac/x919_mac.cpp @@ -0,0 +1,92 @@ +/************************************************* +* ANSI X9.19 MAC Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/x919_mac.h> +#include <botan/lookup.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/************************************************* +* Update an ANSI X9.19 MAC Calculation * +*************************************************/ +void ANSI_X919_MAC::add_data(const byte input[], u32bit length) + { + u32bit xored = std::min(8 - position, length); + xor_buf(state + position, input, xored); + position += xored; + + if(position < 8) return; + + e->encrypt(state); + input += xored; + length -= xored; + while(length >= 8) + { + xor_buf(state, input, 8); + e->encrypt(state); + input += 8; + length -= 8; + } + + xor_buf(state, input, length); + position = length; + } + +/************************************************* +* Finalize an ANSI X9.19 MAC Calculation * +*************************************************/ +void ANSI_X919_MAC::final_result(byte mac[]) + { + if(position) + e->encrypt(state); + d->decrypt(state, mac); + e->encrypt(mac); + state.clear(); + position = 0; + } + +/************************************************* +* ANSI X9.19 MAC Key Schedule * +*************************************************/ +void ANSI_X919_MAC::key(const byte key[], u32bit length) + { + e->set_key(key, 8); + if(length == 8) d->set_key(key, 8); + else d->set_key(key + 8, 8); + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void ANSI_X919_MAC::clear() throw() + { + e->clear(); + d->clear(); + state.clear(); + position = 0; + } + +/************************************************* +* ANSI X9.19 MAC Constructor * +*************************************************/ +ANSI_X919_MAC::ANSI_X919_MAC() : MessageAuthenticationCode(8, 8, 16, 8) + { + e = get_block_cipher("DES"); + d = get_block_cipher("DES"); + position = 0; + } + +/************************************************* +* ANSI X9.19 MAC Destructor * +*************************************************/ +ANSI_X919_MAC::~ANSI_X919_MAC() + { + delete e; + delete d; + } + +} diff --git a/src/mac/x919_mac/x919_mac.h b/src/mac/x919_mac/x919_mac.h new file mode 100644 index 000000000..bedb2cf58 --- /dev/null +++ b/src/mac/x919_mac/x919_mac.h @@ -0,0 +1,36 @@ +/************************************************* +* ANSI X9.19 MAC Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_ANSI_X919_MAC_H__ +#define BOTAN_ANSI_X919_MAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* ANSI X9.19 MAC * +*************************************************/ +class BOTAN_DLL ANSI_X919_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const { return "X9.19-MAC"; } + MessageAuthenticationCode* clone() const { return new ANSI_X919_MAC; } + ANSI_X919_MAC(); + ~ANSI_X919_MAC(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + BlockCipher* e; + BlockCipher* d; + SecureBuffer<byte, 8> state; + u32bit position; + }; + +} + +#endif |