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