aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mac/x919_mac
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-01 21:20:55 +0000
committerlloyd <[email protected]>2014-01-01 21:20:55 +0000
commit197dc467dec28a04c3b2f30da7cef122dfbb13e9 (patch)
treecdbd3ddaec051c72f0a757db461973d90c37b97a /lib/mac/x919_mac
parent62faac373c07cfe10bc8c309e89ebdd30d8e5eaa (diff)
Shuffle things around. Add NIST X.509 test to build.
Diffstat (limited to 'lib/mac/x919_mac')
-rw-r--r--lib/mac/x919_mac/info.txt5
-rw-r--r--lib/mac/x919_mac/x919_mac.cpp103
-rw-r--r--lib/mac/x919_mac/x919_mac.h54
3 files changed, 162 insertions, 0 deletions
diff --git a/lib/mac/x919_mac/info.txt b/lib/mac/x919_mac/info.txt
new file mode 100644
index 000000000..63bf40790
--- /dev/null
+++ b/lib/mac/x919_mac/info.txt
@@ -0,0 +1,5 @@
+define ANSI_X919_MAC 20131128
+
+<requires>
+block
+</requires>
diff --git a/lib/mac/x919_mac/x919_mac.cpp b/lib/mac/x919_mac/x919_mac.cpp
new file mode 100644
index 000000000..faf6138ef
--- /dev/null
+++ b/lib/mac/x919_mac/x919_mac.cpp
@@ -0,0 +1,103 @@
+/*
+* ANSI X9.19 MAC
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/x919_mac.h>
+#include <botan/internal/xor_buf.h>
+#include <algorithm>
+
+namespace Botan {
+
+/*
+* Update an ANSI X9.19 MAC Calculation
+*/
+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;
+
+ 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[0], mac);
+ e->encrypt(mac);
+ zeroise(state);
+ position = 0;
+ }
+
+/*
+* ANSI X9.19 MAC Key Schedule
+*/
+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);
+ }
+
+/*
+* Clear memory of sensitive data
+*/
+void ANSI_X919_MAC::clear()
+ {
+ e->clear();
+ d->clear();
+ zeroise(state);
+ position = 0;
+ }
+
+std::string ANSI_X919_MAC::name() const
+ {
+ return "X9.19-MAC";
+ }
+
+MessageAuthenticationCode* ANSI_X919_MAC::clone() const
+ {
+ return new ANSI_X919_MAC(e->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)
+ {
+ if(e->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/lib/mac/x919_mac/x919_mac.h b/lib/mac/x919_mac/x919_mac.h
new file mode 100644
index 000000000..b7b7d685e
--- /dev/null
+++ b/lib/mac/x919_mac/x919_mac.h
@@ -0,0 +1,54 @@
+/*
+* ANSI X9.19 MAC
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_ANSI_X919_MAC_H__
+#define BOTAN_ANSI_X919_MAC_H__
+
+#include <botan/mac.h>
+#include <botan/block_cipher.h>
+
+namespace Botan {
+
+/**
+* DES/3DES-based MAC from ANSI X9.19
+*/
+class BOTAN_DLL ANSI_X919_MAC : public MessageAuthenticationCode
+ {
+ public:
+ void clear();
+ std::string name() const;
+ size_t output_length() const { return e->block_size(); }
+ MessageAuthenticationCode* clone() const;
+
+ Key_Length_Specification key_spec() const
+ {
+ return Key_Length_Specification(8, 16, 8);
+ }
+
+ /**
+ * @param cipher the underlying block cipher to use
+ */
+ ANSI_X919_MAC(BlockCipher* cipher);
+
+ 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;
+ };
+
+}
+
+#endif