ccm.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // ccm.h - originally written and placed in the public domain by Wei Dai
  2. /// \file ccm.h
  3. /// \brief CCM block cipher mode of operation
  4. /// \since Crypto++ 5.6.0
  5. #ifndef CRYPTOPP_CCM_H
  6. #define CRYPTOPP_CCM_H
  7. #include "authenc.h"
  8. #include "modes.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief CCM block cipher base implementation
  11. /// \details Base implementation of the AuthenticatedSymmetricCipher interface
  12. /// \since Crypto++ 5.6.0
  13. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
  14. {
  15. public:
  16. CCM_Base()
  17. : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
  18. // AuthenticatedSymmetricCipher
  19. std::string AlgorithmName() const
  20. {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
  21. std::string AlgorithmProvider() const
  22. {return GetBlockCipher().AlgorithmProvider();}
  23. size_t MinKeyLength() const
  24. {return GetBlockCipher().MinKeyLength();}
  25. size_t MaxKeyLength() const
  26. {return GetBlockCipher().MaxKeyLength();}
  27. size_t DefaultKeyLength() const
  28. {return GetBlockCipher().DefaultKeyLength();}
  29. size_t GetValidKeyLength(size_t keylength) const
  30. {return GetBlockCipher().GetValidKeyLength(keylength);}
  31. bool IsValidKeyLength(size_t keylength) const
  32. {return GetBlockCipher().IsValidKeyLength(keylength);}
  33. unsigned int OptimalDataAlignment() const
  34. {return GetBlockCipher().OptimalDataAlignment();}
  35. IV_Requirement IVRequirement() const
  36. {return UNIQUE_IV;}
  37. unsigned int IVSize() const
  38. {return 8;}
  39. unsigned int MinIVLength() const
  40. {return 7;}
  41. unsigned int MaxIVLength() const
  42. {return 13;}
  43. unsigned int DigestSize() const
  44. {return m_digestSize;}
  45. lword MaxHeaderLength() const
  46. {return W64LIT(0)-1;}
  47. lword MaxMessageLength() const
  48. {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
  49. bool NeedsPrespecifiedDataLengths() const
  50. {return true;}
  51. void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
  52. protected:
  53. // AuthenticatedSymmetricCipherBase
  54. bool AuthenticationIsOnPlaintext() const
  55. {return true;}
  56. unsigned int AuthenticationBlockSize() const
  57. {return GetBlockCipher().BlockSize();}
  58. void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
  59. void Resync(const byte *iv, size_t len);
  60. size_t AuthenticateBlocks(const byte *data, size_t len);
  61. void AuthenticateLastHeaderBlock();
  62. void AuthenticateLastConfidentialBlock();
  63. void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
  64. SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
  65. virtual BlockCipher & AccessBlockCipher() =0;
  66. virtual int DefaultDigestSize() const =0;
  67. const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();}
  68. byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
  69. enum {REQUIRED_BLOCKSIZE = 16};
  70. int m_digestSize, m_L;
  71. word64 m_messageLength, m_aadLength;
  72. CTR_Mode_ExternalCipher::Encryption m_ctr;
  73. };
  74. /// \brief CCM block cipher final implementation
  75. /// \tparam T_BlockCipher block cipher
  76. /// \tparam T_DefaultDigestSize default digest size, in bytes
  77. /// \tparam T_IsEncryption direction in which to operate the cipher
  78. /// \since Crypto++ 5.6.0
  79. template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
  80. class CCM_Final : public CCM_Base
  81. {
  82. public:
  83. static std::string StaticAlgorithmName()
  84. {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
  85. bool IsForwardTransformation() const
  86. {return T_IsEncryption;}
  87. private:
  88. BlockCipher & AccessBlockCipher() {return m_cipher;}
  89. int DefaultDigestSize() const {return T_DefaultDigestSize;}
  90. typename T_BlockCipher::Encryption m_cipher;
  91. };
  92. /// \brief CCM block cipher mode of operation
  93. /// \tparam T_BlockCipher block cipher
  94. /// \tparam T_DefaultDigestSize default digest size, in bytes
  95. /// \details \p CCM provides the \p Encryption and \p Decryption typedef. See GCM_Base
  96. /// and GCM_Final for the AuthenticatedSymmetricCipher implementation.
  97. /// \sa <a href="http://www.cryptopp.com/wiki/CCM_Mode">CCM Mode</a> and
  98. /// <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
  99. /// on the Crypto++ wiki.
  100. /// \since Crypto++ 5.6.0
  101. template <class T_BlockCipher, int T_DefaultDigestSize = 16>
  102. struct CCM : public AuthenticatedSymmetricCipherDocumentation
  103. {
  104. typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, true> Encryption;
  105. typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, false> Decryption;
  106. };
  107. NAMESPACE_END
  108. #endif