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.

127 lines
4.5 KiB

  1. // gcm.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \headerfile gcm.h
  4. //! \brief GCM block cipher mode of operation
  5. #ifndef CRYPTOPP_GCM_H
  6. #define CRYPTOPP_GCM_H
  7. #include "authenc.h"
  8. #include "modes.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! \enum GCM_TablesOption
  11. //! \brief Use either 2K or 64K size tables.
  12. enum GCM_TablesOption {GCM_2K_Tables, GCM_64K_Tables};
  13. //! \class GCM_Base
  14. //! \brief CCM block cipher mode of operation.
  15. //! \details Implementations and overrides in \p GCM_Base apply to both \p ENCRYPTION and \p DECRYPTION directions
  16. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE GCM_Base : public AuthenticatedSymmetricCipherBase
  17. {
  18. public:
  19. // AuthenticatedSymmetricCipher
  20. std::string AlgorithmName() const
  21. {return GetBlockCipher().AlgorithmName() + std::string("/GCM");}
  22. size_t MinKeyLength() const
  23. {return GetBlockCipher().MinKeyLength();}
  24. size_t MaxKeyLength() const
  25. {return GetBlockCipher().MaxKeyLength();}
  26. size_t DefaultKeyLength() const
  27. {return GetBlockCipher().DefaultKeyLength();}
  28. size_t GetValidKeyLength(size_t n) const
  29. {return GetBlockCipher().GetValidKeyLength(n);}
  30. bool IsValidKeyLength(size_t n) const
  31. {return GetBlockCipher().IsValidKeyLength(n);}
  32. unsigned int OptimalDataAlignment() const;
  33. IV_Requirement IVRequirement() const
  34. {return UNIQUE_IV;}
  35. unsigned int IVSize() const
  36. {return 12;}
  37. unsigned int MinIVLength() const
  38. {return 1;}
  39. unsigned int MaxIVLength() const
  40. {return UINT_MAX;} // (W64LIT(1)<<61)-1 in the standard
  41. unsigned int DigestSize() const
  42. {return 16;}
  43. lword MaxHeaderLength() const
  44. {return (W64LIT(1)<<61)-1;}
  45. lword MaxMessageLength() const
  46. {return ((W64LIT(1)<<39)-256)/8;}
  47. protected:
  48. // AuthenticatedSymmetricCipherBase
  49. bool AuthenticationIsOnPlaintext() const
  50. {return false;}
  51. unsigned int AuthenticationBlockSize() const
  52. {return HASH_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 GCM_TablesOption GetTablesOption() const =0;
  62. const BlockCipher & GetBlockCipher() const {return const_cast<GCM_Base *>(this)->AccessBlockCipher();};
  63. byte *HashBuffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
  64. byte *HashKey() {return m_buffer+2*REQUIRED_BLOCKSIZE;}
  65. byte *MulTable() {return m_buffer+3*REQUIRED_BLOCKSIZE;}
  66. inline void ReverseHashBufferIfNeeded();
  67. class CRYPTOPP_DLL GCTR : public CTR_Mode_ExternalCipher::Encryption
  68. {
  69. protected:
  70. void IncrementCounterBy256();
  71. };
  72. GCTR m_ctr;
  73. static word16 s_reductionTable[256];
  74. static volatile bool s_reductionTableInitialized;
  75. enum {REQUIRED_BLOCKSIZE = 16, HASH_BLOCKSIZE = 16};
  76. };
  77. //! \class GCM_Final
  78. //! \brief Class specific methods used to operate the cipher.
  79. //! \tparam T_BlockCipher block cipher
  80. //! \tparam T_TablesOption table size, either \p GCM_2K_Tables or \p GCM_64K_Tables
  81. //! \tparam T_IsEncryption direction in which to operate the cipher
  82. //! \details Implementations and overrides in \p GCM_Final apply to either
  83. //! \p ENCRYPTION or \p DECRYPTION, depending on the template parameter \p T_IsEncryption.
  84. //! \details \p GCM_Final does not use inner classes \p Enc and \p Dec.
  85. template <class T_BlockCipher, GCM_TablesOption T_TablesOption, bool T_IsEncryption>
  86. class GCM_Final : public GCM_Base
  87. {
  88. public:
  89. static std::string StaticAlgorithmName()
  90. {return T_BlockCipher::StaticAlgorithmName() + std::string("/GCM");}
  91. bool IsForwardTransformation() const
  92. {return T_IsEncryption;}
  93. private:
  94. GCM_TablesOption GetTablesOption() const {return T_TablesOption;}
  95. BlockCipher & AccessBlockCipher() {return m_cipher;}
  96. typename T_BlockCipher::Encryption m_cipher;
  97. };
  98. //! \class GCM
  99. //! \brief The GCM mode of operation
  100. //! \tparam T_BlockCipher block cipher
  101. //! \tparam T_TablesOption table size, either \p GCM_2K_Tables or \p GCM_64K_Tables
  102. //! \details \p GCM provides the \p Encryption and \p Decryption typedef.
  103. //! \sa <a href="http://www.cryptolounge.org/wiki/GCM">GCM</a> at the Crypto Lounge
  104. template <class T_BlockCipher, GCM_TablesOption T_TablesOption=GCM_2K_Tables>
  105. struct GCM : public AuthenticatedSymmetricCipherDocumentation
  106. {
  107. typedef GCM_Final<T_BlockCipher, T_TablesOption, true> Encryption;
  108. typedef GCM_Final<T_BlockCipher, T_TablesOption, false> Decryption;
  109. };
  110. NAMESPACE_END
  111. #endif