Crypto++
|
00001 #ifndef CRYPTOPP_GCM_H 00002 #define CRYPTOPP_GCM_H 00003 00004 #include "authenc.h" 00005 #include "modes.h" 00006 00007 NAMESPACE_BEGIN(CryptoPP) 00008 00009 //! . 00010 enum GCM_TablesOption {GCM_2K_Tables, GCM_64K_Tables}; 00011 00012 //! . 00013 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE GCM_Base : public AuthenticatedSymmetricCipherBase 00014 { 00015 public: 00016 // AuthenticatedSymmetricCipher 00017 std::string AlgorithmName() const 00018 {return GetBlockCipher().AlgorithmName() + std::string("/GCM");} 00019 size_t MinKeyLength() const 00020 {return GetBlockCipher().MinKeyLength();} 00021 size_t MaxKeyLength() const 00022 {return GetBlockCipher().MaxKeyLength();} 00023 size_t DefaultKeyLength() const 00024 {return GetBlockCipher().DefaultKeyLength();} 00025 size_t GetValidKeyLength(size_t n) const 00026 {return GetBlockCipher().GetValidKeyLength(n);} 00027 bool IsValidKeyLength(size_t n) const 00028 {return GetBlockCipher().IsValidKeyLength(n);} 00029 unsigned int OptimalDataAlignment() const; 00030 IV_Requirement IVRequirement() const 00031 {return UNIQUE_IV;} 00032 unsigned int IVSize() const 00033 {return 12;} 00034 unsigned int MinIVLength() const 00035 {return 1;} 00036 unsigned int MaxIVLength() const 00037 {return UINT_MAX;} // (W64LIT(1)<<61)-1 in the standard 00038 unsigned int DigestSize() const 00039 {return 16;} 00040 lword MaxHeaderLength() const 00041 {return (W64LIT(1)<<61)-1;} 00042 lword MaxMessageLength() const 00043 {return ((W64LIT(1)<<39)-256)/8;} 00044 00045 protected: 00046 // AuthenticatedSymmetricCipherBase 00047 bool AuthenticationIsOnPlaintext() const 00048 {return false;} 00049 unsigned int AuthenticationBlockSize() const 00050 {return HASH_BLOCKSIZE;} 00051 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms); 00052 void Resync(const byte *iv, size_t len); 00053 size_t AuthenticateBlocks(const byte *data, size_t len); 00054 void AuthenticateLastHeaderBlock(); 00055 void AuthenticateLastConfidentialBlock(); 00056 void AuthenticateLastFooterBlock(byte *mac, size_t macSize); 00057 SymmetricCipher & AccessSymmetricCipher() {return m_ctr;} 00058 00059 virtual BlockCipher & AccessBlockCipher() =0; 00060 virtual GCM_TablesOption GetTablesOption() const =0; 00061 00062 const BlockCipher & GetBlockCipher() const {return const_cast<GCM_Base *>(this)->AccessBlockCipher();}; 00063 byte *HashBuffer() {return m_buffer+REQUIRED_BLOCKSIZE;} 00064 byte *HashKey() {return m_buffer+2*REQUIRED_BLOCKSIZE;} 00065 byte *MulTable() {return m_buffer+3*REQUIRED_BLOCKSIZE;} 00066 inline void ReverseHashBufferIfNeeded(); 00067 00068 class CRYPTOPP_DLL GCTR : public CTR_Mode_ExternalCipher::Encryption 00069 { 00070 protected: 00071 void IncrementCounterBy256(); 00072 }; 00073 00074 GCTR m_ctr; 00075 static word16 s_reductionTable[256]; 00076 static volatile bool s_reductionTableInitialized; 00077 enum {REQUIRED_BLOCKSIZE = 16, HASH_BLOCKSIZE = 16}; 00078 }; 00079 00080 //! . 00081 template <class T_BlockCipher, GCM_TablesOption T_TablesOption, bool T_IsEncryption> 00082 class GCM_Final : public GCM_Base 00083 { 00084 public: 00085 static std::string StaticAlgorithmName() 00086 {return T_BlockCipher::StaticAlgorithmName() + std::string("/GCM");} 00087 bool IsForwardTransformation() const 00088 {return T_IsEncryption;} 00089 00090 private: 00091 GCM_TablesOption GetTablesOption() const {return T_TablesOption;} 00092 BlockCipher & AccessBlockCipher() {return m_cipher;} 00093 typename T_BlockCipher::Encryption m_cipher; 00094 }; 00095 00096 //! <a href="http://www.cryptolounge.org/wiki/GCM">GCM</a> 00097 template <class T_BlockCipher, GCM_TablesOption T_TablesOption=GCM_2K_Tables> 00098 struct GCM : public AuthenticatedSymmetricCipherDocumentation 00099 { 00100 typedef GCM_Final<T_BlockCipher, T_TablesOption, true> Encryption; 00101 typedef GCM_Final<T_BlockCipher, T_TablesOption, false> Decryption; 00102 }; 00103 00104 NAMESPACE_END 00105 00106 #endif