Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GrayLib::cCipherGCM Class Reference

#include <cCipherGCM.h>

Inheritance diagram for GrayLib::cCipherGCM:
GrayLib::cCipherBlockBase GrayLib::cCipherBase Gray::CObject ICipherBase

Public Member Functions

 cCipherGCM (bool bEncodeMode, CipherAlgorithm_TYPE eCipherAlgorithm)
 
virtual ~cCipherGCM ()
 
virtual HRESULT SetCipherKey (const void *pKeyData, size_t nKeySize) override
 
virtual HRESULT AuthEncrypt (BYTE *pOutput, const BYTE *pInput, size_t nSize, const BYTE *pIV, size_t nSizeIV, const BYTE *pAdd, size_t nSizeAdd, BYTE *pTag, size_t nSizeTag) override
 
virtual HRESULT AuthDecrypt (BYTE *pOutput, const BYTE *pInput, size_t nSize, const BYTE *pIV, size_t nSizeIV, const BYTE *pAdd, size_t nSizeAdd, const BYTE *pTag, size_t nSizeTag) override
 
HRESULT StartGCM (const BYTE *pIV, size_t nSizeIV, const BYTE *pAdd, size_t nSizeAdd)
 
HRESULT UpdateGCM (BYTE *pOutput, const BYTE *pInput, size_t nSize)
 
HRESULT FinishGCM (BYTE *pTag, size_t nSizeTag)
 
 UNITTEST_FRIEND (cCipherGCM)
 
- Public Member Functions inherited from GrayLib::cCipherBlockBase
 cCipherBlockBase (bool bEncodeMode, CipherAlgorithm_TYPE eCipherAlgorithm) noexcept
 
- Public Member Functions inherited from GrayLib::cCipherBase
 cCipherBase (bool bEncodeMode=true, CIPHER_BLOCK_TYPE eBlockMode=CIPHER_BLOCK_UNK) noexcept
 
virtual ~cCipherBase ()
 
void put_EncodeMode (bool bEncodeMode) noexcept
 
bool get_EncodeMode () const noexcept
 
void put_BlockMode (CIPHER_BLOCK_TYPE eBlockMode) noexcept
 
virtual size_t get_BlockAlignSize () const override
 
virtual HRESULT CipherModeECB (BYTE *pOutput, const BYTE *pInput)
 
virtual HRESULT CipherModeCBC (BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pIV)
 
virtual HRESULT CipherModeCFB (BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pIV, size_t *pIVOffset)
 
virtual HRESULT CipherModeCTR (BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pNonceCounter, size_t *pNCOffset, BYTE *pStreamBlock)
 
virtual HRESULT Cipher (BYTE *pOutput, const BYTE *pInput, size_t nSize) override
 
- Public Member Functions inherited from Gray::CObject
virtual ~CObject ()
 
virtual bool isValidCheck () const noexcept
 < memory allocation and structure definitions are valid. More...
 
virtual void AssertValid () const
 < memory allocation and structure definitions are valid. More...
 
virtual void Serialize (cArchive &a)
 

Public Attributes

UINT64 m_HL [k_BlockAlign]
 Pre-calculated HTable BuildGCMTable. More...
 
UINT64 m_HH [k_BlockAlign]
 Pre-calculated HTable. More...
 
UINT64 m_len
 Total data length. More...
 
UINT64 m_add_len
 Total pAdd length. More...
 
BYTE m_base_ectr [k_BlockAlign]
 First ECTR for tag. More...
 
BYTE m_y [k_BlockAlign]
 Y working value. More...
 
BYTE m_buf [k_BlockAlign]
 buf working value More...
 
- Public Attributes inherited from GrayLib::cCipherBlockBase
CipherAlgorithm_TYPE m_eCipherAlgorithm
 cCipherAES or cCipherCamellia More...
 
cNewPtr< cCipherBasem_pCipherBase
 cipher base used. cCipherAES or cCipherCamellia More...
 

Static Public Attributes

static const size_t k_BlockAlign = 16
 
static const UINT64 k_last4 [k_BlockAlign]
 

Additional Inherited Members

- Static Public Member Functions inherited from GrayLib::cCipherBase
static size_t GRAYCALL CopyFillX (void *pOutput, size_t nOutSize, const void *pInput, size_t nInputSize)
 
- Protected Attributes inherited from GrayLib::cCipherBase
bool m_bEncodeMode
 We are in encode/encrypt mode vs decode/decrypt mode. More...
 
CIPHER_BLOCK_TYPE m_eBlockMode
 Cipher block mode. How are sequences of blocks treated? More...
 

Detailed Description

CIPHER_BLOCK_GCM context structure can use cCipherAES, cCipherCamellia

Constructor & Destructor Documentation

◆ cCipherGCM()

GrayLib::cCipherGCM::cCipherGCM ( bool  bEncodeMode,
CipherAlgorithm_TYPE  eCipherAlgorithm 
)
inline

◆ ~cCipherGCM()

virtual GrayLib::cCipherGCM::~cCipherGCM ( )
inlinevirtual

Member Function Documentation

◆ AuthDecrypt()

HRESULT GrayLib::cCipherGCM::AuthDecrypt ( BYTE *  pOutput,
const BYTE *  pInput,
size_t  nSize,
const BYTE *  pIV,
size_t  nSizeIV,
const BYTE *  pAdd,
size_t  nSizeAdd,
const BYTE *  pTag,
size_t  nSizeTag 
)
overridevirtual

GCM buffer authenticated decryption using a block cipher Packet-oriented decryption for AEAD modes

Note
On decryption, the output buffer cannot be the same as input buffer. If buffers overlap, the output buffer must trail at least 8 bytes behind the input buffer.
  • pOutput = buffer for holding the output data
  • pInput = buffer holding the input data
  • nSize = length of the input data
  • pIV = initialization vector
  • nSizeIV = length of IV
  • pAdd = additional data
  • nSizeAdd = length of additional data
  • pTag = buffer holding the tag
  • nSizeTag = length of the tag
Returns
0 if successful and authenticated, HRESULT_WIN32_C(ERROR_NOT_AUTHENTICATED) if tag does not match

Implements GrayLib::cCipherBlockBase.

◆ AuthEncrypt()

HRESULT GrayLib::cCipherGCM::AuthEncrypt ( BYTE *  pOutput,
const BYTE *  pInput,
size_t  nSize,
const BYTE *  pIV,
size_t  nSizeIV,
const BYTE *  pAdd,
size_t  nSizeAdd,
BYTE *  pTag,
size_t  nSizeTag 
)
overridevirtual

GCM buffer encryption/decryption using a block cipher

Note
On encryption, the output buffer can be the same as the input buffer. On decryption, the output buffer cannot be the same as input buffer. If buffers overlap, the output buffer must trail at least 8 bytes behind the input buffer.
  • pOutput = buffer for holding the output data
  • pInput = buffer holding the input data
  • nSize = length of the input data
  • pIV = initialization vector
  • nSizeIV = length of IV
  • pAdd = additional data
  • nSizeAdd = length of additional data
  • pTag = buffer for holding the tag
  • nSizeTag=length of the tag to generate
Returns
0 if successful

Implements GrayLib::cCipherBlockBase.

◆ FinishGCM()

HRESULT GrayLib::cCipherGCM::FinishGCM ( BYTE *  pTag,
size_t  nSizeTag 
)

Generic GCM finalization function. Wraps up the GCM stream and generates the tag. The tag can have a maximum length of 16 bytes.

  • pTag = buffer for holding the tag (may be nullptr if tag_len is 0)
  • nSizeTag = length of the tag to generate
    Returns
    0 if successful or < 0 = error

◆ SetCipherKey()

HRESULT GrayLib::cCipherGCM::SetCipherKey ( const void *  pKeyData,
size_t  nKeySize 
)
overridevirtual

GCM initialization (encryption)

  • pKeyData = set cipher key. Fix it if unaligned.
  • nKeySize = must be 128/8, 192/8 or 256/8
    Returns
    0 if successful, or a cipher specific error code

Reimplemented from GrayLib::cCipherBase.

◆ StartGCM()

HRESULT GrayLib::cCipherGCM::StartGCM ( const BYTE *  pIV,
size_t  nSizeIV,
const BYTE *  pAdd,
size_t  nSizeAdd 
)

Generic GCM stream start function

  • pIV = initialization vector
  • nSizeIV = length of IV
  • pAdd = additional data (or nullptr if length is 0)
  • nSizeAdd = length of additional data
    Returns
    0 if successful

CIPHER_BLOCK_GCM IV and AD are limited to 2^64 bits, so 2^61 bytes

◆ UNITTEST_FRIEND()

GrayLib::cCipherGCM::UNITTEST_FRIEND ( cCipherGCM  )

◆ UpdateGCM()

HRESULT GrayLib::cCipherGCM::UpdateGCM ( BYTE *  pOutput,
const BYTE *  pInput,
size_t  nSize 
)

Generic GCM update function. Encrypts/decrypts using the given GCM context. Expects input to be a multiple of 16 bytes! Only the last call before FinishGCM() can be less than 16 bytes!

Note
On decryption, the output buffer cannot be the same as input buffer.
  • pOutput = buffer for holding the output data
  • pInput = buffer holding the input data
  • nSize = length of the input data
Returns
0 if successful or < 0 = error

Member Data Documentation

◆ k_BlockAlign

const size_t GrayLib::cCipherGCM::k_BlockAlign = 16
static

◆ k_last4

const UINT64 GrayLib::cCipherGCM::k_last4
static
Initial value:
=
{
0x0000, 0x1c20, 0x3840, 0x2460,
0x7080, 0x6ca0, 0x48c0, 0x54e0,
0xe100, 0xfd20, 0xd940, 0xc560,
0x9180, 0x8da0, 0xa9c0, 0xb5e0
}

◆ m_add_len

UINT64 GrayLib::cCipherGCM::m_add_len

Total pAdd length.

◆ m_base_ectr

BYTE GrayLib::cCipherGCM::m_base_ectr[k_BlockAlign]

First ECTR for tag.

◆ m_buf

BYTE GrayLib::cCipherGCM::m_buf[k_BlockAlign]

buf working value

◆ m_HH

UINT64 GrayLib::cCipherGCM::m_HH[k_BlockAlign]

Pre-calculated HTable.

◆ m_HL

UINT64 GrayLib::cCipherGCM::m_HL[k_BlockAlign]

Pre-calculated HTable BuildGCMTable.

◆ m_len

UINT64 GrayLib::cCipherGCM::m_len

Total data length.

◆ m_y

BYTE GrayLib::cCipherGCM::m_y[k_BlockAlign]

Y working value.


The documentation for this class was generated from the following files: