Fabcoin Core  0.16.2
P2P Digital Currency
gcm.h
Go to the documentation of this file.
1 // gcm.h - written and placed in the public domain by Wei Dai
2 
6 
7 #ifndef CRYPTOPP_GCM_H
8 #define CRYPTOPP_GCM_H
9 
10 #include "authenc.h"
11 #include "modes.h"
12 
14 
15 enum GCM_TablesOption {
22 
28 {
29 public:
30  // AuthenticatedSymmetricCipher
31  std::string AlgorithmName() const
32  {return GetBlockCipher().AlgorithmName() + std::string("/GCM");}
33  size_t MinKeyLength() const
34  {return GetBlockCipher().MinKeyLength();}
35  size_t MaxKeyLength() const
36  {return GetBlockCipher().MaxKeyLength();}
37  size_t DefaultKeyLength() const
38  {return GetBlockCipher().DefaultKeyLength();}
39  size_t GetValidKeyLength(size_t n) const
40  {return GetBlockCipher().GetValidKeyLength(n);}
41  bool IsValidKeyLength(size_t n) const
42  {return GetBlockCipher().IsValidKeyLength(n);}
43  unsigned int OptimalDataAlignment() const;
45  {return UNIQUE_IV;}
46  unsigned int IVSize() const
47  {return 12;}
48  unsigned int MinIVLength() const
49  {return 1;}
50  unsigned int MaxIVLength() const
51  {return UINT_MAX;} // (W64LIT(1)<<61)-1 in the standard
52  unsigned int DigestSize() const
53  {return 16;}
55  {return (W64LIT(1)<<61)-1;}
57  {return ((W64LIT(1)<<39)-256)/8;}
58 
59 protected:
60  // AuthenticatedSymmetricCipherBase
62  {return false;}
63  unsigned int AuthenticationBlockSize() const
64  {return HASH_BLOCKSIZE;}
65  void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
66  void Resync(const byte *iv, size_t len);
67  size_t AuthenticateBlocks(const byte *data, size_t len);
70  void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
72 
73  virtual BlockCipher & AccessBlockCipher() =0;
74  virtual GCM_TablesOption GetTablesOption() const =0;
75 
76  const BlockCipher & GetBlockCipher() const {return const_cast<GCM_Base *>(this)->AccessBlockCipher();};
77  byte *HashBuffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
78  byte *HashKey() {return m_buffer+2*REQUIRED_BLOCKSIZE;}
79  byte *MulTable() {return m_buffer+3*REQUIRED_BLOCKSIZE;}
80  inline void ReverseHashBufferIfNeeded();
81 
83  {
84  protected:
85  void IncrementCounterBy256();
86  };
87 
89  static word16 s_reductionTable[256];
90  static volatile bool s_reductionTableInitialized;
91  enum {REQUIRED_BLOCKSIZE = 16, HASH_BLOCKSIZE = 16};
92 };
93 
100 template <class T_BlockCipher, GCM_TablesOption T_TablesOption, bool T_IsEncryption>
101 class GCM_Final : public GCM_Base
102 {
103 public:
104  static std::string StaticAlgorithmName()
105  {return T_BlockCipher::StaticAlgorithmName() + std::string("/GCM");}
107  {return T_IsEncryption;}
108 
109 private:
110  GCM_TablesOption GetTablesOption() const {return T_TablesOption;}
111  BlockCipher & AccessBlockCipher() {return m_cipher;}
112  typename T_BlockCipher::Encryption m_cipher;
113 };
114 
123 template <class T_BlockCipher, GCM_TablesOption T_TablesOption=GCM_2K_Tables>
125 {
128 };
129 
131 
132 #endif
GCM block cipher base implementation.
Definition: gcm.h:27
uint8_t byte
Definition: Common.h:57
byte * HashKey()
Definition: gcm.h:78
BlockCipher & AccessBlockCipher()
Definition: gcm.h:111
unsigned int MaxIVLength() const
Provides the maximum size of an IV.
Definition: gcm.h:50
Use a table with 2K entries.
Definition: gcm.h:19
unsigned short word16
Definition: config.h:230
const BlockCipher & GetBlockCipher() const
Definition: gcm.h:76
unsigned int MinIVLength() const
Provides the minimum size of an IV.
Definition: gcm.h:48
Class file for modes of operation.
byte * HashBuffer()
Definition: gcm.h:77
T_BlockCipher::Encryption m_cipher
Definition: gcm.h:112
virtual unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cryptlib.cpp:234
lword MaxHeaderLength() const
Provides the maximum length of AAD that can be input.
Definition: gcm.h:54
size_t MinKeyLength() const
Returns smallest valid key length.
Definition: gcm.h:33
GCM_Final< T_BlockCipher, T_TablesOption, false > Decryption
Definition: gcm.h:127
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
virtual void AuthenticateLastHeaderBlock()=0
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:437
unsigned int AuthenticationBlockSize() const
Definition: gcm.h:63
virtual void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params)=0
GCM_Final< T_BlockCipher, T_TablesOption, true > Encryption
Definition: gcm.h:126
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1095
Use a table with 64K entries.
Definition: gcm.h:21
virtual size_t AuthenticateBlocks(const byte *data, size_t len)=0
virtual void AuthenticateLastFooterBlock(byte *mac, size_t macSize)=0
GCTR m_ctr
Definition: gcm.h:88
byte * MulTable()
Definition: gcm.h:79
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: gcm.h:52
#define W64LIT(x)
Definition: config.h:241
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1103
GCM block cipher mode of operation.
Definition: gcm.h:124
Base implementation for one direction (encryption or decryption) of a stream cipher or block cipher m...
Definition: authenc.h:19
size_t MaxKeyLength() const
Returns largest valid key length.
Definition: gcm.h:35
size_t DefaultKeyLength() const
Returns default key length.
Definition: gcm.h:37
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: gcm.h:106
lword MaxMessageLength() const
Provides the maximum length of encrypted data.
Definition: gcm.h:56
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
size_t GetValidKeyLength(size_t n) const
Returns a valid key length for the algorithm.
Definition: gcm.h:39
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:598
virtual void AuthenticateLastConfidentialBlock()
Definition: authenc.h:54
GCM_TablesOption
GCM table size options.
Definition: gcm.h:17
GCM_TablesOption GetTablesOption() const
Definition: gcm.h:110
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: gcm.h:31
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: gcm.h:44
SymmetricCipher & AccessSymmetricCipher()
Definition: gcm.h:71
static std::string StaticAlgorithmName()
Definition: gcm.h:104
#define NAMESPACE_END
Definition: config.h:201
GCM block cipher final implementation.
Definition: gcm.h:101
word64 lword
Definition: config.h:245
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: gcm.h:46
virtual void Resync(const byte *iv, size_t len)=0
#define CRYPTOPP_DLL
Definition: config.h:704
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
Definition: gcm.h:41
static volatile bool s_reductionTableInitialized
Definition: gcm.h:90
Base classes for working with authenticated encryption modes of encryption.
uint8_t const * data
Definition: sha3.h:19
bool AuthenticationIsOnPlaintext() const
Definition: gcm.h:61
The IV must be unique.
Definition: cryptlib.h:600
Interface for retrieving values given their names.
Definition: cryptlib.h:279