Mixe for Privacy and Anonymity in the Internet
CADataRetentionLogFile.cpp
Go to the documentation of this file.
1 #include "StdAfx.h"
2 #ifdef DATA_RETENTION_LOG
4 #include "CAMsg.hpp"
5 #include "CACmdLnOptions.hpp"
6 #include "CALibProxytest.hpp"
8  {
10  m_hLogFile=-1;
11  m_nBytesPerLogEntry=sizeof(t_dataretentionLogEntry);
15  m_pGCMCtx=new gcm_ctx_64k;
17  }
18 
20  {
21  closeLog();
22  }
23 
25  {
28  UINT8* strFileName=new UINT8[4096];
29  UINT8 strDate[256];
30  struct tm* theTime;
31  time_t t=date;
32  theTime=gmtime(&t);
33  strftime((char*) strDate,255,"%Y%m%d-%H%M%S",theTime);
34  m_Day=theTime->tm_mday;
35  m_Month=theTime->tm_mon+1;
36  m_Year=theTime->tm_year+1900;
37 
38  m_nMaxLogTime=date-theTime->tm_min*60-theTime->tm_hour*3600-theTime->tm_sec+24*3600-1;
39 
40 // m_nMaxLogTime=date+60;
41 
42  snprintf((char*)strFileName,4096,"%s/dataretentionlog_%s",strLogDir,strDate);
44  m_hLogFile=open((char*)strFileName,O_APPEND|O_CREAT|O_WRONLY|O_LARGEFILE|O_BINARY,S_IREAD|S_IWRITE);
45  delete [] strFileName;
46  if(m_hLogFile<=0)
47  return E_UNKNOWN;
48  return writeHeader(pPublicKey);
49  }
50 
52  {
54  memset(&oHeader,0,sizeof(oHeader));
55  oHeader.day=m_Day;
56  oHeader.month=m_Month;
57  oHeader.year=htons(m_Year);
59  oHeader.keys=1;
60  oHeader.loggedFields=0x00FF;//DATARETETION_LOGGED_FIELD_T_IN|DATARETETION_LOGGED_FIELD_T_OUT
61  if(CALibProxytest::getOptions()->isFirstMix())
62  {
64  }
65  else if(CALibProxytest::getOptions()->isMiddleMix())
66  {
68  }
69  else if(CALibProxytest::getOptions()->isLastMix())
70  {
72  }
73  if(write(m_hLogFile,&oHeader,sizeof(oHeader))!=sizeof(oHeader))
74  return E_UNKNOWN;
75 
76 //Generate sym key and write it to header
77  UINT8 keybuff[256];
78  getRandom(keybuff,16);
79  gcm_init_64k(m_pGCMCtx,keybuff,128);
80  UINT8 encKey[2048];
81  UINT32 encKeyLen=2048;
82 //Set date
83  keybuff[16]=m_Day;
84  keybuff[17]=m_Month;
85  keybuff[18]=m_Year>>8;
86  keybuff[19]=(m_Year&0x00FF);
87 //Calculate MAC
88  UINT8 nonce[12];
89  memset(nonce,0xFF,11);
90  nonce[11]=0xFE;
91  ::gcm_encrypt_64k(m_pGCMCtx, nonce, 12, keybuff,20,
92  NULL,0,encKey,keybuff+20);
93 
94  encKeyLen=256;
95  pPublicKey->encryptPKCS1(keybuff,36,encKey,&encKeyLen);
96 
97 //Calculate the symmetric key
98  UINT8 md[SHA512_DIGEST_LENGTH];
99  SHA512(keybuff+16,4,md);
100  for(UINT32 i=0;i<16;i++)
101  {
102  keybuff[i]^=md[i];
103  }
104  gcm_destroy_64k(m_pGCMCtx);
105  gcm_init_64k(m_pGCMCtx,keybuff,128);
106 
107  memset(keybuff,0,256);
108 
109  if(write(m_hLogFile,encKey,encKeyLen)!=encKeyLen)
110  return E_UNKNOWN;
111 
112  //Calculate Auth tag and writ it..
113  UINT8 tmpBuff[2048];
114  nonce[11]=0xFD;
115  memcpy(tmpBuff,&oHeader,sizeof(oHeader));
116  memcpy(tmpBuff+sizeof(oHeader),encKey,encKeyLen);
117  ::gcm_encrypt_64k(m_pGCMCtx, nonce, 12, tmpBuff,sizeof(oHeader)+encKeyLen,
118  NULL,0,tmpBuff+1024,keybuff);
119 
120  if(write(m_hLogFile,keybuff,16)!=16)
121  return E_UNKNOWN;
122 
123  memset(m_nonceBuffForLogEntries,0,12);
124 
125 
126  return E_SUCCESS;
127  }
128 
130 {
132  {//Writte remaining log entries
133  UINT32 nonce=htonl(m_nCurrentBlockNumber);
134  memcpy(m_nonceBuffForLogEntries+8,&nonce,4);
138  {
139  CAMsg::printMsg(LOG_ERR,"Error: data retention log entry was not fully written to disk!\n");
140  return E_UNKNOWN;
141  }
142  }
143  return E_SUCCESS;
144 }
145 
147  {
148  SINT32 ret=flushLogEntries();
149  if(ret!=E_SUCCESS)
150  return E_UNKNOWN;
152  UINT8 out[32];
153  UINT8 nonce[12];
154  memset(nonce,0xFF,12);
155  ::gcm_encrypt_64k(m_pGCMCtx, nonce ,12,(UINT8*) &u,4,
156  NULL,0,out,out+4);
157  if(write(m_hLogFile,out,20)!=20)
158  return E_UNKNOWN;
159  return E_SUCCESS;
160  }
161 
163  {
164  if(m_hLogFile==-1)
165  return E_SUCCESS;
166  SINT32 ret=writeFooter();
167  if(ret!=E_SUCCESS)
168  return ret;
169  ret=close(m_hLogFile);
170  m_hLogFile=-1;
171  if(ret==0)
172  return E_SUCCESS;
173  return E_UNKNOWN;
174  }
175 
176 SINT32 CADataRetentionLogFile::log(t_dataretentionLogEntry* logEntry)
177  {
178  SINT32 ret=E_SUCCESS;
181 
183  {//Block is full -->encrypt and write them
184  UINT32 nonce=htonl(m_nCurrentBlockNumber);
185  memcpy(m_nonceBuffForLogEntries+8,&nonce,4);
189  {
190  CAMsg::printMsg(LOG_ERR,"Error: data retention log entry was not fully written to disk!\n");
191  ret=E_UNKNOWN;
192  }
195  }
196  return ret;
197  }
198 
200 {
201  UINT8 keybuff[256];
202  UINT8 tag[128];
203  UINT8 nonce[12];
204  memset(nonce,0xFF,12);
205  UINT8 oneBlock[8192];
206  memset(oneBlock,0xd4,8192);
207  UINT8 encBlock[10000];
208  memset(keybuff,0xC1,16);
209  UINT32 lenBlock=64;
210  UINT64 start;
211  const UINT32 runs=100000;
212  gcm_ctx_4k* pGCMCtx=new gcm_ctx_4k;
213 
214  for(int l=0;l<7;l++)
215  {
216  gcm_init_4k(pGCMCtx,keybuff,128);
217  getcurrentTimeMillis(start);
218  for(UINT32 i =0;i<runs;i++)
219  ::gcm_encrypt_4k(pGCMCtx, nonce ,12, oneBlock,lenBlock,NULL,0,encBlock,tag);
220  UINT64 end;
222  print64(encBlock,diff64(end,start));
223  double bytes=(double)(runs*lenBlock);
224  double thetime=(double)diff64(end,start);
225  printf("Time for %u run of 4k encypt of %u bytes: %s [ms] (%f bytes/s)\n",runs,lenBlock,encBlock,bytes/thetime*1000.0);
226  lenBlock<<=1;
227  }
228  gcm_ctx_64k* pGCMCtx64=new gcm_ctx_64k;
229  lenBlock=64;
230  UINT8 tmpBlock[8192];
231 
232  for(int l=0;l<7;l++)
233  {
234  gcm_init_64k(pGCMCtx64,keybuff,128);
235  getcurrentTimeMillis(start);
236  for(UINT32 i =0;i<runs;i++)
237  ::gcm_encrypt_64k(pGCMCtx64, nonce ,12, oneBlock,lenBlock,NULL,0,encBlock,tag);
238  UINT64 end;
240  print64(tmpBlock,diff64(end,start));
241  double bytes=(double)(runs*lenBlock);
242  double thetime=(double)diff64(end,start);
243  printf("Time for %u run of 64k encypt of %u bytes: %s [ms] (%f bytes/s)\n",runs,lenBlock,tmpBlock,bytes/thetime*1000.0);
244  lenBlock<<=1;
245  }
246  lenBlock>>=1;
247  printf("Test finished!\n");
248 
249  memset(tmpBlock,1,8192);
250  if(gcm_decrypt_4k(pGCMCtx,nonce,12,encBlock,lenBlock,tag,16,NULL,0,tmpBlock)==0||memcmp(oneBlock,tmpBlock,lenBlock)!=0)
251  printf("Check failed!\n");
252  else
253  printf("Check success!\n");
254 
255  printf("Check finished!\n");
256  return E_SUCCESS;
257 }
258 
259 #endif //DATA_RETENTION_LOG
const UINT8 DATARETENTION_ENTITY_MIDDLE_MIX
const UINT8 DATARETENTION_ENTITY_LAST_MIX
const UINT8 DATARETENTION_ENTITY_FIRST_MIX
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
Definition: CAUtil.cpp:346
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
Definition: CAUtil.hpp:398
void print64(UINT8 *buff, UINT64 num)
Definition: CAUtil.hpp:482
#define O_LARGEFILE
Definition: StdAfx.h:635
#define S_IWRITE
Definition: StdAfx.h:491
#define S_IREAD
Definition: StdAfx.h:488
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 encryptPKCS1(const UINT8 *from, UINT32 fromlen, UINT8 *to, UINT32 *len)
Encrypts one block of plain text using PKCS1 padding.
SINT32 log(t_dataretentionLogEntry *)
SINT32 openLog(UINT8 *strLogDir, UINT32 date, CAASymCipher *pPublicKey)
static SINT32 doCheckAndPerformanceTest()
SINT32 writeHeader(CAASymCipher *pPublicKey)
static CACmdLnOptions * getOptions()
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
#define O_BINARY
Definition: config.h:179
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3