Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cCipherWrap.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cCipherWrap_H
8 #define _INC_cCipherWrap_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 #include "cCipherTypeDef.h"
14 
15 namespace GrayLib
16 {
17  class cCipherPadding;
18 
20  {
23 
24  public:
27 
28  size_t m_nSizeKey;
30 
32 
33  static const size_t k_MAX_BLOCK_LENGTH = 16;
34  BYTE m_Unprocessed[k_MAX_BLOCK_LENGTH];
36 
37  static const size_t k_MAX_IV_LENGTH = 16;
38  BYTE m_iv[k_MAX_IV_LENGTH];
39  size_t m_nSizeIV;
40 
41  public:
43  : m_pCipherTypeDef(nullptr)
44  , m_nSizeKey(0)
45  , m_bEncodeMode(false)
46  , m_pCBCPadding(nullptr)
47  , m_nSizeUnprocessed(0)
48  , m_nSizeIV(0)
49  {
50  cMem::Zero(m_Unprocessed, sizeof(m_Unprocessed));
51  cMem::Zero(m_iv, sizeof(m_iv));
52  }
54  {
55  SetZeroCipher();
56  }
57 
58  HRESULT SetCipherType(bool bEncodeMode, const cCipherTypeDef* pCipherTypeDef, const void* pKeyData);
59 
60  void SetZeroCipher();
61 
62  HRESULT SetCipherKey(const void* pKeyData, size_t nSizeKey);
63  HRESULT SetCipherIV(const BYTE* pIV, size_t nSizeIV);
64  void ResetCipher();
65  HRESULT UpdateCipher(BYTE* pOutput, const BYTE* pInput, size_t nInputSize);
66  HRESULT FinishCipher(BYTE* pOutput, size_t* nOutputSize);
67 
68  inline size_t get_BlockAlignSize() const
69  {
71  if ( nullptr == this->m_pCipherTypeDef)
72  return 0;
73  return this->m_pCipherTypeDef->m_nBlockAlignSize;
74  }
75 
77  {
80  if ( nullptr == this->m_pCipherTypeDef)
81  return CIPHER_BLOCK_UNK;
82  return this->m_pCipherTypeDef->m_eBlockMode;
83  }
84 
85  inline size_t get_CipherIVSize() const
86  {
90  if (this->m_nSizeIV != 0) // overridden?
91  return this->m_nSizeIV;
92  if (nullptr == this->m_pCipherTypeDef)
93  return 0;
94  return this->m_pCipherTypeDef->m_nSizeIV;
95  }
96 
97  HRESULT CipherCrypt(BYTE* pOutput, const BYTE* pInput, size_t nInputSize, const BYTE* pIV, size_t nSizeIV);
98 
99  HRESULT put_CBCPadding(const cCipherPadding* pPadding = nullptr);
100 
101  HRESULT WriteGCMTag(BYTE* pTag, size_t nSizeTag);
102  HRESULT CheckGCMTag(const BYTE* pTag, size_t nSizeTag);
103 
104  // For CIPHER_BLOCK_GCM or CIPHER_BLOCK_CCM
105  HRESULT AuthEncrypt(BYTE* pOutput, const BYTE* pInput, size_t nInputSize, const BYTE* pIV, size_t nSizeIV, const BYTE* pAdd, size_t nSizeAdd, BYTE* pTag, size_t nSizeTag);
106  HRESULT AuthDecrypt(BYTE* pOutput, const BYTE* pInput, size_t nInputSize, const BYTE* pIV, size_t nSizeIV, const BYTE* pAdd, size_t nSizeAdd, const BYTE* pTag, size_t nSizeTag);
107 
109  };
110 }
111 
112 #endif // _INC_cCipherWrap_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cCipherPadding.h:16
Definition: cCipherTypeDef.h:35
CIPHER_BLOCK_TYPE m_eBlockMode
Cipher mode (e.g. CIPHER_BLOCK_CBC)
Definition: cCipherTypeDef.h:44
size_t m_nBlockAlignSize
block size, in bytes, size_t
Definition: cCipherTypeDef.h:55
size_t m_nSizeIV
IV/NONCE size, in bytes. For cipher that accept many sizes: recommended size.
Definition: cCipherTypeDef.h:50
Definition: cCipherWrap.h:20
cCipherWrap()
Definition: cCipherWrap.h:42
size_t get_CipherIVSize() const
Definition: cCipherWrap.h:85
size_t m_nSizeUnprocessed
Number of bytes that still need processing.
Definition: cCipherWrap.h:35
~cCipherWrap()
Definition: cCipherWrap.h:53
const cCipherTypeDef * m_pCipherTypeDef
Information about the associated SSL_Cipher_TYPE.
Definition: cCipherWrap.h:25
size_t m_nSizeIV
IV size in bytes (for ciphers with variable-length IVs)
Definition: cCipherWrap.h:39
size_t m_nSizeKey
Key length to use (if variable)
Definition: cCipherWrap.h:28
bool m_bEncodeMode
Operation that the m_pCryptBase has been initialized for.
Definition: cCipherWrap.h:29
cNewPtr< cCipherBase > m_pCryptBase
e.g. cCipherBase created from m_pCipherTypeDef, e.g. cCipherCCM, cCipherGCM
Definition: cCipherWrap.h:26
UNITTEST_FRIEND(cCipherWrap)
size_t get_BlockAlignSize() const
Definition: cCipherWrap.h:68
CIPHER_BLOCK_TYPE get_CipherMode() const
Definition: cCipherWrap.h:76
const cCipherPadding * m_pCBCPadding
What Padding functions to use, if using CIPHER_BLOCK_CBC.
Definition: cCipherWrap.h:31
Definition: cNewPtr.h:18
Definition: cMesh.h:22
CIPHER_BLOCK_TYPE
Definition: cCipherBase.h:73
@ CIPHER_BLOCK_UNK
Also defined also in "WinCrypt.h" as "#define CRYPT_MODE_*" (same values).
Definition: cCipherBase.h:80
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100