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.

106 lines
3.4 KiB

  1. #ifndef CRYPTOPP_GCM_H
  2. #define CRYPTOPP_GCM_H
  3. #include "authenc.h"
  4. #include "modes.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. //! .
  7. enum GCM_TablesOption {GCM_2K_Tables, GCM_64K_Tables};
  8. //! .
  9. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE GCM_Base : public AuthenticatedSymmetricCipherBase
  10. {
  11. public:
  12. // AuthenticatedSymmetricCipher
  13. std::string AlgorithmName() const
  14. {return GetBlockCipher().AlgorithmName() + std::string("/GCM");}
  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. IV_Requirement IVRequirement() const
  27. {return UNIQUE_IV;}
  28. unsigned int IVSize() const
  29. {return 12;}
  30. unsigned int MinIVLength() const
  31. {return 1;}
  32. unsigned int MaxIVLength() const
  33. {return UINT_MAX;} // (W64LIT(1)<<61)-1 in the standard
  34. unsigned int DigestSize() const
  35. {return 16;}
  36. lword MaxHeaderLength() const
  37. {return (W64LIT(1)<<61)-1;}
  38. lword MaxMessageLength() const
  39. {return ((W64LIT(1)<<39)-256)/8;}
  40. protected:
  41. // AuthenticatedSymmetricCipherBase
  42. bool AuthenticationIsOnPlaintext() const
  43. {return false;}
  44. unsigned int AuthenticationBlockSize() const
  45. {return HASH_BLOCKSIZE;}
  46. void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
  47. void Resync(const byte *iv, size_t len);
  48. size_t AuthenticateBlocks(const byte *data, size_t len);
  49. void AuthenticateLastHeaderBlock();
  50. void AuthenticateLastConfidentialBlock();
  51. void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
  52. SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
  53. virtual BlockCipher & AccessBlockCipher() =0;
  54. virtual GCM_TablesOption GetTablesOption() const =0;
  55. const BlockCipher & GetBlockCipher() const {return const_cast<GCM_Base *>(this)->AccessBlockCipher();};
  56. byte *HashBuffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
  57. byte *HashKey() {return m_buffer+2*REQUIRED_BLOCKSIZE;}
  58. byte *MulTable() {return m_buffer+3*REQUIRED_BLOCKSIZE;}
  59. inline void ReverseHashBufferIfNeeded();
  60. class CRYPTOPP_DLL GCTR : public CTR_Mode_ExternalCipher::Encryption
  61. {
  62. protected:
  63. void IncrementCounterBy256();
  64. };
  65. GCTR m_ctr;
  66. static word16 s_reductionTable[256];
  67. static volatile bool s_reductionTableInitialized;
  68. enum {REQUIRED_BLOCKSIZE = 16, HASH_BLOCKSIZE = 16};
  69. };
  70. //! .
  71. template <class T_BlockCipher, GCM_TablesOption T_TablesOption, bool T_IsEncryption>
  72. class GCM_Final : public GCM_Base
  73. {
  74. public:
  75. static std::string StaticAlgorithmName()
  76. {return T_BlockCipher::StaticAlgorithmName() + std::string("/GCM");}
  77. bool IsForwardTransformation() const
  78. {return T_IsEncryption;}
  79. private:
  80. GCM_TablesOption GetTablesOption() const {return T_TablesOption;}
  81. BlockCipher & AccessBlockCipher() {return m_cipher;}
  82. typename T_BlockCipher::Encryption m_cipher;
  83. };
  84. //! <a href="http://www.cryptolounge.org/wiki/GCM">GCM</a>
  85. template <class T_BlockCipher, GCM_TablesOption T_TablesOption=GCM_2K_Tables>
  86. struct GCM : public AuthenticatedSymmetricCipherDocumentation
  87. {
  88. typedef GCM_Final<T_BlockCipher, T_TablesOption, true> Encryption;
  89. typedef GCM_Final<T_BlockCipher, T_TablesOption, false> Decryption;
  90. };
  91. NAMESPACE_END
  92. #endif