- Main Page
- Namespaces
- Classes
- Files

# gcm.h

    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 &params);
    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
    00067         class GCTR : public CTR_Mode_ExternalCipher::Encryption
    00068         {
    00069         protected:
    00070                 void IncrementCounterBy256();
    00071         };
    00072
    00073         GCTR m_ctr;
    00074         static word16 s_reductionTable[256];
    00075         static bool s_reductionTableInitialized;
    00076         enum {REQUIRED_BLOCKSIZE = 16, HASH_BLOCKSIZE = 16};
    00077 };
    00078
    00079 //! .
    00080 template <class T_BlockCipher, GCM_TablesOption T_TablesOption, bool T_IsEncryption>
    00081 class GCM_Final : public GCM_Base
    00082 {
    00083 public:
    00084         static std::string StaticAlgorithmName()
    00085                 {return T_BlockCipher::StaticAlgorithmName() + std::string("/GCM");}
    00086         bool IsForwardTransformation() const
    00087                 {return T_IsEncryption;}
    00088
    00089 private:
    00090         GCM_TablesOption GetTablesOption() const {return T_TablesOption;}
    00091         BlockCipher & AccessBlockCipher() {return m_cipher;}
    00092         typename T_BlockCipher::Encryption m_cipher;
    00093 };
    00094
    00095 //! <a href="http://www.cryptolounge.org/wiki/GCM">GCM</a>
    00096 template <class T_BlockCipher, GCM_TablesOption T_TablesOption=GCM_2K_Tables>
    00097 struct GCM : public AuthenticatedSymmetricCipherDocumentation
    00098 {
    00099         typedef GCM_Final<T_BlockCipher, T_TablesOption, true> Encryption;
    00100         typedef GCM_Final<T_BlockCipher, T_TablesOption, false> Decryption;
    00101 };
    00102
    00103 NAMESPACE_END
    00104
    00105 #endif