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

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

#include <CASymCipherOFB.hpp>

Inheritance diagram for CASymCipherOFB:
Collaboration diagram for CASymCipherOFB:

Public Member Functions

 CASymCipherOFB ()
 
 ~CASymCipherOFB ()
 
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 setKey (const UINT8 *key, bool bEncrypt)
 Sets the key1 and key2 used for encryption/decryption to the same value of key. 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...
 

Static Public Member Functions

static SINT32 testSpeed ()
 
- Static Public Member Functions inherited from CASymChannelCipher
static const UINT8 *const getAlgorithmName (SYMCHANNELCIPHER_ALGORITHM alg)
 

Protected Attributes

AES_KEY * m_keyAES1
 
AES_KEY * m_keyAES2
 
UINT8m_iv1
 
UINT8m_iv2
 
bool m_bKeySet
 

Private Attributes

CAMutexm_pcsEnc
 
CAMutexm_pcsDec
 
UINT32m_pEncMsgIV
 
UINT32m_pDecMsgIV
 

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

Because of the OFB 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 CASymCipherOFB.hpp.

Constructor & Destructor Documentation

◆ CASymCipherOFB()

CASymCipherOFB::CASymCipherOFB ( )
inline

Definition at line 47 of file CASymCipherOFB.hpp.

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

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

◆ ~CASymCipherOFB()

CASymCipherOFB::~CASymCipherOFB ( )
inline

Definition at line 67 of file CASymCipherOFB.hpp.

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

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

Here is the call graph for this function:

Member Function Documentation

◆ crypt1()

SINT32 CASymCipherOFB::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 136 of file CASymCipherOFB.cpp.

137 {
138 #ifdef INTEL_IPP_CRYPTO
139  UINT32 k=len&0xFFFFFFF0;
140  ippsRijndael128EncryptOFB(in,out,k,16, m_keyAES1,m_iv1);
141 // if((len%16)!=0)
142 // {
143  ippsRijndael128EncryptOFB(in+k,out+k,len%16,len%16, m_keyAES1,m_iv1);
144 // }
145  return E_SUCCESS;
146 #endif
147  UINT32 i=0;
148  while(i+15<len)
149  {
150 #ifdef INTEL_IPP_CRYPTO
151  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES1, IppsCPPaddingNONE);
152 #else
153 #ifdef AES_NI
154 // int outlen = 16;
155 // EVP_EncryptUpdate(m_ctxAES1, m_iv1, &outlen, m_iv1, 16);
156  aesni_encrypt(m_iv1,m_iv1,m_keyAES1);
157 #else
158  AES_encrypt(m_iv1,m_iv1,m_keyAES1);
159 #endif
160 #endif
161  out[i]=in[i]^m_iv1[0];
162  i++;
163  out[i]=in[i]^m_iv1[1];
164  i++;
165  out[i]=in[i]^m_iv1[2];
166  i++;
167  out[i]=in[i]^m_iv1[3];
168  i++;
169  out[i]=in[i]^m_iv1[4];
170  i++;
171  out[i]=in[i]^m_iv1[5];
172  i++;
173  out[i]=in[i]^m_iv1[6];
174  i++;
175  out[i]=in[i]^m_iv1[7];
176  i++;
177  out[i]=in[i]^m_iv1[8];
178  i++;
179  out[i]=in[i]^m_iv1[9];
180  i++;
181  out[i]=in[i]^m_iv1[10];
182  i++;
183  out[i]=in[i]^m_iv1[11];
184  i++;
185  out[i]=in[i]^m_iv1[12];
186  i++;
187  out[i]=in[i]^m_iv1[13];
188  i++;
189  out[i]=in[i]^m_iv1[14];
190  i++;
191  out[i]=in[i]^m_iv1[15];
192  i++;
193  }
194  if(i<len) //In this case len-i<16 !
195  {
196 #ifdef INTEL_IPP_CRYPTO
197  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES1, IppsCPPaddingNONE);
198 #else
199 #ifdef AES_NI
200  //int outlen = 16;
201  //EVP_EncryptUpdate(m_ctxAES1, m_iv1, &outlen, m_iv1, 16);
202  aesni_encrypt(m_iv1,m_iv1,m_keyAES1);
203 
204 #else
205  AES_encrypt(m_iv1,m_iv1,m_keyAES1);
206 #endif
207 #endif
208  len-=i;
209  for(UINT32 k=0; k<len; k++)
210  {
211  out[i]=in[i]^m_iv1[k];
212  i++;
213  }
214  }
215  return E_SUCCESS;
216 }
#define KEY_SIZE
Definition: CASymCipher.hpp:31
unsigned int UINT32
Definition: basetypedefs.h:131
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
UINT16 len
Definition: typedefs.hpp:0

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

◆ crypt2()

SINT32 CASymCipherOFB::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 225 of file CASymCipherOFB.cpp.

226 {
227 
228  UINT32 i=0;
229  while(i+15<len)
230  {
231 #ifdef INTEL_IPP_CRYPTO
232  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES2, IppsCPPaddingNONE);
233 #else
234 #ifdef AES_NI
235  aesni_encrypt(m_iv2,m_iv2,m_keyAES2);
236 #else
237  AES_encrypt(m_iv2,m_iv2,m_keyAES2);
238 #endif
239 #endif
240  out[i]=in[i]^m_iv2[0];
241  i++;
242  out[i]=in[i]^m_iv2[1];
243  i++;
244  out[i]=in[i]^m_iv2[2];
245  i++;
246  out[i]=in[i]^m_iv2[3];
247  i++;
248  out[i]=in[i]^m_iv2[4];
249  i++;
250  out[i]=in[i]^m_iv2[5];
251  i++;
252  out[i]=in[i]^m_iv2[6];
253  i++;
254  out[i]=in[i]^m_iv2[7];
255  i++;
256  out[i]=in[i]^m_iv2[8];
257  i++;
258  out[i]=in[i]^m_iv2[9];
259  i++;
260  out[i]=in[i]^m_iv2[10];
261  i++;
262  out[i]=in[i]^m_iv2[11];
263  i++;
264  out[i]=in[i]^m_iv2[12];
265  i++;
266  out[i]=in[i]^m_iv2[13];
267  i++;
268  out[i]=in[i]^m_iv2[14];
269  i++;
270  out[i]=in[i]^m_iv2[15];
271  i++;
272  }
273  if(i<len)
274  {
275 #ifdef INTEL_IPP_CRYPTO
276  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES2, IppsCPPaddingNONE);
277 #else
278 #ifdef AES_NI
279  aesni_encrypt(m_iv2,m_iv2,m_keyAES2);
280 #else
281  AES_encrypt(m_iv2,m_iv2,m_keyAES2);
282 #endif
283 #endif
284  len-=i;
285  for(UINT32 k=0; k<len; k++)
286  {
287  out[i]=in[i]^m_iv2[k];
288  i++;
289  }
290  }
291  return E_SUCCESS;
292 }

References E_SUCCESS, KEY_SIZE, len, m_iv1, m_iv2, and m_keyAES2.

◆ isKeyValid()

bool CASymCipherOFB::isKeyValid ( )
inlinevirtual

Implements CASymChannelCipher.

Definition at line 91 of file CASymCipherOFB.hpp.

92  {
93  return m_bKeySet;
94  }

References m_bKeySet.

◆ setIV2()

SINT32 CASymCipherOFB::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 120 of file CASymCipherOFB.hpp.

121  {
122  memcpy(m_iv2,p_iv,16);
123  return E_SUCCESS;
124  }

References E_SUCCESS, and m_iv2.

◆ setIVs()

SINT32 CASymCipherOFB::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 109 of file CASymCipherOFB.hpp.

110  {
111  memcpy(m_iv1,p_iv,16);
112  memcpy(m_iv2,p_iv,16);
113  return E_SUCCESS;
114  }

References E_SUCCESS, m_iv1, and m_iv2.

◆ setKey() [1/2]

SINT32 CASymCipherOFB::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.

Also resets the IVs to zero!

Parameters
key16 random bytes used as key
Return values
E_SUCCESS

Implements CASymChannelCipher.

Definition at line 48 of file CASymCipherOFB.cpp.

49 {
50  return setKey(key,true);
51 }
SINT32 setKey(const UINT8 *key)
Sets the keys for crypt1() and crypt2() to the same key.

Referenced by setKeys().

Here is the caller graph for this function:

◆ setKey() [2/2]

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

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 58 of file CASymCipherOFB.cpp.

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

References E_SUCCESS, m_bKeySet, m_iv1, m_iv2, m_keyAES1, and m_keyAES2.

◆ setKeys()

SINT32 CASymCipherOFB::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 93 of file CASymCipherOFB.cpp.

94 {
95 #ifdef _DEBUG
96  CAMsg::printMsg(LOG_DEBUG, "CASymCipherOFB::setKeys() - keysize %u\n",keysize);
97 #endif
98  if(keysize==KEY_SIZE)
99  {
100  return setKey(key);
101  }
102  else if(keysize==2*KEY_SIZE)
103  {
104 #ifdef INTEL_IPP_CRYPTO
105  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES1);
106  ippsRijndael128Init(key+KEY_SIZE, IppsRijndaelKey128,m_keyAES2);
107 #else
108 #ifdef AES_NI
109  aesni_set_encrypt_key(key,128,m_keyAES1);
110  aesni_set_encrypt_key(key+KEY_SIZE,128,m_keyAES2);
111 #else
112  AES_set_encrypt_key(key,128,m_keyAES1);
113  AES_set_encrypt_key(key+KEY_SIZE,128,m_keyAES2);
114 #endif
115 #endif
116  memset(m_iv1,0,16);
117  memset(m_iv2,0,16);
118  m_bKeySet=true;
119  return E_SUCCESS;
120  }
121  return E_UNKNOWN;
122 }
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
#define E_UNKNOWN
Definition: errorcodes.hpp:3

References E_SUCCESS, E_UNKNOWN, KEY_SIZE, m_bKeySet, m_iv1, m_iv2, m_keyAES1, m_keyAES2, CAMsg::printMsg(), and setKey().

Here is the call graph for this function:

◆ testSpeed()

static SINT32 CASymCipherOFB::testSpeed ( )
static

Member Data Documentation

◆ m_bKeySet

bool CASymCipherOFB::m_bKeySet
protected

Definition at line 149 of file CASymCipherOFB.hpp.

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

◆ m_iv1

UINT8* CASymCipherOFB::m_iv1
protected

◆ m_iv2

UINT8* CASymCipherOFB::m_iv2
protected

◆ m_keyAES1

AES_KEY* CASymCipherOFB::m_keyAES1
protected

Definition at line 143 of file CASymCipherOFB.hpp.

Referenced by CASymCipherOFB(), crypt1(), setKey(), setKeys(), and ~CASymCipherOFB().

◆ m_keyAES2

AES_KEY* CASymCipherOFB::m_keyAES2
protected

Definition at line 144 of file CASymCipherOFB.hpp.

Referenced by CASymCipherOFB(), crypt2(), setKey(), setKeys(), and ~CASymCipherOFB().

◆ m_pcsDec

CAMutex* CASymCipherOFB::m_pcsDec
private

Definition at line 133 of file CASymCipherOFB.hpp.

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

◆ m_pcsEnc

CAMutex* CASymCipherOFB::m_pcsEnc
private

Definition at line 132 of file CASymCipherOFB.hpp.

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

◆ m_pDecMsgIV

UINT32* CASymCipherOFB::m_pDecMsgIV
private

Definition at line 135 of file CASymCipherOFB.hpp.

◆ m_pEncMsgIV

UINT32* CASymCipherOFB::m_pEncMsgIV
private

Definition at line 134 of file CASymCipherOFB.hpp.


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