Mixes for Privacy and Anonymity in the Internet
Public Member Functions | Static 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 OFB mode. More...

Inheritance diagram for CASymCipher:
CALockAble

Public Member Functions

 CASymCipher ()
 
 ~CASymCipher ()
 
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...
 
SINT32 decrypt1CBCwithPKCS7 (const UINT8 *in, UINT8 *out, UINT32 *len)
 En-/Decryptes in to out using iv1 and key1. More...
 
SINT32 encrypt1CBCwithPKCS7 (const UINT8 *in, UINT32 inlen, UINT8 *out, UINT32 *len)
 En-/Decryptes in to out using IV1 and key1. More...
 
void setGCMKeys (UINT8 *keyRecv, UINT8 *keySend)
 
SINT32 encryptMessage (const UINT8 *in, UINT32 inlen, UINT8 *out)
 
SINT32 decryptMessage (const UINT8 *in, UINT32 inlen, UINT8 *out, bool integrityCheck)
 
- 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 ()
 

Protected Attributes

AES_KEY * m_keyAES1
 
AES_KEY * m_keyAES2
 
UINT8m_iv1
 
UINT8m_iv2
 
bool m_bKeySet
 

Private Attributes

CAMutexm_pcsEnc
 
CAMutexm_pcsDec
 
gcm_ctx_64k * m_pGCMCtxEnc
 
gcm_ctx_64k * m_pGCMCtxDec
 
UINT32 m_nEncMsgCounter
 
UINT32m_pEncMsgIV
 
UINT32 m_nDecMsgCounter
 
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.

Constructor & Destructor Documentation

◆ CASymCipher()

CASymCipher::CASymCipher ( )

◆ ~CASymCipher()

CASymCipher::~CASymCipher ( )

Member Function Documentation

◆ crypt1()

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

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

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

Referenced by fm_loopPacketProcessing(), lm_loopPacketProcessing(), CAFirstMixB::loop(), CALastMixB::loop(), CALocalProxy::loop(), mm_loopReadFromMixBefore(), CAMuxSocket::prepareForSend(), CAMuxSocket::receive(), CAMuxSocket::send(), setIV2(), CAFirstMixA::shutDown(), and testSpeed().

◆ crypt2()

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

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

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

Referenced by fm_loopPacketProcessing(), lm_loopPacketProcessing(), CAFirstMixB::loop(), CALocalProxy::loop(), mm_loopReadFromMixAfter(), CAChain::processDownstream(), setIV2(), and CAFirstMixA::shutDown().

◆ decrypt1CBCwithPKCS7()

SINT32 CASymCipher::decrypt1CBCwithPKCS7 ( 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

References E_SUCCESS, E_UNKNOWN, m_iv1, and m_keyAES1.

Referenced by decryptXMLElement(), and setIV2().

◆ decryptMessage()

SINT32 CASymCipher::decryptMessage ( const UINT8 in,
UINT32  inlen,
UINT8 out,
bool  integrityCheck 
)

◆ encrypt1CBCwithPKCS7()

SINT32 CASymCipher::encrypt1CBCwithPKCS7 ( 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

References E_SPACE, E_SUCCESS, m_iv1, and m_keyAES1.

Referenced by encryptXMLElement(), and setIV2().

◆ encryptMessage()

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

◆ isKeyValid()

bool CASymCipher::isKeyValid ( )

References m_bKeySet, setKey(), and setKeys().

Referenced by CALocalProxy::loop().

◆ setGCMKeys()

void CASymCipher::setGCMKeys ( UINT8 keyRecv,
UINT8 keySend 
)

◆ setIV2()

SINT32 CASymCipher::setIV2 ( const UINT8 p_iv)

Sets iv2 to p_iv.

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

References crypt1(), crypt2(), decrypt1CBCwithPKCS7(), decryptMessage(), E_SUCCESS, encrypt1CBCwithPKCS7(), encryptMessage(), len, m_iv2, setGCMKeys(), and testSpeed().

Referenced by fm_loopPacketProcessing(), CAFirstMixB::loop(), CALocalProxy::loop(), and CAFirstMixA::shutDown().

◆ setIVs()

SINT32 CASymCipher::setIVs ( const UINT8 p_iv)

Sets iv1 and iv2 to p_iv.

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

References E_SUCCESS, m_iv1, and m_iv2.

Referenced by decryptXMLElement(), CAFirstMix::doUserLogin_internal(), encryptXMLElement(), and CALocalProxy::processKeyExchange().

◆ setKey() [1/2]

SINT32 CASymCipher::setKey ( const UINT8 key)

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

Referenced by decryptXMLElement(), CAFirstMix::doUserLogin_internal(), encryptXMLElement(), isKeyValid(), CAFirstMixB::loop(), CALastMixB::loop(), CALocalProxy::processKeyExchange(), setKeys(), and testSpeed().

◆ setKey() [2/2]

SINT32 CASymCipher::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

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

◆ setKeys()

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

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

Referenced by fm_loopPacketProcessing(), isKeyValid(), lm_loopPacketProcessing(), CALocalProxy::loop(), mm_loopReadFromMixBefore(), and CAFirstMixA::shutDown().

◆ testSpeed()

SINT32 CASymCipher::testSpeed ( )
static

Member Data Documentation

◆ m_bKeySet

bool CASymCipher::m_bKeySet
protected

◆ m_iv1

UINT8* CASymCipher::m_iv1
protected

◆ m_iv2

UINT8* CASymCipher::m_iv2
protected

◆ m_keyAES1

AES_KEY* CASymCipher::m_keyAES1
protected

◆ m_keyAES2

AES_KEY* CASymCipher::m_keyAES2
protected

◆ m_nDecMsgCounter

UINT32 CASymCipher::m_nDecMsgCounter
private

◆ m_nEncMsgCounter

UINT32 CASymCipher::m_nEncMsgCounter
private

◆ m_pcsDec

CAMutex* CASymCipher::m_pcsDec
private

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

◆ m_pcsEnc

CAMutex* CASymCipher::m_pcsEnc
private

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

◆ m_pDecMsgIV

UINT32* CASymCipher::m_pDecMsgIV
private

◆ m_pEncMsgIV

UINT32* CASymCipher::m_pEncMsgIV
private

◆ m_pGCMCtxDec

gcm_ctx_64k* CASymCipher::m_pGCMCtxDec
private

◆ m_pGCMCtxEnc

gcm_ctx_64k* CASymCipher::m_pGCMCtxEnc
private