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 | |
parent | 6309f7d3f6f84c51380b51400f4e09728ec05e5d (diff) |
Add CCM mode
-rw-r--r-- | checks/validate.dat | 35 | ||||
-rw-r--r-- | doc/algos.rst | 2 | ||||
-rw-r--r-- | doc/relnotes/1_11_5.rst | 2 | ||||
-rw-r--r-- | src/engine/core_engine/core_modes.cpp | 47 | ||||
-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 |
8 files changed, 483 insertions, 21 deletions
diff --git a/checks/validate.dat b/checks/validate.dat index 0cc96c2e7..30575d2de 100644 --- a/checks/validate.dat +++ b/checks/validate.dat @@ -26742,6 +26742,41 @@ A7D581F78E4848DC3B01E5F2A5C01BA8910D0F144BC494E29450271174B866868EE8DC6B0DD396ED 2B7E151628AED2A6ABF7158809CF4F3C:\ F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF +[AES-128/CCM(128,2)] +# From NIST test vectors +:\ +0EAD29EF205FBB86D11ABE5ED704B880:\ +2EBF60F0969013A54A3DEDB19D20F6C8:\ +1DE8C5E21F9DB33123FF870ADD:\ +E1DE6C6119D7DB471136285D10B47A450221B16978569190EF6A22B055295603 + +B506A6BA900C1147C806775324B36EB376AA01D4C3EEF6F5:\ +14B14FE5B317411392861638EC383AE40BA95FEFE34255DC2EC067887114BC370281DE6F00836CE4:\ +43C1142877D9F450E12D7B6DB47A85BA:\ +76BECD9D27CA8A026215F32712:\ +6A59AACADD416E465264C15E1A1E9BFA084687492710F9BDA832E2571E468224 + +[AES-128/CCM(64,2)] +# From RFC 3610 +08090A0B0C0D0E0F101112131415161718191A1B1C1D1E:\ +588C979A61C663D2F066D0C2C0F989806D5F6B61DAC38417E8D12CFDF926E0:\ +C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF:\ +00000003020100A0A1A2A3A4A5:\ +0001020304050607 + +08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F:\ +72C91A36E135F8CF291CA894085C87E3CC15C439C9E43A3BA091D56E10400916:\ +C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF:\ +00000004030201A0A1A2A3A4A5:\ +0001020304050607 + +[AES-128/CCM(80,2)] +# RFC 3610 +0C0D0E0F101112131415161718191A1B1C1D1E1F20:\ +C0FFA0D6F05BDB67F24D43A4338D2AA4BED7B20E43CD1AA31662E7AD65D6DB:\ +C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF:\ +0000000E0D0C0BA0A1A2A3A4A5:\ +000102030405060708090A0B [AES-128/XTS] # Vectors are from IEEE P1619 D11 diff --git a/doc/algos.rst b/doc/algos.rst index 7f976a141..d7e3f2498 100644 --- a/doc/algos.rst +++ b/doc/algos.rst @@ -45,7 +45,7 @@ Hash functions Block ciphers ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - * Authenticated cipher modes EAX, OCB, and GCM + * Authenticated cipher modes EAX, OCB, GCM, and CCM * Unauthenticated cipher modes CTR, CBC, XTS, CFB, OFB, and ECB * AES (Rijndael) and AES candidates Serpent, Twofish, MARS, CAST-256, RC6 * DES, and variants 3DES and DESX diff --git a/doc/relnotes/1_11_5.rst b/doc/relnotes/1_11_5.rst index 48dea85e5..bed6261a5 100644 --- a/doc/relnotes/1_11_5.rst +++ b/doc/relnotes/1_11_5.rst @@ -5,6 +5,8 @@ Version 1.11.5, Not Yet Released processing of messages is now used by all cipher modes. An adaptor filter allows them to be used in a pipe. +* An implementation of CCM mode has been added + * The implementation of OCB mode now supports 64 and 96 bit tags * Optimized computation of XTS tweaks, producing a substantial speedup diff --git a/src/engine/core_engine/core_modes.cpp b/src/engine/core_engine/core_modes.cpp index aa9080c2d..336c8a3f3 100644 --- a/src/engine/core_engine/core_modes.cpp +++ b/src/engine/core_engine/core_modes.cpp @@ -40,6 +40,10 @@ #include <botan/aead_filt.h> +#if defined(BOTAN_HAS_AEAD_CCM) + #include <botan/ccm.h> +#endif + #if defined(BOTAN_HAS_AEAD_EAX) #include <botan/eax.h> #endif @@ -146,18 +150,25 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, if(mode.find("CFB") != std::string::npos || mode.find("EAX") != std::string::npos || mode.find("GCM") != std::string::npos || - mode.find("OCB") != std::string::npos) + mode.find("OCB") != std::string::npos || + mode.find("CCM") != std::string::npos) { std::vector<std::string> algo_info = parse_algorithm_name(mode); const std::string mode_name = algo_info[0]; - size_t bits = 0; - if(algo_info.size() == 1) - bits = 8 * block_cipher->block_size(); - else if(algo_info.size() == 2) + size_t bits = 8 * block_cipher->block_size(); + if(algo_info.size() > 1) bits = to_u32bit(algo_info[1]); - else - return nullptr; + +#if defined(BOTAN_HAS_MODE_CFB) + if(mode_name == "CFB") + { + if(direction == ENCRYPTION) + return new Transformation_Filter(new CFB_Encryption(block_cipher->clone(), bits)); + else + return new Transformation_Filter(new CFB_Decryption(block_cipher->clone(), bits)); + } +#endif #if defined(BOTAN_HAS_AEAD_FILTER) @@ -166,6 +177,17 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, const size_t tag_size = bits / 8; +#if defined(BOTAN_HAS_AEAD_CCM) + if(mode_name == "CCM") + { + const size_t L = (algo_info.size() == 3) ? to_u32bit(algo_info[2]) : 3; + if(direction == ENCRYPTION) + return new AEAD_Filter(new CCM_Encryption(block_cipher->clone(), tag_size, L)); + else + return new AEAD_Filter(new CCM_Decryption(block_cipher->clone(), tag_size, L)); + } +#endif + #if defined(BOTAN_HAS_AEAD_EAX) if(mode_name == "EAX") { @@ -197,17 +219,6 @@ Keyed_Filter* get_cipher_mode(const BlockCipher* block_cipher, #endif #endif - -#if defined(BOTAN_HAS_MODE_CFB) - if(mode_name == "CFB") - { - if(direction == ENCRYPTION) - return new Transformation_Filter(new CFB_Encryption(block_cipher->clone(), bits)); - else - return new Transformation_Filter(new CFB_Decryption(block_cipher->clone(), bits)); - } -#endif - } return nullptr; 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 |