aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/modes/aead/aead.cpp14
-rw-r--r--src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp136
-rw-r--r--src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h90
-rw-r--r--src/lib/modes/aead/chacha20poly1305/info.txt6
-rw-r--r--src/tests/data/aead/chacha20poly1305.vec13
5 files changed, 259 insertions, 0 deletions
diff --git a/src/lib/modes/aead/aead.cpp b/src/lib/modes/aead/aead.cpp
index e8316ec63..ee410f031 100644
--- a/src/lib/modes/aead/aead.cpp
+++ b/src/lib/modes/aead/aead.cpp
@@ -29,10 +29,24 @@
#include <botan/ocb.h>
#endif
+#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
+ #include <botan/chacha20poly1305.h>
+#endif
+
namespace Botan {
AEAD_Mode* get_aead(const std::string& algo_spec, Cipher_Dir direction)
{
+#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
+ if(algo_spec == "ChaCha20Poly1305")
+ {
+ if(direction == ENCRYPTION)
+ return new ChaCha20Poly1305_Encryption;
+ else
+ return new ChaCha20Poly1305_Decryption;
+ }
+#endif
+
Algorithm_Factory& af = global_state().algorithm_factory();
const std::vector<std::string> algo_parts = split_on(algo_spec, '/');
diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
new file mode 100644
index 000000000..6f835ca6b
--- /dev/null
+++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
@@ -0,0 +1,136 @@
+/*
+* ChaCha20Poly1305 AEAD
+* (C) 2014 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/chacha20poly1305.h>
+#include <botan/chacha.h>
+#include <botan/poly1305.h>
+#include <botan/loadstor.h>
+#include <algorithm>
+
+namespace Botan {
+
+void ChaCha20Poly1305_Mode::clear()
+ {
+ m_chacha.reset();
+ m_poly1305.reset();
+ m_ad.clear();
+ m_ctext_len = 0;
+ }
+
+void ChaCha20Poly1305_Mode::key_schedule(const byte key[], size_t length)
+ {
+ if(!m_chacha.get())
+ m_chacha.reset(new ChaCha);
+ m_chacha->set_key(key, length);
+ }
+
+void ChaCha20Poly1305_Mode::set_associated_data(const byte ad[], size_t length)
+ {
+ if(m_ctext_len)
+ throw std::runtime_error("Too late to set AD for ChaCha20Poly1305");
+ m_ad.assign(ad, ad + length);
+ }
+
+void ChaCha20Poly1305_Mode::update_len(size_t len)
+ {
+ byte len8[8] = { 0 };
+ store_le(static_cast<u64bit>(len), len8);
+ m_poly1305->update(len8, 8);
+ }
+
+secure_vector<byte> ChaCha20Poly1305_Mode::start_raw(const byte nonce[], size_t nonce_len)
+ {
+ if(!valid_nonce_length(nonce_len))
+ throw Invalid_IV_Length(name(), nonce_len);
+
+ m_ctext_len = 0;
+
+ m_chacha->set_iv(nonce, nonce_len);
+
+ secure_vector<byte> zeros(64);
+ m_chacha->encrypt(zeros);
+
+ if(!m_poly1305.get())
+ m_poly1305.reset(new Poly1305);
+ m_poly1305->set_key(&zeros[0], 32);
+ // Remainder of output is discard
+
+ m_poly1305->update(m_ad);
+ for(size_t i = 0; i != 16 - m_ad.size() % 16; ++i)
+ m_poly1305->update(0);
+
+ return secure_vector<byte>();
+ }
+
+void ChaCha20Poly1305_Encryption::update(secure_vector<byte>& buffer, size_t offset)
+ {
+ BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
+ const size_t sz = buffer.size() - offset;
+ byte* buf = &buffer[offset];
+
+ m_chacha->cipher1(buf, sz);
+ m_poly1305->update(buf, sz); // poly1305 of ciphertext
+ m_ctext_len += sz;
+ }
+
+void ChaCha20Poly1305_Encryption::finish(secure_vector<byte>& buffer, size_t offset)
+ {
+ update(buffer, offset);
+ for(size_t i = 0; i != 16 - m_ctext_len % 16; ++i)
+ m_poly1305->update(0);
+ update_len(m_ad.size());
+ update_len(m_ctext_len);
+
+ const secure_vector<byte> mac = m_poly1305->final();
+ buffer += std::make_pair(&mac[0], tag_size());
+ m_ctext_len = 0;
+ }
+
+void ChaCha20Poly1305_Decryption::update(secure_vector<byte>& buffer, size_t offset)
+ {
+ BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
+ const size_t sz = buffer.size() - offset;
+ byte* buf = &buffer[offset];
+
+ m_poly1305->update(buf, sz); // poly1305 of ciphertext
+ m_chacha->cipher1(buf, sz);
+ m_ctext_len += sz;
+ }
+
+void ChaCha20Poly1305_Decryption::finish(secure_vector<byte>& buffer, size_t offset)
+ {
+ BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
+ const size_t sz = buffer.size() - offset;
+ byte* buf = &buffer[offset];
+
+ BOTAN_ASSERT(sz >= tag_size(), "Have the tag as part of final input");
+
+ const size_t remaining = sz - tag_size();
+
+ if(remaining)
+ {
+ m_poly1305->update(buf, remaining); // poly1305 of ciphertext
+ m_chacha->cipher1(buf, remaining);
+ m_ctext_len += remaining;
+ }
+
+ for(size_t i = 0; i != 16 - m_ctext_len % 16; ++i)
+ m_poly1305->update(0);
+ update_len(m_ad.size());
+ update_len(m_ctext_len);
+ const secure_vector<byte> mac = m_poly1305->final();
+
+ const byte* included_tag = &buf[remaining];
+
+ m_ctext_len = 0;
+
+ if(!same_mem(&mac[0], included_tag, tag_size()))
+ throw Integrity_Failure("ChaCha20Poly1305 tag check failed");
+ buffer.resize(offset + remaining);
+ }
+
+}
diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h
new file mode 100644
index 000000000..de4560be7
--- /dev/null
+++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h
@@ -0,0 +1,90 @@
+/*
+* ChaCha20Poly1305 AEAD
+* (C) 2014 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_AEAD_CHACHA20_POLY1305_H__
+#define BOTAN_AEAD_CHACHA20_POLY1305_H__
+
+#include <botan/aead.h>
+#include <botan/stream_cipher.h>
+#include <botan/mac.h>
+
+namespace Botan {
+
+/**
+* Base class
+* See draft-irtf-cfrg-chacha20-poly1305-03 for specification
+*/
+class BOTAN_DLL ChaCha20Poly1305_Mode : public AEAD_Mode
+ {
+ public:
+ void set_associated_data(const byte ad[], size_t ad_len) override;
+
+ std::string name() const override { return "ChaCha20Poly1305"; }
+
+ size_t update_granularity() const override { return 64; }
+
+ Key_Length_Specification key_spec() const override
+ { return Key_Length_Specification(32); }
+
+ bool valid_nonce_length(size_t n) const override
+ { return (n == 12); }
+
+ size_t tag_size() const override { return 16; }
+
+ void clear() override;
+ protected:
+ std::unique_ptr<StreamCipher> m_chacha;
+ std::unique_ptr<MessageAuthenticationCode> m_poly1305;
+
+ secure_vector<byte> m_ad;
+ size_t m_ctext_len = 0;
+
+ void update_len(size_t len);
+ private:
+ secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override;
+
+ void key_schedule(const byte key[], size_t length) override;
+ };
+
+/**
+* ChaCha20Poly1305 Encryption
+*/
+class BOTAN_DLL ChaCha20Poly1305_Encryption : public ChaCha20Poly1305_Mode
+ {
+ public:
+ size_t output_length(size_t input_length) const override
+ { return input_length + tag_size(); }
+
+ size_t minimum_final_size() const override { return 0; }
+
+ void update(secure_vector<byte>& blocks, size_t offset = 0) override;
+
+ void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
+ };
+
+/**
+* ChaCha20Poly1305 Decryption
+*/
+class BOTAN_DLL ChaCha20Poly1305_Decryption : public ChaCha20Poly1305_Mode
+ {
+ public:
+ size_t output_length(size_t input_length) const override
+ {
+ BOTAN_ASSERT(input_length > tag_size(), "Sufficient input");
+ return input_length - tag_size();
+ }
+
+ size_t minimum_final_size() const override { return tag_size(); }
+
+ void update(secure_vector<byte>& blocks, size_t offset = 0) override;
+
+ void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
+ };
+
+}
+
+#endif
diff --git a/src/lib/modes/aead/chacha20poly1305/info.txt b/src/lib/modes/aead/chacha20poly1305/info.txt
new file mode 100644
index 000000000..f03f2797e
--- /dev/null
+++ b/src/lib/modes/aead/chacha20poly1305/info.txt
@@ -0,0 +1,6 @@
+define AEAD_CHACHA20_POLY1305 20141228
+
+<requires>
+chacha
+poly1305
+</requires>
diff --git a/src/tests/data/aead/chacha20poly1305.vec b/src/tests/data/aead/chacha20poly1305.vec
new file mode 100644
index 000000000..5b5f8397d
--- /dev/null
+++ b/src/tests/data/aead/chacha20poly1305.vec
@@ -0,0 +1,13 @@
+[ChaCha20Poly1305]
+# draft-irtf-cfrg-chacha20-poly1305-03
+In = 4C616469657320616E642047656E746C656D656E206F662074686520636C617373206F66202739393A204966204920636F756C64206F6666657220796F75206F6E6C79206F6E652074697020666F7220746865206675747572652C2073756E73637265656E20776F756C642062652069742E
+AD = 50515253C0C1C2C3C4C5C6C7
+Key = 808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F
+Nonce = 070000004041424344454647
+Out = D31A8D34648E60DB7B86AFBC53EF7EC2A4ADED51296E08FEA9E2B5A736EE62D63DBEA45E8CA9671282FAFB69DA92728B1A71DE0A9E060B2905D6A5B67ECD3B3692DDBD7F2D778B8C9803AEE328091B58FAB324E4FAD675945585808B4831D7BC3FF4DEF08E4B7A9DE576D26586CEC64B61161AE10B594F09E26A7E902ECBD0600691
+
+Key = 1C9240A5EB55D38AF333888604F6B5F0473917C1402B80099DCA5CBC207075C0
+Nonce = 000000000102030405060708
+AD = F33388860000000000004E91
+In = 496E7465726E65742D4472616674732061726520647261667420646F63756D656E74732076616C696420666F722061206D6178696D756D206F6620736978206D6F6E74687320616E64206D617920626520757064617465642C207265706C616365642C206F72206F62736F6C65746564206279206F7468657220646F63756D656E747320617420616E792074696D652E20497420697320696E617070726F70726961746520746F2075736520496E7465726E65742D447261667473206173207265666572656E6365206D6174657269616C206F7220746F2063697465207468656D206F74686572207468616E206173202FE2809C776F726B20696E2070726F67726573732E2FE2809D
+Out = 64A0861575861AF460F062C79BE643BD5E805CFD345CF389F108670AC76C8CB24C6CFC18755D43EEA09EE94E382D26B0BDB7B73C321B0100D4F03B7F355894CF332F830E710B97CE98C8A84ABD0B948114AD176E008D33BD60F982B1FF37C8559797A06EF4F0EF61C186324E2B3506383606907B6A7C02B0F9F6157B53C867E4B9166C767B804D46A59B5216CDE7A4E99040C5A40433225EE282A1B0A06C523EAF4534D7F83FA1155B0047718CBC546A0D072B04B3564EEA1B422273F548271A0BB2316053FA76991955EBD63159434ECEBB4E466DAE5A1073A6727627097A1049E617D91D361094FA68F0FF77987130305BEABA2EDA04DF997B714D6C6F2C29A6AD5CB4022B02709BEEAD9D67890CBB22392336FEA1851F38