Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.4 KiB

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