File indexing completed on 2025-01-18 09:54:54
0001
0002
0003
0004
0005
0006
0007 #ifndef CRYPTOPP_CCM_H
0008 #define CRYPTOPP_CCM_H
0009
0010 #include "authenc.h"
0011 #include "modes.h"
0012
0013 NAMESPACE_BEGIN(CryptoPP)
0014
0015
0016
0017
0018 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
0019 {
0020 public:
0021 CCM_Base()
0022 : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
0023
0024
0025 std::string AlgorithmName() const
0026 {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
0027 std::string AlgorithmProvider() const
0028 {return GetBlockCipher().AlgorithmProvider();}
0029 size_t MinKeyLength() const
0030 {return GetBlockCipher().MinKeyLength();}
0031 size_t MaxKeyLength() const
0032 {return GetBlockCipher().MaxKeyLength();}
0033 size_t DefaultKeyLength() const
0034 {return GetBlockCipher().DefaultKeyLength();}
0035 size_t GetValidKeyLength(size_t keylength) const
0036 {return GetBlockCipher().GetValidKeyLength(keylength);}
0037 bool IsValidKeyLength(size_t keylength) const
0038 {return GetBlockCipher().IsValidKeyLength(keylength);}
0039 unsigned int OptimalDataAlignment() const
0040 {return GetBlockCipher().OptimalDataAlignment();}
0041 IV_Requirement IVRequirement() const
0042 {return UNIQUE_IV;}
0043 unsigned int IVSize() const
0044 {return 8;}
0045 unsigned int MinIVLength() const
0046 {return 7;}
0047 unsigned int MaxIVLength() const
0048 {return 13;}
0049 unsigned int DigestSize() const
0050 {return m_digestSize;}
0051 lword MaxHeaderLength() const
0052 {return W64LIT(0)-1;}
0053 lword MaxMessageLength() const
0054 {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
0055 bool NeedsPrespecifiedDataLengths() const
0056 {return true;}
0057 void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
0058
0059 protected:
0060
0061 bool AuthenticationIsOnPlaintext() const
0062 {return true;}
0063 unsigned int AuthenticationBlockSize() const
0064 {return GetBlockCipher().BlockSize();}
0065 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms);
0066 void Resync(const byte *iv, size_t len);
0067 size_t AuthenticateBlocks(const byte *data, size_t len);
0068 void AuthenticateLastHeaderBlock();
0069 void AuthenticateLastConfidentialBlock();
0070 void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
0071 SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
0072
0073 virtual BlockCipher & AccessBlockCipher() =0;
0074 virtual int DefaultDigestSize() const =0;
0075
0076 const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();}
0077 byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
0078
0079 enum {REQUIRED_BLOCKSIZE = 16};
0080 int m_digestSize, m_L;
0081 word64 m_messageLength, m_aadLength;
0082 CTR_Mode_ExternalCipher::Encryption m_ctr;
0083 };
0084
0085
0086
0087
0088
0089
0090 template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
0091 class CCM_Final : public CCM_Base
0092 {
0093 public:
0094 static std::string StaticAlgorithmName()
0095 {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
0096 bool IsForwardTransformation() const
0097 {return T_IsEncryption;}
0098
0099 private:
0100 BlockCipher & AccessBlockCipher() {return m_cipher;}
0101 int DefaultDigestSize() const {return T_DefaultDigestSize;}
0102 typename T_BlockCipher::Encryption m_cipher;
0103 };
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 template <class T_BlockCipher, int T_DefaultDigestSize = 16>
0115 struct CCM : public AuthenticatedSymmetricCipherDocumentation
0116 {
0117 typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, true> Encryption;
0118 typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, false> Decryption;
0119 };
0120
0121 NAMESPACE_END
0122
0123 #endif