aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/mac
diff options
context:
space:
mode:
authorMatthias Gierlings <[email protected]>2016-02-03 22:38:41 +0100
committerMatthias Gierlings <[email protected]>2016-10-27 19:42:32 +0200
commit425a2c2497387b7b5804738a77c757b93e630322 (patch)
treedfe9d327cfcf1bfe3a628b8f007200ff18083daf /src/lib/mac
parent1b9d13aed71152d61fab7e0ba016d1951909bac5 (diff)
Added implementation for GMAC
- Added GMAC class - Integrated GMAC into MAC-Class test bench. Run GMAC tests using ./botan-test mac
Diffstat (limited to 'src/lib/mac')
-rw-r--r--src/lib/mac/gmac/gmac.cpp76
-rw-r--r--src/lib/mac/gmac/gmac.h77
-rw-r--r--src/lib/mac/gmac/info.txt6
-rw-r--r--src/lib/mac/mac.cpp4
-rw-r--r--src/lib/mac/mac.h22
5 files changed, 184 insertions, 1 deletions
diff --git a/src/lib/mac/gmac/gmac.cpp b/src/lib/mac/gmac/gmac.cpp
new file mode 100644
index 000000000..5d82456ae
--- /dev/null
+++ b/src/lib/mac/gmac/gmac.cpp
@@ -0,0 +1,76 @@
+/*
+ * GMAC
+ * (C) 2016 Matthias Gierlings, René Korthaus
+ *
+ * Botan is released under the Simplified BSD License (see license.txt)
+ */
+
+#include <botan/gmac.h>
+
+namespace Botan {
+
+GMAC* GMAC::make(const Spec& spec)
+ {
+ if(spec.arg_count() == 1)
+ {
+ if(auto bc = BlockCipher::create(spec.arg(0)))
+ return new GMAC(bc.release());
+ }
+ return nullptr;
+ }
+
+GMAC::GMAC(BlockCipher* cipher)
+ : m_iv(), m_aad(),
+ m_gcm(GCM_Encryption(cipher)), m_cipher(cipher->clone())
+ {
+ }
+
+void GMAC::clear()
+ {
+ m_gcm.clear();
+ zeroise(m_iv);
+ zeroise(m_aad);
+ }
+
+std::string GMAC::name() const
+ {
+ return "GMAC(" + m_cipher->name() + ")";
+ }
+
+size_t GMAC::output_length() const
+ {
+ return m_gcm.tag_size();
+ }
+
+void GMAC::add_data(const byte input[], size_t length)
+ {
+ m_aad.insert(m_aad.end(), input, input + length);
+ }
+
+void GMAC::start(const std::vector<byte>& nonce)
+ {
+ m_iv.assign(nonce.begin(), nonce.end());
+ }
+
+void GMAC::start(const secure_vector<byte>& nonce)
+ {
+ m_iv.assign(nonce.begin(), nonce.end());
+ }
+
+void GMAC::final_result(byte mac[])
+ {
+ secure_vector<byte> result;
+ m_gcm.set_associated_data(m_aad.data(), m_aad.size());
+ m_gcm.start(m_iv);
+ m_gcm.finish(result);
+ std::copy(result.begin(), result.end(), mac);
+
+ zeroise(m_aad);
+ m_aad.clear();
+ }
+
+MessageAuthenticationCode* GMAC::clone() const
+ {
+ return new GMAC(m_cipher->clone());
+ }
+}
diff --git a/src/lib/mac/gmac/gmac.h b/src/lib/mac/gmac/gmac.h
new file mode 100644
index 000000000..d83236b32
--- /dev/null
+++ b/src/lib/mac/gmac/gmac.h
@@ -0,0 +1,77 @@
+/*
+ * GMAC
+ * (C) 2016 Matthias Gierlings, René Korthaus
+ *
+ * Botan is released under the Simplified BSD License (see license.txt)
+ */
+
+#ifndef BOTAN_GMAC_H__
+#define BOTAN_GMAC_H__
+
+#include <botan/types.h>
+#include <botan/mac.h>
+#include <botan/gcm.h>
+#include <algorithm>
+
+namespace Botan {
+
+/**
+* GMAC
+*/
+class BOTAN_DLL GMAC : public MessageAuthenticationCode
+ {
+ public:
+ void clear() override;
+ std::string name() const override;
+ size_t output_length() const override;
+ MessageAuthenticationCode* clone() const override;
+
+ /**
+ * Must be called to set the initialization vector prior to GMAC
+ * calculation.
+ *
+ * @param nonce Initialization vector.
+ */
+ void start(const secure_vector<byte>& nonce);
+
+ /**
+ * Must be called to set the initialization vector prior to GMAC
+ * calculation.
+ *
+ * @param nonce Initialization vector.
+ */
+ void start(const std::vector<byte>& nonce);
+
+ Key_Length_Specification key_spec() const
+ {
+ return m_gcm.key_spec();
+ }
+
+ /**
+ * Creates a new GMAC instance.
+ *
+ * @param cipher the underlying block cipher to use
+ */
+ explicit GMAC(BlockCipher* cipher);
+
+ static GMAC* make(const Spec& spec);
+
+ GMAC(const GMAC&) = delete;
+ GMAC& operator=(const GMAC&) = delete;
+ private:
+ void add_data(const byte[], size_t) override;
+ void final_result(byte[]) override;
+
+ void key_schedule(const byte key[], size_t size) override
+ {
+ m_gcm.set_key(key, size);
+ }
+
+ secure_vector<byte> m_iv;
+ secure_vector<byte> m_aad;
+ GCM_Encryption m_gcm;
+ std::unique_ptr<BlockCipher> m_cipher;
+ };
+
+}
+#endif
diff --git a/src/lib/mac/gmac/info.txt b/src/lib/mac/gmac/info.txt
new file mode 100644
index 000000000..921690c93
--- /dev/null
+++ b/src/lib/mac/gmac/info.txt
@@ -0,0 +1,6 @@
+define GMAC 20160207
+
+<requires>
+gcm
+mac
+</requires>
diff --git a/src/lib/mac/mac.cpp b/src/lib/mac/mac.cpp
index 70807b39f..f2c5557c7 100644
--- a/src/lib/mac/mac.cpp
+++ b/src/lib/mac/mac.cpp
@@ -17,6 +17,10 @@
#include <botan/cmac.h>
#endif
+#if defined(BOTAN_HAS_GMAC)
+ #include <botan/gmac.h>
+#endif
+
#if defined(BOTAN_HAS_HMAC)
#include <botan/hmac.h>
#endif
diff --git a/src/lib/mac/mac.h b/src/lib/mac/mac.h
index f3befc512..9c3614f33 100644
--- a/src/lib/mac/mac.h
+++ b/src/lib/mac/mac.h
@@ -59,7 +59,27 @@ class BOTAN_DLL MessageAuthenticationCode : public Buffered_Computation,
virtual bool verify_mac(const byte in[], size_t length);
/**
- * @return a new object representing the same algorithm as *this
+ * Verify a MAC.
+ * @param in the MAC to verify as a byte array
+ * @return true if the MAC is valid, false otherwise
+ */
+ virtual bool verify_mac(const std::vector<byte>& in)
+ {
+ return verify_mac(in.data(), in.size());
+ }
+
+ /**
+ * Verify a MAC.
+ * @param in the MAC to verify as a byte array
+ * @return true if the MAC is valid, false otherwise
+ */
+ virtual bool verify_mac(const secure_vector<byte>& in)
+ {
+ return verify_mac(in.data(), in.size());
+ }
+
+ /**
+ * Get a new object representing the same algorithm as *this
*/
virtual MessageAuthenticationCode* clone() const = 0;