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