diff options
author | lloyd <[email protected]> | 2013-03-16 17:44:46 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2013-03-16 17:44:46 +0000 |
commit | 6982cf184cf4a51a54d781aba8f8a4e05d25a49c (patch) | |
tree | c97da2d236f83e25f2b636c7635b29f65283150d /src | |
parent | 3b6eac4497ecf68053d28dd7f84056ee469129d7 (diff) | |
parent | 09f961ac72d2a1266156ac98e85248610789763c (diff) |
propagate from branch 'net.randombit.botan' (head 039c91aa543bad85f227e8127ed048f9005e2fa0)
to branch 'net.randombit.botan.aead-modes' (head 97d8d24b545f18084e39bf928c174c45efbb63bd)
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/core_engine/core_modes.cpp | 19 | ||||
-rw-r--r-- | src/filters/modes/aead/aead.h | 38 | ||||
-rw-r--r-- | src/filters/modes/aead/info.txt | 1 | ||||
-rw-r--r-- | src/filters/modes/ocb/info.txt | 7 | ||||
-rw-r--r-- | src/filters/modes/ocb/ocb.cpp | 381 | ||||
-rw-r--r-- | src/filters/modes/ocb/ocb.h | 110 | ||||
-rw-r--r-- | src/utils/xor_buf.h | 12 |
7 files changed, 567 insertions, 1 deletions
diff --git a/src/engine/core_engine/core_modes.cpp b/src/engine/core_engine/core_modes.cpp index 21a5863e2..08124a3a0 100644 --- a/src/engine/core_engine/core_modes.cpp +++ b/src/engine/core_engine/core_modes.cpp @@ -40,6 +40,10 @@ #include <botan/eax.h> #endif +#if defined(BOTAN_HAS_OCB) + #include <botan/ocb.h> +#endif + #if defined(BOTAN_HAS_XTS) #include <botan/xts.h> #endif @@ -127,6 +131,16 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, #endif } +#if defined(BOTAN_HAS_OCB) + if(mode == "OCB") + { + if(direction == ENCRYPTION) + return new OCB_Encryption(block_cipher->clone(), 16); + else + return new OCB_Decryption(block_cipher->clone(), 16); + } +#endif + #if defined(BOTAN_HAS_XTS) if(mode == "XTS") { @@ -221,7 +235,10 @@ Keyed_Filter* Core_Engine::get_cipher(const std::string& algo_spec, if(filt) return filt; - throw Algorithm_Not_Found(cipher_name + "/" + mode + "/" + padding); + if(padding != "NoPadding") + throw Algorithm_Not_Found(cipher_name + "/" + mode + "/" + padding); + else + throw Algorithm_Not_Found(cipher_name + "/" + mode); } } diff --git a/src/filters/modes/aead/aead.h b/src/filters/modes/aead/aead.h new file mode 100644 index 000000000..868008e5d --- /dev/null +++ b/src/filters/modes/aead/aead.h @@ -0,0 +1,38 @@ +/* +* Interface for AEAD modes +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AEAD_H__ +#define BOTAN_AEAD_H__ + +#include <botan/key_filt.h> + +namespace Botan { + +class AEAD_Mode : public Keyed_Filter + { + public: + /** + * Set associated data that is not included in the ciphertext but + * that should be authenticated. Must be called after set_key + * and before end_msg. + * + * @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; + + virtual void set_nonce(const byte nonce[], size_t nonce_len) = 0; + + void set_iv(const InitializationVector& iv) override + { + set_nonce(iv.begin(), iv.length()); + } + }; + +} + +#endif diff --git a/src/filters/modes/aead/info.txt b/src/filters/modes/aead/info.txt new file mode 100644 index 000000000..b43aab2f6 --- /dev/null +++ b/src/filters/modes/aead/info.txt @@ -0,0 +1 @@ +define AEAD diff --git a/src/filters/modes/ocb/info.txt b/src/filters/modes/ocb/info.txt new file mode 100644 index 000000000..a98d7d54e --- /dev/null +++ b/src/filters/modes/ocb/info.txt @@ -0,0 +1,7 @@ +define OCB + +<requires> +block +aead +cmac +</requires> diff --git a/src/filters/modes/ocb/ocb.cpp b/src/filters/modes/ocb/ocb.cpp new file mode 100644 index 000000000..a8481ab8a --- /dev/null +++ b/src/filters/modes/ocb/ocb.cpp @@ -0,0 +1,381 @@ +/* +* 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> + +#include <botan/hex.h> +#include <iostream> + +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; + }; + +#if 0 +class Nonce_State + { + public: + secure_vector<byte> update_nonce(const byte nonce[], size_t nonce_len); + + bool fresh_nonce() { bool b = false; std::swap(b, m_fresh); return b; } + private: + secure_vector<byte> m_stretch; + bool m_fresh = false; + }; +#endif + +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, bool decrypting) : + Buffered_Filter(cipher->parallel_bytes(), decrypting ? tag_size : 0), + 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) // 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 */ } + +bool OCB_Mode::valid_keylength(size_t n) const + { + return m_cipher->valid_keylength(n); + } + +std::string OCB_Mode::name() const + { + return m_cipher->name() + "/OCB"; // include tag size + } + +void OCB_Mode::set_key(const SymmetricKey& key) + { + m_cipher->set_key(key); + m_L.reset(new L_computer(*m_cipher)); + } + +void OCB_Mode::set_nonce(const byte nonce[], size_t nonce_len) + { + if(!valid_iv_length(nonce_len)) + throw Invalid_IV_Length(name(), nonce_len); + + byte bottom; + secure_vector<byte> stretch; + + if(1) // need to recompute stretch (save iv to compare) + { + secure_vector<byte> buf(BS); + + const size_t offset = BS - nonce_len; + + copy_mem(&buf[offset], nonce, nonce_len); + buf[offset-1] = 1; + + bottom = buf[15] & 0x3F; + buf[15] &= 0xC0; + + m_cipher->encrypt(buf); + + for(size_t i = 0; i != 8; ++i) + buf.push_back(buf[i] ^ buf[i+1]); + + stretch = buf; + } + + // now set the offset from stretch and bottom + + const size_t shift_bytes = bottom / 8; + const size_t shift_bits = bottom % 8; + + for(size_t i = 0; i != BS; ++i) + { + m_offset[i] = (stretch[i+shift_bytes] << shift_bits); + m_offset[i] |= (stretch[i+shift_bytes+1] >> (8-shift_bits)); + } + } + +void OCB_Mode::start_msg() + { + //BOTAN_ASSERT(m_nonce_state.fresh_nonce(), "Nonce state is fresh"); + } + +void OCB_Mode::set_associated_data(const byte ad[], size_t ad_len) + { + m_ad_hash = ocb_hash(*m_L, *m_cipher, &ad[0], ad_len); + } + +void OCB_Mode::write(const byte input[], size_t length) + { + Buffered_Filter::write(input, length); + } + +void OCB_Mode::end_msg() + { + Buffered_Filter::end_msg(); + } + +void OCB_Encryption::buffered_block(const byte input[], size_t input_length) + { + BOTAN_ASSERT(input_length % BS == 0, "Input length is an even number of blocks"); + + const size_t blocks = input_length / BS; + +#if 1 + const size_t par_bytes = m_cipher->parallel_bytes(); +#else + const size_t par_bytes = 2*m_cipher->block_size(); +#endif + + BOTAN_ASSERT(par_bytes % BS == 0, "Cipher is parallel in full blocks"); + + const size_t par_blocks = par_bytes / BS; + + const L_computer& L = *m_L; // convenient name + + secure_vector<byte> ctext_buf(BS); + secure_vector<byte> csum_accum(par_bytes); + + size_t blocks_left = blocks; + + while(blocks_left) + { + const size_t to_proc = std::min(blocks_left, par_blocks); + const size_t proc_bytes = to_proc * BS; + + xor_buf(&csum_accum[0], &input[0], proc_bytes); + + for(size_t i = 0; i != to_proc; ++i) + { + m_offset ^= L(ctz(++m_block_index)); + + ctext_buf = m_offset; + xor_buf(&ctext_buf[0], &input[BS*i], BS); + m_cipher->encrypt(ctext_buf); + ctext_buf ^= m_offset; + + send(ctext_buf); + } + + input += proc_bytes; + blocks_left -= to_proc; + } + + // fold into checksum + for(size_t i = 0; i != csum_accum.size(); ++i) + m_checksum[i % BS] ^= csum_accum[i]; + } + +void OCB_Encryption::buffered_final(const byte input[], size_t input_length) + { + if(input_length >= BS) + { + const size_t final_blocks = input_length / BS; + const size_t final_bytes = final_blocks * BS; + buffered_block(input, final_bytes); + input += final_bytes; + input_length -= final_bytes; + } + + if(input_length) + { + BOTAN_ASSERT(input_length < BS, "Only a partial block left"); + + xor_buf(&m_checksum[0], &input[0], input_length); + m_checksum[input_length] ^= 0x80; + + m_offset ^= m_L->star(); // Offset_* + + secure_vector<byte> buf(BS); + m_cipher->encrypt(m_offset, buf); + xor_buf(&buf[0], &input[0], input_length); + + send(buf, input_length); // final ciphertext + } + + // 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; + + send(mac); + + zeroise(m_checksum); + zeroise(m_offset); + m_block_index = 0; + } + +void OCB_Decryption::buffered_block(const byte input[], size_t input_length) + { + BOTAN_ASSERT(input_length % BS == 0, "Input length is an even number of blocks"); + + const size_t blocks = input_length / BS; + + const L_computer& L = *m_L; + + secure_vector<byte> ptext_buf(BS); + + for(size_t i = 0; i != blocks; ++i) + { + // could run in parallel + + m_offset ^= L(ctz(++m_block_index)); + + ptext_buf = m_offset; + xor_buf(&ptext_buf[0], &input[BS*i], BS); + m_cipher->decrypt(ptext_buf); + ptext_buf ^= m_offset; + + send(ptext_buf); + m_checksum ^= ptext_buf; + } + } + +void OCB_Decryption::buffered_final(const byte input[], size_t input_length) + { + BOTAN_ASSERT(input_length >= m_tag_size, "We have the tag"); + + const byte* included_tag = &input[input_length-m_tag_size]; + input_length -= m_tag_size; + + if(input_length >= BS) + { + const size_t final_blocks = input_length / BS; + const size_t final_bytes = final_blocks * BS; + buffered_block(input, final_bytes); + input += final_bytes; + input_length -= final_bytes; + } + + if(input_length) + { + BOTAN_ASSERT(input_length < BS, "Only a partial block left"); + + m_offset ^= m_L->star(); // Offset_* + + secure_vector<byte> buf(BS); + m_cipher->encrypt(m_offset, buf); // P_* + + xor_buf(&buf[0], &input[0], input_length); + + xor_buf(&m_checksum[0], &buf[0], input_length); + m_checksum[input_length] ^= 0x80; + + send(buf, input_length); // final plaintext + } + + // 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; + + zeroise(m_checksum); + zeroise(m_offset); + m_block_index = 0; + + if(!same_mem(&mac[0], included_tag, m_tag_size)) + throw Integrity_Failure("OCB tag check failed"); + } + +} diff --git a/src/filters/modes/ocb/ocb.h b/src/filters/modes/ocb/ocb.h new file mode 100644 index 000000000..5497b08a4 --- /dev/null +++ b/src/filters/modes/ocb/ocb.h @@ -0,0 +1,110 @@ +/* +* OCB Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OCB_H__ +#define BOTAN_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-00 +* @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, + private Buffered_Filter + { + public: + void set_key(const SymmetricKey& key) override; + + void set_nonce(const byte nonce[], size_t nonce_len) override; + + void set_associated_data(const byte ad[], size_t ad_len) override; + + bool valid_keylength(size_t n) const override; + + std::string name() const override; + + bool valid_iv_length(size_t length) const override + { + return (length > 0 && length < 16); + } + + ~OCB_Mode(); + + protected: + /** + * @param cipher the 128-bit block cipher to use + * @param tag_size is how big the auth tag will be + * @param decrypting true if decrypting + */ + OCB_Mode(BlockCipher* cipher, size_t tag_size, bool decrypting); + + static const size_t BS = 16; // intrinsic to OCB definition + + // 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: + void write(const byte input[], size_t input_length) override; + void start_msg() override; + void end_msg() override; + }; + +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, false) {} + + private: + void buffered_block(const byte input[], size_t input_length) override; + void buffered_final(const byte input[], size_t input_length) override; + }; + +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, 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/utils/xor_buf.h b/src/utils/xor_buf.h index b67d84c50..5773a619c 100644 --- a/src/utils/xor_buf.h +++ b/src/utils/xor_buf.h @@ -96,6 +96,18 @@ void xor_buf(std::vector<byte, Alloc>& out, xor_buf(&out[0], &in[0], &in2[0], n); } +template<typename T, typename Alloc, typename Alloc2> +std::vector<T, Alloc>& +operator^=(std::vector<T, Alloc>& out, + const std::vector<T, Alloc2>& in) + { + if(out.size() < in.size()) + out.resize(in.size()); + + xor_buf(&out[0], &in[0], in.size()); + return out; + } + } #endif |