diff options
author | lloyd <[email protected]> | 2013-09-05 20:01:37 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2013-09-05 20:01:37 +0000 |
commit | 12b410b3dbb85dfdbef708e26052311fbd0bd2ec (patch) | |
tree | 4c943bcb14fa1c33b71ff93ffe1901bc2aafc92a /src/modes | |
parent | 6309f7d3f6f84c51380b51400f4e09728ec05e5d (diff) |
Add CCM mode
Diffstat (limited to 'src/modes')
-rw-r--r-- | src/modes/aead/aead.cpp | 23 | ||||
-rw-r--r-- | src/modes/aead/ccm/ccm.cpp | 266 | ||||
-rw-r--r-- | src/modes/aead/ccm/ccm.h | 128 | ||||
-rw-r--r-- | src/modes/aead/ccm/info.txt | 1 |
4 files changed, 416 insertions, 2 deletions
diff --git a/src/modes/aead/aead.cpp b/src/modes/aead/aead.cpp index 1ec7b4a4a..980a212d4 100644 --- a/src/modes/aead/aead.cpp +++ b/src/modes/aead/aead.cpp @@ -8,6 +8,10 @@ #include <botan/aead.h> #include <botan/libstate.h> +#if defined(BOTAN_HAS_AEAD_CCM) + #include <botan/ccm.h> +#endif + #if defined(BOTAN_HAS_AEAD_EAX) #include <botan/eax.h> #endif @@ -34,14 +38,29 @@ AEAD_Mode* get_aead(const std::string& algo_spec, Cipher_Dir direction) return nullptr; const std::string cipher_name = algo_parts[0]; - const std::string mode_name = algo_parts[1]; + const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]); - const size_t tag_size = 16; // default for all current AEAD + if(mode_info.empty()) + return nullptr; + + const std::string mode_name = mode_info[0]; + const size_t tag_size = (mode_info.size() > 1) ? to_u32bit(mode_info[1]) : 16; const BlockCipher* cipher = af.prototype_block_cipher(cipher_name); if(!cipher) return nullptr; +#if defined(BOTAN_HAS_AEAD_CCM) + if(mode_name == "CCM") + { + const size_t L = (mode_info.size() > 2) ? to_u32bit(mode_info[2]) : 3; + if(direction == ENCRYPTION) + return new CCM_Encryption(cipher->clone(), tag_size, L); + else + return new CCM_Decryption(cipher->clone(), tag_size, L); + } +#endif + #if defined(BOTAN_HAS_AEAD_EAX) if(mode_name == "EAX") { diff --git a/src/modes/aead/ccm/ccm.cpp b/src/modes/aead/ccm/ccm.cpp new file mode 100644 index 000000000..898964ff4 --- /dev/null +++ b/src/modes/aead/ccm/ccm.cpp @@ -0,0 +1,266 @@ +/* +* CCM Mode Encryption +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/ccm.h> +#include <botan/parsing.h> +#include <botan/internal/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/* +* CCM_Mode Constructor +*/ +CCM_Mode::CCM_Mode(BlockCipher* cipher, size_t tag_size, size_t L) : + m_tag_size(tag_size), + m_L(L), + m_cipher(cipher) + { + if(m_cipher->block_size() != 16) + throw std::invalid_argument(m_cipher->name() + " cannot be used with CCM mode"); + + if(L < 2 || L > 8) + throw std::invalid_argument("Invalid CCM L value " + std::to_string(L)); + + if(tag_size < 4 || tag_size > 16 || tag_size % 2 != 0) + throw std::invalid_argument("invalid CCM tag length " + std::to_string(tag_size)); + } + +void CCM_Mode::clear() + { + m_cipher.reset(); + m_msg_buf.clear(); + m_ad_buf.clear(); + } + +std::string CCM_Mode::name() const + { + return (m_cipher->name() + "/CCM(" + std::to_string(tag_size()) + "," + std::to_string(L())) + ")"; + } + +bool CCM_Mode::valid_nonce_length(size_t n) const + { + return (n == (15-L())); + } + +size_t CCM_Mode::default_nonce_size() const + { + return (15-L()); + } + +size_t CCM_Mode::update_granularity() const + { + /* + This value does not particularly matter as regardless CCM_Mode::update + buffers all input, so in theory this could be 1. However as for instance + Transformation_Filter creates update_granularity() byte buffers, use a + somewhat large size to avoid bouncing on a tiny buffer. + */ + return m_cipher->parallel_bytes(); + } + +Key_Length_Specification CCM_Mode::key_spec() const + { + return m_cipher->key_spec(); + } + +void CCM_Mode::key_schedule(const byte key[], size_t length) + { + m_cipher->set_key(key, length); + } + +void CCM_Mode::set_associated_data(const byte ad[], size_t length) + { + m_ad_buf.clear(); + + if(length) + { + // FIXME: support larger AD using length encoding rules + BOTAN_ASSERT(length < (0xFFFF - 0xFF), "Supported CCM AD length"); + + m_ad_buf.push_back(get_byte<u16bit>(0, length)); + m_ad_buf.push_back(get_byte<u16bit>(1, length)); + m_ad_buf += std::make_pair(ad, length); + while(m_ad_buf.size() % 16) + m_ad_buf.push_back(0); // pad with zeros to full block size + } + } + +secure_vector<byte> CCM_Mode::start(const byte nonce[], size_t nonce_len) + { + if(!valid_nonce_length(nonce_len)) + throw Invalid_IV_Length(name(), nonce_len); + + m_nonce.assign(nonce, nonce + nonce_len); + m_msg_buf.clear(); + + return secure_vector<byte>(); + } + +void CCM_Mode::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_msg_buf.insert(m_msg_buf.end(), buf, buf + sz); + buffer.resize(offset); // truncate msg + } + +void CCM_Mode::encode_length(size_t len, byte out[]) + { + const size_t len_bytes = L(); + + BOTAN_ASSERT(len_bytes < sizeof(size_t), "Length field fits"); + + for(size_t i = 0; i != len_bytes; ++i) + out[i] = get_byte(sizeof(size_t)-i, len); + + BOTAN_ASSERT((len >> (len_bytes*8)) == 0, "Message length fits in field"); + } + +void CCM_Mode::inc(secure_vector<byte>& C) + { + for(size_t i = 0; i != C.size(); ++i) + if(++C[C.size()-i-1]) + break; + } + +secure_vector<byte> CCM_Mode::format_b0(size_t sz) + { + secure_vector<byte> B0(BS); + + const byte b_flags = (m_ad_buf.size() ? 64 : 0) + (((tag_size()/2)-1) << 3) + (L()-1); + + B0[0] = b_flags; + copy_mem(&B0[1], &m_nonce[0], m_nonce.size()); + encode_length(sz, &B0[m_nonce.size()+1]); + + return B0; + } + +secure_vector<byte> CCM_Mode::format_c0() + { + secure_vector<byte> C(BS); + + const byte a_flags = L()-1; + + C[0] = a_flags; + copy_mem(&C[1], &m_nonce[0], m_nonce.size()); + + return C; + } + +void CCM_Encryption::finish(secure_vector<byte>& buffer, size_t offset) + { + BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane"); + + buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end()); + + const size_t sz = buffer.size() - offset; + byte* buf = &buffer[offset]; + + const secure_vector<byte>& ad = ad_buf(); + BOTAN_ASSERT(ad.size() % BS == 0, "AD is block size multiple"); + + const BlockCipher& E = cipher(); + + secure_vector<byte> T(BS); + E.encrypt(format_b0(sz), T); + + for(size_t i = 0; i != ad.size(); i += BS) + { + xor_buf(&T[0], &ad[i], BS); + E.encrypt(T); + } + + secure_vector<byte> C = format_c0(); + secure_vector<byte> S0(BS); + E.encrypt(C, S0); + inc(C); + + secure_vector<byte> X(BS); + + const byte* buf_end = &buf[sz]; + + while(buf != buf_end) + { + const size_t to_proc = std::min<size_t>(BS, buf_end - buf); + + xor_buf(&T[0], buf, to_proc); + E.encrypt(T); + + E.encrypt(C, X); + xor_buf(buf, &X[0], to_proc); + inc(C); + + buf += to_proc; + } + + T ^= S0; + + buffer += std::make_pair(&T[0], tag_size()); + } + +void CCM_Decryption::finish(secure_vector<byte>& buffer, size_t offset) + { + BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane"); + + buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end()); + + const size_t sz = buffer.size() - offset; + byte* buf = &buffer[offset]; + + BOTAN_ASSERT(sz >= tag_size(), "We have the tag"); + + const secure_vector<byte>& ad = ad_buf(); + BOTAN_ASSERT(ad.size() % BS == 0, "AD is block size multiple"); + + const BlockCipher& E = cipher(); + + secure_vector<byte> T(BS); + E.encrypt(format_b0(sz - tag_size()), T); + + for(size_t i = 0; i != ad.size(); i += BS) + { + xor_buf(&T[0], &ad[i], BS); + E.encrypt(T); + } + + secure_vector<byte> C = format_c0(); + + secure_vector<byte> S0(BS); + E.encrypt(C, S0); + inc(C); + + secure_vector<byte> X(BS); + + const byte* buf_end = &buf[sz - tag_size()]; + + while(buf != buf_end) + { + const size_t to_proc = std::min<size_t>(BS, buf_end - buf); + + E.encrypt(C, X); + xor_buf(buf, &X[0], to_proc); + inc(C); + + xor_buf(&T[0], buf, to_proc); + E.encrypt(T); + + buf += to_proc; + } + + T ^= S0; + + if(!same_mem(&T[0], buf_end, tag_size())) + throw Integrity_Failure("CCM tag check failed"); + + buffer.resize(buffer.size() - tag_size()); + } + +} diff --git a/src/modes/aead/ccm/ccm.h b/src/modes/aead/ccm/ccm.h new file mode 100644 index 000000000..d0a44a51d --- /dev/null +++ b/src/modes/aead/ccm/ccm.h @@ -0,0 +1,128 @@ +/* +* CCM Mode +* (C) 2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AEAD_CCM_H__ +#define BOTAN_AEAD_CCM_H__ + +#include <botan/aead.h> +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/mac.h> +#include <memory> + +namespace Botan { + +/** +* Base class for CCM encryption and decryption +* @see RFC 3610 +*/ +class BOTAN_DLL CCM_Mode : public AEAD_Mode + { + public: + secure_vector<byte> start(const byte nonce[], size_t nonce_len) override; + + void update(secure_vector<byte>& blocks, size_t offset) 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; + + size_t default_nonce_size() const override; + + void clear(); + + size_t tag_size() const { return m_tag_size; } + + protected: + static const size_t BS = 16; // intrinsic to CCM definition + + CCM_Mode(BlockCipher* cipher, size_t tag_size, size_t L); + + size_t L() const { return m_L; } + + const BlockCipher& cipher() const { return *m_cipher; } + + void encode_length(size_t len, byte out[]); + + void inc(secure_vector<byte>& C); + + const secure_vector<byte>& ad_buf() const { return m_ad_buf; } + + secure_vector<byte>& msg_buf() { return m_msg_buf; } + + secure_vector<byte> format_b0(size_t msg_size); + secure_vector<byte> format_c0(); + private: + void key_schedule(const byte key[], size_t length) override; + + const size_t m_tag_size; + const size_t m_L; + + std::unique_ptr<BlockCipher> m_cipher; + secure_vector<byte> m_nonce, m_msg_buf, m_ad_buf; + }; + +/** +* CCM Encryption +*/ +class BOTAN_DLL CCM_Encryption : public CCM_Mode + { + public: + /** + * @param cipher a 128-bit block cipher + * @param tag_size is how big the auth tag will be (even values + * between 4 and 16 are accepted) + * @param L length of L parameter. The total message length + * must be less than 2**L bytes, and the nonce is 15-L bytes. + */ + CCM_Encryption(BlockCipher* cipher, size_t tag_size = 16, size_t L = 3) : + CCM_Mode(cipher, tag_size, L) {} + + void finish(secure_vector<byte>& final_block, size_t offset) override; + + size_t output_length(size_t input_length) const override + { return input_length + tag_size(); } + + size_t minimum_final_size() const override { return 0; } + }; + +/** +* CCM Decryption +*/ +class BOTAN_DLL CCM_Decryption : public CCM_Mode + { + public: + /** + * @param cipher a 128-bit block cipher + * @param tag_size is how big the auth tag will be (even values + * between 4 and 16 are accepted) + * @param L length of L parameter. The total message length + * must be less than 2**L bytes, and the nonce is 15-L bytes. + */ + CCM_Decryption(BlockCipher* cipher, size_t tag_size = 16, size_t L = 3) : + CCM_Mode(cipher, tag_size, L) {} + + void finish(secure_vector<byte>& final_block, size_t offset) override; + + 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(); } + }; + +} + +#endif diff --git a/src/modes/aead/ccm/info.txt b/src/modes/aead/ccm/info.txt new file mode 100644 index 000000000..8ec85ec7d --- /dev/null +++ b/src/modes/aead/ccm/info.txt @@ -0,0 +1 @@ +define AEAD_CCM |