Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cCipherBlowfish.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cCipherBlowfish_H
7 #define _INC_cCipherBlowfish_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cCipherBase.h"
13 #include "cCipherTypeDef.h"
14 #include "GrayCore/include/cMem.h"
16 
17 namespace GrayLib
18 {
20  {
25 
26  public:
28  public:
30  {
31  this->m_v.u_qw = 0;
32  }
33  cCipherBlowfishBlock(const BYTE* pInput)
34  {
35  cMem::CopyNtoH(&m_v.u_qw, pInput, sizeof(m_v));
36  }
38  {
39  this->m_v.u_qw = b.m_v.u_qw;
40  }
42  {
43  this->m_v.u_qw ^= b.m_v.u_qw;
44  return *this;
45  }
46  void put_BytesToBlock(const BYTE* pInput)
47  {
49  cMem::CopyNtoH(&m_v.u_qw, pInput, sizeof(m_v));
50  }
51  void get_BlockBytes(BYTE* pOutput) const
52  {
54  cMem::CopyHtoN(pOutput, &m_v.u_qw, sizeof(m_v));
55  }
56  };
57 
59  {
66 
67  private:
68  static const int k_Rounds = 16;
69  static const UINT32 k_P[k_Rounds + 2];
70  static const UINT32 k_S[4][256];
71 
72  UINT32 m_RK[k_Rounds + 2];
73  UINT32 m_S[4][256];
74 
75  cCipherBlowfishBlock m_oChain; // The Initialization Vector, by default {0, 0} for use with CIPHER_BLOCK_TYPE
76 
77  public:
78  static const size_t k_BlockAlign = 8;
79  static const size_t k_KeySizeMax = 56;
80 
81  private:
82  inline UINT32 F(UINT32 ui) const noexcept
83  {
84  return ((m_S[0][LOBYTE(ui >> 24)] + m_S[1][LOBYTE(ui >> 16)]) ^ m_S[2][LOBYTE(ui >> 8)]) + m_S[3][LOBYTE(ui)];
85  }
86  void EncryptBlock(cCipherBlowfishBlock& b) const;
87  void DecryptBlock(cCipherBlowfishBlock& b) const;
88 
89  public:
90  // Constructor - Initialize the P and S boxes for a given Key
91  cCipherBlowfish(bool bEncodeMode = true, const void* pKeyData = nullptr, size_t nKeySize = 0);
92  virtual ~cCipherBlowfish();
93 
94  // cCipherBase support
95  virtual size_t get_BlockAlignSize() const override
96  {
97  // k_BlockAlign
98  return sizeof(cCipherBlowfishBlock);
99  }
100 
101  virtual HRESULT SetCipherKey(const void* pKeyData = nullptr, size_t nKeySize = 0) override;
102 
103  virtual HRESULT CipherModeECB(BYTE pOutput[k_BlockAlign], const BYTE pInput[k_BlockAlign]) override; // [k_BlockAlign]
104  virtual HRESULT CipherModeCBC(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE* pIV) override; // [k_BlockAlign]
105  virtual HRESULT CipherModeCFB(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE pIV[k_BlockAlign], size_t* pnIVOffset) override; // [k_BlockAlign]
106  virtual HRESULT CipherModeCTR(BYTE* pOutput, const BYTE* pInput, size_t nSize, BYTE pNonceCounter[k_BlockAlign], size_t* pNCOffset, BYTE pStreamBlock[k_BlockAlign]) override; // [k_BlockAlign]
107 
108  virtual HRESULT Cipher(BYTE* pOutput, const BYTE* pInput, size_t nSizeBytes) override;
109 
111  };
112 
114  {
117  public:
119  {
120  }
121  virtual cCipherBase* AllocCipherAlg(bool bEncodeMode) const
122  {
123  return new cCipherBlowfish(bEncodeMode);
124  }
125  };
126 }
127 
128 #endif // _INC_cCipherBlowfish_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define F(x, y, z)
Definition: cCipherBlowfish.h:114
virtual cCipherBase * AllocCipherAlg(bool bEncodeMode) const
Definition: cCipherBlowfish.h:121
cCipherAlgBlowfish()
Definition: cCipherBlowfish.h:118
Definition: cCipherTypeDef.h:67
Definition: cCipherBase.h:125
Definition: cCipherBlowfish.h:20
cCipherBlowfishBlock(const cCipherBlowfishBlock &b) noexcept
Definition: cCipherBlowfish.h:37
cCipherBlowfishBlock & operator^=(const cCipherBlowfishBlock &b)
Definition: cCipherBlowfish.h:41
cCipherBlowfishBlock(const BYTE *pInput)
Definition: cCipherBlowfish.h:33
void get_BlockBytes(BYTE *pOutput) const
Definition: cCipherBlowfish.h:51
cCipherBlowfishBlock() noexcept
Definition: cCipherBlowfish.h:29
cUnion64 m_v
Definition: cCipherBlowfish.h:27
void put_BytesToBlock(const BYTE *pInput)
Definition: cCipherBlowfish.h:46
Definition: cCipherBlowfish.h:59
UNITTEST_FRIEND(cCipherBlowfish)
virtual size_t get_BlockAlignSize() const override
Definition: cCipherBlowfish.h:95
Definition: cMesh.h:22
@ CipherAlgorithm_Blowfish
Definition: cCipherBase.h:33
static void CopyHtoN(BYTE *pDst, const void *pSrc, size_t nSizeBlock) noexcept
Definition: cMem.h:194
static void CopyNtoH(void *pDst, const BYTE *pSrc, size_t nSizeBlock) noexcept
Definition: cMem.h:203
UINT64 u_qw
64 bits = QuadPart = ULONGLONG.
Definition: cTypes.h:122