Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cCipherDES Class Reference

#include <cCipherDES.h>

Inheritance diagram for GrayLib::cCipherDES:
GrayLib::cCipherBase Gray::CObject ICipherBase

Public Member Functions

 cCipherDES (bool bEncodeMode)
 
virtual ~cCipherDES ()
 
size_t get_BlockAlignSize () const override
 
virtual HRESULT SetCipherKey (const void *pKeyData, size_t nKeySize) override
 
virtual HRESULT CipherModeECB (BYTE pOutput[k_KeySize], const BYTE pInput[k_KeySize]) override
 
virtual HRESULT CipherModeCBC (BYTE *pOutput, const BYTE *pInput, size_t nSize, BYTE *pIV) override
 
virtual HRESULT Cipher (BYTE *pOutput, const BYTE *pInput, size_t nSizeBytes) override
 
 UNITTEST_FRIEND (cCipherDES)
 
- 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 HRESULT CipherModeECB (BYTE *pOutput, const BYTE *pInput)
 
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)
 
- 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)
 

Static Public Member Functions

static void GRAYCALL SetKeyParity (BYTE pKey[k_KeySize])
 
static bool GRAYCALL IsKeyParity (const BYTE pKey[k_KeySize])
 
static bool GRAYCALL IsKeyWeak (const BYTE pKey[k_KeySize])
 
- Static Public Member Functions inherited from GrayLib::cCipherBase
static size_t GRAYCALL CopyFillX (void *pOutput, size_t nOutSize, const void *pInput, size_t nInputSize)
 

Public Attributes

size_t m_nSizeKey
 Multiple of k_KeySize. More...
 
UINT32 m_RK [k_BlockSize *3]
 96 = size for 3DES subkeys More...
 
BYTE m_IV [k_KeySize]
 for use with Cipher CIPHER_BLOCK_CBC More...
 

Static Public Attributes

static const size_t k_KeySize = 8
 
static const size_t k_BlockSize = 32
 

Static Protected Attributes

static const UINT32 k_SB1 [64]
 
static const UINT32 k_SB2 [64]
 
static const UINT32 k_SB3 [64]
 
static const UINT32 k_SB4 [64]
 
static const UINT32 k_SB5 [64]
 
static const UINT32 k_SB6 [64]
 
static const UINT32 k_SB7 [64]
 
static const UINT32 k_SB8 [64]
 
static const UINT32 k_LHs [16]
 
static const UINT32 k_RHs [16]
 
static const BYTE k_odd_parity_table [128]
 

Additional Inherited Members

- 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

DES and Triple-DES context structure

Constructor & Destructor Documentation

◆ cCipherDES()

GrayLib::cCipherDES::cCipherDES ( bool  bEncodeMode)
inline

◆ ~cCipherDES()

virtual GrayLib::cCipherDES::~cCipherDES ( )
inlinevirtual

Member Function Documentation

◆ Cipher()

HRESULT GrayLib::cCipherDES::Cipher ( BYTE *  pOutput,
const BYTE *  pInput,
size_t  nSizeBytes 
)
overridevirtual

Reimplemented from GrayLib::cCipherBase.

◆ CipherModeCBC()

HRESULT GrayLib::cCipherDES::CipherModeCBC ( BYTE *  pOutput,
const BYTE *  pInput,
size_t  nSize,
BYTE *  pIV 
)
overridevirtual

DES-CBC buffer encryption/decryption. CIPHER_BLOCK_CBC

Note
Upon exit, the content of the IV is updated so that you can call the function same function again on the following block(s) of data and get the same result as if it was encrypted in one call. This allows a "streaming" usage. If on the other hand you need to retain the contents of the IV, you should either save it manually or use the cipher module instead.
  • pOutput = buffer holding the output data
  • pInput = buffer holding the input data
  • nSize = length of the input data
  • pIV = initialization vector (updated after use)

Reimplemented from GrayLib::cCipherBase.

◆ CipherModeECB()

HRESULT GrayLib::cCipherDES::CipherModeECB ( BYTE  pOutput[k_KeySize],
const BYTE  pInput[k_KeySize] 
)
overridevirtual

DES-ECB block encryption/decryption. CIPHER_BLOCK_ECB

  • pInput = 64-bit input block
  • pOutput = 64-bit output block
    Returns
    0 if successful

◆ get_BlockAlignSize()

size_t GrayLib::cCipherDES::get_BlockAlignSize ( ) const
inlineoverridevirtual

Reimplemented from GrayLib::cCipherBase.

◆ IsKeyParity()

bool GRAYCALL GrayLib::cCipherDES::IsKeyParity ( const BYTE  pKey[k_KeySize])
static

NOT USED Check the given key's parity, returns 1 on failure, 0 on SUCCESS Check that key parity on the given key is odd. DES keys are 56 bits long, but each byte is padded with a parity bit to allow verification.

  • pKey = 8-byte secret key
    Returns
    0 is parity was ok, 1 if parity was not correct.

◆ IsKeyWeak()

bool GRAYCALL GrayLib::cCipherDES::IsKeyWeak ( const BYTE  pKey[k_KeySize])
static

NOT USED Table of weak and semi-weak keys Source: http://en.wikipedia.org/wiki/Weak_key

Weak: Alternating ones + zeros (0x0101010101010101) Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE) '0xE0E0E0E0F1F1F1F1' '0x1F1F1F1F0E0E0E0E'

Semi-weak: 0x011F011F010E010E and 0x1F011F010E010E01 0x01E001E001F101F1 and 0xE001E001F101F101 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1

Check that key is not a weak or semi-weak DES key

  • pKey = 8-byte secret key
    Returns
    0 if no weak key was found, 1 if a weak key was identified.

◆ SetCipherKey()

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

DES key schedule (56-bit, encryption) Triple-DES key schedule (112-bit, encryption)

  • pKeyData = set cipher key. Fix it if unaligned.
  • nKeySize = 8,16,24
    Returns
    0

Reimplemented from GrayLib::cCipherBase.

◆ SetKeyParity()

void GRAYCALL GrayLib::cCipherDES::SetKeyParity ( BYTE  pKey[k_KeySize])
static

NOT USED Set key parity on the given key to odd. DES keys are 56 bits long, but each byte is padded with a parity bit to allow verification.

  • pKey = 8-byte secret key

◆ UNITTEST_FRIEND()

GrayLib::cCipherDES::UNITTEST_FRIEND ( cCipherDES  )

Member Data Documentation

◆ k_BlockSize

const size_t GrayLib::cCipherDES::k_BlockSize = 32
static

◆ k_KeySize

const size_t GrayLib::cCipherDES::k_KeySize = 8
static

◆ k_LHs

const UINT32 GrayLib::cCipherDES::k_LHs
staticprotected
Initial value:
=
{
0x00000000, 0x00000001, 0x00000100, 0x00000101,
0x00010000, 0x00010001, 0x00010100, 0x00010101,
0x01000000, 0x01000001, 0x01000100, 0x01000101,
0x01010000, 0x01010001, 0x01010100, 0x01010101
}

◆ k_odd_parity_table

const BYTE GrayLib::cCipherDES::k_odd_parity_table
staticprotected
Initial value:
=
{
1, 2, 4, 7, 8,
11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
254
}

◆ k_RHs

const UINT32 GrayLib::cCipherDES::k_RHs
staticprotected
Initial value:
=
{
0x00000000, 0x01000000, 0x00010000, 0x01010000,
0x00000100, 0x01000100, 0x00010100, 0x01010100,
0x00000001, 0x01000001, 0x00010001, 0x01010001,
0x00000101, 0x01000101, 0x00010101, 0x01010101,
}

◆ k_SB1

const UINT32 GrayLib::cCipherDES::k_SB1
staticprotected
Initial value:
=
{
0x01010400, 0x00000000, 0x00010000, 0x01010404,
0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
0x00000404, 0x01000400, 0x01000400, 0x00010400,
0x00010400, 0x01010000, 0x01010000, 0x01000404,
0x00010004, 0x01000004, 0x01000004, 0x00010004,
0x00000000, 0x00000404, 0x00010404, 0x01000000,
0x00010000, 0x01010404, 0x00000004, 0x01010000,
0x01010400, 0x01000000, 0x01000000, 0x00000400,
0x01010004, 0x00010000, 0x00010400, 0x01000004,
0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
0x01000004, 0x00000404, 0x00010404, 0x01010400,
0x00000404, 0x01000400, 0x01000400, 0x00000000,
0x00010004, 0x00010400, 0x00000000, 0x01010004
}

◆ k_SB2

const UINT32 GrayLib::cCipherDES::k_SB2
staticprotected
Initial value:
=
{
0x80108020, 0x80008000, 0x00008000, 0x00108020,
0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
0x00108000, 0x00100020, 0x80008020, 0x00000000,
0x80000000, 0x00008000, 0x00108020, 0x80100000,
0x00100020, 0x80000020, 0x00000000, 0x00108000,
0x00008020, 0x80108000, 0x80100000, 0x00008020,
0x00000000, 0x00108020, 0x80100020, 0x00100000,
0x80008020, 0x80100000, 0x80108000, 0x00008000,
0x80100000, 0x80008000, 0x00000020, 0x80108020,
0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
0x00100020, 0x80008020, 0x80000020, 0x00100020,
0x00108000, 0x00000000, 0x80008000, 0x00008020,
0x80000000, 0x80100020, 0x80108020, 0x00108000
}

◆ k_SB3

const UINT32 GrayLib::cCipherDES::k_SB3
staticprotected
Initial value:
=
{
0x00000208, 0x08020200, 0x00000000, 0x08020008,
0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
0x08000000, 0x00000008, 0x08020200, 0x00000200,
0x00020200, 0x08020000, 0x08020008, 0x00020208,
0x08000208, 0x00020200, 0x00020000, 0x08000208,
0x00000008, 0x08020208, 0x00000200, 0x08000000,
0x08020200, 0x08000000, 0x00020008, 0x00000208,
0x00020000, 0x08020200, 0x08000200, 0x00000000,
0x00000200, 0x00020008, 0x08020208, 0x08000200,
0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208,
0x00000008, 0x00020208, 0x00020200, 0x08000008,
0x08020000, 0x08000208, 0x00000208, 0x08020000,
0x00020208, 0x00000008, 0x08020008, 0x00020200
}

◆ k_SB4

const UINT32 GrayLib::cCipherDES::k_SB4
staticprotected
Initial value:
=
{
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802080, 0x00800081, 0x00800001, 0x00002001,
0x00000000, 0x00802000, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00800080, 0x00800001,
0x00000001, 0x00002000, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002001, 0x00002080,
0x00800081, 0x00000001, 0x00002080, 0x00800080,
0x00002000, 0x00802080, 0x00802081, 0x00000081,
0x00800080, 0x00800001, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00000000, 0x00802000,
0x00002080, 0x00800080, 0x00800081, 0x00000001,
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000,
0x00800001, 0x00002001, 0x00802080, 0x00800081,
0x00002001, 0x00002080, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002000, 0x00802080
}

◆ k_SB5

const UINT32 GrayLib::cCipherDES::k_SB5
staticprotected
Initial value:
=
{
0x00000100, 0x02080100, 0x02080000, 0x42000100,
0x00080000, 0x00000100, 0x40000000, 0x02080000,
0x40080100, 0x00080000, 0x02000100, 0x40080100,
0x42000100, 0x42080000, 0x00080100, 0x40000000,
0x02000000, 0x40080000, 0x40080000, 0x00000000,
0x40000100, 0x42080100, 0x42080100, 0x02000100,
0x42080000, 0x40000100, 0x00000000, 0x42000000,
0x02080100, 0x02000000, 0x42000000, 0x00080100,
0x00080000, 0x42000100, 0x00000100, 0x02000000,
0x40000000, 0x02080000, 0x42000100, 0x40080100,
0x02000100, 0x40000000, 0x42080000, 0x02080100,
0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100,
0x02080000, 0x00000000, 0x40080000, 0x42000000,
0x00080100, 0x02000100, 0x40000100, 0x00080000,
0x00000000, 0x40080000, 0x02080100, 0x40000100
}

◆ k_SB6

const UINT32 GrayLib::cCipherDES::k_SB6
staticprotected
Initial value:
=
{
0x20000010, 0x20400000, 0x00004000, 0x20404010,
0x20400000, 0x00000010, 0x20404010, 0x00400000,
0x20004000, 0x00404010, 0x00400000, 0x20000010,
0x00400010, 0x20004000, 0x20000000, 0x00004010,
0x00000000, 0x00400010, 0x20004010, 0x00004000,
0x00404000, 0x20004010, 0x00000010, 0x20400010,
0x20400010, 0x00000000, 0x00404010, 0x20404000,
0x00004010, 0x00404000, 0x20404000, 0x20000000,
0x20004000, 0x00000010, 0x20400010, 0x00404000,
0x20404010, 0x00400000, 0x00004010, 0x20000010,
0x00400000, 0x20004000, 0x20000000, 0x00004010,
0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010,
0x00000010, 0x00004000, 0x20400000, 0x00404010,
0x00004000, 0x00400010, 0x20004010, 0x00000000,
0x20404000, 0x20000000, 0x00400010, 0x20004010
}

◆ k_SB7

const UINT32 GrayLib::cCipherDES::k_SB7
staticprotected
Initial value:
=
{
0x00200000, 0x04200002, 0x04000802, 0x00000000,
0x00000800, 0x04000802, 0x00200802, 0x04200800,
0x04200802, 0x00200000, 0x00000000, 0x04000002,
0x00000002, 0x04000000, 0x04200002, 0x00000802,
0x04000800, 0x00200802, 0x00200002, 0x04000800,
0x04000002, 0x04200000, 0x04200800, 0x00200002,
0x04200000, 0x00000800, 0x00000802, 0x04200802,
0x00200800, 0x00000002, 0x04000000, 0x00200800,
0x04000000, 0x00200800, 0x00200000, 0x04000802,
0x04000802, 0x04200002, 0x04200002, 0x00000002,
0x00200002, 0x04000000, 0x04000800, 0x00200000,
0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000,
0x00200800, 0x00000000, 0x00000002, 0x04200802,
0x00000000, 0x00200802, 0x04200000, 0x00000800,
0x04000002, 0x04000800, 0x00000800, 0x00200002
}

◆ k_SB8

const UINT32 GrayLib::cCipherDES::k_SB8
staticprotected
Initial value:
=
{
0x10001040, 0x00001000, 0x00040000, 0x10041040,
0x10000000, 0x10001040, 0x00000040, 0x10000000,
0x00040040, 0x10040000, 0x10041040, 0x00041000,
0x10041000, 0x00041040, 0x00001000, 0x00000040,
0x10040000, 0x10000040, 0x10001000, 0x00001040,
0x00041000, 0x00040040, 0x10040040, 0x10041000,
0x00001040, 0x00000000, 0x00000000, 0x10040040,
0x10000040, 0x10001000, 0x00041040, 0x00040000,
0x00041040, 0x00040000, 0x10041000, 0x00001000,
0x00000040, 0x10040040, 0x00001000, 0x00041040,
0x10001000, 0x00000040, 0x10000040, 0x10040000,
0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040,
0x10040000, 0x10001000, 0x10001040, 0x00000000,
0x10041040, 0x00041000, 0x00041000, 0x00001040,
0x00001040, 0x00040040, 0x10000000, 0x10041000
}

◆ m_IV

BYTE GrayLib::cCipherDES::m_IV[k_KeySize]

for use with Cipher CIPHER_BLOCK_CBC

◆ m_nSizeKey

size_t GrayLib::cCipherDES::m_nSizeKey

Multiple of k_KeySize.

◆ m_RK

UINT32 GrayLib::cCipherDES::m_RK[k_BlockSize *3]

96 = size for 3DES subkeys


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