00001
00002
00003
00004
00005
00006
00007 #ifndef CRYPTOPP_CCM_H
00008 #define CRYPTOPP_CCM_H
00009
00010 #include "authenc.h"
00011 #include "modes.h"
00012
00013 NAMESPACE_BEGIN(CryptoPP)
00014
00015 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
00016 {
00017 public:
00018 CCM_Base()
00019 : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
00020
00021
00022 std::string AlgorithmName() const
00023 {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
00024 size_t MinKeyLength() const
00025 {return GetBlockCipher().MinKeyLength();}
00026 size_t MaxKeyLength() const
00027 {return GetBlockCipher().MaxKeyLength();}
00028 size_t DefaultKeyLength() const
00029 {return GetBlockCipher().DefaultKeyLength();}
00030 size_t GetValidKeyLength(size_t n) const
00031 {return GetBlockCipher().GetValidKeyLength(n);}
00032 bool IsValidKeyLength(size_t n) const
00033 {return GetBlockCipher().IsValidKeyLength(n);}
00034 unsigned int OptimalDataAlignment() const
00035 {return GetBlockCipher().OptimalDataAlignment();}
00036 IV_Requirement IVRequirement() const
00037 {return UNIQUE_IV;}
00038 unsigned int IVSize() const
00039 {return 8;}
00040 unsigned int MinIVLength() const
00041 {return 7;}
00042 unsigned int MaxIVLength() const
00043 {return 13;}
00044 unsigned int DigestSize() const
00045 {return m_digestSize;}
00046 lword MaxHeaderLength() const
00047 {return W64LIT(0)-1;}
00048 lword MaxMessageLength() const
00049 {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
00050 bool NeedsPrespecifiedDataLengths() const
00051 {return true;}
00052 void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
00053
00054 protected:
00055
00056 bool AuthenticationIsOnPlaintext() const
00057 {return true;}
00058 unsigned int AuthenticationBlockSize() const
00059 {return GetBlockCipher().BlockSize();}
00060 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms);
00061 void Resync(const byte *iv, size_t len);
00062 size_t AuthenticateBlocks(const byte *data, size_t len);
00063 void AuthenticateLastHeaderBlock();
00064 void AuthenticateLastConfidentialBlock();
00065 void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
00066 SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
00067
00068 virtual BlockCipher & AccessBlockCipher() =0;
00069 virtual int DefaultDigestSize() const =0;
00070
00071 const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();};
00072 byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
00073
00074 enum {REQUIRED_BLOCKSIZE = 16};
00075 int m_digestSize, m_L;
00076 word64 m_messageLength, m_aadLength;
00077 CTR_Mode_ExternalCipher::Encryption m_ctr;
00078 };
00079
00080 template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
00081 class CCM_Final : public CCM_Base
00082 {
00083 public:
00084 static std::string StaticAlgorithmName()
00085 {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
00086 bool IsForwardTransformation() const
00087 {return T_IsEncryption;}
00088
00089 private:
00090 BlockCipher & AccessBlockCipher() {return m_cipher;}
00091 int DefaultDigestSize() const {return T_DefaultDigestSize;}
00092 typename T_BlockCipher::Encryption m_cipher;
00093 };
00094
00095
00096
00097 template <class T_BlockCipher, int T_DefaultDigestSize = 16>
00098 struct CCM : public AuthenticatedSymmetricCipherDocumentation
00099 {
00100 typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, true> Encryption;
00101 typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, false> Decryption;
00102 };
00103
00104 NAMESPACE_END
00105
00106 #endif