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

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

#include <CASymCipher.hpp>

Inheritance diagram for CASymCipher:
Collaboration diagram for CASymCipher:

Public Member Functions

 CASymCipher ()
 
 ~CASymCipher ()
 
bool isKeyValid ()
 
SINT32 setKey (const UINT8 *key)
 Sets the key for encryption. More...
 
SINT32 setKey (const UINT8 *key, bool bEncrypt)
 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 setIV (const UINT8 *p_iv)
 Sets iv to p_iv. More...
 
SINT32 decryptCBCwithPKCS7 (const UINT8 *in, UINT8 *out, UINT32 *len)
 En-/Decryptes in to out using iv1 and key1. More...
 
SINT32 encryptCBCwithPKCS7 (const UINT8 *in, UINT32 inlen, UINT8 *out, UINT32 *len)
 En-/Decryptes in to out using IV1 and key1. 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

AES_KEY * m_keyAES1
 
UINT8m_iv1
 
bool m_bKeySet
 

Private Attributes

CAMutexm_pcsEnc
 
CAMutexm_pcsDec
 

Additional Inherited Members

- 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 CBC mode.

Definition at line 38 of file CASymCipher.hpp.

Constructor & Destructor Documentation

◆ CASymCipher()

CASymCipher::CASymCipher ( )
inline

Definition at line 44 of file CASymCipher.hpp.

45  {
46  m_bKeySet=false;
47 #ifdef INTEL_IPP_CRYPTO
48  int size=0;
49  ippsRijndael128GetSize(&size);
50  m_keyAES1=(IppsRijndael128Spec*)new UINT8[size];
51 #else
52  m_keyAES1=new AES_KEY;
53 #endif
54 
55  m_iv1=new UINT8[16];
56 
57 
58 
59  m_pcsEnc = new CAMutex();
60  m_pcsDec = new CAMutex();
61  }
unsigned char UINT8
Definition: basetypedefs.h:135
UINT8 * m_iv1
CAMutex * m_pcsDec
AES_KEY * m_keyAES1
CAMutex * m_pcsEnc

References m_bKeySet, m_iv1, m_keyAES1, m_pcsDec, and m_pcsEnc.

◆ ~CASymCipher()

CASymCipher::~CASymCipher ( )
inline

Definition at line 63 of file CASymCipher.hpp.

64  {
65 #ifndef ONLY_LOCAL_PROXY
67 #endif
68 #ifdef INTEL_IPP_CRYPTO
69  delete[] (UINT8*)m_keyAES1;
70 #else
71  delete m_keyAES1;
72 #endif
73  m_keyAES1 = NULL;
74  delete[] m_iv1;
75  m_iv1 = NULL;
76 
77 
78 
79 
80  delete m_pcsEnc;
81  m_pcsEnc = NULL;
82  delete m_pcsDec;
83  m_pcsDec = NULL;
84  }
SINT32 waitForDestroy()
If called checks if the reference counter equals zero.
Definition: CALockAble.hpp:82

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

Here is the call graph for this function:

Member Function Documentation

◆ decryptCBCwithPKCS7()

SINT32 CASymCipher::decryptCBCwithPKCS7 ( const UINT8 in,
UINT8 out,
UINT32 len 
)

En-/Decryptes in to out using iv1 and key1.

AES is used for en-/dcryption and the cryption is done with CBC mode and PKCS7 padding.

Parameters
ininput (plain or ciphertext) bytes
outoutput (plain or ciphertext) bytes
lenlen of input. on return the output len, which is always <= len of input
Return values
E_SUCCESS
E_UNKNOWN,iferror

Definition at line 134 of file CASymCipher.cpp.

135 {
136  if(in==NULL||out==NULL||len==NULL||*len==0)
137  return E_UNKNOWN;
138 #ifdef INTEL_IPP_CRYPTO
139 #else
140 #ifndef AES_NI
141  AES_cbc_encrypt(in,out,*len,m_keyAES1,m_iv1,AES_DECRYPT);
142 #else
143  aesni_cbc_encrypt(in,out,*len,m_keyAES1,m_iv1,AES_DECRYPT);
144 #endif
145  //Now remove padding
146  UINT32 pad=out[*len-1];
147  if(pad>16||pad>*len)
148  return E_UNKNOWN;
149  for(UINT32 i=*len-pad; i<*len-1; i++)
150  if(out[i]!=pad)
151  return E_UNKNOWN;
152  *len-=pad;
153 #endif
154  return E_SUCCESS;
155 }
unsigned int UINT32
Definition: basetypedefs.h:131
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0

References E_SUCCESS, E_UNKNOWN, len, m_iv1, and m_keyAES1.

Referenced by decryptXMLElement().

Here is the caller graph for this function:

◆ encryptCBCwithPKCS7()

SINT32 CASymCipher::encryptCBCwithPKCS7 ( const UINT8 in,
UINT32  inlen,
UINT8 out,
UINT32 len 
)

En-/Decryptes in to out using IV1 and key1.

AES is used for en-/decryption and the cryption is done with CBC mode and PKCS7 padding.

Parameters
ininput (plain or ciphertext) bytes
inlensize of the input buffer
outoutput (plain or ciphertext) bytes
lenon call len of output buffer; on return size of output buffer used, which is always > len of input
Return values
E_SUCCESS

Definition at line 166 of file CASymCipher.cpp.

167 {
168 #ifdef INTEL_IPP_CRYPTO
169 #else
170  UINT32 padlen=16-(inlen%16);
171  if(inlen+padlen>(*len))
172  {
173  return E_SPACE;
174  }
175  UINT8* tmp=new UINT8[inlen+padlen];
176  memcpy(tmp,in,inlen);
177  for(UINT32 i=inlen; i<inlen+padlen; i++)
178  {
179  tmp[i]=(UINT8)padlen;
180  }
181 #ifndef AES_NI
182  AES_cbc_encrypt(tmp,out,inlen+padlen,m_keyAES1,m_iv1,AES_ENCRYPT);
183 #else
184  aesni_cbc_encrypt(tmp,out,inlen+padlen,m_keyAES1,m_iv1,AES_ENCRYPT);
185 #endif
186  delete[] tmp;
187  tmp = NULL;
188  *len=inlen+padlen;
189 #endif
190  return E_SUCCESS;
191 }
#define E_SPACE
Definition: errorcodes.hpp:7

References E_SPACE, E_SUCCESS, len, m_iv1, and m_keyAES1.

Referenced by encryptXMLElement().

Here is the caller graph for this function:

◆ isKeyValid()

bool CASymCipher::isKeyValid ( )
inline

Definition at line 85 of file CASymCipher.hpp.

86  {
87  return m_bKeySet;
88  }

References m_bKeySet.

◆ setIV()

SINT32 CASymCipher::setIV ( const UINT8 p_iv)
inline

Sets iv to p_iv.

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

Definition at line 103 of file CASymCipher.hpp.

104  {
105  memcpy(m_iv1,p_iv,16);
106  return E_SUCCESS;
107  }

References E_SUCCESS, and m_iv1.

Referenced by decryptXMLElement(), and encryptXMLElement().

Here is the caller graph for this function:

◆ setKey() [1/2]

SINT32 CASymCipher::setKey ( const UINT8 key)

Sets the key for encryption.

Sets the key1 and key2 used for encryption/decryption.

Also resets the IVs to zero!

Parameters
key16 random bytes used as key
Return values
E_SUCCESS

Definition at line 52 of file CASymCipher.cpp.

53 {
54  return setKey(key,true);
55 }
SINT32 setKey(const UINT8 *key)
Sets the key for encryption.
Definition: CASymCipher.cpp:52

Referenced by decryptXMLElement(), encryptXMLElement(), CAFirstMixB::loop(), and CALastMixB::loop().

Here is the caller graph for this function:

◆ setKey() [2/2]

SINT32 CASymCipher::setKey ( const UINT8 key,
bool  bEncrypt 
)

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.

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

Definition at line 62 of file CASymCipher.cpp.

63 {
64  memset(m_iv1,0,16);
65 #ifdef INTEL_IPP_CRYPTO
66  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES1);
67 #else
68  if(bEncrypt)
69  {
70 #ifdef AES_NI
71  aesni_set_encrypt_key(key,128,m_keyAES1);
72 
73 #else
74  AES_set_encrypt_key(key,128,m_keyAES1);
75 #endif
76  }
77  else
78  {
79 #ifdef AES_NI
80  aesni_set_decrypt_key(key,128,m_keyAES1);
81 #else
82  AES_set_decrypt_key(key,128,m_keyAES1);
83 #endif
84  }
85 #endif
86  m_bKeySet=true;
87  return E_SUCCESS;
88 }

References E_SUCCESS, m_bKeySet, m_iv1, and m_keyAES1.

Member Data Documentation

◆ m_bKeySet

bool CASymCipher::m_bKeySet
protected

Definition at line 129 of file CASymCipher.hpp.

Referenced by CASymCipher(), isKeyValid(), and setKey().

◆ m_iv1

UINT8* CASymCipher::m_iv1
protected

◆ m_keyAES1

AES_KEY* CASymCipher::m_keyAES1
protected

◆ m_pcsDec

CAMutex* CASymCipher::m_pcsDec
private

Definition at line 116 of file CASymCipher.hpp.

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

◆ m_pcsEnc

CAMutex* CASymCipher::m_pcsEnc
private

Definition at line 115 of file CASymCipher.hpp.

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


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