From 35ac296082030fffde867cbac768815efe271522 Mon Sep 17 00:00:00 2001 From: lloyd Date: Wed, 27 Mar 2013 15:16:15 +0000 Subject: Convert OCB to new AEAD interface --- checks/ocb.cpp | 58 ++--- src/aead/gcm/gcm.cpp | 1 - src/aead/ocb/info.txt | 6 + src/aead/ocb/ocb.cpp | 423 +++++++++++++++++++++++++++++++++++++ src/aead/ocb/ocb.h | 115 ++++++++++ src/filters/aead_filt/ocb/info.txt | 6 - src/filters/aead_filt/ocb/ocb.cpp | 418 ------------------------------------ src/filters/aead_filt/ocb/ocb.h | 112 ---------- 8 files changed, 575 insertions(+), 564 deletions(-) create mode 100644 src/aead/ocb/info.txt create mode 100644 src/aead/ocb/ocb.cpp create mode 100644 src/aead/ocb/ocb.h delete mode 100644 src/filters/aead_filt/ocb/info.txt delete mode 100644 src/filters/aead_filt/ocb/ocb.cpp delete mode 100644 src/filters/aead_filt/ocb/ocb.h diff --git a/checks/ocb.cpp b/checks/ocb.cpp index 78be74bef..c5d38c65b 100644 --- a/checks/ocb.cpp +++ b/checks/ocb.cpp @@ -1,7 +1,6 @@ #include "validate.h" -#include #include #include #include @@ -19,31 +18,35 @@ std::vector ocb_encrypt(const SymmetricKey& key, { //std::unique_ptr ocb = get_aead("AES-128/OCB", ENCRYPTION); - OCB_Encryption* ocb = new OCB_Encryption(new AES_128); + OCB_Encryption ocb(new AES_128); - ocb->set_key(key); - ocb->set_nonce(&nonce[0], nonce.size()); - ocb->set_associated_data(ad, ad_len); + ocb.set_key(key); + ocb.set_associated_data(ad, ad_len); - Pipe pipe(ocb); - pipe.process_msg(pt, pt_len); - return unlock(pipe.read_all()); + ocb.start(&nonce[0], nonce.size()); + + secure_vector buf(pt, pt+pt_len); + ocb.finish(buf); + + return unlock(buf); } std::vector ocb_decrypt(const SymmetricKey& key, const std::vector& nonce, - const byte pt[], size_t pt_len, + const byte ct[], size_t ct_len, const byte ad[], size_t ad_len) { - OCB_Decryption* ocb = new OCB_Decryption(new AES_128); + OCB_Decryption ocb(new AES_128); + + ocb.set_key(key); + ocb.set_associated_data(ad, ad_len); - ocb->set_key(key); - ocb->set_nonce(&nonce[0], nonce.size()); - ocb->set_associated_data(ad, ad_len); + ocb.start(&nonce[0], nonce.size()); - Pipe pipe(ocb); - pipe.process_msg(pt, pt_len); - return unlock(pipe.read_all()); + secure_vector buf(ct, ct+ct_len); + ocb.finish(buf); + + return unlock(buf); } template @@ -65,26 +68,27 @@ std::vector ocb_decrypt(const SymmetricKey& key, } std::vector ocb_encrypt(OCB_Encryption& ocb, - Pipe& pipe, const std::vector& nonce, const std::vector& pt, const std::vector& ad) { - ocb.set_nonce(&nonce[0], nonce.size()); ocb.set_associated_data(&ad[0], ad.size()); - pipe.process_msg(pt); - return unlock(pipe.read_all(Pipe::LAST_MESSAGE)); + ocb.start(&nonce[0], nonce.size()); + + secure_vector buf(pt.begin(), pt.end()); + ocb.finish(buf); + + return unlock(buf); } void test_ocb_long_filters() { SymmetricKey key("00000000000000000000000000000000"); - OCB_Encryption* ocb = new OCB_Encryption(new AES_128); + OCB_Encryption ocb(new AES_128); - ocb->set_key(key); - Pipe pipe(ocb); + ocb.set_key(key); const std::vector empty; std::vector N(12); @@ -95,9 +99,9 @@ void test_ocb_long_filters() const std::vector S(i); N[11] = i; - const std::vector C1 = ocb_encrypt(*ocb, pipe, N, S, S); - const std::vector C2 = ocb_encrypt(*ocb, pipe, N, S, empty); - const std::vector C3 = ocb_encrypt(*ocb, pipe, N, empty, S); + const std::vector C1 = ocb_encrypt(ocb, N, S, S); + const std::vector C2 = ocb_encrypt(ocb, N, S, empty); + const std::vector C3 = ocb_encrypt(ocb, N, empty, S); //std::cout << "C_" << i << " = " << hex_encode(C1) << " " << hex_encode(C2) << " " << hex_encode(C3) << "\n"; @@ -120,7 +124,7 @@ void test_ocb_long_filters() //std::cout << "SHA-256(C) = " << C_hash << "\n"; N[11] = 0; - const std::vector cipher = ocb_encrypt(*ocb, pipe, N, empty, C); + const std::vector cipher = ocb_encrypt(ocb, N, empty, C); const std::string expected = "B2B41CBF9B05037DA7F16C24A35C1C94"; diff --git a/src/aead/gcm/gcm.cpp b/src/aead/gcm/gcm.cpp index 0ff73b034..628dcc270 100644 --- a/src/aead/gcm/gcm.cpp +++ b/src/aead/gcm/gcm.cpp @@ -225,5 +225,4 @@ void GCM_Decryption::finish(secure_vector& buffer) throw Integrity_Failure("GCM tag check failed"); } - } diff --git a/src/aead/ocb/info.txt b/src/aead/ocb/info.txt new file mode 100644 index 000000000..8d6a93ed9 --- /dev/null +++ b/src/aead/ocb/info.txt @@ -0,0 +1,6 @@ +define AEAD_OCB + + +block +cmac + diff --git a/src/aead/ocb/ocb.cpp b/src/aead/ocb/ocb.cpp new file mode 100644 index 000000000..5bd42766f --- /dev/null +++ b/src/aead/ocb/ocb.cpp @@ -0,0 +1,423 @@ +/* +* OCB Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include +#include +#include + +#include +#include + +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& star() const { return m_L_star; } + + const secure_vector& dollar() const { return m_L_dollar; } + + const secure_vector& operator()(size_t i) const { return get(i); } + + const secure_vector& 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 poly_double(const secure_vector& in) const + { + return CMAC::poly_double(in, 0x87); + } + + secure_vector m_L_dollar, m_L_star; + mutable std::vector> m_L; + }; + +class Nonce_State + { + public: + Nonce_State(const BlockCipher& cipher) : m_cipher(cipher) {} + + secure_vector update_nonce(const byte nonce[], + size_t nonce_len); + private: + const BlockCipher& m_cipher; + secure_vector m_last_nonce; + secure_vector m_stretch; + }; + +secure_vector +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 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 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 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 sum(BS); + secure_vector offset(BS); + + secure_vector 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 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(); + } + +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 csum_accum(par_bytes); + secure_vector 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& buffer) + { + BOTAN_ASSERT(buffer.size() % BS == 0, "Input length is an even number of blocks"); + + encrypt(&buffer[0], buffer.size() / BS); + } + +void OCB_Encryption::finish(secure_vector& buffer) + { + if(!buffer.empty()) + { + const size_t final_full_blocks = buffer.size() / BS; + const size_t remainder_bytes = buffer.size() - (final_full_blocks * BS); + + encrypt(&buffer[0], final_full_blocks); + + if(remainder_bytes) + { + BOTAN_ASSERT(remainder_bytes < BS, "Only a partial block left"); + byte* remainder = &buffer[buffer.size() - remainder_bytes]; + + xor_buf(&m_checksum[0], &remainder[0], remainder_bytes); + m_checksum[remainder_bytes] ^= 0x80; + + m_offset ^= m_L->star(); // Offset_* + + secure_vector buf(BS); + m_cipher->encrypt(m_offset, buf); + xor_buf(&remainder[0], &buf[0], remainder_bytes); + } + } + + // now compute the tag + secure_vector mac = m_offset; + mac ^= m_checksum; + mac ^= m_L->dollar(); + + m_cipher->encrypt(mac); + + mac ^= m_ad_hash; + + buffer += mac; + + 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 csum_accum(par_bytes); + secure_vector 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& buffer) + { + BOTAN_ASSERT(buffer.size() % BS == 0, "Input length is an even number of blocks"); + + decrypt(&buffer[0], buffer.size() / BS); + } + +void OCB_Decryption::finish(secure_vector& buffer) + { + BOTAN_ASSERT(buffer.size() >= tag_size(), "We have the tag"); + + if(const size_t remaining_ctext = buffer.size() - tag_size()) + { + const size_t final_full_blocks = remaining_ctext / BS; + const size_t remainder_bytes = remaining_ctext - (final_full_blocks * BS); + + decrypt(&buffer[0], final_full_blocks); + + if(remainder_bytes) + { + BOTAN_ASSERT(remainder_bytes < BS, "Only a partial block left"); + + byte* remainder = &buffer[buffer.size() - remainder_bytes]; + + m_offset ^= m_L->star(); // Offset_* + + secure_vector pad(BS); + m_cipher->encrypt(m_offset, pad); // P_* + + xor_buf(&remainder[0], &pad[0], remainder_bytes); + + xor_buf(&m_checksum[0], &remainder[0], remainder_bytes); + m_checksum[remainder_bytes] ^= 0x80; + } + } + + const byte* included_tag = &buffer[buffer.size() - tag_size()]; + + secure_vector 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/aead/ocb/ocb.h b/src/aead/ocb/ocb.h new file mode 100644 index 000000000..9d10c2656 --- /dev/null +++ b/src/aead/ocb/ocb.h @@ -0,0 +1,115 @@ +/* +* OCB Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OCB_H__ +#define BOTAN_OCB_H__ + +#include +#include +#include +#include + +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 + { + public: + secure_vector 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 m_cipher; + std::unique_ptr m_L; + + size_t m_tag_size = 0; + size_t m_block_index = 0; + + secure_vector m_ad_hash; + secure_vector m_offset; + secure_vector m_checksum; + private: + std::unique_ptr 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 minimum_final_size() const override { return 0; } + + void update(secure_vector& blocks) override; + + void finish(secure_vector& final_block) 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 minimum_final_size() const override { return tag_size(); } + + void update(secure_vector& blocks) override; + + void finish(secure_vector& final_block) override; + private: + void decrypt(byte input[], size_t blocks); + }; + +} + +#endif diff --git a/src/filters/aead_filt/ocb/info.txt b/src/filters/aead_filt/ocb/info.txt deleted file mode 100644 index 0ee41681d..000000000 --- a/src/filters/aead_filt/ocb/info.txt +++ /dev/null @@ -1,6 +0,0 @@ -define OCB - - -block -cmac - diff --git a/src/filters/aead_filt/ocb/ocb.cpp b/src/filters/aead_filt/ocb/ocb.cpp deleted file mode 100644 index eb10b6e9f..000000000 --- a/src/filters/aead_filt/ocb/ocb.cpp +++ /dev/null @@ -1,418 +0,0 @@ -/* -* OCB Mode -* (C) 2013 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include -#include -#include - -#include -#include - -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& star() const { return m_L_star; } - - const secure_vector& dollar() const { return m_L_dollar; } - - const secure_vector& operator()(size_t i) const { return get(i); } - - const secure_vector& 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 poly_double(const secure_vector& in) const - { - return CMAC::poly_double(in, 0x87); - } - - secure_vector m_L_dollar, m_L_star; - mutable std::vector> m_L; - }; - -class Nonce_State - { - public: - Nonce_State(const BlockCipher& cipher) : m_cipher(cipher) {} - - secure_vector update_nonce(const byte nonce[], - size_t nonce_len); - - bool fresh_nonce() { bool b = false; std::swap(b, m_fresh); return b; } - private: - const BlockCipher& m_cipher; - secure_vector m_last_nonce; - secure_vector m_stretch; - bool m_fresh = false; - }; - -secure_vector -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 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 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)); - } - - m_fresh = true; - return offset; - } - -namespace { - -/* -* OCB's HASH -*/ -secure_vector 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 sum(BS); - secure_vector offset(BS); - - secure_vector 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 */ } - -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)); - m_nonce_state.reset(new Nonce_State(*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); - - BOTAN_ASSERT(m_nonce_state, "A key was set"); - - m_offset = m_nonce_state->update_nonce(nonce, nonce_len); - } - -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; - - 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; - - const L_computer& L = *m_L; // convenient name - - secure_vector ctext_buf(par_bytes); - secure_vector csum_accum(par_bytes); - secure_vector offsets(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)); - copy_mem(&offsets[BS*i], &m_offset[0], BS); - } - - copy_mem(&ctext_buf[0], &input[0], proc_bytes); - - ctext_buf ^= offsets; - m_cipher->encrypt(ctext_buf); - ctext_buf ^= offsets; - - send(ctext_buf, proc_bytes); - - 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 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 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 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; - - const L_computer& L = *m_L; // convenient name - - secure_vector ptext_buf(par_bytes); - secure_vector csum_accum(par_bytes); - secure_vector offsets(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; - - for(size_t i = 0; i != to_proc; ++i) - { - m_offset ^= L(ctz(++m_block_index)); - copy_mem(&offsets[BS*i], &m_offset[0], BS); - } - - copy_mem(&ptext_buf[0], &input[0], proc_bytes); - - ptext_buf ^= offsets; - m_cipher->decrypt(ptext_buf); - ptext_buf ^= offsets; - - xor_buf(&csum_accum[0], &ptext_buf[0], proc_bytes); - - send(ptext_buf, proc_bytes); - - 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_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 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 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/aead_filt/ocb/ocb.h b/src/filters/aead_filt/ocb/ocb.h deleted file mode 100644 index 0a1cbcaff..000000000 --- a/src/filters/aead_filt/ocb/ocb.h +++ /dev/null @@ -1,112 +0,0 @@ -/* -* OCB Mode -* (C) 2013 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_OCB_H__ -#define BOTAN_OCB_H__ - -#include -#include -#include -#include - -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_Filter, - 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; - - Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); } - - 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 m_cipher; - std::unique_ptr m_L; - - size_t m_tag_size = 0; - size_t m_block_index = 0; - - secure_vector m_ad_hash; - secure_vector m_offset; - secure_vector m_checksum; - - private: - void write(const byte input[], size_t input_length) override; - void start_msg() override; - void end_msg() override; - - std::unique_ptr 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, 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 -- cgit v1.2.3