Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cCipherBase.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cCipherBase_H
7 #define _INC_cCipherBase_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
18 
19 namespace GrayLib
20 {
22  {
27 
28  CipherAlgorithm_NULL = 0, // Just pass the data through.
31  CipherAlgorithm_RC4, // RC4, ARC4
38 
40  };
41 
43  {
48  };
49 
51  {
54  CipherKeySize_NONE = 0, // Undefined key length
55 
56  // Key length, in bits (including parity), for DES keys
57  CipherKeySize_DES = 8, // 64 bits
58  CipherKeySize_64 = 8, // 64 bits
59 
60  // Key length, in bits (including parity), for DES in two key EDE
61  CipherKeySize_DES_EDE = 16, // 128 bits = DES2
63 
64  // Key length, in bits (including parity), for DES in three-key EDE
65  CipherKeySize_DES_EDE3 = 24, // 192 bits = DES3
67  CipherKeySize_256 = 32, // 256 bits
68 
69  // Blowfish = 448 bits max.
70  };
71 
73  {
78 
81 
86  CIPHER_BLOCK_CBC = 1, // like _WIN32 CRYPT_MODE_CBC
87 
92  CIPHER_BLOCK_ECB = 2, // like _WIN32 CRYPT_MODE_ECB
93 
95  CIPHER_BLOCK_OFB = 3, // like _WIN32 CRYPT_MODE_OFB
96 
101  CIPHER_BLOCK_CFB = 4, // (default) like _WIN32 CRYPT_MODE_CFB
102 
104  CIPHER_BLOCK_CTR = 5, // Counter. // CTS = Ciphertext stealing mode ?
105 
106  // Special Counter modes. Not true modes.
107  CIPHER_BLOCK_GCM, // Galois/Counter mode for 128-bit block ciphers. AES or Camellia
108  CIPHER_BLOCK_CCM, // Counter with CBC-MAC (CCM) for 128-bit block ciphers. AES or Camellia
109  };
110 
111  MIDL_INTERFACE("0C3E2E71-B93C-11d2-AAD0-006007348453") ICipherBase
112  {
118 
120  virtual size_t get_BlockAlignSize() const = 0;
121  virtual HRESULT Cipher(BYTE* pOutput, const BYTE* pInput, size_t nSizeBytes) = 0;
122  };
123 
124  class GRAYLIB_LINK cCipherBase : public CObject, public ICipherBase // for cObjectFactory.
125  {
133 
134  protected:
137 
138  public:
139  cCipherBase(bool bEncodeMode = true, CIPHER_BLOCK_TYPE eBlockMode = CIPHER_BLOCK_UNK) noexcept
140  : m_bEncodeMode(bEncodeMode)
141  , m_eBlockMode(eBlockMode)
142  {
143  }
144  virtual ~cCipherBase()
145  {
146  }
147 
148  void put_EncodeMode(bool bEncodeMode) noexcept
149  {
152  m_bEncodeMode = bEncodeMode;
153  }
154  bool get_EncodeMode() const noexcept
155  {
156  return m_bEncodeMode;
157  }
158 
159  void put_BlockMode(CIPHER_BLOCK_TYPE eBlockMode) noexcept
160  {
161  m_eBlockMode = eBlockMode;
162  }
163 
164  virtual size_t get_BlockAlignSize() const override
165  {
169  return 1;
170  }
171 
172  virtual HRESULT SetCipherKey(const void* pKeyData = nullptr, size_t nKeySize = 0)
173  {
177  UNREFERENCED_PARAMETER(pKeyData);
178  UNREFERENCED_PARAMETER(nKeySize);
179  return S_OK;
180  }
181 
182  virtual HRESULT CipherModeECB(BYTE* pOutput, const BYTE* pInput)
183  {
186  UNREFERENCED_PARAMETER(pOutput);
187  UNREFERENCED_PARAMETER(pInput);
188  return E_NOTIMPL;
189  }
190  virtual HRESULT CipherModeCBC(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE* pIV)
191  {
193  UNREFERENCED_PARAMETER(pOutput);
194  UNREFERENCED_PARAMETER(pInput);
195  UNREFERENCED_PARAMETER(nSize);
197  return E_NOTIMPL;
198  }
199  virtual HRESULT CipherModeCFB(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE* pIV, size_t* pIVOffset)
200  {
203  UNREFERENCED_PARAMETER(pOutput);
204  UNREFERENCED_PARAMETER(pInput);
205  UNREFERENCED_PARAMETER(nSize);
207  UNREFERENCED_PARAMETER(pIVOffset);
208  return E_NOTIMPL;
209  }
210  virtual HRESULT CipherModeCTR(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE* pNonceCounter, size_t* pNCOffset, BYTE* pStreamBlock)
211  {
213  UNREFERENCED_PARAMETER(pOutput);
214  UNREFERENCED_PARAMETER(pInput);
215  UNREFERENCED_PARAMETER(nSize);
216  UNREFERENCED_PARAMETER(pNonceCounter);
217  UNREFERENCED_PARAMETER(pNCOffset);
218  UNREFERENCED_PARAMETER(pStreamBlock);
219  return E_NOTIMPL;
220  }
221 
222  virtual HRESULT Cipher(BYTE* pOutput, const BYTE* pInput, size_t nSize) override
223  {
226  ::memcpy(pOutput, pInput, nSize);
227  return (HRESULT)nSize;
228  }
229 
230  static size_t GRAYCALL CopyFillX(void* pOutput, size_t nOutSize, const void* pInput, size_t nInputSize);
231  };
232 
234  {
237 
238  public:
241 
242  public:
243  cCipherBlockBase(bool bEncodeMode, CipherAlgorithm_TYPE eCipherAlgorithm) noexcept
244  : cCipherBase(bEncodeMode)
245  , m_eCipherAlgorithm(eCipherAlgorithm)
246  {
247  }
248 
249  virtual HRESULT AuthEncrypt(BYTE* pOutput, const BYTE* pInput, size_t nSize,
250  const BYTE* pIV, size_t nSizeIV,
251  const BYTE* pAdd, size_t nSizeAdd,
252  BYTE* pTag, size_t nSizeTag) = 0;
253 
254  virtual HRESULT AuthDecrypt(BYTE* pOutput, const BYTE* pInput, size_t nSize,
255  const BYTE* pIV, size_t nSizeIV,
256  const BYTE* pAdd, size_t nSizeAdd,
257  const BYTE* pTag, size_t nSizeTag) = 0;
258  };
259 
261  {
264 
265  public:
267 
268  public:
269  cCipherNone(bool bEncodeMode = true, size_t nBlockAlignSize = 1) noexcept
270  : cCipherBase(bEncodeMode)
271  , m_nBlockAlignSize(nBlockAlignSize)
272  {
273  }
274 
275  virtual HRESULT SetCipherKey(const void* pKeyData = nullptr, size_t nKeySize = 0) override
276  {
278  UNREFERENCED_PARAMETER(pKeyData);
279  UNREFERENCED_PARAMETER(nKeySize);
280  return S_OK;
281  }
282 
283  virtual size_t get_BlockAlignSize() const override
284  {
288  return m_nBlockAlignSize;
289  }
290 
291  virtual HRESULT Cipher(BYTE* pOutput, const BYTE* pInput, size_t nSizeBytes) override
292  {
295  if (m_nBlockAlignSize > 1)
296  {
297  nSizeBytes -= nSizeBytes % m_nBlockAlignSize;
298  }
299  ::memcpy(pOutput, pInput, nSizeBytes);
300  return (HRESULT)nSizeBytes; // all with m_nBlockAlignSize
301  }
302  };
303 };
304 
305 #endif // _INC_cCipherBase_H
#define IGNORE_WARN_INTERFACE(c)
Definition: GrayCore.h:79
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cCipherBase.h:125
virtual HRESULT CipherModeCTR(BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pNonceCounter, size_t *pNCOffset, BYTE *pStreamBlock)
Definition: cCipherBase.h:210
virtual size_t get_BlockAlignSize() const override
Definition: cCipherBase.h:164
void put_EncodeMode(bool bEncodeMode) noexcept
Definition: cCipherBase.h:148
void put_BlockMode(CIPHER_BLOCK_TYPE eBlockMode) noexcept
Definition: cCipherBase.h:159
bool get_EncodeMode() const noexcept
Definition: cCipherBase.h:154
virtual HRESULT SetCipherKey(const void *pKeyData=nullptr, size_t nKeySize=0)
Definition: cCipherBase.h:172
virtual HRESULT CipherModeECB(BYTE *pOutput, const BYTE *pInput)
Definition: cCipherBase.h:182
cCipherBase(bool bEncodeMode=true, CIPHER_BLOCK_TYPE eBlockMode=CIPHER_BLOCK_UNK) noexcept
Definition: cCipherBase.h:139
virtual HRESULT CipherModeCBC(BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pIV)
Definition: cCipherBase.h:190
CIPHER_BLOCK_TYPE m_eBlockMode
Cipher block mode. How are sequences of blocks treated?
Definition: cCipherBase.h:136
virtual HRESULT Cipher(BYTE *pOutput, const BYTE *pInput, size_t nSize) override
Definition: cCipherBase.h:222
bool m_bEncodeMode
We are in encode/encrypt mode vs decode/decrypt mode.
Definition: cCipherBase.h:135
virtual HRESULT CipherModeCFB(BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pIV, size_t *pIVOffset)
Definition: cCipherBase.h:199
virtual ~cCipherBase()
Definition: cCipherBase.h:144
Definition: cCipherBase.h:234
cCipherBlockBase(bool bEncodeMode, CipherAlgorithm_TYPE eCipherAlgorithm) noexcept
Definition: cCipherBase.h:243
CipherAlgorithm_TYPE m_eCipherAlgorithm
cCipherAES or cCipherCamellia
Definition: cCipherBase.h:239
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)=0
cNewPtr< cCipherBase > m_pCipherBase
cipher base used. cCipherAES or cCipherCamellia
Definition: cCipherBase.h:240
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)=0
Definition: cCipherBase.h:261
virtual size_t get_BlockAlignSize() const override
Definition: cCipherBase.h:283
virtual HRESULT SetCipherKey(const void *pKeyData=nullptr, size_t nKeySize=0) override
Definition: cCipherBase.h:275
cCipherNone(bool bEncodeMode=true, size_t nBlockAlignSize=1) noexcept
Definition: cCipherBase.h:269
size_t m_nBlockAlignSize
for testing. 1 = no block size.
Definition: cCipherBase.h:266
virtual HRESULT Cipher(BYTE *pOutput, const BYTE *pInput, size_t nSizeBytes) override
Definition: cCipherBase.h:291
Definition: cObject.h:67
Definition: cNewPtr.h:18
Definition: cCipherBase.h:111
Definition: cMesh.h:22
CipherVariable_TYPE
Definition: cCipherBase.h:43
@ CipherVariable_IV_LEN
Cipher accepts IVs of variable length.
Definition: cCipherBase.h:46
@ CipherVariable_KEY_LEN
Cipher accepts keys of variable length.
Definition: cCipherBase.h:47
@ CipherVariable_NULL
Definition: cCipherBase.h:45
CIPHER_BLOCK_TYPE
Definition: cCipherBase.h:73
@ CIPHER_BLOCK_CFB
Definition: cCipherBase.h:101
@ CIPHER_BLOCK_CCM
Definition: cCipherBase.h:108
@ CIPHER_BLOCK_CBC
Definition: cCipherBase.h:86
@ CIPHER_BLOCK_OFB
Output feedback. NOT USED.
Definition: cCipherBase.h:95
@ CIPHER_BLOCK_GCM
Definition: cCipherBase.h:107
@ CIPHER_BLOCK_ECB
Definition: cCipherBase.h:92
@ CIPHER_BLOCK_UNK
Also defined also in "WinCrypt.h" as "#define CRYPT_MODE_*" (same values).
Definition: cCipherBase.h:80
@ CIPHER_BLOCK_CTR
Counter.
Definition: cCipherBase.h:104
CipherAlgorithm_TYPE
Definition: cCipherBase.h:22
@ CipherAlgorithm_NULL
Definition: cCipherBase.h:28
@ CipherAlgorithm_CCM_Camellia
Definition: cCipherBase.h:37
@ CipherAlgorithm_DES
Definition: cCipherBase.h:32
@ CipherAlgorithm_AES
Definition: cCipherBase.h:29
@ CipherAlgorithm_GCM_AES
Definition: cCipherBase.h:34
@ CipherAlgorithm_QTY
Definition: cCipherBase.h:39
@ CipherAlgorithm_Blowfish
Definition: cCipherBase.h:33
@ CipherAlgorithm_Camellia
Definition: cCipherBase.h:30
@ CipherAlgorithm_RC4
Definition: cCipherBase.h:31
@ CipherAlgorithm_CCM_AES
Definition: cCipherBase.h:36
@ CipherAlgorithm_GCM_Camellia
Definition: cCipherBase.h:35
CipherKeySize_TYPE
Definition: cCipherBase.h:51
@ CipherKeySize_128
Definition: cCipherBase.h:62
@ CipherKeySize_DES_EDE
Definition: cCipherBase.h:61
@ CipherKeySize_DES
Definition: cCipherBase.h:57
@ CipherKeySize_64
Definition: cCipherBase.h:58
@ CipherKeySize_DES_EDE3
Definition: cCipherBase.h:65
@ CipherKeySize_192
Definition: cCipherBase.h:66
@ CipherKeySize_NONE
Definition: cCipherBase.h:54
@ CipherKeySize_256
Definition: cCipherBase.h:67
MIDL_INTERFACE("0C3E2E71-B93C-11d2-AAD0-006007654304") IScriptableObj