Crypto++ 8.2 Free C++ class library of cryptographic schemes

gcm.h
Go to the documentation of this file.
1 // gcm.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file gcm.h
4 /// \brief GCM block cipher mode of operation
5 /// \since Crypto++ 5.6.0
6
7 #ifndef CRYPTOPP_GCM_H
8 #define CRYPTOPP_GCM_H
9
10 #include "authenc.h"
11 #include "modes.h"
12
13 // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
14 // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
15 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
16 # define CRYPTOPP_DISABLE_GCM_ASM 1
17 #endif
18
19 NAMESPACE_BEGIN(CryptoPP)
20
21 /// \enum GCM_TablesOption
22 /// \brief GCM table size options
23 enum GCM_TablesOption {
24 /// \brief Use a table with 2K entries
25 GCM_2K_Tables,
26 /// \brief Use a table with 64K entries
27 GCM_64K_Tables};
28
29 /// \brief GCM block cipher base implementation
30 /// \details Base implementation of the AuthenticatedSymmetricCipher interface
31 /// \since Crypto++ 5.6.0
32 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE GCM_Base : public AuthenticatedSymmetricCipherBase
33 {
34 public:
35 // AuthenticatedSymmetricCipher
36 std::string AlgorithmName() const
37 {return GetBlockCipher().AlgorithmName() + std::string("/GCM");}
38 std::string AlgorithmProvider() const
39 {return GetBlockCipher().AlgorithmProvider();}
40 size_t MinKeyLength() const
41 {return GetBlockCipher().MinKeyLength();}
42 size_t MaxKeyLength() const
43 {return GetBlockCipher().MaxKeyLength();}
44 size_t DefaultKeyLength() const
45 {return GetBlockCipher().DefaultKeyLength();}
46 size_t GetValidKeyLength(size_t n) const
47 {return GetBlockCipher().GetValidKeyLength(n);}
48 bool IsValidKeyLength(size_t n) const
49 {return GetBlockCipher().IsValidKeyLength(n);}
50 unsigned int OptimalDataAlignment() const;
51 IV_Requirement IVRequirement() const
52 {return UNIQUE_IV;}
53 unsigned int IVSize() const
54 {return 12;}
55 unsigned int MinIVLength() const
56 {return 1;}
57 unsigned int MaxIVLength() const
58 {return UINT_MAX;} // (W64LIT(1)<<61)-1 in the standard
59 unsigned int DigestSize() const
60 {return 16;}
61 lword MaxHeaderLength() const
62 {return (W64LIT(1)<<61)-1;}
63 lword MaxMessageLength() const
64 {return ((W64LIT(1)<<39)-256)/8;}
65
66 protected:
67 // AuthenticatedSymmetricCipherBase
68 bool AuthenticationIsOnPlaintext() const
69 {return false;}
70 unsigned int AuthenticationBlockSize() const
71 {return HASH_BLOCKSIZE;}
72 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
73 void Resync(const byte *iv, size_t len);
74 size_t AuthenticateBlocks(const byte *data, size_t len);
75 void AuthenticateLastHeaderBlock();
76 void AuthenticateLastConfidentialBlock();
77 void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
78 SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
79
80 virtual BlockCipher & AccessBlockCipher() =0;
81 virtual GCM_TablesOption GetTablesOption() const =0;
82
83 const BlockCipher & GetBlockCipher() const {return const_cast<GCM_Base *>(this)->AccessBlockCipher();}
84 byte *HashBuffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
85 byte *HashKey() {return m_buffer+2*REQUIRED_BLOCKSIZE;}
86 byte *MulTable() {return m_buffer+3*REQUIRED_BLOCKSIZE;}
87 inline void ReverseHashBufferIfNeeded();
88
89 class CRYPTOPP_DLL GCTR : public CTR_Mode_ExternalCipher::Encryption
90 {
91 protected:
92 void IncrementCounterBy256();
93 };
94
95 GCTR m_ctr;
96 static word16 s_reductionTable[256];
97 static volatile bool s_reductionTableInitialized;
98 enum {REQUIRED_BLOCKSIZE = 16, HASH_BLOCKSIZE = 16};
99 };
100
101 /// \brief GCM block cipher final implementation
102 /// \tparam T_BlockCipher block cipher
103 /// \tparam T_TablesOption table size, either \p GCM_2K_Tables or \p GCM_64K_Tables
104 /// \tparam T_IsEncryption direction in which to operate the cipher
105 /// \since Crypto++ 5.6.0
106 template <class T_BlockCipher, GCM_TablesOption T_TablesOption, bool T_IsEncryption>
107 class GCM_Final : public GCM_Base
108 {
109 public:
110 static std::string StaticAlgorithmName()
111 {return T_BlockCipher::StaticAlgorithmName() + std::string("/GCM");}
112 bool IsForwardTransformation() const
113 {return T_IsEncryption;}
114
115 private:
116 GCM_TablesOption GetTablesOption() const {return T_TablesOption;}
117 BlockCipher & AccessBlockCipher() {return m_cipher;}
118 typename T_BlockCipher::Encryption m_cipher;
119 };
120
121 /// \brief GCM block cipher mode of operation
122 /// \tparam T_BlockCipher block cipher
123 /// \tparam T_TablesOption table size, either \p GCM_2K_Tables or \p GCM_64K_Tables
124 /// \details \p GCM provides the \p Encryption and \p Decryption typedef. See GCM_Base
125 /// and GCM_Final for the AuthenticatedSymmetricCipher implementation.
126 /// \sa <a href="http://www.cryptopp.com/wiki/GCM_Mode">GCM Mode</a> and
127 /// <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
128 /// on the Crypto++ wiki.
129 /// \since Crypto++ 5.6.0
130 template <class T_BlockCipher, GCM_TablesOption T_TablesOption=GCM_2K_Tables>
131 struct GCM : public AuthenticatedSymmetricCipherDocumentation
132 {
133 typedef GCM_Final<T_BlockCipher, T_TablesOption, true> Encryption;
134 typedef GCM_Final<T_BlockCipher, T_TablesOption, false> Decryption;
135 };
136
137 NAMESPACE_END
138
139 #endif
GCM_Base
GCM block cipher base implementation.
Definition: gcm.h:32
GCM_Base::GetValidKeyLength
size_t GetValidKeyLength(size_t n) const
Returns a valid key length for the algorithm.
Definition: gcm.h:46
CipherModeFinalTemplate_ExternalCipher< ConcretePolicyHolder< Empty, AdditiveCipherTemplate< AbstractPolicyHolder< AdditiveCipherAbstractPolicy, CTR_ModePolicy > > > >
GCM_2K_Tables
Use a table with 2K entries.
Definition: gcm.h:25
modes.h
Classes for block cipher modes of operation.
GCM_Base::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: gcm.h:51
AuthenticatedSymmetricCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:425
GCM_Base::MaxHeaderLength
lword MaxHeaderLength() const
Provides the maximum length of AAD that can be input.
Definition: gcm.h:61
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1278
GCM_64K_Tables
Use a table with 64K entries.
Definition: gcm.h:27
GCM_Base::MinKeyLength
size_t MinKeyLength() const
Returns smallest valid key length.
Definition: gcm.h:40
GCM_Base::DefaultKeyLength
size_t DefaultKeyLength() const
Returns default key length.
Definition: gcm.h:44
GCM_Base::MaxIVLength
unsigned int MaxIVLength() const
Provides the maximum size of an IV.
Definition: gcm.h:57
SymmetricCipher
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1286
GCM
GCM block cipher mode of operation.
Definition: gcm.h:131
GCM_Base::MaxKeyLength
size_t MaxKeyLength() const
Returns largest valid key length.
Definition: gcm.h:42
AuthenticatedSymmetricCipherBase
Base class for authenticated encryption modes of operation.
Definition: authenc.h:40
StreamTransformation::OptimalDataAlignment
virtual unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cryptlib.cpp:195
GCM_Base::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: gcm.h:36
GCM_Final::IsForwardTransformation
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: gcm.h:112
SimpleKeyingInterface::IV_Requirement
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:719
GCM_Base::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: gcm.h:38
GCM_TablesOption
GCM_TablesOption
GCM table size options.
Definition: gcm.h:23
CryptoPP
Crypto++ library namespace.
GCM_Final
GCM block cipher final implementation.
Definition: gcm.h:107
GCM_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: gcm.h:59
authenc.h
Classes for authenticated encryption modes of operation.
GCM_Base::MinIVLength
unsigned int MinIVLength() const
Provides the minimum size of an IV.
Definition: gcm.h:55
GCM_Base::MaxMessageLength
lword MaxMessageLength() const
Provides the maximum length of encrypted data.
Definition: gcm.h:63
GCM_Base::IVSize
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: gcm.h:53
SimpleKeyingInterface::UNIQUE_IV
The IV must be unique.
Definition: cryptlib.h:721
GCM_Base::IsValidKeyLength
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
Definition: gcm.h:48
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321