Counter Strike : Global Offensive Source Code
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.

101 lines
3.2 KiB

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