Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // ccm.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file ccm.h

0004 /// \brief CCM block cipher mode of operation

0005 /// \since Crypto++ 5.6.0

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 /// \brief CCM block cipher base implementation

0016 /// \details Base implementation of the AuthenticatedSymmetricCipher interface

0017 /// \since Crypto++ 5.6.0

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     // AuthenticatedSymmetricCipher

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     // AuthenticatedSymmetricCipherBase

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 &params);
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 /// \brief CCM block cipher final implementation

0086 /// \tparam T_BlockCipher block cipher

0087 /// \tparam T_DefaultDigestSize default digest size, in bytes

0088 /// \tparam T_IsEncryption direction in which to operate the cipher

0089 /// \since Crypto++ 5.6.0

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 /// \brief CCM block cipher mode of operation

0106 /// \tparam T_BlockCipher block cipher

0107 /// \tparam T_DefaultDigestSize default digest size, in bytes

0108 /// \details \p CCM provides the \p Encryption and \p Decryption typedef. See GCM_Base

0109 ///   and GCM_Final for the AuthenticatedSymmetricCipher implementation.

0110 /// \sa <a href="http://www.cryptopp.com/wiki/CCM_Mode">CCM Mode</a> and

0111 ///   <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>

0112 ///   on the Crypto++ wiki.

0113 /// \since Crypto++ 5.6.0

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