diff options
Diffstat (limited to 'src/modes/aead')
-rw-r--r-- | src/modes/aead/aead.cpp | 78 | ||||
-rw-r--r-- | src/modes/aead/aead.h | 59 | ||||
-rw-r--r-- | src/modes/aead/eax/eax.cpp | 170 | ||||
-rw-r--r-- | src/modes/aead/eax/eax.h | 114 | ||||
-rw-r--r-- | src/modes/aead/eax/info.txt | 7 | ||||
-rw-r--r-- | src/modes/aead/gcm/gcm.cpp | 243 | ||||
-rw-r--r-- | src/modes/aead/gcm/gcm.h | 109 | ||||
-rw-r--r-- | src/modes/aead/gcm/info.txt | 6 | ||||
-rw-r--r-- | src/modes/aead/info.txt | 1 | ||||
-rw-r--r-- | src/modes/aead/ocb/info.txt | 6 | ||||
-rw-r--r-- | src/modes/aead/ocb/ocb.cpp | 444 | ||||
-rw-r--r-- | src/modes/aead/ocb/ocb.h | 124 |
12 files changed, 1361 insertions, 0 deletions
diff --git a/src/modes/aead/aead.cpp b/src/modes/aead/aead.cpp new file mode 100644 index 000000000..1ec7b4a4a --- /dev/null +++ b/src/modes/aead/aead.cpp @@ -0,0 +1,78 @@ +/* +* Interface for AEAD modes +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/aead.h> +#include <botan/libstate.h> + +#if defined(BOTAN_HAS_AEAD_EAX) + #include <botan/eax.h> +#endif + +#if defined(BOTAN_HAS_AEAD_GCM) + #include <botan/gcm.h> +#endif + +#if defined(BOTAN_HAS_AEAD_OCB) + #include <botan/ocb.h> +#endif + +namespace Botan { + +AEAD_Mode* get_aead(const std::string& algo_spec, Cipher_Dir direction) + { + Algorithm_Factory& af = global_state().algorithm_factory(); + + const std::vector<std::string> algo_parts = split_on(algo_spec, '/'); + if(algo_parts.empty()) + throw Invalid_Algorithm_Name(algo_spec); + + if(algo_parts.size() < 2) + return nullptr; + + const std::string cipher_name = algo_parts[0]; + const std::string mode_name = algo_parts[1]; + + const size_t tag_size = 16; // default for all current AEAD + + const BlockCipher* cipher = af.prototype_block_cipher(cipher_name); + if(!cipher) + return nullptr; + +#if defined(BOTAN_HAS_AEAD_EAX) + if(mode_name == "EAX") + { + if(direction == ENCRYPTION) + return new EAX_Encryption(cipher->clone(), tag_size); + else + return new EAX_Decryption(cipher->clone(), tag_size); + } +#endif + +#if defined(BOTAN_HAS_AEAD_GCM) + if(mode_name == "GCM") + { + if(direction == ENCRYPTION) + return new GCM_Encryption(cipher->clone(), tag_size); + else + return new GCM_Decryption(cipher->clone(), tag_size); + } +#endif + +#if defined(BOTAN_HAS_AEAD_OCB) + if(mode_name == "OCB") + { + if(direction == ENCRYPTION) + return new OCB_Encryption(cipher->clone(), tag_size); + else + return new OCB_Decryption(cipher->clone(), tag_size); + } +#endif + + return nullptr; + } + +} diff --git a/src/modes/aead/aead.h b/src/modes/aead/aead.h new file mode 100644 index 000000000..97f156d60 --- /dev/null +++ b/src/modes/aead/aead.h @@ -0,0 +1,59 @@ +/* +* Interface for AEAD modes +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AEAD_MODE_H__ +#define BOTAN_AEAD_MODE_H__ + +#include <botan/transform.h> + +namespace Botan { + +/** +* Interface for AEAD (Authenticated Encryption with Associated Data) +* modes. These modes provide both encryption and message +* authentication, and can authenticate additional per-message data +* which is not included in the ciphertext (for instance a sequence +* number). +*/ +class AEAD_Mode : public Transformation + { + public: + /** + * Set associated data that is not included in the ciphertext but + * that should be authenticated. Must be called after set_key + * and before finish. + * + * Unless reset by another call, the associated data is kept + * between messages. Thus, if the AD does not change, calling + * once (after set_key) is the optimum. + * + * @param ad the associated data + * @param ad_len length of add in bytes + */ + virtual void set_associated_data(const byte ad[], size_t ad_len) = 0; + + template<typename Alloc> + void set_associated_data_vec(const std::vector<byte, Alloc>& ad) + { + set_associated_data(&ad[0], ad.size()); + } + + /** + * Default AEAD nonce size (a commonly supported value among AEAD + * modes, and, large enough that random collisions are unlikely). + */ + size_t default_nonce_size() const override { return 12; } + }; + +/** +* Get an AEAD mode by name (eg "AES-128/GCM" or "Serpent/EAX") +*/ +BOTAN_DLL AEAD_Mode* get_aead(const std::string& name, Cipher_Dir direction); + +} + +#endif diff --git a/src/modes/aead/eax/eax.cpp b/src/modes/aead/eax/eax.cpp new file mode 100644 index 000000000..c6aaa9e85 --- /dev/null +++ b/src/modes/aead/eax/eax.cpp @@ -0,0 +1,170 @@ +/* +* EAX Mode Encryption +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/eax.h> +#include <botan/cmac.h> +#include <botan/ctr.h> +#include <botan/parsing.h> +#include <botan/internal/xor_buf.h> +#include <algorithm> + +namespace Botan { + +namespace { + +/* +* EAX MAC-based PRF +*/ +secure_vector<byte> eax_prf(byte tag, size_t block_size, + MessageAuthenticationCode& mac, + const byte in[], size_t length) + { + for(size_t i = 0; i != block_size - 1; ++i) + mac.update(0); + mac.update(tag); + mac.update(in, length); + return mac.final(); + } + +} + +/* +* EAX_Mode Constructor +*/ +EAX_Mode::EAX_Mode(BlockCipher* cipher, size_t tag_size) : + m_tag_size(tag_size), + m_cipher(cipher), + m_ctr(new CTR_BE(m_cipher->clone())), + m_cmac(new CMAC(m_cipher->clone())) + { + if(tag_size < 8 || tag_size > m_cmac->output_length()) + throw Invalid_Argument(name() + ": Bad tag size " + std::to_string(tag_size)); + } + +void EAX_Mode::clear() + { + m_cipher.reset(); + m_ctr.reset(); + m_cmac.reset(); + zeroise(m_ad_mac); + zeroise(m_nonce_mac); + } + +std::string EAX_Mode::name() const + { + return (m_cipher->name() + "/EAX"); + } + +size_t EAX_Mode::update_granularity() const + { + return 8 * m_cipher->parallel_bytes(); + } + +Key_Length_Specification EAX_Mode::key_spec() const + { + return m_cipher->key_spec(); + } + +/* +* Set the EAX key +*/ +void EAX_Mode::key_schedule(const byte key[], size_t length) + { + /* + * These could share the key schedule, which is one nice part of EAX, + * but it's much easier to ignore that here... + */ + m_ctr->set_key(key, length); + m_cmac->set_key(key, length); + + m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0); + } + +/* +* Set the EAX associated data +*/ +void EAX_Mode::set_associated_data(const byte ad[], size_t length) + { + m_ad_mac = eax_prf(1, block_size(), *m_cmac, ad, length); + } + +secure_vector<byte> EAX_Mode::start(const byte nonce[], size_t nonce_len) + { + if(!valid_nonce_length(nonce_len)) + throw Invalid_IV_Length(name(), nonce_len); + + m_nonce_mac = eax_prf(0, block_size(), *m_cmac, nonce, nonce_len); + + m_ctr->set_iv(&m_nonce_mac[0], m_nonce_mac.size()); + + for(size_t i = 0; i != block_size() - 1; ++i) + m_cmac->update(0); + m_cmac->update(2); + + return secure_vector<byte>(); + } + +void EAX_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_ctr->cipher(buf, buf, sz); + m_cmac->update(buf, sz); + } + +void EAX_Encryption::finish(secure_vector<byte>& buffer, size_t offset) + { + update(buffer, offset); + + secure_vector<byte> data_mac = m_cmac->final(); + xor_buf(data_mac, m_nonce_mac, data_mac.size()); + xor_buf(data_mac, m_ad_mac, data_mac.size()); + + buffer += std::make_pair(&data_mac[0], tag_size()); + } + +void EAX_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_cmac->update(buf, sz); + m_ctr->cipher(buf, buf, sz); + } + +void EAX_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_cmac->update(buf, remaining); + m_ctr->cipher(buf, buf, remaining); + } + + const byte* included_tag = &buf[remaining]; + + secure_vector<byte> mac = m_cmac->final(); + mac ^= m_nonce_mac; + mac ^= m_ad_mac; + + if(!same_mem(&mac[0], included_tag, tag_size())) + throw Integrity_Failure("EAX tag check failed"); + + buffer.resize(offset + remaining); + } + +} diff --git a/src/modes/aead/eax/eax.h b/src/modes/aead/eax/eax.h new file mode 100644 index 000000000..6815e3ce0 --- /dev/null +++ b/src/modes/aead/eax/eax.h @@ -0,0 +1,114 @@ +/* +* EAX Mode +* (C) 1999-2007,2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AEAD_EAX_H__ +#define BOTAN_AEAD_EAX_H__ + +#include <botan/aead.h> +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/mac.h> +#include <memory> + +namespace Botan { + +/** +* EAX base class +*/ +class BOTAN_DLL EAX_Mode : public AEAD_Mode + { + public: + secure_vector<byte> start(const byte nonce[], size_t nonce_len) override; + + void set_associated_data(const byte ad[], size_t ad_len) override; + + std::string name() const override; + + size_t update_granularity() const; + + Key_Length_Specification key_spec() const override; + + // EAX supports arbitrary nonce lengths + bool valid_nonce_length(size_t) const override { return true; } + + void clear(); + protected: + void key_schedule(const byte key[], size_t length) override; + + /** + * @param cipher the cipher to use + * @param tag_size is how big the auth tag will be + */ + EAX_Mode(BlockCipher* cipher, size_t tag_size); + + size_t tag_size() const { return m_tag_size; } + + size_t block_size() const { return m_cipher->block_size(); } + + size_t m_tag_size; + + std::unique_ptr<BlockCipher> m_cipher; + std::unique_ptr<StreamCipher> m_ctr; + std::unique_ptr<MessageAuthenticationCode> m_cmac; + + secure_vector<byte> m_ad_mac; + + secure_vector<byte> m_nonce_mac; + }; + +/** +* EAX Encryption +*/ +class BOTAN_DLL EAX_Encryption : public EAX_Mode + { + public: + /** + * @param cipher a 128-bit block cipher + * @param tag_size is how big the auth tag will be + */ + EAX_Encryption(BlockCipher* cipher, size_t tag_size = 16) : + EAX_Mode(cipher, tag_size) {} + + 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) override; + + void finish(secure_vector<byte>& final_block, size_t offset) override; + }; + +/** +* EAX Decryption +*/ +class BOTAN_DLL EAX_Decryption : public EAX_Mode + { + public: + /** + * @param cipher a 128-bit block cipher + * @param tag_size is how big the auth tag will be + */ + EAX_Decryption(BlockCipher* cipher, size_t tag_size = 16) : + EAX_Mode(cipher, tag_size) {} + + 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) override; + + void finish(secure_vector<byte>& final_block, size_t offset) override; + }; + +} + +#endif diff --git a/src/modes/aead/eax/info.txt b/src/modes/aead/eax/info.txt new file mode 100644 index 000000000..94924e682 --- /dev/null +++ b/src/modes/aead/eax/info.txt @@ -0,0 +1,7 @@ +define AEAD_EAX + +<requires> +block +cmac +ctr +</requires> diff --git a/src/modes/aead/gcm/gcm.cpp b/src/modes/aead/gcm/gcm.cpp new file mode 100644 index 000000000..7b8e0aa36 --- /dev/null +++ b/src/modes/aead/gcm/gcm.cpp @@ -0,0 +1,243 @@ +/* +* 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 { + +namespace { + +void gcm_multiply(secure_vector<byte>& x, + const secure_vector<byte>& h) + { + static const u64bit R = 0xE100000000000000; + + u64bit H[2] = { + load_be<u64bit>(&h[0], 0), + load_be<u64bit>(&h[0], 1) + }; + + u64bit Z[2] = { 0, 0 }; + + // Both CLMUL and SSE2 versions would be useful + + for(size_t i = 0; i != 2; ++i) + { + const u64bit X = load_be<u64bit>(&x[0], i); + + for(size_t j = 0; j != 64; ++j) + { + if((X >> (63-j)) & 1) + { + Z[0] ^= H[0]; + Z[1] ^= H[1]; + } + + const u64bit r = (H[1] & 1) ? R : 0; + + H[1] = (H[0] << 63) | (H[1] >> 1); + H[0] = (H[0] >> 1) ^ r; + } + } + + store_be<u64bit>(&x[0], Z[0], Z[1]); + } + +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); + + 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) : + m_tag_size(tag_size), + m_cipher_name(cipher->name()), + m_H(BS), m_H_ad(BS), m_mac(BS), m_enc_y0(BS), + m_ad_len(0), m_text_len(0) + { + if(cipher->block_size() != BS) + throw std::invalid_argument("GCM 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)); + } + +void GCM_Mode::clear() + { + zeroise(m_H); + zeroise(m_H_ad); + zeroise(m_mac); + zeroise(m_enc_y0); + m_ad_len = 0; + m_text_len = 0; + m_ctr.reset(); + } + +std::string GCM_Mode::name() const + { + return (m_cipher_name + "/GCM"); + } + +size_t GCM_Mode::update_granularity() const + { + return 4096; // CTR-BE's internal block size + } + +Key_Length_Specification GCM_Mode::key_spec() const + { + return m_ctr->key_spec(); + } + +void GCM_Mode::key_schedule(const byte key[], size_t keylen) + { + m_ctr->set_key(key, keylen); + + 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()); + } + +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; + } + +secure_vector<byte> GCM_Mode::start(const byte nonce[], size_t nonce_len) + { + if(!valid_nonce_length(nonce_len)) + throw Invalid_IV_Length(name(), 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()); + + zeroise(m_enc_y0); + m_ctr->encipher(m_enc_y0); + + m_text_len = 0; + m_mac = m_H_ad; + + return secure_vector<byte>(); + } + +void GCM_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_ctr->cipher(buf, buf, sz); + ghash_update(m_H, m_mac, buf, sz); + m_text_len += sz; + } + +void GCM_Encryption::finish(secure_vector<byte>& buffer, size_t offset) + { + update(buffer, offset); + + ghash_finalize(m_H, m_mac, m_ad_len, m_text_len); + + m_mac ^= m_enc_y0; + + buffer += std::make_pair(&m_mac[0], tag_size()); + } + +void GCM_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]; + + ghash_update(m_H, m_mac, buf, sz); + m_ctr->cipher(buf, buf, sz); + m_text_len += sz; + } + +void GCM_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(); + + // handle any final input before the tag + if(remaining) + { + ghash_update(m_H, m_mac, buf, remaining); + m_ctr->cipher(buf, buf, remaining); + m_text_len += remaining; + } + + ghash_finalize(m_H, m_mac, m_ad_len, m_text_len); + + m_mac ^= m_enc_y0; + + const byte* included_tag = &buffer[remaining]; + + if(!same_mem(&m_mac[0], included_tag, tag_size())) + throw Integrity_Failure("GCM tag check failed"); + + buffer.resize(offset + remaining); + } + +} diff --git a/src/modes/aead/gcm/gcm.h b/src/modes/aead/gcm/gcm.h new file mode 100644 index 000000000..e1479c27f --- /dev/null +++ b/src/modes/aead/gcm/gcm.h @@ -0,0 +1,109 @@ +/* +* GCM Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AEAD_GCM_H__ +#define BOTAN_AEAD_GCM_H__ + +#include <botan/aead.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 + { + public: + secure_vector<byte> start(const byte nonce[], size_t nonce_len) override; + + void set_associated_data(const byte ad[], size_t ad_len) override; + + std::string name() const override; + + size_t update_granularity() const; + + Key_Length_Specification key_spec() const override; + + // GCM supports arbitrary nonce lengths + bool valid_nonce_length(size_t) const override { return true; } + + void clear(); + protected: + void key_schedule(const byte key[], size_t length) override; + + GCM_Mode(BlockCipher* cipher, size_t tag_size); + + size_t tag_size() const { return m_tag_size; } + + 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; + }; + +/** +* GCM Encryption +*/ +class BOTAN_DLL GCM_Encryption : public GCM_Mode + { + public: + /** + * @param cipher the 128 bit block cipher to use + * @param tag_size is how big the auth tag will be + */ + GCM_Encryption(BlockCipher* cipher, size_t tag_size = 16) : + GCM_Mode(cipher, tag_size) {} + + 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) override; + + void finish(secure_vector<byte>& final_block, size_t offset) override; + }; + +/** +* GCM Decryption +*/ +class BOTAN_DLL GCM_Decryption : public GCM_Mode + { + public: + /** + * @param cipher the 128 bit block 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) {} + + 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) override; + + void finish(secure_vector<byte>& final_block, size_t offset) override; + }; + +} + +#endif diff --git a/src/modes/aead/gcm/info.txt b/src/modes/aead/gcm/info.txt new file mode 100644 index 000000000..698cd0803 --- /dev/null +++ b/src/modes/aead/gcm/info.txt @@ -0,0 +1,6 @@ +define AEAD_GCM + +<requires> +block +ctr +</requires> diff --git a/src/modes/aead/info.txt b/src/modes/aead/info.txt new file mode 100644 index 000000000..c2985ea85 --- /dev/null +++ b/src/modes/aead/info.txt @@ -0,0 +1 @@ +define AEAD_MODES diff --git a/src/modes/aead/ocb/info.txt b/src/modes/aead/ocb/info.txt new file mode 100644 index 000000000..8d6a93ed9 --- /dev/null +++ b/src/modes/aead/ocb/info.txt @@ -0,0 +1,6 @@ +define AEAD_OCB + +<requires> +block +cmac +</requires> diff --git a/src/modes/aead/ocb/ocb.cpp b/src/modes/aead/ocb/ocb.cpp new file mode 100644 index 000000000..4cbd8bde8 --- /dev/null +++ b/src/modes/aead/ocb/ocb.cpp @@ -0,0 +1,444 @@ +/* +* OCB Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ocb.h> +#include <botan/cmac.h> +#include <botan/internal/xor_buf.h> +#include <botan/internal/bit_ops.h> +#include <algorithm> + +namespace Botan { + +// Has to be in Botan namespace so unique_ptr can reference it +class L_computer + { + public: + L_computer(const BlockCipher& cipher) + { + m_L_star.resize(cipher.block_size()); + cipher.encrypt(m_L_star); + m_L_dollar = poly_double(star()); + m_L.push_back(poly_double(dollar())); + } + + const secure_vector<byte>& star() const { return m_L_star; } + + const secure_vector<byte>& dollar() const { return m_L_dollar; } + + const secure_vector<byte>& operator()(size_t i) const { return get(i); } + + const secure_vector<byte>& get(size_t i) const + { + while(m_L.size() <= i) + m_L.push_back(poly_double(m_L.back())); + + return m_L.at(i); + } + + private: + secure_vector<byte> poly_double(const secure_vector<byte>& in) const + { + return CMAC::poly_double(in, 0x87); + } + + secure_vector<byte> m_L_dollar, m_L_star; + mutable std::vector<secure_vector<byte>> m_L; + }; + +class Nonce_State + { + public: + Nonce_State(const BlockCipher& cipher) : m_cipher(cipher) {} + + secure_vector<byte> update_nonce(const byte nonce[], + size_t nonce_len); + private: + const BlockCipher& m_cipher; + secure_vector<byte> m_last_nonce; + secure_vector<byte> m_stretch; + }; + +secure_vector<byte> +Nonce_State::update_nonce(const byte nonce[], size_t nonce_len) + { + const size_t BS = 16; + + BOTAN_ASSERT(nonce_len < BS, "Nonce is less than 128 bits"); + + secure_vector<byte> nonce_buf(BS); + + copy_mem(&nonce_buf[BS - nonce_len], nonce, nonce_len); + nonce_buf[BS - nonce_len - 1] = 1; + + const byte bottom = nonce_buf[15] & 0x3F; + nonce_buf[15] &= 0xC0; + + const bool need_new_stretch = (m_last_nonce != nonce_buf); + + if(need_new_stretch) + { + m_last_nonce = nonce_buf; + + m_cipher.encrypt(nonce_buf); + + for(size_t i = 0; i != 8; ++i) + nonce_buf.push_back(nonce_buf[i] ^ nonce_buf[i+1]); + + m_stretch = nonce_buf; + } + + // now set the offset from stretch and bottom + + const size_t shift_bytes = bottom / 8; + const size_t shift_bits = bottom % 8; + + secure_vector<byte> offset(BS); + for(size_t i = 0; i != BS; ++i) + { + offset[i] = (m_stretch[i+shift_bytes] << shift_bits); + offset[i] |= (m_stretch[i+shift_bytes+1] >> (8-shift_bits)); + } + + return offset; + } + +namespace { + +/* +* OCB's HASH +*/ +secure_vector<byte> ocb_hash(const L_computer& L, + const BlockCipher& cipher, + const byte ad[], size_t ad_len) + { + const size_t BS = cipher.block_size(); + + secure_vector<byte> sum(BS); + secure_vector<byte> offset(BS); + + secure_vector<byte> buf(BS); + + const size_t ad_blocks = (ad_len / BS); + const size_t ad_remainder = (ad_len % BS); + + for(size_t i = 0; i != ad_blocks; ++i) + { + // this loop could run in parallel + offset ^= L(ctz(i+1)); + + buf = offset; + xor_buf(&buf[0], &ad[BS*i], BS); + + cipher.encrypt(buf); + + sum ^= buf; + } + + if(ad_remainder) + { + offset ^= L.star(); + + buf = offset; + xor_buf(&buf[0], &ad[BS*ad_blocks], ad_remainder); + buf[ad_len % BS] ^= 0x80; + + cipher.encrypt(buf); + + sum ^= buf; + } + + return sum; + } + +} + +OCB_Mode::OCB_Mode(BlockCipher* cipher, size_t tag_size) : + m_cipher(cipher), + m_tag_size(tag_size), + m_ad_hash(BS), m_offset(BS), m_checksum(BS) + { + if(m_cipher->block_size() != BS) + throw std::invalid_argument("OCB requires a 128 bit cipher so cannot be used with " + + m_cipher->name()); + + if(m_tag_size != 16) // fixme: 64, 96 bits also supported + throw std::invalid_argument("OCB cannot produce a " + std::to_string(m_tag_size) + + " byte tag"); + + } + +OCB_Mode::~OCB_Mode() { /* for unique_ptr destructor */ } + +void OCB_Mode::clear() + { + m_cipher.reset(); + m_L.reset(); + zeroise(m_ad_hash); + zeroise(m_offset); + zeroise(m_checksum); + } + +bool OCB_Mode::valid_nonce_length(size_t length) const + { + return (length > 0 && length < 16); + } + +std::string OCB_Mode::name() const + { + return m_cipher->name() + "/OCB"; // include tag size + } + +size_t OCB_Mode::update_granularity() const + { + return 8 * m_cipher->parallel_bytes(); + } + +Key_Length_Specification OCB_Mode::key_spec() const + { + return m_cipher->key_spec(); + } + +void OCB_Mode::key_schedule(const byte key[], size_t length) + { + m_cipher->set_key(key, length); + m_L.reset(new L_computer(*m_cipher)); + m_nonce_state.reset(new Nonce_State(*m_cipher)); + } + +void OCB_Mode::set_associated_data(const byte ad[], size_t ad_len) + { + BOTAN_ASSERT(m_L, "A key was set"); + m_ad_hash = ocb_hash(*m_L, *m_cipher, &ad[0], ad_len); + } + +secure_vector<byte> OCB_Mode::start(const byte nonce[], size_t nonce_len) + { + if(!valid_nonce_length(nonce_len)) + throw Invalid_IV_Length(name(), nonce_len); + + BOTAN_ASSERT(m_nonce_state, "A key was set"); + + m_offset = m_nonce_state->update_nonce(nonce, nonce_len); + zeroise(m_checksum); + m_block_index = 0; + + return secure_vector<byte>(); + } + +void OCB_Encryption::encrypt(byte buffer[], size_t blocks) + { + const L_computer& L = *m_L; // convenient name + + const size_t par_bytes = m_cipher->parallel_bytes(); + + BOTAN_ASSERT(par_bytes % BS == 0, "Cipher is parallel in full blocks"); + + const size_t par_blocks = par_bytes / BS; + + secure_vector<byte> csum_accum(par_bytes); + secure_vector<byte> offsets(par_bytes); + + size_t blocks_left = blocks; + + while(blocks_left) + { + const size_t proc_blocks = std::min(blocks_left, par_blocks); + const size_t proc_bytes = proc_blocks * BS; + + for(size_t i = 0; i != proc_blocks; ++i) + { // could be done in parallel + m_offset ^= L(ctz(++m_block_index)); + copy_mem(&offsets[BS*i], &m_offset[0], BS); + } + + xor_buf(&csum_accum[0], &buffer[0], proc_bytes); + + xor_buf(&buffer[0], &offsets[0], proc_bytes); + + m_cipher->encrypt_n(&buffer[0], &buffer[0], proc_blocks); + + xor_buf(&buffer[0], &offsets[0], proc_bytes); + + buffer += proc_bytes; + blocks_left -= proc_blocks; + } + + // fold into checksum + for(size_t i = 0; i != csum_accum.size(); ++i) + m_checksum[i % BS] ^= csum_accum[i]; + } + +void OCB_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]; + + BOTAN_ASSERT(sz % BS == 0, "Input length is an even number of blocks"); + + encrypt(buf, sz / BS); + } + +void OCB_Encryption::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]; + + if(sz) + { + const size_t final_full_blocks = sz / BS; + const size_t remainder_bytes = sz - (final_full_blocks * BS); + + encrypt(buf, final_full_blocks); + + if(remainder_bytes) + { + BOTAN_ASSERT(remainder_bytes < BS, "Only a partial block left"); + byte* remainder = &buf[sz - remainder_bytes]; + + xor_buf(&m_checksum[0], &remainder[0], remainder_bytes); + m_checksum[remainder_bytes] ^= 0x80; + + m_offset ^= m_L->star(); // Offset_* + + secure_vector<byte> buf(BS); + m_cipher->encrypt(m_offset, buf); + xor_buf(&remainder[0], &buf[0], remainder_bytes); + } + } + + // now compute the tag + secure_vector<byte> mac = m_offset; + mac ^= m_checksum; + mac ^= m_L->dollar(); + + m_cipher->encrypt(mac); + + mac ^= m_ad_hash; + + buffer += std::make_pair(&mac[0], tag_size()); + + zeroise(m_checksum); + zeroise(m_offset); + m_block_index = 0; + } + +void OCB_Decryption::decrypt(byte buffer[], size_t blocks) + { + const L_computer& L = *m_L; // convenient name + + const size_t par_bytes = m_cipher->parallel_bytes(); + + BOTAN_ASSERT(par_bytes % BS == 0, "Cipher is parallel in full blocks"); + + const size_t par_blocks = par_bytes / BS; + + secure_vector<byte> csum_accum(par_bytes); + secure_vector<byte> offsets(par_bytes); + + size_t blocks_left = blocks; + + while(blocks_left) + { + const size_t proc_blocks = std::min(blocks_left, par_blocks); + const size_t proc_bytes = proc_blocks * BS; + + for(size_t i = 0; i != proc_blocks; ++i) + { // could be done in parallel + m_offset ^= L(ctz(++m_block_index)); + copy_mem(&offsets[BS*i], &m_offset[0], BS); + } + + xor_buf(&buffer[0], &offsets[0], proc_bytes); + + m_cipher->decrypt_n(&buffer[0], &buffer[0], proc_blocks); + + xor_buf(&buffer[0], &offsets[0], proc_bytes); + + xor_buf(&csum_accum[0], &buffer[0], proc_bytes); + + buffer += proc_bytes; + blocks_left -= proc_blocks; + } + + // fold into checksum + for(size_t i = 0; i != csum_accum.size(); ++i) + m_checksum[i % BS] ^= csum_accum[i]; + } + +void OCB_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]; + + BOTAN_ASSERT(sz % BS == 0, "Input length is an even number of blocks"); + + decrypt(buf, sz / BS); + } + +void OCB_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(), "We have the tag"); + + const size_t remaining = sz - tag_size(); + + if(remaining) + { + const size_t final_full_blocks = remaining / BS; + const size_t final_bytes = remaining - (final_full_blocks * BS); + + decrypt(&buf[0], final_full_blocks); + + if(final_bytes) + { + BOTAN_ASSERT(final_bytes < BS, "Only a partial block left"); + + byte* remainder = &buf[remaining - final_bytes]; + + m_offset ^= m_L->star(); // Offset_* + + secure_vector<byte> pad(BS); + m_cipher->encrypt(m_offset, pad); // P_* + + xor_buf(&remainder[0], &pad[0], final_bytes); + + xor_buf(&m_checksum[0], &remainder[0], final_bytes); + m_checksum[final_bytes] ^= 0x80; + } + } + + // compute the mac + secure_vector<byte> mac = m_offset; + mac ^= m_checksum; + mac ^= m_L->dollar(); + + m_cipher->encrypt(mac); + + mac ^= m_ad_hash; + + // reset state + zeroise(m_checksum); + zeroise(m_offset); + m_block_index = 0; + + // compare mac + const byte* included_tag = &buf[remaining]; + + if(!same_mem(&mac[0], included_tag, tag_size())) + throw Integrity_Failure("OCB tag check failed"); + + // remove tag from end of message + buffer.resize(remaining + offset); + } + +} diff --git a/src/modes/aead/ocb/ocb.h b/src/modes/aead/ocb/ocb.h new file mode 100644 index 000000000..ea7729348 --- /dev/null +++ b/src/modes/aead/ocb/ocb.h @@ -0,0 +1,124 @@ +/* +* OCB Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AEAD_OCB_H__ +#define BOTAN_AEAD_OCB_H__ + +#include <botan/aead.h> +#include <botan/block_cipher.h> +#include <botan/buf_filt.h> +#include <memory> + +namespace Botan { + +class L_computer; +class Nonce_State; + +/** +* OCB Mode (base class for OCB_Encryption and OCB_Decryption). Note +* that OCB is patented, but is freely licensed in some circumstances. +* +* @see "The OCB Authenticated-Encryption Algorithm" internet draft + http://tools.ietf.org/html/draft-irtf-cfrg-ocb-01 +* @see Free Licenses http://www.cs.ucdavis.edu/~rogaway/ocb/license.htm +* @see OCB home page http://www.cs.ucdavis.edu/~rogaway/ocb +*/ +class BOTAN_DLL OCB_Mode : public AEAD_Mode + { + public: + secure_vector<byte> start(const byte nonce[], size_t nonce_len) override; + + void set_associated_data(const byte ad[], size_t ad_len) override; + + std::string name() const override; + + size_t update_granularity() const; + + Key_Length_Specification key_spec() const override; + + bool valid_nonce_length(size_t) const override; + + void clear(); + + ~OCB_Mode(); + protected: + static const size_t BS = 16; // intrinsic to OCB definition + + /** + * @param cipher the 128-bit block cipher to use + * @param tag_size is how big the auth tag will be + */ + OCB_Mode(BlockCipher* cipher, size_t tag_size); + + void key_schedule(const byte key[], size_t length) override; + + size_t tag_size() const { return m_tag_size; } + + // fixme make these private + std::unique_ptr<BlockCipher> m_cipher; + std::unique_ptr<L_computer> m_L; + + size_t m_tag_size = 0; + size_t m_block_index = 0; + + secure_vector<byte> m_ad_hash; + secure_vector<byte> m_offset; + secure_vector<byte> m_checksum; + private: + std::unique_ptr<Nonce_State> m_nonce_state; + }; + +class BOTAN_DLL OCB_Encryption : public OCB_Mode + { + public: + /** + * @param cipher the 128-bit block cipher to use + * @param tag_size is how big the auth tag will be + */ + OCB_Encryption(BlockCipher* cipher, size_t tag_size = 16) : + OCB_Mode(cipher, tag_size) {} + + 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) override; + + void finish(secure_vector<byte>& final_block, size_t offset) override; + private: + void encrypt(byte input[], size_t blocks); + }; + +class BOTAN_DLL OCB_Decryption : public OCB_Mode + { + public: + /** + * @param cipher the 128-bit block cipher to use + * @param tag_size is how big the auth tag will be + */ + OCB_Decryption(BlockCipher* cipher, size_t tag_size = 16) : + OCB_Mode(cipher, tag_size) {} + + 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) override; + + void finish(secure_vector<byte>& final_block, size_t offset) override; + private: + void decrypt(byte input[], size_t blocks); + }; + +} + +#endif |