diff options
author | lloyd <[email protected]> | 2014-12-29 02:47:28 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-12-29 02:47:28 +0000 |
commit | 28eaccc661862b5e129e1226e2a0ac6af99bec75 (patch) | |
tree | 3cd2e30d6a05d48aa87a88fd795bfceb45bf9436 /src/lib | |
parent | 32674508acebe1a0454a1267c01a5fb7db7f485d (diff) |
Add AEAD based on ChaCha20 and Poly1305 defined in draft-irtf-cfrg-chacha20-poly1305-03
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/modes/aead/aead.cpp | 14 | ||||
-rw-r--r-- | src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp | 136 | ||||
-rw-r--r-- | src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h | 90 | ||||
-rw-r--r-- | src/lib/modes/aead/chacha20poly1305/info.txt | 6 |
4 files changed, 246 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> |