Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Protected Attributes | Private Attributes | List of all members
CASymCipherCTR Class Reference

This class could be used for encryption/decryption of data (streams) with AES using 128bit CTR mode. More...

#include <CASymCipherCTR.hpp>

Inheritance diagram for CASymCipherCTR:
Collaboration diagram for CASymCipherCTR:

Public Member Functions

 CASymCipherCTR ()
 
 ~CASymCipherCTR ()
 
bool isKeyValid ()
 
SINT32 setKey (const UINT8 *key)
 Sets the keys for crypt1() and crypt2() to the same key. More...
 
SINT32 setKeys (const UINT8 *key, UINT32 keysize)
 Sets the keys for crypt1() and crypt2() either to the same key (if keysize==KEY_SIZE) or to different values, if keysize==2* KEY_SIZE. More...
 
SINT32 setIVs (const UINT8 *p_iv)
 Sets iv1 and iv2 to p_iv. More...
 
SINT32 setIV2 (const UINT8 *p_iv)
 Sets iv2 to p_iv. More...
 
SINT32 crypt1 (const UINT8 *in, UINT8 *out, UINT32 len)
 Encryptes/Decrpytes in to out using iv1 and key1. More...
 
SINT32 crypt2 (const UINT8 *in, UINT8 *out, UINT32 len)
 Decryptes in to out using iv2 and key2. More...
 
- Public Member Functions inherited from CALockAble
 CALockAble ()
 
virtual ~CALockAble ()
 
SINT32 lock ()
 Locks the lockable object by threadsafe incrementing a reference counter. More...
 
SINT32 unlock ()
 Unlocks the lockable object by threadsafe decrementing a reference counter. More...
 

Protected Attributes

EVP_CIPHER_CTX * m_ctxAES1
 
EVP_CIPHER_CTX * m_ctxAES2
 
UINT8 key1 [16]
 
UINT8 key2 [16]
 
UINT8m_iv1
 
UINT8m_iv2
 
bool m_bKeySet
 

Private Attributes

CAMutexm_pcsEnc
 
CAMutexm_pcsDec
 

Additional Inherited Members

- Static Public Member Functions inherited from CASymChannelCipher
static const UINT8 *const getAlgorithmName (SYMCHANNELCIPHER_ALGORITHM alg)
 
- Protected Member Functions inherited from CALockAble
SINT32 waitForDestroy ()
 If called checks if the reference counter equals zero. More...
 

Detailed Description

This class could be used for encryption/decryption of data (streams) with AES using 128bit CTR mode.

Because of the CTR mode technical encryption and decrpytion are the same (depending on the kind of input). Therefore there is only a general crypt() function. This class has a 2-in-1 feature: Two independent IVs are available. Therefore we have crypt1() and crypt2() depending on the used IV.

Definition at line 43 of file CASymCipherCTR.hpp.

Constructor & Destructor Documentation

◆ CASymCipherCTR()

CASymCipherCTR::CASymCipherCTR ( )
inline

Definition at line 47 of file CASymCipherCTR.hpp.

48  {
49  m_bKeySet=false;
50  m_ctxAES1=EVP_CIPHER_CTX_new();
51  m_ctxAES2=EVP_CIPHER_CTX_new();
52 
53  m_iv1=new UINT8[16];
54  m_iv2=new UINT8[16];
55 
56  m_pcsEnc = new CAMutex();
57  m_pcsDec = new CAMutex();
58  }
unsigned char UINT8
Definition: basetypedefs.h:135
EVP_CIPHER_CTX * m_ctxAES2
EVP_CIPHER_CTX * m_ctxAES1

References m_bKeySet, m_ctxAES1, m_ctxAES2, m_iv1, m_iv2, m_pcsDec, and m_pcsEnc.

◆ ~CASymCipherCTR()

CASymCipherCTR::~CASymCipherCTR ( )
inline

Definition at line 60 of file CASymCipherCTR.hpp.

61  {
62 #ifndef ONLY_LOCAL_PROXY
64 #endif
65  delete[] m_iv1;
66  m_iv1 = NULL;
67  delete[] m_iv2;
68  m_iv2 = NULL;
69 
70  delete m_pcsEnc;
71  m_pcsEnc = NULL;
72  delete m_pcsDec;
73  m_pcsDec = NULL;
74  }
SINT32 waitForDestroy()
If called checks if the reference counter equals zero.
Definition: CALockAble.hpp:82

References m_iv1, m_iv2, m_pcsDec, m_pcsEnc, and CALockAble::waitForDestroy().

Here is the call graph for this function:

Member Function Documentation

◆ crypt1()

SINT32 CASymCipherCTR::crypt1 ( const UINT8 in,
UINT8 out,
UINT32  len 
)
virtual

Encryptes/Decrpytes in to out using iv1 and key1.

AES is used for encryption and the encryption is done with a special 128bit-OFB mode: In the case that (len mod 16 !=0) the unused cipher output bits are discarded and NOT used next time encryptAES() is called. That means that every time encrpytAES() is called at first new cipher output is created by calling AES-encrypt(iv).

Parameters
ininput (plain text) bytes
outoutput (encrpyted) bytes
lenlen of input. because the cipher preserves the size, len of output=len of input
Return values
E_SUCCESS

Implements CASymChannelCipher.

Definition at line 85 of file CASymCipherCTR.cpp.

86  {
87  UINT32 i=2000;
88  UINT8 tmpBuff[2000];
89  /*int ret=*/
90  EVP_DecryptUpdate(m_ctxAES1, tmpBuff, (int*)&i, in, len);
91  /*if (ret != 1)
92  {
93  CAMsg::printMsg(LOG_ERR, "Error in CASymCipher::crypt1()\n ");
94  return E_UNKNOWN;
95  }*/
96  memcpy(out, tmpBuff, len);
97  return E_SUCCESS;
98  }
unsigned int UINT32
Definition: basetypedefs.h:131
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
UINT16 len
Definition: typedefs.hpp:0

References E_SUCCESS, len, and m_ctxAES1.

◆ crypt2()

SINT32 CASymCipherCTR::crypt2 ( const UINT8 in,
UINT8 out,
UINT32  len 
)
virtual

Decryptes in to out using iv2 and key2.

Parameters
ininput (encrypted) bytes
outoutput (decrpyted) bytes
lenlen of input. because the cipher preserves the size, len of output=len of input
Return values
E_SUCCESS

Implements CASymChannelCipher.

Definition at line 107 of file CASymCipherCTR.cpp.

108 {
109  UINT32 i=2000;
110  UINT8 tmpBuff[2000];
111 
112  EVP_EncryptUpdate(m_ctxAES2, tmpBuff, (int*)&i, in, len);
113  memcpy(out, tmpBuff, len);
114  return E_SUCCESS;
115 }

References E_SUCCESS, len, and m_ctxAES2.

◆ isKeyValid()

bool CASymCipherCTR::isKeyValid ( )
inlinevirtual

Implements CASymChannelCipher.

Definition at line 75 of file CASymCipherCTR.hpp.

76  {
77  return m_bKeySet;
78  }

References m_bKeySet.

◆ setIV2()

SINT32 CASymCipherCTR::setIV2 ( const UINT8 p_iv)
inlinevirtual

Sets iv2 to p_iv.

Parameters
p_iv16 random bytes used for new iv2.
Return values
E_SUCCESS

Implements CASymChannelCipher.

Definition at line 104 of file CASymCipherCTR.hpp.

105  {
106  memcpy(m_iv2,p_iv,16);
107  EVP_EncryptInit_ex(m_ctxAES2, EVP_aes_128_ctr(), NULL, key2, m_iv2);
108  return E_SUCCESS;
109  }

References E_SUCCESS, key2, m_ctxAES2, and m_iv2.

◆ setIVs()

SINT32 CASymCipherCTR::setIVs ( const UINT8 p_iv)
inlinevirtual

Sets iv1 and iv2 to p_iv.

Parameters
p_iv16 random bytes used for new iv1 and iv2.
Return values
E_SUCCESS

Implements CASymChannelCipher.

Definition at line 91 of file CASymCipherCTR.hpp.

92  {
93  memcpy(m_iv1,p_iv,16);
94  memcpy(m_iv2,p_iv,16);
95  EVP_DecryptInit_ex(m_ctxAES1, EVP_aes_128_ctr(), NULL, key1, m_iv1);
96  EVP_EncryptInit_ex(m_ctxAES2, EVP_aes_128_ctr(), NULL, key2, m_iv2);
97  return E_SUCCESS;
98  }

References E_SUCCESS, key1, key2, m_ctxAES1, m_ctxAES2, m_iv1, and m_iv2.

◆ setKey()

SINT32 CASymCipherCTR::setKey ( const UINT8 key)
virtual

Sets the keys for crypt1() and crypt2() to the same key.

Sets the key1 and key2 used for encryption/decryption to the same value of key.

Also resets the IVs to zero!

Parameters
key16 random bytes used as key
bEncryptif true, the key should be used for encryption (otherwise it will be used for decryption)
Return values
E_SUCCESS

Implements CASymChannelCipher.

Definition at line 39 of file CASymCipherCTR.cpp.

40 {
41  memset(m_iv1,0,16);
42  memset(m_iv2,0,16);
43 
44  EVP_DecryptInit_ex(m_ctxAES1,EVP_aes_128_ctr(), NULL, key, m_iv1);
45  EVP_EncryptInit_ex(m_ctxAES2, EVP_aes_128_ctr(), NULL, key, m_iv2);
46  memcpy(key1, key, 16);
47  memcpy(key2, key, 16);
48 
49  m_bKeySet=true;
50  return E_SUCCESS;
51 }

References E_SUCCESS, key1, key2, m_bKeySet, m_ctxAES1, m_ctxAES2, m_iv1, and m_iv2.

Referenced by setKeys().

Here is the caller graph for this function:

◆ setKeys()

SINT32 CASymCipherCTR::setKeys ( const UINT8 key,
UINT32  keysize 
)
virtual

Sets the keys for crypt1() and crypt2() either to the same key (if keysize==KEY_SIZE) or to different values, if keysize==2* KEY_SIZE.

Implements CASymChannelCipher.

Definition at line 53 of file CASymCipherCTR.cpp.

54 {
55  if(keysize==KEY_SIZE)
56  {
57  return setKey(key);
58  }
59  else if(keysize==2*KEY_SIZE)
60  {
61  memset(m_iv1,0,16);
62  memset(m_iv2,0,16);
63  EVP_DecryptInit_ex(m_ctxAES1,EVP_aes_128_ctr(), NULL, key, m_iv1);
64  EVP_EncryptInit_ex(m_ctxAES2, EVP_aes_128_ctr(), NULL, key+KEY_SIZE, m_iv2);
65  memcpy(key1, key, 16);
66  memcpy(key2, key + KEY_SIZE, 16);
67  m_bKeySet=true;
68  return E_SUCCESS;
69  }
70  return E_UNKNOWN;
71 }
#define KEY_SIZE
Definition: CASymCipher.hpp:31
SINT32 setKey(const UINT8 *key)
Sets the keys for crypt1() and crypt2() to the same key.
#define E_UNKNOWN
Definition: errorcodes.hpp:3

References E_SUCCESS, E_UNKNOWN, key1, key2, KEY_SIZE, m_bKeySet, m_ctxAES1, m_ctxAES2, m_iv1, m_iv2, and setKey().

Here is the call graph for this function:

Member Data Documentation

◆ key1

UINT8 CASymCipherCTR::key1[16]
protected

Definition at line 123 of file CASymCipherCTR.hpp.

Referenced by setIVs(), setKey(), and setKeys().

◆ key2

UINT8 CASymCipherCTR::key2[16]
protected

Definition at line 124 of file CASymCipherCTR.hpp.

Referenced by setIV2(), setIVs(), setKey(), and setKeys().

◆ m_bKeySet

bool CASymCipherCTR::m_bKeySet
protected

Definition at line 129 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), isKeyValid(), setKey(), and setKeys().

◆ m_ctxAES1

EVP_CIPHER_CTX* CASymCipherCTR::m_ctxAES1
protected

Definition at line 121 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), crypt1(), setIVs(), setKey(), and setKeys().

◆ m_ctxAES2

EVP_CIPHER_CTX* CASymCipherCTR::m_ctxAES2
protected

Definition at line 122 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), crypt2(), setIV2(), setIVs(), setKey(), and setKeys().

◆ m_iv1

UINT8* CASymCipherCTR::m_iv1
protected

Definition at line 127 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), setIVs(), setKey(), setKeys(), and ~CASymCipherCTR().

◆ m_iv2

UINT8* CASymCipherCTR::m_iv2
protected

Definition at line 128 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), setIV2(), setIVs(), setKey(), setKeys(), and ~CASymCipherCTR().

◆ m_pcsDec

CAMutex* CASymCipherCTR::m_pcsDec
private

Definition at line 116 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), and ~CASymCipherCTR().

◆ m_pcsEnc

CAMutex* CASymCipherCTR::m_pcsEnc
private

Definition at line 115 of file CASymCipherCTR.hpp.

Referenced by CASymCipherCTR(), and ~CASymCipherCTR().


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