Mixe for Privacy and Anonymity in the Internet
CACryptoBenchmark.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #include "StdAfx.h"
29 #include "CACryptoBenchmark.hpp"
30 #include "CAThread.hpp"
31 #include "CAThreadList.hpp"
32 #include "CASymCipherCTR.hpp"
33 #include "CASymCipherOFB.hpp"
34 #include "CAUtil.hpp"
35 
36 
37 const UINT32 CACryptoBenchmark::ms_BlockSizes[ms_NUM_BLOCKSIZES] = { 16,64,256,512,1024 };
38 
40 {
41 }
42 
43 
45 {
46 }
47 
49 {
50  CAThreadList* pThreadList=new CAThreadList();
51  CAMutex* pMutexTotlaBytes = new CAMutex();
52  volatile UINT64 totalBytesProcessed;
53  for (UINT32 iBlocksize = 0; iBlocksize < ms_NUM_BLOCKSIZES; iBlocksize++)
54  {
55  for (UINT32 numThreads = 1; numThreads <= MAX_CRYPTOBENCHMARK_THREADS; numThreads += 4)
56  {
57  CAMsg::printMsg(LOG_DEBUG, "Do crypto benchmark for %u threads\n", numThreads);
58  totalBytesProcessed = 0;
59  UINT64 startTime, endTime, time;
60  getcurrentTimeMillis(startTime);
61  for (UINT32 nThreads = 0; nThreads < numThreads; nThreads++)
62  {
63  tBenchmarkParams* pParams = new tBenchmarkParams;
64  pParams->blocksize = ms_BlockSizes[iBlocksize];
65  pParams->pMutexTotalBytesProcessed = pMutexTotlaBytes;
66  pParams->outparam_pTotalBytesProcessed = &totalBytesProcessed;
67  CAThread* pThread = new CAThread();
68  pThread->setMainLoop(benchmarkThread);
69  pThread->start(pParams, false, true);
70  pThreadList->put(pThread);
71  }
72  pThreadList->waitAndRemoveAll();
73  getcurrentTimeMillis(endTime);
74  time = diff64(endTime, startTime);
75  UINT8 tmpbuff[256];
76  formatBytesPerSecond(tmpbuff, 256, (totalBytesProcessed * 1000) / time);
77  CAMsg::printMsg(LOG_DEBUG, "Total crypto speed (blocksize: %u): %llu bytes in %u ms -- %s\n", ms_BlockSizes[iBlocksize],totalBytesProcessed, (UINT32)time,tmpbuff);
78 
79  numThreads -= (numThreads % 4); // Produce a series like: 1,4,8,12,...
80  }
81  }
82  delete pThreadList;
83  delete pMutexTotlaBytes;
84 }
85 
87 {
88  tBenchmarkTimerParams* pTimerParams = (tBenchmarkTimerParams*)a_pParams;
89  msSleep(pTimerParams->timerInMillis);
90  *(pTimerParams->bTimer) = false;
92 }
93 
95 {
96  tBenchmarkParams* pParams = (tBenchmarkParams*)a_pParams;
97  CASymChannelCipher* pCipher = new CASymCipherCTR();
98  UINT8 iv[32];
99  UINT8 key[32];
100  pCipher->setKeys(key, 32);
101  pCipher->setIVs(iv);
102  volatile bool bTimer = true;
103  CAThread* pTimerThread = new CAThread();
104  pTimerThread->setMainLoop(timerThread);
105  tBenchmarkTimerParams* pTimerParams = new tBenchmarkTimerParams();
106  pTimerParams->timerInMillis = 3000;
107  pTimerParams->bTimer = &bTimer;
108  UINT32 blocksize = pParams->blocksize;
109  UINT8* in = new UINT8[blocksize];
110  UINT8* out = new UINT8[blocksize];
111  UINT64 numCrypts = 0;
112  UINT64 startTime, endTime, time;
113  pTimerThread->start(pTimerParams,true,true);
114  getcurrentTimeMillis(startTime);
115  while (bTimer)
116  {
117  pCipher->crypt1(in, out, blocksize);
118  numCrypts++;
119  }
120  getcurrentTimeMillis(endTime);
121  time =diff64( endTime,startTime);
122  pParams->pMutexTotalBytesProcessed->lock();
123  *(pParams->outparam_pTotalBytesProcessed) += blocksize * numCrypts;
124  pParams->pMutexTotalBytesProcessed->unlock();
125  UINT64 bytes = numCrypts * 1000 * blocksize;
126  UINT8 tmpbuff[256];
127  formatBytesPerSecond(tmpbuff, 256, bytes / time);
128 
129  CAMsg::printMsg(LOG_DEBUG,"Did %llu crypts in %u ms - blocksize %u - %s\n", numCrypts,(UINT32) time, blocksize, tmpbuff);
130  delete pCipher;
131  delete pTimerThread;
132  delete pTimerParams;
133  delete[] in;
134  delete[] out;
136 
137 }
#define MAX_CRYPTOBENCHMARK_THREADS
struct t_benchmarkParams tBenchmarkParams
struct t_benchmarkTimerParams tBenchmarkTimerParams
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
void formatBytesPerSecond(UINT8 *buff, UINT32 buffsize, UINT64 bytespersecond)
Definition: CAUtil.cpp:1821
SINT32 msSleep(UINT32 ms)
Sleeps ms milliseconds.
Definition: CAUtil.cpp:406
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
Definition: CAUtil.hpp:398
#define THREAD_RETURN
Definition: StdAfx.h:540
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static const UINT32 ms_NUM_BLOCKSIZES
static THREAD_RETURN timerThread(void *param)
static const UINT32 ms_BlockSizes[ms_NUM_BLOCKSIZES]
static THREAD_RETURN benchmarkThread(void *param)
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
virtual SINT32 setKeys(const UINT8 *key, UINT32 keysize)=0
Sets the keys for crypt1() and crypt2() either to the same key (if keysize==KEY_SIZE) or to different...
virtual SINT32 setIVs(const UINT8 *p_iv)=0
Sets iv1 and iv2 to p_iv.
virtual SINT32 crypt1(const UINT8 *in, UINT8 *out, UINT32 len)=0
This class could be used for encryption/decryption of data (streams) with AES using 128bit CTR mode.
SINT32 start(void *param, bool bDaemon=false, bool bSilent=false)
Starts the execution of the main function of this thread.
Definition: CAThread.cpp:115
SINT32 setMainLoop(THREAD_MAIN_TYP fnc)
Sets the main function which will be executed within this thread.
Definition: CAThread.hpp:148
SINT32 waitAndRemoveAll()
Waits for finishing of all threads in list and removes them.
SINT32 put(const CAThread *const thread)
volatile UINT64 * outparam_pTotalBytesProcessed
CAMutex * pMutexTotalBytesProcessed