aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2013-03-16 22:24:07 +0000
committerlloyd <[email protected]>2013-03-16 22:24:07 +0000
commit78a97a203d3243d8ca48696fe0105b2cf4e4d969 (patch)
treeafc4fd97cd09519eb3cb81c25d3f48a23afab8d6 /src
parent71f17341d8861dd707f2d9efa9902b884f6fc2a9 (diff)
parent158b20308dcc5c81e712e2e496fa0a5944e484a4 (diff)
propagate from branch 'net.randombit.botan.aead-modes' (head 9f75a9bcacc77447b2916104f0ccbabd2c93b516)
to branch 'net.randombit.botan' (head 9e94d89357c08ffc5f7cb5cc3e86c755da96ca8e)
Diffstat (limited to 'src')
-rw-r--r--src/engine/core_engine/core_modes.cpp14
-rw-r--r--src/filters/modes/gcm/gcm.cpp253
-rw-r--r--src/filters/modes/gcm/gcm.h101
-rw-r--r--src/filters/modes/gcm/info.txt6
-rw-r--r--src/selftest/selftest.cpp40
-rw-r--r--src/stream/stream_cipher.h4
6 files changed, 409 insertions, 9 deletions
diff --git a/src/engine/core_engine/core_modes.cpp b/src/engine/core_engine/core_modes.cpp
index 08124a3a0..b30097eaa 100644
--- a/src/engine/core_engine/core_modes.cpp
+++ b/src/engine/core_engine/core_modes.cpp
@@ -44,6 +44,10 @@
#include <botan/ocb.h>
#endif
+#if defined(BOTAN_HAS_GCM)
+ #include <botan/gcm.h>
+#endif
+
#if defined(BOTAN_HAS_XTS)
#include <botan/xts.h>
#endif
@@ -141,6 +145,16 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher,
}
#endif
+#if defined(BOTAN_HAS_GCM)
+ if(mode == "GCM")
+ {
+ if(direction == ENCRYPTION)
+ return new GCM_Encryption(block_cipher->clone(), 16);
+ else
+ return new GCM_Decryption(block_cipher->clone(), 16);
+ }
+#endif
+
#if defined(BOTAN_HAS_XTS)
if(mode == "XTS")
{
diff --git a/src/filters/modes/gcm/gcm.cpp b/src/filters/modes/gcm/gcm.cpp
new file mode 100644
index 000000000..527310069
--- /dev/null
+++ b/src/filters/modes/gcm/gcm.cpp
@@ -0,0 +1,253 @@
+/*
+* GCM Mode Encryption
+* (C) 2013 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/gcm.h>
+#include <botan/ctr.h>
+#include <botan/internal/xor_buf.h>
+#include <botan/loadstor.h>
+
+namespace Botan {
+
+secure_vector<byte>
+gcm_multiply(const secure_vector<byte>& x,
+ const secure_vector<byte>& y)
+ {
+ static const u64bit R = 0xE100000000000000;
+
+ u64bit V[2] = {
+ load_be<u64bit>(&y[0], 0),
+ load_be<u64bit>(&y[0], 1)
+ };
+
+ u64bit Z[2] = { 0, 0 };
+
+ for(size_t i = 0; i != 2; ++i)
+ {
+ u64bit X = load_be<u64bit>(&x[0], i);
+
+ for(size_t j = 0; j != 64; ++j)
+ {
+ if(X >> 63)
+ {
+ Z[0] ^= V[0];
+ Z[1] ^= V[1];
+ }
+
+ const u64bit r = (V[1] & 1) ? R : 0;
+
+ V[1] = (V[0] << 63) | (V[1] >> 1);
+ V[0] = (V[0] >> 1) ^ r;
+
+ X <<= 1;
+ }
+ }
+
+ secure_vector<byte> out(16);
+ store_be<u64bit>(&out[0], Z[0], Z[1]);
+ return out;
+ }
+
+void ghash_update(const secure_vector<byte>& H,
+ secure_vector<byte>& ghash,
+ const byte input[], size_t length)
+ {
+ const size_t BS = 16;
+
+ /*
+ This assumes if less than block size input then we're just on the
+ final block and should pad with zeros
+ */
+ while(length)
+ {
+ const size_t to_proc = std::min(length, BS);
+
+ xor_buf(&ghash[0], &input[0], to_proc);
+
+ ghash = gcm_multiply(ghash, H);
+
+ input += to_proc;
+ length -= to_proc;
+ }
+ }
+
+void ghash_finalize(const secure_vector<byte>& H,
+ secure_vector<byte>& ghash,
+ size_t ad_len, size_t text_len)
+ {
+ secure_vector<byte> final_block(16);
+ store_be<u64bit>(&final_block[0], 8*ad_len, 8*text_len);
+ ghash_update(H, ghash, &final_block[0], final_block.size());
+ }
+
+/*
+* GCM_Mode Constructor
+*/
+GCM_Mode::GCM_Mode(BlockCipher* cipher, size_t tag_size, bool decrypting) :
+ Buffered_Filter(cipher->parallel_bytes(), decrypting ? tag_size : 0),
+ m_tag_size(tag_size), m_cipher_name(cipher->name()),
+ m_H(16), m_H_ad(16), m_mac(16),
+ m_ad_len(0), m_text_len(0),
+ m_ctr_buf(8 * cipher->parallel_bytes())
+ {
+ if(cipher->block_size() != BS)
+ throw std::invalid_argument("OCB requires a 128 bit cipher so cannot be used with " +
+ cipher->name());
+
+ m_ctr.reset(new CTR_BE(cipher)); // CTR_BE takes ownership of cipher
+
+ if(m_tag_size < 8 || m_tag_size > 16)
+ throw Invalid_Argument(name() + ": Bad tag size " + std::to_string(m_tag_size));
+ }
+
+/*
+* Check if a keylength is valid for GCM
+*/
+bool GCM_Mode::valid_keylength(size_t n) const
+ {
+ if(!m_ctr->valid_keylength(n))
+ return false;
+ return true;
+ }
+
+void GCM_Mode::set_key(const SymmetricKey& key)
+ {
+ m_ctr->set_key(key);
+
+ const std::vector<byte> zeros(BS);
+ m_ctr->set_iv(&zeros[0], zeros.size());
+
+ zeroise(m_H);
+ m_ctr->cipher(&m_H[0], &m_H[0], m_H.size());
+ }
+
+/*
+* Set the GCM associated data
+*/
+void GCM_Mode::set_associated_data(const byte ad[], size_t ad_len)
+ {
+ zeroise(m_H_ad);
+
+ ghash_update(m_H, m_H_ad, ad, ad_len);
+ m_ad_len = ad_len;
+ }
+
+/*
+* Set the GCM nonce
+*/
+void GCM_Mode::set_nonce(const byte nonce[], size_t nonce_len)
+ {
+ secure_vector<byte> y0(BS);
+
+ if(nonce_len == 12)
+ {
+ copy_mem(&y0[0], nonce, nonce_len);
+ y0[15] = 1;
+ }
+ else
+ {
+ ghash_update(m_H, y0, nonce, nonce_len);
+ ghash_finalize(m_H, y0, 0, nonce_len);
+ }
+
+ m_ctr->set_iv(&y0[0], y0.size());
+
+ m_enc_y0.resize(BS);
+ m_ctr->encipher(m_enc_y0);
+ }
+
+/*
+* Do setup at the start of each message
+*/
+void GCM_Mode::start_msg()
+ {
+ m_text_len = 0;
+ m_mac = m_H_ad;
+ }
+
+/*
+* Return the name of this cipher mode
+*/
+std::string GCM_Mode::name() const
+ {
+ return (m_cipher_name + "/GCM");
+ }
+
+void GCM_Mode::write(const byte input[], size_t length)
+ {
+ Buffered_Filter::write(input, length);
+ }
+
+void GCM_Mode::end_msg()
+ {
+ Buffered_Filter::end_msg();
+ }
+
+void GCM_Encryption::buffered_block(const byte input[], size_t length)
+ {
+ while(length)
+ {
+ size_t copied = std::min<size_t>(length, m_ctr_buf.size());
+
+ m_ctr->cipher(input, &m_ctr_buf[0], copied);
+ ghash_update(m_H, m_mac, &m_ctr_buf[0], copied);
+ m_text_len += copied;
+
+ send(m_ctr_buf, copied);
+
+ input += copied;
+ length -= copied;
+ }
+ }
+
+void GCM_Encryption::buffered_final(const byte input[], size_t input_length)
+ {
+ buffered_block(input, input_length);
+
+ ghash_finalize(m_H, m_mac, m_ad_len, m_text_len);
+
+ m_mac ^= m_enc_y0;
+
+ send(m_mac, m_tag_size);
+ }
+
+void GCM_Decryption::buffered_block(const byte input[], size_t length)
+ {
+ while(length)
+ {
+ size_t copied = std::min<size_t>(length, m_ctr_buf.size());
+
+ ghash_update(m_H, m_mac, &input[0], copied);
+ m_ctr->cipher(input, &m_ctr_buf[0], copied);
+ m_text_len += copied;
+
+ send(m_ctr_buf, copied);
+
+ input += copied;
+ length -= copied;
+ }
+ }
+
+void GCM_Decryption::buffered_final(const byte input[], size_t input_length)
+ {
+ BOTAN_ASSERT(input_length >= m_tag_size, "Have the tag as part of final input");
+
+ const byte* included_tag = &input[input_length - m_tag_size];
+ input_length -= m_tag_size;
+
+ if(input_length) // handle any remaining input
+ buffered_block(input, input_length);
+
+ ghash_finalize(m_H, m_mac, m_ad_len, m_text_len);
+
+ m_mac ^= m_enc_y0;
+
+ if(!same_mem(&m_mac[0], included_tag, m_tag_size))
+ throw Integrity_Failure("GCM tag check failed");
+ }
+
+
+}
diff --git a/src/filters/modes/gcm/gcm.h b/src/filters/modes/gcm/gcm.h
new file mode 100644
index 000000000..fa13597ce
--- /dev/null
+++ b/src/filters/modes/gcm/gcm.h
@@ -0,0 +1,101 @@
+/*
+* GCM Mode
+* (C) 2013 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_GCM_H__
+#define BOTAN_GCM_H__
+
+#include <botan/aead.h>
+#include <botan/buf_filt.h>
+#include <botan/block_cipher.h>
+#include <botan/stream_cipher.h>
+#include <memory>
+
+namespace Botan {
+
+/**
+* GCM Mode
+*/
+class BOTAN_DLL GCM_Mode : public AEAD_Mode,
+ private Buffered_Filter
+ {
+ public:
+ void set_key(const SymmetricKey& key) override;
+
+ void set_nonce(const byte nonce[], size_t nonce_len) override;
+
+ /**
+ * @note must be called before start_msg or not at all
+ */
+ void set_associated_data(const byte ad[], size_t ad_len) override;
+
+ bool valid_keylength(size_t key_len) const override;
+
+ // GCM supports arbitrary IV lengths
+ bool valid_iv_length(size_t) const override { return true; }
+
+ std::string name() const override;
+ protected:
+ GCM_Mode(BlockCipher* cipher, size_t tag_size, bool decrypting);
+
+ static const size_t BS = 16;
+
+ const size_t m_tag_size;
+ const std::string m_cipher_name;
+
+ std::unique_ptr<StreamCipher> m_ctr;
+ secure_vector<byte> m_H;
+ secure_vector<byte> m_H_ad;
+ secure_vector<byte> m_mac;
+ secure_vector<byte> m_enc_y0;
+ size_t m_ad_len, m_text_len;
+ secure_vector<byte> m_ctr_buf;
+
+ private:
+ void write(const byte[], size_t);
+ void start_msg();
+ void end_msg();
+ };
+
+/**
+* GCM Encryption
+*/
+class BOTAN_DLL GCM_Encryption : public GCM_Mode
+ {
+ public:
+ /**
+ * @param ciph the cipher to use
+ * @param tag_size is how big the auth tag will be
+ */
+ GCM_Encryption(BlockCipher* ciph, size_t tag_size = 16) :
+ GCM_Mode(ciph, tag_size, false) {}
+
+ private:
+ void buffered_block(const byte input[], size_t input_length) override;
+ void buffered_final(const byte input[], size_t input_length) override;
+ };
+
+/**
+* GCM Decryption
+*/
+class BOTAN_DLL GCM_Decryption : public GCM_Mode
+ {
+ public:
+ /**
+ * @param ciph the cipher to use
+ * @param tag_size is how big the auth tag will be
+ */
+ GCM_Decryption(BlockCipher* cipher, size_t tag_size = 16) :
+ GCM_Mode(cipher, tag_size, true) {}
+
+ private:
+ void buffered_block(const byte input[], size_t input_length) override;
+ void buffered_final(const byte input[], size_t input_length) override;
+ };
+
+}
+
+#endif
diff --git a/src/filters/modes/gcm/info.txt b/src/filters/modes/gcm/info.txt
new file mode 100644
index 000000000..52e677a03
--- /dev/null
+++ b/src/filters/modes/gcm/info.txt
@@ -0,0 +1,6 @@
+define GCM
+
+<requires>
+block
+ctr
+</requires>
diff --git a/src/selftest/selftest.cpp b/src/selftest/selftest.cpp
index 4be0fa4cf..e291a8e9d 100644
--- a/src/selftest/selftest.cpp
+++ b/src/selftest/selftest.cpp
@@ -1,12 +1,14 @@
/*
* Startup Self Tests
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2013 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/selftest.h>
#include <botan/filters.h>
+#include <botan/aead.h>
+#include <botan/hex.h>
#include <botan/internal/core_engine.h>
#include <botan/internal/stl_util.h>
@@ -23,17 +25,24 @@ std::string test_filter_kat(Filter* filter,
const std::string& input,
const std::string& expected)
{
- Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder);
- pipe.process_msg(input);
+ try
+ {
+ Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder);
+ pipe.process_msg(input);
- const std::string got = pipe.read_all_as_string();
+ const std::string got = pipe.read_all_as_string();
- const bool same = (got == expected);
+ const bool same = (got == expected);
- if(same)
- return "passed";
- else
- return (std::string("got ") + got + " expected " + expected);
+ if(same)
+ return "passed";
+ else
+ return (std::string("got ") + got + " expected " + expected);
+ }
+ catch(std::exception& e)
+ {
+ return std::string("exception ") + e.what();
+ }
}
}
@@ -117,6 +126,19 @@ algorithm_kat_detailed(const SCAN_Name& algo_name,
else if(!dec->valid_iv_length(0))
throw Invalid_IV_Length(algo, iv.length());
+#if defined(BOTAN_HAS_AEAD)
+
+ if(AEAD_Mode* enc_aead = dynamic_cast<AEAD_Mode*>(enc))
+ {
+ const std::vector<byte> ad = hex_decode(search_map(vars, std::string("ad")));
+
+ enc_aead->set_associated_data(&ad[0], ad.size());
+
+ if(AEAD_Mode* dec_aead = dynamic_cast<AEAD_Mode*>(dec))
+ dec_aead->set_associated_data(&ad[0], ad.size());
+ }
+#endif
+
all_results[provider + " (encrypt)"] = test_filter_kat(enc, input, output);
all_results[provider + " (decrypt)"] = test_filter_kat(dec, output, input);
}
diff --git a/src/stream/stream_cipher.h b/src/stream/stream_cipher.h
index 301e71f07..231414589 100644
--- a/src/stream/stream_cipher.h
+++ b/src/stream/stream_cipher.h
@@ -34,6 +34,10 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm
void cipher1(byte buf[], size_t len)
{ cipher(buf, buf, len); }
+ template<typename Alloc>
+ void encipher(std::vector<byte, Alloc>& inout)
+ { cipher(&inout[0], &inout[0], inout.size()); }
+
/**
* Resync the cipher using the IV
* @param iv the initialization vector