Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cCipherDES.h
Go to the documentation of this file.
1 //
5 //
6 #ifndef _INC_cCipherDES_H
7 #define _INC_cCipherDES_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 #include "cCipherBase.h"
12 #include "cCipherTypeDef.h"
13 
14 namespace GrayLib
15 {
17  {
20 
21  public:
22  static const size_t k_KeySize = 8; // bytes = 64 bits
23  static const size_t k_BlockSize = 32; //
24 
25  size_t m_nSizeKey;
26  UINT32 m_RK[k_BlockSize * 3];
27  BYTE m_IV[k_KeySize];
28 
29  protected:
30  static const UINT32 k_SB1[64];
31  static const UINT32 k_SB2[64];
32  static const UINT32 k_SB3[64];
33  static const UINT32 k_SB4[64];
34  static const UINT32 k_SB5[64];
35  static const UINT32 k_SB6[64];
36  static const UINT32 k_SB7[64];
37  static const UINT32 k_SB8[64];
38 
39  static const UINT32 k_LHs[16];
40  static const UINT32 k_RHs[16];
41  static const BYTE k_odd_parity_table[128];
42 
43  private:
44  static void SetCipherKeyInt1(UINT32 pSK[k_BlockSize], const BYTE pKey[k_KeySize]);
45  static void SetCipherKeyInt2(UINT32 esk[96], UINT32 dsk[96], const BYTE pKey[k_KeySize * 2]);
46  static void SetCipherKeyInt3(UINT32 esk[96], UINT32 dsk[96], const BYTE pKey[k_KeySize * 3]);
47 
48  public:
49  cCipherDES(bool bEncodeMode)
50  : cCipherBase(bEncodeMode)
51  , m_nSizeKey(0)
52  {
53  cMem::Zero(m_RK, sizeof(m_RK));
54  }
55  virtual ~cCipherDES()
56  {
57  cMem::ZeroSecure(m_RK, sizeof(m_RK));
58  }
59 
60  size_t get_BlockAlignSize() const override
61  {
62  return k_KeySize;
63  }
64 
65  static void GRAYCALL SetKeyParity(BYTE pKey[k_KeySize]);
66  static bool GRAYCALL IsKeyParity(const BYTE pKey[k_KeySize]);
67  static bool GRAYCALL IsKeyWeak(const BYTE pKey[k_KeySize]);
68 
69  virtual HRESULT SetCipherKey(const void* pKeyData, size_t nKeySize) override;
70 
71  virtual HRESULT CipherModeECB(BYTE pOutput[k_KeySize], const BYTE pInput[k_KeySize] ) override;
72  virtual HRESULT CipherModeCBC(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE* pIV) override;
73 
74  virtual HRESULT Cipher(BYTE* pOutput, const BYTE* pInput, size_t nSizeBytes) override;
75 
77  };
78 
80  {
83  public:
85  {
86  }
87  virtual cCipherBase* AllocCipherAlg(bool bEncodeMode) const
88  {
89  return new cCipherDES(bEncodeMode);
90  }
91  };
92 }
93 #endif
#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
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cCipherDES.h:80
cCipherAlgDES()
Definition: cCipherDES.h:84
virtual cCipherBase * AllocCipherAlg(bool bEncodeMode) const
Definition: cCipherDES.h:87
Definition: cCipherTypeDef.h:67
Definition: cCipherBase.h:125
Definition: cCipherDES.h:17
cCipherDES(bool bEncodeMode)
Definition: cCipherDES.h:49
size_t get_BlockAlignSize() const override
Definition: cCipherDES.h:60
virtual ~cCipherDES()
Definition: cCipherDES.h:55
size_t m_nSizeKey
Multiple of k_KeySize.
Definition: cCipherDES.h:25
UNITTEST_FRIEND(cCipherDES)
Definition: cMesh.h:22
@ CipherAlgorithm_DES
Definition: cCipherBase.h:32
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100
static void ZeroSecure(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:110