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

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

#include <CASymCipherGCM.hpp>

Inheritance diagram for CASymCipherGCM:
Collaboration diagram for CASymCipherGCM:

Public Member Functions

 CASymCipherGCM ()
 
 ~CASymCipherGCM ()
 
bool isKeyValid ()
 
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...
 

Protected Attributes

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

Definition at line 38 of file CASymCipherGCM.hpp.

Constructor & Destructor Documentation

◆ CASymCipherGCM()

CASymCipherGCM::CASymCipherGCM ( )
inline

Definition at line 44 of file CASymCipherGCM.hpp.

45  {
46  m_bKeySet=false;
47 
48 
49  m_nEncMsgCounter = 0;
50  m_pEncMsgIV = new UINT32[3];
51  memset(m_pEncMsgIV, 0, 12);
52  m_nDecMsgCounter = 0;
53  m_pDecMsgIV = new UINT32[3];
54  memset(m_pDecMsgIV, 0, 12);
55 
56  m_pGCMCtxEnc = NULL;
57  m_pGCMCtxDec = NULL;
58 
59  m_pcsEnc = new CAMutex();
60  m_pcsDec = new CAMutex();
61  }
unsigned int UINT32
Definition: basetypedefs.h:131
gcm_ctx_64k * m_pGCMCtxDec
gcm_ctx_64k * m_pGCMCtxEnc

References m_bKeySet, m_nDecMsgCounter, m_nEncMsgCounter, m_pcsDec, m_pcsEnc, m_pDecMsgIV, m_pEncMsgIV, m_pGCMCtxDec, and m_pGCMCtxEnc.

◆ ~CASymCipherGCM()

CASymCipherGCM::~CASymCipherGCM ( )
inline

Definition at line 63 of file CASymCipherGCM.hpp.

64  {
65 #ifndef ONLY_LOCAL_PROXY
67 #endif
68 
69  delete [] m_pEncMsgIV;
70  m_pEncMsgIV = NULL;
71  delete [] m_pDecMsgIV;
72  m_pDecMsgIV = NULL;
73 
74 #ifndef USE_OPENSSL_GCM
75  delete m_pGCMCtxEnc;
76  delete m_pGCMCtxDec;
77 #else
78  CRYPTO_gcm128_release(m_pGCMCtxEnc);
79  CRYPTO_gcm128_release(m_pGCMCtxDec);
80 #endif
81 
82  m_pGCMCtxEnc = NULL;
83  m_pGCMCtxDec = NULL;
84 
85  delete m_pcsEnc;
86  m_pcsEnc = NULL;
87  delete m_pcsDec;
88  m_pcsDec = NULL;
89  }
SINT32 waitForDestroy()
If called checks if the reference counter equals zero.
Definition: CALockAble.hpp:82

References m_pcsDec, m_pcsEnc, m_pDecMsgIV, m_pEncMsgIV, m_pGCMCtxDec, m_pGCMCtxEnc, and CALockAble::waitForDestroy().

Here is the call graph for this function:

Member Function Documentation

◆ decryptMessage()

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

Definition at line 156 of file CASymCipherGCM.hpp.

157  {
158 #ifdef NO_ENCRYPTION
159  memmove(out, in, inlen);
160  return E_SUCCESS;
161 #endif
162 
163  SINT32 ret = E_UNKNOWN;
164  //m_pcsDec->lock();
165  m_pDecMsgIV[2] = htonl(m_nDecMsgCounter);
166  if (integrityCheck)
167  {
169 #ifndef USE_OPENSSL_GCM
170  ret = ::gcm_decrypt_64k(m_pGCMCtxDec, m_pDecMsgIV, in, inlen - 16, in + inlen - 16, out);
171 #else
172  CRYPTO_gcm128_setiv(m_pGCMCtxDec, (UINT8*)m_pDecMsgIV, 12);
173  CRYPTO_gcm128_decrypt(m_pGCMCtxDec, in, out, inlen - 16);
174  ret = CRYPTO_gcm128_finish(m_pGCMCtxDec, in + inlen - 16, 16);
175 #endif
176  }
177  else
178  {
179 #ifndef USE_OPENSSL_GCM
180  ret = ::gcm_decrypt_64k(m_pGCMCtxDec, m_pDecMsgIV, in, inlen, out);
181 #else
182  CRYPTO_gcm128_setiv(m_pGCMCtxDec, (UINT8*)m_pDecMsgIV, 12);
183  ret = CRYPTO_gcm128_decrypt(m_pGCMCtxDec, in, out, inlen);
184 #endif
185  }
186  //m_pcsDec->unlock();
187 #ifndef USE_OPENSSL_GCM
188  if (ret == 0)
189 #else
190  if (ret != 0)
191 #endif
192  return E_UNKNOWN;
193  return E_SUCCESS;
194 
195  }
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3

References E_SUCCESS, E_UNKNOWN, m_nDecMsgCounter, m_pDecMsgIV, and m_pGCMCtxDec.

Referenced by CALastMixA::loop().

Here is the caller graph for this function:

◆ encryptMessage()

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

Definition at line 136 of file CASymCipherGCM.hpp.

137  {
138 #ifdef NO_ENCRYPTION
139  memmove(out, in, inlen);
140  return E_SUCCESS;
141 #endif
142 
143  //m_pcsEnc->lock();
144  m_pEncMsgIV[2] = htonl(m_nEncMsgCounter);
146 #ifndef USE_OPENSSL_GCM
147  gcm_encrypt_64k(m_pGCMCtxEnc, m_pEncMsgIV, in, inlen, out, (UINT32*)(out + inlen));
148 #else
149  CRYPTO_gcm128_setiv(m_pGCMCtxEnc, (UINT8*)m_pEncMsgIV, 12);
150  CRYPTO_gcm128_encrypt(m_pGCMCtxEnc, in, out, inlen);
151  CRYPTO_gcm128_tag(m_pGCMCtxEnc, out + inlen, 16);
152 #endif
153  //m_pcsEnc->unlock();
154  return E_SUCCESS;
155  }

References E_SUCCESS, m_nEncMsgCounter, m_pEncMsgIV, and m_pGCMCtxEnc.

Referenced by CALastMixA::loop().

Here is the caller graph for this function:

◆ isKeyValid()

bool CASymCipherGCM::isKeyValid ( )
inline

Definition at line 90 of file CASymCipherGCM.hpp.

91  {
92  return m_bKeySet;
93  }

References m_bKeySet.

◆ setGCMKeys()

void CASymCipherGCM::setGCMKeys ( UINT8 keyRecv,
UINT8 keySend 
)
inline

Definition at line 95 of file CASymCipherGCM.hpp.

96  {
97 
98 #ifndef USE_OPENSSL_GCM
99  m_pGCMCtxEnc = new gcm_ctx_64k;
100  m_pGCMCtxDec = new gcm_ctx_64k;
101 #else
102  //Note the have to provide *some* key (OpenSSL API enforced --> so the use the variables we have anyway..)
103  // The Key will be overriden by a call to setKeyGCM in any case!
104  AES_set_encrypt_key(m_iv1, 128, m_keyAES1);
105  m_pGCMCtxEnc = CRYPTO_gcm128_new(m_keyAES1, (block128_f)AES_encrypt);
106  m_pGCMCtxDec = CRYPTO_gcm128_new(m_keyAES1, (block128_f)AES_encrypt);
107 #endif
108 
109 
110 
111 #ifndef USE_OPENSSL_GCM
112  if (m_pGCMCtxDec != NULL)
113  delete m_pGCMCtxDec;
114  if (m_pGCMCtxEnc != NULL)
115  delete m_pGCMCtxEnc;
116 
117  m_pGCMCtxEnc = new gcm_ctx_64k;
118  m_pGCMCtxDec = new gcm_ctx_64k;
119  gcm_init_64k(m_pGCMCtxEnc, keySend, 128);
120  gcm_init_64k(m_pGCMCtxDec, keyRecv, 128);
121 #else
122  AES_set_encrypt_key(keyRecv, 128, m_keyAES1);
123  AES_set_encrypt_key(keySend, 128, m_keyAES2);
124  CRYPTO_gcm128_release(m_pGCMCtxEnc);
125  CRYPTO_gcm128_release(m_pGCMCtxDec);
126  m_pGCMCtxEnc = CRYPTO_gcm128_new(m_keyAES2, (block128_f)AES_encrypt);
127  m_pGCMCtxDec = CRYPTO_gcm128_new(m_keyAES1, (block128_f)AES_encrypt);
128 #endif
129  //reset IV
130  m_nEncMsgCounter = 0;
131  memset(m_pEncMsgIV, 0, 12);
132  m_nDecMsgCounter = 0;
133  memset(m_pDecMsgIV, 0, 12);
134 
135  }

References m_nDecMsgCounter, m_nEncMsgCounter, m_pDecMsgIV, m_pEncMsgIV, m_pGCMCtxDec, and m_pGCMCtxEnc.

Referenced by CALastMixA::loop().

Here is the caller graph for this function:

Member Data Documentation

◆ m_bKeySet

bool CASymCipherGCM::m_bKeySet
protected

Definition at line 214 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), and isKeyValid().

◆ m_nDecMsgCounter

UINT32 CASymCipherGCM::m_nDecMsgCounter
private

Definition at line 210 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), decryptMessage(), and setGCMKeys().

◆ m_nEncMsgCounter

UINT32 CASymCipherGCM::m_nEncMsgCounter
private

Definition at line 208 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), encryptMessage(), and setGCMKeys().

◆ m_pcsDec

CAMutex* CASymCipherGCM::m_pcsDec
private

Definition at line 200 of file CASymCipherGCM.hpp.

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

◆ m_pcsEnc

CAMutex* CASymCipherGCM::m_pcsEnc
private

Definition at line 199 of file CASymCipherGCM.hpp.

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

◆ m_pDecMsgIV

UINT32* CASymCipherGCM::m_pDecMsgIV
private

Definition at line 211 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), decryptMessage(), setGCMKeys(), and ~CASymCipherGCM().

◆ m_pEncMsgIV

UINT32* CASymCipherGCM::m_pEncMsgIV
private

Definition at line 209 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), encryptMessage(), setGCMKeys(), and ~CASymCipherGCM().

◆ m_pGCMCtxDec

gcm_ctx_64k* CASymCipherGCM::m_pGCMCtxDec
private

Definition at line 203 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), decryptMessage(), setGCMKeys(), and ~CASymCipherGCM().

◆ m_pGCMCtxEnc

gcm_ctx_64k* CASymCipherGCM::m_pGCMCtxEnc
private

Definition at line 202 of file CASymCipherGCM.hpp.

Referenced by CASymCipherGCM(), encryptMessage(), setGCMKeys(), and ~CASymCipherGCM().


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